blob: 7ca6d605a05b989d4a2830e6f2993cfd9de1322b [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
6 * drivers that access key material via opaque references. This is
7 * meant for cryptoprocessors that have a separate key storage from the
8 * space in which the PSA Crypto implementation runs, typically secure
9 * elements.
10 *
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. */
46typedef uint32_t psa_key_slot_t;
47
48/** \defgroup opaque_mac Opaque Message Authentication Code
49 * Generation and authentication of Message Authentication Codes (MACs) using
50 * opaque keys can be done either as a single function call (via the
51 * `psa_drv_mac_opaque_generate_t` or `psa_drv_mac_opaque_verify_t` functions), or in
52 * parts using the following sequence:
53 * - `psa_drv_mac_opaque_setup_t`
54 * - `psa_drv_mac_opaque_update_t`
55 * - `psa_drv_mac_opaque_update_t`
56 * - ...
57 * - `psa_drv_mac_opaque_finish_t` or `psa_drv_mac_opaque_finish_verify_t`
58 *
59 * If a previously started Opaque MAC operation needs to be terminated, it
60 * should be done so by the `psa_drv_mac_opaque_abort_t`. Failure to do so may
61 * result in allocated resources not being freed or in other undefined
62 * behavior.
63 */
64/**@{*/
65/** \brief A function that starts a MAC operation for a PSA Crypto Driver
66 * implementation using an opaque key
67 *
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,
79 psa_key_slot_t key_slot,
80 psa_algorithm_t algorithm);
Gilles Peskine75976892018-12-12 15:55:09 +010081
82/** \brief A function that continues a previously started MAC operation using
83 * an opaque key
84 *
85 * \param[in,out] p_context A hardware-specific structure for the
86 * previously-established MAC operation to be
87 * continued
88 * \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
96/** \brief a function that completes a previously started MAC operation by
97 * returning the resulting MAC using an opaque key
98 *
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
117/** \brief A function that completes a previously started MAC operation by
118 * comparing the resulting MAC against a known value using an opaque key
119 *
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
137/** \brief A function that aborts a previous started opaque-key MAC operation
138
139 * \param[in,out] p_context A hardware-specific structure for the previously
140 * started MAC operation to be aborted
141 */
Derek Miller83d26622019-02-15 16:41:22 -0600142typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100143
144/** \brief A function that performs a MAC operation in one command and returns
145 * the calculated MAC using an opaque key
146 *
147 * \param[in] p_input A buffer containing the message to be MACed
148 * \param[in] input_length The size in bytes of `p_input`
149 * \param[in] key_slot The slot of the key to be used
150 * \param[in] alg The algorithm to be used to underlie the MAC
151 * operation
152 * \param[out] p_mac A buffer where the generated MAC will be
153 * placed
154 * \param[in] mac_size The size in bytes of the `p_mac` buffer
155 * \param[out] p_mac_length After completion, will contain the number of
156 * bytes placed in the `output` buffer
157 *
158 * \retval PSA_SUCCESS
159 * Success.
160 */
Derek Miller83d26622019-02-15 16:41:22 -0600161typedef psa_status_t (*psa_drv_se_mac_generate_t)(const uint8_t *p_input,
162 size_t input_length,
163 psa_key_slot_t key_slot,
164 psa_algorithm_t alg,
165 uint8_t *p_mac,
166 size_t mac_size,
167 size_t *p_mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100168
169/** \brief A function that performs an MAC operation in one command and
170 * compare the resulting MAC against a known value using an opaque key
171 *
172 * \param[in] p_input A buffer containing the message to be MACed
173 * \param[in] input_length The size in bytes of `input`
174 * \param[in] key_slot The slot of the key to be used
175 * \param[in] alg The algorithm to be used to underlie the MAC
176 * operation
177 * \param[in] p_mac The MAC value against which the resulting MAC will
178 * be compared against
179 * \param[in] mac_length The size in bytes of `mac`
180 *
181 * \retval PSA_SUCCESS
182 * The operation completed successfully and the MACs matched each
183 * other
184 * \retval PSA_ERROR_INVALID_SIGNATURE
185 * The operation completed successfully, but the calculated MAC did
186 * not match the provided MAC
187 */
Derek Miller83d26622019-02-15 16:41:22 -0600188typedef psa_status_t (*psa_drv_se_mac_verify_t)(const uint8_t *p_input,
189 size_t input_length,
190 psa_key_slot_t key_slot,
191 psa_algorithm_t alg,
192 const uint8_t *p_mac,
193 size_t mac_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100194
195/** \brief A struct containing all of the function pointers needed to
196 * implement MAC operations using opaque keys.
197 *
198 * PSA Crypto API implementations should populate the table as appropriate
199 * upon startup.
200 *
201 * If one of the functions is not implemented (such as
202 * `psa_drv_mac_opaque_generate_t`), it should be set to NULL.
203 *
204 * Driver implementers should ensure that they implement all of the functions
205 * that make sense for their hardware, and that they provide a full solution
206 * (for example, if they support `p_setup`, they should also support
207 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
208 *
209 */
210typedef struct {
211 /**The size in bytes of the hardware-specific Opaque-MAC Context structure
212 */
213 size_t context_size;
214 /** Function that performs the setup operation
215 */
Derek Miller83d26622019-02-15 16:41:22 -0600216 psa_drv_se_mac_setup_t *p_setup;
217 /** Function that performs the update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100218 */
Derek Miller83d26622019-02-15 16:41:22 -0600219 psa_drv_se_mac_update_t *p_update;
Gilles Peskine75976892018-12-12 15:55:09 +0100220 /** Function that completes the operation
221 */
Derek Miller83d26622019-02-15 16:41:22 -0600222 psa_drv_se_mac_finish_t *p_finish;
Gilles Peskine75976892018-12-12 15:55:09 +0100223 /** Function that completed a MAC operation with a verify check
224 */
Derek Miller83d26622019-02-15 16:41:22 -0600225 psa_drv_se_mac_finish_verify_t *p_finish_verify;
Gilles Peskine75976892018-12-12 15:55:09 +0100226 /** Function that aborts a previoustly started operation
227 */
Derek Miller83d26622019-02-15 16:41:22 -0600228 psa_drv_se_mac_abort_t *p_abort;
Gilles Peskine75976892018-12-12 15:55:09 +0100229 /** Function that performs the MAC operation in one call
230 */
Derek Miller83d26622019-02-15 16:41:22 -0600231 psa_drv_se_mac_generate_t *p_mac;
Gilles Peskine75976892018-12-12 15:55:09 +0100232 /** Function that performs the MAC and verify operation in one call
233 */
Derek Miller83d26622019-02-15 16:41:22 -0600234 psa_drv_se_mac_verify_t *p_mac_verify;
235} psa_drv_se_mac_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100236/**@}*/
237
238/** \defgroup opaque_cipher Opaque Symmetric Ciphers
239 *
240 * Encryption and Decryption using opaque keys in block modes other than ECB
241 * must be done in multiple parts, using the following flow:
242 * - `psa_drv_cipher_opaque_setup_t`
243 * - `psa_drv_cipher_opaque_set_iv_t` (optional depending upon block mode)
244 * - `psa_drv_cipher_opaque_update_t`
245 * - ...
246 * - `psa_drv_cipher_opaque_finish_t`
247
248 * If a previously started Opaque Cipher operation needs to be terminated, it
249 * should be done so by the `psa_drv_cipher_opaque_abort_t`. Failure to do so may
250 * result in allocated resources not being freed or in other undefined
251 * behavior.
252 *
253 * In situations where a PSA Cryptographic API implementation is using a block
254 * mode not-supported by the underlying hardware or driver, it can construct
255 * the block mode itself, while calling the `psa_drv_cipher_opaque_ecb_t` function
256 * pointer for the cipher operations.
257 */
258/**@{*/
259
260/** \brief A function pointer that provides the cipher setup function for
261 * opaque-key operations
262 *
263 * \param[in,out] p_context A structure that will contain the
264 * hardware-specific cipher context.
265 * \param[in] key_slot The slot of the key to be used for the
266 * operation
267 * \param[in] algorithm The algorithm to be used in the cipher
268 * operation
269 * \param[in] direction Indicates whether the operation is an encrypt
270 * or decrypt
271 *
272 * \retval PSA_SUCCESS
273 * \retval PSA_ERROR_NOT_SUPPORTED
274 */
Derek Miller83d26622019-02-15 16:41:22 -0600275typedef psa_status_t (*psa_drv_se_cipher_setup_t)(void *p_context,
276 psa_key_slot_t key_slot,
277 psa_algorithm_t algorithm,
278 psa_encrypt_or_decrypt_t direction);
Gilles Peskine75976892018-12-12 15:55:09 +0100279
280/** \brief A function pointer that sets the initialization vector (if
281 * necessary) for an opaque cipher operation
282 *
283 * Rationale: The `psa_cipher_*` function in the PSA Cryptographic API has two
284 * IV functions: one to set the IV, and one to generate it internally. The
285 * generate function is not necessary for the drivers to implement as the PSA
286 * Crypto implementation can do the generation using its RNG features.
287 *
288 * \param[in,out] p_context A structure that contains the previously set up
289 * hardware-specific cipher context
290 * \param[in] p_iv A buffer containing the initialization vector
291 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
292 *
293 * \retval PSA_SUCCESS
294 */
Derek Miller83d26622019-02-15 16:41:22 -0600295typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *p_context,
296 const uint8_t *p_iv,
297 size_t iv_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100298
299/** \brief A function that continues a previously started opaque-key cipher
300 * operation
301 *
302 * \param[in,out] p_context A hardware-specific structure for the
303 * previously started cipher operation
304 * \param[in] p_input A buffer containing the data to be
305 * encrypted/decrypted
306 * \param[in] input_size The size in bytes of the buffer pointed to
307 * by `p_input`
308 * \param[out] p_output The caller-allocated buffer where the
309 * output will be placed
310 * \param[in] output_size The allocated size in bytes of the
311 * `p_output` buffer
312 * \param[out] p_output_length After completion, will contain the number
313 * of bytes placed in the `p_output` buffer
314 *
315 * \retval PSA_SUCCESS
316 */
Derek Miller83d26622019-02-15 16:41:22 -0600317typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *p_context,
318 const uint8_t *p_input,
319 size_t input_size,
320 uint8_t *p_output,
321 size_t output_size,
322 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100323
324/** \brief A function that completes a previously started opaque-key cipher
325 * operation
326 *
327 * \param[in,out] p_context A hardware-specific structure for the
328 * previously started cipher operation
329 * \param[out] p_output The caller-allocated buffer where the output
330 * will be placed
331 * \param[in] output_size The allocated size in bytes of the `p_output`
332 * buffer
333 * \param[out] p_output_length After completion, will contain the number of
334 * bytes placed in the `p_output` buffer
335 *
336 * \retval PSA_SUCCESS
337 */
Derek Miller83d26622019-02-15 16:41:22 -0600338typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *p_context,
339 uint8_t *p_output,
340 size_t output_size,
341 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100342
343/** \brief A function that aborts a previously started opaque-key cipher
344 * operation
345 *
346 * \param[in,out] p_context A hardware-specific structure for the
347 * previously started cipher operation
348 */
Derek Miller83d26622019-02-15 16:41:22 -0600349typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *p_context);
Gilles Peskine75976892018-12-12 15:55:09 +0100350
351/** \brief A function that performs the ECB block mode for opaque-key cipher
352 * operations
353 *
354 * Note: this function should only be used with implementations that do not
355 * provide a needed higher-level operation.
356 *
357 * \param[in] key_slot The slot of the key to be used for the operation
358 * \param[in] algorithm The algorithm to be used in the cipher operation
359 * \param[in] direction Indicates whether the operation is an encrypt or
360 * decrypt
361 * \param[in] p_input A buffer containing the data to be
362 * encrypted/decrypted
363 * \param[in] input_size The size in bytes of the buffer pointed to by
364 * `p_input`
365 * \param[out] p_output The caller-allocated buffer where the output will
366 * be placed
367 * \param[in] output_size The allocated size in bytes of the `p_output`
368 * buffer
369 *
370 * \retval PSA_SUCCESS
371 * \retval PSA_ERROR_NOT_SUPPORTED
372 */
Derek Miller83d26622019-02-15 16:41:22 -0600373typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_key_slot_t key_slot,
374 psa_algorithm_t algorithm,
375 psa_encrypt_or_decrypt_t direction,
376 const uint8_t *p_input,
377 size_t input_size,
378 uint8_t *p_output,
379 size_t output_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100380
381/**
382 * \brief A struct containing all of the function pointers needed to implement
383 * cipher operations using opaque keys.
384 *
385 * PSA Crypto API implementations should populate instances of the table as
386 * appropriate upon startup.
387 *
388 * If one of the functions is not implemented (such as
389 * `psa_drv_cipher_opaque_ecb_t`), it should be set to NULL.
390 */
391typedef struct {
392 /** The size in bytes of the hardware-specific Opaque Cipher context
393 * structure
394 */
Derek Miller83d26622019-02-15 16:41:22 -0600395 size_t size;
Gilles Peskine75976892018-12-12 15:55:09 +0100396 /** Function that performs the setup operation */
Derek Miller83d26622019-02-15 16:41:22 -0600397 psa_drv_se_cipher_setup_t *p_setup;
Gilles Peskine75976892018-12-12 15:55:09 +0100398 /** Function that sets the IV (if necessary) */
Derek Miller83d26622019-02-15 16:41:22 -0600399 psa_drv_se_cipher_set_iv_t *p_set_iv;
Gilles Peskine75976892018-12-12 15:55:09 +0100400 /** Function that performs the update operation */
Derek Miller83d26622019-02-15 16:41:22 -0600401 psa_drv_se_cipher_update_t *p_update;
Gilles Peskine75976892018-12-12 15:55:09 +0100402 /** Function that completes the operation */
Derek Miller83d26622019-02-15 16:41:22 -0600403 psa_drv_se_cipher_finish_t *p_finish;
Gilles Peskine75976892018-12-12 15:55:09 +0100404 /** Function that aborts the operation */
Derek Miller83d26622019-02-15 16:41:22 -0600405 psa_drv_se_cipher_abort_t *p_abort;
Gilles Peskine75976892018-12-12 15:55:09 +0100406 /** Function that performs ECB mode for the cipher
407 * (Danger: ECB mode should not be used directly by clients of the PSA
408 * Crypto Client API)
409 */
Derek Miller83d26622019-02-15 16:41:22 -0600410 psa_drv_se_cipher_ecb_t *p_ecb;
411} psa_drv_se_cipher_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100412
413/**@}*/
414
415/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
416 *
417 * Since the amount of data that can (or should) be encrypted or signed using
418 * asymmetric keys is limited by the key size, asymmetric key operations using
419 * opaque keys must be done in single function calls.
420 */
421/**@{*/
422
423/**
424 * \brief A function that signs a hash or short message with a private key
425 *
426 * \param[in] key_slot Key slot of an asymmetric key pair
427 * \param[in] alg A signature algorithm that is compatible
428 * with the type of `key`
429 * \param[in] p_hash The hash to sign
430 * \param[in] hash_length Size of the `p_hash` buffer in bytes
431 * \param[out] p_signature Buffer where the signature is to be written
432 * \param[in] signature_size Size of the `p_signature` buffer in bytes
433 * \param[out] p_signature_length On success, the number of bytes
434 * that make up the returned signature value
435 *
436 * \retval PSA_SUCCESS
437 */
Derek Miller83d26622019-02-15 16:41:22 -0600438typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_key_slot_t key_slot,
439 psa_algorithm_t alg,
440 const uint8_t *p_hash,
441 size_t hash_length,
442 uint8_t *p_signature,
443 size_t signature_size,
444 size_t *p_signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100445
446/**
447 * \brief A function that verifies the signature a hash or short message using
448 * an asymmetric public key
449 *
450 * \param[in] key_slot Key slot of a public key or an asymmetric key
451 * pair
452 * \param[in] alg A signature algorithm that is compatible with
453 * the type of `key`
454 * \param[in] p_hash The hash whose signature is to be verified
455 * \param[in] hash_length Size of the `p_hash` buffer in bytes
456 * \param[in] p_signature Buffer containing the signature to verify
457 * \param[in] signature_length Size of the `p_signature` buffer in bytes
458 *
459 * \retval PSA_SUCCESS
460 * The signature is valid.
461 */
Derek Miller83d26622019-02-15 16:41:22 -0600462typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_key_slot_t key_slot,
463 psa_algorithm_t alg,
464 const uint8_t *p_hash,
465 size_t hash_length,
466 const uint8_t *p_signature,
467 size_t signature_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100468
469/**
470 * \brief A function that encrypts a short message with an asymmetric public
471 * key
472 *
473 * \param[in] key_slot Key slot of a public key or an asymmetric key
474 * pair
475 * \param[in] alg An asymmetric encryption algorithm that is
476 * compatible with the type of `key`
477 * \param[in] p_input The message to encrypt
478 * \param[in] input_length Size of the `p_input` buffer in bytes
479 * \param[in] p_salt A salt or label, if supported by the
480 * encryption algorithm
481 * If the algorithm does not support a
482 * salt, pass `NULL`.
483 * If the algorithm supports an optional
484 * salt and you do not want to pass a salt,
485 * pass `NULL`.
486 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
487 * supported.
488 * \param[in] salt_length Size of the `p_salt` buffer in bytes
489 * If `p_salt` is `NULL`, pass 0.
490 * \param[out] p_output Buffer where the encrypted message is to
491 * be written
492 * \param[in] output_size Size of the `p_output` buffer in bytes
493 * \param[out] p_output_length On success, the number of bytes that make up
494 * the returned output
495 *
496 * \retval PSA_SUCCESS
497 */
Derek Miller83d26622019-02-15 16:41:22 -0600498typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_key_slot_t key_slot,
499 psa_algorithm_t alg,
500 const uint8_t *p_input,
501 size_t input_length,
502 const uint8_t *p_salt,
503 size_t salt_length,
504 uint8_t *p_output,
505 size_t output_size,
506 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100507
508/**
509 * \brief Decrypt a short message with an asymmetric private key.
510 *
511 * \param[in] key_slot Key slot of an asymmetric key pair
512 * \param[in] alg An asymmetric encryption algorithm that is
513 * compatible with the type of `key`
514 * \param[in] p_input The message to decrypt
515 * \param[in] input_length Size of the `p_input` buffer in bytes
516 * \param[in] p_salt A salt or label, if supported by the
517 * encryption algorithm
518 * If the algorithm does not support a
519 * salt, pass `NULL`.
520 * If the algorithm supports an optional
521 * salt and you do not want to pass a salt,
522 * pass `NULL`.
523 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
524 * supported.
525 * \param[in] salt_length Size of the `p_salt` buffer in bytes
526 * If `p_salt` is `NULL`, pass 0.
527 * \param[out] p_output Buffer where the decrypted message is to
528 * be written
529 * \param[in] output_size Size of the `p_output` buffer in bytes
530 * \param[out] p_output_length On success, the number of bytes
531 * that make up the returned output
532 *
533 * \retval PSA_SUCCESS
534 */
Derek Miller83d26622019-02-15 16:41:22 -0600535typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_key_slot_t key_slot,
536 psa_algorithm_t alg,
537 const uint8_t *p_input,
538 size_t input_length,
539 const uint8_t *p_salt,
540 size_t salt_length,
541 uint8_t *p_output,
542 size_t output_size,
543 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100544
545/**
546 * \brief A struct containing all of the function pointers needed to implement
547 * asymmetric cryptographic operations using opaque keys.
548 *
549 * PSA Crypto API implementations should populate instances of the table as
550 * appropriate upon startup.
551 *
552 * If one of the functions is not implemented, it should be set to NULL.
553 */
554typedef struct {
555 /** Function that performs the asymmetric sign operation */
Derek Miller83d26622019-02-15 16:41:22 -0600556 psa_drv_se_asymmetric_sign_t *p_sign;
Gilles Peskine75976892018-12-12 15:55:09 +0100557 /** Function that performs the asymmetric verify operation */
Derek Miller83d26622019-02-15 16:41:22 -0600558 psa_drv_se_asymmetric_verify_t *p_verify;
Gilles Peskine75976892018-12-12 15:55:09 +0100559 /** Function that performs the asymmetric encrypt operation */
Derek Miller83d26622019-02-15 16:41:22 -0600560 psa_drv_se_asymmetric_encrypt_t *p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100561 /** Function that performs the asymmetric decrypt operation */
Derek Miller83d26622019-02-15 16:41:22 -0600562 psa_drv_se_asymmetric_decrypt_t *p_decrypt;
563} psa_drv_se_asymmetric_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100564
565/**@}*/
566
567/** \defgroup aead_opaque AEAD Opaque
568 * Authenticated Encryption with Additional Data (AEAD) operations with opaque
569 * keys must be done in one function call. While this creates a burden for
570 * implementers as there must be sufficient space in memory for the entire
571 * message, it prevents decrypted data from being made available before the
572 * authentication operation is complete and the data is known to be authentic.
573 */
574/**@{*/
575
576/** \brief Process an authenticated encryption operation using an opaque key
577 *
578 * \param[in] key_slot Slot containing the key to use.
579 * \param[in] algorithm The AEAD algorithm to compute
580 * (\c PSA_ALG_XXX value such that
581 * #PSA_ALG_IS_AEAD(`alg`) is true)
582 * \param[in] p_nonce Nonce or IV to use
583 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
584 * \param[in] p_additional_data Additional data that will be
585 * authenticated but not encrypted
586 * \param[in] additional_data_length Size of `p_additional_data` in bytes
587 * \param[in] p_plaintext Data that will be authenticated and
588 * encrypted
589 * \param[in] plaintext_length Size of `p_plaintext` in bytes
590 * \param[out] p_ciphertext Output buffer for the authenticated and
591 * encrypted data. The additional data is
592 * not part of this output. For algorithms
593 * where the encrypted data and the
594 * authentication tag are defined as
595 * separate outputs, the authentication
596 * tag is appended to the encrypted data.
597 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
598 * bytes
599 * \param[out] p_ciphertext_length On success, the size of the output in
600 * the `p_ciphertext` buffer
601 *
602 * \retval #PSA_SUCCESS
603 * Success.
604 */
Derek Miller83d26622019-02-15 16:41:22 -0600605typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_key_slot_t key_slot,
606 psa_algorithm_t algorithm,
607 const uint8_t *p_nonce,
608 size_t nonce_length,
609 const uint8_t *p_additional_data,
610 size_t additional_data_length,
611 const uint8_t *p_plaintext,
612 size_t plaintext_length,
613 uint8_t *p_ciphertext,
614 size_t ciphertext_size,
615 size_t *p_ciphertext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100616
617/** Process an authenticated decryption operation using an opaque key
618 *
619 * \param[in] key_slot Slot containing the key to use
620 * \param[in] algorithm The AEAD algorithm to compute
621 * (\c PSA_ALG_XXX value such that
622 * #PSA_ALG_IS_AEAD(`alg`) is true)
623 * \param[in] p_nonce Nonce or IV to use
624 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
625 * \param[in] p_additional_data Additional data that has been
626 * authenticated but not encrypted
627 * \param[in] additional_data_length Size of `p_additional_data` in bytes
628 * \param[in] p_ciphertext Data that has been authenticated and
629 * encrypted.
630 * For algorithms where the encrypted data
631 * and the authentication tag are defined
632 * as separate inputs, the buffer must
633 * contain the encrypted data followed by
634 * the authentication tag.
635 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
636 * \param[out] p_plaintext Output buffer for the decrypted data
637 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
638 * bytes
639 * \param[out] p_plaintext_length On success, the size of the output in
640 * the `p_plaintext` buffer
641 *
642 * \retval #PSA_SUCCESS
643 * Success.
644 */
Derek Miller83d26622019-02-15 16:41:22 -0600645typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_key_slot_t key_slot,
646 psa_algorithm_t algorithm,
647 const uint8_t *p_nonce,
648 size_t nonce_length,
649 const uint8_t *p_additional_data,
650 size_t additional_data_length,
651 const uint8_t *p_ciphertext,
652 size_t ciphertext_length,
653 uint8_t *p_plaintext,
654 size_t plaintext_size,
655 size_t *p_plaintext_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100656
657/**
658 * \brief A struct containing all of the function pointers needed to implement
659 * Authenticated Encryption with Additional Data operations using opaque keys
660 *
661 * PSA Crypto API implementations should populate instances of the table as
662 * appropriate upon startup.
663 *
664 * If one of the functions is not implemented, it should be set to NULL.
665 */
666typedef struct {
667 /** Function that performs the AEAD encrypt operation */
Derek Miller83d26622019-02-15 16:41:22 -0600668 psa_drv_se_aead_encrypt_t *p_encrypt;
Gilles Peskine75976892018-12-12 15:55:09 +0100669 /** Function that performs the AEAD decrypt operation */
Derek Miller83d26622019-02-15 16:41:22 -0600670 psa_drv_se_aead_decrypt_t *p_decrypt;
671} psa_drv_se_aead_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100672/**@}*/
673
674/** \defgroup driver_key_management Key Management
675 * Currently, key management is limited to importing keys in the clear,
676 * destroying keys, and exporting keys in the clear.
677 * Whether a key may be exported is determined by the key policies in place
678 * on the key slot.
679 */
680/**@{*/
681
682/** \brief Import a key in binary format
683 *
684 * This function can support any output from psa_export_key(). Refer to the
685 * documentation of psa_export_key() for the format for each key type.
686 *
687 * \param[in] key_slot Slot where the key will be stored
688 * This must be a valid slot for a key of the chosen
689 * type. It must be unoccupied.
690 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
691 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
692 * \param[in] usage The allowed uses of the key
693 * \param[in] p_data Buffer containing the key data
694 * \param[in] data_length Size of the `data` buffer in bytes
695 *
696 * \retval #PSA_SUCCESS
697 * Success.
698 */
Derek Miller83d26622019-02-15 16:41:22 -0600699typedef psa_status_t (*psa_drv_se_import_key_t)(psa_key_slot_t key_slot,
700 psa_key_type_t type,
701 psa_algorithm_t algorithm,
702 psa_key_usage_t usage,
703 const uint8_t *p_data,
704 size_t data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100705
706/**
707 * \brief Destroy a key and restore the slot to its default state
708 *
709 * This function destroys the content of the key slot from both volatile
710 * memory and, if applicable, non-volatile storage. Implementations shall
711 * make a best effort to ensure that any previous content of the slot is
712 * unrecoverable.
713 *
714 * This function also erases any metadata such as policies. It returns the
715 * specified slot to its default state.
716 *
717 * \param[in] key_slot The key slot to erase.
718 *
719 * \retval #PSA_SUCCESS
720 * The slot's content, if any, has been erased.
721 */
Derek Miller83d26622019-02-15 16:41:22 -0600722typedef psa_status_t (*psa_drv_se_destroy_key_t)(psa_key_slot_t key);
Gilles Peskine75976892018-12-12 15:55:09 +0100723
724/**
725 * \brief Export a key in binary format
726 *
727 * The output of this function can be passed to psa_import_key() to
728 * create an equivalent object.
729 *
730 * If a key is created with `psa_import_key()` and then exported with
731 * this function, it is not guaranteed that the resulting data is
732 * identical: the implementation may choose a different representation
733 * of the same key if the format permits it.
734 *
735 * For standard key types, the output format is as follows:
736 *
737 * - For symmetric keys (including MAC keys), the format is the
738 * raw bytes of the key.
739 * - For DES, the key data consists of 8 bytes. The parity bits must be
740 * correct.
741 * - For Triple-DES, the format is the concatenation of the
742 * two or three DES keys.
743 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
744 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
745 * as RSAPrivateKey.
746 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
747 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
748 *
749 * \param[in] key Slot whose content is to be exported. This must
750 * be an occupied key slot.
751 * \param[out] p_data Buffer where the key data is to be written.
752 * \param[in] data_size Size of the `p_data` buffer in bytes.
753 * \param[out] p_data_length On success, the number of bytes
754 * that make up the key data.
755 *
756 * \retval #PSA_SUCCESS
757 * \retval #PSA_ERROR_EMPTY_SLOT
758 * \retval #PSA_ERROR_NOT_PERMITTED
759 * \retval #PSA_ERROR_NOT_SUPPORTED
760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
761 * \retval #PSA_ERROR_HARDWARE_FAILURE
762 * \retval #PSA_ERROR_TAMPERING_DETECTED
763 */
Derek Miller83d26622019-02-15 16:41:22 -0600764typedef psa_status_t (*psa_drv_se_export_key_t)(psa_key_slot_t key,
765 uint8_t *p_data,
766 size_t data_size,
767 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100768
769/**
770 * \brief Export a public key or the public part of a key pair in binary format
771 *
772 * The output of this function can be passed to psa_import_key() to
773 * create an object that is equivalent to the public key.
774 *
775 * For standard key types, the output format is as follows:
776 *
777 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
778 * the format is the DER representation of the public key defined by RFC 5280
779 * as SubjectPublicKeyInfo.
780 *
781 * \param[in] key_slot Slot whose content is to be exported. This must
782 * be an occupied key slot.
783 * \param[out] p_data Buffer where the key data is to be written.
784 * \param[in] data_size Size of the `data` buffer in bytes.
785 * \param[out] p_data_length On success, the number of bytes
786 * that make up the key data.
787 *
788 * \retval #PSA_SUCCESS
789 */
Derek Miller83d26622019-02-15 16:41:22 -0600790typedef psa_status_t (*psa_drv_se_export_public_key_t)(psa_key_slot_t key,
Gilles Peskine75976892018-12-12 15:55:09 +0100791 uint8_t *p_data,
792 size_t data_size,
793 size_t *p_data_length);
794
795/**
796 * \brief A struct containing all of the function pointers needed to for key
797 * management using opaque keys
798 *
799 * PSA Crypto API implementations should populate instances of the table as
800 * appropriate upon startup.
801 *
802 * If one of the functions is not implemented, it should be set to NULL.
803 */
804typedef struct {
805 /** Function that performs the key import operation */
Derek Miller83d26622019-02-15 16:41:22 -0600806 psa_drv_se_import_key_t *p_import;
Gilles Peskine75976892018-12-12 15:55:09 +0100807 /** Function that performs the key destroy operation */
Derek Miller83d26622019-02-15 16:41:22 -0600808 psa_drv_se_destroy_key_t *p_destroy;
Gilles Peskine75976892018-12-12 15:55:09 +0100809 /** Function that performs the key export operation */
Derek Miller83d26622019-02-15 16:41:22 -0600810 psa_drv_se_export_key_t *p_export;
Gilles Peskine75976892018-12-12 15:55:09 +0100811 /** Function that perforsm the public key export operation */
Derek Miller83d26622019-02-15 16:41:22 -0600812 psa_drv_se_export_public_key_t *p_export_public;
813} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100814
815/**@}*/
816
817/** \defgroup driver_derivation Key Derivation and Agreement
818 * Key derivation is the process of generating new key material using an
819 * existing key and additional parameters, iterating through a basic
820 * cryptographic function, such as a hash.
821 * Key agreement is a part of cryptographic protocols that allows two parties
822 * to agree on the same key value, but starting from different original key
823 * material.
824 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
825 * for both of the flows.
826 *
827 * There are two different final functions for the flows,
828 * `psa_drv_key_derivation_derive` and `psa_drv_key_derivation_export`.
829 * `psa_drv_key_derivation_derive` is used when the key material should be placed
830 * in a slot on the hardware and not exposed to the caller.
831 * `psa_drv_key_derivation_export` is used when the key material should be returned
832 * to the PSA Cryptographic API implementation.
833 *
834 * Different key derivation algorithms require a different number of inputs.
835 * Instead of having an API that takes as input variable length arrays, which
836 * can be problemmatic to manage on embedded platforms, the inputs are passed
837 * to the driver via a function, `psa_drv_key_derivation_collateral`, that is
838 * called multiple times with different `collateral_id`s. Thus, for a key
839 * derivation algorithm that required 3 paramter inputs, the flow would look
840 * something like:
841 * ~~~~~~~~~~~~~{.c}
842 * psa_drv_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
843 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_0,
844 * p_collateral_0,
845 * collateral_0_size);
846 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_1,
847 * p_collateral_1,
848 * collateral_1_size);
849 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_2,
850 * p_collateral_2,
851 * collateral_2_size);
852 * psa_drv_key_derivation_derive();
853 * ~~~~~~~~~~~~~
854 *
855 * key agreement example:
856 * ~~~~~~~~~~~~~{.c}
857 * psa_drv_key_derivation_setup(alg, source_key. dest_key_size_bytes);
858 * psa_drv_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
859 * psa_drv_key_derivation_export(p_session_key,
860 * session_key_size,
861 * &session_key_length);
862 * ~~~~~~~~~~~~~
863 */
864/**@{*/
865
866/** \brief The hardware-specific key derivation context structure
867 *
868 * The contents of this structure are implementation dependent and are
869 * therefore not described here
870 */
871typedef struct psa_drv_key_derivation_context_s psa_drv_key_derivation_context_t;
872
873/** \brief Set up a key derivation operation by specifying the algorithm and
874 * the source key sot
875 *
876 * \param[in,out] p_context A hardware-specific structure containing any
877 * context information for the implementation
878 * \param[in] kdf_alg The algorithm to be used for the key derivation
879 * \param[in] souce_key The key to be used as the source material for the
880 * key derivation
881 *
882 * \retval PSA_SUCCESS
883 */
Derek Miller83d26622019-02-15 16:41:22 -0600884typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_key_derivation_context_t *p_context,
885 psa_algorithm_t kdf_alg,
886 psa_key_slot_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100887
888/** \brief Provide collateral (parameters) needed for a key derivation or key
889 * agreement operation
890 *
891 * Since many key derivation algorithms require multiple parameters, it is
892 * expeced that this function may be called multiple times for the same
893 * operation, each with a different algorithm-specific `collateral_id`
894 *
895 * \param[in,out] p_context A hardware-specific structure containing any
896 * context information for the implementation
897 * \param[in] collateral_id An ID for the collateral being provided
898 * \param[in] p_collateral A buffer containing the collateral data
899 * \param[in] collateral_size The size in bytes of the collateral
900 *
901 * \retval PSA_SUCCESS
902 */
Derek Miller83d26622019-02-15 16:41:22 -0600903typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(psa_drv_key_derivation_context_t *p_context,
904 uint32_t collateral_id,
905 const uint8_t *p_collateral,
906 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100907
908/** \brief Perform the final key derivation step and place the generated key
909 * material in a slot
910 * \param[in,out] p_context A hardware-specific structure containing any
911 * context information for the implementation
912 * \param[in] dest_key The slot where the generated key material
913 * should be placed
914 *
915 * \retval PSA_SUCCESS
916 */
Derek Miller83d26622019-02-15 16:41:22 -0600917typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(psa_drv_key_derivation_context_t *p_context,
Gilles Peskine75976892018-12-12 15:55:09 +0100918 psa_key_slot_t dest_key);
919
920/** \brief Perform the final step of a key agreement and place the generated
921 * key material in a buffer
922 *
923 * \param[out] p_output Buffer in which to place the generated key
924 * material
925 * \param[in] output_size The size in bytes of `p_output`
926 * \param[out] p_output_length Upon success, contains the number of bytes of
927 * key material placed in `p_output`
928 *
929 * \retval PSA_SUCCESS
930 */
Derek Miller83d26622019-02-15 16:41:22 -0600931typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(uint8_t *p_output,
Gilles Peskine75976892018-12-12 15:55:09 +0100932 size_t output_size,
933 size_t *p_output_length);
934
935/**
936 * \brief A struct containing all of the function pointers needed to for key
937 * derivation and agreement
938 *
939 * PSA Crypto API implementations should populate instances of the table as
940 * appropriate upon startup.
941 *
942 * If one of the functions is not implemented, it should be set to NULL.
943 */
944typedef struct {
945 /** Function that performs the key derivation setup */
Derek Miller83d26622019-02-15 16:41:22 -0600946 psa_drv_se_key_derivation_setup_t *p_setup;
Gilles Peskine75976892018-12-12 15:55:09 +0100947 /** Function that sets the key derivation collateral */
Derek Miller83d26622019-02-15 16:41:22 -0600948 psa_drv_se_key_derivation_collateral_t *p_collateral;
Gilles Peskine75976892018-12-12 15:55:09 +0100949 /** Function that performs the final key derivation step */
Derek Miller83d26622019-02-15 16:41:22 -0600950 psa_drv_se_key_derivation_derive_t *p_derive;
Gilles Peskine75976892018-12-12 15:55:09 +0100951 /** Function that perforsm the final key derivation or agreement and
952 * exports the key */
Derek Miller83d26622019-02-15 16:41:22 -0600953 psa_drv_se_key_derivation_export_t *p_export;
954} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100955
956/**@}*/
957
958#ifdef __cplusplus
959}
960#endif
961
962#endif /* PSA_CRYPTO_SE_DRIVER_H */