blob: 18ef1c47b0b220345d211365a2bcb5b6fc28cd8e [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 *
11 * This file is part of the PSA Crypto Driver Model, containing functions for
12 * driver developers to implement to enable hardware to be called in a
13 * standardized way by a PSA Cryptographic API implementation. The functions
14 * comprising the driver model, which driver authors implement, are not
15 * intended to be called by application developers.
16 */
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
43/** An internal designation of a key slot between the core part of the
44 * PSA Crypto implementation and the driver. The meaning of this value
45 * is driver-dependent. */
Derek Millerb2a1cce2019-02-15 17:03:42 -060046typedef uint32_t psa_key_slot_number_t; // TODO: Change this to psa_key_slot_t after psa_key_slot_t is removed from Mbed crypto
Gilles Peskine75976892018-12-12 15:55:09 +010047
Derek Millerf0c1d0d2019-02-15 17:23:42 -060048/** \defgroup se_mac Secure Element Message Authentication Codes
Gilles Peskine75976892018-12-12 15:55:09 +010049 * Generation and authentication of Message Authentication Codes (MACs) using
Derek Millerf0c1d0d2019-02-15 17:23:42 -060050 * a secure element can be done either as a single function call (via the
51 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
Gilles Peskine75976892018-12-12 15:55:09 +010052 * parts using the following sequence:
Derek Millerf0c1d0d2019-02-15 17:23:42 -060053 * - `psa_drv_se_mac_setup_t`
54 * - `psa_drv_se_mac_update_t`
55 * - `psa_drv_se_mac_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +010056 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -060057 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
Gilles Peskine75976892018-12-12 15:55:09 +010058 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -060059 * If a previously started secure element MAC operation needs to be terminated,
60 * 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 +010061 * result in allocated resources not being freed or in other undefined
62 * behavior.
63 */
64/**@{*/
Derek Millerf0c1d0d2019-02-15 17:23:42 -060065/** \brief A function that starts a secure element MAC operation for a PSA
66 * Crypto Driver implementation
Gilles Peskine75976892018-12-12 15:55:09 +010067 *
68 * \param[in,out] p_context A structure that will contain the
69 * hardware-specific MAC context
70 * \param[in] key_slot The slot of the key to be used for the
71 * operation
72 * \param[in] algorithm The algorithm to be used to underly the MAC
73 * operation
74 *
75 * \retval PSA_SUCCESS
76 * Success.
77 */
Derek Miller83d26622019-02-15 16:41:22 -060078typedef psa_status_t (*psa_drv_se_mac_setup_t)(void *p_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -060079 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -060080 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +010081
Derek Millerf0c1d0d2019-02-15 17:23:42 -060082/** \brief A function that continues a previously started secure element MAC
83 * operation
Gilles Peskine75976892018-12-12 15:55:09 +010084 *
85 * \param[in,out] p_context A hardware-specific structure for the
86 * previously-established MAC operation to be
Derek Millerf0c1d0d2019-02-15 17:23:42 -060087 * updated
Gilles Peskine75976892018-12-12 15:55:09 +010088 * \param[in] p_input A buffer containing the message to be appended
89 * to the MAC operation
90 * \param[in] input_length The size in bytes of the input message buffer
91 */
Derek Miller83d26622019-02-15 16:41:22 -060092typedef psa_status_t (*psa_drv_se_mac_update_t)(void *p_context,
93 const uint8_t *p_input,
94 size_t input_length);
Gilles Peskine75976892018-12-12 15:55:09 +010095
Derek Millerf0c1d0d2019-02-15 17:23:42 -060096/** \brief a function that completes a previously started secure element MAC
97 * operation by returning the resulting MAC.
Gilles Peskine75976892018-12-12 15:55:09 +010098 *
99 * \param[in,out] p_context A hardware-specific structure for the
100 * previously started MAC operation to be
101 * finished
102 * \param[out] p_mac A buffer where the generated MAC will be
103 * placed
104 * \param[in] mac_size The size in bytes of the buffer that has been
105 * allocated for the `output` buffer
106 * \param[out] p_mac_length After completion, will contain the number of
107 * bytes placed in the `p_mac` buffer
108 *
109 * \retval PSA_SUCCESS
110 * Success.
111 */
Derek Miller83d26622019-02-15 16:41:22 -0600112typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *p_context,
113 uint8_t *p_mac,
114 size_t mac_size,
115 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100116
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600117/** \brief A function that completes a previously started secure element MAC
118 * operation by comparing the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100119 *
120 * \param[in,out] p_context A hardware-specific structure for the previously
121 * started MAC operation to be fiinished
122 * \param[in] p_mac The MAC value against which the resulting MAC will
123 * be compared against
124 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
125 *
126 * \retval PSA_SUCCESS
127 * The operation completed successfully and the MACs matched each
128 * other
129 * \retval PSA_ERROR_INVALID_SIGNATURE
130 * The operation completed successfully, but the calculated MAC did
131 * not match the provided MAC
132 */
Derek Miller83d26622019-02-15 16:41:22 -0600133typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *p_context,
134 const uint8_t *p_mac,
135 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100136
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600137/** \brief A function that aborts a previous started secure element MAC
138 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100139
140 * \param[in,out] p_context A hardware-specific structure for the previously
141 * started MAC operation to be aborted
142 */
Derek Miller83d26622019-02-15 16:41:22 -0600143typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100144
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600145/** \brief A function that performs a secure element MAC operation in one
146 * command and returns the calculated MAC
Gilles Peskine75976892018-12-12 15:55:09 +0100147 *
148 * \param[in] p_input A buffer containing the message to be MACed
149 * \param[in] input_length The size in bytes of `p_input`
150 * \param[in] key_slot The slot of the key to be used
151 * \param[in] alg The algorithm to be used to underlie the MAC
152 * operation
153 * \param[out] p_mac A buffer where the generated MAC will be
154 * placed
155 * \param[in] mac_size The size in bytes of the `p_mac` buffer
156 * \param[out] p_mac_length After completion, will contain the number of
157 * bytes placed in the `output` buffer
158 *
159 * \retval PSA_SUCCESS
160 * Success.
161 */
Derek Miller83d26622019-02-15 16:41:22 -0600162typedef psa_status_t (*psa_drv_se_mac_generate_t)(const uint8_t *p_input,
163 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600164 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600165 psa_algorithm_t alg,
166 uint8_t *p_mac,
167 size_t mac_size,
168 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100169
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600170/** \brief A function that performs a secure element MAC operation in one
171 * command and compares the resulting MAC against a provided value
Gilles Peskine75976892018-12-12 15:55:09 +0100172 *
173 * \param[in] p_input A buffer containing the message to be MACed
174 * \param[in] input_length The size in bytes of `input`
175 * \param[in] key_slot The slot of the key to be used
176 * \param[in] alg The algorithm to be used to underlie the MAC
177 * operation
178 * \param[in] p_mac The MAC value against which the resulting MAC will
179 * be compared against
180 * \param[in] mac_length The size in bytes of `mac`
181 *
182 * \retval PSA_SUCCESS
183 * The operation completed successfully and the MACs matched each
184 * other
185 * \retval PSA_ERROR_INVALID_SIGNATURE
186 * The operation completed successfully, but the calculated MAC did
187 * not match the provided MAC
188 */
Derek Miller83d26622019-02-15 16:41:22 -0600189typedef psa_status_t (*psa_drv_se_mac_verify_t)(const uint8_t *p_input,
190 size_t input_length,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600191 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600192 psa_algorithm_t alg,
193 const uint8_t *p_mac,
194 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100195
196/** \brief A struct containing all of the function pointers needed to
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600197 * perform secure element MAC operations
Gilles Peskine75976892018-12-12 15:55:09 +0100198 *
199 * PSA Crypto API implementations should populate the table as appropriate
200 * upon startup.
201 *
202 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600203 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100204 *
205 * Driver implementers should ensure that they implement all of the functions
206 * that make sense for their hardware, and that they provide a full solution
207 * (for example, if they support `p_setup`, they should also support
208 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
209 *
210 */
211typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600212 /**The size in bytes of the hardware-specific secure element MAC context
213 * structure
Gilles Peskine75976892018-12-12 15:55:09 +0100214 */
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600215 size_t context_size;
216 /** Function that performs a MAC setup operation
Gilles Peskine75976892018-12-12 15:55:09 +0100217 */
Derek Millerea743cf2019-02-15 17:06:29 -0600218 psa_drv_se_mac_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600219 /** Function that performs a MAC update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100220 */
Derek Millerea743cf2019-02-15 17:06:29 -0600221 psa_drv_se_mac_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600222 /** Function that completes a MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100223 */
Derek Millerea743cf2019-02-15 17:06:29 -0600224 psa_drv_se_mac_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600225 /** Function that completes a MAC operation with a verify check
Gilles Peskine75976892018-12-12 15:55:09 +0100226 */
Derek Millerea743cf2019-02-15 17:06:29 -0600227 psa_drv_se_mac_finish_verify_t p_finish_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600228 /** Function that aborts a previoustly started MAC operation
Gilles Peskine75976892018-12-12 15:55:09 +0100229 */
Derek Millerea743cf2019-02-15 17:06:29 -0600230 psa_drv_se_mac_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600231 /** Function that performs a MAC operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100232 */
Derek Millerea743cf2019-02-15 17:06:29 -0600233 psa_drv_se_mac_generate_t p_mac;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600234 /** Function that performs a MAC and verify operation in one call
Gilles Peskine75976892018-12-12 15:55:09 +0100235 */
Derek Millerea743cf2019-02-15 17:06:29 -0600236 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600237} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100238/**@}*/
239
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600240/** \defgroup se_cipher Secure Element Symmetric Ciphers
Gilles Peskine75976892018-12-12 15:55:09 +0100241 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600242 * Encryption and Decryption using secure element keys in block modes other
243 * than ECB must be done in multiple parts, using the following flow:
244 * - `psa_drv_se_cipher_setup_t`
245 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
246 * - `psa_drv_se_cipher_update_t`
247 * - `psa_drv_se_cipher_update_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100248 * - ...
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600249 * - `psa_drv_se_cipher_finish_t`
Gilles Peskine75976892018-12-12 15:55:09 +0100250
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600251 * If a previously started secure element Cipher operation needs to be
252 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
253 * to do so may result in allocated resources not being freed or in other
254 * undefined behavior.
Gilles Peskine75976892018-12-12 15:55:09 +0100255 *
256 * In situations where a PSA Cryptographic API implementation is using a block
257 * mode not-supported by the underlying hardware or driver, it can construct
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600258 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
259 * for the cipher operations.
Gilles Peskine75976892018-12-12 15:55:09 +0100260 */
261/**@{*/
262
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600263/** \brief A function that provides the cipher setup function for a
264 * secure element driver
Gilles Peskine75976892018-12-12 15:55:09 +0100265 *
266 * \param[in,out] p_context A structure that will contain the
267 * hardware-specific cipher context.
268 * \param[in] key_slot The slot of the key to be used for the
269 * operation
270 * \param[in] algorithm The algorithm to be used in the cipher
271 * operation
272 * \param[in] direction Indicates whether the operation is an encrypt
273 * or decrypt
274 *
275 * \retval PSA_SUCCESS
276 * \retval PSA_ERROR_NOT_SUPPORTED
277 */
Derek Miller83d26622019-02-15 16:41:22 -0600278typedef psa_status_t (*psa_drv_se_cipher_setup_t)(void *p_context,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600279 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600280 psa_algorithm_t algorithm,
281 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100282
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600283/** \brief A function that sets the initialization vector (if
284 * necessary) for an secure element cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100285 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600286 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
287 * two IV functions: one to set the IV, and one to generate it internally. The
Gilles Peskine75976892018-12-12 15:55:09 +0100288 * generate function is not necessary for the drivers to implement as the PSA
289 * Crypto implementation can do the generation using its RNG features.
290 *
291 * \param[in,out] p_context A structure that contains the previously set up
292 * hardware-specific cipher context
293 * \param[in] p_iv A buffer containing the initialization vector
294 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
295 *
296 * \retval PSA_SUCCESS
297 */
Derek Miller83d26622019-02-15 16:41:22 -0600298typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *p_context,
299 const uint8_t *p_iv,
300 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100301
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600302/** \brief A function that continues a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100303 * operation
304 *
305 * \param[in,out] p_context A hardware-specific structure for the
306 * previously started cipher operation
307 * \param[in] p_input A buffer containing the data to be
308 * encrypted/decrypted
309 * \param[in] input_size The size in bytes of the buffer pointed to
310 * by `p_input`
311 * \param[out] p_output The caller-allocated buffer where the
312 * output will be placed
313 * \param[in] output_size The allocated size in bytes of the
314 * `p_output` buffer
315 * \param[out] p_output_length After completion, will contain the number
316 * of bytes placed in the `p_output` buffer
317 *
318 * \retval PSA_SUCCESS
319 */
Derek Miller83d26622019-02-15 16:41:22 -0600320typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *p_context,
321 const uint8_t *p_input,
322 size_t input_size,
323 uint8_t *p_output,
324 size_t output_size,
325 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100326
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600327/** \brief A function that completes a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100328 * operation
329 *
330 * \param[in,out] p_context A hardware-specific structure for the
331 * previously started cipher operation
332 * \param[out] p_output The caller-allocated buffer where the output
333 * will be placed
334 * \param[in] output_size The allocated size in bytes of the `p_output`
335 * buffer
336 * \param[out] p_output_length After completion, will contain the number of
337 * bytes placed in the `p_output` buffer
338 *
339 * \retval PSA_SUCCESS
340 */
Derek Miller83d26622019-02-15 16:41:22 -0600341typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *p_context,
342 uint8_t *p_output,
343 size_t output_size,
344 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100345
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600346/** \brief A function that aborts a previously started secure element cipher
Gilles Peskine75976892018-12-12 15:55:09 +0100347 * operation
348 *
349 * \param[in,out] p_context A hardware-specific structure for the
350 * previously started cipher operation
351 */
Derek Miller83d26622019-02-15 16:41:22 -0600352typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100353
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600354/** \brief A function that performs the ECB block mode for secure element
355 * cipher operations
Gilles Peskine75976892018-12-12 15:55:09 +0100356 *
357 * Note: this function should only be used with implementations that do not
358 * provide a needed higher-level operation.
359 *
360 * \param[in] key_slot The slot of the key to be used for the operation
361 * \param[in] algorithm The algorithm to be used in the cipher operation
362 * \param[in] direction Indicates whether the operation is an encrypt or
363 * decrypt
364 * \param[in] p_input A buffer containing the data to be
365 * encrypted/decrypted
366 * \param[in] input_size The size in bytes of the buffer pointed to by
367 * `p_input`
368 * \param[out] p_output The caller-allocated buffer where the output will
369 * be placed
370 * \param[in] output_size The allocated size in bytes of the `p_output`
371 * buffer
372 *
373 * \retval PSA_SUCCESS
374 * \retval PSA_ERROR_NOT_SUPPORTED
375 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600376typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600377 psa_algorithm_t algorithm,
378 psa_encrypt_or_decrypt_t direction,
379 const uint8_t *p_input,
380 size_t input_size,
381 uint8_t *p_output,
382 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100383
384/**
385 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600386 * cipher operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100387 *
388 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600389 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100390 *
391 * If one of the functions is not implemented (such as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600392 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
Gilles Peskine75976892018-12-12 15:55:09 +0100393 */
394typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600395 /** The size in bytes of the hardware-specific secure element cipher
396 * context structure
Gilles Peskine75976892018-12-12 15:55:09 +0100397 */
Derek Miller34b33f12019-02-15 17:13:54 -0600398 size_t context_size;
399 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600400 psa_drv_se_cipher_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600401 /** Function that sets a cipher IV (if necessary) */
Derek Millerea743cf2019-02-15 17:06:29 -0600402 psa_drv_se_cipher_set_iv_t p_set_iv;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600403 /** Function that performs a cipher update operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600404 psa_drv_se_cipher_update_t p_update;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600405 /** Function that completes a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600406 psa_drv_se_cipher_finish_t p_finish;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600407 /** Function that aborts a cipher operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600408 psa_drv_se_cipher_abort_t p_abort;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600409 /** Function that performs ECB mode for a cipher operation
Gilles Peskine75976892018-12-12 15:55:09 +0100410 * (Danger: ECB mode should not be used directly by clients of the PSA
411 * Crypto Client API)
412 */
Derek Millerea743cf2019-02-15 17:06:29 -0600413 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600414} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100415
416/**@}*/
417
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600418/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
Gilles Peskine75976892018-12-12 15:55:09 +0100419 *
420 * Since the amount of data that can (or should) be encrypted or signed using
421 * asymmetric keys is limited by the key size, asymmetric key operations using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600422 * keys in a secure element must be done in single function calls.
Gilles Peskine75976892018-12-12 15:55:09 +0100423 */
424/**@{*/
425
426/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600427 * \brief A function that signs a hash or short message with a private key in
428 * a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100429 *
430 * \param[in] key_slot Key slot of an asymmetric key pair
431 * \param[in] alg A signature algorithm that is compatible
432 * with the type of `key`
433 * \param[in] p_hash The hash to sign
434 * \param[in] hash_length Size of the `p_hash` buffer in bytes
435 * \param[out] p_signature Buffer where the signature is to be written
436 * \param[in] signature_size Size of the `p_signature` buffer in bytes
437 * \param[out] p_signature_length On success, the number of bytes
438 * that make up the returned signature value
439 *
440 * \retval PSA_SUCCESS
441 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600442typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600443 psa_algorithm_t alg,
444 const uint8_t *p_hash,
445 size_t hash_length,
446 uint8_t *p_signature,
447 size_t signature_size,
448 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100449
450/**
451 * \brief A function that verifies the signature a hash or short message using
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600452 * an asymmetric public key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100453 *
454 * \param[in] key_slot Key slot of a public key or an asymmetric key
455 * pair
456 * \param[in] alg A signature algorithm that is compatible with
457 * the type of `key`
458 * \param[in] p_hash The hash whose signature is to be verified
459 * \param[in] hash_length Size of the `p_hash` buffer in bytes
460 * \param[in] p_signature Buffer containing the signature to verify
461 * \param[in] signature_length Size of the `p_signature` buffer in bytes
462 *
463 * \retval PSA_SUCCESS
464 * The signature is valid.
465 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600466typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600467 psa_algorithm_t alg,
468 const uint8_t *p_hash,
469 size_t hash_length,
470 const uint8_t *p_signature,
471 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100472
473/**
474 * \brief A function that encrypts a short message with an asymmetric public
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600475 * key in a secure element
Gilles Peskine75976892018-12-12 15:55:09 +0100476 *
477 * \param[in] key_slot Key slot of a public key or an asymmetric key
478 * pair
479 * \param[in] alg An asymmetric encryption algorithm that is
480 * compatible with the type of `key`
481 * \param[in] p_input The message to encrypt
482 * \param[in] input_length Size of the `p_input` buffer in bytes
483 * \param[in] p_salt A salt or label, if supported by the
484 * encryption algorithm
485 * If the algorithm does not support a
486 * salt, pass `NULL`.
487 * If the algorithm supports an optional
488 * salt and you do not want to pass a salt,
489 * pass `NULL`.
490 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
491 * supported.
492 * \param[in] salt_length Size of the `p_salt` buffer in bytes
493 * If `p_salt` is `NULL`, pass 0.
494 * \param[out] p_output Buffer where the encrypted message is to
495 * be written
496 * \param[in] output_size Size of the `p_output` buffer in bytes
497 * \param[out] p_output_length On success, the number of bytes that make up
498 * the returned output
499 *
500 * \retval PSA_SUCCESS
501 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600502typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600503 psa_algorithm_t alg,
504 const uint8_t *p_input,
505 size_t input_length,
506 const uint8_t *p_salt,
507 size_t salt_length,
508 uint8_t *p_output,
509 size_t output_size,
510 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100511
512/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600513 * \brief A function that decrypts a short message with an asymmetric private
514 * key in a secure element.
Gilles Peskine75976892018-12-12 15:55:09 +0100515 *
516 * \param[in] key_slot Key slot of an asymmetric key pair
517 * \param[in] alg An asymmetric encryption algorithm that is
518 * compatible with the type of `key`
519 * \param[in] p_input The message to decrypt
520 * \param[in] input_length Size of the `p_input` buffer in bytes
521 * \param[in] p_salt A salt or label, if supported by the
522 * encryption algorithm
523 * If the algorithm does not support a
524 * salt, pass `NULL`.
525 * If the algorithm supports an optional
526 * salt and you do not want to pass a salt,
527 * pass `NULL`.
528 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
529 * supported.
530 * \param[in] salt_length Size of the `p_salt` buffer in bytes
531 * If `p_salt` is `NULL`, pass 0.
532 * \param[out] p_output Buffer where the decrypted message is to
533 * be written
534 * \param[in] output_size Size of the `p_output` buffer in bytes
535 * \param[out] p_output_length On success, the number of bytes
536 * that make up the returned output
537 *
538 * \retval PSA_SUCCESS
539 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600540typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600541 psa_algorithm_t alg,
542 const uint8_t *p_input,
543 size_t input_length,
544 const uint8_t *p_salt,
545 size_t salt_length,
546 uint8_t *p_output,
547 size_t output_size,
548 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100549
550/**
551 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600552 * asymmetric cryptographic operations using secure elements.
Gilles Peskine75976892018-12-12 15:55:09 +0100553 *
554 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600555 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100556 *
557 * If one of the functions is not implemented, it should be set to NULL.
558 */
559typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600560 /** Function that performs an asymmetric sign operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600561 psa_drv_se_asymmetric_sign_t p_sign;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600562 /** Function that performs an asymmetric verify operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600563 psa_drv_se_asymmetric_verify_t p_verify;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600564 /** Function that performs an asymmetric encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600565 psa_drv_se_asymmetric_encrypt_t p_encrypt;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600566 /** Function that performs an asymmetric decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600567 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600568} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100569
570/**@}*/
571
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600572/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
573 * Authenticated Encryption with Additional Data (AEAD) operations with secure
574 * elements must be done in one function call. While this creates a burden for
Gilles Peskine75976892018-12-12 15:55:09 +0100575 * implementers as there must be sufficient space in memory for the entire
576 * message, it prevents decrypted data from being made available before the
577 * authentication operation is complete and the data is known to be authentic.
578 */
579/**@{*/
580
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600581/** \brief A function that performs a secure element authenticated encryption
582 * operation
Gilles Peskine75976892018-12-12 15:55:09 +0100583 *
584 * \param[in] key_slot Slot containing the key to use.
585 * \param[in] algorithm The AEAD algorithm to compute
586 * (\c PSA_ALG_XXX value such that
587 * #PSA_ALG_IS_AEAD(`alg`) is true)
588 * \param[in] p_nonce Nonce or IV to use
589 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
590 * \param[in] p_additional_data Additional data that will be
591 * authenticated but not encrypted
592 * \param[in] additional_data_length Size of `p_additional_data` in bytes
593 * \param[in] p_plaintext Data that will be authenticated and
594 * encrypted
595 * \param[in] plaintext_length Size of `p_plaintext` in bytes
596 * \param[out] p_ciphertext Output buffer for the authenticated and
597 * encrypted data. The additional data is
598 * not part of this output. For algorithms
599 * where the encrypted data and the
600 * authentication tag are defined as
601 * separate outputs, the authentication
602 * tag is appended to the encrypted data.
603 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
604 * bytes
605 * \param[out] p_ciphertext_length On success, the size of the output in
606 * the `p_ciphertext` buffer
607 *
608 * \retval #PSA_SUCCESS
609 * Success.
610 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600611typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600612 psa_algorithm_t algorithm,
613 const uint8_t *p_nonce,
614 size_t nonce_length,
615 const uint8_t *p_additional_data,
616 size_t additional_data_length,
617 const uint8_t *p_plaintext,
618 size_t plaintext_length,
619 uint8_t *p_ciphertext,
620 size_t ciphertext_size,
621 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100622
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600623/** A function that peforms a secure element authenticated decryption operation
Gilles Peskine75976892018-12-12 15:55:09 +0100624 *
625 * \param[in] key_slot Slot containing the key to use
626 * \param[in] algorithm The AEAD algorithm to compute
627 * (\c PSA_ALG_XXX value such that
628 * #PSA_ALG_IS_AEAD(`alg`) is true)
629 * \param[in] p_nonce Nonce or IV to use
630 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
631 * \param[in] p_additional_data Additional data that has been
632 * authenticated but not encrypted
633 * \param[in] additional_data_length Size of `p_additional_data` in bytes
634 * \param[in] p_ciphertext Data that has been authenticated and
635 * encrypted.
636 * For algorithms where the encrypted data
637 * and the authentication tag are defined
638 * as separate inputs, the buffer must
639 * contain the encrypted data followed by
640 * the authentication tag.
641 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
642 * \param[out] p_plaintext Output buffer for the decrypted data
643 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
644 * bytes
645 * \param[out] p_plaintext_length On success, the size of the output in
646 * the `p_plaintext` buffer
647 *
648 * \retval #PSA_SUCCESS
649 * Success.
650 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600651typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600652 psa_algorithm_t algorithm,
653 const uint8_t *p_nonce,
654 size_t nonce_length,
655 const uint8_t *p_additional_data,
656 size_t additional_data_length,
657 const uint8_t *p_ciphertext,
658 size_t ciphertext_length,
659 uint8_t *p_plaintext,
660 size_t plaintext_size,
661 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100662
663/**
664 * \brief A struct containing all of the function pointers needed to implement
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600665 * secure element Authenticated Encryption with Additional Data operations
Gilles Peskine75976892018-12-12 15:55:09 +0100666 *
667 * PSA Crypto API implementations should populate instances of the table as
668 * appropriate upon startup.
669 *
670 * If one of the functions is not implemented, it should be set to NULL.
671 */
672typedef struct {
673 /** Function that performs the AEAD encrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600674 psa_drv_se_aead_encrypt_t p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100675 /** Function that performs the AEAD decrypt operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600676 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600677} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100678/**@}*/
679
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600680/** \defgroup se_key_management Secure Element Key Management
Gilles Peskine75976892018-12-12 15:55:09 +0100681 * Currently, key management is limited to importing keys in the clear,
682 * destroying keys, and exporting keys in the clear.
683 * Whether a key may be exported is determined by the key policies in place
684 * on the key slot.
685 */
686/**@{*/
687
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600688/** \brief A function that imports a key into a secure element in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100689 *
690 * This function can support any output from psa_export_key(). Refer to the
691 * documentation of psa_export_key() for the format for each key type.
692 *
693 * \param[in] key_slot Slot where the key will be stored
694 * This must be a valid slot for a key of the chosen
695 * type. It must be unoccupied.
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600696 * \param[in] lifetime The required lifetime of the key storage
Gilles Peskine75976892018-12-12 15:55:09 +0100697 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
698 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
699 * \param[in] usage The allowed uses of the key
700 * \param[in] p_data Buffer containing the key data
701 * \param[in] data_length Size of the `data` buffer in bytes
702 *
703 * \retval #PSA_SUCCESS
704 * Success.
705 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600706typedef psa_status_t (*psa_drv_se_import_key_t)(psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600707 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600708 psa_key_type_t type,
709 psa_algorithm_t algorithm,
710 psa_key_usage_t usage,
711 const uint8_t *p_data,
712 size_t data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100713
714/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600715 * \brief A function that destroys a secure element key and restore the slot to
716 * its default state
Gilles Peskine75976892018-12-12 15:55:09 +0100717 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600718 * This function destroys the content of the key from a secure element.
719 * Implementations shall make a best effort to ensure that any previous content
720 * of the slot is unrecoverable.
Gilles Peskine75976892018-12-12 15:55:09 +0100721 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600722 * This function returns the specified slot to its default state.
Gilles Peskine75976892018-12-12 15:55:09 +0100723 *
724 * \param[in] key_slot The key slot to erase.
725 *
726 * \retval #PSA_SUCCESS
727 * The slot's content, if any, has been erased.
728 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600729typedef psa_status_t (*psa_drv_se_destroy_key_t)(psa_key_slot_number_t key);
Gilles Peskine75976892018-12-12 15:55:09 +0100730
731/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600732 * \brief A function that exports a secure element key in binary format
Gilles Peskine75976892018-12-12 15:55:09 +0100733 *
734 * The output of this function can be passed to psa_import_key() to
735 * create an equivalent object.
736 *
737 * If a key is created with `psa_import_key()` and then exported with
738 * this function, it is not guaranteed that the resulting data is
739 * identical: the implementation may choose a different representation
740 * of the same key if the format permits it.
741 *
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600742 * This function should generate output in the same format that
743 * `psa_export_key()` does. Refer to the
744 * documentation of `psa_export_key()` for the format for each key type.
Gilles Peskine75976892018-12-12 15:55:09 +0100745 *
746 * \param[in] key Slot whose content is to be exported. This must
747 * be an occupied key slot.
748 * \param[out] p_data Buffer where the key data is to be written.
749 * \param[in] data_size Size of the `p_data` buffer in bytes.
750 * \param[out] p_data_length On success, the number of bytes
751 * that make up the key data.
752 *
753 * \retval #PSA_SUCCESS
754 * \retval #PSA_ERROR_EMPTY_SLOT
755 * \retval #PSA_ERROR_NOT_PERMITTED
756 * \retval #PSA_ERROR_NOT_SUPPORTED
757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
758 * \retval #PSA_ERROR_HARDWARE_FAILURE
759 * \retval #PSA_ERROR_TAMPERING_DETECTED
760 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600761typedef psa_status_t (*psa_drv_se_export_key_t)(psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600762 uint8_t *p_data,
763 size_t data_size,
764 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100765
766/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600767 * \brief A function that generates a symmetric or asymmetric key on a secure
768 * element
769 *
770 * If `type` is asymmetric (`#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) == 1`),
771 * the public component of the generated key will be placed in `p_pubkey_out`.
772 * The format of the public key information will match the format specified for
773 * the `psa_export_key()` function for the key type.
774 *
775 * \param[in] key_slot Slot where the generated key will be placed
776 * \param[in] type The type of the key to be generated
777 * \param[in] usage The prescribed usage of the generated key
778 * Note: Not all Secure Elements support the same
779 * restrictions that PSA Crypto does (and vice versa).
780 * Driver developers should endeavor to match the
781 * usages as close as possible.
782 * \param[in] bits The size in bits of the key to be generated.
783 * \param[in] extra Extra parameters for key generation. The
784 * interpretation of this parameter should match the
785 * interpretation in the `extra` parameter is the
786 * `psa_generate_key` function
787 * \param[in] extra_size The size in bytes of the \ref extra buffer
788 * \param[out] p_pubkey_out The buffer where the public key information will
789 * be placed
790 * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer
791 * \param[out] p_pubkey_length Upon successful completion, will contain the
792 * size of the data placed in `p_pubkey_out`.
Gilles Peskine75976892018-12-12 15:55:09 +0100793 */
Derek Miller0b3098a2019-02-15 17:10:49 -0600794typedef psa_status_t (*psa_drv_se_generate_key_t) (psa_key_slot_number_t key_slot,
795 psa_key_type_t type,
796 psa_key_usage_t usage,
797 size_t bits,
798 const void *extra,
799 size_t extra_size,
800 uint8_t *p_pubkey_out,
801 size_t pubkey_out_size,
802 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100803
804/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600805 * \brief A struct containing all of the function pointers needed to for secure
806 * element key management
Gilles Peskine75976892018-12-12 15:55:09 +0100807 *
808 * PSA Crypto API implementations should populate instances of the table as
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600809 * appropriate upon startup or at build time.
Gilles Peskine75976892018-12-12 15:55:09 +0100810 *
811 * If one of the functions is not implemented, it should be set to NULL.
812 */
813typedef struct {
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600814 /** Function that performs a key import operation */
815 psa_drv_se_import_key_t p_import;
816 /** Function that performs a generation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600817 psa_drv_se_generate_key_t p_generate;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600818 /** Function that performs a key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600819 psa_drv_se_destroy_key_t p_destroy;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600820 /** Function that performs a key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600821 psa_drv_se_export_key_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -0600822} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100823
824/**@}*/
825
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600826/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100827 * Key derivation is the process of generating new key material using an
828 * existing key and additional parameters, iterating through a basic
829 * cryptographic function, such as a hash.
830 * Key agreement is a part of cryptographic protocols that allows two parties
831 * to agree on the same key value, but starting from different original key
832 * material.
833 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
834 * for both of the flows.
835 *
836 * There are two different final functions for the flows,
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600837 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
838 * `psa_drv_se_key_derivation_derive` is used when the key material should be
839 * placed in a slot on the hardware and not exposed to the caller.
840 * `psa_drv_se_key_derivation_export` is used when the key material should be
841 * returned to the PSA Cryptographic API implementation.
Gilles Peskine75976892018-12-12 15:55:09 +0100842 *
843 * Different key derivation algorithms require a different number of inputs.
844 * Instead of having an API that takes as input variable length arrays, which
845 * can be problemmatic to manage on embedded platforms, the inputs are passed
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600846 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
847 * is called multiple times with different `collateral_id`s. Thus, for a key
Gilles Peskine75976892018-12-12 15:55:09 +0100848 * derivation algorithm that required 3 paramter inputs, the flow would look
849 * something like:
850 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600851 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
852 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
853 * p_collateral_0,
854 * collateral_0_size);
855 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
856 * p_collateral_1,
857 * collateral_1_size);
858 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
859 * p_collateral_2,
860 * collateral_2_size);
861 * psa_drv_se_key_derivation_derive();
Gilles Peskine75976892018-12-12 15:55:09 +0100862 * ~~~~~~~~~~~~~
863 *
864 * key agreement example:
865 * ~~~~~~~~~~~~~{.c}
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600866 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
867 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
868 * psa_drv_se_key_derivation_export(p_session_key,
869 * session_key_size,
870 * &session_key_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100871 * ~~~~~~~~~~~~~
872 */
873/**@{*/
874
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600875/** \brief A function that Sets up a secure element key derivation operation by
876 * specifying the algorithm and the source key sot
Gilles Peskine75976892018-12-12 15:55:09 +0100877 *
878 * \param[in,out] p_context A hardware-specific structure containing any
879 * context information for the implementation
880 * \param[in] kdf_alg The algorithm to be used for the key derivation
881 * \param[in] souce_key The key to be used as the source material for the
882 * key derivation
883 *
884 * \retval PSA_SUCCESS
885 */
Derek Miller62117262019-02-15 17:12:26 -0600886typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600887 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600888 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100889
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600890/** \brief A function that provides collateral (parameters) needed for a secure
891 * element key derivation or key agreement operation
Gilles Peskine75976892018-12-12 15:55:09 +0100892 *
893 * Since many key derivation algorithms require multiple parameters, it is
894 * expeced that this function may be called multiple times for the same
895 * operation, each with a different algorithm-specific `collateral_id`
896 *
897 * \param[in,out] p_context A hardware-specific structure containing any
898 * context information for the implementation
899 * \param[in] collateral_id An ID for the collateral being provided
900 * \param[in] p_collateral A buffer containing the collateral data
901 * \param[in] collateral_size The size in bytes of the collateral
902 *
903 * \retval PSA_SUCCESS
904 */
Derek Miller62117262019-02-15 17:12:26 -0600905typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600906 uint32_t collateral_id,
907 const uint8_t *p_collateral,
908 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100909
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600910/** \brief A function that performs the final secure element key derivation
911 * step and place the generated key material in a slot
912 *
Gilles Peskine75976892018-12-12 15:55:09 +0100913 * \param[in,out] p_context A hardware-specific structure containing any
914 * context information for the implementation
915 * \param[in] dest_key The slot where the generated key material
916 * should be placed
917 *
918 * \retval PSA_SUCCESS
919 */
Derek Miller62117262019-02-15 17:12:26 -0600920typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *p_context,
921 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100922
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600923/** \brief A function that performs the final step of a secure element key
924 * agreement and place the generated key material in a buffer
Gilles Peskine75976892018-12-12 15:55:09 +0100925 *
926 * \param[out] p_output Buffer in which to place the generated key
927 * material
928 * \param[in] output_size The size in bytes of `p_output`
929 * \param[out] p_output_length Upon success, contains the number of bytes of
930 * key material placed in `p_output`
931 *
932 * \retval PSA_SUCCESS
933 */
Derek Miller62117262019-02-15 17:12:26 -0600934typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *p_context,
935 uint8_t *p_output,
936 size_t output_size,
937 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100938
939/**
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600940 * \brief A struct containing all of the function pointers needed to for secure
941 * element key derivation and agreement
Gilles Peskine75976892018-12-12 15:55:09 +0100942 *
943 * PSA Crypto API implementations should populate instances of the table as
944 * appropriate upon startup.
945 *
946 * If one of the functions is not implemented, it should be set to NULL.
947 */
948typedef struct {
Derek Miller62117262019-02-15 17:12:26 -0600949 /** The driver-specific size of the key derivation context */
950 size_t context_size;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600951 /** Function that performs a key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -0600952 psa_drv_se_key_derivation_setup_t p_setup;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600953 /** Function that sets key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -0600954 psa_drv_se_key_derivation_collateral_t p_collateral;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600955 /** Function that performs a final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -0600956 psa_drv_se_key_derivation_derive_t p_derive;
Derek Millerf0c1d0d2019-02-15 17:23:42 -0600957 /** Function that perforsm a final key derivation or agreement and
Gilles Peskine75976892018-12-12 15:55:09 +0100958 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -0600959 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -0600960} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100961
962/**@}*/
963
964#ifdef __cplusplus
965}
966#endif
967
968#endif /* PSA_CRYPTO_SE_DRIVER_H */