blob: 4458562d12718d9859f558d70735bf14a2daf2fe [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 *
58 * The PSA Cryptography core saves the persistent data from one
59 * session to the next.
60 *
61 * The core allocates a memory buffer for the persistent data.
62 * The pointer is guaranteed to be suitably alignedfor any data type,
63 * like a pointer returned by `malloc` (but the core can use any
64 * method to allocate the buffer, not necessarily `malloc`).
65 *
66 * The size of this buffer is given by psa_drv_se_t::persistent_data_size
67 * when the driver is registered, and this value is also recorded in the
68 * ::persistent_data_size field of this structure.
69 *
70 * Before the driver is initialized for the first time, the content of
71 * the persistent data is all-bits-zero. After a driver upgrade, if the
72 * size of the persistent data has increased, the original data is padded
73 * on the right with zeros; if the size has decreased, the original data
74 * is truncated to the new size.
75 *
76 * This pointer is to read-only data. Only a few driver functions are
77 * allowed to modify the persistent data. These functions receive a
78 * writable pointer.
79 */
80 const void *const persistent_data;
81
82 /** The size of \c persistent_data in bytes.
83 *
84 * This is always equal to the value of
85 * psa_drv_se_t::persistent_data_size when the driver is registered.
86 */
87 const size_t persistent_data_size;
88
89 /** Driver transient data.
90 *
91 * The core initializes this value to 0 and does not read or modify it
92 * afterwards. The driver may store whatever it wants in this field.
93 */
94 uintptr_t transient_data;
95} psa_drv_se_context_t;
96
97/** \brief A driver initialization function.
98 *
99 * \param[in,out] drv_context The driver context structure.
100 * \param lifetime The lifetime value for which this driver
101 * is registered.
102 *
103 * \retval #PSA_SUCCESS
104 * The driver is operational.
105 * The core will update the persistent data in storage.
106 * \return
107 * Any other return value prevents the driver from being used in
108 * this session.
109 * The core will NOT update the persistent data in storage.
110 */
111typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
112 psa_key_lifetime_t lifetime);
113
Gilles Peskine75976892018-12-12 15:55:09 +0100114/** An internal designation of a key slot between the core part of the
115 * PSA Crypto implementation and the driver. The meaning of this value
116 * is driver-dependent. */
Gilles Peskinef03143a2019-07-12 23:18:29 +0200117typedef uint64_t psa_key_slot_number_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100118
Gilles Peskine7a86da12019-07-12 23:25:38 +0200119/**@}*/
120
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600121/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +0100122 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600123 * a secure element can be done either as a single function call (via the
124 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +0100125 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600126 * - `psa_drv_se_mac_setup_t`
127 * - `psa_drv_se_mac_update_t`
128 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100129 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600130 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100131 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600132 * If a previously started secure element MAC operation needs to be terminated,
133 * 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 +0100134 * result in allocated resources not being freed or in other undefined
135 * behavior.
136 */
137/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600138/** \brief A function that starts a secure element MAC operation for a PSA
139 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +0100140 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200141 * \param[in,out] drv_context The driver context structure.
142 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100143 * hardware-specific MAC context
144 * \param[in] key_slot The slot of the key to be used for the
145 * operation
146 * \param[in] algorithm The algorithm to be used to underly the MAC
147 * operation
148 *
149 * \retval PSA_SUCCESS
150 * Success.
151 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200152typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
153 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600154 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600155 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100156
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600157/** \brief A function that continues a previously started secure element MAC
158 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100159 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200160 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100161 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600162 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100163 * \param[in] p_input A buffer containing the message to be appended
164 * to the MAC operation
165 * \param[in] input_length The size in bytes of the input message buffer
166 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200167typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600168 const uint8_t *p_input,
169 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100170
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600171/** \brief a function that completes a previously started secure element MAC
172 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100173 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200174 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100175 * previously started MAC operation to be
176 * finished
177 * \param[out] p_mac A buffer where the generated MAC will be
178 * placed
179 * \param[in] mac_size The size in bytes of the buffer that has been
180 * allocated for the `output` buffer
181 * \param[out] p_mac_length After completion, will contain the number of
182 * bytes placed in the `p_mac` buffer
183 *
184 * \retval PSA_SUCCESS
185 * Success.
186 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200187typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600188 uint8_t *p_mac,
189 size_t mac_size,
190 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100191
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600192/** \brief A function that completes a previously started secure element MAC
193 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100194 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200195 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine75976892018-12-12 15:55:09 +0100196 * started MAC operation to be fiinished
197 * \param[in] p_mac The MAC value against which the resulting MAC will
198 * be compared against
199 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
200 *
201 * \retval PSA_SUCCESS
202 * The operation completed successfully and the MACs matched each
203 * other
204 * \retval PSA_ERROR_INVALID_SIGNATURE
205 * The operation completed successfully, but the calculated MAC did
206 * not match the provided MAC
207 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200208typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600209 const uint8_t *p_mac,
210 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100211
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600212/** \brief A function that aborts a previous started secure element MAC
213 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100214 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200215 * \param[in,out] op_context A hardware-specific structure for the previously
Gilles Peskine75976892018-12-12 15:55:09 +0100216 * started MAC operation to be aborted
217 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200218typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100219
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600220/** \brief A function that performs a secure element MAC operation in one
221 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100222 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200223 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100224 * \param[in] p_input A buffer containing the message to be MACed
225 * \param[in] input_length The size in bytes of `p_input`
226 * \param[in] key_slot The slot of the key to be used
227 * \param[in] alg The algorithm to be used to underlie the MAC
228 * operation
229 * \param[out] p_mac A buffer where the generated MAC will be
230 * placed
231 * \param[in] mac_size The size in bytes of the `p_mac` buffer
232 * \param[out] p_mac_length After completion, will contain the number of
233 * bytes placed in the `output` buffer
234 *
235 * \retval PSA_SUCCESS
236 * Success.
237 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200238typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
239 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600240 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600241 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600242 psa_algorithm_t alg,
243 uint8_t *p_mac,
244 size_t mac_size,
245 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100246
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600247/** \brief A function that performs a secure element MAC operation in one
248 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100249 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200250 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100251 * \param[in] p_input A buffer containing the message to be MACed
252 * \param[in] input_length The size in bytes of `input`
253 * \param[in] key_slot The slot of the key to be used
254 * \param[in] alg The algorithm to be used to underlie the MAC
255 * operation
256 * \param[in] p_mac The MAC value against which the resulting MAC will
257 * be compared against
258 * \param[in] mac_length The size in bytes of `mac`
259 *
260 * \retval PSA_SUCCESS
261 * The operation completed successfully and the MACs matched each
262 * other
263 * \retval PSA_ERROR_INVALID_SIGNATURE
264 * The operation completed successfully, but the calculated MAC did
265 * not match the provided MAC
266 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200267typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
268 const uint8_t *p_input,
Derek Miller83d26622019-02-15 16:41:22 -0600269 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600270 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600271 psa_algorithm_t alg,
272 const uint8_t *p_mac,
273 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100274
275/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600276 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100277 *
278 * PSA Crypto API implementations should populate the table as appropriate
279 * upon startup.
280 *
281 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600282 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100283 *
284 * Driver implementers should ensure that they implement all of the functions
285 * that make sense for their hardware, and that they provide a full solution
286 * (for example, if they support `p_setup`, they should also support
287 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
288 *
289 */
290typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600291 /**The size in bytes of the hardware-specific secure element MAC context
292 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100293 */
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600294 size_t context_size;
295 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100296 */
Derek Millerea743cf2019-02-15 17:06:29 -0600297 psa_drv_se_mac_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600298 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100299 */
Derek Millerea743cf2019-02-15 17:06:29 -0600300 psa_drv_se_mac_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600301 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100302 */
Derek Millerea743cf2019-02-15 17:06:29 -0600303 psa_drv_se_mac_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600304 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100305 */
Derek Millerea743cf2019-02-15 17:06:29 -0600306 psa_drv_se_mac_finish_verify_t p_finish_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600307 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100308 */
Derek Millerea743cf2019-02-15 17:06:29 -0600309 psa_drv_se_mac_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600310 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100311 */
Derek Millerea743cf2019-02-15 17:06:29 -0600312 psa_drv_se_mac_generate_t p_mac;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600313 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100314 */
Derek Millerea743cf2019-02-15 17:06:29 -0600315 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600316} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100317/**@}*/
318
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600319/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100320 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600321 * Encryption and Decryption using secure element keys in block modes other
322 * than ECB must be done in multiple parts, using the following flow:
323 * - `psa_drv_se_cipher_setup_t`
324 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
325 * - `psa_drv_se_cipher_update_t`
326 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100327 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600328 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100329 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600330 * If a previously started secure element Cipher operation needs to be
331 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
332 * to do so may result in allocated resources not being freed or in other
333 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100334 *
335 * In situations where a PSA Cryptographic API implementation is using a block
336 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600337 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
338 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100339 */
340/**@{*/
341
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600342/** \brief A function that provides the cipher setup function for a
343 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100344 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200345 * \param[in,out] drv_context The driver context structure.
346 * \param[in,out] op_context A structure that will contain the
Gilles Peskine75976892018-12-12 15:55:09 +0100347 * hardware-specific cipher context.
348 * \param[in] key_slot The slot of the key to be used for the
349 * operation
350 * \param[in] algorithm The algorithm to be used in the cipher
351 * operation
352 * \param[in] direction Indicates whether the operation is an encrypt
353 * or decrypt
354 *
355 * \retval PSA_SUCCESS
356 * \retval PSA_ERROR_NOT_SUPPORTED
357 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200358typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
359 void *op_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600360 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600361 psa_algorithm_t algorithm,
362 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100363
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600364/** \brief A function that sets the initialization vector (if
365 * necessary) for an secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100366 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600367 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
368 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100369 * generate function is not necessary for the drivers to implement as the PSA
370 * Crypto implementation can do the generation using its RNG features.
371 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200372 * \param[in,out] op_context A structure that contains the previously set up
Gilles Peskine75976892018-12-12 15:55:09 +0100373 * hardware-specific cipher context
374 * \param[in] p_iv A buffer containing the initialization vector
375 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
376 *
377 * \retval PSA_SUCCESS
378 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200379typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600380 const uint8_t *p_iv,
381 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100382
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600383/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100384 * operation
385 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200386 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100387 * previously started cipher operation
388 * \param[in] p_input A buffer containing the data to be
389 * encrypted/decrypted
390 * \param[in] input_size The size in bytes of the buffer pointed to
391 * by `p_input`
392 * \param[out] p_output The caller-allocated buffer where the
393 * output will be placed
394 * \param[in] output_size The allocated size in bytes of the
395 * `p_output` buffer
396 * \param[out] p_output_length After completion, will contain the number
397 * of bytes placed in the `p_output` buffer
398 *
399 * \retval PSA_SUCCESS
400 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200401typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600402 const uint8_t *p_input,
403 size_t input_size,
404 uint8_t *p_output,
405 size_t output_size,
406 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100407
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600408/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100409 * operation
410 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200411 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100412 * previously started cipher operation
413 * \param[out] p_output The caller-allocated buffer where the output
414 * will be placed
415 * \param[in] output_size The allocated size in bytes of the `p_output`
416 * buffer
417 * \param[out] p_output_length After completion, will contain the number of
418 * bytes placed in the `p_output` buffer
419 *
420 * \retval PSA_SUCCESS
421 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200422typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -0600423 uint8_t *p_output,
424 size_t output_size,
425 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100426
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600427/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100428 * operation
429 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200430 * \param[in,out] op_context A hardware-specific structure for the
Gilles Peskine75976892018-12-12 15:55:09 +0100431 * previously started cipher operation
432 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200433typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100434
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600435/** \brief A function that performs the ECB block mode for secure element
436 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100437 *
438 * Note: this function should only be used with implementations that do not
439 * provide a needed higher-level operation.
440 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200441 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100442 * \param[in] key_slot The slot of the key to be used for the operation
443 * \param[in] algorithm The algorithm to be used in the cipher operation
444 * \param[in] direction Indicates whether the operation is an encrypt or
445 * decrypt
446 * \param[in] p_input A buffer containing the data to be
447 * encrypted/decrypted
448 * \param[in] input_size The size in bytes of the buffer pointed to by
449 * `p_input`
450 * \param[out] p_output The caller-allocated buffer where the output will
451 * be placed
452 * \param[in] output_size The allocated size in bytes of the `p_output`
453 * buffer
454 *
455 * \retval PSA_SUCCESS
456 * \retval PSA_ERROR_NOT_SUPPORTED
457 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200458typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
459 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600460 psa_algorithm_t algorithm,
461 psa_encrypt_or_decrypt_t direction,
462 const uint8_t *p_input,
463 size_t input_size,
464 uint8_t *p_output,
465 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100466
467/**
468 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600469 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100470 *
471 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600472 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100473 *
474 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600475 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100476 */
477typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600478 /** The size in bytes of the hardware-specific secure element cipher
479 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100480 */
Derek Miller34b33f12019-02-15 17:13:54 -0600481 size_t context_size;
482 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600483 psa_drv_se_cipher_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600484 /** Function that sets a cipher IV (if necessary) */
Derek Millerea743cf2019-02-15 17:06:29 -0600485 psa_drv_se_cipher_set_iv_t p_set_iv;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600486 /** Function that performs a cipher update operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600487 psa_drv_se_cipher_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600488 /** Function that completes a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600489 psa_drv_se_cipher_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600490 /** Function that aborts a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600491 psa_drv_se_cipher_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600492 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100493 * (Danger: ECB mode should not be used directly by clients of the PSA
494 * Crypto Client API)
495 */
Derek Millerea743cf2019-02-15 17:06:29 -0600496 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600497} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100498
499/**@}*/
500
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600501/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100502 *
503 * Since the amount of data that can (or should) be encrypted or signed using
504 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600505 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100506 */
507/**@{*/
508
509/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600510 * \brief A function that signs a hash or short message with a private key in
511 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100512 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200513 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100514 * \param[in] key_slot Key slot of an asymmetric key pair
515 * \param[in] alg A signature algorithm that is compatible
516 * with the type of `key`
517 * \param[in] p_hash The hash to sign
518 * \param[in] hash_length Size of the `p_hash` buffer in bytes
519 * \param[out] p_signature Buffer where the signature is to be written
520 * \param[in] signature_size Size of the `p_signature` buffer in bytes
521 * \param[out] p_signature_length On success, the number of bytes
522 * that make up the returned signature value
523 *
524 * \retval PSA_SUCCESS
525 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200526typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
527 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600528 psa_algorithm_t alg,
529 const uint8_t *p_hash,
530 size_t hash_length,
531 uint8_t *p_signature,
532 size_t signature_size,
533 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100534
535/**
536 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600537 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100538 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200539 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100540 * \param[in] key_slot Key slot of a public key or an asymmetric key
541 * pair
542 * \param[in] alg A signature algorithm that is compatible with
543 * the type of `key`
544 * \param[in] p_hash The hash whose signature is to be verified
545 * \param[in] hash_length Size of the `p_hash` buffer in bytes
546 * \param[in] p_signature Buffer containing the signature to verify
547 * \param[in] signature_length Size of the `p_signature` buffer in bytes
548 *
549 * \retval PSA_SUCCESS
550 * The signature is valid.
551 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200552typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
553 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600554 psa_algorithm_t alg,
555 const uint8_t *p_hash,
556 size_t hash_length,
557 const uint8_t *p_signature,
558 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100559
560/**
561 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600562 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100563 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200564 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100565 * \param[in] key_slot Key slot of a public key or an asymmetric key
566 * pair
567 * \param[in] alg An asymmetric encryption algorithm that is
568 * compatible with the type of `key`
569 * \param[in] p_input The message to encrypt
570 * \param[in] input_length Size of the `p_input` buffer in bytes
571 * \param[in] p_salt A salt or label, if supported by the
572 * encryption algorithm
573 * If the algorithm does not support a
574 * salt, pass `NULL`.
575 * If the algorithm supports an optional
576 * salt and you do not want to pass a salt,
577 * pass `NULL`.
578 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
579 * supported.
580 * \param[in] salt_length Size of the `p_salt` buffer in bytes
581 * If `p_salt` is `NULL`, pass 0.
582 * \param[out] p_output Buffer where the encrypted message is to
583 * be written
584 * \param[in] output_size Size of the `p_output` buffer in bytes
585 * \param[out] p_output_length On success, the number of bytes that make up
586 * the returned output
587 *
588 * \retval PSA_SUCCESS
589 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200590typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
591 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600592 psa_algorithm_t alg,
593 const uint8_t *p_input,
594 size_t input_length,
595 const uint8_t *p_salt,
596 size_t salt_length,
597 uint8_t *p_output,
598 size_t output_size,
599 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100600
601/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600602 * \brief A function that decrypts a short message with an asymmetric private
603 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100604 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200605 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100606 * \param[in] key_slot Key slot of an asymmetric key pair
607 * \param[in] alg An asymmetric encryption algorithm that is
608 * compatible with the type of `key`
609 * \param[in] p_input The message to decrypt
610 * \param[in] input_length Size of the `p_input` buffer in bytes
611 * \param[in] p_salt A salt or label, if supported by the
612 * encryption algorithm
613 * If the algorithm does not support a
614 * salt, pass `NULL`.
615 * If the algorithm supports an optional
616 * salt and you do not want to pass a salt,
617 * pass `NULL`.
618 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
619 * supported.
620 * \param[in] salt_length Size of the `p_salt` buffer in bytes
621 * If `p_salt` is `NULL`, pass 0.
622 * \param[out] p_output Buffer where the decrypted message is to
623 * be written
624 * \param[in] output_size Size of the `p_output` buffer in bytes
625 * \param[out] p_output_length On success, the number of bytes
626 * that make up the returned output
627 *
628 * \retval PSA_SUCCESS
629 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200630typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
631 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600632 psa_algorithm_t alg,
633 const uint8_t *p_input,
634 size_t input_length,
635 const uint8_t *p_salt,
636 size_t salt_length,
637 uint8_t *p_output,
638 size_t output_size,
639 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100640
641/**
642 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600643 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100644 *
645 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600646 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100647 *
648 * If one of the functions is not implemented, it should be set to NULL.
649 */
650typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600651 /** Function that performs an asymmetric sign operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600652 psa_drv_se_asymmetric_sign_t p_sign;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600653 /** Function that performs an asymmetric verify operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600654 psa_drv_se_asymmetric_verify_t p_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600655 /** Function that performs an asymmetric encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600656 psa_drv_se_asymmetric_encrypt_t p_encrypt;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600657 /** Function that performs an asymmetric decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600658 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600659} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100660
661/**@}*/
662
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600663/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
664 * Authenticated Encryption with Additional Data (AEAD) operations with secure
665 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100666 * implementers as there must be sufficient space in memory for the entire
667 * message, it prevents decrypted data from being made available before the
668 * authentication operation is complete and the data is known to be authentic.
669 */
670/**@{*/
671
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600672/** \brief A function that performs a secure element authenticated encryption
673 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100674 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200675 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100676 * \param[in] key_slot Slot containing the key to use.
677 * \param[in] algorithm The AEAD algorithm to compute
678 * (\c PSA_ALG_XXX value such that
679 * #PSA_ALG_IS_AEAD(`alg`) is true)
680 * \param[in] p_nonce Nonce or IV to use
681 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
682 * \param[in] p_additional_data Additional data that will be
683 * authenticated but not encrypted
684 * \param[in] additional_data_length Size of `p_additional_data` in bytes
685 * \param[in] p_plaintext Data that will be authenticated and
686 * encrypted
687 * \param[in] plaintext_length Size of `p_plaintext` in bytes
688 * \param[out] p_ciphertext Output buffer for the authenticated and
689 * encrypted data. The additional data is
690 * not part of this output. For algorithms
691 * where the encrypted data and the
692 * authentication tag are defined as
693 * separate outputs, the authentication
694 * tag is appended to the encrypted data.
695 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
696 * bytes
697 * \param[out] p_ciphertext_length On success, the size of the output in
698 * the `p_ciphertext` buffer
699 *
700 * \retval #PSA_SUCCESS
701 * Success.
702 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200703typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
704 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600705 psa_algorithm_t algorithm,
706 const uint8_t *p_nonce,
707 size_t nonce_length,
708 const uint8_t *p_additional_data,
709 size_t additional_data_length,
710 const uint8_t *p_plaintext,
711 size_t plaintext_length,
712 uint8_t *p_ciphertext,
713 size_t ciphertext_size,
714 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100715
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600716/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100717 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200718 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100719 * \param[in] key_slot Slot containing the key to use
720 * \param[in] algorithm The AEAD algorithm to compute
721 * (\c PSA_ALG_XXX value such that
722 * #PSA_ALG_IS_AEAD(`alg`) is true)
723 * \param[in] p_nonce Nonce or IV to use
724 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
725 * \param[in] p_additional_data Additional data that has been
726 * authenticated but not encrypted
727 * \param[in] additional_data_length Size of `p_additional_data` in bytes
728 * \param[in] p_ciphertext Data that has been authenticated and
729 * encrypted.
730 * For algorithms where the encrypted data
731 * and the authentication tag are defined
732 * as separate inputs, the buffer must
733 * contain the encrypted data followed by
734 * the authentication tag.
735 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
736 * \param[out] p_plaintext Output buffer for the decrypted data
737 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
738 * bytes
739 * \param[out] p_plaintext_length On success, the size of the output in
740 * the `p_plaintext` buffer
741 *
742 * \retval #PSA_SUCCESS
743 * Success.
744 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200745typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
746 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600747 psa_algorithm_t algorithm,
748 const uint8_t *p_nonce,
749 size_t nonce_length,
750 const uint8_t *p_additional_data,
751 size_t additional_data_length,
752 const uint8_t *p_ciphertext,
753 size_t ciphertext_length,
754 uint8_t *p_plaintext,
755 size_t plaintext_size,
756 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100757
758/**
759 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600760 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100761 *
762 * PSA Crypto API implementations should populate instances of the table as
763 * appropriate upon startup.
764 *
765 * If one of the functions is not implemented, it should be set to NULL.
766 */
767typedef struct {
768 /** Function that performs the AEAD encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600769 psa_drv_se_aead_encrypt_t p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100770 /** Function that performs the AEAD decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600771 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600772} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100773/**@}*/
774
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600775/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100776 * Currently, key management is limited to importing keys in the clear,
777 * destroying keys, and exporting keys in the clear.
778 * Whether a key may be exported is determined by the key policies in place
779 * on the key slot.
780 */
781/**@{*/
782
Gilles Peskinef2223c82019-07-12 23:33:02 +0200783/* This type is documented in crypto.h. As far as drivers are concerned,
784 * this is an opaque type. */
785typedef struct psa_key_attributes_s psa_key_attributes_t;
786
787/** \brief A function that allocates a slot for a key.
788 *
789 * \param[in,out] drv_context The driver context structure.
790 * \param[in] attributes Attributes of the key.
791 * \param[out] key_slot Slot where the key will be stored.
792 * This must be a valid slot for a key of the
793 * chosen type. It must be unoccupied.
794 *
795 * \retval #PSA_SUCCESS
796 * Success.
797 * The core will record \c *key_slot as the key slot where the key
798 * is stored and will update the persistent data in storage.
799 * \retval #PSA_ERROR_NOT_SUPPORTED
800 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
801 */
802typedef psa_status_t (*psa_drv_se_allocate_key_t)(
803 psa_drv_se_context_t *drv_context,
804 const psa_key_attributes_t *attributes,
805 psa_key_slot_number_t *key_slot);
806
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600807/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100808 *
809 * This function can support any output from psa_export_key(). Refer to the
810 * documentation of psa_export_key() for the format for each key type.
811 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200812 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100813 * \param[in] key_slot Slot where the key will be stored
814 * This must be a valid slot for a key of the chosen
815 * type. It must be unoccupied.
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600816 * \param[in] lifetime The required lifetime of the key storage
Gilles Peskine75976892018-12-12 15:55:09 +0100817 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
818 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
819 * \param[in] usage The allowed uses of the key
820 * \param[in] p_data Buffer containing the key data
821 * \param[in] data_length Size of the `data` buffer in bytes
822 *
823 * \retval #PSA_SUCCESS
824 * Success.
825 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200826typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_context,
827 psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600828 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600829 psa_key_type_t type,
830 psa_algorithm_t algorithm,
831 psa_key_usage_t usage,
832 const uint8_t *p_data,
833 size_t data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100834
835/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600836 * \brief A function that destroys a secure element key and restore the slot to
837 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100838 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600839 * This function destroys the content of the key from a secure element.
840 * Implementations shall make a best effort to ensure that any previous content
841 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100842 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600843 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100844 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200845 * \param[in,out] drv_context The driver context structure.
846 * \param key_slot The key slot to erase.
Gilles Peskine75976892018-12-12 15:55:09 +0100847 *
848 * \retval #PSA_SUCCESS
849 * The slot's content, if any, has been erased.
850 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200851typedef psa_status_t (*psa_drv_se_destroy_key_t)(
852 psa_drv_se_context_t *drv_context,
853 psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +0100854
855/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600856 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100857 *
858 * The output of this function can be passed to psa_import_key() to
859 * create an equivalent object.
860 *
861 * If a key is created with `psa_import_key()` and then exported with
862 * this function, it is not guaranteed that the resulting data is
863 * identical: the implementation may choose a different representation
864 * of the same key if the format permits it.
865 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600866 * This function should generate output in the same format that
867 * `psa_export_key()` does. Refer to the
868 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100869 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200870 * \param[in,out] drv_context The driver context structure.
Gilles Peskine75976892018-12-12 15:55:09 +0100871 * \param[in] key Slot whose content is to be exported. This must
872 * be an occupied key slot.
873 * \param[out] p_data Buffer where the key data is to be written.
874 * \param[in] data_size Size of the `p_data` buffer in bytes.
875 * \param[out] p_data_length On success, the number of bytes
876 * that make up the key data.
877 *
878 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +0200879 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine75976892018-12-12 15:55:09 +0100880 * \retval #PSA_ERROR_NOT_PERMITTED
881 * \retval #PSA_ERROR_NOT_SUPPORTED
882 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
883 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200884 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine75976892018-12-12 15:55:09 +0100885 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200886typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
887 psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600888 uint8_t *p_data,
889 size_t data_size,
890 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100891
892/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600893 * \brief A function that generates a symmetric or asymmetric key on a secure
894 * element
Gilles Peskine75976892018-12-12 15:55:09 +0100895 *
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100896 * If \p type is asymmetric (`#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) == 1`),
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600897 * the public component of the generated key will be placed in `p_pubkey_out`.
898 * The format of the public key information will match the format specified for
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100899 * the psa_export_key() function for the key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100900 *
Gilles Peskine8597bc12019-07-12 23:28:46 +0200901 * \param[in,out] drv_context The driver context structure.
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600902 * \param[in] key_slot Slot where the generated key will be placed
903 * \param[in] type The type of the key to be generated
904 * \param[in] usage The prescribed usage of the generated key
Gilles Peskinec3044a62019-03-06 17:56:28 +0100905 * Note: Not all Secure Elements support the same
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600906 * restrictions that PSA Crypto does (and vice versa).
907 * Driver developers should endeavor to match the
908 * usages as close as possible.
909 * \param[in] bits The size in bits of the key to be generated.
910 * \param[in] extra Extra parameters for key generation. The
911 * interpretation of this parameter should match the
912 * interpretation in the `extra` parameter is the
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200913 * `psa_generate_key` function
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100914 * \param[in] extra_size The size in bytes of the \p extra buffer
Gilles Peskinec3044a62019-03-06 17:56:28 +0100915 * \param[out] p_pubkey_out The buffer where the public key information will
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600916 * be placed
917 * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer
918 * \param[out] p_pubkey_length Upon successful completion, will contain the
919 * size of the data placed in `p_pubkey_out`.
Gilles Peskine75976892018-12-12 15:55:09 +0100920 */
Gilles Peskine8597bc12019-07-12 23:28:46 +0200921typedef psa_status_t (*psa_drv_se_generate_key_t)(psa_drv_se_context_t *drv_context,
922 psa_key_slot_number_t key_slot,
Gilles Peskine32668ce2019-03-06 18:29:57 +0100923 psa_key_type_t type,
924 psa_key_usage_t usage,
925 size_t bits,
926 const void *extra,
927 size_t extra_size,
928 uint8_t *p_pubkey_out,
929 size_t pubkey_out_size,
930 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100931
932/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600933 * \brief A struct containing all of the function pointers needed to for secure
934 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +0100935 *
936 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600937 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100938 *
939 * If one of the functions is not implemented, it should be set to NULL.
940 */
941typedef struct {
Gilles Peskinef2223c82019-07-12 23:33:02 +0200942 /** Function that allocates a slot. */
943 psa_drv_se_allocate_key_t p_allocate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600944 /** Function that performs a key import operation */
945 psa_drv_se_import_key_t p_import;
946 /** Function that performs a generation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600947 psa_drv_se_generate_key_t p_generate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600948 /** Function that performs a key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600949 psa_drv_se_destroy_key_t p_destroy;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600950 /** Function that performs a key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600951 psa_drv_se_export_key_t p_export;
Gilles Peskinee62b74e2019-06-25 15:25:09 +0200952 /** Function that performs a public key export operation */
953 psa_drv_se_export_key_t p_export_public;
Derek Miller83d26622019-02-15 16:41:22 -0600954} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100955
956/**@}*/
957
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600958/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100959 * Key derivation is the process of generating new key material using an
960 * existing key and additional parameters, iterating through a basic
961 * cryptographic function, such as a hash.
962 * Key agreement is a part of cryptographic protocols that allows two parties
963 * to agree on the same key value, but starting from different original key
964 * material.
965 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
966 * for both of the flows.
967 *
968 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600969 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
970 * `psa_drv_se_key_derivation_derive` is used when the key material should be
971 * placed in a slot on the hardware and not exposed to the caller.
972 * `psa_drv_se_key_derivation_export` is used when the key material should be
973 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +0100974 *
975 * Different key derivation algorithms require a different number of inputs.
976 * Instead of having an API that takes as input variable length arrays, which
977 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600978 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
979 * is called multiple times with different `collateral_id`s. Thus, for a key
Gilles Peskine75976892018-12-12 15:55:09 +0100980 * derivation algorithm that required 3 paramter inputs, the flow would look
981 * something like:
982 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600983 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
984 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
985 * p_collateral_0,
986 * collateral_0_size);
987 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
988 * p_collateral_1,
989 * collateral_1_size);
990 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
991 * p_collateral_2,
992 * collateral_2_size);
993 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +0100994 * ~~~~~~~~~~~~~
995 *
996 * key agreement example:
997 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600998 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
999 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1000 * psa_drv_se_key_derivation_export(p_session_key,
1001 * session_key_size,
1002 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001003 * ~~~~~~~~~~~~~
1004 */
1005/**@{*/
1006
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001007/** \brief A function that Sets up a secure element key derivation operation by
1008 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +01001009 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001010 * \param[in,out] drv_context The driver context structure.
1011 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001012 * context information for the implementation
1013 * \param[in] kdf_alg The algorithm to be used for the key derivation
Gilles Peskine45a8ca32019-06-24 15:08:56 +02001014 * \param[in] source_key The key to be used as the source material for the
Gilles Peskine75976892018-12-12 15:55:09 +01001015 * key derivation
1016 *
1017 * \retval PSA_SUCCESS
1018 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001019typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1020 void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001021 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -06001022 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001023
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001024/** \brief A function that provides collateral (parameters) needed for a secure
1025 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +01001026 *
1027 * Since many key derivation algorithms require multiple parameters, it is
1028 * expeced that this function may be called multiple times for the same
1029 * operation, each with a different algorithm-specific `collateral_id`
1030 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001031 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001032 * context information for the implementation
1033 * \param[in] collateral_id An ID for the collateral being provided
1034 * \param[in] p_collateral A buffer containing the collateral data
1035 * \param[in] collateral_size The size in bytes of the collateral
1036 *
1037 * \retval PSA_SUCCESS
1038 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001039typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
Derek Miller83d26622019-02-15 16:41:22 -06001040 uint32_t collateral_id,
1041 const uint8_t *p_collateral,
1042 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +01001043
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001044/** \brief A function that performs the final secure element key derivation
1045 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +01001046 *
Gilles Peskine8597bc12019-07-12 23:28:46 +02001047 * \param[in,out] op_context A hardware-specific structure containing any
Gilles Peskine75976892018-12-12 15:55:09 +01001048 * context information for the implementation
1049 * \param[in] dest_key The slot where the generated key material
1050 * should be placed
1051 *
1052 * \retval PSA_SUCCESS
1053 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001054typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001055 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +01001056
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001057/** \brief A function that performs the final step of a secure element key
1058 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001059 *
1060 * \param[out] p_output Buffer in which to place the generated key
1061 * material
1062 * \param[in] output_size The size in bytes of `p_output`
1063 * \param[out] p_output_length Upon success, contains the number of bytes of
1064 * key material placed in `p_output`
1065 *
1066 * \retval PSA_SUCCESS
1067 */
Gilles Peskine8597bc12019-07-12 23:28:46 +02001068typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
Derek Miller62117262019-02-15 17:12:26 -06001069 uint8_t *p_output,
1070 size_t output_size,
1071 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001072
1073/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001074 * \brief A struct containing all of the function pointers needed to for secure
1075 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001076 *
1077 * PSA Crypto API implementations should populate instances of the table as
1078 * appropriate upon startup.
1079 *
1080 * If one of the functions is not implemented, it should be set to NULL.
1081 */
1082typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001083 /** The driver-specific size of the key derivation context */
1084 size_t context_size;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001085 /** Function that performs a key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -06001086 psa_drv_se_key_derivation_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001087 /** Function that sets key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -06001088 psa_drv_se_key_derivation_collateral_t p_collateral;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001089 /** Function that performs a final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -06001090 psa_drv_se_key_derivation_derive_t p_derive;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001091 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001092 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -06001093 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -06001094} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001095
1096/**@}*/
1097
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001098/** \defgroup se_registration Secure element driver registration
1099 */
1100/**@{*/
1101
1102/** A structure containing pointers to all the entry points of a
1103 * secure element driver.
1104 *
1105 * Future versions of this specification may add extra substructures at
1106 * the end of this structure.
1107 */
1108typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001109 /** The version of the driver HAL that this driver implements.
1110 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001111 * a different version of this specification.
1112 * Use #PSA_DRV_SE_HAL_VERSION.
1113 */
1114 uint32_t hal_version;
Gilles Peskine7a86da12019-07-12 23:25:38 +02001115
1116 /** The size of the driver's persistent data in bytes. */
1117 size_t persistent_data_size;
1118
1119 /** The driver initialization function.
1120 *
1121 * This function is called once during the initialization of the
1122 * PSA Cryptography subsystem, before any other function of the
1123 * driver is called. If this function returns a failure status,
1124 * the driver will be unusable, at least until the next system reset.
1125 *
1126 * If this field is \c NULL, it is equivalent to a function that does
1127 * nothing and returns #PSA_SUCCESS.
1128 */
1129 psa_drv_se_init_t p_init;
1130
Gilles Peskine6e59c422019-06-26 19:06:52 +02001131 const psa_drv_se_key_management_t *key_management;
1132 const psa_drv_se_mac_t *mac;
1133 const psa_drv_se_cipher_t *cipher;
1134 const psa_drv_se_aead_t *aead;
1135 const psa_drv_se_asymmetric_t *asymmetric;
1136 const psa_drv_se_key_derivation_t *derivation;
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001137} psa_drv_se_t;
1138
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001139/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001140 */
1141/* 0.0.0 patchlevel 5 */
1142#define PSA_DRV_SE_HAL_VERSION 0x00000005
1143
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001144/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001145 *
1146 * This function is only intended to be used by driver code, not by
1147 * application code. In implementations with separation between the
1148 * PSA cryptography module and applications, this function should
1149 * only be available to callers that run in the same memory space as
1150 * the cryptography module, and should not be exposed to applications
1151 * running in a different memory space.
1152 *
1153 * This function may be called before psa_crypto_init(). It is
1154 * implementation-defined whether this function may be called
1155 * after psa_crypto_init().
1156 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001157 * \note Implementations store metadata about keys including the lifetime
1158 * value. Therefore, from one instantiation of the PSA Cryptography
1159 * library to the next one, if there is a key in storage with a certain
1160 * lifetime value, you must always register the same driver (or an
1161 * updated version that communicates with the same secure element)
1162 * with the same lifetime value.
1163 *
Gilles Peskined910e922019-06-24 13:47:07 +02001164 * \param lifetime The lifetime value through which this driver will
1165 * be exposed to applications.
1166 * The values #PSA_KEY_LIFETIME_VOLATILE and
1167 * #PSA_KEY_LIFETIME_PERSISTENT are reserved and
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001168 * may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001169 * may reserve other values.
1170 * \param[in] methods The method table of the driver. This structure must
1171 * remain valid for as long as the cryptography
1172 * module keeps running. It is typically a global
1173 * constant.
1174 *
1175 * \return PSA_SUCCESS
1176 * The driver was successfully registered. Applications can now
1177 * use \p lifetime to access keys through the methods passed to
1178 * this function.
1179 * \return PSA_ERROR_BAD_STATE
1180 * This function was called after the initialization of the
1181 * cryptography module, and this implementation does not support
1182 * driver registration at this stage.
1183 * \return PSA_ERROR_ALREADY_EXISTS
1184 * There is already a registered driver for this value of \p lifetime.
1185 * \return PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001186 * \p lifetime is a reserved value.
Gilles Peskined910e922019-06-24 13:47:07 +02001187 * \return PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001188 * `methods->hal_version` is not supported by this implementation.
Gilles Peskined910e922019-06-24 13:47:07 +02001189 * \return PSA_ERROR_INSUFFICIENT_MEMORY
1190 * \return PSA_ERROR_NOT_PERMITTED
1191 */
1192psa_status_t psa_register_se_driver(
1193 psa_key_lifetime_t lifetime,
1194 const psa_drv_se_t *methods);
1195
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001196/**@}*/
1197
Gilles Peskine75976892018-12-12 15:55:09 +01001198#ifdef __cplusplus
1199}
1200#endif
1201
1202#endif /* PSA_CRYPTO_SE_DRIVER_H */