blob: f95eaeb33d4ccbeb40907edc407f969699eb8b8e [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 Peskine75976892018-12-12 15:55:09 +0100137/** An internal designation of a key slot between the core part of the
138 * PSA Crypto implementation and the driver. The meaning of this value
139 * is driver-dependent. */
Gilles Peskinef03143a2019-07-12 23:18:29 +0200140typedef uint64_t psa_key_slot_number_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100141
Gilles Peskine7a86da12019-07-12 23:25:38 +0200142/**@}*/
143
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600144/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +0100145 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600146 * a secure element can be done either as a single function call (via the
147 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +0100148 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600149 * - `psa_drv_se_mac_setup_t`
150 * - `psa_drv_se_mac_update_t`
151 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100152 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600153 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100154 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600155 * If a previously started secure element MAC operation needs to be terminated,
156 * 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 +0100157 * result in allocated resources not being freed or in other undefined
158 * behavior.
159 */
160/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600161/** \brief A function that starts a secure element MAC operation for a PSA
162 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +0100163 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200164 * \param[in,out] drv_context The driver context structure.
165 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100166 * hardware-specific MAC context
167 * \param[in] key_slot The slot of the key to be used for the
168 * operation
169 * \param[in] algorithm The algorithm to be used to underly the MAC
170 * operation
171 *
172 * \retval PSA_SUCCESS
173 * Success.
174 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200175typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
176 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600177 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600178 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100179
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600180/** \brief A function that continues a previously started secure element MAC
181 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100182 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200183 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100184 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600185 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100186 * \param[in] p_input A buffer containing the message to be appended
187 * to the MAC operation
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200188 * \param[in] input_length The size in bytes of the input message buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100189 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200190typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600191 const uint8_t *p_input,
192 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100193
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600194/** \brief a function that completes a previously started secure element MAC
195 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100196 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200197 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100198 * previously started MAC operation to be
199 * finished
200 * \param[out] p_mac A buffer where the generated MAC will be
201 * placed
202 * \param[in] mac_size The size in bytes of the buffer that has been
203 * allocated for the `output` buffer
204 * \param[out] p_mac_length After completion, will contain the number of
205 * bytes placed in the `p_mac` buffer
206 *
207 * \retval PSA_SUCCESS
208 * Success.
209 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200210typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600211 uint8_t *p_mac,
212 size_t mac_size,
213 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100214
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600215/** \brief A function that completes a previously started secure element MAC
216 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100217 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200218 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200219 * started MAC operation to be fiinished
220 * \param[in] p_mac The MAC value against which the resulting MAC
221 * will be compared against
222 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Gilles Peskine75976892018-12-12 15:55:09 +0100223 *
224 * \retval PSA_SUCCESS
225 * The operation completed successfully and the MACs matched each
226 * other
227 * \retval PSA_ERROR_INVALID_SIGNATURE
228 * The operation completed successfully, but the calculated MAC did
229 * not match the provided MAC
230 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200231typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600232 const uint8_t *p_mac,
233 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100234
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600235/** \brief A function that aborts a previous started secure element MAC
236 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100237 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200238 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200239 * started MAC operation to be aborted
Gilles Peskine75976892018-12-12 15:55:09 +0100240 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200241typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100242
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600243/** \brief A function that performs a secure element MAC operation in one
244 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100245 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200246 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100247 * \param[in] p_input A buffer containing the message to be MACed
248 * \param[in] input_length The size in bytes of `p_input`
249 * \param[in] key_slot The slot of the key to be used
250 * \param[in] alg The algorithm to be used to underlie the MAC
251 * operation
252 * \param[out] p_mac A buffer where the generated MAC will be
253 * placed
254 * \param[in] mac_size The size in bytes of the `p_mac` buffer
255 * \param[out] p_mac_length After completion, will contain the number of
256 * bytes placed in the `output` buffer
257 *
258 * \retval PSA_SUCCESS
259 * Success.
260 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200261typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
262 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600263 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600264 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600265 psa_algorithm_t alg,
266 uint8_t *p_mac,
267 size_t mac_size,
268 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100269
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600270/** \brief A function that performs a secure element MAC operation in one
271 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100272 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200273 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100274 * \param[in] p_input A buffer containing the message to be MACed
275 * \param[in] input_length The size in bytes of `input`
276 * \param[in] key_slot The slot of the key to be used
277 * \param[in] alg The algorithm to be used to underlie the MAC
278 * operation
279 * \param[in] p_mac The MAC value against which the resulting MAC will
280 * be compared against
281 * \param[in] mac_length The size in bytes of `mac`
282 *
283 * \retval PSA_SUCCESS
284 * The operation completed successfully and the MACs matched each
285 * other
286 * \retval PSA_ERROR_INVALID_SIGNATURE
287 * The operation completed successfully, but the calculated MAC did
288 * not match the provided MAC
289 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200290typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
291 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600292 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600293 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600294 psa_algorithm_t alg,
295 const uint8_t *p_mac,
296 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100297
298/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600299 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100300 *
301 * PSA Crypto API implementations should populate the table as appropriate
302 * upon startup.
303 *
304 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600305 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100306 *
307 * Driver implementers should ensure that they implement all of the functions
308 * that make sense for their hardware, and that they provide a full solution
309 * (for example, if they support `p_setup`, they should also support
310 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
311 *
312 */
313typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600314 /**The size in bytes of the hardware-specific secure element MAC context
315 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100316 */
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600317 size_t context_size;
318 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100319 */
Derek Millerea743cf2019-02-15 17:06:29 -0600320 psa_drv_se_mac_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600321 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100322 */
Derek Millerea743cf2019-02-15 17:06:29 -0600323 psa_drv_se_mac_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600324 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100325 */
Derek Millerea743cf2019-02-15 17:06:29 -0600326 psa_drv_se_mac_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600327 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100328 */
Derek Millerea743cf2019-02-15 17:06:29 -0600329 psa_drv_se_mac_finish_verify_t p_finish_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600330 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100331 */
Derek Millerea743cf2019-02-15 17:06:29 -0600332 psa_drv_se_mac_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600333 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100334 */
Derek Millerea743cf2019-02-15 17:06:29 -0600335 psa_drv_se_mac_generate_t p_mac;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600336 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100337 */
Derek Millerea743cf2019-02-15 17:06:29 -0600338 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600339} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100340/**@}*/
341
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600342/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100343 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600344 * Encryption and Decryption using secure element keys in block modes other
345 * than ECB must be done in multiple parts, using the following flow:
346 * - `psa_drv_se_cipher_setup_t`
347 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
348 * - `psa_drv_se_cipher_update_t`
349 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100350 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600351 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100352 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600353 * If a previously started secure element Cipher operation needs to be
354 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
355 * to do so may result in allocated resources not being freed or in other
356 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100357 *
358 * In situations where a PSA Cryptographic API implementation is using a block
359 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600360 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
361 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100362 */
363/**@{*/
364
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600365/** \brief A function that provides the cipher setup function for a
366 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100367 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200368 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +0200369 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100370 * hardware-specific cipher context.
371 * \param[in] key_slot The slot of the key to be used for the
372 * operation
373 * \param[in] algorithm The algorithm to be used in the cipher
374 * operation
375 * \param[in] direction Indicates whether the operation is an encrypt
376 * or decrypt
377 *
378 * \retval PSA_SUCCESS
379 * \retval PSA_ERROR_NOT_SUPPORTED
380 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200381typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
382 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600383 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600384 psa_algorithm_t algorithm,
385 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100386
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600387/** \brief A function that sets the initialization vector (if
388 * necessary) for an secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100389 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600390 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
391 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100392 * generate function is not necessary for the drivers to implement as the PSA
393 * Crypto implementation can do the generation using its RNG features.
394 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200395 * \param[in,out] op_context A structure that contains the previously set up
Gilles Peskine75976892018-12-12 15:55:09 +0100396 * hardware-specific cipher context
397 * \param[in] p_iv A buffer containing the initialization vector
398 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
399 *
400 * \retval PSA_SUCCESS
401 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200402typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600403 const uint8_t *p_iv,
404 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100405
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600406/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100407 * operation
408 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200409 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100410 * previously started cipher operation
411 * \param[in] p_input A buffer containing the data to be
412 * encrypted/decrypted
413 * \param[in] input_size The size in bytes of the buffer pointed to
414 * by `p_input`
415 * \param[out] p_output The caller-allocated buffer where the
416 * output will be placed
417 * \param[in] output_size The allocated size in bytes of the
418 * `p_output` buffer
419 * \param[out] p_output_length After completion, will contain the number
420 * of bytes placed in the `p_output` buffer
421 *
422 * \retval PSA_SUCCESS
423 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200424typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600425 const uint8_t *p_input,
426 size_t input_size,
427 uint8_t *p_output,
428 size_t output_size,
429 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100430
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600431/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100432 * operation
433 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200434 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100435 * previously started cipher operation
436 * \param[out] p_output The caller-allocated buffer where the output
437 * will be placed
438 * \param[in] output_size The allocated size in bytes of the `p_output`
439 * buffer
440 * \param[out] p_output_length After completion, will contain the number of
441 * bytes placed in the `p_output` buffer
442 *
443 * \retval PSA_SUCCESS
444 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200445typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600446 uint8_t *p_output,
447 size_t output_size,
448 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100449
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600450/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100451 * operation
452 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200453 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100454 * previously started cipher operation
455 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200456typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100457
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600458/** \brief A function that performs the ECB block mode for secure element
459 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100460 *
461 * Note: this function should only be used with implementations that do not
462 * provide a needed higher-level operation.
463 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200464 * \param[in,out] drv_context The driver context structure.
465 * \param[in] key_slot The slot of the key to be used for the operation
466 * \param[in] algorithm The algorithm to be used in the cipher operation
467 * \param[in] direction Indicates whether the operation is an encrypt or
468 * decrypt
469 * \param[in] p_input A buffer containing the data to be
470 * encrypted/decrypted
471 * \param[in] input_size The size in bytes of the buffer pointed to by
472 * `p_input`
473 * \param[out] p_output The caller-allocated buffer where the output
474 * will be placed
475 * \param[in] output_size The allocated size in bytes of the `p_output`
476 * buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100477 *
478 * \retval PSA_SUCCESS
479 * \retval PSA_ERROR_NOT_SUPPORTED
480 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200481typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
482 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600483 psa_algorithm_t algorithm,
484 psa_encrypt_or_decrypt_t direction,
485 const uint8_t *p_input,
486 size_t input_size,
487 uint8_t *p_output,
488 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100489
490/**
491 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600492 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100493 *
494 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600495 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100496 *
497 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600498 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100499 */
500typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600501 /** The size in bytes of the hardware-specific secure element cipher
502 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100503 */
Derek Miller34b33f12019-02-15 17:13:54 -0600504 size_t context_size;
505 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600506 psa_drv_se_cipher_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600507 /** Function that sets a cipher IV (if necessary) */
Derek Millerea743cf2019-02-15 17:06:29 -0600508 psa_drv_se_cipher_set_iv_t p_set_iv;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600509 /** Function that performs a cipher update operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600510 psa_drv_se_cipher_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600511 /** Function that completes a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600512 psa_drv_se_cipher_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600513 /** Function that aborts a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600514 psa_drv_se_cipher_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600515 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100516 * (Danger: ECB mode should not be used directly by clients of the PSA
517 * Crypto Client API)
518 */
Derek Millerea743cf2019-02-15 17:06:29 -0600519 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600520} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100521
522/**@}*/
523
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600524/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100525 *
526 * Since the amount of data that can (or should) be encrypted or signed using
527 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600528 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100529 */
530/**@{*/
531
532/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600533 * \brief A function that signs a hash or short message with a private key in
534 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100535 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200536 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100537 * \param[in] key_slot Key slot of an asymmetric key pair
538 * \param[in] alg A signature algorithm that is compatible
539 * with the type of `key`
540 * \param[in] p_hash The hash to sign
541 * \param[in] hash_length Size of the `p_hash` buffer in bytes
542 * \param[out] p_signature Buffer where the signature is to be written
543 * \param[in] signature_size Size of the `p_signature` buffer in bytes
544 * \param[out] p_signature_length On success, the number of bytes
545 * that make up the returned signature value
546 *
547 * \retval PSA_SUCCESS
548 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200549typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
550 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600551 psa_algorithm_t alg,
552 const uint8_t *p_hash,
553 size_t hash_length,
554 uint8_t *p_signature,
555 size_t signature_size,
556 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100557
558/**
559 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600560 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100561 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200562 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100563 * \param[in] key_slot Key slot of a public key or an asymmetric key
564 * pair
565 * \param[in] alg A signature algorithm that is compatible with
566 * the type of `key`
567 * \param[in] p_hash The hash whose signature is to be verified
568 * \param[in] hash_length Size of the `p_hash` buffer in bytes
569 * \param[in] p_signature Buffer containing the signature to verify
570 * \param[in] signature_length Size of the `p_signature` buffer in bytes
571 *
572 * \retval PSA_SUCCESS
573 * The signature is valid.
574 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200575typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
576 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600577 psa_algorithm_t alg,
578 const uint8_t *p_hash,
579 size_t hash_length,
580 const uint8_t *p_signature,
581 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100582
583/**
584 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600585 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100586 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200587 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100588 * \param[in] key_slot Key slot of a public key or an asymmetric key
589 * pair
590 * \param[in] alg An asymmetric encryption algorithm that is
591 * compatible with the type of `key`
592 * \param[in] p_input The message to encrypt
593 * \param[in] input_length Size of the `p_input` buffer in bytes
594 * \param[in] p_salt A salt or label, if supported by the
595 * encryption algorithm
596 * If the algorithm does not support a
597 * salt, pass `NULL`.
598 * If the algorithm supports an optional
599 * salt and you do not want to pass a salt,
600 * pass `NULL`.
601 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
602 * supported.
603 * \param[in] salt_length Size of the `p_salt` buffer in bytes
604 * If `p_salt` is `NULL`, pass 0.
605 * \param[out] p_output Buffer where the encrypted message is to
606 * be written
607 * \param[in] output_size Size of the `p_output` buffer in bytes
608 * \param[out] p_output_length On success, the number of bytes that make up
609 * the returned output
610 *
611 * \retval PSA_SUCCESS
612 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200613typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
614 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600615 psa_algorithm_t alg,
616 const uint8_t *p_input,
617 size_t input_length,
618 const uint8_t *p_salt,
619 size_t salt_length,
620 uint8_t *p_output,
621 size_t output_size,
622 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100623
624/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600625 * \brief A function that decrypts a short message with an asymmetric private
626 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100627 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200628 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100629 * \param[in] key_slot Key slot of an asymmetric key pair
630 * \param[in] alg An asymmetric encryption algorithm that is
631 * compatible with the type of `key`
632 * \param[in] p_input The message to decrypt
633 * \param[in] input_length Size of the `p_input` buffer in bytes
634 * \param[in] p_salt A salt or label, if supported by the
635 * encryption algorithm
636 * If the algorithm does not support a
637 * salt, pass `NULL`.
638 * If the algorithm supports an optional
639 * salt and you do not want to pass a salt,
640 * pass `NULL`.
641 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
642 * supported.
643 * \param[in] salt_length Size of the `p_salt` buffer in bytes
644 * If `p_salt` is `NULL`, pass 0.
645 * \param[out] p_output Buffer where the decrypted message is to
646 * be written
647 * \param[in] output_size Size of the `p_output` buffer in bytes
648 * \param[out] p_output_length On success, the number of bytes
649 * that make up the returned output
650 *
651 * \retval PSA_SUCCESS
652 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200653typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
654 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600655 psa_algorithm_t alg,
656 const uint8_t *p_input,
657 size_t input_length,
658 const uint8_t *p_salt,
659 size_t salt_length,
660 uint8_t *p_output,
661 size_t output_size,
662 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100663
664/**
665 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600666 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100667 *
668 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600669 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100670 *
671 * If one of the functions is not implemented, it should be set to NULL.
672 */
673typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600674 /** Function that performs an asymmetric sign operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600675 psa_drv_se_asymmetric_sign_t p_sign;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600676 /** Function that performs an asymmetric verify operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600677 psa_drv_se_asymmetric_verify_t p_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600678 /** Function that performs an asymmetric encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600679 psa_drv_se_asymmetric_encrypt_t p_encrypt;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600680 /** Function that performs an asymmetric decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600681 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600682} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100683
684/**@}*/
685
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600686/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
687 * Authenticated Encryption with Additional Data (AEAD) operations with secure
688 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100689 * implementers as there must be sufficient space in memory for the entire
690 * message, it prevents decrypted data from being made available before the
691 * authentication operation is complete and the data is known to be authentic.
692 */
693/**@{*/
694
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600695/** \brief A function that performs a secure element authenticated encryption
696 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100697 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200698 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100699 * \param[in] key_slot Slot containing the key to use.
700 * \param[in] algorithm The AEAD algorithm to compute
701 * (\c PSA_ALG_XXX value such that
702 * #PSA_ALG_IS_AEAD(`alg`) is true)
703 * \param[in] p_nonce Nonce or IV to use
704 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
705 * \param[in] p_additional_data Additional data that will be
706 * authenticated but not encrypted
707 * \param[in] additional_data_length Size of `p_additional_data` in bytes
708 * \param[in] p_plaintext Data that will be authenticated and
709 * encrypted
710 * \param[in] plaintext_length Size of `p_plaintext` in bytes
711 * \param[out] p_ciphertext Output buffer for the authenticated and
712 * encrypted data. The additional data is
713 * not part of this output. For algorithms
714 * where the encrypted data and the
715 * authentication tag are defined as
716 * separate outputs, the authentication
717 * tag is appended to the encrypted data.
718 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
719 * bytes
720 * \param[out] p_ciphertext_length On success, the size of the output in
721 * the `p_ciphertext` buffer
722 *
723 * \retval #PSA_SUCCESS
724 * Success.
725 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200726typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
727 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600728 psa_algorithm_t algorithm,
729 const uint8_t *p_nonce,
730 size_t nonce_length,
731 const uint8_t *p_additional_data,
732 size_t additional_data_length,
733 const uint8_t *p_plaintext,
734 size_t plaintext_length,
735 uint8_t *p_ciphertext,
736 size_t ciphertext_size,
737 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100738
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600739/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100740 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200741 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100742 * \param[in] key_slot Slot containing the key to use
743 * \param[in] algorithm The AEAD algorithm to compute
744 * (\c PSA_ALG_XXX value such that
745 * #PSA_ALG_IS_AEAD(`alg`) is true)
746 * \param[in] p_nonce Nonce or IV to use
747 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
748 * \param[in] p_additional_data Additional data that has been
749 * authenticated but not encrypted
750 * \param[in] additional_data_length Size of `p_additional_data` in bytes
751 * \param[in] p_ciphertext Data that has been authenticated and
752 * encrypted.
753 * For algorithms where the encrypted data
754 * and the authentication tag are defined
755 * as separate inputs, the buffer must
756 * contain the encrypted data followed by
757 * the authentication tag.
758 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
759 * \param[out] p_plaintext Output buffer for the decrypted data
760 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
761 * bytes
762 * \param[out] p_plaintext_length On success, the size of the output in
763 * the `p_plaintext` buffer
764 *
765 * \retval #PSA_SUCCESS
766 * Success.
767 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200768typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
769 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600770 psa_algorithm_t algorithm,
771 const uint8_t *p_nonce,
772 size_t nonce_length,
773 const uint8_t *p_additional_data,
774 size_t additional_data_length,
775 const uint8_t *p_ciphertext,
776 size_t ciphertext_length,
777 uint8_t *p_plaintext,
778 size_t plaintext_size,
779 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100780
781/**
782 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600783 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100784 *
785 * PSA Crypto API implementations should populate instances of the table as
786 * appropriate upon startup.
787 *
788 * If one of the functions is not implemented, it should be set to NULL.
789 */
790typedef struct {
791 /** Function that performs the AEAD encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600792 psa_drv_se_aead_encrypt_t p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100793 /** Function that performs the AEAD decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600794 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600795} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100796/**@}*/
797
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600798/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100799 * Currently, key management is limited to importing keys in the clear,
800 * destroying keys, and exporting keys in the clear.
801 * Whether a key may be exported is determined by the key policies in place
802 * on the key slot.
803 */
804/**@{*/
805
Gilles Peskinef2223c82019-07-12 23:33:02 +0200806/** \brief A function that allocates a slot for a key.
807 *
808 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200809 * \param[in,out] persistent_data A pointer to the persistent data
810 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200811 * \param[in] attributes Attributes of the key.
812 * \param[out] key_slot Slot where the key will be stored.
813 * This must be a valid slot for a key of the
814 * chosen type. It must be unoccupied.
Gilles Peskinef2223c82019-07-12 23:33:02 +0200815 *
816 * \retval #PSA_SUCCESS
817 * Success.
818 * The core will record \c *key_slot as the key slot where the key
819 * is stored and will update the persistent data in storage.
820 * \retval #PSA_ERROR_NOT_SUPPORTED
821 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
822 */
823typedef psa_status_t (*psa_drv_se_allocate_key_t)(
824 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200825 void *persistent_data,
Gilles Peskinef2223c82019-07-12 23:33:02 +0200826 const psa_key_attributes_t *attributes,
827 psa_key_slot_number_t *key_slot);
828
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600829/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100830 *
831 * This function can support any output from psa_export_key(). Refer to the
832 * documentation of psa_export_key() for the format for each key type.
833 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200834 * \param[in,out] drv_context The driver context structure.
835 * \param[in] key_slot Slot where the key will be stored
Gilles Peskine18017402019-07-24 20:25:59 +0200836 * This must be a valid slot for a key of the
837 * chosen type. It must be unoccupied.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200838 * \param[in] lifetime The required lifetime of the key storage
839 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
840 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
841 * \param[in] usage The allowed uses of the key
842 * \param[in] p_data Buffer containing the key data
843 * \param[in] data_length Size of the `data` buffer in bytes
Gilles Peskine18017402019-07-24 20:25:59 +0200844 * \param[out] bits On success, the key size in bits. The driver
845 * must determine this value after parsing the
846 * key according to the key type.
847 * This value is not used if the function fails.
Gilles Peskine75976892018-12-12 15:55:09 +0100848 *
849 * \retval #PSA_SUCCESS
850 * Success.
851 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200852typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_context,
853 psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600854 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600855 psa_key_type_t type,
856 psa_algorithm_t algorithm,
857 psa_key_usage_t usage,
858 const uint8_t *p_data,
Gilles Peskine18017402019-07-24 20:25:59 +0200859 size_t data_length,
860 size_t *bits);
Gilles Peskine75976892018-12-12 15:55:09 +0100861
862/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600863 * \brief A function that destroys a secure element key and restore the slot to
864 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100865 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600866 * This function destroys the content of the key from a secure element.
867 * Implementations shall make a best effort to ensure that any previous content
868 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100869 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600870 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100871 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200872 * \param[in,out] drv_context The driver context structure.
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200873 * \param[in,out] persistent_data A pointer to the persistent data
874 * that allows writing.
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200875 * \param key_slot The key slot to erase.
Gilles Peskine75976892018-12-12 15:55:09 +0100876 *
877 * \retval #PSA_SUCCESS
878 * The slot's content, if any, has been erased.
879 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200880typedef psa_status_t (*psa_drv_se_destroy_key_t)(
881 psa_drv_se_context_t *drv_context,
Gilles Peskine94cc42c2019-07-12 23:34:20 +0200882 void *persistent_data,
Gilles Peskine8597bc12019-07-12 23:28:46 +0200883 psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +0100884
885/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600886 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100887 *
888 * The output of this function can be passed to psa_import_key() to
889 * create an equivalent object.
890 *
891 * If a key is created with `psa_import_key()` and then exported with
892 * this function, it is not guaranteed that the resulting data is
893 * identical: the implementation may choose a different representation
894 * of the same key if the format permits it.
895 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600896 * This function should generate output in the same format that
897 * `psa_export_key()` does. Refer to the
898 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100899 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200900 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100901 * \param[in] key Slot whose content is to be exported. This must
902 * be an occupied key slot.
903 * \param[out] p_data Buffer where the key data is to be written.
904 * \param[in] data_size Size of the `p_data` buffer in bytes.
905 * \param[out] p_data_length On success, the number of bytes
906 * that make up the key data.
907 *
908 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +0200909 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine75976892018-12-12 15:55:09 +0100910 * \retval #PSA_ERROR_NOT_PERMITTED
911 * \retval #PSA_ERROR_NOT_SUPPORTED
912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
913 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200914 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine75976892018-12-12 15:55:09 +0100915 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200916typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
917 psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600918 uint8_t *p_data,
919 size_t data_size,
920 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100921
922/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600923 * \brief A function that generates a symmetric or asymmetric key on a secure
924 * element
Gilles Peskine75976892018-12-12 15:55:09 +0100925 *
Gilles Peskine9dd125d2019-07-23 18:26:43 +0200926 * If \p type is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) = 1),
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600927 * the public component of the generated key will be placed in `p_pubkey_out`.
928 * The format of the public key information will match the format specified for
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100929 * the psa_export_key() function for the key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100930 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +0200931 * \param[in,out] drv_context The driver context structure.
932 * \param[in] key_slot Slot where the generated key will be placed
933 * \param[in] type The type of the key to be generated
934 * \param[in] usage The prescribed usage of the generated key
935 * Note: Not all Secure Elements support the same
936 * restrictions that PSA Crypto does (and vice
937 * versa).
938 * Driver developers should endeavor to match the
939 * usages as close as possible.
940 * \param[in] bits The size in bits of the key to be generated.
941 * \param[in] extra Extra parameters for key generation. The
942 * interpretation of this parameter should match
943 * the interpretation in the `extra` parameter is
944 * the `psa_generate_key` function
945 * \param[in] extra_size The size in bytes of the \p extra buffer
946 * \param[out] p_pubkey_out The buffer where the public key information will
947 * be placed
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600948 * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer
949 * \param[out] p_pubkey_length Upon successful completion, will contain the
950 * size of the data placed in `p_pubkey_out`.
Gilles Peskine75976892018-12-12 15:55:09 +0100951 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200952typedef psa_status_t (*psa_drv_se_generate_key_t)(psa_drv_se_context_t *drv_context,
953 psa_key_slot_number_t key_slot,
Gilles Peskine32668ce2019-03-06 18:29:57 +0100954 psa_key_type_t type,
955 psa_key_usage_t usage,
956 size_t bits,
957 const void *extra,
958 size_t extra_size,
959 uint8_t *p_pubkey_out,
960 size_t pubkey_out_size,
961 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100962
963/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600964 * \brief A struct containing all of the function pointers needed to for secure
965 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +0100966 *
967 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600968 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100969 *
970 * If one of the functions is not implemented, it should be set to NULL.
971 */
972typedef struct {
Gilles Peskinef2223c82019-07-12 23:33:02 +0200973 /** Function that allocates a slot. */
974 psa_drv_se_allocate_key_t p_allocate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600975 /** Function that performs a key import operation */
976 psa_drv_se_import_key_t p_import;
977 /** Function that performs a generation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600978 psa_drv_se_generate_key_t p_generate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600979 /** Function that performs a key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600980 psa_drv_se_destroy_key_t p_destroy;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600981 /** Function that performs a key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600982 psa_drv_se_export_key_t p_export;
Gilles Peskinee62b74e2019-06-25 15:25:09 +0200983 /** Function that performs a public key export operation */
984 psa_drv_se_export_key_t p_export_public;
Derek Miller83d26622019-02-15 16:41:22 -0600985} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100986
987/**@}*/
988
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600989/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100990 * Key derivation is the process of generating new key material using an
991 * existing key and additional parameters, iterating through a basic
992 * cryptographic function, such as a hash.
993 * Key agreement is a part of cryptographic protocols that allows two parties
994 * to agree on the same key value, but starting from different original key
995 * material.
996 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
997 * for both of the flows.
998 *
999 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001000 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1001 * `psa_drv_se_key_derivation_derive` is used when the key material should be
1002 * placed in a slot on the hardware and not exposed to the caller.
1003 * `psa_drv_se_key_derivation_export` is used when the key material should be
1004 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +01001005 *
1006 * Different key derivation algorithms require a different number of inputs.
1007 * Instead of having an API that takes as input variable length arrays, which
1008 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001009 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1010 * is called multiple times with different `collateral_id`s. Thus, for a key
Gilles Peskine75976892018-12-12 15:55:09 +01001011 * derivation algorithm that required 3 paramter inputs, the flow would look
1012 * something like:
1013 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001014 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1015 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1016 * p_collateral_0,
1017 * collateral_0_size);
1018 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1019 * p_collateral_1,
1020 * collateral_1_size);
1021 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1022 * p_collateral_2,
1023 * collateral_2_size);
1024 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +01001025 * ~~~~~~~~~~~~~
1026 *
1027 * key agreement example:
1028 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001029 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1030 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1031 * psa_drv_se_key_derivation_export(p_session_key,
1032 * session_key_size,
1033 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001034 * ~~~~~~~~~~~~~
1035 */
1036/**@{*/
1037
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001038/** \brief A function that Sets up a secure element key derivation operation by
1039 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +01001040 *
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001041 * \param[in,out] drv_context The driver context structure.
Gilles Peskine8597bc12019-07-12 23:28:46 +02001042 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine6a3dd892019-07-25 10:56:39 +02001043 * context information for the implementation
1044 * \param[in] kdf_alg The algorithm to be used for the key derivation
1045 * \param[in] source_key The key to be used as the source material for
1046 * the key derivation
Gilles Peskine75976892018-12-12 15:55:09 +01001047 *
1048 * \retval PSA_SUCCESS
1049 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001050typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1051 void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001052 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -06001053 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001054
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001055/** \brief A function that provides collateral (parameters) needed for a secure
1056 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +01001057 *
1058 * Since many key derivation algorithms require multiple parameters, it is
1059 * expeced that this function may be called multiple times for the same
1060 * operation, each with a different algorithm-specific `collateral_id`
1061 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001062 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001063 * context information for the implementation
1064 * \param[in] collateral_id An ID for the collateral being provided
1065 * \param[in] p_collateral A buffer containing the collateral data
1066 * \param[in] collateral_size The size in bytes of the collateral
1067 *
1068 * \retval PSA_SUCCESS
1069 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001070typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001071 uint32_t collateral_id,
1072 const uint8_t *p_collateral,
1073 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +01001074
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001075/** \brief A function that performs the final secure element key derivation
1076 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +01001077 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001078 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001079 * context information for the implementation
1080 * \param[in] dest_key The slot where the generated key material
1081 * should be placed
1082 *
1083 * \retval PSA_SUCCESS
1084 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001085typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001086 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001087
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001088/** \brief A function that performs the final step of a secure element key
1089 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001090 *
1091 * \param[out] p_output Buffer in which to place the generated key
1092 * material
1093 * \param[in] output_size The size in bytes of `p_output`
1094 * \param[out] p_output_length Upon success, contains the number of bytes of
1095 * key material placed in `p_output`
1096 *
1097 * \retval PSA_SUCCESS
1098 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001099typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001100 uint8_t *p_output,
1101 size_t output_size,
1102 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001103
1104/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001105 * \brief A struct containing all of the function pointers needed to for secure
1106 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001107 *
1108 * PSA Crypto API implementations should populate instances of the table as
1109 * appropriate upon startup.
1110 *
1111 * If one of the functions is not implemented, it should be set to NULL.
1112 */
1113typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001114 /** The driver-specific size of the key derivation context */
1115 size_t context_size;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001116 /** Function that performs a key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -06001117 psa_drv_se_key_derivation_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001118 /** Function that sets key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -06001119 psa_drv_se_key_derivation_collateral_t p_collateral;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001120 /** Function that performs a final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -06001121 psa_drv_se_key_derivation_derive_t p_derive;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001122 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001123 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -06001124 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -06001125} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001126
1127/**@}*/
1128
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001129/** \defgroup se_registration Secure element driver registration
1130 */
1131/**@{*/
1132
1133/** A structure containing pointers to all the entry points of a
1134 * secure element driver.
1135 *
1136 * Future versions of this specification may add extra substructures at
1137 * the end of this structure.
1138 */
1139typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001140 /** The version of the driver HAL that this driver implements.
1141 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001142 * a different version of this specification.
1143 * Use #PSA_DRV_SE_HAL_VERSION.
1144 */
1145 uint32_t hal_version;
Gilles Peskine7a86da12019-07-12 23:25:38 +02001146
Gilles Peskine0c3ae1f2019-07-25 14:04:38 +02001147 /** The size of the driver's persistent data in bytes.
1148 *
1149 * This can be 0 if the driver does not need persistent data.
1150 *
1151 * See the documentation of psa_drv_se_context_t::persistent_data
1152 * for more information about why and how a driver can use
1153 * persistent data.
1154 */
Gilles Peskine7a86da12019-07-12 23:25:38 +02001155 size_t persistent_data_size;
1156
1157 /** The driver initialization function.
1158 *
1159 * This function is called once during the initialization of the
1160 * PSA Cryptography subsystem, before any other function of the
1161 * driver is called. If this function returns a failure status,
1162 * the driver will be unusable, at least until the next system reset.
1163 *
1164 * If this field is \c NULL, it is equivalent to a function that does
1165 * nothing and returns #PSA_SUCCESS.
1166 */
1167 psa_drv_se_init_t p_init;
1168
Gilles Peskine6e59c422019-06-26 19:06:52 +02001169 const psa_drv_se_key_management_t *key_management;
1170 const psa_drv_se_mac_t *mac;
1171 const psa_drv_se_cipher_t *cipher;
1172 const psa_drv_se_aead_t *aead;
1173 const psa_drv_se_asymmetric_t *asymmetric;
1174 const psa_drv_se_key_derivation_t *derivation;
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001175} psa_drv_se_t;
1176
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001177/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001178 */
1179/* 0.0.0 patchlevel 5 */
1180#define PSA_DRV_SE_HAL_VERSION 0x00000005
1181
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001182/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001183 *
1184 * This function is only intended to be used by driver code, not by
1185 * application code. In implementations with separation between the
1186 * PSA cryptography module and applications, this function should
1187 * only be available to callers that run in the same memory space as
1188 * the cryptography module, and should not be exposed to applications
1189 * running in a different memory space.
1190 *
1191 * This function may be called before psa_crypto_init(). It is
1192 * implementation-defined whether this function may be called
1193 * after psa_crypto_init().
1194 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001195 * \note Implementations store metadata about keys including the lifetime
1196 * value. Therefore, from one instantiation of the PSA Cryptography
1197 * library to the next one, if there is a key in storage with a certain
1198 * lifetime value, you must always register the same driver (or an
1199 * updated version that communicates with the same secure element)
1200 * with the same lifetime value.
1201 *
Gilles Peskined910e922019-06-24 13:47:07 +02001202 * \param lifetime The lifetime value through which this driver will
1203 * be exposed to applications.
1204 * The values #PSA_KEY_LIFETIME_VOLATILE and
1205 * #PSA_KEY_LIFETIME_PERSISTENT are reserved and
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001206 * may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001207 * may reserve other values.
1208 * \param[in] methods The method table of the driver. This structure must
1209 * remain valid for as long as the cryptography
1210 * module keeps running. It is typically a global
1211 * constant.
1212 *
1213 * \return PSA_SUCCESS
1214 * The driver was successfully registered. Applications can now
1215 * use \p lifetime to access keys through the methods passed to
1216 * this function.
1217 * \return PSA_ERROR_BAD_STATE
1218 * This function was called after the initialization of the
1219 * cryptography module, and this implementation does not support
1220 * driver registration at this stage.
1221 * \return PSA_ERROR_ALREADY_EXISTS
1222 * There is already a registered driver for this value of \p lifetime.
1223 * \return PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001224 * \p lifetime is a reserved value.
Gilles Peskined910e922019-06-24 13:47:07 +02001225 * \return PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001226 * `methods->hal_version` is not supported by this implementation.
Gilles Peskined910e922019-06-24 13:47:07 +02001227 * \return PSA_ERROR_INSUFFICIENT_MEMORY
1228 * \return PSA_ERROR_NOT_PERMITTED
1229 */
1230psa_status_t psa_register_se_driver(
1231 psa_key_lifetime_t lifetime,
1232 const psa_drv_se_t *methods);
1233
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001234/**@}*/
1235
Gilles Peskine75976892018-12-12 15:55:09 +01001236#ifdef __cplusplus
1237}
1238#endif
1239
1240#endif /* PSA_CRYPTO_SE_DRIVER_H */