Mbed OS Reference
Loading...
Searching...
No Matches
crypto_se_driver.h
Go to the documentation of this file.
1/**
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.
7 * This is meant for cryptoprocessors that have a separate key storage from the
8 * space in which the PSA Crypto implementation runs, typically secure
9 * elements (SEs).
10 *
11 * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer),
12 * containing functions for driver developers to implement to enable hardware
13 * to be called in a standardized way by a PSA Cryptography API
14 * implementation. The functions comprising the driver HAL, which driver
15 * authors implement, are not intended to be called by application developers.
16 */
17
18/*
19 * Copyright The Mbed TLS Contributors
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
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/** \defgroup se_init Secure element driver initialization
44 \ingroup experimental-crypto-psa
45 */
46/**@{*/
47
48/** \brief Driver context structure
49 *
50 * Driver functions receive a pointer to this structure.
51 * Each registered driver has one instance of this structure.
52 *
53 * Implementations must include the fields specified here and
54 * may include other fields.
55 */
56typedef struct {
57 /** A read-only pointer to the driver's persistent data.
58 *
59 * Drivers typically use this persistent data to keep track of
60 * which slot numbers are available. This is only a guideline:
61 * drivers may use the persistent data for any purpose, keeping
62 * in mind the restrictions on when the persistent data is saved
63 * to storage: the persistent data is only saved after calling
64 * certain functions that receive a writable pointer to the
65 * persistent data.
66 *
67 * The core allocates a memory buffer for the persistent data.
68 * The pointer is guaranteed to be suitably aligned for any data type,
69 * like a pointer returned by `malloc` (but the core can use any
70 * method to allocate the buffer, not necessarily `malloc`).
71 *
72 * The size of this buffer is in the \c persistent_data_size field of
73 * this structure.
74 *
75 * Before the driver is initialized for the first time, the content of
76 * the persistent data is all-bits-zero. After a driver upgrade, if the
77 * size of the persistent data has increased, the original data is padded
78 * on the right with zeros; if the size has decreased, the original data
79 * is truncated to the new size.
80 *
81 * This pointer is to read-only data. Only a few driver functions are
82 * allowed to modify the persistent data. These functions receive a
83 * writable pointer. These functions are:
84 * - psa_drv_se_t::p_init
85 * - psa_drv_se_key_management_t::p_allocate
86 * - psa_drv_se_key_management_t::p_destroy
87 *
88 * The PSA Cryptography core saves the persistent data from one
89 * session to the next. It does this before returning from API functions
90 * that call a driver method that is allowed to modify the persistent
91 * data, specifically:
92 * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
93 * psa_drv_se_key_management_t::p_destroy to complete an action
94 * that was interrupted by a power failure.
95 * - Key creation functions cause a call to
96 * psa_drv_se_key_management_t::p_allocate, and may cause a call to
97 * psa_drv_se_key_management_t::p_destroy in case an error occurs.
98 * - psa_destroy_key() causes a call to
99 * psa_drv_se_key_management_t::p_destroy.
100 */
101 const void *const persistent_data;
102
103 /** The size of \c persistent_data in bytes.
104 *
105 * This is always equal to the value of the `persistent_data_size` field
106 * of the ::psa_drv_se_t structure when the driver is registered.
107 */
109
110 /** Driver transient data.
111 *
112 * The core initializes this value to 0 and does not read or modify it
113 * afterwards. The driver may store whatever it wants in this field.
114 */
115 uintptr_t transient_data;
117
118/** \brief A driver initialization function.
119 *
120 * \param[in,out] drv_context The driver context structure.
121 * \param[in,out] persistent_data A pointer to the persistent data
122 * that allows writing.
123 * \param location The location value for which this driver
124 * is registered. The driver will be invoked
125 * for all keys whose lifetime is in this
126 * location.
127 *
128 * \retval #PSA_SUCCESS
129 * The driver is operational.
130 * The core will update the persistent data in storage.
131 * \return
132 * Any other return value prevents the driver from being used in
133 * this session.
134 * The core will NOT update the persistent data in storage.
135 */
137 void *persistent_data,
138 psa_key_location_t location);
139
140#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
141/* Mbed Crypto with secure element support enabled defines this type in
142 * crypto_types.h because it is also visible to applications through an
143 * implementation-specific extension.
144 * For the PSA Cryptography specification, this type is only visible
145 * via crypto_se_driver.h. */
146/** An internal designation of a key slot between the core part of the
147 * PSA Crypto implementation and the driver. The meaning of this value
148 * is driver-dependent. */
149typedef uint64_t psa_key_slot_number_t;
150#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
151
152/**@}*/
153
154/** \defgroup se_mac Secure Element Message Authentication Codes
155 * \ingroup experimental-crypto-psa
156 * Generation and authentication of Message Authentication Codes (MACs) using
157 * a secure element can be done either as a single function call (via the
158 * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
159 * parts using the following sequence:
160 * - `psa_drv_se_mac_setup_t`
161 * - `psa_drv_se_mac_update_t`
162 * - `psa_drv_se_mac_update_t`
163 * - ...
164 * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
165 *
166 * If a previously started secure element MAC operation needs to be terminated,
167 * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may
168 * result in allocated resources not being freed or in other undefined
169 * behavior.
170 */
171/**@{*/
172/** \brief A function that starts a secure element MAC operation for a PSA
173 * Crypto Driver implementation
174 *
175 * \param[in,out] drv_context The driver context structure.
176 * \param[in,out] op_context A structure that will contain the
177 * hardware-specific MAC context
178 * \param[in] key_slot The slot of the key to be used for the
179 * operation
180 * \param[in] algorithm The algorithm to be used to underly the MAC
181 * operation
182 *
183 * \retval #PSA_SUCCESS
184 * Success.
185 */
187 void *op_context,
188 psa_key_slot_number_t key_slot,
189 psa_algorithm_t algorithm);
190
191/** \brief A function that continues a previously started secure element MAC
192 * operation
193 *
194 * \param[in,out] op_context A hardware-specific structure for the
195 * previously-established MAC operation to be
196 * updated
197 * \param[in] p_input A buffer containing the message to be appended
198 * to the MAC operation
199 * \param[in] input_length The size in bytes of the input message buffer
200 */
201typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
202 const uint8_t *p_input,
203 size_t input_length);
204
205/** \brief a function that completes a previously started secure element MAC
206 * operation by returning the resulting MAC.
207 *
208 * \param[in,out] op_context A hardware-specific structure for the
209 * previously started MAC operation to be
210 * finished
211 * \param[out] p_mac A buffer where the generated MAC will be
212 * placed
213 * \param[in] mac_size The size in bytes of the buffer that has been
214 * allocated for the `output` buffer
215 * \param[out] p_mac_length After completion, will contain the number of
216 * bytes placed in the `p_mac` buffer
217 *
218 * \retval #PSA_SUCCESS
219 * Success.
220 */
221typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
222 uint8_t *p_mac,
223 size_t mac_size,
224 size_t *p_mac_length);
225
226/** \brief A function that completes a previously started secure element MAC
227 * operation by comparing the resulting MAC against a provided value
228 *
229 * \param[in,out] op_context A hardware-specific structure for the previously
230 * started MAC operation to be fiinished
231 * \param[in] p_mac The MAC value against which the resulting MAC
232 * will be compared against
233 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
234 *
235 * \retval #PSA_SUCCESS
236 * The operation completed successfully and the MACs matched each
237 * other
238 * \retval #PSA_ERROR_INVALID_SIGNATURE
239 * The operation completed successfully, but the calculated MAC did
240 * not match the provided MAC
241 */
243 const uint8_t *p_mac,
244 size_t mac_length);
245
246/** \brief A function that aborts a previous started secure element MAC
247 * operation
248 *
249 * \param[in,out] op_context A hardware-specific structure for the previously
250 * started MAC operation to be aborted
251 */
252typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
253
254/** \brief A function that performs a secure element MAC operation in one
255 * command and returns the calculated MAC
256 *
257 * \param[in,out] drv_context The driver context structure.
258 * \param[in] p_input A buffer containing the message to be MACed
259 * \param[in] input_length The size in bytes of `p_input`
260 * \param[in] key_slot The slot of the key to be used
261 * \param[in] alg The algorithm to be used to underlie the MAC
262 * operation
263 * \param[out] p_mac A buffer where the generated MAC will be
264 * placed
265 * \param[in] mac_size The size in bytes of the `p_mac` buffer
266 * \param[out] p_mac_length After completion, will contain the number of
267 * bytes placed in the `output` buffer
268 *
269 * \retval #PSA_SUCCESS
270 * Success.
271 */
273 const uint8_t *p_input,
274 size_t input_length,
275 psa_key_slot_number_t key_slot,
276 psa_algorithm_t alg,
277 uint8_t *p_mac,
278 size_t mac_size,
279 size_t *p_mac_length);
280
281/** \brief A function that performs a secure element MAC operation in one
282 * command and compares the resulting MAC against a provided value
283 *
284 * \param[in,out] drv_context The driver context structure.
285 * \param[in] p_input A buffer containing the message to be MACed
286 * \param[in] input_length The size in bytes of `input`
287 * \param[in] key_slot The slot of the key to be used
288 * \param[in] alg The algorithm to be used to underlie the MAC
289 * operation
290 * \param[in] p_mac The MAC value against which the resulting MAC will
291 * be compared against
292 * \param[in] mac_length The size in bytes of `mac`
293 *
294 * \retval #PSA_SUCCESS
295 * The operation completed successfully and the MACs matched each
296 * other
297 * \retval #PSA_ERROR_INVALID_SIGNATURE
298 * The operation completed successfully, but the calculated MAC did
299 * not match the provided MAC
300 */
302 const uint8_t *p_input,
303 size_t input_length,
304 psa_key_slot_number_t key_slot,
305 psa_algorithm_t alg,
306 const uint8_t *p_mac,
307 size_t mac_length);
308
309/** \brief A struct containing all of the function pointers needed to
310 * perform secure element MAC operations
311 *
312 * PSA Crypto API implementations should populate the table as appropriate
313 * upon startup.
314 *
315 * If one of the functions is not implemented (such as
316 * `psa_drv_se_mac_generate_t`), it should be set to NULL.
317 *
318 * Driver implementers should ensure that they implement all of the functions
319 * that make sense for their hardware, and that they provide a full solution
320 * (for example, if they support `p_setup`, they should also support
321 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
322 *
323 */
324typedef struct {
325 /**The size in bytes of the hardware-specific secure element MAC context
326 * structure
327 */
329 /** Function that performs a MAC setup operation
330 */
332 /** Function that performs a MAC update operation
333 */
335 /** Function that completes a MAC operation
336 */
338 /** Function that completes a MAC operation with a verify check
339 */
341 /** Function that aborts a previoustly started MAC operation
342 */
344 /** Function that performs a MAC operation in one call
345 */
347 /** Function that performs a MAC and verify operation in one call
348 */
351/**@}*/
352
353/** \defgroup se_cipher Secure Element Symmetric Ciphers
354 * \ingroup experimental-crypto-psa
355 *
356 * Encryption and Decryption using secure element keys in block modes other
357 * than ECB must be done in multiple parts, using the following flow:
358 * - `psa_drv_se_cipher_setup_t`
359 * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
360 * - `psa_drv_se_cipher_update_t`
361 * - `psa_drv_se_cipher_update_t`
362 * - ...
363 * - `psa_drv_se_cipher_finish_t`
364 *
365 * If a previously started secure element Cipher operation needs to be
366 * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
367 * to do so may result in allocated resources not being freed or in other
368 * undefined behavior.
369 *
370 * In situations where a PSA Cryptographic API implementation is using a block
371 * mode not-supported by the underlying hardware or driver, it can construct
372 * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
373 * for the cipher operations.
374 */
375/**@{*/
376
377/** \brief A function that provides the cipher setup function for a
378 * secure element driver
379 *
380 * \param[in,out] drv_context The driver context structure.
381 * \param[in,out] op_context A structure that will contain the
382 * hardware-specific cipher context.
383 * \param[in] key_slot The slot of the key to be used for the
384 * operation
385 * \param[in] algorithm The algorithm to be used in the cipher
386 * operation
387 * \param[in] direction Indicates whether the operation is an encrypt
388 * or decrypt
389 *
390 * \retval #PSA_SUCCESS
391 * \retval #PSA_ERROR_NOT_SUPPORTED
392 */
394 void *op_context,
395 psa_key_slot_number_t key_slot,
396 psa_algorithm_t algorithm,
397 psa_encrypt_or_decrypt_t direction);
398
399/** \brief A function that sets the initialization vector (if
400 * necessary) for an secure element cipher operation
401 *
402 * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
403 * two IV functions: one to set the IV, and one to generate it internally. The
404 * generate function is not necessary for the drivers to implement as the PSA
405 * Crypto implementation can do the generation using its RNG features.
406 *
407 * \param[in,out] op_context A structure that contains the previously set up
408 * hardware-specific cipher context
409 * \param[in] p_iv A buffer containing the initialization vector
410 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
411 *
412 * \retval #PSA_SUCCESS
413 */
414typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
415 const uint8_t *p_iv,
416 size_t iv_length);
417
418/** \brief A function that continues a previously started secure element cipher
419 * operation
420 *
421 * \param[in,out] op_context A hardware-specific structure for the
422 * previously started cipher operation
423 * \param[in] p_input A buffer containing the data to be
424 * encrypted/decrypted
425 * \param[in] input_size The size in bytes of the buffer pointed to
426 * by `p_input`
427 * \param[out] p_output The caller-allocated buffer where the
428 * output will be placed
429 * \param[in] output_size The allocated size in bytes of the
430 * `p_output` buffer
431 * \param[out] p_output_length After completion, will contain the number
432 * of bytes placed in the `p_output` buffer
433 *
434 * \retval #PSA_SUCCESS
435 */
436typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
437 const uint8_t *p_input,
438 size_t input_size,
439 uint8_t *p_output,
440 size_t output_size,
441 size_t *p_output_length);
442
443/** \brief A function that completes a previously started secure element cipher
444 * operation
445 *
446 * \param[in,out] op_context A hardware-specific structure for the
447 * previously started cipher operation
448 * \param[out] p_output The caller-allocated buffer where the output
449 * will be placed
450 * \param[in] output_size The allocated size in bytes of the `p_output`
451 * buffer
452 * \param[out] p_output_length After completion, will contain the number of
453 * bytes placed in the `p_output` buffer
454 *
455 * \retval #PSA_SUCCESS
456 */
457typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
458 uint8_t *p_output,
459 size_t output_size,
460 size_t *p_output_length);
461
462/** \brief A function that aborts a previously started secure element cipher
463 * operation
464 *
465 * \param[in,out] op_context A hardware-specific structure for the
466 * previously started cipher operation
467 */
468typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
469
470/** \brief A function that performs the ECB block mode for secure element
471 * cipher operations
472 *
473 * Note: this function should only be used with implementations that do not
474 * provide a needed higher-level operation.
475 *
476 * \param[in,out] drv_context The driver context structure.
477 * \param[in] key_slot The slot of the key to be used for the operation
478 * \param[in] algorithm The algorithm to be used in the cipher operation
479 * \param[in] direction Indicates whether the operation is an encrypt or
480 * decrypt
481 * \param[in] p_input A buffer containing the data to be
482 * encrypted/decrypted
483 * \param[in] input_size The size in bytes of the buffer pointed to by
484 * `p_input`
485 * \param[out] p_output The caller-allocated buffer where the output
486 * will be placed
487 * \param[in] output_size The allocated size in bytes of the `p_output`
488 * buffer
489 *
490 * \retval #PSA_SUCCESS
491 * \retval #PSA_ERROR_NOT_SUPPORTED
492 */
494 psa_key_slot_number_t key_slot,
495 psa_algorithm_t algorithm,
496 psa_encrypt_or_decrypt_t direction,
497 const uint8_t *p_input,
498 size_t input_size,
499 uint8_t *p_output,
500 size_t output_size);
501
502/**
503 * \brief A struct containing all of the function pointers needed to implement
504 * cipher operations using secure elements.
505 *
506 * PSA Crypto API implementations should populate instances of the table as
507 * appropriate upon startup or at build time.
508 *
509 * If one of the functions is not implemented (such as
510 * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
511 */
512typedef struct {
513 /** The size in bytes of the hardware-specific secure element cipher
514 * context structure
515 */
517 /** Function that performs a cipher setup operation */
519 /** Function that sets a cipher IV (if necessary) */
521 /** Function that performs a cipher update operation */
523 /** Function that completes a cipher operation */
525 /** Function that aborts a cipher operation */
527 /** Function that performs ECB mode for a cipher operation
528 * (Danger: ECB mode should not be used directly by clients of the PSA
529 * Crypto Client API)
530 */
533
534/**@}*/
535
536/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
537 * \ingroup experimental-crypto-psa
538 *
539 * Since the amount of data that can (or should) be encrypted or signed using
540 * asymmetric keys is limited by the key size, asymmetric key operations using
541 * keys in a secure element must be done in single function calls.
542 */
543/**@{*/
544
545/**
546 * \brief A function that signs a hash or short message with a private key in
547 * a secure element
548 *
549 * \param[in,out] drv_context The driver context structure.
550 * \param[in] key_slot Key slot of an asymmetric key pair
551 * \param[in] alg A signature algorithm that is compatible
552 * with the type of `key`
553 * \param[in] p_hash The hash to sign
554 * \param[in] hash_length Size of the `p_hash` buffer in bytes
555 * \param[out] p_signature Buffer where the signature is to be written
556 * \param[in] signature_size Size of the `p_signature` buffer in bytes
557 * \param[out] p_signature_length On success, the number of bytes
558 * that make up the returned signature value
559 *
560 * \retval #PSA_SUCCESS
561 */
563 psa_key_slot_number_t key_slot,
564 psa_algorithm_t alg,
565 const uint8_t *p_hash,
566 size_t hash_length,
567 uint8_t *p_signature,
568 size_t signature_size,
569 size_t *p_signature_length);
570
571/**
572 * \brief A function that verifies the signature a hash or short message using
573 * an asymmetric public key in a secure element
574 *
575 * \param[in,out] drv_context The driver context structure.
576 * \param[in] key_slot Key slot of a public key or an asymmetric key
577 * pair
578 * \param[in] alg A signature algorithm that is compatible with
579 * the type of `key`
580 * \param[in] p_hash The hash whose signature is to be verified
581 * \param[in] hash_length Size of the `p_hash` buffer in bytes
582 * \param[in] p_signature Buffer containing the signature to verify
583 * \param[in] signature_length Size of the `p_signature` buffer in bytes
584 *
585 * \retval #PSA_SUCCESS
586 * The signature is valid.
587 */
589 psa_key_slot_number_t key_slot,
590 psa_algorithm_t alg,
591 const uint8_t *p_hash,
592 size_t hash_length,
593 const uint8_t *p_signature,
594 size_t signature_length);
595
596/**
597 * \brief A function that encrypts a short message with an asymmetric public
598 * key in a secure element
599 *
600 * \param[in,out] drv_context The driver context structure.
601 * \param[in] key_slot Key slot of a public key or an asymmetric key
602 * pair
603 * \param[in] alg An asymmetric encryption algorithm that is
604 * compatible with the type of `key`
605 * \param[in] p_input The message to encrypt
606 * \param[in] input_length Size of the `p_input` buffer in bytes
607 * \param[in] p_salt A salt or label, if supported by the
608 * encryption algorithm
609 * If the algorithm does not support a
610 * salt, pass `NULL`.
611 * If the algorithm supports an optional
612 * salt and you do not want to pass a salt,
613 * pass `NULL`.
614 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
615 * supported.
616 * \param[in] salt_length Size of the `p_salt` buffer in bytes
617 * If `p_salt` is `NULL`, pass 0.
618 * \param[out] p_output Buffer where the encrypted message is to
619 * be written
620 * \param[in] output_size Size of the `p_output` buffer in bytes
621 * \param[out] p_output_length On success, the number of bytes that make up
622 * the returned output
623 *
624 * \retval #PSA_SUCCESS
625 */
627 psa_key_slot_number_t key_slot,
628 psa_algorithm_t alg,
629 const uint8_t *p_input,
630 size_t input_length,
631 const uint8_t *p_salt,
632 size_t salt_length,
633 uint8_t *p_output,
634 size_t output_size,
635 size_t *p_output_length);
636
637/**
638 * \brief A function that decrypts a short message with an asymmetric private
639 * key in a secure element.
640 *
641 * \param[in,out] drv_context The driver context structure.
642 * \param[in] key_slot Key slot of an asymmetric key pair
643 * \param[in] alg An asymmetric encryption algorithm that is
644 * compatible with the type of `key`
645 * \param[in] p_input The message to decrypt
646 * \param[in] input_length Size of the `p_input` buffer in bytes
647 * \param[in] p_salt A salt or label, if supported by the
648 * encryption algorithm
649 * If the algorithm does not support a
650 * salt, pass `NULL`.
651 * If the algorithm supports an optional
652 * salt and you do not want to pass a salt,
653 * pass `NULL`.
654 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
655 * supported.
656 * \param[in] salt_length Size of the `p_salt` buffer in bytes
657 * If `p_salt` is `NULL`, pass 0.
658 * \param[out] p_output Buffer where the decrypted message is to
659 * be written
660 * \param[in] output_size Size of the `p_output` buffer in bytes
661 * \param[out] p_output_length On success, the number of bytes
662 * that make up the returned output
663 *
664 * \retval #PSA_SUCCESS
665 */
667 psa_key_slot_number_t key_slot,
668 psa_algorithm_t alg,
669 const uint8_t *p_input,
670 size_t input_length,
671 const uint8_t *p_salt,
672 size_t salt_length,
673 uint8_t *p_output,
674 size_t output_size,
675 size_t *p_output_length);
676
677/**
678 * \brief A struct containing all of the function pointers needed to implement
679 * asymmetric cryptographic operations using secure elements.
680 *
681 * PSA Crypto API implementations should populate instances of the table as
682 * appropriate upon startup or at build time.
683 *
684 * If one of the functions is not implemented, it should be set to NULL.
685 */
686typedef struct {
687 /** Function that performs an asymmetric sign operation */
689 /** Function that performs an asymmetric verify operation */
691 /** Function that performs an asymmetric encrypt operation */
693 /** Function that performs an asymmetric decrypt operation */
696
697/**@}*/
698
699/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
700 * \ingroup experimental-crypto-psa
701 * Authenticated Encryption with Additional Data (AEAD) operations with secure
702 * elements must be done in one function call. While this creates a burden for
703 * implementers as there must be sufficient space in memory for the entire
704 * message, it prevents decrypted data from being made available before the
705 * authentication operation is complete and the data is known to be authentic.
706 */
707/**@{*/
708
709/** \brief A function that performs a secure element authenticated encryption
710 * operation
711 *
712 * \param[in,out] drv_context The driver context structure.
713 * \param[in] key_slot Slot containing the key to use.
714 * \param[in] algorithm The AEAD algorithm to compute
715 * (\c PSA_ALG_XXX value such that
716 * #PSA_ALG_IS_AEAD(`alg`) is true)
717 * \param[in] p_nonce Nonce or IV to use
718 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
719 * \param[in] p_additional_data Additional data that will be
720 * authenticated but not encrypted
721 * \param[in] additional_data_length Size of `p_additional_data` in bytes
722 * \param[in] p_plaintext Data that will be authenticated and
723 * encrypted
724 * \param[in] plaintext_length Size of `p_plaintext` in bytes
725 * \param[out] p_ciphertext Output buffer for the authenticated and
726 * encrypted data. The additional data is
727 * not part of this output. For algorithms
728 * where the encrypted data and the
729 * authentication tag are defined as
730 * separate outputs, the authentication
731 * tag is appended to the encrypted data.
732 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
733 * bytes
734 * \param[out] p_ciphertext_length On success, the size of the output in
735 * the `p_ciphertext` buffer
736 *
737 * \retval #PSA_SUCCESS
738 * Success.
739 */
741 psa_key_slot_number_t key_slot,
742 psa_algorithm_t algorithm,
743 const uint8_t *p_nonce,
744 size_t nonce_length,
745 const uint8_t *p_additional_data,
746 size_t additional_data_length,
747 const uint8_t *p_plaintext,
748 size_t plaintext_length,
749 uint8_t *p_ciphertext,
750 size_t ciphertext_size,
751 size_t *p_ciphertext_length);
752
753/** A function that peforms a secure element authenticated decryption operation
754 *
755 * \param[in,out] drv_context The driver context structure.
756 * \param[in] key_slot Slot containing the key to use
757 * \param[in] algorithm The AEAD algorithm to compute
758 * (\c PSA_ALG_XXX value such that
759 * #PSA_ALG_IS_AEAD(`alg`) is true)
760 * \param[in] p_nonce Nonce or IV to use
761 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
762 * \param[in] p_additional_data Additional data that has been
763 * authenticated but not encrypted
764 * \param[in] additional_data_length Size of `p_additional_data` in bytes
765 * \param[in] p_ciphertext Data that has been authenticated and
766 * encrypted.
767 * For algorithms where the encrypted data
768 * and the authentication tag are defined
769 * as separate inputs, the buffer must
770 * contain the encrypted data followed by
771 * the authentication tag.
772 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
773 * \param[out] p_plaintext Output buffer for the decrypted data
774 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
775 * bytes
776 * \param[out] p_plaintext_length On success, the size of the output in
777 * the `p_plaintext` buffer
778 *
779 * \retval #PSA_SUCCESS
780 * Success.
781 */
783 psa_key_slot_number_t key_slot,
784 psa_algorithm_t algorithm,
785 const uint8_t *p_nonce,
786 size_t nonce_length,
787 const uint8_t *p_additional_data,
788 size_t additional_data_length,
789 const uint8_t *p_ciphertext,
790 size_t ciphertext_length,
791 uint8_t *p_plaintext,
792 size_t plaintext_size,
793 size_t *p_plaintext_length);
794
795/**
796 * \brief A struct containing all of the function pointers needed to implement
797 * secure element Authenticated Encryption with Additional Data operations
798 *
799 * PSA Crypto API implementations should populate instances of the table as
800 * appropriate upon startup.
801 *
802 * If one of the functions is not implemented, it should be set to NULL.
803 */
804typedef struct {
805 /** Function that performs the AEAD encrypt operation */
807 /** Function that performs the AEAD decrypt operation */
810/**@}*/
811
812/** \defgroup se_key_management Secure Element Key Management
813 * \ingroup experimental-crypto-psa
814 * Currently, key management is limited to importing keys in the clear,
815 * destroying keys, and exporting keys in the clear.
816 * Whether a key may be exported is determined by the key policies in place
817 * on the key slot.
818 */
819/**@{*/
820
821/** An enumeration indicating how a key is created.
822 */
823typedef enum
824{
825 PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */
826 PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */
827 PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */
828 PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */
829
830#ifndef __DOXYGEN_ONLY__
831 /** A key is being registered with mbedtls_psa_register_se_key().
832 *
833 * The core only passes this value to
834 * psa_drv_se_key_management_t::p_validate_slot_number, not to
835 * psa_drv_se_key_management_t::p_allocate. The call to
836 * `p_validate_slot_number` is not followed by any other call to the
837 * driver: the key is considered successfully registered if the call to
838 * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is
839 * null.
840 *
841 * With this creation method, the driver must return #PSA_SUCCESS if
842 * the given attributes are compatible with the existing key in the slot,
843 * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there
844 * is no key with the specified slot number.
845 *
846 * This is an Mbed Crypto extension.
847 */
849#endif
851
852/** \brief A function that allocates a slot for a key.
853 *
854 * To create a key in a specific slot in a secure element, the core
855 * first calls this function to determine a valid slot number,
856 * then calls a function to create the key material in that slot.
857 * In nominal conditions (that is, if no error occurs),
858 * the effect of a call to a key creation function in the PSA Cryptography
859 * API with a lifetime that places the key in a secure element is the
860 * following:
861 * -# The core calls psa_drv_se_key_management_t::p_allocate
862 * (or in some implementations
863 * psa_drv_se_key_management_t::p_validate_slot_number). The driver
864 * selects (or validates) a suitable slot number given the key attributes
865 * and the state of the secure element.
866 * -# The core calls a key creation function in the driver.
867 *
868 * The key creation functions in the PSA Cryptography API are:
869 * - psa_import_key(), which causes
870 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT
871 * then a call to psa_drv_se_key_management_t::p_import.
872 * - psa_generate_key(), which causes
873 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE
874 * then a call to psa_drv_se_key_management_t::p_import.
875 * - psa_key_derivation_output_key(), which causes
876 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE
877 * then a call to psa_drv_se_key_derivation_t::p_derive.
878 * - psa_copy_key(), which causes
879 * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY
880 * then a call to psa_drv_se_key_management_t::p_export.
881 *
882 * In case of errors, other behaviors are possible.
883 * - If the PSA Cryptography subsystem dies after the first step,
884 * for example because the device has lost power abruptly,
885 * the second step may never happen, or may happen after a reset
886 * and re-initialization. Alternatively, after a reset and
887 * re-initialization, the core may call
888 * psa_drv_se_key_management_t::p_destroy on the slot number that
889 * was allocated (or validated) instead of calling a key creation function.
890 * - If an error occurs, the core may call
891 * psa_drv_se_key_management_t::p_destroy on the slot number that
892 * was allocated (or validated) instead of calling a key creation function.
893 *
894 * Errors and system resets also have an impact on the driver's persistent
895 * data. If a reset happens before the overall key creation process is
896 * completed (before or after the second step above), it is unspecified
897 * whether the persistent data after the reset is identical to what it
898 * was before or after the call to `p_allocate` (or `p_validate_slot_number`).
899 *
900 * \param[in,out] drv_context The driver context structure.
901 * \param[in,out] persistent_data A pointer to the persistent data
902 * that allows writing.
903 * \param[in] attributes Attributes of the key.
904 * \param method The way in which the key is being created.
905 * \param[out] key_slot Slot where the key will be stored.
906 * This must be a valid slot for a key of the
907 * chosen type. It must be unoccupied.
908 *
909 * \retval #PSA_SUCCESS
910 * Success.
911 * The core will record \c *key_slot as the key slot where the key
912 * is stored and will update the persistent data in storage.
913 * \retval #PSA_ERROR_NOT_SUPPORTED
914 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
915 */
917 psa_drv_se_context_t *drv_context,
918 void *persistent_data,
919 const psa_key_attributes_t *attributes,
921 psa_key_slot_number_t *key_slot);
922
923/** \brief A function that determines whether a slot number is valid
924 * for a key.
925 *
926 * To create a key in a specific slot in a secure element, the core
927 * first calls this function to validate the choice of slot number,
928 * then calls a function to create the key material in that slot.
929 * See the documentation of #psa_drv_se_allocate_key_t for more details.
930 *
931 * As of the PSA Cryptography API specification version 1.0, there is no way
932 * for applications to trigger a call to this function. However some
933 * implementations offer the capability to create or declare a key in
934 * a specific slot via implementation-specific means, generally for the
935 * sake of initial device provisioning or onboarding. Such a mechanism may
936 * be added to a future version of the PSA Cryptography API specification.
937 *
938 * This function may update the driver's persistent data through
939 * \p persistent_data. The core will save the updated persistent data at the
940 * end of the key creation process. See the description of
941 * ::psa_drv_se_allocate_key_t for more information.
942 *
943 * \param[in,out] drv_context The driver context structure.
944 * \param[in,out] persistent_data A pointer to the persistent data
945 * that allows writing.
946 * \param[in] attributes Attributes of the key.
947 * \param method The way in which the key is being created.
948 * \param[in] key_slot Slot where the key is to be stored.
949 *
950 * \retval #PSA_SUCCESS
951 * The given slot number is valid for a key with the given
952 * attributes.
953 * \retval #PSA_ERROR_INVALID_ARGUMENT
954 * The given slot number is not valid for a key with the
955 * given attributes. This includes the case where the slot
956 * number is not valid at all.
957 * \retval #PSA_ERROR_ALREADY_EXISTS
958 * There is already a key with the specified slot number.
959 * Drivers may choose to return this error from the key
960 * creation function instead.
961 */
963 psa_drv_se_context_t *drv_context,
964 void *persistent_data,
965 const psa_key_attributes_t *attributes,
967 psa_key_slot_number_t key_slot);
968
969/** \brief A function that imports a key into a secure element in binary format
970 *
971 * This function can support any output from psa_export_key(). Refer to the
972 * documentation of psa_export_key() for the format for each key type.
973 *
974 * \param[in,out] drv_context The driver context structure.
975 * \param key_slot Slot where the key will be stored.
976 * This must be a valid slot for a key of the
977 * chosen type. It must be unoccupied.
978 * \param[in] attributes The key attributes, including the lifetime,
979 * the key type and the usage policy.
980 * Drivers should not access the key size stored
981 * in the attributes: it may not match the
982 * data passed in \p data.
983 * Drivers can call psa_get_key_lifetime(),
984 * psa_get_key_type(),
985 * psa_get_key_usage_flags() and
986 * psa_get_key_algorithm() to access this
987 * information.
988 * \param[in] data Buffer containing the key data.
989 * \param[in] data_length Size of the \p data buffer in bytes.
990 * \param[out] bits On success, the key size in bits. The driver
991 * must determine this value after parsing the
992 * key according to the key type.
993 * This value is not used if the function fails.
994 *
995 * \retval #PSA_SUCCESS
996 * Success.
997 */
999 psa_drv_se_context_t *drv_context,
1000 psa_key_slot_number_t key_slot,
1001 const psa_key_attributes_t *attributes,
1002 const uint8_t *data,
1003 size_t data_length,
1004 size_t *bits);
1005
1006/**
1007 * \brief A function that destroys a secure element key and restore the slot to
1008 * its default state
1009 *
1010 * This function destroys the content of the key from a secure element.
1011 * Implementations shall make a best effort to ensure that any previous content
1012 * of the slot is unrecoverable.
1013 *
1014 * This function returns the specified slot to its default state.
1015 *
1016 * \param[in,out] drv_context The driver context structure.
1017 * \param[in,out] persistent_data A pointer to the persistent data
1018 * that allows writing.
1019 * \param key_slot The key slot to erase.
1020 *
1021 * \retval #PSA_SUCCESS
1022 * The slot's content, if any, has been erased.
1023 */
1025 psa_drv_se_context_t *drv_context,
1026 void *persistent_data,
1027 psa_key_slot_number_t key_slot);
1028
1029/**
1030 * \brief A function that exports a secure element key in binary format
1031 *
1032 * The output of this function can be passed to psa_import_key() to
1033 * create an equivalent object.
1034 *
1035 * If a key is created with `psa_import_key()` and then exported with
1036 * this function, it is not guaranteed that the resulting data is
1037 * identical: the implementation may choose a different representation
1038 * of the same key if the format permits it.
1039 *
1040 * This function should generate output in the same format that
1041 * `psa_export_key()` does. Refer to the
1042 * documentation of `psa_export_key()` for the format for each key type.
1043 *
1044 * \param[in,out] drv_context The driver context structure.
1045 * \param[in] key Slot whose content is to be exported. This must
1046 * be an occupied key slot.
1047 * \param[out] p_data Buffer where the key data is to be written.
1048 * \param[in] data_size Size of the `p_data` buffer in bytes.
1049 * \param[out] p_data_length On success, the number of bytes
1050 * that make up the key data.
1051 *
1052 * \retval #PSA_SUCCESS
1053 * \retval #PSA_ERROR_DOES_NOT_EXIST
1054 * \retval #PSA_ERROR_NOT_PERMITTED
1055 * \retval #PSA_ERROR_NOT_SUPPORTED
1056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1057 * \retval #PSA_ERROR_HARDWARE_FAILURE
1058 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1059 */
1062 uint8_t *p_data,
1063 size_t data_size,
1064 size_t *p_data_length);
1065
1066/**
1067 * \brief A function that generates a symmetric or asymmetric key on a secure
1068 * element
1069 *
1070 * If \p type is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) = 1),
1071 * the driver may export the public key at the time of generation,
1072 * in the format documented for psa_export_public_key() by writing it
1073 * to the \p pubkey buffer.
1074 * This is optional, intended for secure elements that output the
1075 * public key at generation time and that cannot export the public key
1076 * later. Drivers that do not need this feature should leave
1077 * \p *pubkey_length set to 0 and should
1078 * implement the psa_drv_key_management_t::p_export_public function.
1079 * Some implementations do not support this feature, in which case
1080 * \p pubkey is \c NULL and \p pubkey_size is 0.
1081 *
1082 * \param[in,out] drv_context The driver context structure.
1083 * \param key_slot Slot where the key will be stored.
1084 * This must be a valid slot for a key of the
1085 * chosen type. It must be unoccupied.
1086 * \param[in] attributes The key attributes, including the lifetime,
1087 * the key type and size, and the usage policy.
1088 * Drivers can call psa_get_key_lifetime(),
1089 * psa_get_key_type(), psa_get_key_bits(),
1090 * psa_get_key_usage_flags() and
1091 * psa_get_key_algorithm() to access this
1092 * information.
1093 * \param[out] pubkey A buffer where the driver can write the
1094 * public key, when generating an asymmetric
1095 * key pair.
1096 * This is \c NULL when generating a symmetric
1097 * key or if the core does not support
1098 * exporting the public key at generation time.
1099 * \param pubkey_size The size of the `pubkey` buffer in bytes.
1100 * This is 0 when generating a symmetric
1101 * key or if the core does not support
1102 * exporting the public key at generation time.
1103 * \param[out] pubkey_length On entry, this is always 0.
1104 * On success, the number of bytes written to
1105 * \p pubkey. If this is 0 or unchanged on return,
1106 * the core will not read the \p pubkey buffer,
1107 * and will instead call the driver's
1108 * psa_drv_key_management_t::p_export_public
1109 * function to export the public key when needed.
1110 */
1112 psa_drv_se_context_t *drv_context,
1113 psa_key_slot_number_t key_slot,
1114 const psa_key_attributes_t *attributes,
1115 uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length);
1116
1117/**
1118 * \brief A struct containing all of the function pointers needed to for secure
1119 * element key management
1120 *
1121 * PSA Crypto API implementations should populate instances of the table as
1122 * appropriate upon startup or at build time.
1123 *
1124 * If one of the functions is not implemented, it should be set to NULL.
1125 */
1126typedef struct {
1127 /** Function that allocates a slot for a key. */
1129 /** Function that checks the validity of a slot for a key. */
1131 /** Function that performs a key import operation */
1133 /** Function that performs a generation */
1135 /** Function that performs a key destroy operation */
1137 /** Function that performs a key export operation */
1139 /** Function that performs a public key export operation */
1142
1143/**@}*/
1144
1145/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
1146 * \ingroup experimental-crypto-psa
1147 * Key derivation is the process of generating new key material using an
1148 * existing key and additional parameters, iterating through a basic
1149 * cryptographic function, such as a hash.
1150 * Key agreement is a part of cryptographic protocols that allows two parties
1151 * to agree on the same key value, but starting from different original key
1152 * material.
1153 * The flows are similar, and the PSA Crypto Driver Model uses the same functions
1154 * for both of the flows.
1155 *
1156 * There are two different final functions for the flows,
1157 * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1158 * `psa_drv_se_key_derivation_derive` is used when the key material should be
1159 * placed in a slot on the hardware and not exposed to the caller.
1160 * `psa_drv_se_key_derivation_export` is used when the key material should be
1161 * returned to the PSA Cryptographic API implementation.
1162 *
1163 * Different key derivation algorithms require a different number of inputs.
1164 * Instead of having an API that takes as input variable length arrays, which
1165 * can be problemmatic to manage on embedded platforms, the inputs are passed
1166 * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1167 * is called multiple times with different `collateral_id`s. Thus, for a key
1168 * derivation algorithm that required 3 paramter inputs, the flow would look
1169 * something like:
1170 * ~~~~~~~~~~~~~{.c}
1171 * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1172 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1173 * p_collateral_0,
1174 * collateral_0_size);
1175 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1176 * p_collateral_1,
1177 * collateral_1_size);
1178 * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1179 * p_collateral_2,
1180 * collateral_2_size);
1181 * psa_drv_se_key_derivation_derive();
1182 * ~~~~~~~~~~~~~
1183 *
1184 * key agreement example:
1185 * ~~~~~~~~~~~~~{.c}
1186 * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1187 * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1188 * psa_drv_se_key_derivation_export(p_session_key,
1189 * session_key_size,
1190 * &session_key_length);
1191 * ~~~~~~~~~~~~~
1192 */
1193/**@{*/
1194
1195/** \brief A function that Sets up a secure element key derivation operation by
1196 * specifying the algorithm and the source key sot
1197 *
1198 * \param[in,out] drv_context The driver context structure.
1199 * \param[in,out] op_context A hardware-specific structure containing any
1200 * context information for the implementation
1201 * \param[in] kdf_alg The algorithm to be used for the key derivation
1202 * \param[in] source_key The key to be used as the source material for
1203 * the key derivation
1204 *
1205 * \retval #PSA_SUCCESS
1206 */
1208 void *op_context,
1209 psa_algorithm_t kdf_alg,
1210 psa_key_slot_number_t source_key);
1211
1212/** \brief A function that provides collateral (parameters) needed for a secure
1213 * element key derivation or key agreement operation
1214 *
1215 * Since many key derivation algorithms require multiple parameters, it is
1216 * expeced that this function may be called multiple times for the same
1217 * operation, each with a different algorithm-specific `collateral_id`
1218 *
1219 * \param[in,out] op_context A hardware-specific structure containing any
1220 * context information for the implementation
1221 * \param[in] collateral_id An ID for the collateral being provided
1222 * \param[in] p_collateral A buffer containing the collateral data
1223 * \param[in] collateral_size The size in bytes of the collateral
1224 *
1225 * \retval #PSA_SUCCESS
1226 */
1228 uint32_t collateral_id,
1229 const uint8_t *p_collateral,
1230 size_t collateral_size);
1231
1232/** \brief A function that performs the final secure element key derivation
1233 * step and place the generated key material in a slot
1234 *
1235 * \param[in,out] op_context A hardware-specific structure containing any
1236 * context information for the implementation
1237 * \param[in] dest_key The slot where the generated key material
1238 * should be placed
1239 *
1240 * \retval #PSA_SUCCESS
1241 */
1243 psa_key_slot_number_t dest_key);
1244
1245/** \brief A function that performs the final step of a secure element key
1246 * agreement and place the generated key material in a buffer
1247 *
1248 * \param[in,out] op_context A hardware-specific structure containing any
1249 * context information for the implementation
1250 * \param[out] p_output Buffer in which to place the generated key
1251 * material
1252 * \param[in] output_size The size in bytes of `p_output`
1253 * \param[out] p_output_length Upon success, contains the number of bytes of
1254 * key material placed in `p_output`
1255 *
1256 * \retval #PSA_SUCCESS
1257 */
1259 uint8_t *p_output,
1260 size_t output_size,
1261 size_t *p_output_length);
1262
1263/**
1264 * \brief A struct containing all of the function pointers needed to for secure
1265 * element key derivation and agreement
1266 *
1267 * PSA Crypto API implementations should populate instances of the table as
1268 * appropriate upon startup.
1269 *
1270 * If one of the functions is not implemented, it should be set to NULL.
1271 */
1272typedef struct {
1273 /** The driver-specific size of the key derivation context */
1275 /** Function that performs a key derivation setup */
1277 /** Function that sets key derivation collateral */
1279 /** Function that performs a final key derivation step */
1281 /** Function that perforsm a final key derivation or agreement and
1282 * exports the key */
1285
1286/**@}*/
1287
1288/** \defgroup se_registration Secure element driver registration
1289 * \ingroup experimental-crypto-psa
1290 */
1291/**@{*/
1292
1293/** A structure containing pointers to all the entry points of a
1294 * secure element driver.
1295 *
1296 * Future versions of this specification may add extra substructures at
1297 * the end of this structure.
1298 */
1299typedef struct {
1300 /** The version of the driver HAL that this driver implements.
1301 * This is a protection against loading driver binaries built against
1302 * a different version of this specification.
1303 * Use #PSA_DRV_SE_HAL_VERSION.
1304 */
1305 uint32_t hal_version;
1306
1307 /** The size of the driver's persistent data in bytes.
1308 *
1309 * This can be 0 if the driver does not need persistent data.
1310 *
1311 * See the documentation of psa_drv_se_context_t::persistent_data
1312 * for more information about why and how a driver can use
1313 * persistent data.
1314 */
1316
1317 /** The driver initialization function.
1318 *
1319 * This function is called once during the initialization of the
1320 * PSA Cryptography subsystem, before any other function of the
1321 * driver is called. If this function returns a failure status,
1322 * the driver will be unusable, at least until the next system reset.
1323 *
1324 * If this field is \c NULL, it is equivalent to a function that does
1325 * nothing and returns #PSA_SUCCESS.
1326 */
1328
1329 const psa_drv_se_key_management_t *key_management;
1330 const psa_drv_se_mac_t *mac;
1331 const psa_drv_se_cipher_t *cipher;
1332 const psa_drv_se_aead_t *aead;
1333 const psa_drv_se_asymmetric_t *asymmetric;
1334 const psa_drv_se_key_derivation_t *derivation;
1335} psa_drv_se_t;
1336
1337/** The current version of the secure element driver HAL.
1338 */
1339/* 0.0.0 patchlevel 5 */
1340#define PSA_DRV_SE_HAL_VERSION 0x00000005
1341
1342/** Register an external cryptoprocessor (secure element) driver.
1343 *
1344 * This function is only intended to be used by driver code, not by
1345 * application code. In implementations with separation between the
1346 * PSA cryptography module and applications, this function should
1347 * only be available to callers that run in the same memory space as
1348 * the cryptography module, and should not be exposed to applications
1349 * running in a different memory space.
1350 *
1351 * This function may be called before psa_crypto_init(). It is
1352 * implementation-defined whether this function may be called
1353 * after psa_crypto_init().
1354 *
1355 * \note Implementations store metadata about keys including the lifetime
1356 * value, which contains the driver's location indicator. Therefore,
1357 * from one instantiation of the PSA Cryptography
1358 * library to the next one, if there is a key in storage with a certain
1359 * lifetime value, you must always register the same driver (or an
1360 * updated version that communicates with the same secure element)
1361 * with the same location value.
1362 *
1363 * \param location The location value through which this driver will
1364 * be exposed to applications.
1365 * This driver will be used for all keys such that
1366 * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`.
1367 * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
1368 * and may not be used for drivers. Implementations
1369 * may reserve other values.
1370 * \param[in] methods The method table of the driver. This structure must
1371 * remain valid for as long as the cryptography
1372 * module keeps running. It is typically a global
1373 * constant.
1374 *
1375 * \return #PSA_SUCCESS
1376 * The driver was successfully registered. Applications can now
1377 * use \p lifetime to access keys through the methods passed to
1378 * this function.
1379 * \return #PSA_ERROR_BAD_STATE
1380 * This function was called after the initialization of the
1381 * cryptography module, and this implementation does not support
1382 * driver registration at this stage.
1383 * \return #PSA_ERROR_ALREADY_EXISTS
1384 * There is already a registered driver for this value of \p lifetime.
1385 * \return #PSA_ERROR_INVALID_ARGUMENT
1386 * \p lifetime is a reserved value.
1387 * \return #PSA_ERROR_NOT_SUPPORTED
1388 * `methods->hal_version` is not supported by this implementation.
1389 * \return #PSA_ERROR_INSUFFICIENT_MEMORY
1390 * \return #PSA_ERROR_NOT_PERMITTED
1391 */
1393 psa_key_location_t location,
1394 const psa_drv_se_t *methods);
1395
1396/**@}*/
1397
1398#ifdef __cplusplus
1399}
1400#endif
1401
1402#endif /* PSA_CRYPTO_SE_DRIVER_H */
Definitions for all PSA crypto drivers.
psa_encrypt_or_decrypt_t
For encrypt-decrypt functions, whether the operation is an encryption or a decryption.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:98
psa_status_t(* psa_drv_se_key_derivation_export_t)(void *op_context, uint8_t *p_output, size_t output_size, size_t *p_output_length)
A function that performs the final step of a secure element key agreement and place the generated key...
psa_status_t(* psa_drv_se_key_derivation_derive_t)(void *op_context, psa_key_slot_number_t dest_key)
A function that performs the final secure element key derivation step and place the generated key mat...
psa_status_t(* psa_drv_se_key_derivation_collateral_t)(void *op_context, uint32_t collateral_id, const uint8_t *p_collateral, size_t collateral_size)
A function that provides collateral (parameters) needed for a secure element key derivation or key ag...
psa_status_t(* psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context, void *op_context, psa_algorithm_t kdf_alg, psa_key_slot_number_t source_key)
A function that Sets up a secure element key derivation operation by specifying the algorithm and the...
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:55
uint32_t psa_key_location_t
Encoding of key location indicators.
Definition: crypto_types.h:220
psa_status_t(* psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t algorithm, const uint8_t *p_nonce, size_t nonce_length, const uint8_t *p_additional_data, size_t additional_data_length, const uint8_t *p_plaintext, size_t plaintext_length, uint8_t *p_ciphertext, size_t ciphertext_size, size_t *p_ciphertext_length)
A function that performs a secure element authenticated encryption operation.
psa_status_t(* psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t algorithm, const uint8_t *p_nonce, size_t nonce_length, const uint8_t *p_additional_data, size_t additional_data_length, const uint8_t *p_ciphertext, size_t ciphertext_length, uint8_t *p_plaintext, size_t plaintext_size, size_t *p_plaintext_length)
A function that peforms a secure element authenticated decryption operation.
psa_status_t(* psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t alg, const uint8_t *p_input, size_t input_length, const uint8_t *p_salt, size_t salt_length, uint8_t *p_output, size_t output_size, size_t *p_output_length)
A function that decrypts a short message with an asymmetric private key in a secure element.
psa_status_t(* psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t alg, const uint8_t *p_input, size_t input_length, const uint8_t *p_salt, size_t salt_length, uint8_t *p_output, size_t output_size, size_t *p_output_length)
A function that encrypts a short message with an asymmetric public key in a secure element.
psa_status_t(* psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t alg, const uint8_t *p_hash, size_t hash_length, uint8_t *p_signature, size_t signature_size, size_t *p_signature_length)
A function that signs a hash or short message with a private key in a secure element.
psa_status_t(* psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t alg, const uint8_t *p_hash, size_t hash_length, const uint8_t *p_signature, size_t signature_length)
A function that verifies the signature a hash or short message using an asymmetric public key in a se...
psa_status_t(* psa_drv_se_cipher_finish_t)(void *op_context, uint8_t *p_output, size_t output_size, size_t *p_output_length)
A function that completes a previously started secure element cipher operation.
psa_status_t(* psa_drv_se_cipher_abort_t)(void *op_context)
A function that aborts a previously started secure element cipher operation.
psa_status_t(* psa_drv_se_cipher_set_iv_t)(void *op_context, const uint8_t *p_iv, size_t iv_length)
A function that sets the initialization vector (if necessary) for an secure element cipher operation.
psa_status_t(* psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context, void *op_context, psa_key_slot_number_t key_slot, psa_algorithm_t algorithm, psa_encrypt_or_decrypt_t direction)
A function that provides the cipher setup function for a secure element driver.
psa_status_t(* psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, psa_algorithm_t algorithm, psa_encrypt_or_decrypt_t direction, const uint8_t *p_input, size_t input_size, uint8_t *p_output, size_t output_size)
A function that performs the ECB block mode for secure element cipher operations.
psa_status_t(* psa_drv_se_cipher_update_t)(void *op_context, const uint8_t *p_input, size_t input_size, uint8_t *p_output, size_t output_size, size_t *p_output_length)
A function that continues a previously started secure element cipher operation.
psa_status_t(* psa_drv_se_init_t)(psa_drv_se_context_t *drv_context, void *persistent_data, psa_key_location_t location)
A driver initialization function.
uint64_t psa_key_slot_number_t
An internal designation of a key slot between the core part of the PSA Crypto implementation and the ...
psa_status_t(* psa_drv_se_validate_slot_number_t)(psa_drv_se_context_t *drv_context, void *persistent_data, const psa_key_attributes_t *attributes, psa_key_creation_method_t method, psa_key_slot_number_t key_slot)
A function that determines whether a slot number is valid for a key.
psa_status_t(* psa_drv_se_generate_key_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, const psa_key_attributes_t *attributes, uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length)
A function that generates a symmetric or asymmetric key on a secure element.
psa_status_t(* psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key, uint8_t *p_data, size_t data_size, size_t *p_data_length)
A function that exports a secure element key in binary format.
psa_key_creation_method_t
An enumeration indicating how a key is created.
psa_status_t(* psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_context, psa_key_slot_number_t key_slot, const psa_key_attributes_t *attributes, const uint8_t *data, size_t data_length, size_t *bits)
A function that imports a key into a secure element in binary format.
psa_status_t(* psa_drv_se_allocate_key_t)(psa_drv_se_context_t *drv_context, void *persistent_data, const psa_key_attributes_t *attributes, psa_key_creation_method_t method, psa_key_slot_number_t *key_slot)
A function that allocates a slot for a key.
psa_status_t(* psa_drv_se_destroy_key_t)(psa_drv_se_context_t *drv_context, void *persistent_data, psa_key_slot_number_t key_slot)
A function that destroys a secure element key and restore the slot to its default state.
@ PSA_KEY_CREATION_COPY
During psa_copy_key()
@ PSA_KEY_CREATION_IMPORT
During psa_import_key()
@ PSA_KEY_CREATION_DERIVE
During psa_key_derivation_output_key()
@ PSA_KEY_CREATION_GENERATE
During psa_generate_key()
@ PSA_KEY_CREATION_REGISTER
A key is being registered with mbedtls_psa_register_se_key().
psa_status_t(* psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context, void *op_context, psa_key_slot_number_t key_slot, psa_algorithm_t algorithm)
A function that starts a secure element MAC operation for a PSA Crypto Driver implementation.
psa_status_t(* psa_drv_se_mac_update_t)(void *op_context, const uint8_t *p_input, size_t input_length)
A function that continues a previously started secure element MAC operation.
psa_status_t(* psa_drv_se_mac_finish_verify_t)(void *op_context, const uint8_t *p_mac, size_t mac_length)
A function that completes a previously started secure element MAC operation by comparing the resultin...
psa_status_t(* psa_drv_se_mac_abort_t)(void *op_context)
A function that aborts a previous started secure element MAC operation.
psa_status_t(* psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context, const uint8_t *p_input, size_t input_length, psa_key_slot_number_t key_slot, psa_algorithm_t alg, const uint8_t *p_mac, size_t mac_length)
A function that performs a secure element MAC operation in one command and compares the resulting MAC...
psa_status_t(* psa_drv_se_mac_finish_t)(void *op_context, uint8_t *p_mac, size_t mac_size, size_t *p_mac_length)
a function that completes a previously started secure element MAC operation by returning the resultin...
psa_status_t(* psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context, const uint8_t *p_input, size_t input_length, psa_key_slot_number_t key_slot, psa_algorithm_t alg, uint8_t *p_mac, size_t mac_size, size_t *p_mac_length)
A function that performs a secure element MAC operation in one command and returns the calculated MAC...
psa_status_t psa_register_se_driver(psa_key_location_t location, const psa_drv_se_t *methods)
Register an external cryptoprocessor (secure element) driver.
A struct containing all of the function pointers needed to implement secure element Authenticated Enc...
psa_drv_se_aead_encrypt_t p_encrypt
Function that performs the AEAD encrypt operation.
psa_drv_se_aead_decrypt_t p_decrypt
Function that performs the AEAD decrypt operation.
A struct containing all of the function pointers needed to implement asymmetric cryptographic operati...
psa_drv_se_asymmetric_verify_t p_verify
Function that performs an asymmetric verify operation.
psa_drv_se_asymmetric_encrypt_t p_encrypt
Function that performs an asymmetric encrypt operation.
psa_drv_se_asymmetric_decrypt_t p_decrypt
Function that performs an asymmetric decrypt operation.
psa_drv_se_asymmetric_sign_t p_sign
Function that performs an asymmetric sign operation.
A struct containing all of the function pointers needed to implement cipher operations using secure e...
psa_drv_se_cipher_setup_t p_setup
Function that performs a cipher setup operation.
psa_drv_se_cipher_ecb_t p_ecb
Function that performs ECB mode for a cipher operation (Danger: ECB mode should not be used directly ...
psa_drv_se_cipher_finish_t p_finish
Function that completes a cipher operation.
psa_drv_se_cipher_set_iv_t p_set_iv
Function that sets a cipher IV (if necessary)
size_t context_size
The size in bytes of the hardware-specific secure element cipher context structure.
psa_drv_se_cipher_abort_t p_abort
Function that aborts a cipher operation.
psa_drv_se_cipher_update_t p_update
Function that performs a cipher update operation.
Driver context structure.
const void *const persistent_data
A read-only pointer to the driver's persistent data.
uintptr_t transient_data
Driver transient data.
const size_t persistent_data_size
The size of persistent_data in bytes.
A struct containing all of the function pointers needed to for secure element key derivation and agre...
psa_drv_se_key_derivation_export_t p_export
Function that perforsm a final key derivation or agreement and exports the key.
size_t context_size
The driver-specific size of the key derivation context.
psa_drv_se_key_derivation_setup_t p_setup
Function that performs a key derivation setup.
psa_drv_se_key_derivation_derive_t p_derive
Function that performs a final key derivation step.
psa_drv_se_key_derivation_collateral_t p_collateral
Function that sets key derivation collateral.
A struct containing all of the function pointers needed to for secure element key management.
psa_drv_se_export_key_t p_export
Function that performs a key export operation.
psa_drv_se_export_key_t p_export_public
Function that performs a public key export operation.
psa_drv_se_validate_slot_number_t p_validate_slot_number
Function that checks the validity of a slot for a key.
psa_drv_se_destroy_key_t p_destroy
Function that performs a key destroy operation.
psa_drv_se_generate_key_t p_generate
Function that performs a generation.
psa_drv_se_allocate_key_t p_allocate
Function that allocates a slot for a key.
psa_drv_se_import_key_t p_import
Function that performs a key import operation.
A struct containing all of the function pointers needed to perform secure element MAC operations.
psa_drv_se_mac_finish_verify_t p_finish_verify
Function that completes a MAC operation with a verify check.
psa_drv_se_mac_generate_t p_mac
Function that performs a MAC operation in one call.
psa_drv_se_mac_update_t p_update
Function that performs a MAC update operation.
size_t context_size
The size in bytes of the hardware-specific secure element MAC context structure.
psa_drv_se_mac_setup_t p_setup
Function that performs a MAC setup operation.
psa_drv_se_mac_verify_t p_mac_verify
Function that performs a MAC and verify operation in one call.
psa_drv_se_mac_abort_t p_abort
Function that aborts a previoustly started MAC operation.
psa_drv_se_mac_finish_t p_finish
Function that completes a MAC operation.
A structure containing pointers to all the entry points of a secure element driver.
psa_drv_se_init_t p_init
The driver initialization function.
size_t persistent_data_size
The size of the driver's persistent data in bytes.
uint32_t hal_version
The version of the driver HAL that this driver implements.