Skip to content
0.2.0-dev Download ZIP
972 lines ·43.1 KB Raw
1/*
2 * PSA crypto core internal interfaces
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9#ifndef TF_PSA_CRYPTO_PSA_CRYPTO_CORE_H
10#define TF_PSA_CRYPTO_PSA_CRYPTO_CORE_H
11
12#include "tf-psa-crypto/build_info.h"
13
14#include "psa/crypto.h"
15#if defined(MBEDTLS_THREADING_C)
16#include "mbedtls/threading.h"
17#endif
18
19typedef enum {
20 PSA_SLOT_EMPTY = 0,
21 PSA_SLOT_FILLING,
22 PSA_SLOT_FULL,
23 PSA_SLOT_PENDING_DELETION,
24} psa_key_slot_state_t;
25
26/** The data structure representing a key slot, containing key material
27 * and metadata for one key.
28 */
29typedef struct {
30 /* This field is accessed in a lot of places. Putting it first
31 * reduces the code size. */
32 psa_key_attributes_t attr;
33
34 /*
35 * The current state of the key slot, as described in
36 * docs/architecture/psa-thread-safety/psa-thread-safety.md.
37 *
38 * Library functions can modify the state of a key slot by calling
39 * psa_key_slot_state_transition.
40 *
41 * The state variable is used to help determine whether library functions
42 * which operate on the slot succeed. For example, psa_finish_key_creation,
43 * which transfers the state of a slot from PSA_SLOT_FILLING to
44 * PSA_SLOT_FULL, must fail with error code PSA_ERROR_CORRUPTION_DETECTED
45 * if the state of the slot is not PSA_SLOT_FILLING.
46 *
47 * Library functions which traverse the array of key slots only consider
48 * slots that are in a suitable state for the function.
49 * For example, psa_get_and_lock_key_slot_in_memory, which finds a slot
50 * containing a given key ID, will only check slots whose state variable is
51 * PSA_SLOT_FULL.
52 */
53 psa_key_slot_state_t state;
54
55#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
56 /* The index of the slice containing this slot.
57 * This field must be filled if the slot contains a key
58 * (including keys being created or destroyed), and can be either
59 * filled or 0 when the slot is free.
60 *
61 * In most cases, the slice index can be deduced from the key identifer.
62 * We keep it in a separate field for robustness (it reduces the chance
63 * that a coding mistake in the key store will result in accessing the
64 * wrong slice), and also so that it's available even on code paths
65 * during creation or destruction where the key identifier might not be
66 * filled in.
67 * */
68 uint8_t slice_index;
69#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
70
71 union {
72 struct {
73 /* The index of the next slot in the free list for this
74 * slice, relative * to the next array element.
75 *
76 * That is, 0 means the next slot, 1 means the next slot
77 * but one, etc. -1 would mean the slot itself. -2 means
78 * the previous slot, etc.
79 *
80 * If this is beyond the array length, the free list ends with the
81 * current element.
82 *
83 * The reason for this strange encoding is that 0 means the next
84 * element. This way, when we allocate a slice and initialize it
85 * to all-zero, the slice is ready for use, with a free list that
86 * consists of all the slots in order.
87 */
88 int32_t next_free_relative_to_next;
89 } free;
90
91 struct {
92 /*
93 * Number of functions registered as reading the material in the key slot.
94 *
95 * Library functions must not write directly to registered_readers
96 *
97 * A function must call psa_register_read(slot) before reading
98 * the current contents of the slot for an operation.
99 * They then must call psa_unregister_read(slot) once they have
100 * finished reading the current contents of the slot. If the key
101 * slot mutex is not held (when mutexes are enabled), this call
102 * must be done via a call to
103 * psa_unregister_read_under_mutex(slot).
104 * A function must call psa_key_slot_has_readers(slot) to check if
105 * the slot is in use for reading.
106 *
107 * This counter is used to prevent resetting the key slot while
108 * the library may access it. For example, such control is needed
109 * in the following scenarios:
110 * . In case of key slot starvation, all key slots contain the
111 * description of a key, and the library asks for the
112 * description of a persistent key not present in the
113 * key slots, the key slots currently accessed by the
114 * library cannot be reclaimed to free a key slot to load
115 * the persistent key.
116 * . In case of a multi-threaded application where one thread
117 * asks to close or purge or destroy a key while it is in use
118 * by the library through another thread. */
119 size_t registered_readers;
120 } occupied;
121 } var;
122
123 /* Dynamically allocated key data buffer.
124 * Format as specified in psa_export_key(). */
125 struct key_data {
126#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
127 uint8_t data[MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE];
128#else
129 uint8_t *data;
130#endif
131 size_t bytes;
132 } key;
133} psa_key_slot_t;
134
135#if defined(MBEDTLS_THREADING_C)
136
137/** Perform a mutex operation and return immediately upon failure.
138 *
139 * Returns PSA_ERROR_SERVICE_FAILURE if the operation fails
140 * and status was PSA_SUCCESS.
141 *
142 * Assumptions:
143 * psa_status_t status exists.
144 * f is a mutex operation which returns 0 upon success.
145 */
146#define PSA_THREADING_CHK_RET(f) \
147 do \
148 { \
149 if ((f) != 0) { \
150 if (status == PSA_SUCCESS) { \
151 return PSA_ERROR_SERVICE_FAILURE; \
152 } \
153 return status; \
154 } \
155 } while (0);
156
157/** Perform a mutex operation and goto exit on failure.
158 *
159 * Sets status to PSA_ERROR_SERVICE_FAILURE if status was PSA_SUCCESS.
160 *
161 * Assumptions:
162 * psa_status_t status exists.
163 * Label exit: exists.
164 * f is a mutex operation which returns 0 upon success.
165 */
166#define PSA_THREADING_CHK_GOTO_EXIT(f) \
167 do \
168 { \
169 if ((f) != 0) { \
170 if (status == PSA_SUCCESS) { \
171 status = PSA_ERROR_SERVICE_FAILURE; \
172 } \
173 goto exit; \
174 } \
175 } while (0);
176#endif
177
178/** Test whether a key slot has any registered readers.
179 * If multi-threading is enabled, the caller must hold the
180 * global key slot mutex.
181 *
182 * \param[in] slot The key slot to test.
183 *
184 * \return 1 if the slot has any registered readers, 0 otherwise.
185 */
186static inline int psa_key_slot_has_readers(const psa_key_slot_t *slot)
187{
188 return slot->var.occupied.registered_readers > 0;
189}
190
191/** Completely wipe a slot in memory, including its policy.
192 *
193 * Persistent storage is not affected.
194 * Sets the slot's state to PSA_SLOT_EMPTY.
195 * If multi-threading is enabled, the caller must hold the
196 * global key slot mutex.
197 *
198 * \param[in,out] slot The key slot to wipe.
199 *
200 * \retval #PSA_SUCCESS
201 * The slot has been successfully wiped.
202 * \retval #PSA_ERROR_CORRUPTION_DETECTED
203 * The slot's state was PSA_SLOT_FULL or PSA_SLOT_PENDING_DELETION, and
204 * the amount of registered readers was not equal to 1. Or,
205 * the slot's state was PSA_SLOT_EMPTY. Or,
206 * the slot's state was PSA_SLOT_FILLING, and the amount
207 * of registered readers was not equal to 0.
208 */
209psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot);
210
211/** Try to allocate a buffer to an empty key slot.
212 *
213 * \param[in,out] slot Key slot to attach buffer to.
214 * \param[in] buffer_length Requested size of the buffer.
215 *
216 * \retval #PSA_SUCCESS
217 * The buffer has been successfully allocated.
218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
219 * Not enough memory was available for allocation.
220 * \retval #PSA_ERROR_ALREADY_EXISTS
221 * Trying to allocate a buffer to a non-empty key slot.
222 */
223psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
224 size_t buffer_length);
225
226/** Wipe key data from a slot. Preserves metadata such as the policy. */
227psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot);
228
229/** Copy key data (in export format) into an empty key slot.
230 *
231 * This function assumes that the slot does not contain
232 * any key material yet. On failure, the slot content is unchanged.
233 *
234 * \param[in,out] slot Key slot to copy the key into.
235 * \param[in] data Buffer containing the key material.
236 * \param data_length Size of the key buffer.
237 *
238 * \retval #PSA_SUCCESS
239 * The key has been copied successfully.
240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
241 * Not enough memory was available for allocation of the
242 * copy buffer.
243 * \retval #PSA_ERROR_ALREADY_EXISTS
244 * There was other key material already present in the slot.
245 */
246psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
247 const uint8_t *data,
248 size_t data_length);
249
250/** Convert an Mbed TLS error code to a PSA error code
251 *
252 * \note This function is provided solely for the convenience of
253 * Mbed TLS and may be removed at any time without notice.
254 *
255 * \param ret An Mbed TLS-thrown error code
256 *
257 * \return The corresponding PSA error code
258 */
259psa_status_t mbedtls_to_psa_error(int ret);
260
261/** Whether PSA is ready for a cipher operation.
262 *
263 * This is a legacy concept inherited from "driver-only" work in Mbed TLS 3.x.
264 * The block_cipher module uses this to determine whether to call a legacy
265 * module directly. This is necessary in some builds involving drivers, where
266 * the PSA RNG is powered by CTR_DRBG, but AES is not accelerated. This is
267 * an implementation kludge that should be fixed.
268 * https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/469
269 */
270int psa_is_ready_for_cipher(void);
271
272/** Import a key in binary format.
273 *
274 * \note The signature of this function is that of a PSA driver
275 * import_key entry point. This function behaves as an import_key
276 * entry point as defined in the PSA driver interface specification for
277 * transparent drivers.
278 *
279 * \param[in] attributes The attributes for the key to import.
280 * \param[in] data The buffer containing the key data in import
281 * format.
282 * \param[in] data_length Size of the \p data buffer in bytes.
283 * \param[out] key_buffer The buffer to contain the key data in output
284 * format upon successful return.
285 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
286 * size is greater or equal to \p data_length.
287 * \param[out] key_buffer_length The length of the data written in \p
288 * key_buffer in bytes.
289 * \param[out] bits The key size in number of bits.
290 *
291 * \retval #PSA_SUCCESS The key was imported successfully.
292 * \retval #PSA_ERROR_INVALID_ARGUMENT
293 * The key data is not correctly formatted.
294 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
295 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
296 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
297 */
298psa_status_t psa_import_key_into_slot(
299 const psa_key_attributes_t *attributes,
300 const uint8_t *data, size_t data_length,
301 uint8_t *key_buffer, size_t key_buffer_size,
302 size_t *key_buffer_length, size_t *bits);
303
304/** Export a key in binary format
305 *
306 * \note The signature of this function is that of a PSA driver export_key
307 * entry point. This function behaves as an export_key entry point as
308 * defined in the PSA driver interface specification.
309 *
310 * \param[in] attributes The attributes for the key to export.
311 * \param[in] key_buffer Material or context of the key to export.
312 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
313 * \param[out] data Buffer where the key data is to be written.
314 * \param[in] data_size Size of the \p data buffer in bytes.
315 * \param[out] data_length On success, the number of bytes written in
316 * \p data
317 *
318 * \retval #PSA_SUCCESS The key was exported successfully.
319 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
321 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
322 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
323 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
325 */
326psa_status_t psa_export_key_internal(
327 const psa_key_attributes_t *attributes,
328 const uint8_t *key_buffer, size_t key_buffer_size,
329 uint8_t *data, size_t data_size, size_t *data_length);
330
331/** Export a public key or the public part of a key pair in binary format.
332 *
333 * \note The signature of this function is that of a PSA driver
334 * export_public_key entry point. This function behaves as an
335 * export_public_key entry point as defined in the PSA driver interface
336 * specification.
337 *
338 * \param[in] attributes The attributes for the key to export.
339 * \param[in] key_buffer Material or context of the key to export.
340 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
341 * \param[out] data Buffer where the key data is to be written.
342 * \param[in] data_size Size of the \p data buffer in bytes.
343 * \param[out] data_length On success, the number of bytes written in
344 * \p data
345 *
346 * \retval #PSA_SUCCESS The public key was exported successfully.
347 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
349 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
350 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
351 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
352 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
353 */
354psa_status_t psa_export_public_key_internal(
355 const psa_key_attributes_t *attributes,
356 const uint8_t *key_buffer, size_t key_buffer_size,
357 uint8_t *data, size_t data_size, size_t *data_length);
358
359/** Whether a key custom production parameters structure is the default.
360 *
361 * Calls to a key generation driver with non-default custom production parameters
362 * require a driver supporting custom production parameters.
363 *
364 * \param[in] custom The key custom production parameters to check.
365 * \param custom_data_length Size of the associated variable-length data
366 * in bytes.
367 */
368int psa_custom_key_parameters_are_default(
369 const psa_custom_key_parameters_t *custom,
370 size_t custom_data_length);
371
372/**
373 * \brief Generate a key.
374 *
375 * \note The signature of the function is that of a PSA driver generate_key
376 * entry point.
377 *
378 * \param[in] attributes The attributes for the key to generate.
379 * \param[in] custom Custom parameters for the key generation.
380 * \param[in] custom_data Variable-length data associated with \c custom.
381 * \param custom_data_length Length of `custom_data` in bytes.
382 * \param[out] key_buffer Buffer where the key data is to be written.
383 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
384 * \param[out] key_buffer_length On success, the number of bytes written in
385 * \p key_buffer.
386 *
387 * \retval #PSA_SUCCESS
388 * The key was generated successfully.
389 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
390 * \retval #PSA_ERROR_NOT_SUPPORTED
391 * Key size in bits or type not supported.
392 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
393 * The size of \p key_buffer is too small.
394 */
395psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes,
396 const psa_custom_key_parameters_t *custom,
397 const uint8_t *custom_data,
398 size_t custom_data_length,
399 uint8_t *key_buffer,
400 size_t key_buffer_size,
401 size_t *key_buffer_length);
402
403/** Sign a message with a private key. For hash-and-sign algorithms,
404 * this includes the hashing step.
405 *
406 * \note The signature of this function is that of a PSA driver
407 * sign_message entry point. This function behaves as a sign_message
408 * entry point as defined in the PSA driver interface specification for
409 * transparent drivers.
410 *
411 * \note This function will call the driver for psa_sign_hash
412 * and go through driver dispatch again.
413 *
414 * \param[in] attributes The attributes of the key to use for the
415 * operation.
416 * \param[in] key_buffer The buffer containing the key context.
417 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
418 * \param[in] alg A signature algorithm that is compatible with
419 * the type of the key.
420 * \param[in] input The input message to sign.
421 * \param[in] input_length Size of the \p input buffer in bytes.
422 * \param[out] signature Buffer where the signature is to be written.
423 * \param[in] signature_size Size of the \p signature buffer in bytes.
424 * \param[out] signature_length On success, the number of bytes
425 * that make up the returned signature value.
426 *
427 * \retval #PSA_SUCCESS \emptydescription
428 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
429 * The size of the \p signature buffer is too small. You can
430 * determine a sufficient buffer size by calling
431 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
432 * where \c key_type and \c key_bits are the type and bit-size
433 * respectively of the key.
434 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
435 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
437 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
438 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
439 */
440psa_status_t psa_sign_message_builtin(
441 const psa_key_attributes_t *attributes,
442 const uint8_t *key_buffer, size_t key_buffer_size,
443 psa_algorithm_t alg, const uint8_t *input, size_t input_length,
444 uint8_t *signature, size_t signature_size, size_t *signature_length);
445
446/** Verify the signature of a message with a public key, using
447 * a hash-and-sign verification algorithm.
448 *
449 * \note The signature of this function is that of a PSA driver
450 * verify_message entry point. This function behaves as a verify_message
451 * entry point as defined in the PSA driver interface specification for
452 * transparent drivers.
453 *
454 * \note This function will call the driver for psa_verify_hash
455 * and go through driver dispatch again.
456 *
457 * \param[in] attributes The attributes of the key to use for the
458 * operation.
459 * \param[in] key_buffer The buffer containing the key context.
460 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
461 * \param[in] alg A signature algorithm that is compatible with
462 * the type of the key.
463 * \param[in] input The message whose signature is to be verified.
464 * \param[in] input_length Size of the \p input buffer in bytes.
465 * \param[in] signature Buffer containing the signature to verify.
466 * \param[in] signature_length Size of the \p signature buffer in bytes.
467 *
468 * \retval #PSA_SUCCESS
469 * The signature is valid.
470 * \retval #PSA_ERROR_INVALID_SIGNATURE
471 * The calculation was performed successfully, but the passed
472 * signature is not a valid signature.
473 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
474 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
476 */
477psa_status_t psa_verify_message_builtin(
478 const psa_key_attributes_t *attributes,
479 const uint8_t *key_buffer, size_t key_buffer_size,
480 psa_algorithm_t alg, const uint8_t *input, size_t input_length,
481 const uint8_t *signature, size_t signature_length);
482
483/** Sign an already-calculated hash with a private key.
484 *
485 * \note The signature of this function is that of a PSA driver
486 * sign_hash entry point. This function behaves as a sign_hash
487 * entry point as defined in the PSA driver interface specification for
488 * transparent drivers.
489 *
490 * \param[in] attributes The attributes of the key to use for the
491 * operation.
492 * \param[in] key_buffer The buffer containing the key context.
493 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
494 * \param[in] alg A signature algorithm that is compatible with
495 * the type of the key.
496 * \param[in] hash The hash or message to sign.
497 * \param[in] hash_length Size of the \p hash buffer in bytes.
498 * \param[out] signature Buffer where the signature is to be written.
499 * \param[in] signature_size Size of the \p signature buffer in bytes.
500 * \param[out] signature_length On success, the number of bytes
501 * that make up the returned signature value.
502 *
503 * \retval #PSA_SUCCESS \emptydescription
504 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
505 * The size of the \p signature buffer is too small. You can
506 * determine a sufficient buffer size by calling
507 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
508 * where \c key_type and \c key_bits are the type and bit-size
509 * respectively of the key.
510 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
511 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
513 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
514 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
515 */
516psa_status_t psa_sign_hash_builtin(
517 const psa_key_attributes_t *attributes,
518 const uint8_t *key_buffer, size_t key_buffer_size,
519 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
520 uint8_t *signature, size_t signature_size, size_t *signature_length);
521
522/**
523 * \brief Verify the signature a hash or short message using a public key.
524 *
525 * \note The signature of this function is that of a PSA driver
526 * verify_hash entry point. This function behaves as a verify_hash
527 * entry point as defined in the PSA driver interface specification for
528 * transparent drivers.
529 *
530 * \param[in] attributes The attributes of the key to use for the
531 * operation.
532 * \param[in] key_buffer The buffer containing the key context.
533 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
534 * \param[in] alg A signature algorithm that is compatible with
535 * the type of the key.
536 * \param[in] hash The hash or message whose signature is to be
537 * verified.
538 * \param[in] hash_length Size of the \p hash buffer in bytes.
539 * \param[in] signature Buffer containing the signature to verify.
540 * \param[in] signature_length Size of the \p signature buffer in bytes.
541 *
542 * \retval #PSA_SUCCESS
543 * The signature is valid.
544 * \retval #PSA_ERROR_INVALID_SIGNATURE
545 * The calculation was performed successfully, but the passed
546 * signature is not a valid signature.
547 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
548 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
550 */
551psa_status_t psa_verify_hash_builtin(
552 const psa_key_attributes_t *attributes,
553 const uint8_t *key_buffer, size_t key_buffer_size,
554 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
555 const uint8_t *signature, size_t signature_length);
556
557/**
558 * \brief Validate the key bit size for unstructured keys.
559 *
560 * \note Check that the bit size is acceptable for a given key type for
561 * unstructured keys.
562 *
563 * \param[in] type The key type
564 * \param[in] bits The number of bits of the key
565 *
566 * \retval #PSA_SUCCESS
567 * The key type and size are valid.
568 * \retval #PSA_ERROR_INVALID_ARGUMENT
569 * The size in bits of the key is not valid.
570 * \retval #PSA_ERROR_NOT_SUPPORTED
571 * The type and/or the size in bits of the key or the combination of
572 * the two is not supported.
573 */
574psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
575 size_t bits);
576
577/** Perform a key agreement and return the raw shared secret, using
578 built-in raw key agreement functions.
579 *
580 * \note The signature of this function is that of a PSA driver
581 * key_agreement entry point. This function behaves as a key_agreement
582 * entry point as defined in the PSA driver interface specification for
583 * transparent drivers.
584 *
585 * \param[in] attributes The attributes of the key to use for the
586 * operation.
587 * \param[in] key_buffer The buffer containing the private key
588 * context.
589 * \param[in] key_buffer_size Size of the \p key_buffer buffer in
590 * bytes.
591 * \param[in] alg A key agreement algorithm that is
592 * compatible with the type of the key.
593 * \param[in] peer_key The buffer containing the key context
594 * of the peer's public key.
595 * \param[in] peer_key_length Size of the \p peer_key buffer in
596 * bytes.
597 * \param[out] shared_secret The buffer to which the shared secret
598 * is to be written.
599 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
600 * bytes.
601 * \param[out] shared_secret_length On success, the number of bytes that make
602 * up the returned shared secret.
603 * \retval #PSA_SUCCESS
604 * Success. Shared secret successfully calculated.
605 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
606 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
607 * \retval #PSA_ERROR_INVALID_ARGUMENT
608 * \p alg is not a key agreement algorithm, or
609 * \p private_key is not compatible with \p alg,
610 * or \p peer_key is not valid for \p alg or not compatible with
611 * \p private_key.
612 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
613 * \p shared_secret_size is too small
614 * \retval #PSA_ERROR_NOT_SUPPORTED
615 * \p alg is not a supported key agreement algorithm.
616 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
618 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
619 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
620 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
621 * \retval #PSA_ERROR_BAD_STATE \emptydescription
622 */
623psa_status_t psa_key_agreement_raw_builtin(
624 const psa_key_attributes_t *attributes,
625 const uint8_t *key_buffer,
626 size_t key_buffer_size,
627 psa_algorithm_t alg,
628 const uint8_t *peer_key,
629 size_t peer_key_length,
630 uint8_t *shared_secret,
631 size_t shared_secret_size,
632 size_t *shared_secret_length);
633
634/**
635 * \brief Set the maximum number of ops allowed to be executed by an
636 * interruptible function in a single call.
637 *
638 * \note The signature of this function is that of a PSA driver
639 * interruptible_set_max_ops entry point. This function behaves as an
640 * interruptible_set_max_ops entry point as defined in the PSA driver
641 * interface specification for transparent drivers.
642 *
643 * \param[in] max_ops The maximum number of ops to be executed in a
644 * single call, this can be a number from 0 to
645 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
646 * is obviously the least amount of work done per
647 * call.
648 */
649void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops);
650
651/**
652 * \brief Get the maximum number of ops allowed to be executed by an
653 * interruptible function in a single call.
654 *
655 * \note The signature of this function is that of a PSA driver
656 * interruptible_get_max_ops entry point. This function behaves as an
657 * interruptible_get_max_ops entry point as defined in the PSA driver
658 * interface specification for transparent drivers.
659 *
660 * \return Maximum number of ops allowed to be executed
661 * by an interruptible function in a single call.
662 */
663uint32_t mbedtls_psa_interruptible_get_max_ops(void);
664
665/**
666 * \brief Get the number of ops that a hash signing operation has taken for the
667 * previous call. If no call or work has taken place, this will return
668 * zero.
669 *
670 * \note The signature of this function is that of a PSA driver
671 * sign_hash_get_num_ops entry point. This function behaves as an
672 * sign_hash_get_num_ops entry point as defined in the PSA driver
673 * interface specification for transparent drivers.
674 *
675 * \param operation The \c
676 * mbedtls_psa_sign_hash_interruptible_operation_t
677 * to use. This must be initialized first.
678 *
679 * \return Number of ops that were completed
680 * in the last call to \c
681 * mbedtls_psa_sign_hash_complete().
682 */
683uint32_t mbedtls_psa_sign_hash_get_num_ops(
684 const mbedtls_psa_sign_hash_interruptible_operation_t *operation);
685
686/**
687 * \brief Get the number of ops that a hash verification operation has taken for
688 * the previous call. If no call or work has taken place, this will
689 * return zero.
690 *
691 * \note The signature of this function is that of a PSA driver
692 * verify_hash_get_num_ops entry point. This function behaves as an
693 * verify_hash_get_num_ops entry point as defined in the PSA driver
694 * interface specification for transparent drivers.
695 *
696 * \param operation The \c
697 * mbedtls_psa_verify_hash_interruptible_operation_t
698 * to use. This must be initialized first.
699 *
700 * \return Number of ops that were completed
701 * in the last call to \c
702 * mbedtls_psa_verify_hash_complete().
703 */
704uint32_t mbedtls_psa_verify_hash_get_num_ops(
705 const mbedtls_psa_verify_hash_interruptible_operation_t *operation);
706
707/**
708 * \brief Start signing a hash or short message with a private key, in an
709 * interruptible manner.
710 *
711 * \note The signature of this function is that of a PSA driver
712 * sign_hash_start entry point. This function behaves as a
713 * sign_hash_start entry point as defined in the PSA driver interface
714 * specification for transparent drivers.
715 *
716 * \param[in] operation The \c
717 * mbedtls_psa_sign_hash_interruptible_operation_t
718 * to use. This must be initialized first.
719 * \param[in] attributes The attributes of the key to use for the
720 * operation.
721 * \param[in] key_buffer The buffer containing the key context.
722 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
723 * \param[in] alg A signature algorithm that is compatible with
724 * the type of the key.
725 * \param[in] hash The hash or message to sign.
726 * \param hash_length Size of the \p hash buffer in bytes.
727 *
728 * \retval #PSA_SUCCESS
729 * The operation started successfully - call \c psa_sign_hash_complete()
730 * with the same context to complete the operation
731 * \retval #PSA_ERROR_INVALID_ARGUMENT
732 * An unsupported, incorrectly formatted or incorrect type of key was
733 * used.
734 * \retval #PSA_ERROR_NOT_SUPPORTED Either no internal interruptible operations
735 * are currently supported, or the key type is currently unsupported.
736 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
737 * There was insufficient memory to load the key representation.
738 */
739psa_status_t mbedtls_psa_sign_hash_start(
740 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
741 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
742 size_t key_buffer_size, psa_algorithm_t alg,
743 const uint8_t *hash, size_t hash_length);
744
745/**
746 * \brief Continue and eventually complete the action of signing a hash or
747 * short message with a private key, in an interruptible manner.
748 *
749 * \note The signature of this function is that of a PSA driver
750 * sign_hash_complete entry point. This function behaves as a
751 * sign_hash_complete entry point as defined in the PSA driver interface
752 * specification for transparent drivers.
753 *
754 * \param[in] operation The \c
755 * mbedtls_psa_sign_hash_interruptible_operation_t
756 * to use. This must be initialized first.
757 *
758 * \param[out] signature Buffer where the signature is to be written.
759 * \param signature_size Size of the \p signature buffer in bytes. This
760 * must be appropriate for the selected
761 * algorithm and key.
762 * \param[out] signature_length On success, the number of bytes that make up
763 * the returned signature value.
764 *
765 * \retval #PSA_SUCCESS
766 * Operation completed successfully
767 *
768 * \retval #PSA_OPERATION_INCOMPLETE
769 * Operation was interrupted due to the setting of \c
770 * psa_interruptible_set_max_ops(), there is still work to be done,
771 * please call this function again with the same operation object.
772 *
773 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
774 * The size of the \p signature buffer is too small. You can
775 * determine a sufficient buffer size by calling
776 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
777 * where \c key_type and \c key_bits are the type and bit-size
778 * respectively of \p key.
779 *
780 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
781 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
782 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
783 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
784 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
785 */
786psa_status_t mbedtls_psa_sign_hash_complete(
787 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
788 uint8_t *signature, size_t signature_size,
789 size_t *signature_length);
790
791/**
792 * \brief Abort a sign hash operation.
793 *
794 * \note The signature of this function is that of a PSA driver sign_hash_abort
795 * entry point. This function behaves as a sign_hash_abort entry point as
796 * defined in the PSA driver interface specification for transparent
797 * drivers.
798 *
799 * \param[in] operation The \c
800 * mbedtls_psa_sign_hash_interruptible_operation_t
801 * to abort.
802 *
803 * \retval #PSA_SUCCESS
804 * The operation was aborted successfully.
805 */
806psa_status_t mbedtls_psa_sign_hash_abort(
807 mbedtls_psa_sign_hash_interruptible_operation_t *operation);
808
809/**
810 * \brief Start reading and verifying a hash or short message, in an
811 * interruptible manner.
812 *
813 * \note The signature of this function is that of a PSA driver
814 * verify_hash_start entry point. This function behaves as a
815 * verify_hash_start entry point as defined in the PSA driver interface
816 * specification for transparent drivers.
817 *
818 * \param[in] operation The \c
819 * mbedtls_psa_verify_hash_interruptible_operation_t
820 * to use. This must be initialized first.
821 * \param[in] attributes The attributes of the key to use for the
822 * operation.
823 * \param[in] key_buffer The buffer containing the key context.
824 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
825 * \param[in] alg A signature algorithm that is compatible with
826 * the type of the key.
827 * \param[in] hash The hash whose signature is to be verified.
828 * \param hash_length Size of the \p hash buffer in bytes.
829 * \param[in] signature Buffer containing the signature to verify.
830 * \param signature_length Size of the \p signature buffer in bytes.
831 *
832 * \retval #PSA_SUCCESS
833 * The operation started successfully - call \c psa_sign_hash_complete()
834 * with the same context to complete the operation
835 * \retval #PSA_ERROR_INVALID_ARGUMENT
836 * An unsupported or incorrect type of key was used.
837 * \retval #PSA_ERROR_NOT_SUPPORTED
838 * Either no internal interruptible operations are currently supported,
839 * or the key type is currently unsupported.
840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
841 * There was insufficient memory either to load the key representation,
842 * or to prepare the operation.
843 */
844psa_status_t mbedtls_psa_verify_hash_start(
845 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
846 const psa_key_attributes_t *attributes,
847 const uint8_t *key_buffer, size_t key_buffer_size,
848 psa_algorithm_t alg,
849 const uint8_t *hash, size_t hash_length,
850 const uint8_t *signature, size_t signature_length);
851
852/**
853 * \brief Continue and eventually complete the action of signing a hash or
854 * short message with a private key, in an interruptible manner.
855 *
856 * \note The signature of this function is that of a PSA driver
857 * sign_hash_complete entry point. This function behaves as a
858 * sign_hash_complete entry point as defined in the PSA driver interface
859 * specification for transparent drivers.
860 *
861 * \param[in] operation The \c
862 * mbedtls_psa_sign_hash_interruptible_operation_t
863 * to use. This must be initialized first.
864 *
865 * \retval #PSA_SUCCESS
866 * Operation completed successfully, and the passed signature is valid.
867 *
868 * \retval #PSA_OPERATION_INCOMPLETE
869 * Operation was interrupted due to the setting of \c
870 * psa_interruptible_set_max_ops(), there is still work to be done,
871 * please call this function again with the same operation object.
872 *
873 * \retval #PSA_ERROR_INVALID_SIGNATURE
874 * The calculation was performed successfully, but the passed
875 * signature is not a valid signature.
876 *
877 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
878 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
879 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
880 */
881psa_status_t mbedtls_psa_verify_hash_complete(
882 mbedtls_psa_verify_hash_interruptible_operation_t *operation);
883
884/**
885 * \brief Abort a verify signed hash operation.
886 *
887 * \note The signature of this function is that of a PSA driver
888 * verify_hash_abort entry point. This function behaves as a
889 * verify_hash_abort entry point as defined in the PSA driver interface
890 * specification for transparent drivers.
891 *
892 * \param[in] operation The \c
893 * mbedtls_psa_verify_hash_interruptible_operation_t
894 * to abort.
895 *
896 * \retval #PSA_SUCCESS
897 * The operation was aborted successfully.
898 */
899psa_status_t mbedtls_psa_verify_hash_abort(
900 mbedtls_psa_verify_hash_interruptible_operation_t *operation);
901
902typedef struct psa_crypto_local_input_s {
903 uint8_t *buffer;
904 size_t length;
905} psa_crypto_local_input_t;
906
907#define PSA_CRYPTO_LOCAL_INPUT_INIT ((psa_crypto_local_input_t) { NULL, 0 })
908
909/** Allocate a local copy of an input buffer and copy the contents into it.
910 *
911 * \param[in] input Pointer to input buffer.
912 * \param[in] input_len Length of the input buffer.
913 * \param[out] local_input Pointer to a psa_crypto_local_input_t struct
914 * containing a local input copy.
915 * \return #PSA_SUCCESS, if the buffer was successfully
916 * copied.
917 * \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
918 * the buffer cannot be allocated.
919 */
920psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
921 psa_crypto_local_input_t *local_input);
922
923/** Free a local copy of an input buffer.
924 *
925 * \param[in] local_input Pointer to a psa_crypto_local_input_t struct
926 * populated by a previous call to
927 * psa_crypto_local_input_alloc().
928 */
929void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input);
930
931typedef struct psa_crypto_local_output_s {
932 uint8_t *original;
933 uint8_t *buffer;
934 size_t length;
935} psa_crypto_local_output_t;
936
937#define PSA_CRYPTO_LOCAL_OUTPUT_INIT ((psa_crypto_local_output_t) { NULL, NULL, 0 })
938
939/** Allocate a local copy of an output buffer.
940 *
941 * \note This does not copy any data from the original
942 * output buffer but only allocates a buffer
943 * whose contents will be copied back to the
944 * original in a future call to
945 * psa_crypto_local_output_free().
946 *
947 * \param[in] output Pointer to output buffer.
948 * \param[in] output_len Length of the output buffer.
949 * \param[out] local_output Pointer to a psa_crypto_local_output_t struct to
950 * populate with the local output copy.
951 * \return #PSA_SUCCESS, if the buffer was successfully
952 * copied.
953 * \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
954 * the buffer cannot be allocated.
955 */
956psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
957 psa_crypto_local_output_t *local_output);
958
959/** Copy from a local copy of an output buffer back to the original, then
960 * free the local copy.
961 *
962 * \param[in] local_output Pointer to a psa_crypto_local_output_t struct
963 * populated by a previous call to
964 * psa_crypto_local_output_alloc().
965 * \return #PSA_SUCCESS, if the local output was
966 * successfully copied back to the original.
967 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the output
968 * could not be copied back to the original.
969 */
970psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output);
971
972#endif /* TF_PSA_CRYPTO_PSA_CRYPTO_CORE_H */