blob: 64c21b591b7981c0ce8d83fa2c375cfa9ee8476d [file] [log] [blame]
Derek Miller5b3417a2018-10-10 17:55:03 -05001#ifndef __PSA_CRYPTO_DRIVER_H__
2#define __PSA_CRYPTO_DRIVER_H__
3
4#include <stddef.h>
5#include <stdint.h>
6
7typedef uint32_t psa_status_t;
8typedef uint32_t psa_algorithm_t;
9typedef uint32_t encrypt_or_decrypt_t;
10typedef uint32_t psa_key_slot_t;
11typedef uint32_t psa_key_type_t;
12
13/** \defgroup opaque_mac Opaque Message Authentication Code
14 * @{
15 */
16/** \brief A function that starts an MAC operation for a PSA Crypto Driver implementation using an opaque key
17 *
18 * \param p_context A structure that will contain the hardware-specific MAC context
19 * \param key_slot The slot of the key to be used for the operation
20 * \param algorithm The algorithm to be used to underly the MAC operation
21 *
22 * \retval PSA_SUCCESS
23 * Success.
24 */
25typedef psa_status_t (*pcd_mac_opaque_setup_t)( void *p_context,
26 psa_key_slot_t key_slot,
27 psa_algorithm_t algorithm );
28
29/** \brief A function that continues a previously started MAC operation using an opaque key
30 *
31 * \param p_context A hardware-specific structure for the previously-established MAC operation to be continued
32 * \param p_input A buffer containing the message to be appended to the MAC operation
33 * \param input_length The size in bytes of the input message buffer
34 */
35typedef psa_status_t (*pcd_mac_opaque_update_t)( void *p_context,
36 const unsigned char *p_input,
37 size_t input_length );
38
39/** \brief a function that completes a previously started MAC operation by returning the resulting MAC using an opaque key
40 *
41 * \param p_context A hardware-specific structure for the previously started MAC operation to be fiinished
42 * \param p_output A buffer where the generated MAC will be placed
43 * \param output_size The size in bytes of the buffer that has been allocated for the `output` buffer
44 * \param p_output_length After completion, the address will contain the number of bytes placed in the `p_output` buffer
45 *
46 * \retval PSA_SUCCESS
47 * Success.
48 */
49typedef psa_status_t (*pcd_mac_opaque_finish_t)( void *p_ctx,
50 unsigned char *p_output,
51 size_t output_size,
52 size_t *p_output_length );
53
54/** \brief A function that completes a previously started MAC operation by comparing the resulting MAC against a known value
55 * using an opaque key
56 *
57 * \param p_context A hardware-specific structure for the previously started MAC operation to be fiinished
58 * \param p_mac The MAC value against which the resulting MAC will be compared against
59 * \param mac_length The size in bytes of the value stored in `mac`
60 *
61 * \retval PSA_SUCCESS
62 * The operation completed successfully and the MACs matched each other
63 * \retval PSA_ERROR_INVALID_SIGNATURE
64 * The operation completed successfully, but the calculated MAC did not match the provided MAC
65 */
66typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)( void *p_context,
67 const unsigned char *p_mac,
68 size_t mac_length );
69
70/** \brief A funciton that performs an MAC operation in one command and return the calculated MAC using an opaque key
71 *
72 * \param p_input A buffer containing the message to be MACed
73 * \param input_length The size in bytes of `input`
74 * \param key_slot The slot of the key to be used
75 * \param alg The algorithm to be used to underlie the MAC operation
76 * \param p_output A buffer where the generated MAC will be placed
77 * \param output_size The size in bytes of the `output` buffer
78 * \param p_output_length After completion, the address will contain the number of bytes placed in the `output` buffer
79 *
80 * \retval PSA_SUCCESS
81 * Success.
82 */
83typedef psa_status_t (*pcd_mac_opaque_t)( const unsigned char *p_input,
84 size_t input_length,
85 psa_key_slot_t key_slot,
86 psa_algorithm_t alg,
87 unsigned char *p_output,
88 size_t output_size,
89 size_t *p_output_length );
90
91/** \brief A function that performs an MAC operation in one command and compare the resulting MAC against a known value using an opaque key
92 *
93 * \param p_input A buffer containing the message to be MACed
94 * \param input_length The size in bytes of `input`
95 * \param key_slot The slot of the key to be used
96 * \param alg The algorithm to be used to underlie the MAC operation
97 * \param p_mac The MAC value against which the resulting MAC will be compared against
98 * \param mac_length The size in bytes of `mac`
99 *
100 * \retval PSA_SUCCESS
101 * The operation completed successfully and the MACs matched each other
102 * \retval PSA_ERROR_INVALID_SIGNATURE
103 * The operation completed successfully, but the calculated MAC did not match the provided MAC
104 */
105typedef psa_status_t (*pcd_mac_opaque_verify_t)( const unsigned char *p_input,
106 size_t input_length,
107 psa_key_slot_t key_slot,
108 psa_algorithm_t alg,
109 const unsigned char *p_mac,
110 size_t mac_length );
111
112/** \brief A struct containing all of the function pointers needed to implement MAC operations using opaque keys.
113 *
114 * PSA Crypto API implementations should populate the table as appropriate upon startup.
115 *
116 * If one of the functions is not implemented (such as `pcd_mac_opaque_t`), it should be set to NULL.
117 *
118 */
119struct pcd_mac_opaque_t {
120 size_t context_size; /**<The size in bytes of the hardware-specific Opaque-MAC Context structure */
121 pcd_mac_opaque_setup_t *p_setup; /**< Function that performs the setup operation */
122 pcd_mac_opaque_update_t *p_update; /**< Function that performs the update operation */
123 pcd_mac_opaque_finish_t *p_finish; /**< Function that completes the operation */
124 pcd_mac_opaque_t *p_mac; /**< Function that performs the MAC operation in one call */
125 pcd_mac_opaque_verify_t *p_mac_verify; /**<Function that performs the MAC and verify operation in one call */
126};
127/** @} */
128
129/** \defgroup transparent_mac Transparent Message Authentication Code
130 * @{
131 */
132
133/** \brief The hardware-specific transparent-key MAC context structure
134 * The contents of this structure are implementation dependent and are therefore not described here
135 */
136struct pcd_mac_transparent_context_t {
137 // Implementation specific
138};
139
140/** \brief The function prototype for the setup operation of a transparent-key MAC operation
141 *
142 * Functions that implement the prototype should be named in the following convention:
143 * ~~~~~~~~~~~~~{.c}
144 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_start
145 * ~~~~~~~~~~~~~
146 * Where `ALGO` is the name of the underlying hash function, and `MAC_VARIANT` is the specific variant of a
147 * MAC operation (such as HMAC or CMAC)
148 *
149 * \param p_context A structure that will contain the hardware-specific MAC context
150 * \param p_key A buffer containing the cleartext key material to be used in the operation
151 * \param key_length The size in bytes of the key material
152 *
153 * \retval PSA_SUCCESS
154 * Success.
155 */
156typedef psa_status_t (*pcd_mac_transparent_start_t)( struct pcd_mac_transparent_context_t *p_context,
157 const unsigned char *p_key,
158 size_t key_length );
159
160/** \brief The function prototype for the update operation of a transparent-key MAC operation
161 *
162 * Functions that implement the prototype should be named in the following convention:
163 * ~~~~~~~~~~~~~{.c}
164 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update
165 * ~~~~~~~~~~~~~
166 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is the specific variant of a
167 * MAC operation (such as HMAC or CMAC)
168 *
169 * \param p_context A hardware-specific structure for the previously-established MAC operation to be continued
170 * \param p_input A buffer containing the message to be appended to the MAC operation
171 * \param input_length The size in bytes of the input message buffer
172 */
173typedef psa_status_t (*pcd_mac_transparent_update_t)( struct pcd_mac_transparent_context_t *p_context,
174 const unsigned char *p_input,
175 size_t input_length );
176
177/** \brief The function prototype for the finish operation of a transparent-key MAC operation
178 *
179 * Functions that implement the prototype should be named in the following convention:
180 * ~~~~~~~~~~~~~{.c}
181 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish
182 * ~~~~~~~~~~~~~
183 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is the specific variant of a
184 * MAC operation (such as HMAC or CMAC)
185 *
186 * \param p_context A hardware-specific structure for the previously started MAC operation to be fiinished
187 * \param p_output A buffer where the generated MAC will be placed
188 * \param output_size The size in bytes of the buffer that has been allocated for the `p_output` buffer
189 * \param p_output_length After completion, will contain the number of bytes placed in the `p_output` buffer
190 *
191 * \retval PSA_SUCCESS
192 * Success.
193 */
194typedef psa_status_t (*pcd_mac_transparent_finish_t)( struct pcd_mac_transparent_context_t *p_context,
195 unsigned char *p_output,
196 size_t output_size,
197 size_t *p_output_length );
198
199/** @}
200 */
201
202/** \defgroup opaque_cipher Opaque Symmetric Ciphers
203 ** @{
204 */
205
206/** \brief A function pointer that provides the cipher setup function for opaque-key operations
207 *
208 * TBD: Since this is an opaque API (External, in Gilles nomeclature), shouldn't we be receiving a key handle/slot instead of key data? This is how I
209 * will write it
210
211 * \param p_context A structure that will contain the hardware-specific cipher context.
212 * \param key_slot THe slot of the key to be used for the operation
213 * \param algorithm The algorithm to be used in the cipher operation
214 * \param direction Indicates whether the operation is an encrypt or decrypt
215 *
216 * \retval PSA_SUCCESS
217 * \retval PSA_ERROR_NOT_SUPPORTED
218 */
219typedef psa_status_t (*pcd_cipher_opaque_setup_t) ( void *p_context,
220 psa_key_slot_t key_slot,
221 psa_algorithm_t algorithm,
222 encrypt_or_decrypt_t direction );
223
224
225/** \brief A function pointer that sets the initialization vector (if necessary) for an opaque cipher operation
226 *
227 * Note that the psa_cipher_* function set has two IV functions: one to set the IV, and one to generate it
228 * internally. the generate function is not necessary for the driver API as the PSA Crypto implementation
229 * can do the generation using it's RNG features
230 *
231 * \param p_context A structure that contains the previously set up hardware-specific cipher context
232 * \param p_iv A buffer containing the initialization vector
233 * \param iv_length The size (in bytes) of the `p_iv` buffer
234 *
235 * \retval PSA_SUCCESS
236 */
237typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)( void *p_context,
238 const uint8_t *p_iv,
239 size_t iv_length );
240
241/** \brief A function that continues a previously started opaque-key cipher operation
242 *
243 * \param p_context A hardware-specific structure for the previously started cipher operation
244 * \param p_input A buffer containing the data to be encrypted/decrypted
245 * \param input_size The size in bytes of the buffer pointed to by `p_input`
246 * \param p_output The caller-allocated buffer where the output will be placed
247 * \param output_size The allocated size in bytes of the `p_output` buffer
248 * \param p_output_length After completion, will contain the number of bytes placed in the `p_output` buffer
249 *
250 * \retval PSA_SUCCESS
251 */
252typedef psa_status_t (*pcd_cipher_opaque_update_t) (void *p_context,
253 const uint8_t *p_input,
254 size_t input_size,
255 uint8_t *p_output,
256 size_t output_size,
257 size_t *p_output_length );
258
259/** \brief A function that completes a previously started opaque-key cipher operation
260 *
261 * \param p_context A hardware-specific structure for the previously started cipher operation
262 * \param p_output The caller-callocated buffer where the output will be placed
263 * \param output_size The allocated size in bytes of the `p_output` buffer
264 * \param p_output_length After completion, will contain the number of bytes placed in the `p_output` buffer
265 *
266 * \retval PSA_SUCCESS
267 */
268typedef psa_status_t (*pcd_cipher_opaque_finish_t) (void *p_context, uint8_t *p_output, size_t output_size, size_t *p_output_length );
269
270/** \brief A function that performs the ECB block mode for opaque-key cipher operations
271 *
272 * Note: this function should only be used with implementations that do not provide a needed higher-level operation.
273 *
274 * \param key_slot The slot of the key to be used for the operation
275 * \param algorithm The algorithm to be used in the cipher operation
276 * \param direction Indicates whether the operation is an encrypt or decrypt
277 * \param p_input A buffer containing the data to be encrypted/decrypted
278 * \param input_size The size in bytes of the buffer pointed to by `p_input`
279 * \param p_output The caller-allocated byffer where the output will be placed
280 * \param output_size The allocated size in bytes of the `p_output` buffer
281 *
282 * \retval PSA_SUCCESS
283 * \retval PSA_ERROR_NOT_SUPPORTED
284 */
285typedef psa_status_t (*pcd_cipher_opaque_ecb_t) ( psa_key_slot_t key_slot,
286 psa_algorithm_t algorithm,
287 encrypt_or_decrypt_t direction,
288 const uint8_t *p_input,
289 size_t input_size,
290 uint8_t *p_output,
291 size_t output_size );
292
293/**
294 * \brief A struct containing all of the function pointers needed to implement cipher operations using opaque keys.
295 *
296 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
297 *
298 * If one of the functions is not implemented (such as `pcd_cipher_opaque_ecb_t`), it should be set to NULL.
299 */
300struct pcd_cipher_opaque_t {
301 size_t size; /**<The size in bytes of the hardware-specific Opaque Cipher context structure */
302 pcd_cipher_opaque_setup_t *p_setup; /**< Function that performs the setup operation */
303 pcd_cipher_opaque_set_iv_t *p_set_iv; /**< Function that sets the IV (if necessary) */
304 pcd_cipher_opaque_update_t *p_update; /**< Function that performs the update operation */
305 pcd_cipher_opaque_finish_t *p_finish; /**< Function that completes the operation */
306 pcd_cipher_opaque_ecb_t *p_ecb; /**< Function that performs ECB mode for the cipher (Danger: ECB mode is insecure) */
307};
308
309/** @}
310 */
311
312/** \defgroup transparent_cipher Transparent Block Cipher
313 * @{
314 */
315
316/** \brief The hardware-specific transparent-key Cipher context structure
317 * The contents of this structure are implementation dependent and are therefore not described here
318 */
319struct pcd_cipher_transparent_context_t {
320 // Implementation specific
321};
322
323/** \brief The function prototype for the setup operation of transparent-key block cipher operations.
324 * Functions that implement the prototype should be named in the following convention:
325 * ~~~~~~~~~~~~~{.c}
326 * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE>
327 * ~~~~~~~~~~~~~
328 * Where
329 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
330 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
331 *
332 * \param p_context A structure that will contain the hardware-specific cipher context
333 * \param direction Indicates if the operation is an encrypt or a decrypt
334 * \param p_key_data A buffer containing the cleartext key material to be used in the operation
335 * \param key_data_size The size in bytes of the key material
336 *
337 * \retval PSA_SUCCESS
338 */
339typedef psa_status_t (*pcd_cipher_transparent_setup_t) ( struct pcd_cipher_transparent_context_t *p_context,
340 encrypt_or_decrypt_t direction,
341 const uint8_t *p_key_data,
342 size_t key_data_size );
343
344/** \brief The function prototype for the set initialization vector operation of transparent-key block cipher operations
345 * Functions that implement the prototype should be named in the following convention:
346 * ~~~~~~~~~~~~~{.c}
347 * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE>
348 * ~~~~~~~~~~~~~
349 * Where
350 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
351 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
352 *
353 * \param p_context A structure that contains the previously setup hardware-specific cipher context
354 * \param p_iv A buffer containing the initialization vecotr
355 * \param iv_length The size in bytes of the contents of `p_iv`
356 *
357 * \retval PSA_SUCCESS
358*/
359typedef psa_status_t (*pcd_cipher_transparent_set_iv_t) ( struct pcd_cipher_transparent_context_t *p_context,
360 const uint8_t *p_iv,
361 size_t iv_length );
362/** \brief The function prototype for the update operation of transparent-key block cipher operations.
363 *
364 * Functions that implement the prototype should be named in the following convention:
365 * ~~~~~~~~~~~~~{.c}
366 * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE>
367 * ~~~~~~~~~~~~~
368 * Where
369 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
370 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
371 *
372 * TODO: Should the PSA Crypto API implementation calling these functions handle padding? What about hardware that handles padding?
373 *
374 * \param p_context A hardware-specific structure for the previously started cipher operation
375 * \param p_input A buffer containing the data to be encrypted or decrypted
376 * \param input_size The size in bytes of the `p_input` buffer
377 * \param p_output A caller-allocated buffer where the generated output will be placed
378 * \param output_size The size in bytes of the `p_output` buffer
379 * \param p_output_length After completion, will contain the number of bytes placed in the `p_output` buffer
380 *
381 * \retval PSA_SUCCESS
382 */
383typedef psa_status_t (*pcd_cipher_transparent_update_t) ( struct pcd_cipher_transparent_context_t *p_context,
384 const uint8_t *p_input,
385 size_t input_size,
386 uint8_t *p_output,
387 size_t output_size,
388 size_t *p_output_length );
389
390/** \brief The function prototype for the finish operation of transparent-key block cipher operations.
391*
392 * Functions that implement the prototype should be named in the following convention:
393 * ~~~~~~~~~~~~~{.c}
394 * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE>
395 * ~~~~~~~~~~~~~
396 * Where
397 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
398 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
399 *
400 * TODO: Should the PSA Crypto API implementation calling these functions handle padding? What about hardware that handles padding?
401 *
402 * \param p_context A hardware-specific structure for the previously started cipher operation
403 * \param p_output A caller-allocated buffer where the generated output will be placed
404 * \param output_size The size in bytes of the `p_output` buffer
405 * \param p_output_length After completion, will contain the number of bytes placed in the `p_output` buffer
406 *
407 * \retval PSA_SUCCESS
408 */
409typedef psa_status_t (*pcd_cipher_transparent_finish_t) ( struct pcd_cipher_transparent_context_t *p_context,
410 uint8_t *p_output,
411 size_t output_size,
412 size_t *p_output_length );
413
414/** \brief The function prototype for the abort operation of transparent-key block cipher operations.
415 *
416 * Functions that implement the following prototype should be named in the following convention:
417 * ~~~~~~~~~~~~~{.c}
418 * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE>
419 * ~~~~~~~~~~~~~
420 * Where
421 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
422 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
423 *
424 * TODO: Should the PSA Crypto API implementation calling these functions handle padding? What about hardware that handles padding?
425 *
426 * \param p_context A hardware-specific structure for the previously started cipher operation
427 *
428 * \retval PSA_SUCCESS
429 */
430typedef psa_status_t (*pcd_cipher_transparent_abort_t) ( struct pcd_cipher_transparent_context_t *p_context );
431
432/** @}
433 */
434
435/** \defgroup digest Message Digests
436 * @{
437 */
438
439/** \brief The hardware-specific hash context structure
440 * The contents of this structure are implementation dependent and are therefore not described here
441 */
442struct pcd_hash_context_t {
443 // Implementation specific
444};
445
446/** \brief The function prototype for the start operation of a hash (message digest) operation
447 *
448 * Functions that implement the prototype should be named in the following convention:
449 * ~~~~~~~~~~~~~{.c}
450 * pcd_hash_<ALGO>_start
451 * ~~~~~~~~~~~~~
452 * Where `ALGO` is the name of the underlying hash function
453 *
454 * \param p_context A structure that will contain the hardware-specific hash context
455 *
456 * \retval PSA_SUCCESS Success.
457 */
458typedef psa_status_t (*pcd_hash_start_t)(struct pcd_hash_context_t *p_context );
459
460
461
462/** \brief The function prototype for the update operation of a hash (message digest) operation
463 *
464 * Functions that implement the prototype should be named in the following convention:
465 * ~~~~~~~~~~~~~{.c}
466 * pcd_hash_<ALGO>_update
467 * ~~~~~~~~~~~~~
468 * Where `ALGO` is the name of the underlying algorithm
469 *
470 * \param p_context A hardware-specific structure for the previously-established hash operation to be continued
471 * \param p_input A buffer containing the message to be appended to the hash operation
472 * \param input_length The size in bytes of the input message buffer
473 */
474typedef psa_status_t (*pcd_hash_update_t)(struct pcd_hash_context_t *p_context, const unsigned char *p_input, size_t input_length);
475
476/** \brief The prototype for the finish operation of a hash (message digest) operation
477 *
478 * Functions that implement the prototype should be named in the following convention:
479 * ~~~~~~~~~~~~~{.c}
480 * pcd_hash_<ALGO>_finish
481 * ~~~~~~~~~~~~~
482 * Where `ALGO` is the name of the underlying algorithm
483 *
484 * \param p_context A hardware-specific structure for the previously started hash operation to be fiinished
485 * \param p_output A buffer where the generated digest will be placed
486 * \param output_size The size in bytes of the buffer that has been allocated for the `p_output` buffer
487 *
488 * \retval PSA_SUCCESS
489 * Success.
490 */
491typedef psa_status_t (*pcd_hash_finish_t)(struct pcd_hash_context_t *p_context, unsigned char *p_output, size_t output_size);
492
493/** \brief The function prototype for the abort operation of a hash (message digest) operation
494 *
495 * Functions that implement the prototype should be named in the following convention:
496 * ~~~~~~~~~~~~~{.c}
497 * pcd_hash_<ALGO>_abort
498 * ~~~~~~~~~~~~~
499 * Where `ALGO` is the name of the underlying algorithm
500 *
501 * \param p_context A hardware-specific structure for the previously started hash operation to be aborted
502 */
503typedef void (*pcd_hash_abort_t)(struct pcd_hash_context_t *p_context);
504
505/** @}
506 */
507
508
509/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
510 * @{
511 */
512
513/**
514 * \brief A function that signs a hash or short message with a private key.
515 *
516 * \param key_slot Key slot of an asymmetric key pair.
517 * \param alg A signature algorithm that is compatible with
518 * the type of `key`.
519 * \param[in] p_hash The hash or message to sign.
520 * \param hash_length Size of the `p_hash` buffer in bytes.
521 * \param[out] p_signature Buffer where the signature is to be written.
522 * \param signature_size Size of the `p_signature` buffer in bytes.
523 * \param[out] p_signature_length On success, the number of bytes
524 * that make up the returned signature value.
525 *
526 * \retval PSA_SUCCESS
527 */
528typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)( psa_key_slot_t key_slot,
529 psa_algorithm_t alg,
530 const uint8_t *p_hash,
531 size_t hash_length,
532 uint8_t *p_signature,
533 size_t signature_size,
534 size_t *p_signature_length );
535
536/**
537 * \brief A function that verifies the signature a hash or short message using a public key.
538 *
539 * \param key_slot Key slot of a public key or an asymmetric key pair.
540 * \param alg A signature algorithm that is compatible with
541 * the type of `key`.
542 * \param[in] p_hash The hash or message whose signature is to be
543 * verified.
544 * \param hash_length Size of the `p_hash` buffer in bytes.
545 * \param[in] p_signature Buffer containing the signature to verify.
546 * \param signature_length Size of the `p_signature` buffer in bytes.
547 *
548 * \retval PSA_SUCCESS
549 * The signature is valid.
550 */
551typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)( psa_key_slot_t key_slot,
552 psa_algorithm_t alg,
553 const uint8_t *p_hash,
554 size_t hash_length,
555 const uint8_t *p_signature,
556 size_t signature_length );
557
558/**
559 * \brief A function that encrypts a short message with a public key.
560 *
561 * \param key_slot Key slot of a public key or an asymmetric key pair.
562 * \param alg An asymmetric encryption algorithm that is
563 * compatible with the type of `key`.
564 * \param[in] p_input The message to encrypt.
565 * \param input_length Size of the `p_input` buffer in bytes.
566 * \param[in] p_salt A salt or label, if supported by the
567 * encryption algorithm.
568 * If the algorithm does not support a
569 * salt, pass `NULL`.
570 * If the algorithm supports an optional
571 * salt and you do not want to pass a salt,
572 * pass `NULL`.
573 *
574 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
575 * supported.
576 * \param salt_length Size of the `p_salt` buffer in bytes.
577 * If `p_salt` is `NULL`, pass 0.
578 * \param[out] p_output Buffer where the encrypted message is to
579 * be written.
580 * \param output_size Size of the `p_output` buffer in bytes.
581 * \param[out] p_output_length On success, the number of bytes
582 * that make up the returned output.
583 *
584 * \retval PSA_SUCCESS
585 */
586typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)( psa_key_slot_t key_slot,
587 psa_algorithm_t alg,
588 const uint8_t *p_input,
589 size_t input_length,
590 const uint8_t *p_salt,
591 size_t salt_length,
592 uint8_t *p_output,
593 size_t output_size,
594 size_t *p_output_length );
595
596/**
597 * \brief Decrypt a short message with a private key.
598 *
599 * \param key_slot Key slot of an asymmetric key pair.
600 * \param alg An asymmetric encryption algorithm that is
601 * compatible with the type of \p key.
602 * \param[in] p_input The message to decrypt.
603 * \param input_length Size of the `p_input` buffer in bytes.
604 * \param[in] p_salt A salt or label, if supported by the
605 * encryption algorithm.
606 * If the algorithm does not support a
607 * salt, pass `NULL`.
608 * If the algorithm supports an optional
609 * salt and you do not want to pass a salt,
610 * pass `NULL`.
611 *
612 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
613 * supported.
614 * \param salt_length Size of the `p_salt` buffer in bytes.
615 * If `p_salt` is `NULL`, pass 0.
616 * \param[out] p_output Buffer where the decrypted message is to
617 * be written.
618 * \param output_size Size of the `p_output` buffer in bytes.
619 * \param[out] p_output_length On success, the number of bytes
620 * that make up the returned output.
621 *
622 * \retval PSA_SUCCESS
623 */
624typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)( psa_key_slot_t key_slot,
625 psa_algorithm_t alg,
626 const uint8_t *p_input,
627 size_t input_length,
628 const uint8_t *p_salt,
629 size_t salt_length,
630 uint8_t *p_output,
631 size_t output_size,
632 size_t *p_output_length );
633
634/**
635 * \brief A struct containing all of the function pointers needed to implement asymmetric cryptographic operations
636 * using opaque keys.
637 *
638 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
639 *
640 * If one of the functions is not implemented, it should be set to NULL.
641 */
642struct pcd_asymmetric_opaque_t {
643 pcd_asymmetric_opaque_sign_t *p_sign; /**< Function that performs the asymmetric sign operation */
644 pcd_asymmetric_opaque_verify_t *p_verify; /**< Function that performs the asymmetric verify operation */
645 pcd_asymmetric_opaque_encrypt_t *p_encrypt; /**< Function that performs the asymmetric encrypt operation */
646 pcd_asymmetric_opaque_decrypt_t *p_decrypt; /**< Function that performs the asymmetric decrypt operation */
647};
648
649/** @}
650 */
651
652/** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography
653 * @{
654 */
655
656
657/**
658 * \brief A function that signs a hash or short message with a transparent private key.
659 *
660 * Functions that implement the prototype should be named in the following convention:
661 * ~~~~~~~~~~~~~{.c}
662 * pcd_asymmetric_<ALGO>_sign
663 * ~~~~~~~~~~~~~
664 * Where `ALGO` is the name of the signing algorithm
665 *
666 * \param p_key A buffer containing the private key material.
667 * \param key_size The size in bytes of the `p_key` data
668 * \param alg A signature algorithm that is compatible with
669 * the type of `p_key`.
670 * \param[in] p_hash The hash or message to sign.
671 * \param hash_length Size of the `p_hash` buffer in bytes.
672 * \param[out] p_signature Buffer where the signature is to be written.
673 * \param signature_size Size of the `p_signature` buffer in bytes.
674 * \param[out] p_signature_length On success, the number of bytes
675 * that make up the returned signature value.
676 *
677 * \retval PSA_SUCCESS
678 */
679typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)( const uint8_t *p_key,
680 size_t key_size,
681 psa_algorithm_t alg,
682 const uint8_t *p_hash,
683 size_t hash_length,
684 uint8_t *p_signature,
685 size_t signature_size,
686 size_t *p_signature_length );
687
688/**
689 * \brief A function that verifies the signature a hash or short message using a transparent public key.
690 *
691 * Functions that implement the prototype should be named in the following convention:
692 * ~~~~~~~~~~~~~{.c}
693 * pcd_asymmetric_<ALGO>_verify
694 * ~~~~~~~~~~~~~
695 * Where `ALGO` is the name of the signing algorithm
696 *
697 * \param p_key A buffer containing the public key material.
698 * \param key_size The size in bytes of the `p_key` data
699 * \param alg A signature algorithm that is compatible with
700 * the type of `key`.
701 * \param[in] p_hash The hash or message whose signature is to be
702 * verified.
703 * \param hash_length Size of the `p_hash` buffer in bytes.
704 * \param[in] p_signature Buffer containing the signature to verify.
705 * \param signature_length Size of the `p_signature` buffer in bytes.
706 *
707 * \retval PSA_SUCCESS
708 * The signature is valid.
709 */
710typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)( const uint8_t *p_key,
711 size_t key_size,
712 psa_algorithm_t alg,
713 const uint8_t *p_hash,
714 size_t hash_length,
715 const uint8_t *p_signature,
716 size_t signature_length );
717
718/**
719 * \brief A function that encrypts a short message with a transparent public key.
720 *
721 * Functions that implement the prototype should be named in the following convention:
722 * ~~~~~~~~~~~~~{.c}
723 * pcd_asymmetric_<ALGO>_encrypt
724 * ~~~~~~~~~~~~~
725 * Where `ALGO` is the name of the encryption algorithm
726 *
727 * \param p_key A buffer containing the public key material
728 * \param key_size The size in bytes of the `p_key` data
729 * \param alg An asymmetric encryption algorithm that is
730 * compatible with the type of `key`.
731 * \param[in] p_input The message to encrypt.
732 * \param input_length Size of the `p_input` buffer in bytes.
733 * \param[in] p_salt A salt or label, if supported by the
734 * encryption algorithm.
735 * If the algorithm does not support a
736 * salt, pass `NULL`.
737 * If the algorithm supports an optional
738 * salt and you do not want to pass a salt,
739 * pass `NULL`.
740 *
741 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
742 * supported.
743 * \param salt_length Size of the `p_salt` buffer in bytes.
744 * If `p_salt` is `NULL`, pass 0.
745 * \param[out] p_output Buffer where the encrypted message is to
746 * be written.
747 * \param output_size Size of the `p_output` buffer in bytes.
748 * \param[out] p_output_length On success, the number of bytes
749 * that make up the returned output.
750 *
751 * \retval PSA_SUCCESS
752 */
753typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)( const uint8_t *p_key,
754 size_t key_size,
755 psa_algorithm_t alg,
756 const uint8_t *p_input,
757 size_t input_length,
758 const uint8_t *p_salt,
759 size_t salt_length,
760 uint8_t *p_output,
761 size_t output_size,
762 size_t *p_output_length );
763
764/**
765 * \brief Decrypt a short message with a transparent private key.
766 *
767 * Functions that implement the prototype should be named in the following convention:
768 * ~~~~~~~~~~~~~{.c}
769 * pcd_asymmetric_<ALGO>_decrypt
770 * ~~~~~~~~~~~~~
771 * Where `ALGO` is the name of the encryption algorithm
772 *
773 * \param p_key A buffer containing the private key material
774 * \param key_size The size in bytes of the `p_key` data
775 * \param alg An asymmetric encryption algorithm that is
776 * compatible with the type of \p key.
777 * \param[in] p_input The message to decrypt.
778 * \param input_length Size of the `p_input` buffer in bytes.
779 * \param[in] p_salt A salt or label, if supported by the
780 * encryption algorithm.
781 * If the algorithm does not support a
782 * salt, pass `NULL`.
783 * If the algorithm supports an optional
784 * salt and you do not want to pass a salt,
785 * pass `NULL`.
786 *
787 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
788 * supported.
789 * \param salt_length Size of the `p_salt` buffer in bytes.
790 * If `p_salt` is `NULL`, pass 0.
791 * \param[out] p_output Buffer where the decrypted message is to
792 * be written.
793 * \param output_size Size of the `p_output` buffer in bytes.
794 * \param[out] p_output_length On success, the number of bytes
795 * that make up the returned output.
796 *
797 * \retval PSA_SUCCESS
798 */
799typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)( const uint8_t *p_key,
800 size_t key_size,
801 psa_algorithm_t alg,
802 const uint8_t *p_input,
803 size_t input_length,
804 const uint8_t *p_salt,
805 size_t salt_length,
806 uint8_t *p_output,
807 size_t output_size,
808 size_t *p_output_length );
809
810/** @}
811 */
812
813/** \defgroup aead_opaque AEAD Opaque
814 * * @{
815 */
816
817/** Process an authenticated encryption operation using an opaque key.
818 *
819 * \param key_slot Slot containing the key to use.
820 * \param algorithm The AEAD algorithm to compute
821 * (\c PSA_ALG_XXX value such that
822 * #PSA_ALG_IS_AEAD(\p alg) is true).
823 * \param[in] p_nonce Nonce or IV to use.
824 * \param nonce_length Size of the `p_nonce` buffer in bytes.
825 * \param[in] p_additional_data Additional data that will be authenticated
826 * but not encrypted.
827 * \param additional_data_length Size of `p_additional_data` in bytes.
828 * \param[in] p_plaintext Data that will be authenticated and
829 * encrypted.
830 * \param plaintext_length Size of `p_plaintext` in bytes.
831 * \param[out] p_ciphertext Output buffer for the authenticated and
832 * encrypted data. The additional data is not
833 * part of this output. For algorithms where the
834 * encrypted data and the authentication tag
835 * are defined as separate outputs, the
836 * authentication tag is appended to the
837 * encrypted data.
838 * \param ciphertext_size Size of the `p_ciphertext` buffer in bytes.
839 * \param[out] p_ciphertext_length On success, the size of the output
840 * in the `p_ciphertext` buffer.
841 *
842 * \retval #PSA_SUCCESS
843 * Success.
844 */
845typedef psa_status_t (*psa_aead_opaque_encrypt_t)( psa_key_slot_t key_slot,
846 psa_algorithm_t algorithm,
847 const uint8_t *p_nonce,
848 size_t nonce_length,
849 const uint8_t *p_additional_data,
850 size_t additional_data_length,
851 const uint8_t *p_plaintext,
852 size_t plaintext_length,
853 uint8_t *p_ciphertext,
854 size_t ciphertext_size,
855 size_t *p_ciphertext_length);
856
857/** Process an authenticated decryption operation using an opaque key.
858 *
859 * \param key_slot Slot containing the key to use.
860 * \param algorithm The AEAD algorithm to compute
861 * (\c PSA_ALG_XXX value such that
862 * #PSA_ALG_IS_AEAD(\p alg) is true).
863 * \param[in] p_nonce Nonce or IV to use.
864 * \param nonce_length Size of the `p_nonce` buffer in bytes.
865 * \param[in] p_additional_data Additional data that has been authenticated
866 * but not encrypted.
867 * \param additional_data_length Size of `p_additional_data` in bytes.
868 * \param[in] p_ciphertext Data that has been authenticated and
869 * encrypted. For algorithms where the
870 * encrypted data and the authentication tag
871 * are defined as separate inputs, the buffer
872 * must contain the encrypted data followed
873 * by the authentication tag.
874 * \param ciphertext_length Size of `p_ciphertext` in bytes.
875 * \param[out] p_plaintext Output buffer for the decrypted data.
876 * \param plaintext_size Size of the `p_plaintext` buffer in bytes.
877 * \param[out] p_plaintext_length On success, the size of the output
878 * in the `p_plaintext` buffer.
879 *
880 * \retval #PSA_SUCCESS
881 * Success.
882 */
883typedef psa_status_t (*psa_aead_opaque_decrypt_t)( psa_key_slot_t key_slot,
884 psa_algorithm_t algorithm,
885 const uint8_t *p_nonce,
886 size_t nonce_length,
887 const uint8_t *p_additional_data,
888 size_t additional_data_length,
889 const uint8_t *p_ciphertext,
890 size_t ciphertext_length,
891 uint8_t *p_plaintext,
892 size_t plaintext_size,
893 size_t *p_plaintext_length);
894
895/**
896 * \brief A struct containing all of the function pointers needed to implement Authenticated Encryption
897 * with Additional Data operations using opaque keys
898 *
899 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
900 *
901 * If one of the functions is not implemented, it should be set to NULL.
902 */
903struct psa_aead_opaque_t {
904 psa_aead_opaque_encrypt_t *p_encrypt; /**< Function that performs the AEAD encrypt operation */
905 psa_aead_opaque_decrypt_t *p_decrypt; /**< Function that performs the AEAD decrypt operation */
906};
907/** @}
908 */
909
910/** \defgroup aead_transparent AEAD Transparent
911 */
912
913/** Process an authenticated encryption operation.
914 *
915 * Functions that implement the prototype should be named in the following convention:
916 * ~~~~~~~~~~~~~{.c}
917 * pcd_aead_<ALGO>_encrypt
918 * ~~~~~~~~~~~~~
919 * Where `ALGO` is the name of the AEAD algorithm
920 *
921 * \param p_key A pointer to the key material
922 * \param key_length The size in bytes of the key material
923 * \param alg The AEAD algorithm to compute
924 * (\c PSA_ALG_XXX value such that
925 * #PSA_ALG_IS_AEAD(\p alg) is true).
926 * \param[in] nonce Nonce or IV to use.
927 * \param nonce_length Size of the \p nonce buffer in bytes.
928 * \param[in] additional_data Additional data that will be MACed
929 * but not encrypted.
930 * \param additional_data_length Size of \p additional_data in bytes.
931 * \param[in] plaintext Data that will be MACed and
932 * encrypted.
933 * \param plaintext_length Size of \p plaintext in bytes.
934 * \param[out] ciphertext Output buffer for the authenticated and
935 * encrypted data. The additional data is not
936 * part of this output. For algorithms where the
937 * encrypted data and the authentication tag
938 * are defined as separate outputs, the
939 * authentication tag is appended to the
940 * encrypted data.
941 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
942 * This must be at least
943 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
944 * \p plaintext_length).
945 * \param[out] ciphertext_length On success, the size of the output
946 * in the \b ciphertext buffer.
947 *
948 * \retval #PSA_SUCCESS
949
950 */
951typedef psa_status_t (*psa_aead_transparent_encrypt_t)( const uint8_t *p_key,
952 size_t key_length,
953 psa_algorithm_t alg,
954 const uint8_t *nonce,
955 size_t nonce_length,
956 const uint8_t *additional_data,
957 size_t additional_data_length,
958 const uint8_t *plaintext,
959 size_t plaintext_length,
960 uint8_t *ciphertext,
961 size_t ciphertext_size,
962 size_t *ciphertext_length );
963
964/** Process an authenticated decryption operation.
965 *
966 * Functions that implement the prototype should be named in the following convention:
967 * ~~~~~~~~~~~~~{.c}
968 * pcd_aead_<ALGO>_decrypt
969 * ~~~~~~~~~~~~~
970 * Where `ALGO` is the name of the AEAD algorithm
971 * \param p_key A pointer to the key material
972 * \param key_length The size in bytes of the key material
973 * \param alg The AEAD algorithm to compute
974 * (\c PSA_ALG_XXX value such that
975 * #PSA_ALG_IS_AEAD(\p alg) is true).
976 * \param[in] nonce Nonce or IV to use.
977 * \param nonce_length Size of the \p nonce buffer in bytes.
978 * \param[in] additional_data Additional data that has been MACed
979 * but not encrypted.
980 * \param additional_data_length Size of \p additional_data in bytes.
981 * \param[in] ciphertext Data that has been MACed and
982 * encrypted. For algorithms where the
983 * encrypted data and the authentication tag
984 * are defined as separate inputs, the buffer
985 * must contain the encrypted data followed
986 * by the authentication tag.
987 * \param ciphertext_length Size of \p ciphertext in bytes.
988 * \param[out] plaintext Output buffer for the decrypted data.
989 * \param plaintext_size Size of the \p plaintext buffer in bytes.
990 * This must be at least
991 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
992 * \p ciphertext_length).
993 * \param[out] plaintext_length On success, the size of the output
994 * in the \b plaintext buffer.
995 *
996 * \retval #PSA_SUCCESS
997 * Success.
998 */
999typedef psa_status_t (*psa_aead_transparent_decrypt_t) ( const uint8_t *p_key,
1000 size_t key_length,
1001 psa_algorithm_t alg,
1002 const uint8_t *nonce,
1003 size_t nonce_length,
1004 const uint8_t *additional_data,
1005 size_t additional_data_length,
1006 const uint8_t *ciphertext,
1007 size_t ciphertext_length,
1008 uint8_t *plaintext,
1009 size_t plaintext_size,
1010 size_t *plaintext_length);
1011
1012/** @}
1013
1014
1015/** \defgroup rng Entropy Generation
1016 * @{
1017 */
1018
1019/** \brief A hardware-specific structure for a entropy providing hardware
1020 */
1021struct pcd_entropy_context_t {
1022 // Implementation specific
1023};
1024
1025/** \brief Initialize an entropy driver
1026 *
1027 *
1028 * \param p_context A hardware-specific structure containing any context information for the implementation
1029 *
1030 * \retval PSA_SUCCESS
1031 */
1032typedef psa_status_t (*pcd_entropy_init_t)( struct pcd_entropy_context_t *p_context );
1033
1034/** \brief Get a specified number of bytes from the entropy source
1035 *
1036 * Retrives `buffer_size` bytes of data from the entropy source. The entropy source will always fill the provided buffer to its full size.
1037 * However, most entropy sources have biases, and the actual amount of entropy contained in the buffer will be less than the number of bytes.
1038 * The driver will return the actual number of bytes of entropy placed in the buffer in `p_received_entropy_bytes`.
1039 * A PSA Crypto API implementation will likely feed the output of this function into a Digital Random Bit Generator (DRBG), and typically has
1040 * a minimum amount of entropy that it needs.
1041 * To accomplish this, the PSA Crypto implementation should be designed to call this function multiple times until it has received the required
1042 * amount of entropy from the entropy source.
1043 *
1044 * \param p_context A hardware-specific structure containing any context information for the implementation
1045 * \param p_buffer A caller-allocated buffer for the retrieved bytes to be placed in
1046 * \param buffer_size The allocated size of `p_buffer`
1047 * \param p_received_entropy_bytes The amount of entropy (in bytes) actually provided in `p_buffer`
1048 *
1049 * \retval PSA_SUCCESS
1050 */
1051typedef psa_status_t (*pcd_entropy_get_bytes_t)( struct pcd_entropy_context_t *p_context, uint8_t *p_buffer, uint32_t buffer_size, uint32_t *p_received_entropy_bytes );
1052
1053/**
1054 * \brief A struct containing all of the function pointers needed to interface to an entropy source
1055 *
1056 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
1057 *
1058 * If one of the functions is not implemented, it should be set to NULL.
1059 */
1060struct pcd_entropy_t {
1061 pcd_entropy_init_t *p_init; /**< Function that performs initialization for the entropy source */
1062 pcd_entropy_get_bytes_t *p_get_bytes; /**< Function that performs the get_bytes operation for the entropy source */
1063};
1064/** @}
1065 */
1066
1067/** \defgroup key_management Key Management
1068 * @{
1069 */
1070
1071/** \brief Import a key in binary format.
1072 *
1073 * This function can support any output from psa_export_key(). Refer to the
1074 * documentation of psa_export_key() for the format for each key type.
1075 *
1076 * \param key_slot Slot where the key will be stored. This must be a
1077 * valid slot for a key of the chosen type. It must
1078 * be unoccupied.
1079 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1080 * \param[in] p_data Buffer containing the key data.
1081 * \param data_length Size of the \p data buffer in bytes.
1082 *
1083 * \retval #PSA_SUCCESS
1084 * Success.
1085 */
1086typedef psa_status_t (*pcd_opaque_import_key_t) ( psa_key_slot_t key_slot,
1087 psa_key_type_t type,
1088 const uint8_t *p_data,
1089 size_t data_length );
1090
1091/**
1092 * \brief Destroy a key and restore the slot to its default state.
1093 *
1094 * This function destroys the content of the key slot from both volatile
1095 * memory and, if applicable, non-volatile storage. Implementations shall
1096 * make a best effort to ensure that any previous content of the slot is
1097 * unrecoverable.
1098 *
1099 * This function also erases any metadata such as policies. It returns the
1100 * specified slot to its default state.
1101 *
1102 * \param key_slot The key slot to erase.
1103 *
1104 * \retval #PSA_SUCCESS
1105 * The slot's content, if any, has been erased.
1106 */
1107typedef psa_status_t (*pcd_destroy_key_t)( psa_key_slot_t key );
1108
1109/**
1110 * \brief Export a key in binary format.
1111 *
1112 * The output of this function can be passed to psa_import_key() to
1113 * create an equivalent object.
1114 *
1115 * If a key is created with psa_import_key() and then exported with
1116 * this function, it is not guaranteed that the resulting data is
1117 * identical: the implementation may choose a different representation
1118 * of the same key if the format permits it.
1119 *
1120 * For standard key types, the output format is as follows:
1121 *
1122 * - For symmetric keys (including MAC keys), the format is the
1123 * raw bytes of the key.
1124 * - For DES, the key data consists of 8 bytes. The parity bits must be
1125 * correct.
1126 * - For Triple-DES, the format is the concatenation of the
1127 * two or three DES keys.
1128 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1129 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1130 * as RSAPrivateKey.
1131 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1132 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1133 *
1134 * \param key Slot whose content is to be exported. This must
1135 * be an occupied key slot.
1136 * \param[out] p_data Buffer where the key data is to be written.
1137 * \param data_size Size of the `p_data` buffer in bytes.
1138 * \param[out] p_data_length On success, the number of bytes
1139 * that make up the key data.
1140 *
1141 * \retval #PSA_SUCCESS
1142 * \retval #PSA_ERROR_EMPTY_SLOT
1143 * \retval #PSA_ERROR_NOT_PERMITTED
1144 * \retval #PSA_ERROR_NOT_SUPPORTED
1145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1146 * \retval #PSA_ERROR_HARDWARE_FAILURE
1147 * \retval #PSA_ERROR_TAMPERING_DETECTED
1148 */
1149typedef psa_status_t (*pcd_export_key_t)( psa_key_slot_t key,
1150 uint8_t *p_data,
1151 size_t data_size,
1152 size_t *p_data_length );
1153
1154/**
1155 * \brief Export a public key or the public part of a key pair in binary format.
1156 *
1157 * The output of this function can be passed to psa_import_key() to
1158 * create an object that is equivalent to the public key.
1159 *
1160 * For standard key types, the output format is as follows:
1161 *
1162 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1163 * the format is the DER representation of the public key defined by RFC 5280
1164 * as SubjectPublicKeyInfo.
1165 *
1166 * \param key_slot Slot whose content is to be exported. This must
1167 * be an occupied key slot.
1168 * \param[out] p_data Buffer where the key data is to be written.
1169 * \param data_size Size of the \p data buffer in bytes.
1170 * \param[out] p_data_length On success, the number of bytes
1171 * that make up the key data.
1172 *
1173 * \retval #PSA_SUCCESS
1174 */
1175typedef psa_status_t (*pcd_export_public_key_t)( psa_key_slot_t key,
1176 uint8_t *p_data,
1177 size_t data_size,
1178 size_t *p_data_length );
1179
1180/**
1181 * \brief A struct containing all of the function pointers needed to for key management using
1182 * opaque keys.
1183 *
1184 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
1185 *
1186 * If one of the functions is not implemented, it should be set to NULL.
1187 */
1188struct pcd_key_management_t {
1189 pcd_opaque_import_key_t *p_import; /**< Function that performs the key import operation */
1190 pcd_destroy_key_t *p_destroy; /**< Function that performs the key destroy operation */
1191 pcd_export_key_t *p_export; /**< Function that performs the key export operation */
1192 pcd_export_public_key_t *p_export_public; /**< Function that perforsm the public key export operation */
1193};
1194
1195/** @}
1196 */
1197
1198/** \defgroup derivation Key Derivation and Agreement
1199 * @{
1200 * Key derivation is the process of generating new key material using an existing key and additional parameters, iterating through a basic
1201 * cryptographic function, such as a hash.
1202 * Key agreement is a part of cryptographic protocols that allows two parties to agree on the same key value, but starting from different original
1203 * key material.
1204 * The flows are similar, and the PSA Crypto Driver API uses the same functions for both of the flows.
1205 *
1206 * There are two different final functions for the flows, `pcd_key_derivation_derive` and `pcd_key_derivation_export`. `pcd_key_derivation_derive`
1207 * is used when the key material should be placed in a slot on the hardware and not exposed to the caller. `pcd_key_derivation_export` is used
1208 * when the key material should be returned to the PSA Cryptographic API implementation.
1209 *
1210 * Different key derivation algorithms require a different number of inputs. Instead of having an API that
1211 * takes as input variable length arrays, which can be problemmatic to manage on embedded platforms, the inputs
1212 * are passed to the driver via a function, `pcd_key_derivation_collateral`, that is called multiple times with different `collateral_id`s.
1213 * Thus, for a key derivation algorithm that required 3 paramter inputs, the flow would look something like:
1214```C
1215pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1216pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0, p_collateral_0, collateral_0_size);
1217pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1, p_collateral_1, collateral_1_size);
1218pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2, p_collateral_2, collateral_2_size);
1219pcd_key_derivation_derive();
1220```
1221
1222key agreement example:
1223```C
1224pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1225pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1226pcd_key_derivation_export(p_session_key, session_key_size, &session_key_length);
1227```
1228 */
1229
1230/** \brief Set up a key derivation operation by specifying the algorithm and the source key sot
1231 *
1232 * \param kdf_alg The algorithm to be used for the key derivation
1233 * \param souce_key The key to be used as the source material for the key derivation
1234 *
1235 * \retval PSA_SUCCESS
1236 */
1237typedef psa_status_t ( *pcd_key_derivation_setup_t )( psa_algorithm_t kdf_alg, psa_key_slot_t source_key );
1238
1239/** \brief Provide collateral (parameters) needed for a key derivation or key agreement operation
1240 *
1241 * Since many key derivation algorithms require multiple parameters, it is expeced that this function may be called multiple
1242 * times for the same operation, each with a different algorithm-specific `collateral_id`
1243 *
1244 * \param collateral_id
1245 * \param p_collateral
1246 * \param collateral_size
1247 *
1248 * \retval PSA_SUCCESS
1249 */
1250typedef psa_status_t (*pcd_key_derivation_collateral_t ) ( uint32_t collateral_id, const uint8_t p_collateral, uint32_t collateral_size );
1251
1252/** \brief Perform the final key derivation step and place the generated key material in a slot
1253 *
1254 * param dest_key The slot where the generated key material should be placed
1255 *
1256 * \retval PSA_SUCCESS
1257 */
1258typedef psa_status_t ( *pcd_key_derivation_derive_t )( psa_key_slot_t dest_key );
1259
1260/** \brief Pefform the final step of a key agreement and place the generated key material in a buffer
1261 *
1262 * \param p_output
1263 * \param output_size
1264 * \param p_output_length
1265 *
1266 * \retval PSA_SUCCESS
1267 */
1268typedef psa_status_t ( *pcd_key_derivation_export_t )( uint8_t *p_output, uint32_t output_size, uint32_t *p_output_length );
1269
1270/**
1271 * \brief A struct containing all of the function pointers needed to for key derivation and agreement
1272 *
1273 * PSA Crypto API implementations should populate instances of the table as appropriate upon startup.
1274 *
1275 * If one of the functions is not implemented, it should be set to NULL.
1276 */
1277struct pcd_key_derivation_t {
1278 pcd_key_derivation_setup_t *p_setup; /**< Function that performs the key derivation setup */
1279 pcd_key_derivation_collateral_t *p_collateral; /**< Function that sets the key derivation collateral */
1280 pcd_key_derivation_derive_t *p_derive; /**< Function that performs the final key derivation step */
1281 pcd_key_derivation_export_t *p_export; /**< Function that perforsm the final key derivation or agreement and exports the key */
1282};
1283
1284/** @}
1285 */
1286
1287#endif // __PSA_CRYPTO_DRIVER_H__