blob: 58515a1881b669c6dcbc03ad65f0190150e71f13 [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 *
141 * \param[in,out] p_context A structure that will contain the
142 * hardware-specific MAC context
143 * \param[in] key_slot The slot of the key to be used for the
144 * operation
145 * \param[in] algorithm The algorithm to be used to underly the MAC
146 * operation
147 *
148 * \retval PSA_SUCCESS
149 * Success.
150 */
Derek Miller83d26622019-02-15 16:41:22 -0600151typedef psa_status_t (*psa_drv_se_mac_setup_t)(void *p_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600152 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600153 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +0100154
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600155/** \brief A function that continues a previously started secure element MAC
156 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100157 *
158 * \param[in,out] p_context A hardware-specific structure for the
159 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600160 * updated
Gilles Peskine75976892018-12-12 15:55:09 +0100161 * \param[in] p_input A buffer containing the message to be appended
162 * to the MAC operation
163 * \param[in] input_length The size in bytes of the input message buffer
164 */
Derek Miller83d26622019-02-15 16:41:22 -0600165typedef psa_status_t (*psa_drv_se_mac_update_t)(void *p_context,
166 const uint8_t *p_input,
167 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100168
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600169/** \brief a function that completes a previously started secure element MAC
170 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +0100171 *
172 * \param[in,out] p_context A hardware-specific structure for the
173 * previously started MAC operation to be
174 * finished
175 * \param[out] p_mac A buffer where the generated MAC will be
176 * placed
177 * \param[in] mac_size The size in bytes of the buffer that has been
178 * allocated for the `output` buffer
179 * \param[out] p_mac_length After completion, will contain the number of
180 * bytes placed in the `p_mac` buffer
181 *
182 * \retval PSA_SUCCESS
183 * Success.
184 */
Derek Miller83d26622019-02-15 16:41:22 -0600185typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *p_context,
186 uint8_t *p_mac,
187 size_t mac_size,
188 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100189
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600190/** \brief A function that completes a previously started secure element MAC
191 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100192 *
193 * \param[in,out] p_context A hardware-specific structure for the previously
194 * started MAC operation to be fiinished
195 * \param[in] p_mac The MAC value against which the resulting MAC will
196 * be compared against
197 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
198 *
199 * \retval PSA_SUCCESS
200 * The operation completed successfully and the MACs matched each
201 * other
202 * \retval PSA_ERROR_INVALID_SIGNATURE
203 * The operation completed successfully, but the calculated MAC did
204 * not match the provided MAC
205 */
Derek Miller83d26622019-02-15 16:41:22 -0600206typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *p_context,
207 const uint8_t *p_mac,
208 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100209
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600210/** \brief A function that aborts a previous started secure element MAC
211 * operation
Gilles Peskine32668ce2019-03-06 18:29:57 +0100212 *
Gilles Peskine75976892018-12-12 15:55:09 +0100213 * \param[in,out] p_context A hardware-specific structure for the previously
214 * started MAC operation to be aborted
215 */
Derek Miller83d26622019-02-15 16:41:22 -0600216typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100217
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600218/** \brief A function that performs a secure element MAC operation in one
219 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100220 *
221 * \param[in] p_input A buffer containing the message to be MACed
222 * \param[in] input_length The size in bytes of `p_input`
223 * \param[in] key_slot The slot of the key to be used
224 * \param[in] alg The algorithm to be used to underlie the MAC
225 * operation
226 * \param[out] p_mac A buffer where the generated MAC will be
227 * placed
228 * \param[in] mac_size The size in bytes of the `p_mac` buffer
229 * \param[out] p_mac_length After completion, will contain the number of
230 * bytes placed in the `output` buffer
231 *
232 * \retval PSA_SUCCESS
233 * Success.
234 */
Derek Miller83d26622019-02-15 16:41:22 -0600235typedef psa_status_t (*psa_drv_se_mac_generate_t)(const uint8_t *p_input,
236 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600237 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600238 psa_algorithm_t alg,
239 uint8_t *p_mac,
240 size_t mac_size,
241 size_t *p_mac_length);
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 compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100245 *
246 * \param[in] p_input A buffer containing the message to be MACed
247 * \param[in] input_length The size in bytes of `input`
248 * \param[in] key_slot The slot of the key to be used
249 * \param[in] alg The algorithm to be used to underlie the MAC
250 * operation
251 * \param[in] p_mac The MAC value against which the resulting MAC will
252 * be compared against
253 * \param[in] mac_length The size in bytes of `mac`
254 *
255 * \retval PSA_SUCCESS
256 * The operation completed successfully and the MACs matched each
257 * other
258 * \retval PSA_ERROR_INVALID_SIGNATURE
259 * The operation completed successfully, but the calculated MAC did
260 * not match the provided MAC
261 */
Derek Miller83d26622019-02-15 16:41:22 -0600262typedef psa_status_t (*psa_drv_se_mac_verify_t)(const uint8_t *p_input,
263 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 const uint8_t *p_mac,
267 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100268
269/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600270 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100271 *
272 * PSA Crypto API implementations should populate the table as appropriate
273 * upon startup.
274 *
275 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600276 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100277 *
278 * Driver implementers should ensure that they implement all of the functions
279 * that make sense for their hardware, and that they provide a full solution
280 * (for example, if they support `p_setup`, they should also support
281 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
282 *
283 */
284typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600285 /**The size in bytes of the hardware-specific secure element MAC context
286 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100287 */
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600288 size_t context_size;
289 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100290 */
Derek Millerea743cf2019-02-15 17:06:29 -0600291 psa_drv_se_mac_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600292 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100293 */
Derek Millerea743cf2019-02-15 17:06:29 -0600294 psa_drv_se_mac_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600295 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100296 */
Derek Millerea743cf2019-02-15 17:06:29 -0600297 psa_drv_se_mac_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600298 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100299 */
Derek Millerea743cf2019-02-15 17:06:29 -0600300 psa_drv_se_mac_finish_verify_t p_finish_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600301 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100302 */
Derek Millerea743cf2019-02-15 17:06:29 -0600303 psa_drv_se_mac_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600304 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100305 */
Derek Millerea743cf2019-02-15 17:06:29 -0600306 psa_drv_se_mac_generate_t p_mac;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600307 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100308 */
Derek Millerea743cf2019-02-15 17:06:29 -0600309 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600310} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100311/**@}*/
312
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600313/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100314 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600315 * Encryption and Decryption using secure element keys in block modes other
316 * than ECB must be done in multiple parts, using the following flow:
317 * - `psa_drv_se_cipher_setup_t`
318 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
319 * - `psa_drv_se_cipher_update_t`
320 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100321 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600322 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine32668ce2019-03-06 18:29:57 +0100323 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600324 * If a previously started secure element Cipher operation needs to be
325 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
326 * to do so may result in allocated resources not being freed or in other
327 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100328 *
329 * In situations where a PSA Cryptographic API implementation is using a block
330 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600331 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
332 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100333 */
334/**@{*/
335
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600336/** \brief A function that provides the cipher setup function for a
337 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100338 *
339 * \param[in,out] p_context A structure that will contain the
340 * hardware-specific cipher context.
341 * \param[in] key_slot The slot of the key to be used for the
342 * operation
343 * \param[in] algorithm The algorithm to be used in the cipher
344 * operation
345 * \param[in] direction Indicates whether the operation is an encrypt
346 * or decrypt
347 *
348 * \retval PSA_SUCCESS
349 * \retval PSA_ERROR_NOT_SUPPORTED
350 */
Derek Miller83d26622019-02-15 16:41:22 -0600351typedef psa_status_t (*psa_drv_se_cipher_setup_t)(void *p_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600352 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600353 psa_algorithm_t algorithm,
354 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100355
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600356/** \brief A function that sets the initialization vector (if
357 * necessary) for an secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100358 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600359 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
360 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100361 * generate function is not necessary for the drivers to implement as the PSA
362 * Crypto implementation can do the generation using its RNG features.
363 *
364 * \param[in,out] p_context A structure that contains the previously set up
365 * hardware-specific cipher context
366 * \param[in] p_iv A buffer containing the initialization vector
367 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
368 *
369 * \retval PSA_SUCCESS
370 */
Derek Miller83d26622019-02-15 16:41:22 -0600371typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *p_context,
372 const uint8_t *p_iv,
373 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100374
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600375/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100376 * operation
377 *
378 * \param[in,out] p_context A hardware-specific structure for the
379 * previously started cipher operation
380 * \param[in] p_input A buffer containing the data to be
381 * encrypted/decrypted
382 * \param[in] input_size The size in bytes of the buffer pointed to
383 * by `p_input`
384 * \param[out] p_output The caller-allocated buffer where the
385 * output will be placed
386 * \param[in] output_size The allocated size in bytes of the
387 * `p_output` buffer
388 * \param[out] p_output_length After completion, will contain the number
389 * of bytes placed in the `p_output` buffer
390 *
391 * \retval PSA_SUCCESS
392 */
Derek Miller83d26622019-02-15 16:41:22 -0600393typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *p_context,
394 const uint8_t *p_input,
395 size_t input_size,
396 uint8_t *p_output,
397 size_t output_size,
398 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100399
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600400/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100401 * operation
402 *
403 * \param[in,out] p_context A hardware-specific structure for the
404 * previously started cipher operation
405 * \param[out] p_output The caller-allocated buffer where the output
406 * will be placed
407 * \param[in] output_size The allocated size in bytes of the `p_output`
408 * buffer
409 * \param[out] p_output_length After completion, will contain the number of
410 * bytes placed in the `p_output` buffer
411 *
412 * \retval PSA_SUCCESS
413 */
Derek Miller83d26622019-02-15 16:41:22 -0600414typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *p_context,
415 uint8_t *p_output,
416 size_t output_size,
417 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100418
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600419/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100420 * operation
421 *
422 * \param[in,out] p_context A hardware-specific structure for the
423 * previously started cipher operation
424 */
Derek Miller83d26622019-02-15 16:41:22 -0600425typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100426
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600427/** \brief A function that performs the ECB block mode for secure element
428 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100429 *
430 * Note: this function should only be used with implementations that do not
431 * provide a needed higher-level operation.
432 *
433 * \param[in] key_slot The slot of the key to be used for the operation
434 * \param[in] algorithm The algorithm to be used in the cipher operation
435 * \param[in] direction Indicates whether the operation is an encrypt or
436 * decrypt
437 * \param[in] p_input A buffer containing the data to be
438 * encrypted/decrypted
439 * \param[in] input_size The size in bytes of the buffer pointed to by
440 * `p_input`
441 * \param[out] p_output The caller-allocated buffer where the output will
442 * be placed
443 * \param[in] output_size The allocated size in bytes of the `p_output`
444 * buffer
445 *
446 * \retval PSA_SUCCESS
447 * \retval PSA_ERROR_NOT_SUPPORTED
448 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600449typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600450 psa_algorithm_t algorithm,
451 psa_encrypt_or_decrypt_t direction,
452 const uint8_t *p_input,
453 size_t input_size,
454 uint8_t *p_output,
455 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100456
457/**
458 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600459 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100460 *
461 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600462 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100463 *
464 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600465 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100466 */
467typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600468 /** The size in bytes of the hardware-specific secure element cipher
469 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100470 */
Derek Miller34b33f12019-02-15 17:13:54 -0600471 size_t context_size;
472 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600473 psa_drv_se_cipher_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600474 /** Function that sets a cipher IV (if necessary) */
Derek Millerea743cf2019-02-15 17:06:29 -0600475 psa_drv_se_cipher_set_iv_t p_set_iv;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600476 /** Function that performs a cipher update operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600477 psa_drv_se_cipher_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600478 /** Function that completes a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600479 psa_drv_se_cipher_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600480 /** Function that aborts a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600481 psa_drv_se_cipher_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600482 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100483 * (Danger: ECB mode should not be used directly by clients of the PSA
484 * Crypto Client API)
485 */
Derek Millerea743cf2019-02-15 17:06:29 -0600486 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600487} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100488
489/**@}*/
490
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600491/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100492 *
493 * Since the amount of data that can (or should) be encrypted or signed using
494 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600495 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100496 */
497/**@{*/
498
499/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600500 * \brief A function that signs a hash or short message with a private key in
501 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100502 *
503 * \param[in] key_slot Key slot of an asymmetric key pair
504 * \param[in] alg A signature algorithm that is compatible
505 * with the type of `key`
506 * \param[in] p_hash The hash to sign
507 * \param[in] hash_length Size of the `p_hash` buffer in bytes
508 * \param[out] p_signature Buffer where the signature is to be written
509 * \param[in] signature_size Size of the `p_signature` buffer in bytes
510 * \param[out] p_signature_length On success, the number of bytes
511 * that make up the returned signature value
512 *
513 * \retval PSA_SUCCESS
514 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600515typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600516 psa_algorithm_t alg,
517 const uint8_t *p_hash,
518 size_t hash_length,
519 uint8_t *p_signature,
520 size_t signature_size,
521 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100522
523/**
524 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600525 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100526 *
527 * \param[in] key_slot Key slot of a public key or an asymmetric key
528 * pair
529 * \param[in] alg A signature algorithm that is compatible with
530 * the type of `key`
531 * \param[in] p_hash The hash whose signature is to be verified
532 * \param[in] hash_length Size of the `p_hash` buffer in bytes
533 * \param[in] p_signature Buffer containing the signature to verify
534 * \param[in] signature_length Size of the `p_signature` buffer in bytes
535 *
536 * \retval PSA_SUCCESS
537 * The signature is valid.
538 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600539typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600540 psa_algorithm_t alg,
541 const uint8_t *p_hash,
542 size_t hash_length,
543 const uint8_t *p_signature,
544 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100545
546/**
547 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600548 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100549 *
550 * \param[in] key_slot Key slot of a public key or an asymmetric key
551 * pair
552 * \param[in] alg An asymmetric encryption algorithm that is
553 * compatible with the type of `key`
554 * \param[in] p_input The message to encrypt
555 * \param[in] input_length Size of the `p_input` buffer in bytes
556 * \param[in] p_salt A salt or label, if supported by the
557 * encryption algorithm
558 * If the algorithm does not support a
559 * salt, pass `NULL`.
560 * If the algorithm supports an optional
561 * salt and you do not want to pass a salt,
562 * pass `NULL`.
563 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
564 * supported.
565 * \param[in] salt_length Size of the `p_salt` buffer in bytes
566 * If `p_salt` is `NULL`, pass 0.
567 * \param[out] p_output Buffer where the encrypted message is to
568 * be written
569 * \param[in] output_size Size of the `p_output` buffer in bytes
570 * \param[out] p_output_length On success, the number of bytes that make up
571 * the returned output
572 *
573 * \retval PSA_SUCCESS
574 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600575typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600576 psa_algorithm_t alg,
577 const uint8_t *p_input,
578 size_t input_length,
579 const uint8_t *p_salt,
580 size_t salt_length,
581 uint8_t *p_output,
582 size_t output_size,
583 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100584
585/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600586 * \brief A function that decrypts a short message with an asymmetric private
587 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100588 *
589 * \param[in] key_slot Key slot of an asymmetric key 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 decrypt
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 decrypted 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
609 * that make up the returned output
610 *
611 * \retval PSA_SUCCESS
612 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600613typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600614 psa_algorithm_t alg,
615 const uint8_t *p_input,
616 size_t input_length,
617 const uint8_t *p_salt,
618 size_t salt_length,
619 uint8_t *p_output,
620 size_t output_size,
621 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100622
623/**
624 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600625 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100626 *
627 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600628 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100629 *
630 * If one of the functions is not implemented, it should be set to NULL.
631 */
632typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600633 /** Function that performs an asymmetric sign operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600634 psa_drv_se_asymmetric_sign_t p_sign;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600635 /** Function that performs an asymmetric verify operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600636 psa_drv_se_asymmetric_verify_t p_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600637 /** Function that performs an asymmetric encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600638 psa_drv_se_asymmetric_encrypt_t p_encrypt;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600639 /** Function that performs an asymmetric decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600640 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600641} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100642
643/**@}*/
644
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600645/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
646 * Authenticated Encryption with Additional Data (AEAD) operations with secure
647 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100648 * implementers as there must be sufficient space in memory for the entire
649 * message, it prevents decrypted data from being made available before the
650 * authentication operation is complete and the data is known to be authentic.
651 */
652/**@{*/
653
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600654/** \brief A function that performs a secure element authenticated encryption
655 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100656 *
657 * \param[in] key_slot Slot containing the key to use.
658 * \param[in] algorithm The AEAD algorithm to compute
659 * (\c PSA_ALG_XXX value such that
660 * #PSA_ALG_IS_AEAD(`alg`) is true)
661 * \param[in] p_nonce Nonce or IV to use
662 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
663 * \param[in] p_additional_data Additional data that will be
664 * authenticated but not encrypted
665 * \param[in] additional_data_length Size of `p_additional_data` in bytes
666 * \param[in] p_plaintext Data that will be authenticated and
667 * encrypted
668 * \param[in] plaintext_length Size of `p_plaintext` in bytes
669 * \param[out] p_ciphertext Output buffer for the authenticated and
670 * encrypted data. The additional data is
671 * not part of this output. For algorithms
672 * where the encrypted data and the
673 * authentication tag are defined as
674 * separate outputs, the authentication
675 * tag is appended to the encrypted data.
676 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
677 * bytes
678 * \param[out] p_ciphertext_length On success, the size of the output in
679 * the `p_ciphertext` buffer
680 *
681 * \retval #PSA_SUCCESS
682 * Success.
683 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600684typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600685 psa_algorithm_t algorithm,
686 const uint8_t *p_nonce,
687 size_t nonce_length,
688 const uint8_t *p_additional_data,
689 size_t additional_data_length,
690 const uint8_t *p_plaintext,
691 size_t plaintext_length,
692 uint8_t *p_ciphertext,
693 size_t ciphertext_size,
694 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100695
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600696/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100697 *
698 * \param[in] key_slot Slot containing the key to use
699 * \param[in] algorithm The AEAD algorithm to compute
700 * (\c PSA_ALG_XXX value such that
701 * #PSA_ALG_IS_AEAD(`alg`) is true)
702 * \param[in] p_nonce Nonce or IV to use
703 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
704 * \param[in] p_additional_data Additional data that has been
705 * authenticated but not encrypted
706 * \param[in] additional_data_length Size of `p_additional_data` in bytes
707 * \param[in] p_ciphertext Data that has been authenticated and
708 * encrypted.
709 * For algorithms where the encrypted data
710 * and the authentication tag are defined
711 * as separate inputs, the buffer must
712 * contain the encrypted data followed by
713 * the authentication tag.
714 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
715 * \param[out] p_plaintext Output buffer for the decrypted data
716 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
717 * bytes
718 * \param[out] p_plaintext_length On success, the size of the output in
719 * the `p_plaintext` buffer
720 *
721 * \retval #PSA_SUCCESS
722 * Success.
723 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600724typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600725 psa_algorithm_t algorithm,
726 const uint8_t *p_nonce,
727 size_t nonce_length,
728 const uint8_t *p_additional_data,
729 size_t additional_data_length,
730 const uint8_t *p_ciphertext,
731 size_t ciphertext_length,
732 uint8_t *p_plaintext,
733 size_t plaintext_size,
734 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100735
736/**
737 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600738 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100739 *
740 * PSA Crypto API implementations should populate instances of the table as
741 * appropriate upon startup.
742 *
743 * If one of the functions is not implemented, it should be set to NULL.
744 */
745typedef struct {
746 /** Function that performs the AEAD encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600747 psa_drv_se_aead_encrypt_t p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100748 /** Function that performs the AEAD decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600749 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600750} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100751/**@}*/
752
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600753/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100754 * Currently, key management is limited to importing keys in the clear,
755 * destroying keys, and exporting keys in the clear.
756 * Whether a key may be exported is determined by the key policies in place
757 * on the key slot.
758 */
759/**@{*/
760
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600761/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100762 *
763 * This function can support any output from psa_export_key(). Refer to the
764 * documentation of psa_export_key() for the format for each key type.
765 *
766 * \param[in] key_slot Slot where the key will be stored
767 * This must be a valid slot for a key of the chosen
768 * type. It must be unoccupied.
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600769 * \param[in] lifetime The required lifetime of the key storage
Gilles Peskine75976892018-12-12 15:55:09 +0100770 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
771 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
772 * \param[in] usage The allowed uses of the key
773 * \param[in] p_data Buffer containing the key data
774 * \param[in] data_length Size of the `data` buffer in bytes
775 *
776 * \retval #PSA_SUCCESS
777 * Success.
778 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600779typedef psa_status_t (*psa_drv_se_import_key_t)(psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600780 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600781 psa_key_type_t type,
782 psa_algorithm_t algorithm,
783 psa_key_usage_t usage,
784 const uint8_t *p_data,
785 size_t data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100786
787/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600788 * \brief A function that destroys a secure element key and restore the slot to
789 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100790 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600791 * This function destroys the content of the key from a secure element.
792 * Implementations shall make a best effort to ensure that any previous content
793 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100794 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600795 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100796 *
797 * \param[in] key_slot The key slot to erase.
798 *
799 * \retval #PSA_SUCCESS
800 * The slot's content, if any, has been erased.
801 */
Gilles Peskine45a8ca32019-06-24 15:08:56 +0200802typedef psa_status_t (*psa_drv_se_destroy_key_t)(psa_key_slot_number_t key_slot);
Gilles Peskine75976892018-12-12 15:55:09 +0100803
804/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600805 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100806 *
807 * The output of this function can be passed to psa_import_key() to
808 * create an equivalent object.
809 *
810 * If a key is created with `psa_import_key()` and then exported with
811 * this function, it is not guaranteed that the resulting data is
812 * identical: the implementation may choose a different representation
813 * of the same key if the format permits it.
814 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600815 * This function should generate output in the same format that
816 * `psa_export_key()` does. Refer to the
817 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100818 *
819 * \param[in] key Slot whose content is to be exported. This must
820 * be an occupied key slot.
821 * \param[out] p_data Buffer where the key data is to be written.
822 * \param[in] data_size Size of the `p_data` buffer in bytes.
823 * \param[out] p_data_length On success, the number of bytes
824 * that make up the key data.
825 *
826 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +0200827 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine75976892018-12-12 15:55:09 +0100828 * \retval #PSA_ERROR_NOT_PERMITTED
829 * \retval #PSA_ERROR_NOT_SUPPORTED
830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
831 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine75976892018-12-12 15:55:09 +0100833 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600834typedef psa_status_t (*psa_drv_se_export_key_t)(psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600835 uint8_t *p_data,
836 size_t data_size,
837 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100838
839/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600840 * \brief A function that generates a symmetric or asymmetric key on a secure
841 * element
Gilles Peskine75976892018-12-12 15:55:09 +0100842 *
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100843 * If \p type is asymmetric (`#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) == 1`),
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600844 * the public component of the generated key will be placed in `p_pubkey_out`.
845 * The format of the public key information will match the format specified for
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100846 * the psa_export_key() function for the key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100847 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600848 * \param[in] key_slot Slot where the generated key will be placed
849 * \param[in] type The type of the key to be generated
850 * \param[in] usage The prescribed usage of the generated key
Gilles Peskinec3044a62019-03-06 17:56:28 +0100851 * Note: Not all Secure Elements support the same
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600852 * restrictions that PSA Crypto does (and vice versa).
853 * Driver developers should endeavor to match the
854 * usages as close as possible.
855 * \param[in] bits The size in bits of the key to be generated.
856 * \param[in] extra Extra parameters for key generation. The
857 * interpretation of this parameter should match the
858 * interpretation in the `extra` parameter is the
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200859 * `psa_generate_key` function
Gilles Peskinee5c025c2019-03-06 18:01:43 +0100860 * \param[in] extra_size The size in bytes of the \p extra buffer
Gilles Peskinec3044a62019-03-06 17:56:28 +0100861 * \param[out] p_pubkey_out The buffer where the public key information will
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600862 * be placed
863 * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer
864 * \param[out] p_pubkey_length Upon successful completion, will contain the
865 * size of the data placed in `p_pubkey_out`.
Gilles Peskine75976892018-12-12 15:55:09 +0100866 */
Gilles Peskine32668ce2019-03-06 18:29:57 +0100867typedef psa_status_t (*psa_drv_se_generate_key_t)(psa_key_slot_number_t key_slot,
868 psa_key_type_t type,
869 psa_key_usage_t usage,
870 size_t bits,
871 const void *extra,
872 size_t extra_size,
873 uint8_t *p_pubkey_out,
874 size_t pubkey_out_size,
875 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100876
877/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600878 * \brief A struct containing all of the function pointers needed to for secure
879 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +0100880 *
881 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600882 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100883 *
884 * If one of the functions is not implemented, it should be set to NULL.
885 */
886typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600887 /** Function that performs a key import operation */
888 psa_drv_se_import_key_t p_import;
889 /** Function that performs a generation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600890 psa_drv_se_generate_key_t p_generate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600891 /** Function that performs a key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600892 psa_drv_se_destroy_key_t p_destroy;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600893 /** Function that performs a key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600894 psa_drv_se_export_key_t p_export;
Gilles Peskinee62b74e2019-06-25 15:25:09 +0200895 /** Function that performs a public key export operation */
896 psa_drv_se_export_key_t p_export_public;
Derek Miller83d26622019-02-15 16:41:22 -0600897} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100898
899/**@}*/
900
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600901/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100902 * Key derivation is the process of generating new key material using an
903 * existing key and additional parameters, iterating through a basic
904 * cryptographic function, such as a hash.
905 * Key agreement is a part of cryptographic protocols that allows two parties
906 * to agree on the same key value, but starting from different original key
907 * material.
908 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
909 * for both of the flows.
910 *
911 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600912 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
913 * `psa_drv_se_key_derivation_derive` is used when the key material should be
914 * placed in a slot on the hardware and not exposed to the caller.
915 * `psa_drv_se_key_derivation_export` is used when the key material should be
916 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +0100917 *
918 * Different key derivation algorithms require a different number of inputs.
919 * Instead of having an API that takes as input variable length arrays, which
920 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600921 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
922 * is called multiple times with different `collateral_id`s. Thus, for a key
Gilles Peskine75976892018-12-12 15:55:09 +0100923 * derivation algorithm that required 3 paramter inputs, the flow would look
924 * something like:
925 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600926 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
927 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
928 * p_collateral_0,
929 * collateral_0_size);
930 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
931 * p_collateral_1,
932 * collateral_1_size);
933 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
934 * p_collateral_2,
935 * collateral_2_size);
936 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +0100937 * ~~~~~~~~~~~~~
938 *
939 * key agreement example:
940 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600941 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
942 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
943 * psa_drv_se_key_derivation_export(p_session_key,
944 * session_key_size,
945 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100946 * ~~~~~~~~~~~~~
947 */
948/**@{*/
949
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600950/** \brief A function that Sets up a secure element key derivation operation by
951 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +0100952 *
953 * \param[in,out] p_context A hardware-specific structure containing any
954 * context information for the implementation
955 * \param[in] kdf_alg The algorithm to be used for the key derivation
Gilles Peskine45a8ca32019-06-24 15:08:56 +0200956 * \param[in] source_key The key to be used as the source material for the
Gilles Peskine75976892018-12-12 15:55:09 +0100957 * key derivation
958 *
959 * \retval PSA_SUCCESS
960 */
Derek Miller62117262019-02-15 17:12:26 -0600961typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600962 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600963 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100964
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600965/** \brief A function that provides collateral (parameters) needed for a secure
966 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +0100967 *
968 * Since many key derivation algorithms require multiple parameters, it is
969 * expeced that this function may be called multiple times for the same
970 * operation, each with a different algorithm-specific `collateral_id`
971 *
972 * \param[in,out] p_context A hardware-specific structure containing any
973 * context information for the implementation
974 * \param[in] collateral_id An ID for the collateral being provided
975 * \param[in] p_collateral A buffer containing the collateral data
976 * \param[in] collateral_size The size in bytes of the collateral
977 *
978 * \retval PSA_SUCCESS
979 */
Derek Miller62117262019-02-15 17:12:26 -0600980typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600981 uint32_t collateral_id,
982 const uint8_t *p_collateral,
983 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100984
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600985/** \brief A function that performs the final secure element key derivation
986 * step and place the generated key material in a slot
Gilles Peskinec3044a62019-03-06 17:56:28 +0100987 *
Gilles Peskine75976892018-12-12 15:55:09 +0100988 * \param[in,out] p_context A hardware-specific structure containing any
989 * context information for the implementation
990 * \param[in] dest_key The slot where the generated key material
991 * should be placed
992 *
993 * \retval PSA_SUCCESS
994 */
Derek Miller62117262019-02-15 17:12:26 -0600995typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *p_context,
996 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100997
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600998/** \brief A function that performs the final step of a secure element key
999 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +01001000 *
1001 * \param[out] p_output Buffer in which to place the generated key
1002 * material
1003 * \param[in] output_size The size in bytes of `p_output`
1004 * \param[out] p_output_length Upon success, contains the number of bytes of
1005 * key material placed in `p_output`
1006 *
1007 * \retval PSA_SUCCESS
1008 */
Derek Miller62117262019-02-15 17:12:26 -06001009typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *p_context,
1010 uint8_t *p_output,
1011 size_t output_size,
1012 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +01001013
1014/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001015 * \brief A struct containing all of the function pointers needed to for secure
1016 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +01001017 *
1018 * PSA Crypto API implementations should populate instances of the table as
1019 * appropriate upon startup.
1020 *
1021 * If one of the functions is not implemented, it should be set to NULL.
1022 */
1023typedef struct {
Derek Miller62117262019-02-15 17:12:26 -06001024 /** The driver-specific size of the key derivation context */
1025 size_t context_size;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001026 /** Function that performs a key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -06001027 psa_drv_se_key_derivation_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001028 /** Function that sets key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -06001029 psa_drv_se_key_derivation_collateral_t p_collateral;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001030 /** Function that performs a final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -06001031 psa_drv_se_key_derivation_derive_t p_derive;
Derek Millerf0c1d0d2019-02-15 17:23:42 -06001032 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +01001033 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -06001034 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -06001035} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +01001036
1037/**@}*/
1038
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001039/** \defgroup se_registration Secure element driver registration
1040 */
1041/**@{*/
1042
1043/** A structure containing pointers to all the entry points of a
1044 * secure element driver.
1045 *
1046 * Future versions of this specification may add extra substructures at
1047 * the end of this structure.
1048 */
1049typedef struct {
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001050 /** The version of the driver HAL that this driver implements.
1051 * This is a protection against loading driver binaries built against
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001052 * a different version of this specification.
1053 * Use #PSA_DRV_SE_HAL_VERSION.
1054 */
1055 uint32_t hal_version;
Gilles Peskine7a86da12019-07-12 23:25:38 +02001056
1057 /** The size of the driver's persistent data in bytes. */
1058 size_t persistent_data_size;
1059
1060 /** The driver initialization function.
1061 *
1062 * This function is called once during the initialization of the
1063 * PSA Cryptography subsystem, before any other function of the
1064 * driver is called. If this function returns a failure status,
1065 * the driver will be unusable, at least until the next system reset.
1066 *
1067 * If this field is \c NULL, it is equivalent to a function that does
1068 * nothing and returns #PSA_SUCCESS.
1069 */
1070 psa_drv_se_init_t p_init;
1071
Gilles Peskine6e59c422019-06-26 19:06:52 +02001072 const psa_drv_se_key_management_t *key_management;
1073 const psa_drv_se_mac_t *mac;
1074 const psa_drv_se_cipher_t *cipher;
1075 const psa_drv_se_aead_t *aead;
1076 const psa_drv_se_asymmetric_t *asymmetric;
1077 const psa_drv_se_key_derivation_t *derivation;
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001078} psa_drv_se_t;
1079
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001080/** The current version of the secure element driver HAL.
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001081 */
1082/* 0.0.0 patchlevel 5 */
1083#define PSA_DRV_SE_HAL_VERSION 0x00000005
1084
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001085/** Register an external cryptoprocessor (secure element) driver.
Gilles Peskined910e922019-06-24 13:47:07 +02001086 *
1087 * This function is only intended to be used by driver code, not by
1088 * application code. In implementations with separation between the
1089 * PSA cryptography module and applications, this function should
1090 * only be available to callers that run in the same memory space as
1091 * the cryptography module, and should not be exposed to applications
1092 * running in a different memory space.
1093 *
1094 * This function may be called before psa_crypto_init(). It is
1095 * implementation-defined whether this function may be called
1096 * after psa_crypto_init().
1097 *
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001098 * \note Implementations store metadata about keys including the lifetime
1099 * value. Therefore, from one instantiation of the PSA Cryptography
1100 * library to the next one, if there is a key in storage with a certain
1101 * lifetime value, you must always register the same driver (or an
1102 * updated version that communicates with the same secure element)
1103 * with the same lifetime value.
1104 *
Gilles Peskined910e922019-06-24 13:47:07 +02001105 * \param lifetime The lifetime value through which this driver will
1106 * be exposed to applications.
1107 * The values #PSA_KEY_LIFETIME_VOLATILE and
1108 * #PSA_KEY_LIFETIME_PERSISTENT are reserved and
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001109 * may not be used for drivers. Implementations
Gilles Peskined910e922019-06-24 13:47:07 +02001110 * may reserve other values.
1111 * \param[in] methods The method table of the driver. This structure must
1112 * remain valid for as long as the cryptography
1113 * module keeps running. It is typically a global
1114 * constant.
1115 *
1116 * \return PSA_SUCCESS
1117 * The driver was successfully registered. Applications can now
1118 * use \p lifetime to access keys through the methods passed to
1119 * this function.
1120 * \return PSA_ERROR_BAD_STATE
1121 * This function was called after the initialization of the
1122 * cryptography module, and this implementation does not support
1123 * driver registration at this stage.
1124 * \return PSA_ERROR_ALREADY_EXISTS
1125 * There is already a registered driver for this value of \p lifetime.
1126 * \return PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001127 * \p lifetime is a reserved value.
Gilles Peskined910e922019-06-24 13:47:07 +02001128 * \return PSA_ERROR_NOT_SUPPORTED
Gilles Peskinec93a43b2019-06-26 11:21:41 +02001129 * `methods->hal_version` is not supported by this implementation.
Gilles Peskined910e922019-06-24 13:47:07 +02001130 * \return PSA_ERROR_INSUFFICIENT_MEMORY
1131 * \return PSA_ERROR_NOT_PERMITTED
1132 */
1133psa_status_t psa_register_se_driver(
1134 psa_key_lifetime_t lifetime,
1135 const psa_drv_se_t *methods);
1136
Gilles Peskineb6cadea2019-06-24 13:46:37 +02001137/**@}*/
1138
Gilles Peskine75976892018-12-12 15:55:09 +01001139#ifdef __cplusplus
1140}
1141#endif
1142
1143#endif /* PSA_CRYPTO_SE_DRIVER_H */