blob: 438067f26b252c0d194adbc6457c7b6e49126d99 [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. */
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
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,
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
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,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600163 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600164 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,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600190 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600191 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 Millerea743cf2019-02-15 17:06:29 -0600216 psa_drv_se_mac_setup_t p_setup;
Derek Miller83d26622019-02-15 16:41:22 -0600217 /** Function that performs the update operation
Gilles Peskine75976892018-12-12 15:55:09 +0100218 */
Derek Millerea743cf2019-02-15 17:06:29 -0600219 psa_drv_se_mac_update_t p_update;
Gilles Peskine75976892018-12-12 15:55:09 +0100220 /** Function that completes the operation
221 */
Derek Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -0600234 psa_drv_se_mac_verify_t p_mac_verify;
Derek Miller83d26622019-02-15 16:41:22 -0600235} 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,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600276 psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600277 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 Millerb2a1cce2019-02-15 17:03:42 -0600373typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600374 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 Miller34b33f12019-02-15 17:13:54 -0600395 size_t context_size;
396 /** Function that performs a cipher setup operation */
Derek Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -0600401 psa_drv_se_cipher_update_t p_update;
Gilles Peskine75976892018-12-12 15:55:09 +0100402 /** Function that completes the operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600403 psa_drv_se_cipher_finish_t p_finish;
Gilles Peskine75976892018-12-12 15:55:09 +0100404 /** Function that aborts the operation */
Derek Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -0600410 psa_drv_se_cipher_ecb_t p_ecb;
Derek Miller83d26622019-02-15 16:41:22 -0600411} 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 Millerb2a1cce2019-02-15 17:03:42 -0600438typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600439 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 Millerb2a1cce2019-02-15 17:03:42 -0600462typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600463 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 Millerb2a1cce2019-02-15 17:03:42 -0600498typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600499 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 Millerb2a1cce2019-02-15 17:03:42 -0600535typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600536 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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -0600562 psa_drv_se_asymmetric_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600563} 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 Millerb2a1cce2019-02-15 17:03:42 -0600605typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600606 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 Millerb2a1cce2019-02-15 17:03:42 -0600645typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_key_slot_number_t key_slot,
Derek Miller83d26622019-02-15 16:41:22 -0600646 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 Millerea743cf2019-02-15 17:06:29 -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 Millerea743cf2019-02-15 17:06:29 -0600670 psa_drv_se_aead_decrypt_t p_decrypt;
Derek Miller83d26622019-02-15 16:41:22 -0600671} 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 Millerb2a1cce2019-02-15 17:03:42 -0600699typedef psa_status_t (*psa_drv_se_import_key_t)(psa_key_slot_number_t key_slot,
Derek Miller0972fe52019-02-15 17:08:27 -0600700 psa_key_lifetime_t lifetime,
Derek Miller83d26622019-02-15 16:41:22 -0600701 psa_key_type_t type,
702 psa_algorithm_t algorithm,
703 psa_key_usage_t usage,
704 const uint8_t *p_data,
705 size_t data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100706
707/**
708 * \brief Destroy a key and restore the slot to its default state
709 *
710 * This function destroys the content of the key slot from both volatile
711 * memory and, if applicable, non-volatile storage. Implementations shall
712 * make a best effort to ensure that any previous content of the slot is
713 * unrecoverable.
714 *
715 * This function also erases any metadata such as policies. It returns the
716 * specified slot to its default state.
717 *
718 * \param[in] key_slot The key slot to erase.
719 *
720 * \retval #PSA_SUCCESS
721 * The slot's content, if any, has been erased.
722 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600723typedef psa_status_t (*psa_drv_se_destroy_key_t)(psa_key_slot_number_t key);
Gilles Peskine75976892018-12-12 15:55:09 +0100724
725/**
726 * \brief Export a key in binary format
727 *
728 * The output of this function can be passed to psa_import_key() to
729 * create an equivalent object.
730 *
731 * If a key is created with `psa_import_key()` and then exported with
732 * this function, it is not guaranteed that the resulting data is
733 * identical: the implementation may choose a different representation
734 * of the same key if the format permits it.
735 *
736 * For standard key types, the output format is as follows:
737 *
738 * - For symmetric keys (including MAC keys), the format is the
739 * raw bytes of the key.
740 * - For DES, the key data consists of 8 bytes. The parity bits must be
741 * correct.
742 * - For Triple-DES, the format is the concatenation of the
743 * two or three DES keys.
744 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
745 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
746 * as RSAPrivateKey.
747 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
748 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
749 *
750 * \param[in] key Slot whose content is to be exported. This must
751 * be an occupied key slot.
752 * \param[out] p_data Buffer where the key data is to be written.
753 * \param[in] data_size Size of the `p_data` buffer in bytes.
754 * \param[out] p_data_length On success, the number of bytes
755 * that make up the key data.
756 *
757 * \retval #PSA_SUCCESS
758 * \retval #PSA_ERROR_EMPTY_SLOT
759 * \retval #PSA_ERROR_NOT_PERMITTED
760 * \retval #PSA_ERROR_NOT_SUPPORTED
761 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
762 * \retval #PSA_ERROR_HARDWARE_FAILURE
763 * \retval #PSA_ERROR_TAMPERING_DETECTED
764 */
Derek Millerb2a1cce2019-02-15 17:03:42 -0600765typedef psa_status_t (*psa_drv_se_export_key_t)(psa_key_slot_number_t key,
Derek Miller83d26622019-02-15 16:41:22 -0600766 uint8_t *p_data,
767 size_t data_size,
768 size_t *p_data_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100769
770/**
771 * \brief Export a public key or the public part of a key pair in binary format
772 *
773 * The output of this function can be passed to psa_import_key() to
774 * create an object that is equivalent to the public key.
775 *
776 * For standard key types, the output format is as follows:
777 *
778 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
779 * the format is the DER representation of the public key defined by RFC 5280
780 * as SubjectPublicKeyInfo.
781 *
782 * \param[in] key_slot Slot whose content is to be exported. This must
783 * be an occupied key slot.
784 * \param[out] p_data Buffer where the key data is to be written.
785 * \param[in] data_size Size of the `data` buffer in bytes.
786 * \param[out] p_data_length On success, the number of bytes
787 * that make up the key data.
788 *
789 * \retval #PSA_SUCCESS
790 */
Derek Miller0b3098a2019-02-15 17:10:49 -0600791typedef psa_status_t (*psa_drv_se_generate_key_t) (psa_key_slot_number_t key_slot,
792 psa_key_type_t type,
793 psa_key_usage_t usage,
794 size_t bits,
795 const void *extra,
796 size_t extra_size,
797 uint8_t *p_pubkey_out,
798 size_t pubkey_out_size,
799 size_t *p_pubkey_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100800
801/**
802 * \brief A struct containing all of the function pointers needed to for key
803 * management using opaque keys
804 *
805 * PSA Crypto API implementations should populate instances of the table as
806 * appropriate upon startup.
807 *
808 * If one of the functions is not implemented, it should be set to NULL.
809 */
810typedef struct {
811 /** Function that performs the key import operation */
Derek Millerea743cf2019-02-15 17:06:29 -0600812 psa_drv_se_import_key_t p_import;
Gilles Peskine75976892018-12-12 15:55:09 +0100813 /** Function that performs the key destroy operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600814 psa_drv_se_generate_key_t p_generate;
Gilles Peskine75976892018-12-12 15:55:09 +0100815 /** Function that performs the key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600816 psa_drv_se_destroy_key_t p_destroy;
Gilles Peskine75976892018-12-12 15:55:09 +0100817 /** Function that perforsm the public key export operation */
Derek Miller0b3098a2019-02-15 17:10:49 -0600818 psa_drv_se_export_key_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -0600819} psa_drv_se_key_management_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100820
821/**@}*/
822
823/** \defgroup driver_derivation Key Derivation and Agreement
824 * Key derivation is the process of generating new key material using an
825 * existing key and additional parameters, iterating through a basic
826 * cryptographic function, such as a hash.
827 * Key agreement is a part of cryptographic protocols that allows two parties
828 * to agree on the same key value, but starting from different original key
829 * material.
830 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
831 * for both of the flows.
832 *
833 * There are two different final functions for the flows,
834 * `psa_drv_key_derivation_derive` and `psa_drv_key_derivation_export`.
835 * `psa_drv_key_derivation_derive` is used when the key material should be placed
836 * in a slot on the hardware and not exposed to the caller.
837 * `psa_drv_key_derivation_export` is used when the key material should be returned
838 * to the PSA Cryptographic API implementation.
839 *
840 * Different key derivation algorithms require a different number of inputs.
841 * Instead of having an API that takes as input variable length arrays, which
842 * can be problemmatic to manage on embedded platforms, the inputs are passed
843 * to the driver via a function, `psa_drv_key_derivation_collateral`, that is
844 * called multiple times with different `collateral_id`s. Thus, for a key
845 * derivation algorithm that required 3 paramter inputs, the flow would look
846 * something like:
847 * ~~~~~~~~~~~~~{.c}
848 * psa_drv_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
849 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_0,
850 * p_collateral_0,
851 * collateral_0_size);
852 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_1,
853 * p_collateral_1,
854 * collateral_1_size);
855 * psa_drv_key_derivation_collateral(kdf_algorithm_collateral_id_2,
856 * p_collateral_2,
857 * collateral_2_size);
858 * psa_drv_key_derivation_derive();
859 * ~~~~~~~~~~~~~
860 *
861 * key agreement example:
862 * ~~~~~~~~~~~~~{.c}
863 * psa_drv_key_derivation_setup(alg, source_key. dest_key_size_bytes);
864 * psa_drv_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
865 * psa_drv_key_derivation_export(p_session_key,
866 * session_key_size,
867 * &session_key_length);
868 * ~~~~~~~~~~~~~
869 */
870/**@{*/
871
872/** \brief The hardware-specific key derivation context structure
873 *
874 * The contents of this structure are implementation dependent and are
875 * therefore not described here
876 */
Gilles Peskine75976892018-12-12 15:55:09 +0100877
878/** \brief Set up a key derivation operation by specifying the algorithm and
879 * the source key sot
880 *
881 * \param[in,out] p_context A hardware-specific structure containing any
882 * context information for the implementation
883 * \param[in] kdf_alg The algorithm to be used for the key derivation
884 * \param[in] souce_key The key to be used as the source material for the
885 * key derivation
886 *
887 * \retval PSA_SUCCESS
888 */
Derek Miller62117262019-02-15 17:12:26 -0600889typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600890 psa_algorithm_t kdf_alg,
Derek Millerb2a1cce2019-02-15 17:03:42 -0600891 psa_key_slot_number_t source_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100892
893/** \brief Provide collateral (parameters) needed for a key derivation or key
894 * agreement operation
895 *
896 * Since many key derivation algorithms require multiple parameters, it is
897 * expeced that this function may be called multiple times for the same
898 * operation, each with a different algorithm-specific `collateral_id`
899 *
900 * \param[in,out] p_context A hardware-specific structure containing any
901 * context information for the implementation
902 * \param[in] collateral_id An ID for the collateral being provided
903 * \param[in] p_collateral A buffer containing the collateral data
904 * \param[in] collateral_size The size in bytes of the collateral
905 *
906 * \retval PSA_SUCCESS
907 */
Derek Miller62117262019-02-15 17:12:26 -0600908typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *p_context,
Derek Miller83d26622019-02-15 16:41:22 -0600909 uint32_t collateral_id,
910 const uint8_t *p_collateral,
911 size_t collateral_size);
Gilles Peskine75976892018-12-12 15:55:09 +0100912
913/** \brief Perform the final key derivation step and place the generated key
914 * material in a slot
915 * \param[in,out] p_context A hardware-specific structure containing any
916 * context information for the implementation
917 * \param[in] dest_key The slot where the generated key material
918 * should be placed
919 *
920 * \retval PSA_SUCCESS
921 */
Derek Miller62117262019-02-15 17:12:26 -0600922typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *p_context,
923 psa_key_slot_number_t dest_key);
Gilles Peskine75976892018-12-12 15:55:09 +0100924
925/** \brief Perform the final step of a key agreement and place the generated
926 * key material in a buffer
927 *
928 * \param[out] p_output Buffer in which to place the generated key
929 * material
930 * \param[in] output_size The size in bytes of `p_output`
931 * \param[out] p_output_length Upon success, contains the number of bytes of
932 * key material placed in `p_output`
933 *
934 * \retval PSA_SUCCESS
935 */
Derek Miller62117262019-02-15 17:12:26 -0600936typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *p_context,
937 uint8_t *p_output,
938 size_t output_size,
939 size_t *p_output_length);
Gilles Peskine75976892018-12-12 15:55:09 +0100940
941/**
942 * \brief A struct containing all of the function pointers needed to for key
943 * derivation and agreement
944 *
945 * PSA Crypto API implementations should populate instances of the table as
946 * appropriate upon startup.
947 *
948 * If one of the functions is not implemented, it should be set to NULL.
949 */
950typedef struct {
Derek Miller62117262019-02-15 17:12:26 -0600951 /** The driver-specific size of the key derivation context */
952 size_t context_size;
Gilles Peskine75976892018-12-12 15:55:09 +0100953 /** Function that performs the key derivation setup */
Derek Millerea743cf2019-02-15 17:06:29 -0600954 psa_drv_se_key_derivation_setup_t p_setup;
Gilles Peskine75976892018-12-12 15:55:09 +0100955 /** Function that sets the key derivation collateral */
Derek Millerea743cf2019-02-15 17:06:29 -0600956 psa_drv_se_key_derivation_collateral_t p_collateral;
Gilles Peskine75976892018-12-12 15:55:09 +0100957 /** Function that performs the final key derivation step */
Derek Millerea743cf2019-02-15 17:06:29 -0600958 psa_drv_se_key_derivation_derive_t p_derive;
Gilles Peskine75976892018-12-12 15:55:09 +0100959 /** Function that perforsm the final key derivation or agreement and
960 * exports the key */
Derek Millerea743cf2019-02-15 17:06:29 -0600961 psa_drv_se_key_derivation_export_t p_export;
Derek Miller83d26622019-02-15 16:41:22 -0600962} psa_drv_se_key_derivation_t;
Gilles Peskine75976892018-12-12 15:55:09 +0100963
964/**@}*/
965
966#ifdef __cplusplus
967}
968#endif
969
970#endif /* PSA_CRYPTO_SE_DRIVER_H */