blob: 9ce14bba621c80470c2e61e3046f0919722853a4 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000020 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine75976892018-12-12 15:55:09 +010021 */
22#ifndef PSA_CRYPTO_SE_DRIVER_H
23#define PSA_CRYPTO_SE_DRIVER_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Gilles Peskine75976892018-12-12 15:55:09 +010025
26#include "crypto_driver_common.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
Gilles Peskine7a86da12019-07-12 23:25:38 +020032/** \defgroup se_init Secure element driver initialization
33 */
34/**@{*/
35
36/** \brief Driver context structure
37 *
38 * Driver functions receive a pointer to this structure.
39 * Each registered driver has one instance of this structure.
40 *
41 * Implementations must include the fields specified here and
42 * may include other fields.
43 */
44typedef struct {
45 /** A read-only pointer to the driver's persistent data.
46 *
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020047 * Drivers typically use this persistent data to keep track of
48 * which slot numbers are available. This is only a guideline:
49 * drivers may use the persistent data for any purpose, keeping
50 * in mind the restrictions on when the persistent data is saved
51 * to storage: the persistent data is only saved after calling
52 * certain functions that receive a writable pointer to the
53 * persistent data.
Gilles Peskine7a86da12019-07-12 23:25:38 +020054 *
55 * The core allocates a memory buffer for the persistent data.
Gilles Peskine6a3dd892019-07-25 10:56:39 +020056 * The pointer is guaranteed to be suitably aligned for any data type,
Gilles Peskine7a86da12019-07-12 23:25:38 +020057 * like a pointer returned by `malloc` (but the core can use any
58 * method to allocate the buffer, not necessarily `malloc`).
59 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +020060 * The size of this buffer is in the \c persistent_data_size field of
61 * this structure.
Gilles Peskine7a86da12019-07-12 23:25:38 +020062 *
63 * Before the driver is initialized for the first time, the content of
64 * the persistent data is all-bits-zero. After a driver upgrade, if the
65 * size of the persistent data has increased, the original data is padded
66 * on the right with zeros; if the size has decreased, the original data
67 * is truncated to the new size.
68 *
69 * This pointer is to read-only data. Only a few driver functions are
70 * allowed to modify the persistent data. These functions receive a
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020071 * writable pointer. These functions are:
72 * - psa_drv_se_t::p_init
73 * - psa_drv_se_key_management_t::p_allocate
74 * - psa_drv_se_key_management_t::p_destroy
75 *
76 * The PSA Cryptography core saves the persistent data from one
77 * session to the next. It does this before returning from API functions
78 * that call a driver method that is allowed to modify the persistent
79 * data, specifically:
80 * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
81 * psa_drv_se_key_management_t::p_destroy to complete an action
82 * that was interrupted by a power failure.
83 * - Key creation functions cause a call to
84 * psa_drv_se_key_management_t::p_allocate, and may cause a call to
85 * psa_drv_se_key_management_t::p_destroy in case an error occurs.
86 * - psa_destroy_key() causes a call to
87 * psa_drv_se_key_management_t::p_destroy.
Gilles Peskine7a86da12019-07-12 23:25:38 +020088 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020089 const void *const MBEDTLS_PRIVATE(persistent_data);
Gilles Peskine7a86da12019-07-12 23:25:38 +020090
91 /** The size of \c persistent_data in bytes.
92 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +020093 * This is always equal to the value of the `persistent_data_size` field
94 * of the ::psa_drv_se_t structure when the driver is registered.
Gilles Peskine7a86da12019-07-12 23:25:38 +020095 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020096 const size_t MBEDTLS_PRIVATE(persistent_data_size);
Gilles Peskine7a86da12019-07-12 23:25:38 +020097
98 /** Driver transient data.
99 *
100 * The core initializes this value to 0 and does not read or modify it
101 * afterwards. The driver may store whatever it wants in this field.
102 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 uintptr_t MBEDTLS_PRIVATE(transient_data);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200104} psa_drv_se_context_t;
105
106/** \brief A driver initialization function.
107 *
108 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200109 * \param[in,out] persistent_data A pointer to the persistent data
110 * that allows writing.
Gilles Peskine52ac9582020-05-10 00:39:18 +0200111 * \param location The location value for which this driver
112 * is registered. The driver will be invoked
113 * for all keys whose lifetime is in this
114 * location.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200115 *
116 * \retval #PSA_SUCCESS
117 * The driver is operational.
118 * The core will update the persistent data in storage.
119 * \return
120 * Any other return value prevents the driver from being used in
121 * this session.
122 * The core will NOT update the persistent data in storage.
123 */
124typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200125 void *persistent_data,
Gilles Peskine52ac9582020-05-10 00:39:18 +0200126 psa_key_location_t location);
Gilles Peskine7a86da12019-07-12 23:25:38 +0200127
Gilles Peskinec8000c02019-08-02 20:15:51 +0200128#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200129/* Mbed TLS with secure element support enabled defines this type in
Gilles Peskinec8000c02019-08-02 20:15:51 +0200130 * crypto_types.h because it is also visible to applications through an
131 * implementation-specific extension.
132 * For the PSA Cryptography specification, this type is only visible
133 * via crypto_se_driver.h. */
Gilles Peskine75976892018-12-12 15:55:09 +0100134/** An internal designation of a key slot between the core part of the
135 * PSA Crypto implementation and the driver. The meaning of this value
136 * is driver-dependent. */
Gilles Peskinef03143a2019-07-12 23:18:29 +0200137typedef uint64_t psa_key_slot_number_t;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200138#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine75976892018-12-12 15:55:09 +0100139
Gilles Peskine7a86da12019-07-12 23:25:38 +0200140/**@}*/
141
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600142/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +0100143 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600144 * a secure element can be done either as a single function call (via the
145 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +0100146 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600147 * - `psa_drv_se_mac_setup_t`
148 * - `psa_drv_se_mac_update_t`
149 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100150 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600151 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100152 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600153 * If a previously started secure element MAC operation needs to be terminated,
154 * 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 +0100155 * result in allocated resources not being freed or in other undefined
156 * behavior.
157 */
158/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600159/** \brief A function that starts a secure element MAC operation for a PSA
160 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +0100161 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200162 * \param[in,out] drv_context The driver context structure.
163 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100164 * hardware-specific MAC context
165 * \param[in] key_slot The slot of the key to be used for the
166 * operation
167 * \param[in] algorithm The algorithm to be used to underly the MAC
168 * operation
169 *
Ronald Cron96783552020-10-19 12:06:30 +0200170 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100171 * Success.
172 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200173typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
174 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600175 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600176 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100177
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600178/** \brief A function that continues a previously started secure element MAC
179 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100180 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200181 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100182 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600183 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100184 * \param[in] p_input A buffer containing the message to be appended
185 * to the MAC operation
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200186 * \param[in] input_length The size in bytes of the input message buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100187 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200188typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600189 const uint8_t *p_input,
190 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100191
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600192/** \brief a function that completes a previously started secure element MAC
193 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100194 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200195 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100196 * previously started MAC operation to be
197 * finished
198 * \param[out] p_mac A buffer where the generated MAC will be
199 * placed
200 * \param[in] mac_size The size in bytes of the buffer that has been
201 * allocated for the `output` buffer
202 * \param[out] p_mac_length After completion, will contain the number of
203 * bytes placed in the `p_mac` buffer
204 *
Ronald Cron96783552020-10-19 12:06:30 +0200205 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100206 * Success.
207 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200208typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600209 uint8_t *p_mac,
210 size_t mac_size,
211 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100212
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600213/** \brief A function that completes a previously started secure element MAC
214 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100215 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200216 * \param[in,out] op_context A hardware-specific structure for the previously
Tom Cosgrove1797b052022-12-04 17:19:59 +0000217 * started MAC operation to be finished
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200218 * \param[in] p_mac The MAC value against which the resulting MAC
219 * will be compared against
220 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Gilles Peskine75976892018-12-12 15:55:09 +0100221 *
Ronald Cron96783552020-10-19 12:06:30 +0200222 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100223 * The operation completed successfully and the MACs matched each
224 * other
Ronald Cron96783552020-10-19 12:06:30 +0200225 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine75976892018-12-12 15:55:09 +0100226 * The operation completed successfully, but the calculated MAC did
227 * not match the provided MAC
228 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200229typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600230 const uint8_t *p_mac,
231 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100232
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600233/** \brief A function that aborts a previous started secure element MAC
234 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100235 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200236 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200237 * started MAC operation to be aborted
Gilles Peskine75976892018-12-12 15:55:09 +0100238 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200239typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100240
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600241/** \brief A function that performs a secure element MAC operation in one
242 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100243 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200244 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100245 * \param[in] p_input A buffer containing the message to be MACed
246 * \param[in] input_length The size in bytes of `p_input`
247 * \param[in] key_slot The slot of the key to be used
248 * \param[in] alg The algorithm to be used to underlie the MAC
249 * operation
250 * \param[out] p_mac A buffer where the generated MAC will be
251 * placed
252 * \param[in] mac_size The size in bytes of the `p_mac` buffer
253 * \param[out] p_mac_length After completion, will contain the number of
254 * bytes placed in the `output` buffer
255 *
Ronald Cron96783552020-10-19 12:06:30 +0200256 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100257 * Success.
258 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200259typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
260 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600261 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600262 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600263 psa_algorithm_t alg,
264 uint8_t *p_mac,
265 size_t mac_size,
266 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100267
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600268/** \brief A function that performs a secure element MAC operation in one
269 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100270 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200271 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100272 * \param[in] p_input A buffer containing the message to be MACed
273 * \param[in] input_length The size in bytes of `input`
274 * \param[in] key_slot The slot of the key to be used
275 * \param[in] alg The algorithm to be used to underlie the MAC
276 * operation
277 * \param[in] p_mac The MAC value against which the resulting MAC will
278 * be compared against
279 * \param[in] mac_length The size in bytes of `mac`
280 *
Ronald Cron96783552020-10-19 12:06:30 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100282 * The operation completed successfully and the MACs matched each
283 * other
Ronald Cron96783552020-10-19 12:06:30 +0200284 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine75976892018-12-12 15:55:09 +0100285 * The operation completed successfully, but the calculated MAC did
286 * not match the provided MAC
287 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200288typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
289 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600290 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600291 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600292 psa_algorithm_t alg,
293 const uint8_t *p_mac,
294 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100295
296/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600297 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100298 *
299 * PSA Crypto API implementations should populate the table as appropriate
300 * upon startup.
301 *
302 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600303 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100304 *
305 * Driver implementers should ensure that they implement all of the functions
306 * that make sense for their hardware, and that they provide a full solution
307 * (for example, if they support `p_setup`, they should also support
308 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
309 *
310 */
311typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600312 /**The size in bytes of the hardware-specific secure element MAC context
313 * structure
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200315 size_t MBEDTLS_PRIVATE(context_size);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600316 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100317 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200318 psa_drv_se_mac_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600319 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100320 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200321 psa_drv_se_mac_update_t MBEDTLS_PRIVATE(p_update);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600322 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100323 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200324 psa_drv_se_mac_finish_t MBEDTLS_PRIVATE(p_finish);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600325 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100326 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200327 psa_drv_se_mac_finish_verify_t MBEDTLS_PRIVATE(p_finish_verify);
Tom Cosgrove1797b052022-12-04 17:19:59 +0000328 /** Function that aborts a previously started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100329 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200330 psa_drv_se_mac_abort_t MBEDTLS_PRIVATE(p_abort);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600331 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100332 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200333 psa_drv_se_mac_generate_t MBEDTLS_PRIVATE(p_mac);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600334 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100335 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200336 psa_drv_se_mac_verify_t MBEDTLS_PRIVATE(p_mac_verify);
Derek Miller83d26622019-02-15 16:41:22 -0600337} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100338/**@}*/
339
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600340/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100341 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600342 * Encryption and Decryption using secure element keys in block modes other
343 * than ECB must be done in multiple parts, using the following flow:
344 * - `psa_drv_se_cipher_setup_t`
345 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
346 * - `psa_drv_se_cipher_update_t`
347 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100348 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600349 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100350 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600351 * If a previously started secure element Cipher operation needs to be
352 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
353 * to do so may result in allocated resources not being freed or in other
354 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100355 *
356 * In situations where a PSA Cryptographic API implementation is using a block
357 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600358 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
359 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100360 */
361/**@{*/
362
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600363/** \brief A function that provides the cipher setup function for a
364 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100365 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200366 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +0200367 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100368 * hardware-specific cipher context.
369 * \param[in] key_slot The slot of the key to be used for the
370 * operation
371 * \param[in] algorithm The algorithm to be used in the cipher
372 * operation
373 * \param[in] direction Indicates whether the operation is an encrypt
374 * or decrypt
375 *
Gilles Peskineed733552023-02-14 19:21:09 +0100376 * \retval #PSA_SUCCESS \emptydescription
377 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100378 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200379typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
380 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600381 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600382 psa_algorithm_t algorithm,
383 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100384
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600385/** \brief A function that sets the initialization vector (if
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100386 * necessary) for a secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100387 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600388 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
389 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100390 * generate function is not necessary for the drivers to implement as the PSA
391 * Crypto implementation can do the generation using its RNG features.
392 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200393 * \param[in,out] op_context A structure that contains the previously set up
Gilles Peskine75976892018-12-12 15:55:09 +0100394 * hardware-specific cipher context
395 * \param[in] p_iv A buffer containing the initialization vector
396 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
397 *
Gilles Peskineed733552023-02-14 19:21:09 +0100398 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100399 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200400typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600401 const uint8_t *p_iv,
402 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100403
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600404/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100405 * operation
406 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200407 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100408 * previously started cipher operation
409 * \param[in] p_input A buffer containing the data to be
410 * encrypted/decrypted
411 * \param[in] input_size The size in bytes of the buffer pointed to
412 * by `p_input`
413 * \param[out] p_output The caller-allocated buffer where the
414 * output will be placed
415 * \param[in] output_size The allocated size in bytes of the
416 * `p_output` buffer
417 * \param[out] p_output_length After completion, will contain the number
418 * of bytes placed in the `p_output` buffer
419 *
Gilles Peskineed733552023-02-14 19:21:09 +0100420 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100421 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200422typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600423 const uint8_t *p_input,
424 size_t input_size,
425 uint8_t *p_output,
426 size_t output_size,
427 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100428
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600429/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100430 * operation
431 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200432 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100433 * previously started cipher operation
434 * \param[out] p_output The caller-allocated buffer where the output
435 * will be placed
436 * \param[in] output_size The allocated size in bytes of the `p_output`
437 * buffer
438 * \param[out] p_output_length After completion, will contain the number of
439 * bytes placed in the `p_output` buffer
440 *
Gilles Peskineed733552023-02-14 19:21:09 +0100441 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100442 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200443typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600444 uint8_t *p_output,
445 size_t output_size,
446 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100447
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600448/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100449 * operation
450 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200451 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100452 * previously started cipher operation
453 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200454typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100455
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600456/** \brief A function that performs the ECB block mode for secure element
457 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100458 *
459 * Note: this function should only be used with implementations that do not
460 * provide a needed higher-level operation.
461 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200462 * \param[in,out] drv_context The driver context structure.
463 * \param[in] key_slot The slot of the key to be used for the operation
464 * \param[in] algorithm The algorithm to be used in the cipher operation
465 * \param[in] direction Indicates whether the operation is an encrypt or
466 * decrypt
467 * \param[in] p_input A buffer containing the data to be
468 * encrypted/decrypted
469 * \param[in] input_size The size in bytes of the buffer pointed to by
470 * `p_input`
471 * \param[out] p_output The caller-allocated buffer where the output
472 * will be placed
473 * \param[in] output_size The allocated size in bytes of the `p_output`
474 * buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100475 *
Gilles Peskineed733552023-02-14 19:21:09 +0100476 * \retval #PSA_SUCCESS \emptydescription
477 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100478 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200479typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
480 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600481 psa_algorithm_t algorithm,
482 psa_encrypt_or_decrypt_t direction,
483 const uint8_t *p_input,
484 size_t input_size,
485 uint8_t *p_output,
486 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100487
488/**
489 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600490 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100491 *
492 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600493 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100494 *
495 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600496 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100497 */
498typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600499 /** The size in bytes of the hardware-specific secure element cipher
500 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100501 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200502 size_t MBEDTLS_PRIVATE(context_size);
Derek Miller34b33f12019-02-15 17:13:54 -0600503 /** Function that performs a cipher setup operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200504 psa_drv_se_cipher_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600505 /** Function that sets a cipher IV (if necessary) */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200506 psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600507 /** Function that performs a cipher update operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200508 psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600509 /** Function that completes a cipher operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200510 psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600511 /** Function that aborts a cipher operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200512 psa_drv_se_cipher_abort_t MBEDTLS_PRIVATE(p_abort);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600513 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100514 * (Danger: ECB mode should not be used directly by clients of the PSA
515 * Crypto Client API)
516 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200517 psa_drv_se_cipher_ecb_t MBEDTLS_PRIVATE(p_ecb);
Derek Miller83d26622019-02-15 16:41:22 -0600518} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100519
520/**@}*/
521
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600522/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100523 *
524 * Since the amount of data that can (or should) be encrypted or signed using
525 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600526 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100527 */
528/**@{*/
529
530/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600531 * \brief A function that signs a hash or short message with a private key in
532 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100533 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200534 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100535 * \param[in] key_slot Key slot of an asymmetric key pair
536 * \param[in] alg A signature algorithm that is compatible
537 * with the type of `key`
538 * \param[in] p_hash The hash to sign
539 * \param[in] hash_length Size of the `p_hash` buffer in bytes
540 * \param[out] p_signature Buffer where the signature is to be written
541 * \param[in] signature_size Size of the `p_signature` buffer in bytes
542 * \param[out] p_signature_length On success, the number of bytes
543 * that make up the returned signature value
544 *
Gilles Peskineed733552023-02-14 19:21:09 +0100545 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100546 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200547typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
548 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600549 psa_algorithm_t alg,
550 const uint8_t *p_hash,
551 size_t hash_length,
552 uint8_t *p_signature,
553 size_t signature_size,
554 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100555
556/**
557 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600558 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100559 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200560 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100561 * \param[in] key_slot Key slot of a public key or an asymmetric key
562 * pair
563 * \param[in] alg A signature algorithm that is compatible with
564 * the type of `key`
565 * \param[in] p_hash The hash whose signature is to be verified
566 * \param[in] hash_length Size of the `p_hash` buffer in bytes
567 * \param[in] p_signature Buffer containing the signature to verify
568 * \param[in] signature_length Size of the `p_signature` buffer in bytes
569 *
Ronald Cron96783552020-10-19 12:06:30 +0200570 * \retval #PSA_SUCCESS
Gilles Peskine75976892018-12-12 15:55:09 +0100571 * The signature is valid.
572 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200573typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
574 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600575 psa_algorithm_t alg,
576 const uint8_t *p_hash,
577 size_t hash_length,
578 const uint8_t *p_signature,
579 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100580
581/**
582 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600583 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100584 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200585 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100586 * \param[in] key_slot Key slot of a public key or an asymmetric key
587 * pair
588 * \param[in] alg An asymmetric encryption algorithm that is
589 * compatible with the type of `key`
590 * \param[in] p_input The message to encrypt
591 * \param[in] input_length Size of the `p_input` buffer in bytes
592 * \param[in] p_salt A salt or label, if supported by the
593 * encryption algorithm
594 * If the algorithm does not support a
595 * salt, pass `NULL`.
596 * If the algorithm supports an optional
597 * salt and you do not want to pass a salt,
598 * pass `NULL`.
599 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
600 * supported.
601 * \param[in] salt_length Size of the `p_salt` buffer in bytes
602 * If `p_salt` is `NULL`, pass 0.
603 * \param[out] p_output Buffer where the encrypted message is to
604 * be written
605 * \param[in] output_size Size of the `p_output` buffer in bytes
606 * \param[out] p_output_length On success, the number of bytes that make up
607 * the returned output
608 *
Gilles Peskineed733552023-02-14 19:21:09 +0100609 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100610 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200611typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
612 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600613 psa_algorithm_t alg,
614 const uint8_t *p_input,
615 size_t input_length,
616 const uint8_t *p_salt,
617 size_t salt_length,
618 uint8_t *p_output,
619 size_t output_size,
620 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100621
622/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600623 * \brief A function that decrypts a short message with an asymmetric private
624 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100625 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200626 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100627 * \param[in] key_slot Key slot of an asymmetric key pair
628 * \param[in] alg An asymmetric encryption algorithm that is
629 * compatible with the type of `key`
630 * \param[in] p_input The message to decrypt
631 * \param[in] input_length Size of the `p_input` buffer in bytes
632 * \param[in] p_salt A salt or label, if supported by the
633 * encryption algorithm
634 * If the algorithm does not support a
635 * salt, pass `NULL`.
636 * If the algorithm supports an optional
637 * salt and you do not want to pass a salt,
638 * pass `NULL`.
639 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
640 * supported.
641 * \param[in] salt_length Size of the `p_salt` buffer in bytes
642 * If `p_salt` is `NULL`, pass 0.
643 * \param[out] p_output Buffer where the decrypted message is to
644 * be written
645 * \param[in] output_size Size of the `p_output` buffer in bytes
646 * \param[out] p_output_length On success, the number of bytes
647 * that make up the returned output
648 *
Gilles Peskineed733552023-02-14 19:21:09 +0100649 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +0100650 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200651typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
652 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600653 psa_algorithm_t alg,
654 const uint8_t *p_input,
655 size_t input_length,
656 const uint8_t *p_salt,
657 size_t salt_length,
658 uint8_t *p_output,
659 size_t output_size,
660 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100661
662/**
663 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600664 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100665 *
666 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600667 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100668 *
669 * If one of the functions is not implemented, it should be set to NULL.
670 */
671typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600672 /** Function that performs an asymmetric sign operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200673 psa_drv_se_asymmetric_sign_t MBEDTLS_PRIVATE(p_sign);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600674 /** Function that performs an asymmetric verify operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200675 psa_drv_se_asymmetric_verify_t MBEDTLS_PRIVATE(p_verify);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600676 /** Function that performs an asymmetric encrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200677 psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600678 /** Function that performs an asymmetric decrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200679 psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
Derek Miller83d26622019-02-15 16:41:22 -0600680} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100681
682/**@}*/
683
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600684/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
685 * Authenticated Encryption with Additional Data (AEAD) operations with secure
686 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100687 * implementers as there must be sufficient space in memory for the entire
688 * message, it prevents decrypted data from being made available before the
689 * authentication operation is complete and the data is known to be authentic.
690 */
691/**@{*/
692
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600693/** \brief A function that performs a secure element authenticated encryption
694 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100695 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200696 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100697 * \param[in] key_slot Slot containing the key to use.
698 * \param[in] algorithm The AEAD algorithm to compute
699 * (\c PSA_ALG_XXX value such that
700 * #PSA_ALG_IS_AEAD(`alg`) is true)
701 * \param[in] p_nonce Nonce or IV to use
702 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
703 * \param[in] p_additional_data Additional data that will be
704 * authenticated but not encrypted
705 * \param[in] additional_data_length Size of `p_additional_data` in bytes
706 * \param[in] p_plaintext Data that will be authenticated and
707 * encrypted
708 * \param[in] plaintext_length Size of `p_plaintext` in bytes
709 * \param[out] p_ciphertext Output buffer for the authenticated and
710 * encrypted data. The additional data is
711 * not part of this output. For algorithms
712 * where the encrypted data and the
713 * authentication tag are defined as
714 * separate outputs, the authentication
715 * tag is appended to the encrypted data.
716 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
717 * bytes
718 * \param[out] p_ciphertext_length On success, the size of the output in
719 * the `p_ciphertext` buffer
720 *
721 * \retval #PSA_SUCCESS
722 * Success.
723 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200724typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
725 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600726 psa_algorithm_t algorithm,
727 const uint8_t *p_nonce,
728 size_t nonce_length,
729 const uint8_t *p_additional_data,
730 size_t additional_data_length,
731 const uint8_t *p_plaintext,
732 size_t plaintext_length,
733 uint8_t *p_ciphertext,
734 size_t ciphertext_size,
735 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100736
Tom Cosgrove1797b052022-12-04 17:19:59 +0000737/** A function that performs a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100738 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200739 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100740 * \param[in] key_slot Slot containing the key to use
741 * \param[in] algorithm The AEAD algorithm to compute
742 * (\c PSA_ALG_XXX value such that
743 * #PSA_ALG_IS_AEAD(`alg`) is true)
744 * \param[in] p_nonce Nonce or IV to use
745 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
746 * \param[in] p_additional_data Additional data that has been
747 * authenticated but not encrypted
748 * \param[in] additional_data_length Size of `p_additional_data` in bytes
749 * \param[in] p_ciphertext Data that has been authenticated and
750 * encrypted.
751 * For algorithms where the encrypted data
752 * and the authentication tag are defined
753 * as separate inputs, the buffer must
754 * contain the encrypted data followed by
755 * the authentication tag.
756 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
757 * \param[out] p_plaintext Output buffer for the decrypted data
758 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
759 * bytes
760 * \param[out] p_plaintext_length On success, the size of the output in
761 * the `p_plaintext` buffer
762 *
763 * \retval #PSA_SUCCESS
764 * Success.
765 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200766typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
767 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600768 psa_algorithm_t algorithm,
769 const uint8_t *p_nonce,
770 size_t nonce_length,
771 const uint8_t *p_additional_data,
772 size_t additional_data_length,
773 const uint8_t *p_ciphertext,
774 size_t ciphertext_length,
775 uint8_t *p_plaintext,
776 size_t plaintext_size,
777 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100778
779/**
780 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600781 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100782 *
783 * PSA Crypto API implementations should populate instances of the table as
784 * appropriate upon startup.
785 *
786 * If one of the functions is not implemented, it should be set to NULL.
787 */
788typedef struct {
789 /** Function that performs the AEAD encrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200790 psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
Gilles Peskine75976892018-12-12 15:55:09 +0100791 /** Function that performs the AEAD decrypt operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200792 psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
Derek Miller83d26622019-02-15 16:41:22 -0600793} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100794/**@}*/
795
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600796/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100797 * Currently, key management is limited to importing keys in the clear,
798 * destroying keys, and exporting keys in the clear.
799 * Whether a key may be exported is determined by the key policies in place
800 * on the key slot.
801 */
802/**@{*/
803
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200804/** An enumeration indicating how a key is created.
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806typedef enum {
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200807 PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */
808 PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */
809 PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */
810 PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */
Gilles Peskinea5f87492019-08-05 16:46:18 +0200811
812#ifndef __DOXYGEN_ONLY__
813 /** A key is being registered with mbedtls_psa_register_se_key().
814 *
815 * The core only passes this value to
816 * psa_drv_se_key_management_t::p_validate_slot_number, not to
817 * psa_drv_se_key_management_t::p_allocate. The call to
818 * `p_validate_slot_number` is not followed by any other call to the
819 * driver: the key is considered successfully registered if the call to
820 * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is
821 * null.
822 *
823 * With this creation method, the driver must return #PSA_SUCCESS if
824 * the given attributes are compatible with the existing key in the slot,
825 * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there
826 * is no key with the specified slot number.
827 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200828 * This is an Mbed TLS extension.
Gilles Peskinea5f87492019-08-05 16:46:18 +0200829 */
830 PSA_KEY_CREATION_REGISTER,
831#endif
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200832} psa_key_creation_method_t;
833
Gilles Peskinef2223c82019-07-12 23:33:02 +0200834/** \brief A function that allocates a slot for a key.
835 *
Gilles Peskine9d752022019-08-09 11:33:48 +0200836 * To create a key in a specific slot in a secure element, the core
837 * first calls this function to determine a valid slot number,
838 * then calls a function to create the key material in that slot.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200839 * In nominal conditions (that is, if no error occurs),
840 * the effect of a call to a key creation function in the PSA Cryptography
841 * API with a lifetime that places the key in a secure element is the
842 * following:
Gilles Peskine9d752022019-08-09 11:33:48 +0200843 * -# The core calls psa_drv_se_key_management_t::p_allocate
844 * (or in some implementations
845 * psa_drv_se_key_management_t::p_validate_slot_number). The driver
846 * selects (or validates) a suitable slot number given the key attributes
847 * and the state of the secure element.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200848 * -# The core calls a key creation function in the driver.
Gilles Peskine9d752022019-08-09 11:33:48 +0200849 *
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200850 * The key creation functions in the PSA Cryptography API are:
851 * - psa_import_key(), which causes
852 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT
853 * then a call to psa_drv_se_key_management_t::p_import.
854 * - psa_generate_key(), which causes
855 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE
856 * then a call to psa_drv_se_key_management_t::p_import.
857 * - psa_key_derivation_output_key(), which causes
858 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE
859 * then a call to psa_drv_se_key_derivation_t::p_derive.
860 * - psa_copy_key(), which causes
861 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY
862 * then a call to psa_drv_se_key_management_t::p_export.
Gilles Peskine9d752022019-08-09 11:33:48 +0200863 *
864 * In case of errors, other behaviors are possible.
865 * - If the PSA Cryptography subsystem dies after the first step,
866 * for example because the device has lost power abruptly,
867 * the second step may never happen, or may happen after a reset
868 * and re-initialization. Alternatively, after a reset and
869 * re-initialization, the core may call
870 * psa_drv_se_key_management_t::p_destroy on the slot number that
871 * was allocated (or validated) instead of calling a key creation function.
872 * - If an error occurs, the core may call
873 * psa_drv_se_key_management_t::p_destroy on the slot number that
874 * was allocated (or validated) instead of calling a key creation function.
875 *
876 * Errors and system resets also have an impact on the driver's persistent
877 * data. If a reset happens before the overall key creation process is
878 * completed (before or after the second step above), it is unspecified
879 * whether the persistent data after the reset is identical to what it
880 * was before or after the call to `p_allocate` (or `p_validate_slot_number`).
881 *
Gilles Peskinef2223c82019-07-12 23:33:02 +0200882 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200883 * \param[in,out] persistent_data A pointer to the persistent data
884 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200885 * \param[in] attributes Attributes of the key.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200886 * \param method The way in which the key is being created.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200887 * \param[out] key_slot Slot where the key will be stored.
888 * This must be a valid slot for a key of the
889 * chosen type. It must be unoccupied.
Gilles Peskinef2223c82019-07-12 23:33:02 +0200890 *
891 * \retval #PSA_SUCCESS
892 * Success.
893 * The core will record \c *key_slot as the key slot where the key
894 * is stored and will update the persistent data in storage.
Gilles Peskineed733552023-02-14 19:21:09 +0100895 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
896 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
Gilles Peskinef2223c82019-07-12 23:33:02 +0200897 */
898typedef psa_status_t (*psa_drv_se_allocate_key_t)(
899 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200900 void *persistent_data,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200901 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200902 psa_key_creation_method_t method,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200903 psa_key_slot_number_t *key_slot);
904
Gilles Peskineae9964d2019-08-05 14:55:14 +0200905/** \brief A function that determines whether a slot number is valid
906 * for a key.
907 *
Gilles Peskine9d752022019-08-09 11:33:48 +0200908 * To create a key in a specific slot in a secure element, the core
909 * first calls this function to validate the choice of slot number,
910 * then calls a function to create the key material in that slot.
911 * See the documentation of #psa_drv_se_allocate_key_t for more details.
912 *
913 * As of the PSA Cryptography API specification version 1.0, there is no way
914 * for applications to trigger a call to this function. However some
915 * implementations offer the capability to create or declare a key in
916 * a specific slot via implementation-specific means, generally for the
917 * sake of initial device provisioning or onboarding. Such a mechanism may
918 * be added to a future version of the PSA Cryptography API specification.
919 *
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200920 * This function may update the driver's persistent data through
921 * \p persistent_data. The core will save the updated persistent data at the
922 * end of the key creation process. See the description of
923 * ::psa_drv_se_allocate_key_t for more information.
924 *
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200925 * \param[in,out] drv_context The driver context structure.
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200926 * \param[in,out] persistent_data A pointer to the persistent data
927 * that allows writing.
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200928 * \param[in] attributes Attributes of the key.
929 * \param method The way in which the key is being created.
930 * \param[in] key_slot Slot where the key is to be stored.
Gilles Peskineae9964d2019-08-05 14:55:14 +0200931 *
932 * \retval #PSA_SUCCESS
933 * The given slot number is valid for a key with the given
934 * attributes.
935 * \retval #PSA_ERROR_INVALID_ARGUMENT
936 * The given slot number is not valid for a key with the
937 * given attributes. This includes the case where the slot
938 * number is not valid at all.
939 * \retval #PSA_ERROR_ALREADY_EXISTS
940 * There is already a key with the specified slot number.
941 * Drivers may choose to return this error from the key
942 * creation function instead.
943 */
944typedef psa_status_t (*psa_drv_se_validate_slot_number_t)(
945 psa_drv_se_context_t *drv_context,
Gilles Peskine5ec3a302019-10-01 14:27:23 +0200946 void *persistent_data,
Gilles Peskineae9964d2019-08-05 14:55:14 +0200947 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200948 psa_key_creation_method_t method,
Gilles Peskineae9964d2019-08-05 14:55:14 +0200949 psa_key_slot_number_t key_slot);
950
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600951/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100952 *
953 * This function can support any output from psa_export_key(). Refer to the
954 * documentation of psa_export_key() for the format for each key type.
955 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200956 * \param[in,out] drv_context The driver context structure.
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200957 * \param key_slot Slot where the key will be stored.
Gilles Peskine18017402019-07-24 20:25:59 +0200958 * This must be a valid slot for a key of the
959 * chosen type. It must be unoccupied.
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200960 * \param[in] attributes The key attributes, including the lifetime,
961 * the key type and the usage policy.
962 * Drivers should not access the key size stored
963 * in the attributes: it may not match the
964 * data passed in \p data.
965 * Drivers can call psa_get_key_lifetime(),
966 * psa_get_key_type(),
967 * psa_get_key_usage_flags() and
968 * psa_get_key_algorithm() to access this
969 * information.
970 * \param[in] data Buffer containing the key data.
971 * \param[in] data_length Size of the \p data buffer in bytes.
Gilles Peskine18017402019-07-24 20:25:59 +0200972 * \param[out] bits On success, the key size in bits. The driver
973 * must determine this value after parsing the
974 * key according to the key type.
975 * This value is not used if the function fails.
Gilles Peskine75976892018-12-12 15:55:09 +0100976 *
977 * \retval #PSA_SUCCESS
978 * Success.
979 */
Gilles Peskinef3801ff2019-08-06 17:32:04 +0200980typedef psa_status_t (*psa_drv_se_import_key_t)(
981 psa_drv_se_context_t *drv_context,
982 psa_key_slot_number_t key_slot,
983 const psa_key_attributes_t *attributes,
984 const uint8_t *data,
985 size_t data_length,
986 size_t *bits);
Gilles Peskine75976892018-12-12 15:55:09 +0100987
988/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600989 * \brief A function that destroys a secure element key and restore the slot to
990 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100991 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600992 * This function destroys the content of the key from a secure element.
993 * Implementations shall make a best effort to ensure that any previous content
994 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100995 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600996 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100997 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200998 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200999 * \param[in,out] persistent_data A pointer to the persistent data
1000 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001001 * \param key_slot The key slot to erase.
Gilles Peskine75976892018-12-12 15:55:09 +01001002 *
1003 * \retval #PSA_SUCCESS
1004 * The slot's content, if any, has been erased.
1005 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001006typedef psa_status_t (*psa_drv_se_destroy_key_t)(
1007 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +02001008 void *persistent_data,
Gilles Peskine8597bc12019-07-12 23:28:46 +02001009 psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +01001010
1011/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001012 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +01001013 *
1014 * The output of this function can be passed to psa_import_key() to
1015 * create an equivalent object.
1016 *
1017 * If a key is created with `psa_import_key()` and then exported with
1018 * this function, it is not guaranteed that the resulting data is
1019 * identical: the implementation may choose a different representation
1020 * of the same key if the format permits it.
1021 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001022 * This function should generate output in the same format that
1023 * `psa_export_key()` does. Refer to the
1024 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +01001025 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001026 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +01001027 * \param[in] key Slot whose content is to be exported. This must
1028 * be an occupied key slot.
1029 * \param[out] p_data Buffer where the key data is to be written.
1030 * \param[in] data_size Size of the `p_data` buffer in bytes.
1031 * \param[out] p_data_length On success, the number of bytes
1032 * that make up the key data.
1033 *
Gilles Peskineed733552023-02-14 19:21:09 +01001034 * \retval #PSA_SUCCESS \emptydescription
1035 * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
1036 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1037 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
1038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1039 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1040 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +01001041 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001042typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
1043 psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -06001044 uint8_t *p_data,
1045 size_t data_size,
1046 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001047
1048/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001049 * \brief A function that generates a symmetric or asymmetric key on a secure
1050 * element
Gilles Peskine75976892018-12-12 15:55:09 +01001051 *
Gilles Peskine364d12c2021-03-08 17:23:47 +01001052 * If the key type \c type recorded in \p attributes
1053 * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001054 * the driver may export the public key at the time of generation,
1055 * in the format documented for psa_export_public_key() by writing it
1056 * to the \p pubkey buffer.
1057 * This is optional, intended for secure elements that output the
1058 * public key at generation time and that cannot export the public key
1059 * later. Drivers that do not need this feature should leave
1060 * \p *pubkey_length set to 0 and should
1061 * implement the psa_drv_key_management_t::p_export_public function.
1062 * Some implementations do not support this feature, in which case
1063 * \p pubkey is \c NULL and \p pubkey_size is 0.
Gilles Peskine75976892018-12-12 15:55:09 +01001064 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001065 * \param[in,out] drv_context The driver context structure.
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001066 * \param key_slot Slot where the key will be stored.
1067 * This must be a valid slot for a key of the
1068 * chosen type. It must be unoccupied.
1069 * \param[in] attributes The key attributes, including the lifetime,
1070 * the key type and size, and the usage policy.
1071 * Drivers can call psa_get_key_lifetime(),
1072 * psa_get_key_type(), psa_get_key_bits(),
1073 * psa_get_key_usage_flags() and
1074 * psa_get_key_algorithm() to access this
1075 * information.
1076 * \param[out] pubkey A buffer where the driver can write the
1077 * public key, when generating an asymmetric
1078 * key pair.
1079 * This is \c NULL when generating a symmetric
1080 * key or if the core does not support
1081 * exporting the public key at generation time.
1082 * \param pubkey_size The size of the `pubkey` buffer in bytes.
1083 * This is 0 when generating a symmetric
1084 * key or if the core does not support
1085 * exporting the public key at generation time.
1086 * \param[out] pubkey_length On entry, this is always 0.
1087 * On success, the number of bytes written to
1088 * \p pubkey. If this is 0 or unchanged on return,
1089 * the core will not read the \p pubkey buffer,
1090 * and will instead call the driver's
1091 * psa_drv_key_management_t::p_export_public
1092 * function to export the public key when needed.
Gilles Peskine75976892018-12-12 15:55:09 +01001093 */
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001094typedef psa_status_t (*psa_drv_se_generate_key_t)(
1095 psa_drv_se_context_t *drv_context,
1096 psa_key_slot_number_t key_slot,
1097 const psa_key_attributes_t *attributes,
1098 uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001099
1100/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001101 * \brief A struct containing all of the function pointers needed to for secure
1102 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +01001103 *
1104 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001105 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +01001106 *
1107 * If one of the functions is not implemented, it should be set to NULL.
1108 */
1109typedef struct {
Gilles Peskine9d752022019-08-09 11:33:48 +02001110 /** Function that allocates a slot for a key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001111 psa_drv_se_allocate_key_t MBEDTLS_PRIVATE(p_allocate);
Gilles Peskine9d752022019-08-09 11:33:48 +02001112 /** Function that checks the validity of a slot for a key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001113 psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001114 /** Function that performs a key import operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001115 psa_drv_se_import_key_t MBEDTLS_PRIVATE(p_import);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001116 /** Function that performs a generation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001117 psa_drv_se_generate_key_t MBEDTLS_PRIVATE(p_generate);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001118 /** Function that performs a key destroy operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001119 psa_drv_se_destroy_key_t MBEDTLS_PRIVATE(p_destroy);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001120 /** Function that performs a key export operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001121 psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export);
Gilles Peskinee62b74e2019-06-25 15:25:09 +02001122 /** Function that performs a public key export operation */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001123 psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export_public);
Derek Miller83d26622019-02-15 16:41:22 -06001124} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001125
1126/**@}*/
1127
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001128/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001129 * Key derivation is the process of generating new key material using an
1130 * existing key and additional parameters, iterating through a basic
1131 * cryptographic function, such as a hash.
1132 * Key agreement is a part of cryptographic protocols that allows two parties
1133 * to agree on the same key value, but starting from different original key
1134 * material.
1135 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
1136 * for both of the flows.
1137 *
1138 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001139 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1140 * `psa_drv_se_key_derivation_derive` is used when the key material should be
1141 * placed in a slot on the hardware and not exposed to the caller.
1142 * `psa_drv_se_key_derivation_export` is used when the key material should be
1143 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +01001144 *
1145 * Different key derivation algorithms require a different number of inputs.
1146 * Instead of having an API that takes as input variable length arrays, which
Tom Cosgrove1797b052022-12-04 17:19:59 +00001147 * can be problematic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001148 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1149 * is called multiple times with different `collateral_id`s. Thus, for a key
Tobias Nießen1e8ca122021-05-10 19:53:15 +02001150 * derivation algorithm that required 3 parameter inputs, the flow would look
Gilles Peskine75976892018-12-12 15:55:09 +01001151 * something like:
1152 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001153 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1154 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1155 * p_collateral_0,
1156 * collateral_0_size);
1157 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1158 * p_collateral_1,
1159 * collateral_1_size);
1160 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1161 * p_collateral_2,
1162 * collateral_2_size);
1163 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +01001164 * ~~~~~~~~~~~~~
1165 *
1166 * key agreement example:
1167 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001168 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1169 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1170 * psa_drv_se_key_derivation_export(p_session_key,
1171 * session_key_size,
1172 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001173 * ~~~~~~~~~~~~~
1174 */
1175/**@{*/
1176
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001177/** \brief A function that Sets up a secure element key derivation operation by
1178 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +01001179 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001180 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +02001181 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001182 * context information for the implementation
1183 * \param[in] kdf_alg The algorithm to be used for the key derivation
1184 * \param[in] source_key The key to be used as the source material for
1185 * the key derivation
Gilles Peskine75976892018-12-12 15:55:09 +01001186 *
Gilles Peskineed733552023-02-14 19:21:09 +01001187 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +01001188 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001189typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1190 void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001191 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -06001192 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001193
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001194/** \brief A function that provides collateral (parameters) needed for a secure
1195 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +01001196 *
1197 * Since many key derivation algorithms require multiple parameters, it is
Tobias Nießen1e8ca122021-05-10 19:53:15 +02001198 * expected that this function may be called multiple times for the same
Gilles Peskine75976892018-12-12 15:55:09 +01001199 * operation, each with a different algorithm-specific `collateral_id`
1200 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001201 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001202 * context information for the implementation
1203 * \param[in] collateral_id An ID for the collateral being provided
1204 * \param[in] p_collateral A buffer containing the collateral data
1205 * \param[in] collateral_size The size in bytes of the collateral
1206 *
Gilles Peskineed733552023-02-14 19:21:09 +01001207 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +01001208 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001209typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001210 uint32_t collateral_id,
1211 const uint8_t *p_collateral,
1212 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +01001213
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001214/** \brief A function that performs the final secure element key derivation
1215 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +01001216 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001217 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001218 * context information for the implementation
1219 * \param[in] dest_key The slot where the generated key material
1220 * should be placed
1221 *
Gilles Peskineed733552023-02-14 19:21:09 +01001222 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +01001223 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001224typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001226
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001227/** \brief A function that performs the final step of a secure element key
1228 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001229 *
1230 * \param[out] p_output Buffer in which to place the generated key
1231 * material
1232 * \param[in] output_size The size in bytes of `p_output`
1233 * \param[out] p_output_length Upon success, contains the number of bytes of
1234 * key material placed in `p_output`
1235 *
Gilles Peskineed733552023-02-14 19:21:09 +01001236 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskine75976892018-12-12 15:55:09 +01001237 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001238typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001239 uint8_t *p_output,
1240 size_t output_size,
1241 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001242
1243/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001244 * \brief A struct containing all of the function pointers needed to for secure
1245 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001246 *
1247 * PSA Crypto API implementations should populate instances of the table as
1248 * appropriate upon startup.
1249 *
1250 * If one of the functions is not implemented, it should be set to NULL.
1251 */
1252typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001253 /** The driver-specific size of the key derivation context */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001254 size_t MBEDTLS_PRIVATE(context_size);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001255 /** Function that performs a key derivation setup */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001256 psa_drv_se_key_derivation_setup_t MBEDTLS_PRIVATE(p_setup);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001257 /** Function that sets key derivation collateral */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001258 psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral);
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001259 /** Function that performs a final key derivation step */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001260 psa_drv_se_key_derivation_derive_t MBEDTLS_PRIVATE(p_derive);
Tom Cosgrove1797b052022-12-04 17:19:59 +00001261 /** Function that performs a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001262 * exports the key */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001263 psa_drv_se_key_derivation_export_t MBEDTLS_PRIVATE(p_export);
Derek Miller83d26622019-02-15 16:41:22 -06001264} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001265
1266/**@}*/
1267
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001268/** \defgroup se_registration Secure element driver registration
1269 */
1270/**@{*/
1271
1272/** A structure containing pointers to all the entry points of a
1273 * secure element driver.
1274 *
1275 * Future versions of this specification may add extra substructures at
1276 * the end of this structure.
1277 */
1278typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001279 /** The version of the driver HAL that this driver implements.
1280 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001281 * a different version of this specification.
1282 * Use #PSA_DRV_SE_HAL_VERSION.
1283 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001284 uint32_t MBEDTLS_PRIVATE(hal_version);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001285
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +02001286 /** The size of the driver's persistent data in bytes.
1287 *
1288 * This can be 0 if the driver does not need persistent data.
1289 *
1290 * See the documentation of psa_drv_se_context_t::persistent_data
1291 * for more information about why and how a driver can use
1292 * persistent data.
1293 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001294 size_t MBEDTLS_PRIVATE(persistent_data_size);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001295
1296 /** The driver initialization function.
1297 *
1298 * This function is called once during the initialization of the
1299 * PSA Cryptography subsystem, before any other function of the
1300 * driver is called. If this function returns a failure status,
1301 * the driver will be unusable, at least until the next system reset.
1302 *
1303 * If this field is \c NULL, it is equivalent to a function that does
1304 * nothing and returns #PSA_SUCCESS.
1305 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001306 psa_drv_se_init_t MBEDTLS_PRIVATE(p_init);
Gilles Peskine7a86da12019-07-12 23:25:38 +02001307
Mateusz Starzyk846f0212021-05-19 19:44:07 +02001308 const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management);
1309 const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac);
1310 const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher);
1311 const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead);
1312 const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric);
1313 const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation);
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001314} psa_drv_se_t;
1315
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001316/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001317 */
1318/* 0.0.0 patchlevel 5 */
1319#define PSA_DRV_SE_HAL_VERSION 0x00000005
1320
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001321/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001322 *
1323 * This function is only intended to be used by driver code, not by
1324 * application code. In implementations with separation between the
1325 * PSA cryptography module and applications, this function should
1326 * only be available to callers that run in the same memory space as
1327 * the cryptography module, and should not be exposed to applications
1328 * running in a different memory space.
1329 *
1330 * This function may be called before psa_crypto_init(). It is
1331 * implementation-defined whether this function may be called
1332 * after psa_crypto_init().
1333 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001334 * \note Implementations store metadata about keys including the lifetime
Gilles Peskine52ac9582020-05-10 00:39:18 +02001335 * value, which contains the driver's location indicator. Therefore,
1336 * from one instantiation of the PSA Cryptography
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001337 * library to the next one, if there is a key in storage with a certain
1338 * lifetime value, you must always register the same driver (or an
1339 * updated version that communicates with the same secure element)
Gilles Peskine52ac9582020-05-10 00:39:18 +02001340 * with the same location value.
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001341 *
Gilles Peskine52ac9582020-05-10 00:39:18 +02001342 * \param location The location value through which this driver will
Gilles Peskined910e922019-06-24 13:47:07 +02001343 * be exposed to applications.
Gilles Peskine52ac9582020-05-10 00:39:18 +02001344 * This driver will be used for all keys such that
Ronald Cron96783552020-10-19 12:06:30 +02001345 * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`.
Gilles Peskine52ac9582020-05-10 00:39:18 +02001346 * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
1347 * and may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001348 * may reserve other values.
1349 * \param[in] methods The method table of the driver. This structure must
1350 * remain valid for as long as the cryptography
1351 * module keeps running. It is typically a global
1352 * constant.
1353 *
Ronald Cron96783552020-10-19 12:06:30 +02001354 * \return #PSA_SUCCESS
Gilles Peskined910e922019-06-24 13:47:07 +02001355 * The driver was successfully registered. Applications can now
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001356 * use \p location to access keys through the methods passed to
Gilles Peskined910e922019-06-24 13:47:07 +02001357 * this function.
Ronald Cron96783552020-10-19 12:06:30 +02001358 * \return #PSA_ERROR_BAD_STATE
Gilles Peskined910e922019-06-24 13:47:07 +02001359 * This function was called after the initialization of the
1360 * cryptography module, and this implementation does not support
1361 * driver registration at this stage.
Ronald Cron96783552020-10-19 12:06:30 +02001362 * \return #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001363 * There is already a registered driver for this value of \p location.
Ronald Cron96783552020-10-19 12:06:30 +02001364 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001365 * \p location is a reserved value.
Ronald Cron96783552020-10-19 12:06:30 +02001366 * \return #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001367 * `methods->hal_version` is not supported by this implementation.
Ronald Cron96783552020-10-19 12:06:30 +02001368 * \return #PSA_ERROR_INSUFFICIENT_MEMORY
1369 * \return #PSA_ERROR_NOT_PERMITTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001370 * \return #PSA_ERROR_STORAGE_FAILURE
1371 * \return #PSA_ERROR_DATA_CORRUPT
Gilles Peskined910e922019-06-24 13:47:07 +02001372 */
1373psa_status_t psa_register_se_driver(
Gilles Peskine344e15b2020-05-10 00:44:30 +02001374 psa_key_location_t location,
Gilles Peskined910e922019-06-24 13:47:07 +02001375 const psa_drv_se_t *methods);
1376
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001377/**@}*/
1378
Gilles Peskine75976892018-12-12 15:55:09 +01001379#ifdef __cplusplus
1380}
1381#endif
1382
1383#endif /* PSA_CRYPTO_SE_DRIVER_H */