blob: f0252c88c08cb5bf4a189f4467966927cac7766e [file] [log] [blame]
Gilles Peskine75976892018-12-12 15:55:09 +01001/**
2 * \file psa/crypto_se_driver.h
3 * \brief PSA external cryptoprocessor driver module
4 *
5 * This header declares types and function signatures for cryptography
Derek Millerf0c1d0d2019-02-15 17:23:42 -06006 * drivers that access key material via opaque references.
7 * This is meant for cryptoprocessors that have a separate key storage from the
Gilles Peskine75976892018-12-12 15:55:09 +01008 * space in which the PSA Crypto implementation runs, typically secure
Derek Millerf0c1d0d2019-02-15 17:23:42 -06009 * elements (SEs).
Gilles Peskine75976892018-12-12 15:55:09 +010010 *
Gilles Peskineb6cadea2019-06-24 13:46:37 +020011 * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer),
12 * containing functions for driver developers to implement to enable hardware
13 * to be called in a standardized way by a PSA Cryptography API
14 * implementation. The functions comprising the driver HAL, which driver
15 * authors implement, are not intended to be called by application developers.
Gilles Peskine75976892018-12-12 15:55:09 +010016 */
17
18/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020019 * Copyright The Mbed TLS Contributors
Gilles Peskine75976892018-12-12 15:55:09 +010020 * SPDX-License-Identifier: Apache-2.0
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34#ifndef PSA_CRYPTO_SE_DRIVER_H
35#define PSA_CRYPTO_SE_DRIVER_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020036#include "mbedtls/private_access.h"
Gilles Peskine75976892018-12-12 15:55:09 +010037
38#include "crypto_driver_common.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Gilles Peskine7a86da12019-07-12 23:25:38 +020044/** \defgroup se_init Secure element driver initialization
45 */
46/**@{*/
47
48/** \brief Driver context structure
49 *
50 * Driver functions receive a pointer to this structure.
51 * Each registered driver has one instance of this structure.
52 *
53 * Implementations must include the fields specified here and
54 * may include other fields.
55 */
56typedef struct {
57 /** A read-only pointer to the driver's persistent data.
58 *
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020059 * Drivers typically use this persistent data to keep track of
60 * which slot numbers are available. This is only a guideline:
61 * drivers may use the persistent data for any purpose, keeping
62 * in mind the restrictions on when the persistent data is saved
63 * to storage: the persistent data is only saved after calling
64 * certain functions that receive a writable pointer to the
65 * persistent data.
Gilles Peskine7a86da12019-07-12 23:25:38 +020066 *
67 * The core allocates a memory buffer for the persistent data.
Gilles Peskine6a3dd892019-07-25 10:56:39 +020068 * The pointer is guaranteed to be suitably aligned for any data type,
Gilles Peskine7a86da12019-07-12 23:25:38 +020069 * like a pointer returned by `malloc` (but the core can use any
70 * method to allocate the buffer, not necessarily `malloc`).
71 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +020072 * The size of this buffer is in the \c persistent_data_size field of
73 * this structure.
Gilles Peskine7a86da12019-07-12 23:25:38 +020074 *
75 * Before the driver is initialized for the first time, the content of
76 * the persistent data is all-bits-zero. After a driver upgrade, if the
77 * size of the persistent data has increased, the original data is padded
78 * on the right with zeros; if the size has decreased, the original data
79 * is truncated to the new size.
80 *
81 * This pointer is to read-only data. Only a few driver functions are
82 * allowed to modify the persistent data. These functions receive a
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020083 * writable pointer. These functions are:
84 * - psa_drv_se_t::p_init
85 * - psa_drv_se_key_management_t::p_allocate
86 * - psa_drv_se_key_management_t::p_destroy
87 *
88 * The PSA Cryptography core saves the persistent data from one
89 * session to the next. It does this before returning from API functions
90 * that call a driver method that is allowed to modify the persistent
91 * data, specifically:
92 * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
93 * psa_drv_se_key_management_t::p_destroy to complete an action
94 * that was interrupted by a power failure.
95 * - Key creation functions cause a call to
96 * psa_drv_se_key_management_t::p_allocate, and may cause a call to
97 * psa_drv_se_key_management_t::p_destroy in case an error occurs.
98 * - psa_destroy_key() causes a call to
99 * psa_drv_se_key_management_t::p_destroy.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200100 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 const void *const MBEDTLS_PRIVATE(persistent_data);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200102
103 /** The size of \c persistent_data in bytes.
104 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +0200105 * This is always equal to the value of the `persistent_data_size` field
106 * of the ::psa_drv_se_t structure when the driver is registered.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200107 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200108 const size_t MBEDTLS_PRIVATE(persistent_data_size);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200109
110 /** Driver transient data.
111 *
112 * The core initializes this value to 0 and does not read or modify it
113 * afterwards. The driver may store whatever it wants in this field.
114 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200115 uintptr_t MBEDTLS_PRIVATE(transient_data);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200116} psa_drv_se_context_t;
117
118/** \brief A driver initialization function.
119 *
120 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200121 * \param[in,out] persistent_data A pointer to the persistent data
122 * that allows writing.
Gilles Peskine52ac9582020-05-10 00:39:18 +0200123 * \param location The location value for which this driver
124 * is registered. The driver will be invoked
125 * for all keys whose lifetime is in this
126 * location.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200127 *
128 * \retval #PSA_SUCCESS
129 * The driver is operational.
130 * The core will update the persistent data in storage.
131 * \return
132 * Any other return value prevents the driver from being used in
133 * this session.
134 * The core will NOT update the persistent data in storage.
135 */
136typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200137 void *persistent_data,
Gilles Peskine52ac9582020-05-10 00:39:18 +0200138 psa_key_location_t location);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200139
Gilles Peskinec8000c02019-08-02 20:15:51 +0200140#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
141/* Mbed Crypto with secure element support enabled defines this type in
142 * crypto_types.h because it is also visible to applications through an
143 * implementation-specific extension.
144 * For the PSA Cryptography specification, this type is only visible
145 * via crypto_se_driver.h. */
Gilles Peskine75976892018-12-12 15:55:09 +0100146/** An internal designation of a key slot between the core part of the
147 * PSA Crypto implementation and the driver. The meaning of this value
148 * is driver-dependent. */
Gilles Peskinef03143a2019-07-12 23:18:29 +0200149typedef uint64_t psa_key_slot_number_t;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200150#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine75976892018-12-12 15:55:09 +0100151
Gilles Peskine7a86da12019-07-12 23:25:38 +0200152/**@}*/
153
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600154/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +0100155 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600156 * a secure element can be done either as a single function call (via the
157 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +0100158 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600159 * - `psa_drv_se_mac_setup_t`
160 * - `psa_drv_se_mac_update_t`
161 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100162 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600163 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100164 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600165 * If a previously started secure element MAC operation needs to be terminated,
166 * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may
Gilles Peskine75976892018-12-12 15:55:09 +0100167 * result in allocated resources not being freed or in other undefined
168 * behavior.
169 */
170/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600171/** \brief A function that starts a secure element MAC operation for a PSA
172 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +0100173 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200174 * \param[in,out] drv_context The driver context structure.
175 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100176 * hardware-specific MAC context
177 * \param[in] key_slot The slot of the key to be used for the
178 * operation
179 * \param[in] algorithm The algorithm to be used to underly the MAC
180 * operation
181 *
Ronald Cron96783552020-10-19 12:06:30 +0200182 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100183 * Success.
184 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200185typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
186 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600187 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600188 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100189
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600190/** \brief A function that continues a previously started secure element MAC
191 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100192 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200193 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100194 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600195 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100196 * \param[in] p_input A buffer containing the message to be appended
197 * to the MAC operation
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200198 * \param[in] input_length The size in bytes of the input message buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100199 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200200typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600201 const uint8_t *p_input,
202 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100203
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600204/** \brief a function that completes a previously started secure element MAC
205 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100206 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200207 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100208 * previously started MAC operation to be
209 * finished
210 * \param[out] p_mac A buffer where the generated MAC will be
211 * placed
212 * \param[in] mac_size The size in bytes of the buffer that has been
213 * allocated for the `output` buffer
214 * \param[out] p_mac_length After completion, will contain the number of
215 * bytes placed in the `p_mac` buffer
216 *
Ronald Cron96783552020-10-19 12:06:30 +0200217 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100218 * Success.
219 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200220typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600221 uint8_t *p_mac,
222 size_t mac_size,
223 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100224
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600225/** \brief A function that completes a previously started secure element MAC
226 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100227 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200228 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200229 * started MAC operation to be fiinished
230 * \param[in] p_mac The MAC value against which the resulting MAC
231 * will be compared against
232 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Gilles Peskine75976892018-12-12 15:55:09 +0100233 *
Ronald Cron96783552020-10-19 12:06:30 +0200234 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100235 * The operation completed successfully and the MACs matched each
236 * other
Ronald Cron96783552020-10-19 12:06:30 +0200237 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine75976892018-12-12 15:55:09 +0100238 * The operation completed successfully, but the calculated MAC did
239 * not match the provided MAC
240 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200241typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600242 const uint8_t *p_mac,
243 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100244
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600245/** \brief A function that aborts a previous started secure element MAC
246 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100247 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200248 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200249 * started MAC operation to be aborted
Gilles Peskine75976892018-12-12 15:55:09 +0100250 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200251typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100252
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600253/** \brief A function that performs a secure element MAC operation in one
254 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100255 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200256 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100257 * \param[in] p_input A buffer containing the message to be MACed
258 * \param[in] input_length The size in bytes of `p_input`
259 * \param[in] key_slot The slot of the key to be used
260 * \param[in] alg The algorithm to be used to underlie the MAC
261 * operation
262 * \param[out] p_mac A buffer where the generated MAC will be
263 * placed
264 * \param[in] mac_size The size in bytes of the `p_mac` buffer
265 * \param[out] p_mac_length After completion, will contain the number of
266 * bytes placed in the `output` buffer
267 *
Ronald Cron96783552020-10-19 12:06:30 +0200268 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100269 * Success.
270 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200271typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
272 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600273 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600274 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600275 psa_algorithm_t alg,
276 uint8_t *p_mac,
277 size_t mac_size,
278 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100279
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600280/** \brief A function that performs a secure element MAC operation in one
281 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100282 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200283 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100284 * \param[in] p_input A buffer containing the message to be MACed
285 * \param[in] input_length The size in bytes of `input`
286 * \param[in] key_slot The slot of the key to be used
287 * \param[in] alg The algorithm to be used to underlie the MAC
288 * operation
289 * \param[in] p_mac The MAC value against which the resulting MAC will
290 * be compared against
291 * \param[in] mac_length The size in bytes of `mac`
292 *
Ronald Cron96783552020-10-19 12:06:30 +0200293 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100294 * The operation completed successfully and the MACs matched each
295 * other
Ronald Cron96783552020-10-19 12:06:30 +0200296 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine75976892018-12-12 15:55:09 +0100297 * The operation completed successfully, but the calculated MAC did
298 * not match the provided MAC
299 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200300typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
301 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600302 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600303 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600304 psa_algorithm_t alg,
305 const uint8_t *p_mac,
306 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100307
308/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600309 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100310 *
311 * PSA Crypto API implementations should populate the table as appropriate
312 * upon startup.
313 *
314 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600315 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100316 *
317 * Driver implementers should ensure that they implement all of the functions
318 * that make sense for their hardware, and that they provide a full solution
319 * (for example, if they support `p_setup`, they should also support
320 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
321 *
322 */
323typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600324 /**The size in bytes of the hardware-specific secure element MAC context
325 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100326 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200327 size_t MBEDTLS_PRIVATE(context_size);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600328 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100329 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200330 psa_drv_se_mac_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600331 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100332 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200333 psa_drv_se_mac_update_t MBEDTLS_PRIVATE(p_update);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600334 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100335 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200336 psa_drv_se_mac_finish_t MBEDTLS_PRIVATE(p_finish);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600337 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100338 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200339 psa_drv_se_mac_finish_verify_t MBEDTLS_PRIVATE(p_finish_verify);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600340 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100341 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200342 psa_drv_se_mac_abort_t MBEDTLS_PRIVATE(p_abort);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600343 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100344 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200345 psa_drv_se_mac_generate_t MBEDTLS_PRIVATE(p_mac);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600346 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100347 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200348 psa_drv_se_mac_verify_t MBEDTLS_PRIVATE(p_mac_verify);
Derek Miller83d26622019-02-15 16:41:22 -0600349} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100350/**@}*/
351
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600352/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100353 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600354 * Encryption and Decryption using secure element keys in block modes other
355 * than ECB must be done in multiple parts, using the following flow:
356 * - `psa_drv_se_cipher_setup_t`
357 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
358 * - `psa_drv_se_cipher_update_t`
359 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100360 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600361 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100362 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600363 * If a previously started secure element Cipher operation needs to be
364 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
365 * to do so may result in allocated resources not being freed or in other
366 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100367 *
368 * In situations where a PSA Cryptographic API implementation is using a block
369 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600370 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
371 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100372 */
373/**@{*/
374
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600375/** \brief A function that provides the cipher setup function for a
376 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100377 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200378 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +0200379 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100380 * hardware-specific cipher context.
381 * \param[in] key_slot The slot of the key to be used for the
382 * operation
383 * \param[in] algorithm The algorithm to be used in the cipher
384 * operation
385 * \param[in] direction Indicates whether the operation is an encrypt
386 * or decrypt
387 *
Ronald Cron96783552020-10-19 12:06:30 +0200388 * \retval #PSA_SUCCESS
389 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine75976892018-12-12 15:55:09 +0100390 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200391typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
392 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600393 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600394 psa_algorithm_t algorithm,
395 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100396
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600397/** \brief A function that sets the initialization vector (if
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100398 * necessary) for a secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100399 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600400 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
401 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100402 * generate function is not necessary for the drivers to implement as the PSA
403 * Crypto implementation can do the generation using its RNG features.
404 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200405 * \param[in,out] op_context A structure that contains the previously set up
Gilles Peskine75976892018-12-12 15:55:09 +0100406 * hardware-specific cipher context
407 * \param[in] p_iv A buffer containing the initialization vector
408 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
409 *
Ronald Cron96783552020-10-19 12:06:30 +0200410 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100411 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200412typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600413 const uint8_t *p_iv,
414 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100415
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600416/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100417 * operation
418 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200419 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100420 * previously started cipher operation
421 * \param[in] p_input A buffer containing the data to be
422 * encrypted/decrypted
423 * \param[in] input_size The size in bytes of the buffer pointed to
424 * by `p_input`
425 * \param[out] p_output The caller-allocated buffer where the
426 * output will be placed
427 * \param[in] output_size The allocated size in bytes of the
428 * `p_output` buffer
429 * \param[out] p_output_length After completion, will contain the number
430 * of bytes placed in the `p_output` buffer
431 *
Ronald Cron96783552020-10-19 12:06:30 +0200432 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100433 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200434typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600435 const uint8_t *p_input,
436 size_t input_size,
437 uint8_t *p_output,
438 size_t output_size,
439 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100440
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600441/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100442 * operation
443 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200444 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100445 * previously started cipher operation
446 * \param[out] p_output The caller-allocated buffer where the output
447 * will be placed
448 * \param[in] output_size The allocated size in bytes of the `p_output`
449 * buffer
450 * \param[out] p_output_length After completion, will contain the number of
451 * bytes placed in the `p_output` buffer
452 *
Ronald Cron96783552020-10-19 12:06:30 +0200453 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100454 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200455typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600456 uint8_t *p_output,
457 size_t output_size,
458 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100459
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600460/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100461 * operation
462 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200463 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100464 * previously started cipher operation
465 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200466typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100467
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600468/** \brief A function that performs the ECB block mode for secure element
469 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100470 *
471 * Note: this function should only be used with implementations that do not
472 * provide a needed higher-level operation.
473 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200474 * \param[in,out] drv_context The driver context structure.
475 * \param[in] key_slot The slot of the key to be used for the operation
476 * \param[in] algorithm The algorithm to be used in the cipher operation
477 * \param[in] direction Indicates whether the operation is an encrypt or
478 * decrypt
479 * \param[in] p_input A buffer containing the data to be
480 * encrypted/decrypted
481 * \param[in] input_size The size in bytes of the buffer pointed to by
482 * `p_input`
483 * \param[out] p_output The caller-allocated buffer where the output
484 * will be placed
485 * \param[in] output_size The allocated size in bytes of the `p_output`
486 * buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100487 *
Ronald Cron96783552020-10-19 12:06:30 +0200488 * \retval #PSA_SUCCESS
489 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine75976892018-12-12 15:55:09 +0100490 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200491typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
492 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600493 psa_algorithm_t algorithm,
494 psa_encrypt_or_decrypt_t direction,
495 const uint8_t *p_input,
496 size_t input_size,
497 uint8_t *p_output,
498 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100499
500/**
501 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600502 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100503 *
504 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600505 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100506 *
507 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600508 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100509 */
510typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600511 /** The size in bytes of the hardware-specific secure element cipher
512 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100513 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200514 size_t MBEDTLS_PRIVATE(context_size);
Derek Miller34b33f12019-02-15 17:13:54 -0600515 /** Function that performs a cipher setup operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200516 psa_drv_se_cipher_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600517 /** Function that sets a cipher IV (if necessary) */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200518 psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600519 /** Function that performs a cipher update operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200520 psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600521 /** Function that completes a cipher operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200522 psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600523 /** Function that aborts a cipher operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200524 psa_drv_se_cipher_abort_t MBEDTLS_PRIVATE(p_abort);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600525 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100526 * (Danger: ECB mode should not be used directly by clients of the PSA
527 * Crypto Client API)
528 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200529 psa_drv_se_cipher_ecb_t MBEDTLS_PRIVATE(p_ecb);
Derek Miller83d26622019-02-15 16:41:22 -0600530} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100531
532/**@}*/
533
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600534/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100535 *
536 * Since the amount of data that can (or should) be encrypted or signed using
537 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600538 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100539 */
540/**@{*/
541
542/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600543 * \brief A function that signs a hash or short message with a private key in
544 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100545 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200546 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100547 * \param[in] key_slot Key slot of an asymmetric key pair
548 * \param[in] alg A signature algorithm that is compatible
549 * with the type of `key`
550 * \param[in] p_hash The hash to sign
551 * \param[in] hash_length Size of the `p_hash` buffer in bytes
552 * \param[out] p_signature Buffer where the signature is to be written
553 * \param[in] signature_size Size of the `p_signature` buffer in bytes
554 * \param[out] p_signature_length On success, the number of bytes
555 * that make up the returned signature value
556 *
Ronald Cron96783552020-10-19 12:06:30 +0200557 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100558 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200559typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
560 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600561 psa_algorithm_t alg,
562 const uint8_t *p_hash,
563 size_t hash_length,
564 uint8_t *p_signature,
565 size_t signature_size,
566 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100567
568/**
569 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600570 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100571 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200572 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100573 * \param[in] key_slot Key slot of a public key or an asymmetric key
574 * pair
575 * \param[in] alg A signature algorithm that is compatible with
576 * the type of `key`
577 * \param[in] p_hash The hash whose signature is to be verified
578 * \param[in] hash_length Size of the `p_hash` buffer in bytes
579 * \param[in] p_signature Buffer containing the signature to verify
580 * \param[in] signature_length Size of the `p_signature` buffer in bytes
581 *
Ronald Cron96783552020-10-19 12:06:30 +0200582 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100583 * The signature is valid.
584 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200585typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
586 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600587 psa_algorithm_t alg,
588 const uint8_t *p_hash,
589 size_t hash_length,
590 const uint8_t *p_signature,
591 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100592
593/**
594 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600595 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100596 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200597 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100598 * \param[in] key_slot Key slot of a public key or an asymmetric key
599 * pair
600 * \param[in] alg An asymmetric encryption algorithm that is
601 * compatible with the type of `key`
602 * \param[in] p_input The message to encrypt
603 * \param[in] input_length Size of the `p_input` buffer in bytes
604 * \param[in] p_salt A salt or label, if supported by the
605 * encryption algorithm
606 * If the algorithm does not support a
607 * salt, pass `NULL`.
608 * If the algorithm supports an optional
609 * salt and you do not want to pass a salt,
610 * pass `NULL`.
611 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
612 * supported.
613 * \param[in] salt_length Size of the `p_salt` buffer in bytes
614 * If `p_salt` is `NULL`, pass 0.
615 * \param[out] p_output Buffer where the encrypted message is to
616 * be written
617 * \param[in] output_size Size of the `p_output` buffer in bytes
618 * \param[out] p_output_length On success, the number of bytes that make up
619 * the returned output
620 *
Ronald Cron96783552020-10-19 12:06:30 +0200621 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100622 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200623typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
624 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600625 psa_algorithm_t alg,
626 const uint8_t *p_input,
627 size_t input_length,
628 const uint8_t *p_salt,
629 size_t salt_length,
630 uint8_t *p_output,
631 size_t output_size,
632 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100633
634/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600635 * \brief A function that decrypts a short message with an asymmetric private
636 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100637 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200638 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100639 * \param[in] key_slot Key slot of an asymmetric key pair
640 * \param[in] alg An asymmetric encryption algorithm that is
641 * compatible with the type of `key`
642 * \param[in] p_input The message to decrypt
643 * \param[in] input_length Size of the `p_input` buffer in bytes
644 * \param[in] p_salt A salt or label, if supported by the
645 * encryption algorithm
646 * If the algorithm does not support a
647 * salt, pass `NULL`.
648 * If the algorithm supports an optional
649 * salt and you do not want to pass a salt,
650 * pass `NULL`.
651 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
652 * supported.
653 * \param[in] salt_length Size of the `p_salt` buffer in bytes
654 * If `p_salt` is `NULL`, pass 0.
655 * \param[out] p_output Buffer where the decrypted message is to
656 * be written
657 * \param[in] output_size Size of the `p_output` buffer in bytes
658 * \param[out] p_output_length On success, the number of bytes
659 * that make up the returned output
660 *
Ronald Cron96783552020-10-19 12:06:30 +0200661 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100662 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200663typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
664 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600665 psa_algorithm_t alg,
666 const uint8_t *p_input,
667 size_t input_length,
668 const uint8_t *p_salt,
669 size_t salt_length,
670 uint8_t *p_output,
671 size_t output_size,
672 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100673
674/**
675 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600676 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100677 *
678 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600679 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100680 *
681 * If one of the functions is not implemented, it should be set to NULL.
682 */
683typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600684 /** Function that performs an asymmetric sign operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200685 psa_drv_se_asymmetric_sign_t MBEDTLS_PRIVATE(p_sign);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600686 /** Function that performs an asymmetric verify operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200687 psa_drv_se_asymmetric_verify_t MBEDTLS_PRIVATE(p_verify);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600688 /** Function that performs an asymmetric encrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200689 psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600690 /** Function that performs an asymmetric decrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200691 psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
Derek Miller83d26622019-02-15 16:41:22 -0600692} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100693
694/**@}*/
695
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600696/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
697 * Authenticated Encryption with Additional Data (AEAD) operations with secure
698 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100699 * implementers as there must be sufficient space in memory for the entire
700 * message, it prevents decrypted data from being made available before the
701 * authentication operation is complete and the data is known to be authentic.
702 */
703/**@{*/
704
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600705/** \brief A function that performs a secure element authenticated encryption
706 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100707 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200708 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100709 * \param[in] key_slot Slot containing the key to use.
710 * \param[in] algorithm The AEAD algorithm to compute
711 * (\c PSA_ALG_XXX value such that
712 * #PSA_ALG_IS_AEAD(`alg`) is true)
713 * \param[in] p_nonce Nonce or IV to use
714 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
715 * \param[in] p_additional_data Additional data that will be
716 * authenticated but not encrypted
717 * \param[in] additional_data_length Size of `p_additional_data` in bytes
718 * \param[in] p_plaintext Data that will be authenticated and
719 * encrypted
720 * \param[in] plaintext_length Size of `p_plaintext` in bytes
721 * \param[out] p_ciphertext Output buffer for the authenticated and
722 * encrypted data. The additional data is
723 * not part of this output. For algorithms
724 * where the encrypted data and the
725 * authentication tag are defined as
726 * separate outputs, the authentication
727 * tag is appended to the encrypted data.
728 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
729 * bytes
730 * \param[out] p_ciphertext_length On success, the size of the output in
731 * the `p_ciphertext` buffer
732 *
733 * \retval #PSA_SUCCESS
734 * Success.
735 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200736typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
737 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600738 psa_algorithm_t algorithm,
739 const uint8_t *p_nonce,
740 size_t nonce_length,
741 const uint8_t *p_additional_data,
742 size_t additional_data_length,
743 const uint8_t *p_plaintext,
744 size_t plaintext_length,
745 uint8_t *p_ciphertext,
746 size_t ciphertext_size,
747 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100748
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600749/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100750 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200751 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100752 * \param[in] key_slot Slot containing the key to use
753 * \param[in] algorithm The AEAD algorithm to compute
754 * (\c PSA_ALG_XXX value such that
755 * #PSA_ALG_IS_AEAD(`alg`) is true)
756 * \param[in] p_nonce Nonce or IV to use
757 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
758 * \param[in] p_additional_data Additional data that has been
759 * authenticated but not encrypted
760 * \param[in] additional_data_length Size of `p_additional_data` in bytes
761 * \param[in] p_ciphertext Data that has been authenticated and
762 * encrypted.
763 * For algorithms where the encrypted data
764 * and the authentication tag are defined
765 * as separate inputs, the buffer must
766 * contain the encrypted data followed by
767 * the authentication tag.
768 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
769 * \param[out] p_plaintext Output buffer for the decrypted data
770 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
771 * bytes
772 * \param[out] p_plaintext_length On success, the size of the output in
773 * the `p_plaintext` buffer
774 *
775 * \retval #PSA_SUCCESS
776 * Success.
777 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200778typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
779 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600780 psa_algorithm_t algorithm,
781 const uint8_t *p_nonce,
782 size_t nonce_length,
783 const uint8_t *p_additional_data,
784 size_t additional_data_length,
785 const uint8_t *p_ciphertext,
786 size_t ciphertext_length,
787 uint8_t *p_plaintext,
788 size_t plaintext_size,
789 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100790
791/**
792 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600793 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100794 *
795 * PSA Crypto API implementations should populate instances of the table as
796 * appropriate upon startup.
797 *
798 * If one of the functions is not implemented, it should be set to NULL.
799 */
800typedef struct {
801 /** Function that performs the AEAD encrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200802 psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
Gilles Peskine75976892018-12-12 15:55:09 +0100803 /** Function that performs the AEAD decrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200804 psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
Derek Miller83d26622019-02-15 16:41:22 -0600805} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100806/**@}*/
807
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600808/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100809 * Currently, key management is limited to importing keys in the clear,
810 * destroying keys, and exporting keys in the clear.
811 * Whether a key may be exported is determined by the key policies in place
812 * on the key slot.
813 */
814/**@{*/
815
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200816/** An enumeration indicating how a key is created.
817 */
818typedef enum
819{
820 PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */
821 PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */
822 PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */
823 PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */
Gilles Peskinea5f87492019-08-05 16:46:18 +0200824
825#ifndef __DOXYGEN_ONLY__
826 /** A key is being registered with mbedtls_psa_register_se_key().
827 *
828 * The core only passes this value to
829 * psa_drv_se_key_management_t::p_validate_slot_number, not to
830 * psa_drv_se_key_management_t::p_allocate. The call to
831 * `p_validate_slot_number` is not followed by any other call to the
832 * driver: the key is considered successfully registered if the call to
833 * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is
834 * null.
835 *
836 * With this creation method, the driver must return #PSA_SUCCESS if
837 * the given attributes are compatible with the existing key in the slot,
838 * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there
839 * is no key with the specified slot number.
840 *
841 * This is an Mbed Crypto extension.
842 */
843 PSA_KEY_CREATION_REGISTER,
844#endif
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200845} psa_key_creation_method_t;
846
Gilles Peskinef2223c82019-07-12 23:33:02 +0200847/** \brief A function that allocates a slot for a key.
848 *
Gilles Peskine9d752022019-08-09 11:33:48 +0200849 * To create a key in a specific slot in a secure element, the core
850 * first calls this function to determine a valid slot number,
851 * then calls a function to create the key material in that slot.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200852 * In nominal conditions (that is, if no error occurs),
853 * the effect of a call to a key creation function in the PSA Cryptography
854 * API with a lifetime that places the key in a secure element is the
855 * following:
Gilles Peskine9d752022019-08-09 11:33:48 +0200856 * -# The core calls psa_drv_se_key_management_t::p_allocate
857 * (or in some implementations
858 * psa_drv_se_key_management_t::p_validate_slot_number). The driver
859 * selects (or validates) a suitable slot number given the key attributes
860 * and the state of the secure element.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200861 * -# The core calls a key creation function in the driver.
Gilles Peskine9d752022019-08-09 11:33:48 +0200862 *
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200863 * The key creation functions in the PSA Cryptography API are:
864 * - psa_import_key(), which causes
865 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT
866 * then a call to psa_drv_se_key_management_t::p_import.
867 * - psa_generate_key(), which causes
868 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE
869 * then a call to psa_drv_se_key_management_t::p_import.
870 * - psa_key_derivation_output_key(), which causes
871 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE
872 * then a call to psa_drv_se_key_derivation_t::p_derive.
873 * - psa_copy_key(), which causes
874 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY
875 * then a call to psa_drv_se_key_management_t::p_export.
Gilles Peskine9d752022019-08-09 11:33:48 +0200876 *
877 * In case of errors, other behaviors are possible.
878 * - If the PSA Cryptography subsystem dies after the first step,
879 * for example because the device has lost power abruptly,
880 * the second step may never happen, or may happen after a reset
881 * and re-initialization. Alternatively, after a reset and
882 * re-initialization, the core may call
883 * psa_drv_se_key_management_t::p_destroy on the slot number that
884 * was allocated (or validated) instead of calling a key creation function.
885 * - If an error occurs, the core may call
886 * psa_drv_se_key_management_t::p_destroy on the slot number that
887 * was allocated (or validated) instead of calling a key creation function.
888 *
889 * Errors and system resets also have an impact on the driver's persistent
890 * data. If a reset happens before the overall key creation process is
891 * completed (before or after the second step above), it is unspecified
892 * whether the persistent data after the reset is identical to what it
893 * was before or after the call to `p_allocate` (or `p_validate_slot_number`).
894 *
Gilles Peskinef2223c82019-07-12 23:33:02 +0200895 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200896 * \param[in,out] persistent_data A pointer to the persistent data
897 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200898 * \param[in] attributes Attributes of the key.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200899 * \param method The way in which the key is being created.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200900 * \param[out] key_slot Slot where the key will be stored.
901 * This must be a valid slot for a key of the
902 * chosen type. It must be unoccupied.
Gilles Peskinef2223c82019-07-12 23:33:02 +0200903 *
904 * \retval #PSA_SUCCESS
905 * Success.
906 * The core will record \c *key_slot as the key slot where the key
907 * is stored and will update the persistent data in storage.
908 * \retval #PSA_ERROR_NOT_SUPPORTED
909 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
910 */
911typedef psa_status_t (*psa_drv_se_allocate_key_t)(
912 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200913 void *persistent_data,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200914 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200915 psa_key_creation_method_t method,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200916 psa_key_slot_number_t *key_slot);
917
Gilles Peskineae9964d2019-08-05 14:55:14 +0200918/** \brief A function that determines whether a slot number is valid
919 * for a key.
920 *
Gilles Peskine9d752022019-08-09 11:33:48 +0200921 * To create a key in a specific slot in a secure element, the core
922 * first calls this function to validate the choice of slot number,
923 * then calls a function to create the key material in that slot.
924 * See the documentation of #psa_drv_se_allocate_key_t for more details.
925 *
926 * As of the PSA Cryptography API specification version 1.0, there is no way
927 * for applications to trigger a call to this function. However some
928 * implementations offer the capability to create or declare a key in
929 * a specific slot via implementation-specific means, generally for the
930 * sake of initial device provisioning or onboarding. Such a mechanism may
931 * be added to a future version of the PSA Cryptography API specification.
932 *
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200933 * This function may update the driver's persistent data through
934 * \p persistent_data. The core will save the updated persistent data at the
935 * end of the key creation process. See the description of
936 * ::psa_drv_se_allocate_key_t for more information.
937 *
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200938 * \param[in,out] drv_context The driver context structure.
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200939 * \param[in,out] persistent_data A pointer to the persistent data
940 * that allows writing.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200941 * \param[in] attributes Attributes of the key.
942 * \param method The way in which the key is being created.
943 * \param[in] key_slot Slot where the key is to be stored.
Gilles Peskineae9964d2019-08-05 14:55:14 +0200944 *
945 * \retval #PSA_SUCCESS
946 * The given slot number is valid for a key with the given
947 * attributes.
948 * \retval #PSA_ERROR_INVALID_ARGUMENT
949 * The given slot number is not valid for a key with the
950 * given attributes. This includes the case where the slot
951 * number is not valid at all.
952 * \retval #PSA_ERROR_ALREADY_EXISTS
953 * There is already a key with the specified slot number.
954 * Drivers may choose to return this error from the key
955 * creation function instead.
956 */
957typedef psa_status_t (*psa_drv_se_validate_slot_number_t)(
958 psa_drv_se_context_t *drv_context,
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200959 void *persistent_data,
Gilles Peskineae9964d2019-08-05 14:55:14 +0200960 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200961 psa_key_creation_method_t method,
Gilles Peskineae9964d2019-08-05 14:55:14 +0200962 psa_key_slot_number_t key_slot);
963
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600964/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100965 *
966 * This function can support any output from psa_export_key(). Refer to the
967 * documentation of psa_export_key() for the format for each key type.
968 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200969 * \param[in,out] drv_context The driver context structure.
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200970 * \param key_slot Slot where the key will be stored.
Gilles Peskine18017402019-07-24 20:25:59 +0200971 * This must be a valid slot for a key of the
972 * chosen type. It must be unoccupied.
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200973 * \param[in] attributes The key attributes, including the lifetime,
974 * the key type and the usage policy.
975 * Drivers should not access the key size stored
976 * in the attributes: it may not match the
977 * data passed in \p data.
978 * Drivers can call psa_get_key_lifetime(),
979 * psa_get_key_type(),
980 * psa_get_key_usage_flags() and
981 * psa_get_key_algorithm() to access this
982 * information.
983 * \param[in] data Buffer containing the key data.
984 * \param[in] data_length Size of the \p data buffer in bytes.
Gilles Peskine18017402019-07-24 20:25:59 +0200985 * \param[out] bits On success, the key size in bits. The driver
986 * must determine this value after parsing the
987 * key according to the key type.
988 * This value is not used if the function fails.
Gilles Peskine75976892018-12-12 15:55:09 +0100989 *
990 * \retval #PSA_SUCCESS
991 * Success.
992 */
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200993typedef psa_status_t (*psa_drv_se_import_key_t)(
994 psa_drv_se_context_t *drv_context,
995 psa_key_slot_number_t key_slot,
996 const psa_key_attributes_t *attributes,
997 const uint8_t *data,
998 size_t data_length,
999 size_t *bits);
Gilles Peskine75976892018-12-12 15:55:09 +01001000
1001/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001002 * \brief A function that destroys a secure element key and restore the slot to
1003 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +01001004 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001005 * This function destroys the content of the key from a secure element.
1006 * Implementations shall make a best effort to ensure that any previous content
1007 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +01001008 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001009 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +01001010 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001011 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +02001012 * \param[in,out] persistent_data A pointer to the persistent data
1013 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001014 * \param key_slot The key slot to erase.
Gilles Peskine75976892018-12-12 15:55:09 +01001015 *
1016 * \retval #PSA_SUCCESS
1017 * The slot's content, if any, has been erased.
1018 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001019typedef psa_status_t (*psa_drv_se_destroy_key_t)(
1020 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +02001021 void *persistent_data,
Gilles Peskine8597bc12019-07-12 23:28:46 +02001022 psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +01001023
1024/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001025 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +01001026 *
1027 * The output of this function can be passed to psa_import_key() to
1028 * create an equivalent object.
1029 *
1030 * If a key is created with `psa_import_key()` and then exported with
1031 * this function, it is not guaranteed that the resulting data is
1032 * identical: the implementation may choose a different representation
1033 * of the same key if the format permits it.
1034 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001035 * This function should generate output in the same format that
1036 * `psa_export_key()` does. Refer to the
1037 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +01001038 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001039 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +01001040 * \param[in] key Slot whose content is to be exported. This must
1041 * be an occupied key slot.
1042 * \param[out] p_data Buffer where the key data is to be written.
1043 * \param[in] data_size Size of the `p_data` buffer in bytes.
1044 * \param[out] p_data_length On success, the number of bytes
1045 * that make up the key data.
1046 *
1047 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02001048 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine75976892018-12-12 15:55:09 +01001049 * \retval #PSA_ERROR_NOT_PERMITTED
1050 * \retval #PSA_ERROR_NOT_SUPPORTED
1051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1052 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001053 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine75976892018-12-12 15:55:09 +01001054 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001055typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
1056 psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -06001057 uint8_t *p_data,
1058 size_t data_size,
1059 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001060
1061/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001062 * \brief A function that generates a symmetric or asymmetric key on a secure
1063 * element
Gilles Peskine75976892018-12-12 15:55:09 +01001064 *
Gilles Peskine364d12c2021-03-08 17:23:47 +01001065 * If the key type \c type recorded in \p attributes
1066 * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001067 * the driver may export the public key at the time of generation,
1068 * in the format documented for psa_export_public_key() by writing it
1069 * to the \p pubkey buffer.
1070 * This is optional, intended for secure elements that output the
1071 * public key at generation time and that cannot export the public key
1072 * later. Drivers that do not need this feature should leave
1073 * \p *pubkey_length set to 0 and should
1074 * implement the psa_drv_key_management_t::p_export_public function.
1075 * Some implementations do not support this feature, in which case
1076 * \p pubkey is \c NULL and \p pubkey_size is 0.
Gilles Peskine75976892018-12-12 15:55:09 +01001077 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001078 * \param[in,out] drv_context The driver context structure.
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001079 * \param key_slot Slot where the key will be stored.
1080 * This must be a valid slot for a key of the
1081 * chosen type. It must be unoccupied.
1082 * \param[in] attributes The key attributes, including the lifetime,
1083 * the key type and size, and the usage policy.
1084 * Drivers can call psa_get_key_lifetime(),
1085 * psa_get_key_type(), psa_get_key_bits(),
1086 * psa_get_key_usage_flags() and
1087 * psa_get_key_algorithm() to access this
1088 * information.
1089 * \param[out] pubkey A buffer where the driver can write the
1090 * public key, when generating an asymmetric
1091 * key pair.
1092 * This is \c NULL when generating a symmetric
1093 * key or if the core does not support
1094 * exporting the public key at generation time.
1095 * \param pubkey_size The size of the `pubkey` buffer in bytes.
1096 * This is 0 when generating a symmetric
1097 * key or if the core does not support
1098 * exporting the public key at generation time.
1099 * \param[out] pubkey_length On entry, this is always 0.
1100 * On success, the number of bytes written to
1101 * \p pubkey. If this is 0 or unchanged on return,
1102 * the core will not read the \p pubkey buffer,
1103 * and will instead call the driver's
1104 * psa_drv_key_management_t::p_export_public
1105 * function to export the public key when needed.
Gilles Peskine75976892018-12-12 15:55:09 +01001106 */
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001107typedef psa_status_t (*psa_drv_se_generate_key_t)(
1108 psa_drv_se_context_t *drv_context,
1109 psa_key_slot_number_t key_slot,
1110 const psa_key_attributes_t *attributes,
1111 uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001112
1113/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001114 * \brief A struct containing all of the function pointers needed to for secure
1115 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +01001116 *
1117 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001118 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +01001119 *
1120 * If one of the functions is not implemented, it should be set to NULL.
1121 */
1122typedef struct {
Gilles Peskine9d752022019-08-09 11:33:48 +02001123 /** Function that allocates a slot for a key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001124 psa_drv_se_allocate_key_t MBEDTLS_PRIVATE(p_allocate);
Gilles Peskine9d752022019-08-09 11:33:48 +02001125 /** Function that checks the validity of a slot for a key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001126 psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001127 /** Function that performs a key import operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001128 psa_drv_se_import_key_t MBEDTLS_PRIVATE(p_import);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001129 /** Function that performs a generation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001130 psa_drv_se_generate_key_t MBEDTLS_PRIVATE(p_generate);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001131 /** Function that performs a key destroy operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001132 psa_drv_se_destroy_key_t MBEDTLS_PRIVATE(p_destroy);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001133 /** Function that performs a key export operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001134 psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export);
Gilles Peskinee62b74e2019-06-25 15:25:09 +02001135 /** Function that performs a public key export operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001136 psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export_public);
Derek Miller83d26622019-02-15 16:41:22 -06001137} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001138
1139/**@}*/
1140
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001141/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001142 * Key derivation is the process of generating new key material using an
1143 * existing key and additional parameters, iterating through a basic
1144 * cryptographic function, such as a hash.
1145 * Key agreement is a part of cryptographic protocols that allows two parties
1146 * to agree on the same key value, but starting from different original key
1147 * material.
1148 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
1149 * for both of the flows.
1150 *
1151 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001152 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1153 * `psa_drv_se_key_derivation_derive` is used when the key material should be
1154 * placed in a slot on the hardware and not exposed to the caller.
1155 * `psa_drv_se_key_derivation_export` is used when the key material should be
1156 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +01001157 *
1158 * Different key derivation algorithms require a different number of inputs.
1159 * Instead of having an API that takes as input variable length arrays, which
1160 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001161 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1162 * is called multiple times with different `collateral_id`s. Thus, for a key
Tobias Nießen1e8ca122021-05-10 19:53:15 +02001163 * derivation algorithm that required 3 parameter inputs, the flow would look
Gilles Peskine75976892018-12-12 15:55:09 +01001164 * something like:
1165 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001166 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1167 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1168 * p_collateral_0,
1169 * collateral_0_size);
1170 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1171 * p_collateral_1,
1172 * collateral_1_size);
1173 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1174 * p_collateral_2,
1175 * collateral_2_size);
1176 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +01001177 * ~~~~~~~~~~~~~
1178 *
1179 * key agreement example:
1180 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001181 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1182 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1183 * psa_drv_se_key_derivation_export(p_session_key,
1184 * session_key_size,
1185 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001186 * ~~~~~~~~~~~~~
1187 */
1188/**@{*/
1189
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001190/** \brief A function that Sets up a secure element key derivation operation by
1191 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +01001192 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001193 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +02001194 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001195 * context information for the implementation
1196 * \param[in] kdf_alg The algorithm to be used for the key derivation
1197 * \param[in] source_key The key to be used as the source material for
1198 * the key derivation
Gilles Peskine75976892018-12-12 15:55:09 +01001199 *
Ronald Cron96783552020-10-19 12:06:30 +02001200 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +01001201 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001202typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1203 void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001204 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -06001205 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001206
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001207/** \brief A function that provides collateral (parameters) needed for a secure
1208 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +01001209 *
1210 * Since many key derivation algorithms require multiple parameters, it is
Tobias Nießen1e8ca122021-05-10 19:53:15 +02001211 * expected that this function may be called multiple times for the same
Gilles Peskine75976892018-12-12 15:55:09 +01001212 * operation, each with a different algorithm-specific `collateral_id`
1213 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001214 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001215 * context information for the implementation
1216 * \param[in] collateral_id An ID for the collateral being provided
1217 * \param[in] p_collateral A buffer containing the collateral data
1218 * \param[in] collateral_size The size in bytes of the collateral
1219 *
Ronald Cron96783552020-10-19 12:06:30 +02001220 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +01001221 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001222typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001223 uint32_t collateral_id,
1224 const uint8_t *p_collateral,
1225 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +01001226
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001227/** \brief A function that performs the final secure element key derivation
1228 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +01001229 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001230 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001231 * context information for the implementation
1232 * \param[in] dest_key The slot where the generated key material
1233 * should be placed
1234 *
Ronald Cron96783552020-10-19 12:06:30 +02001235 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +01001236 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001237typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001238 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001239
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001240/** \brief A function that performs the final step of a secure element key
1241 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001242 *
1243 * \param[out] p_output Buffer in which to place the generated key
1244 * material
1245 * \param[in] output_size The size in bytes of `p_output`
1246 * \param[out] p_output_length Upon success, contains the number of bytes of
1247 * key material placed in `p_output`
1248 *
Ronald Cron96783552020-10-19 12:06:30 +02001249 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +01001250 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001251typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001252 uint8_t *p_output,
1253 size_t output_size,
1254 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001255
1256/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001257 * \brief A struct containing all of the function pointers needed to for secure
1258 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001259 *
1260 * PSA Crypto API implementations should populate instances of the table as
1261 * appropriate upon startup.
1262 *
1263 * If one of the functions is not implemented, it should be set to NULL.
1264 */
1265typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001266 /** The driver-specific size of the key derivation context */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001267 size_t MBEDTLS_PRIVATE(context_size);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001268 /** Function that performs a key derivation setup */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001269 psa_drv_se_key_derivation_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001270 /** Function that sets key derivation collateral */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001271 psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001272 /** Function that performs a final key derivation step */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001273 psa_drv_se_key_derivation_derive_t MBEDTLS_PRIVATE(p_derive);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001274 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001275 * exports the key */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001276 psa_drv_se_key_derivation_export_t MBEDTLS_PRIVATE(p_export);
Derek Miller83d26622019-02-15 16:41:22 -06001277} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001278
1279/**@}*/
1280
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001281/** \defgroup se_registration Secure element driver registration
1282 */
1283/**@{*/
1284
1285/** A structure containing pointers to all the entry points of a
1286 * secure element driver.
1287 *
1288 * Future versions of this specification may add extra substructures at
1289 * the end of this structure.
1290 */
1291typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001292 /** The version of the driver HAL that this driver implements.
1293 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001294 * a different version of this specification.
1295 * Use #PSA_DRV_SE_HAL_VERSION.
1296 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001297 uint32_t MBEDTLS_PRIVATE(hal_version);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001298
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +02001299 /** The size of the driver's persistent data in bytes.
1300 *
1301 * This can be 0 if the driver does not need persistent data.
1302 *
1303 * See the documentation of psa_drv_se_context_t::persistent_data
1304 * for more information about why and how a driver can use
1305 * persistent data.
1306 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001307 size_t MBEDTLS_PRIVATE(persistent_data_size);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001308
1309 /** The driver initialization function.
1310 *
1311 * This function is called once during the initialization of the
1312 * PSA Cryptography subsystem, before any other function of the
1313 * driver is called. If this function returns a failure status,
1314 * the driver will be unusable, at least until the next system reset.
1315 *
1316 * If this field is \c NULL, it is equivalent to a function that does
1317 * nothing and returns #PSA_SUCCESS.
1318 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001319 psa_drv_se_init_t MBEDTLS_PRIVATE(p_init);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001320
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001321 const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management);
1322 const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac);
1323 const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher);
1324 const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead);
1325 const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric);
1326 const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation);
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001327} psa_drv_se_t;
1328
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001329/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001330 */
1331/* 0.0.0 patchlevel 5 */
1332#define PSA_DRV_SE_HAL_VERSION 0x00000005
1333
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001334/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001335 *
1336 * This function is only intended to be used by driver code, not by
1337 * application code. In implementations with separation between the
1338 * PSA cryptography module and applications, this function should
1339 * only be available to callers that run in the same memory space as
1340 * the cryptography module, and should not be exposed to applications
1341 * running in a different memory space.
1342 *
1343 * This function may be called before psa_crypto_init(). It is
1344 * implementation-defined whether this function may be called
1345 * after psa_crypto_init().
1346 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001347 * \note Implementations store metadata about keys including the lifetime
Gilles Peskine52ac9582020-05-10 00:39:18 +02001348 * value, which contains the driver's location indicator. Therefore,
1349 * from one instantiation of the PSA Cryptography
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001350 * library to the next one, if there is a key in storage with a certain
1351 * lifetime value, you must always register the same driver (or an
1352 * updated version that communicates with the same secure element)
Gilles Peskine52ac9582020-05-10 00:39:18 +02001353 * with the same location value.
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001354 *
Gilles Peskine52ac9582020-05-10 00:39:18 +02001355 * \param location The location value through which this driver will
Gilles Peskined910e922019-06-24 13:47:07 +02001356 * be exposed to applications.
Gilles Peskine52ac9582020-05-10 00:39:18 +02001357 * This driver will be used for all keys such that
Ronald Cron96783552020-10-19 12:06:30 +02001358 * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`.
Gilles Peskine52ac9582020-05-10 00:39:18 +02001359 * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
1360 * and may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001361 * may reserve other values.
1362 * \param[in] methods The method table of the driver. This structure must
1363 * remain valid for as long as the cryptography
1364 * module keeps running. It is typically a global
1365 * constant.
1366 *
Ronald Cron96783552020-10-19 12:06:30 +02001367 * \return #PSA_SUCCESS
Gilles Peskined910e922019-06-24 13:47:07 +02001368 * The driver was successfully registered. Applications can now
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001369 * use \p location to access keys through the methods passed to
Gilles Peskined910e922019-06-24 13:47:07 +02001370 * this function.
Ronald Cron96783552020-10-19 12:06:30 +02001371 * \return #PSA_ERROR_BAD_STATE
Gilles Peskined910e922019-06-24 13:47:07 +02001372 * This function was called after the initialization of the
1373 * cryptography module, and this implementation does not support
1374 * driver registration at this stage.
Ronald Cron96783552020-10-19 12:06:30 +02001375 * \return #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001376 * There is already a registered driver for this value of \p location.
Ronald Cron96783552020-10-19 12:06:30 +02001377 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001378 * \p location is a reserved value.
Ronald Cron96783552020-10-19 12:06:30 +02001379 * \return #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001380 * `methods->hal_version` is not supported by this implementation.
Ronald Cron96783552020-10-19 12:06:30 +02001381 * \return #PSA_ERROR_INSUFFICIENT_MEMORY
1382 * \return #PSA_ERROR_NOT_PERMITTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001383 * \return #PSA_ERROR_STORAGE_FAILURE
1384 * \return #PSA_ERROR_DATA_CORRUPT
Gilles Peskined910e922019-06-24 13:47:07 +02001385 */
1386psa_status_t psa_register_se_driver(
Gilles Peskine344e15b2020-05-10 00:44:30 +02001387 psa_key_location_t location,
Gilles Peskined910e922019-06-24 13:47:07 +02001388 const psa_drv_se_t *methods);
1389
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001390/**@}*/
1391
Gilles Peskine75976892018-12-12 15:55:09 +01001392#ifdef __cplusplus
1393}
1394#endif
1395
1396#endif /* PSA_CRYPTO_SE_DRIVER_H */