blob: 69cdababac14d9fb80a023cb9a05065b99e151c9 [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/*
19 * Copyright (C) 2018, ARM Limited, All Rights Reserved
20 * 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
36
37#include "crypto_driver_common.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Gilles Peskine7a86da12019-07-12 23:25:38 +020043/** \defgroup se_init Secure element driver initialization
44 */
45/**@{*/
46
47/** \brief Driver context structure
48 *
49 * Driver functions receive a pointer to this structure.
50 * Each registered driver has one instance of this structure.
51 *
52 * Implementations must include the fields specified here and
53 * may include other fields.
54 */
55typedef struct {
56 /** A read-only pointer to the driver's persistent data.
57 *
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020058 * Drivers typically use this persistent data to keep track of
59 * which slot numbers are available. This is only a guideline:
60 * drivers may use the persistent data for any purpose, keeping
61 * in mind the restrictions on when the persistent data is saved
62 * to storage: the persistent data is only saved after calling
63 * certain functions that receive a writable pointer to the
64 * persistent data.
Gilles Peskine7a86da12019-07-12 23:25:38 +020065 *
66 * The core allocates a memory buffer for the persistent data.
Gilles Peskine6a3dd892019-07-25 10:56:39 +020067 * The pointer is guaranteed to be suitably aligned for any data type,
Gilles Peskine7a86da12019-07-12 23:25:38 +020068 * like a pointer returned by `malloc` (but the core can use any
69 * method to allocate the buffer, not necessarily `malloc`).
70 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +020071 * The size of this buffer is in the \c persistent_data_size field of
72 * this structure.
Gilles Peskine7a86da12019-07-12 23:25:38 +020073 *
74 * Before the driver is initialized for the first time, the content of
75 * the persistent data is all-bits-zero. After a driver upgrade, if the
76 * size of the persistent data has increased, the original data is padded
77 * on the right with zeros; if the size has decreased, the original data
78 * is truncated to the new size.
79 *
80 * This pointer is to read-only data. Only a few driver functions are
81 * allowed to modify the persistent data. These functions receive a
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +020082 * writable pointer. These functions are:
83 * - psa_drv_se_t::p_init
84 * - psa_drv_se_key_management_t::p_allocate
85 * - psa_drv_se_key_management_t::p_destroy
86 *
87 * The PSA Cryptography core saves the persistent data from one
88 * session to the next. It does this before returning from API functions
89 * that call a driver method that is allowed to modify the persistent
90 * data, specifically:
91 * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
92 * psa_drv_se_key_management_t::p_destroy to complete an action
93 * that was interrupted by a power failure.
94 * - Key creation functions cause a call to
95 * psa_drv_se_key_management_t::p_allocate, and may cause a call to
96 * psa_drv_se_key_management_t::p_destroy in case an error occurs.
97 * - psa_destroy_key() causes a call to
98 * psa_drv_se_key_management_t::p_destroy.
Gilles Peskine7a86da12019-07-12 23:25:38 +020099 */
100 const void *const persistent_data;
101
102 /** The size of \c persistent_data in bytes.
103 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +0200104 * This is always equal to the value of the `persistent_data_size` field
105 * of the ::psa_drv_se_t structure when the driver is registered.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200106 */
107 const size_t persistent_data_size;
108
109 /** Driver transient data.
110 *
111 * The core initializes this value to 0 and does not read or modify it
112 * afterwards. The driver may store whatever it wants in this field.
113 */
114 uintptr_t transient_data;
115} psa_drv_se_context_t;
116
117/** \brief A driver initialization function.
118 *
119 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200120 * \param[in,out] persistent_data A pointer to the persistent data
121 * that allows writing.
Gilles Peskine7a86da12019-07-12 23:25:38 +0200122 * \param lifetime The lifetime value for which this driver
123 * is registered.
124 *
125 * \retval #PSA_SUCCESS
126 * The driver is operational.
127 * The core will update the persistent data in storage.
128 * \return
129 * Any other return value prevents the driver from being used in
130 * this session.
131 * The core will NOT update the persistent data in storage.
132 */
133typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200134 void *persistent_data,
Gilles Peskine7a86da12019-07-12 23:25:38 +0200135 psa_key_lifetime_t lifetime);
136
Gilles Peskinec8000c02019-08-02 20:15:51 +0200137#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
138/* Mbed Crypto with secure element support enabled defines this type in
139 * crypto_types.h because it is also visible to applications through an
140 * implementation-specific extension.
141 * For the PSA Cryptography specification, this type is only visible
142 * via crypto_se_driver.h. */
Gilles Peskine75976892018-12-12 15:55:09 +0100143/** An internal designation of a key slot between the core part of the
144 * PSA Crypto implementation and the driver. The meaning of this value
145 * is driver-dependent. */
Gilles Peskinef03143a2019-07-12 23:18:29 +0200146typedef uint64_t psa_key_slot_number_t;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200147#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine75976892018-12-12 15:55:09 +0100148
Gilles Peskine7a86da12019-07-12 23:25:38 +0200149/**@}*/
150
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600151/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +0100152 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600153 * a secure element can be done either as a single function call (via the
154 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +0100155 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600156 * - `psa_drv_se_mac_setup_t`
157 * - `psa_drv_se_mac_update_t`
158 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100159 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600160 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100161 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600162 * If a previously started secure element MAC operation needs to be terminated,
163 * 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 +0100164 * result in allocated resources not being freed or in other undefined
165 * behavior.
166 */
167/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600168/** \brief A function that starts a secure element MAC operation for a PSA
169 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +0100170 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200171 * \param[in,out] drv_context The driver context structure.
172 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100173 * hardware-specific MAC context
174 * \param[in] key_slot The slot of the key to be used for the
175 * operation
176 * \param[in] algorithm The algorithm to be used to underly the MAC
177 * operation
178 *
179 * \retval PSA_SUCCESS
180 * Success.
181 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200182typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
183 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600184 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600185 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100186
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600187/** \brief A function that continues a previously started secure element MAC
188 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100189 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200190 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100191 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600192 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100193 * \param[in] p_input A buffer containing the message to be appended
194 * to the MAC operation
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200195 * \param[in] input_length The size in bytes of the input message buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100196 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200197typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600198 const uint8_t *p_input,
199 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100200
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600201/** \brief a function that completes a previously started secure element MAC
202 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100203 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200204 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100205 * previously started MAC operation to be
206 * finished
207 * \param[out] p_mac A buffer where the generated MAC will be
208 * placed
209 * \param[in] mac_size The size in bytes of the buffer that has been
210 * allocated for the `output` buffer
211 * \param[out] p_mac_length After completion, will contain the number of
212 * bytes placed in the `p_mac` buffer
213 *
214 * \retval PSA_SUCCESS
215 * Success.
216 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200217typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600218 uint8_t *p_mac,
219 size_t mac_size,
220 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100221
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600222/** \brief A function that completes a previously started secure element MAC
223 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100224 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200225 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200226 * started MAC operation to be fiinished
227 * \param[in] p_mac The MAC value against which the resulting MAC
228 * will be compared against
229 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Gilles Peskine75976892018-12-12 15:55:09 +0100230 *
231 * \retval PSA_SUCCESS
232 * The operation completed successfully and the MACs matched each
233 * other
234 * \retval PSA_ERROR_INVALID_SIGNATURE
235 * The operation completed successfully, but the calculated MAC did
236 * not match the provided MAC
237 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200238typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600239 const uint8_t *p_mac,
240 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100241
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600242/** \brief A function that aborts a previous started secure element MAC
243 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100244 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200245 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200246 * started MAC operation to be aborted
Gilles Peskine75976892018-12-12 15:55:09 +0100247 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200248typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100249
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600250/** \brief A function that performs a secure element MAC operation in one
251 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100252 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200253 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100254 * \param[in] p_input A buffer containing the message to be MACed
255 * \param[in] input_length The size in bytes of `p_input`
256 * \param[in] key_slot The slot of the key to be used
257 * \param[in] alg The algorithm to be used to underlie the MAC
258 * operation
259 * \param[out] p_mac A buffer where the generated MAC will be
260 * placed
261 * \param[in] mac_size The size in bytes of the `p_mac` buffer
262 * \param[out] p_mac_length After completion, will contain the number of
263 * bytes placed in the `output` buffer
264 *
265 * \retval PSA_SUCCESS
266 * Success.
267 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200268typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
269 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600270 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600271 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600272 psa_algorithm_t alg,
273 uint8_t *p_mac,
274 size_t mac_size,
275 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100276
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600277/** \brief A function that performs a secure element MAC operation in one
278 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100279 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200280 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100281 * \param[in] p_input A buffer containing the message to be MACed
282 * \param[in] input_length The size in bytes of `input`
283 * \param[in] key_slot The slot of the key to be used
284 * \param[in] alg The algorithm to be used to underlie the MAC
285 * operation
286 * \param[in] p_mac The MAC value against which the resulting MAC will
287 * be compared against
288 * \param[in] mac_length The size in bytes of `mac`
289 *
290 * \retval PSA_SUCCESS
291 * The operation completed successfully and the MACs matched each
292 * other
293 * \retval PSA_ERROR_INVALID_SIGNATURE
294 * The operation completed successfully, but the calculated MAC did
295 * not match the provided MAC
296 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200297typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
298 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600299 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600300 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600301 psa_algorithm_t alg,
302 const uint8_t *p_mac,
303 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100304
305/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600306 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100307 *
308 * PSA Crypto API implementations should populate the table as appropriate
309 * upon startup.
310 *
311 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600312 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100313 *
314 * Driver implementers should ensure that they implement all of the functions
315 * that make sense for their hardware, and that they provide a full solution
316 * (for example, if they support `p_setup`, they should also support
317 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
318 *
319 */
320typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600321 /**The size in bytes of the hardware-specific secure element MAC context
322 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100323 */
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600324 size_t context_size;
325 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100326 */
Derek Millerea743cf2019-02-15 17:06:29 -0600327 psa_drv_se_mac_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600328 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100329 */
Derek Millerea743cf2019-02-15 17:06:29 -0600330 psa_drv_se_mac_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600331 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100332 */
Derek Millerea743cf2019-02-15 17:06:29 -0600333 psa_drv_se_mac_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600334 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100335 */
Derek Millerea743cf2019-02-15 17:06:29 -0600336 psa_drv_se_mac_finish_verify_t p_finish_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600337 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100338 */
Derek Millerea743cf2019-02-15 17:06:29 -0600339 psa_drv_se_mac_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600340 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100341 */
Derek Millerea743cf2019-02-15 17:06:29 -0600342 psa_drv_se_mac_generate_t p_mac;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600343 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100344 */
Derek Millerea743cf2019-02-15 17:06:29 -0600345 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600346} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100347/**@}*/
348
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600349/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100350 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600351 * Encryption and Decryption using secure element keys in block modes other
352 * than ECB must be done in multiple parts, using the following flow:
353 * - `psa_drv_se_cipher_setup_t`
354 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
355 * - `psa_drv_se_cipher_update_t`
356 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100357 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600358 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100359 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600360 * If a previously started secure element Cipher operation needs to be
361 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
362 * to do so may result in allocated resources not being freed or in other
363 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100364 *
365 * In situations where a PSA Cryptographic API implementation is using a block
366 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600367 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
368 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100369 */
370/**@{*/
371
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600372/** \brief A function that provides the cipher setup function for a
373 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100374 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200375 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +0200376 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100377 * hardware-specific cipher context.
378 * \param[in] key_slot The slot of the key to be used for the
379 * operation
380 * \param[in] algorithm The algorithm to be used in the cipher
381 * operation
382 * \param[in] direction Indicates whether the operation is an encrypt
383 * or decrypt
384 *
385 * \retval PSA_SUCCESS
386 * \retval PSA_ERROR_NOT_SUPPORTED
387 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200388typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
389 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600390 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600391 psa_algorithm_t algorithm,
392 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100393
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600394/** \brief A function that sets the initialization vector (if
395 * necessary) for an secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100396 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600397 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
398 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100399 * generate function is not necessary for the drivers to implement as the PSA
400 * Crypto implementation can do the generation using its RNG features.
401 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200402 * \param[in,out] op_context A structure that contains the previously set up
Gilles Peskine75976892018-12-12 15:55:09 +0100403 * hardware-specific cipher context
404 * \param[in] p_iv A buffer containing the initialization vector
405 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
406 *
407 * \retval PSA_SUCCESS
408 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200409typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600410 const uint8_t *p_iv,
411 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100412
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600413/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100414 * operation
415 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200416 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100417 * previously started cipher operation
418 * \param[in] p_input A buffer containing the data to be
419 * encrypted/decrypted
420 * \param[in] input_size The size in bytes of the buffer pointed to
421 * by `p_input`
422 * \param[out] p_output The caller-allocated buffer where the
423 * output will be placed
424 * \param[in] output_size The allocated size in bytes of the
425 * `p_output` buffer
426 * \param[out] p_output_length After completion, will contain the number
427 * of bytes placed in the `p_output` buffer
428 *
429 * \retval PSA_SUCCESS
430 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200431typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600432 const uint8_t *p_input,
433 size_t input_size,
434 uint8_t *p_output,
435 size_t output_size,
436 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100437
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600438/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100439 * operation
440 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200441 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100442 * previously started cipher operation
443 * \param[out] p_output The caller-allocated buffer where the output
444 * will be placed
445 * \param[in] output_size The allocated size in bytes of the `p_output`
446 * buffer
447 * \param[out] p_output_length After completion, will contain the number of
448 * bytes placed in the `p_output` buffer
449 *
450 * \retval PSA_SUCCESS
451 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200452typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600453 uint8_t *p_output,
454 size_t output_size,
455 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100456
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600457/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100458 * operation
459 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200460 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100461 * previously started cipher operation
462 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200463typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100464
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600465/** \brief A function that performs the ECB block mode for secure element
466 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100467 *
468 * Note: this function should only be used with implementations that do not
469 * provide a needed higher-level operation.
470 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200471 * \param[in,out] drv_context The driver context structure.
472 * \param[in] key_slot The slot of the key to be used for the operation
473 * \param[in] algorithm The algorithm to be used in the cipher operation
474 * \param[in] direction Indicates whether the operation is an encrypt or
475 * decrypt
476 * \param[in] p_input A buffer containing the data to be
477 * encrypted/decrypted
478 * \param[in] input_size The size in bytes of the buffer pointed to by
479 * `p_input`
480 * \param[out] p_output The caller-allocated buffer where the output
481 * will be placed
482 * \param[in] output_size The allocated size in bytes of the `p_output`
483 * buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100484 *
485 * \retval PSA_SUCCESS
486 * \retval PSA_ERROR_NOT_SUPPORTED
487 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200488typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
489 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600490 psa_algorithm_t algorithm,
491 psa_encrypt_or_decrypt_t direction,
492 const uint8_t *p_input,
493 size_t input_size,
494 uint8_t *p_output,
495 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100496
497/**
498 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600499 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100500 *
501 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600502 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100503 *
504 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600505 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100506 */
507typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600508 /** The size in bytes of the hardware-specific secure element cipher
509 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100510 */
Derek Miller34b33f12019-02-15 17:13:54 -0600511 size_t context_size;
512 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600513 psa_drv_se_cipher_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600514 /** Function that sets a cipher IV (if necessary) */
Derek Millerea743cf2019-02-15 17:06:29 -0600515 psa_drv_se_cipher_set_iv_t p_set_iv;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600516 /** Function that performs a cipher update operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600517 psa_drv_se_cipher_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600518 /** Function that completes a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600519 psa_drv_se_cipher_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600520 /** Function that aborts a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600521 psa_drv_se_cipher_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600522 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100523 * (Danger: ECB mode should not be used directly by clients of the PSA
524 * Crypto Client API)
525 */
Derek Millerea743cf2019-02-15 17:06:29 -0600526 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600527} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100528
529/**@}*/
530
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600531/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100532 *
533 * Since the amount of data that can (or should) be encrypted or signed using
534 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600535 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100536 */
537/**@{*/
538
539/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600540 * \brief A function that signs a hash or short message with a private key in
541 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100542 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200543 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100544 * \param[in] key_slot Key slot of an asymmetric key pair
545 * \param[in] alg A signature algorithm that is compatible
546 * with the type of `key`
547 * \param[in] p_hash The hash to sign
548 * \param[in] hash_length Size of the `p_hash` buffer in bytes
549 * \param[out] p_signature Buffer where the signature is to be written
550 * \param[in] signature_size Size of the `p_signature` buffer in bytes
551 * \param[out] p_signature_length On success, the number of bytes
552 * that make up the returned signature value
553 *
554 * \retval PSA_SUCCESS
555 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200556typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
557 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600558 psa_algorithm_t alg,
559 const uint8_t *p_hash,
560 size_t hash_length,
561 uint8_t *p_signature,
562 size_t signature_size,
563 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100564
565/**
566 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600567 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100568 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200569 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100570 * \param[in] key_slot Key slot of a public key or an asymmetric key
571 * pair
572 * \param[in] alg A signature algorithm that is compatible with
573 * the type of `key`
574 * \param[in] p_hash The hash whose signature is to be verified
575 * \param[in] hash_length Size of the `p_hash` buffer in bytes
576 * \param[in] p_signature Buffer containing the signature to verify
577 * \param[in] signature_length Size of the `p_signature` buffer in bytes
578 *
579 * \retval PSA_SUCCESS
580 * The signature is valid.
581 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200582typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
583 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600584 psa_algorithm_t alg,
585 const uint8_t *p_hash,
586 size_t hash_length,
587 const uint8_t *p_signature,
588 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100589
590/**
591 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600592 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100593 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200594 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100595 * \param[in] key_slot Key slot of a public key or an asymmetric key
596 * pair
597 * \param[in] alg An asymmetric encryption algorithm that is
598 * compatible with the type of `key`
599 * \param[in] p_input The message to encrypt
600 * \param[in] input_length Size of the `p_input` buffer in bytes
601 * \param[in] p_salt A salt or label, if supported by the
602 * encryption algorithm
603 * If the algorithm does not support a
604 * salt, pass `NULL`.
605 * If the algorithm supports an optional
606 * salt and you do not want to pass a salt,
607 * pass `NULL`.
608 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
609 * supported.
610 * \param[in] salt_length Size of the `p_salt` buffer in bytes
611 * If `p_salt` is `NULL`, pass 0.
612 * \param[out] p_output Buffer where the encrypted message is to
613 * be written
614 * \param[in] output_size Size of the `p_output` buffer in bytes
615 * \param[out] p_output_length On success, the number of bytes that make up
616 * the returned output
617 *
618 * \retval PSA_SUCCESS
619 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200620typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
621 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600622 psa_algorithm_t alg,
623 const uint8_t *p_input,
624 size_t input_length,
625 const uint8_t *p_salt,
626 size_t salt_length,
627 uint8_t *p_output,
628 size_t output_size,
629 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100630
631/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600632 * \brief A function that decrypts a short message with an asymmetric private
633 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100634 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200635 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100636 * \param[in] key_slot Key slot of an asymmetric key pair
637 * \param[in] alg An asymmetric encryption algorithm that is
638 * compatible with the type of `key`
639 * \param[in] p_input The message to decrypt
640 * \param[in] input_length Size of the `p_input` buffer in bytes
641 * \param[in] p_salt A salt or label, if supported by the
642 * encryption algorithm
643 * If the algorithm does not support a
644 * salt, pass `NULL`.
645 * If the algorithm supports an optional
646 * salt and you do not want to pass a salt,
647 * pass `NULL`.
648 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
649 * supported.
650 * \param[in] salt_length Size of the `p_salt` buffer in bytes
651 * If `p_salt` is `NULL`, pass 0.
652 * \param[out] p_output Buffer where the decrypted message is to
653 * be written
654 * \param[in] output_size Size of the `p_output` buffer in bytes
655 * \param[out] p_output_length On success, the number of bytes
656 * that make up the returned output
657 *
658 * \retval PSA_SUCCESS
659 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200660typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
661 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600662 psa_algorithm_t alg,
663 const uint8_t *p_input,
664 size_t input_length,
665 const uint8_t *p_salt,
666 size_t salt_length,
667 uint8_t *p_output,
668 size_t output_size,
669 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100670
671/**
672 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600673 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100674 *
675 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600676 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100677 *
678 * If one of the functions is not implemented, it should be set to NULL.
679 */
680typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600681 /** Function that performs an asymmetric sign operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600682 psa_drv_se_asymmetric_sign_t p_sign;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600683 /** Function that performs an asymmetric verify operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600684 psa_drv_se_asymmetric_verify_t p_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600685 /** Function that performs an asymmetric encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600686 psa_drv_se_asymmetric_encrypt_t p_encrypt;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600687 /** Function that performs an asymmetric decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600688 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600689} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100690
691/**@}*/
692
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600693/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
694 * Authenticated Encryption with Additional Data (AEAD) operations with secure
695 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100696 * implementers as there must be sufficient space in memory for the entire
697 * message, it prevents decrypted data from being made available before the
698 * authentication operation is complete and the data is known to be authentic.
699 */
700/**@{*/
701
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600702/** \brief A function that performs a secure element authenticated encryption
703 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100704 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200705 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100706 * \param[in] key_slot Slot containing the key to use.
707 * \param[in] algorithm The AEAD algorithm to compute
708 * (\c PSA_ALG_XXX value such that
709 * #PSA_ALG_IS_AEAD(`alg`) is true)
710 * \param[in] p_nonce Nonce or IV to use
711 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
712 * \param[in] p_additional_data Additional data that will be
713 * authenticated but not encrypted
714 * \param[in] additional_data_length Size of `p_additional_data` in bytes
715 * \param[in] p_plaintext Data that will be authenticated and
716 * encrypted
717 * \param[in] plaintext_length Size of `p_plaintext` in bytes
718 * \param[out] p_ciphertext Output buffer for the authenticated and
719 * encrypted data. The additional data is
720 * not part of this output. For algorithms
721 * where the encrypted data and the
722 * authentication tag are defined as
723 * separate outputs, the authentication
724 * tag is appended to the encrypted data.
725 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
726 * bytes
727 * \param[out] p_ciphertext_length On success, the size of the output in
728 * the `p_ciphertext` buffer
729 *
730 * \retval #PSA_SUCCESS
731 * Success.
732 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200733typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
734 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600735 psa_algorithm_t algorithm,
736 const uint8_t *p_nonce,
737 size_t nonce_length,
738 const uint8_t *p_additional_data,
739 size_t additional_data_length,
740 const uint8_t *p_plaintext,
741 size_t plaintext_length,
742 uint8_t *p_ciphertext,
743 size_t ciphertext_size,
744 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100745
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600746/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100747 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200748 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100749 * \param[in] key_slot Slot containing the key to use
750 * \param[in] algorithm The AEAD algorithm to compute
751 * (\c PSA_ALG_XXX value such that
752 * #PSA_ALG_IS_AEAD(`alg`) is true)
753 * \param[in] p_nonce Nonce or IV to use
754 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
755 * \param[in] p_additional_data Additional data that has been
756 * authenticated but not encrypted
757 * \param[in] additional_data_length Size of `p_additional_data` in bytes
758 * \param[in] p_ciphertext Data that has been authenticated and
759 * encrypted.
760 * For algorithms where the encrypted data
761 * and the authentication tag are defined
762 * as separate inputs, the buffer must
763 * contain the encrypted data followed by
764 * the authentication tag.
765 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
766 * \param[out] p_plaintext Output buffer for the decrypted data
767 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
768 * bytes
769 * \param[out] p_plaintext_length On success, the size of the output in
770 * the `p_plaintext` buffer
771 *
772 * \retval #PSA_SUCCESS
773 * Success.
774 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200775typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
776 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600777 psa_algorithm_t algorithm,
778 const uint8_t *p_nonce,
779 size_t nonce_length,
780 const uint8_t *p_additional_data,
781 size_t additional_data_length,
782 const uint8_t *p_ciphertext,
783 size_t ciphertext_length,
784 uint8_t *p_plaintext,
785 size_t plaintext_size,
786 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100787
788/**
789 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600790 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100791 *
792 * PSA Crypto API implementations should populate instances of the table as
793 * appropriate upon startup.
794 *
795 * If one of the functions is not implemented, it should be set to NULL.
796 */
797typedef struct {
798 /** Function that performs the AEAD encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600799 psa_drv_se_aead_encrypt_t p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100800 /** Function that performs the AEAD decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600801 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600802} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100803/**@}*/
804
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600805/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100806 * Currently, key management is limited to importing keys in the clear,
807 * destroying keys, and exporting keys in the clear.
808 * Whether a key may be exported is determined by the key policies in place
809 * on the key slot.
810 */
811/**@{*/
812
Gilles Peskinef2223c82019-07-12 23:33:02 +0200813/** \brief A function that allocates a slot for a key.
814 *
815 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200816 * \param[in,out] persistent_data A pointer to the persistent data
817 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200818 * \param[in] attributes Attributes of the key.
819 * \param[out] key_slot Slot where the key will be stored.
820 * This must be a valid slot for a key of the
821 * chosen type. It must be unoccupied.
Gilles Peskinef2223c82019-07-12 23:33:02 +0200822 *
823 * \retval #PSA_SUCCESS
824 * Success.
825 * The core will record \c *key_slot as the key slot where the key
826 * is stored and will update the persistent data in storage.
827 * \retval #PSA_ERROR_NOT_SUPPORTED
828 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
829 */
830typedef psa_status_t (*psa_drv_se_allocate_key_t)(
831 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200832 void *persistent_data,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200833 const psa_key_attributes_t *attributes,
834 psa_key_slot_number_t *key_slot);
835
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600836/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100837 *
838 * This function can support any output from psa_export_key(). Refer to the
839 * documentation of psa_export_key() for the format for each key type.
840 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200841 * \param[in,out] drv_context The driver context structure.
842 * \param[in] key_slot Slot where the key will be stored
Gilles Peskine18017402019-07-24 20:25:59 +0200843 * This must be a valid slot for a key of the
844 * chosen type. It must be unoccupied.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200845 * \param[in] lifetime The required lifetime of the key storage
846 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
847 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
848 * \param[in] usage The allowed uses of the key
849 * \param[in] p_data Buffer containing the key data
850 * \param[in] data_length Size of the `data` buffer in bytes
Gilles Peskine18017402019-07-24 20:25:59 +0200851 * \param[out] bits On success, the key size in bits. The driver
852 * must determine this value after parsing the
853 * key according to the key type.
854 * This value is not used if the function fails.
Gilles Peskine75976892018-12-12 15:55:09 +0100855 *
856 * \retval #PSA_SUCCESS
857 * Success.
858 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200859typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_context,
860 psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600861 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600862 psa_key_type_t type,
863 psa_algorithm_t algorithm,
864 psa_key_usage_t usage,
865 const uint8_t *p_data,
Gilles Peskine18017402019-07-24 20:25:59 +0200866 size_t data_length,
867 size_t *bits);
Gilles Peskine75976892018-12-12 15:55:09 +0100868
869/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600870 * \brief A function that destroys a secure element key and restore the slot to
871 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100872 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600873 * This function destroys the content of the key from a secure element.
874 * Implementations shall make a best effort to ensure that any previous content
875 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100876 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600877 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100878 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200879 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200880 * \param[in,out] persistent_data A pointer to the persistent data
881 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200882 * \param key_slot The key slot to erase.
Gilles Peskine75976892018-12-12 15:55:09 +0100883 *
884 * \retval #PSA_SUCCESS
885 * The slot's content, if any, has been erased.
886 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200887typedef psa_status_t (*psa_drv_se_destroy_key_t)(
888 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200889 void *persistent_data,
Gilles Peskine8597bc12019-07-12 23:28:46 +0200890 psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +0100891
892/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600893 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100894 *
895 * The output of this function can be passed to psa_import_key() to
896 * create an equivalent object.
897 *
898 * If a key is created with `psa_import_key()` and then exported with
899 * this function, it is not guaranteed that the resulting data is
900 * identical: the implementation may choose a different representation
901 * of the same key if the format permits it.
902 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600903 * This function should generate output in the same format that
904 * `psa_export_key()` does. Refer to the
905 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100906 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200907 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100908 * \param[in] key Slot whose content is to be exported. This must
909 * be an occupied key slot.
910 * \param[out] p_data Buffer where the key data is to be written.
911 * \param[in] data_size Size of the `p_data` buffer in bytes.
912 * \param[out] p_data_length On success, the number of bytes
913 * that make up the key data.
914 *
915 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +0200916 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine75976892018-12-12 15:55:09 +0100917 * \retval #PSA_ERROR_NOT_PERMITTED
918 * \retval #PSA_ERROR_NOT_SUPPORTED
919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200921 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine75976892018-12-12 15:55:09 +0100922 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200923typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
924 psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600925 uint8_t *p_data,
926 size_t data_size,
927 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100928
929/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600930 * \brief A function that generates a symmetric or asymmetric key on a secure
931 * element
Gilles Peskine75976892018-12-12 15:55:09 +0100932 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +0200933 * If \p type is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) = 1),
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600934 * the public component of the generated key will be placed in `p_pubkey_out`.
935 * The format of the public key information will match the format specified for
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100936 * the psa_export_key() function for the key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100937 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200938 * \param[in,out] drv_context The driver context structure.
939 * \param[in] key_slot Slot where the generated key will be placed
940 * \param[in] type The type of the key to be generated
941 * \param[in] usage The prescribed usage of the generated key
942 * Note: Not all Secure Elements support the same
943 * restrictions that PSA Crypto does (and vice
944 * versa).
945 * Driver developers should endeavor to match the
946 * usages as close as possible.
947 * \param[in] bits The size in bits of the key to be generated.
948 * \param[in] extra Extra parameters for key generation. The
949 * interpretation of this parameter should match
950 * the interpretation in the `extra` parameter is
951 * the `psa_generate_key` function
952 * \param[in] extra_size The size in bytes of the \p extra buffer
953 * \param[out] p_pubkey_out The buffer where the public key information will
954 * be placed
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600955 * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer
956 * \param[out] p_pubkey_length Upon successful completion, will contain the
957 * size of the data placed in `p_pubkey_out`.
Gilles Peskine75976892018-12-12 15:55:09 +0100958 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200959typedef psa_status_t (*psa_drv_se_generate_key_t)(psa_drv_se_context_t *drv_context,
960 psa_key_slot_number_t key_slot,
Gilles Peskine32668ce2019-03-06 18:29:57 +0100961 psa_key_type_t type,
962 psa_key_usage_t usage,
963 size_t bits,
964 const void *extra,
965 size_t extra_size,
966 uint8_t *p_pubkey_out,
967 size_t pubkey_out_size,
968 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100969
970/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600971 * \brief A struct containing all of the function pointers needed to for secure
972 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +0100973 *
974 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600975 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100976 *
977 * If one of the functions is not implemented, it should be set to NULL.
978 */
979typedef struct {
Gilles Peskinef2223c82019-07-12 23:33:02 +0200980 /** Function that allocates a slot. */
981 psa_drv_se_allocate_key_t p_allocate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600982 /** Function that performs a key import operation */
983 psa_drv_se_import_key_t p_import;
984 /** Function that performs a generation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600985 psa_drv_se_generate_key_t p_generate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600986 /** Function that performs a key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600987 psa_drv_se_destroy_key_t p_destroy;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600988 /** Function that performs a key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600989 psa_drv_se_export_key_t p_export;
Gilles Peskinee62b74e2019-06-25 15:25:09 +0200990 /** Function that performs a public key export operation */
991 psa_drv_se_export_key_t p_export_public;
Derek Miller83d26622019-02-15 16:41:22 -0600992} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100993
994/**@}*/
995
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600996/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100997 * Key derivation is the process of generating new key material using an
998 * existing key and additional parameters, iterating through a basic
999 * cryptographic function, such as a hash.
1000 * Key agreement is a part of cryptographic protocols that allows two parties
1001 * to agree on the same key value, but starting from different original key
1002 * material.
1003 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
1004 * for both of the flows.
1005 *
1006 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001007 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1008 * `psa_drv_se_key_derivation_derive` is used when the key material should be
1009 * placed in a slot on the hardware and not exposed to the caller.
1010 * `psa_drv_se_key_derivation_export` is used when the key material should be
1011 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +01001012 *
1013 * Different key derivation algorithms require a different number of inputs.
1014 * Instead of having an API that takes as input variable length arrays, which
1015 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001016 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1017 * is called multiple times with different `collateral_id`s. Thus, for a key
Gilles Peskine75976892018-12-12 15:55:09 +01001018 * derivation algorithm that required 3 paramter inputs, the flow would look
1019 * something like:
1020 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001021 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1022 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1023 * p_collateral_0,
1024 * collateral_0_size);
1025 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1026 * p_collateral_1,
1027 * collateral_1_size);
1028 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1029 * p_collateral_2,
1030 * collateral_2_size);
1031 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +01001032 * ~~~~~~~~~~~~~
1033 *
1034 * key agreement example:
1035 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001036 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1037 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1038 * psa_drv_se_key_derivation_export(p_session_key,
1039 * session_key_size,
1040 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001041 * ~~~~~~~~~~~~~
1042 */
1043/**@{*/
1044
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001045/** \brief A function that Sets up a secure element key derivation operation by
1046 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +01001047 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001048 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +02001049 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001050 * context information for the implementation
1051 * \param[in] kdf_alg The algorithm to be used for the key derivation
1052 * \param[in] source_key The key to be used as the source material for
1053 * the key derivation
Gilles Peskine75976892018-12-12 15:55:09 +01001054 *
1055 * \retval PSA_SUCCESS
1056 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001057typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1058 void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001059 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -06001060 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001061
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001062/** \brief A function that provides collateral (parameters) needed for a secure
1063 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +01001064 *
1065 * Since many key derivation algorithms require multiple parameters, it is
1066 * expeced that this function may be called multiple times for the same
1067 * operation, each with a different algorithm-specific `collateral_id`
1068 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001069 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001070 * context information for the implementation
1071 * \param[in] collateral_id An ID for the collateral being provided
1072 * \param[in] p_collateral A buffer containing the collateral data
1073 * \param[in] collateral_size The size in bytes of the collateral
1074 *
1075 * \retval PSA_SUCCESS
1076 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001077typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001078 uint32_t collateral_id,
1079 const uint8_t *p_collateral,
1080 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +01001081
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001082/** \brief A function that performs the final secure element key derivation
1083 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +01001084 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001085 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001086 * context information for the implementation
1087 * \param[in] dest_key The slot where the generated key material
1088 * should be placed
1089 *
1090 * \retval PSA_SUCCESS
1091 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001092typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001093 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001094
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001095/** \brief A function that performs the final step of a secure element key
1096 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001097 *
1098 * \param[out] p_output Buffer in which to place the generated key
1099 * material
1100 * \param[in] output_size The size in bytes of `p_output`
1101 * \param[out] p_output_length Upon success, contains the number of bytes of
1102 * key material placed in `p_output`
1103 *
1104 * \retval PSA_SUCCESS
1105 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001106typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001107 uint8_t *p_output,
1108 size_t output_size,
1109 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001110
1111/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001112 * \brief A struct containing all of the function pointers needed to for secure
1113 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001114 *
1115 * PSA Crypto API implementations should populate instances of the table as
1116 * appropriate upon startup.
1117 *
1118 * If one of the functions is not implemented, it should be set to NULL.
1119 */
1120typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001121 /** The driver-specific size of the key derivation context */
1122 size_t context_size;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001123 /** Function that performs a key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -06001124 psa_drv_se_key_derivation_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001125 /** Function that sets key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -06001126 psa_drv_se_key_derivation_collateral_t p_collateral;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001127 /** Function that performs a final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -06001128 psa_drv_se_key_derivation_derive_t p_derive;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001129 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001130 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -06001131 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -06001132} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001133
1134/**@}*/
1135
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001136/** \defgroup se_registration Secure element driver registration
1137 */
1138/**@{*/
1139
1140/** A structure containing pointers to all the entry points of a
1141 * secure element driver.
1142 *
1143 * Future versions of this specification may add extra substructures at
1144 * the end of this structure.
1145 */
1146typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001147 /** The version of the driver HAL that this driver implements.
1148 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001149 * a different version of this specification.
1150 * Use #PSA_DRV_SE_HAL_VERSION.
1151 */
1152 uint32_t hal_version;
Gilles Peskine7a86da12019-07-12 23:25:38 +02001153
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +02001154 /** The size of the driver's persistent data in bytes.
1155 *
1156 * This can be 0 if the driver does not need persistent data.
1157 *
1158 * See the documentation of psa_drv_se_context_t::persistent_data
1159 * for more information about why and how a driver can use
1160 * persistent data.
1161 */
Gilles Peskine7a86da12019-07-12 23:25:38 +02001162 size_t persistent_data_size;
1163
1164 /** The driver initialization function.
1165 *
1166 * This function is called once during the initialization of the
1167 * PSA Cryptography subsystem, before any other function of the
1168 * driver is called. If this function returns a failure status,
1169 * the driver will be unusable, at least until the next system reset.
1170 *
1171 * If this field is \c NULL, it is equivalent to a function that does
1172 * nothing and returns #PSA_SUCCESS.
1173 */
1174 psa_drv_se_init_t p_init;
1175
Gilles Peskine6e59c422019-06-26 19:06:52 +02001176 const psa_drv_se_key_management_t *key_management;
1177 const psa_drv_se_mac_t *mac;
1178 const psa_drv_se_cipher_t *cipher;
1179 const psa_drv_se_aead_t *aead;
1180 const psa_drv_se_asymmetric_t *asymmetric;
1181 const psa_drv_se_key_derivation_t *derivation;
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001182} psa_drv_se_t;
1183
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001184/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001185 */
1186/* 0.0.0 patchlevel 5 */
1187#define PSA_DRV_SE_HAL_VERSION 0x00000005
1188
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001189/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001190 *
1191 * This function is only intended to be used by driver code, not by
1192 * application code. In implementations with separation between the
1193 * PSA cryptography module and applications, this function should
1194 * only be available to callers that run in the same memory space as
1195 * the cryptography module, and should not be exposed to applications
1196 * running in a different memory space.
1197 *
1198 * This function may be called before psa_crypto_init(). It is
1199 * implementation-defined whether this function may be called
1200 * after psa_crypto_init().
1201 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001202 * \note Implementations store metadata about keys including the lifetime
1203 * value. Therefore, from one instantiation of the PSA Cryptography
1204 * library to the next one, if there is a key in storage with a certain
1205 * lifetime value, you must always register the same driver (or an
1206 * updated version that communicates with the same secure element)
1207 * with the same lifetime value.
1208 *
Gilles Peskined910e922019-06-24 13:47:07 +02001209 * \param lifetime The lifetime value through which this driver will
1210 * be exposed to applications.
1211 * The values #PSA_KEY_LIFETIME_VOLATILE and
1212 * #PSA_KEY_LIFETIME_PERSISTENT are reserved and
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001213 * may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001214 * may reserve other values.
1215 * \param[in] methods The method table of the driver. This structure must
1216 * remain valid for as long as the cryptography
1217 * module keeps running. It is typically a global
1218 * constant.
1219 *
1220 * \return PSA_SUCCESS
1221 * The driver was successfully registered. Applications can now
1222 * use \p lifetime to access keys through the methods passed to
1223 * this function.
1224 * \return PSA_ERROR_BAD_STATE
1225 * This function was called after the initialization of the
1226 * cryptography module, and this implementation does not support
1227 * driver registration at this stage.
1228 * \return PSA_ERROR_ALREADY_EXISTS
1229 * There is already a registered driver for this value of \p lifetime.
1230 * \return PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001231 * \p lifetime is a reserved value.
Gilles Peskined910e922019-06-24 13:47:07 +02001232 * \return PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001233 * `methods->hal_version` is not supported by this implementation.
Gilles Peskined910e922019-06-24 13:47:07 +02001234 * \return PSA_ERROR_INSUFFICIENT_MEMORY
1235 * \return PSA_ERROR_NOT_PERMITTED
1236 */
1237psa_status_t psa_register_se_driver(
1238 psa_key_lifetime_t lifetime,
1239 const psa_drv_se_t *methods);
1240
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001241/**@}*/
1242
Gilles Peskine75976892018-12-12 15:55:09 +01001243#ifdef __cplusplus
1244}
1245#endif
1246
1247#endif /* PSA_CRYPTO_SE_DRIVER_H */