Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 9 | #include "common.h" |
Ronald Cron | afbc7ed | 2023-01-24 16:02:04 +0100 | [diff] [blame] | 10 | #include "psa_crypto_core_common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 11 | |
| 12 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 13 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 14 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 15 | #include "check_crypto_config.h" |
| 16 | #endif |
| 17 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 18 | #include "psa/crypto.h" |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 19 | #include "psa/crypto_values.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 20 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 21 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 22 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 23 | #include "psa_crypto_invasive.h" |
Xiaokang Qian | e9c39c4 | 2023-08-09 08:50:19 +0000 | [diff] [blame] | 24 | #include "psa_crypto_driver_wrappers.h" |
Xiaokang Qian | fe9666b | 2023-09-11 10:36:20 +0000 | [diff] [blame] | 25 | #include "psa_crypto_driver_wrappers_no_static.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 26 | #include "psa_crypto_ecp.h" |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 27 | #include "psa_crypto_ffdh.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 28 | #include "psa_crypto_hash.h" |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 29 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 30 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 31 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 33 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 34 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 35 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 36 | /* Include internal declarations that are useful for implementing persistently |
| 37 | * stored keys. */ |
| 38 | #include "psa_crypto_storage.h" |
| 39 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 40 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 41 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include "mbedtls/platform.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 46 | #include "mbedtls/aes.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 47 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 48 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 49 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 50 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 51 | #include "mbedtls/chacha20.h" |
| 52 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 53 | #include "mbedtls/cipher.h" |
| 54 | #include "mbedtls/ccm.h" |
| 55 | #include "mbedtls/cmac.h" |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 56 | #include "mbedtls/constant_time.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 57 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 58 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 59 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 60 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 61 | #include "mbedtls/error.h" |
| 62 | #include "mbedtls/gcm.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/md5.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 64 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 65 | #include "pk_wrap.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 66 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 67 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 69 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/sha1.h" |
| 71 | #include "mbedtls/sha256.h" |
| 72 | #include "mbedtls/sha512.h" |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 73 | #include "mbedtls/psa_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 74 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 75 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 76 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 77 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 78 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 79 | #endif |
| 80 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 81 | /****************************************************************/ |
| 82 | /* Global data, support functions and library management */ |
| 83 | /****************************************************************/ |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | static int key_type_is_raw_bytes(psa_key_type_t type) |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 86 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 90 | /* Values for psa_global_data_t::rng_state */ |
| 91 | #define RNG_NOT_INITIALIZED 0 |
| 92 | #define RNG_INITIALIZED 1 |
| 93 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 94 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | typedef struct { |
Dave Rodgman | 864f594 | 2023-08-16 18:04:44 +0100 | [diff] [blame] | 96 | uint8_t initialized; |
| 97 | uint8_t rng_state; |
| 98 | uint8_t drivers_initialized; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 99 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 100 | } psa_global_data_t; |
| 101 | |
| 102 | static psa_global_data_t global_data; |
| 103 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 104 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 105 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 106 | &global_data.rng.drbg; |
| 107 | #endif |
| 108 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 109 | #define GUARD_MODULE_INITIALIZED \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | if (global_data.initialized == 0) \ |
| 111 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 112 | |
David Horstmann | 513101b | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 113 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 114 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 115 | /* Declare a local copy of an input buffer and a variable that will be used |
| 116 | * to store a pointer to the start of the buffer. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 117 | * |
| 118 | * Note: This macro must be called before any operations which may jump to |
| 119 | * the exit label, so that the local input copy object is safe to be freed. |
| 120 | * |
| 121 | * Assumptions: |
| 122 | * - input is the name of a pointer to the buffer to be copied |
| 123 | * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 124 | * - input_copy_name is a name that is unused in the current scope |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 125 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 126 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 127 | psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \ |
| 128 | const uint8_t *input_copy_name = NULL; |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 129 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 130 | /* Allocate a copy of the buffer input and set the pointer input_copy to |
| 131 | * point to the start of the copy. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 132 | * |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 133 | * Assumptions: |
| 134 | * - psa_status_t status exists |
| 135 | * - An exit label is declared |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 136 | * - input is the name of a pointer to the buffer to be copied |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 137 | * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 138 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 139 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 140 | status = psa_crypto_local_input_alloc(input, length, \ |
| 141 | &LOCAL_INPUT_COPY_OF_##input); \ |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 142 | if (status != PSA_SUCCESS) { \ |
| 143 | goto exit; \ |
| 144 | } \ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 145 | input_copy = LOCAL_INPUT_COPY_OF_##input.buffer; |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 146 | |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 147 | /* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC() |
| 148 | * |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 149 | * Assumptions: |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 150 | * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC() |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 151 | * - input is the name of the original buffer that was copied |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 152 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 153 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 154 | input_copy = NULL; \ |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 155 | psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input); |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 156 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 157 | /* Declare a local copy of an output buffer and a variable that will be used |
| 158 | * to store a pointer to the start of the buffer. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 159 | * |
| 160 | * Note: This macro must be called before any operations which may jump to |
| 161 | * the exit label, so that the local output copy object is safe to be freed. |
| 162 | * |
| 163 | * Assumptions: |
| 164 | * - output is the name of a pointer to the buffer to be copied |
| 165 | * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 166 | * - output_copy_name is a name that is unused in the current scope |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 167 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 168 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 169 | psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \ |
| 170 | uint8_t *output_copy_name = NULL; |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 171 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 172 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 173 | * point to the start of the copy. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 174 | * |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 175 | * Assumptions: |
| 176 | * - psa_status_t status exists |
| 177 | * - An exit label is declared |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 178 | * - output is the name of a pointer to the buffer to be copied |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 179 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 180 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 181 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 182 | status = psa_crypto_local_output_alloc(output, length, \ |
| 183 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 184 | if (status != PSA_SUCCESS) { \ |
| 185 | goto exit; \ |
| 186 | } \ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 187 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 188 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 189 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 190 | * after first copying back its contents to the original buffer. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 191 | * |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 192 | * Assumptions: |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 193 | * - psa_status_t status exists |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 194 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 195 | * - output is the name of the original buffer that was copied |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 196 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 197 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 198 | output_copy = NULL; \ |
David Horstmann | 5a945f5 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 199 | do { \ |
| 200 | psa_status_t local_output_status; \ |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 201 | local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | 5a945f5 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 202 | if (local_output_status != PSA_SUCCESS) { \ |
| 203 | /* Since this error case is an internal error, it's more serious than \ |
| 204 | * any existing error code and so it's fine to overwrite the existing \ |
| 205 | * status. */ \ |
| 206 | status = local_output_status; \ |
| 207 | } \ |
| 208 | } while (0) |
David Horstmann | 513101b | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 209 | #else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 210 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 211 | const uint8_t *input_copy_name = NULL; |
| 212 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 213 | input_copy = input; |
| 214 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 215 | input_copy = NULL; |
| 216 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 217 | uint8_t *output_copy_name = NULL; |
| 218 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 219 | output_copy = output; |
Gabor Mezei | 4892d75 | 2024-01-29 17:27:44 +0100 | [diff] [blame] | 220 | #define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \ |
| 221 | output_copy = output; |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 222 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 223 | output_copy = NULL; |
David Horstmann | 513101b | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 224 | #endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 225 | |
| 226 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 227 | int psa_can_do_hash(psa_algorithm_t hash_alg) |
| 228 | { |
| 229 | (void) hash_alg; |
| 230 | return global_data.drivers_initialized; |
| 231 | } |
Valerio Setti | c6f004f | 2023-12-12 11:27:36 +0100 | [diff] [blame] | 232 | |
Valerio Setti | 1fff4f2 | 2023-12-28 14:19:34 +0100 | [diff] [blame] | 233 | int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg) |
Valerio Setti | c6f004f | 2023-12-12 11:27:36 +0100 | [diff] [blame] | 234 | { |
Valerio Setti | 1fff4f2 | 2023-12-28 14:19:34 +0100 | [diff] [blame] | 235 | (void) key_type; |
Valerio Setti | c6f004f | 2023-12-12 11:27:36 +0100 | [diff] [blame] | 236 | (void) cipher_alg; |
| 237 | return global_data.drivers_initialized; |
| 238 | } |
| 239 | |
| 240 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 241 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 242 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \ |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 243 | defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 244 | static int psa_is_dh_key_size_valid(size_t bits) |
| 245 | { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 246 | if (bits != 2048 && bits != 3072 && bits != 4096 && |
| 247 | bits != 6144 && bits != 8192) { |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | return 1; |
| 252 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 253 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT || |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 254 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY || |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 255 | PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 258 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 259 | /* Mbed TLS error codes can combine a high-level error code and a |
| 260 | * low-level error code. The low-level error usually reflects the |
| 261 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | int low_level_ret = -(-ret & 0x007f); |
| 263 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 264 | case 0: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 266 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 267 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 269 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 09a9e58 | 2023-08-31 11:05:22 +0100 | [diff] [blame] | 271 | case MBEDTLS_ERR_AES_BAD_INPUT_DATA: |
| 272 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 273 | #endif |
| 274 | |
| 275 | #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 276 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 277 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 278 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 279 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 280 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 282 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 284 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 286 | #endif |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 287 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 288 | #if defined(MBEDTLS_CAMELLIA_C) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 289 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 290 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 292 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 293 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 294 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 295 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 299 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 300 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 301 | #if defined(MBEDTLS_CHACHA20_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 302 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 304 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 305 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 306 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 307 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 309 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 311 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 312 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 313 | #if defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 314 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 316 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 318 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 322 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 324 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 326 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | return PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 328 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 331 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 332 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 333 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 334 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 336 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 337 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 339 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 341 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 342 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 343 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 344 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 346 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 348 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 349 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 350 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 352 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 353 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 354 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | return PSA_ERROR_INVALID_SIGNATURE; |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 356 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 358 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 360 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 361 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 362 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 364 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 365 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 366 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 368 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 369 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 371 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 373 | #endif |
| 374 | |
Dave Rodgman | dea266f | 2023-08-31 11:52:43 +0100 | [diff] [blame] | 375 | #if defined(MBEDTLS_MD_LIGHT) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 376 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 378 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 380 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 382 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 383 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 385 | #endif |
| 386 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 387 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 388 | #if defined(MBEDTLS_BIGNUM_C) |
| 389 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 390 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 392 | #endif |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 393 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 395 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 397 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 399 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 401 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 403 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 405 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 407 | #endif |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 408 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 409 | #if defined(MBEDTLS_PK_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 410 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 412 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 413 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 415 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \ |
| 416 | defined(MBEDTLS_PSA_ITS_FILE_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 417 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 419 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 420 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 421 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 423 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 425 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 426 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 428 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 430 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 431 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 432 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 434 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 436 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 438 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 439 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 440 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 442 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 444 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 445 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 446 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 448 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 450 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 452 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 454 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 455 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 457 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 459 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 461 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 463 | #endif |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 464 | |
Dave Rodgman | 1dab445 | 2023-09-01 09:59:51 +0100 | [diff] [blame] | 465 | #if defined(MBEDTLS_ECP_LIGHT) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 466 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 467 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 469 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 471 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 473 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 474 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 476 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 478 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 480 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 481 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 482 | case MBEDTLS_ERR_ECP_IN_PROGRESS: |
| 483 | return PSA_OPERATION_INCOMPLETE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 484 | #endif |
| 485 | #endif |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 486 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 487 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 489 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 490 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 495 | /** |
| 496 | * \brief For output buffers which contain "tags" |
| 497 | * (outputs that may be checked for validity like |
| 498 | * hashes, MACs and signatures), fill the unused |
| 499 | * part of the output buffer (the whole buffer on |
| 500 | * error, the trailing part on success) with |
| 501 | * something that isn't a valid tag (barring an |
| 502 | * attack on the tag and deliberately-crafted |
| 503 | * input), in case the caller doesn't check the |
| 504 | * return status properly. |
| 505 | * |
| 506 | * \param output_buffer Pointer to buffer to wipe. May not be NULL |
| 507 | * unless \p output_buffer_size is zero. |
| 508 | * \param status Status of function called to generate |
| 509 | * output_buffer originally |
| 510 | * \param output_buffer_size Size of output buffer. If zero, \p output_buffer |
| 511 | * could be NULL. |
| 512 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 513 | * less than \p output_buffer_size |
| 514 | */ |
| 515 | static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status, |
Paul Elliott | 7118d17 | 2023-02-26 16:57:05 +0000 | [diff] [blame] | 516 | size_t output_buffer_size, size_t output_buffer_length) |
| 517 | { |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 518 | size_t offset = 0; |
| 519 | |
| 520 | if (output_buffer_size == 0) { |
| 521 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 522 | call memset because output_buffer may be NULL in this case */ |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | if (status == PSA_SUCCESS) { |
| 527 | offset = output_buffer_length; |
| 528 | } |
| 529 | |
| 530 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 531 | } |
| 532 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 535 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 536 | { |
| 537 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 539 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 540 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 541 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 542 | case PSA_KEY_TYPE_PASSWORD: |
| 543 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 544 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 545 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 546 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | if (bits != 128 && bits != 192 && bits != 256) { |
| 548 | return PSA_ERROR_INVALID_ARGUMENT; |
| 549 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 550 | break; |
| 551 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 552 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 553 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | if (bits != 128 && bits != 192 && bits != 256) { |
| 555 | return PSA_ERROR_INVALID_ARGUMENT; |
| 556 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 557 | break; |
| 558 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 559 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 560 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | if (bits != 128 && bits != 192 && bits != 256) { |
| 562 | return PSA_ERROR_INVALID_ARGUMENT; |
| 563 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 564 | break; |
| 565 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 566 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 567 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | if (bits != 64 && bits != 128 && bits != 192) { |
| 569 | return PSA_ERROR_INVALID_ARGUMENT; |
| 570 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 571 | break; |
| 572 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 573 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 574 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | if (bits != 256) { |
| 576 | return PSA_ERROR_INVALID_ARGUMENT; |
| 577 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 578 | break; |
| 579 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 580 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 582 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | if (bits % 8 != 0) { |
| 584 | return PSA_ERROR_INVALID_ARGUMENT; |
| 585 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 590 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 591 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 592 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 593 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 594 | * |
| 595 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 596 | * \param[in] key_type The key type of the key to be used with the |
| 597 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 598 | * |
| 599 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 600 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 601 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 602 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 603 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 604 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 605 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 607 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 609 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 610 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 611 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 612 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 615 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 616 | * key. */ |
| 617 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 618 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 619 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 620 | * the block length (larger than 1) for block ciphers. */ |
| 621 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 622 | return PSA_SUCCESS; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 631 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 632 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | if (slot->key.data != NULL) { |
| 634 | return PSA_ERROR_ALREADY_EXISTS; |
| 635 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 638 | if (slot->key.data == NULL) { |
| 639 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 640 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 641 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 642 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 647 | const uint8_t *data, |
| 648 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 649 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 651 | data_length); |
| 652 | if (status != PSA_SUCCESS) { |
| 653 | return status; |
| 654 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | memcpy(slot->key.data, data, data_length); |
| 657 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 660 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 661 | const psa_key_attributes_t *attributes, |
| 662 | const uint8_t *data, size_t data_length, |
| 663 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 665 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 666 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 667 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 668 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 669 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | if (data_length == 0) { |
| 671 | return PSA_ERROR_NOT_SUPPORTED; |
| 672 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | if (key_type_is_raw_bytes(type)) { |
| 675 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 678 | *bits); |
| 679 | if (status != PSA_SUCCESS) { |
| 680 | return status; |
| 681 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 682 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 683 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 685 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | return PSA_SUCCESS; |
| 689 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 690 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 691 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 692 | if (PSA_KEY_TYPE_IS_DH(type)) { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 693 | if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) { |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 694 | return PSA_ERROR_INVALID_ARGUMENT; |
| 695 | } |
Przemek Stekiel | 33c91eb | 2023-05-30 15:16:35 +0200 | [diff] [blame] | 696 | return mbedtls_psa_ffdh_import_key(attributes, |
| 697 | data, data_length, |
| 698 | key_buffer, key_buffer_size, |
| 699 | key_buffer_length, |
| 700 | bits); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 701 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 702 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 703 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 704 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 706 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 707 | return mbedtls_psa_ecp_import_key(attributes, |
| 708 | data, data_length, |
| 709 | key_buffer, key_buffer_size, |
| 710 | key_buffer_length, |
| 711 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 712 | } |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 713 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 714 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 715 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 716 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 717 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 718 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 719 | return mbedtls_psa_rsa_import_key(attributes, |
| 720 | data, data_length, |
| 721 | key_buffer, key_buffer_size, |
| 722 | key_buffer_length, |
| 723 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 724 | } |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 725 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 726 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 727 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 728 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 731 | } |
| 732 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 733 | /** Calculate the intersection of two algorithm usage policies. |
| 734 | * |
| 735 | * Return 0 (which allows no operation) on incompatibility. |
| 736 | */ |
| 737 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 738 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 739 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 741 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 742 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | if (alg1 == alg2) { |
| 744 | return alg1; |
| 745 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 746 | /* If the policies are from the same hash-and-sign family, check |
| 747 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 749 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 750 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 751 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 752 | return alg2; |
| 753 | } |
| 754 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 755 | return alg1; |
| 756 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 757 | } |
| 758 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 759 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 760 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 762 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 763 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 764 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 765 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 766 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 767 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 768 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 770 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 771 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 772 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 773 | } |
| 774 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 776 | (alg1_len <= alg2_len)) { |
| 777 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 778 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 780 | (alg2_len <= alg1_len)) { |
| 781 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | /* If the policies are from the same MAC family, check whether one |
| 785 | * of them is a minimum-MAC-length policy. Calculate the most |
| 786 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 788 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 789 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 790 | /* Validate the combination of key type and algorithm. Since the base |
| 791 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 793 | return 0; |
| 794 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 795 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 796 | /* Get the (exact or at-least) output lengths for both sides of the |
| 797 | * requested intersection. None of the currently supported algorithms |
| 798 | * have an output length dependent on the actual key size, so setting it |
| 799 | * to a bogus value of 0 is currently OK. |
| 800 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 801 | * Note that for at-least-this-length wildcard algorithms, the output |
| 802 | * length is set to the shortest allowed length, which allows us to |
| 803 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 805 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 806 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 807 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 808 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 810 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 811 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 812 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 813 | |
| 814 | /* If only one is an at-least-this-length policy, the intersection would |
| 815 | * be the other (fixed-length) policy as long as said fixed length is |
| 816 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 818 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 819 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 821 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 822 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 823 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 824 | /* If none of them are wildcards, check whether they define the same tag |
| 825 | * length. This is still possible here when one is default-length and |
| 826 | * the other specific-length. Ensure to always return the |
| 827 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | if (alg1_len == alg2_len) { |
| 829 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 830 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 831 | } |
| 832 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 837 | psa_algorithm_t policy_alg, |
| 838 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 839 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 840 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | if (requested_alg == policy_alg) { |
| 842 | return 1; |
| 843 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 844 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 845 | * and requested_alg is the same hash-and-sign family with any hash, |
| 846 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 848 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 849 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 850 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 851 | } |
| 852 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 853 | * the requested algorithm, check the requested tag length to be |
| 854 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 856 | PSA_ALG_IS_AEAD(requested_alg) && |
| 857 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 858 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 859 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 860 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 861 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 862 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 863 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 864 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 866 | PSA_ALG_IS_MAC(requested_alg) && |
| 867 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 868 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 869 | /* Validate the combination of key type and algorithm. Since the policy |
| 870 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 872 | return 0; |
| 873 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 874 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 875 | /* Get both the requested output length for the algorithm which is to be |
| 876 | * verified, and the default output length for the base algorithm. |
| 877 | * Note that none of the currently supported algorithms have an output |
| 878 | * length dependent on actual key size, so setting it to a bogus value |
| 879 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 880 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 882 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | key_type, 0, |
| 884 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 885 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 886 | /* If the policy is default-length, only allow an algorithm with |
| 887 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 889 | return requested_output_length == default_output_length; |
| 890 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 891 | |
| 892 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 893 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 895 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 896 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 899 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 900 | * check for the requested MAC length to be equal to or longer than the |
| 901 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 903 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 904 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 905 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 906 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 907 | /* If policy_alg is a generic key agreement operation, then using it for |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 908 | * a key derivation with that key agreement should also be allowed. This |
| 909 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 911 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 912 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 913 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 914 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 915 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 917 | } |
| 918 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 919 | /** Test whether a policy permits an algorithm. |
| 920 | * |
| 921 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 922 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 923 | * \note This function requires providing the key type for which the policy is |
| 924 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 925 | * have different properties depending on what kind of cipher it is |
| 926 | * combined with. |
| 927 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 928 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 929 | * allowed by the \p policy. |
| 930 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 931 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 932 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 933 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 935 | psa_key_type_t key_type, |
| 936 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 937 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 938 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | if (alg == 0) { |
| 940 | return PSA_ERROR_INVALID_ARGUMENT; |
| 941 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 942 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 943 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 944 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 945 | return PSA_ERROR_INVALID_ARGUMENT; |
| 946 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 948 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 949 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 950 | return PSA_SUCCESS; |
| 951 | } else { |
| 952 | return PSA_ERROR_NOT_PERMITTED; |
| 953 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 956 | /** Restrict a key policy based on a constraint. |
| 957 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 958 | * \note This function requires providing the key type for which the policy is |
| 959 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 960 | * have different properties depending on what kind of cipher it is |
| 961 | * combined with. |
| 962 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 963 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 964 | * \param[in,out] policy The policy to restrict. |
| 965 | * \param[in] constraint The policy constraint to apply. |
| 966 | * |
| 967 | * \retval #PSA_SUCCESS |
| 968 | * \c *policy contains the intersection of the original value of |
| 969 | * \c *policy and \c *constraint. |
| 970 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 971 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 972 | * \c *policy is unchanged. |
| 973 | */ |
| 974 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 975 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 976 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 978 | { |
| 979 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 981 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 982 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 984 | constraint->alg2); |
| 985 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 986 | return PSA_ERROR_INVALID_ARGUMENT; |
| 987 | } |
| 988 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 989 | return PSA_ERROR_INVALID_ARGUMENT; |
| 990 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 991 | policy->usage &= constraint->usage; |
| 992 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 993 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Przemek Stekiel | 061f694 | 2022-11-30 10:51:35 +0100 | [diff] [blame] | 997 | /** Get the description of a key given its identifier and policy constraints |
| 998 | * and lock it. |
| 999 | * |
| 1000 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1001 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1002 | * zero, the algorithm is not checked. |
| 1003 | * |
| 1004 | * In case of a persistent key, the function loads the description of the key |
| 1005 | * into a key slot if not already done. |
| 1006 | * |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1007 | * On success, the returned key slot has been registered for reading. |
| 1008 | * It is the responsibility of the caller to call psa_unregister_read(slot) |
| 1009 | * when they have finished reading the contents of the slot. |
Przemek Stekiel | 061f694 | 2022-11-30 10:51:35 +0100 | [diff] [blame] | 1010 | */ |
| 1011 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1012 | mbedtls_svc_key_id_t key, |
| 1013 | psa_key_slot_t **p_slot, |
| 1014 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1016 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1017 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 1018 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1021 | if (status != PSA_SUCCESS) { |
| 1022 | return status; |
| 1023 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1024 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* Enforce that usage policy for the key slot contains all the flags |
| 1027 | * required by the usage parameter. There is one exception: public |
| 1028 | * keys can always be exported, so we treat public key objects as |
| 1029 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1031 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1035 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1036 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1037 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1038 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1039 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | if (alg != 0) { |
| 1041 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1042 | slot->attr.type, |
| 1043 | alg); |
| 1044 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1045 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1047 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1050 | |
| 1051 | error: |
| 1052 | *p_slot = NULL; |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1053 | psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1055 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1056 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1057 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1058 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1059 | * |
| 1060 | * A transparent key is a key for which the key material is directly |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1061 | * available, as opposed to a key in a secure element and/or to be used |
| 1062 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1063 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1064 | * This is a temporary function that may be used instead of |
| 1065 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1066 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1067 | * |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1068 | * On success, the returned key slot has been registered for reading. |
| 1069 | * It is the responsibility of the caller to call psa_unregister_read(slot) |
| 1070 | * when they have finished reading the contents of the slot. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1071 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1072 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1073 | mbedtls_svc_key_id_t key, |
| 1074 | psa_key_slot_t **p_slot, |
| 1075 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1077 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1079 | usage, alg); |
| 1080 | if (status != PSA_SUCCESS) { |
| 1081 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1082 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1084 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1085 | psa_unregister_read(*p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | *p_slot = NULL; |
| 1087 | return PSA_ERROR_NOT_SUPPORTED; |
| 1088 | } |
| 1089 | |
| 1090 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1091 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1094 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1095 | if (slot->key.data != NULL) { |
Tom Cosgrove | 52f7e18 | 2023-08-01 09:08:48 +0100 | [diff] [blame] | 1096 | mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1098 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1099 | slot->key.data = NULL; |
| 1100 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1105 | /** Completely wipe a slot in memory, including its policy. |
| 1106 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1108 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1109 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | /* |
| 1112 | * As the return error code may not be handled in case of multiple errors, |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1113 | * do our best to report an unexpected amount of registered readers or |
| 1114 | * an unexpected state. |
| 1115 | * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for |
| 1116 | * wiping. |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1118 | * function is called as part of the execution of a test suite, the |
| 1119 | * execution of the test suite is stopped in error if the assertion fails. |
| 1120 | */ |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1121 | switch (slot->state) { |
| 1122 | case PSA_SLOT_FULL: |
| 1123 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1124 | * caller is the last reader. */ |
| 1125 | case PSA_SLOT_PENDING_DELETION: |
| 1126 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1127 | * caller is the last reader. */ |
| 1128 | if (slot->registered_readers != 1) { |
| 1129 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1); |
| 1130 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1131 | } |
| 1132 | break; |
| 1133 | case PSA_SLOT_FILLING: |
| 1134 | /* In this state registered_readers must be 0. */ |
| 1135 | if (slot->registered_readers != 0) { |
| 1136 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0); |
| 1137 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1138 | } |
| 1139 | break; |
| 1140 | case PSA_SLOT_EMPTY: |
| 1141 | /* The slot is already empty, it cannot be wiped. */ |
| 1142 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY); |
| 1143 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1144 | break; |
| 1145 | default: |
| 1146 | /* The slot's state is invalid. */ |
| 1147 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1150 | /* Multipart operations may still be using the key. This is safe |
| 1151 | * because all multipart operation objects are independent from |
| 1152 | * the key slot: if they need to access the key after the setup |
| 1153 | * phase, they have a copy of the key. Note that this means that |
| 1154 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1155 | /* At this point, key material and other type-specific content has |
| 1156 | * been wiped. Clear remaining metadata. We can call memset and not |
Ryan Everett | aa33c51 | 2023-12-21 17:32:07 +0000 | [diff] [blame] | 1157 | * zeroize because the metadata is not particularly sensitive. |
| 1158 | * This memset also sets the slot's state to PSA_SLOT_EMPTY. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | memset(slot, 0, sizeof(*slot)); |
| 1160 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1163 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1164 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1165 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1166 | psa_status_t status; /* status of the last operation */ |
| 1167 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1168 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1169 | psa_se_drv_table_entry_t *driver; |
| 1170 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1173 | return PSA_SUCCESS; |
| 1174 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1175 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1176 | /* |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1177 | * Get the description of the key in a key slot, and register to read it. |
| 1178 | * In the case of a persistent key, this will load the key description |
| 1179 | * from persistent memory if not done yet. |
| 1180 | * We cannot avoid this loading as without it we don't know if |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1181 | * the key is operated by an SE or not and this information is needed by |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1182 | * the current implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1184 | if (status != PSA_SUCCESS) { |
| 1185 | return status; |
| 1186 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1187 | |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1188 | /* Set the key slot containing the key description's state to |
| 1189 | * PENDING_DELETION. This stops new operations from registering |
| 1190 | * to read the slot. Current readers can safely continue to access |
| 1191 | * the key within the slot; the last registered reader will |
| 1192 | * automatically wipe the slot when they call psa_unregister_read(). |
| 1193 | * If the key is persistent, we can now delete the copy of the key |
| 1194 | * from memory. If the key is opaque, we require the driver to |
| 1195 | * deal with the deletion. */ |
Ryan Everett | c70ce57 | 2024-01-03 16:04:33 +0000 | [diff] [blame] | 1196 | slot->state = PSA_SLOT_PENDING_DELETION; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1199 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1200 | * if we attempt it, depending on whether the key is merely read-only |
| 1201 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1202 | * Just do the best we can, which is to wipe the copy in memory |
| 1203 | * (done in this function's cleanup code). */ |
| 1204 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1205 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1208 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1209 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1210 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1211 | /* For a key in a secure element, we need to do three things: |
| 1212 | * remove the key file in internal storage, destroy the |
| 1213 | * key inside the secure element, and update the driver's |
| 1214 | * persistent data. Start a transaction that will encompass these |
| 1215 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1217 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1219 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | status = psa_crypto_save_transaction(); |
| 1221 | if (status != PSA_SUCCESS) { |
| 1222 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1223 | /* We should still try to destroy the key in the secure |
| 1224 | * element and the key metadata in storage. This is especially |
| 1225 | * important if the error is that the storage is full. |
| 1226 | * But how to do it exactly without risking an inconsistent |
| 1227 | * state after a reset? |
| 1228 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1229 | */ |
| 1230 | overall_status = status; |
| 1231 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1234 | status = psa_destroy_se_key(driver, |
| 1235 | psa_key_slot_get_slot_number(slot)); |
| 1236 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1237 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1238 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1239 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1240 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1241 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1242 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ryan Everett | 4a0ba80 | 2024-01-17 14:12:33 +0000 | [diff] [blame] | 1244 | /* Destroy the copy of the persistent key from storage. |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1245 | * The slot will still hold a copy of the key until the last reader |
| 1246 | * unregisters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1248 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1249 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1250 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1251 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1252 | /* TODO: other slots may have a copy of the same key. We should |
| 1253 | * invalidate them. |
| 1254 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1255 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1256 | } |
| 1257 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1258 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1259 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | if (driver != NULL) { |
| 1261 | status = psa_save_se_persistent_data(driver); |
| 1262 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1263 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | } |
| 1265 | status = psa_crypto_stop_transaction(); |
| 1266 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1267 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1268 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1269 | } |
| 1270 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1271 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1272 | exit: |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1273 | /* Unregister from reading the slot. If we are the last active reader |
| 1274 | * then this will wipe the slot. */ |
| 1275 | status = psa_unregister_read(slot); |
| 1276 | /* Prioritize CORRUPTION_DETECTED from unregistering over |
| 1277 | * a storage error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1278 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1279 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1280 | } |
| 1281 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1284 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1285 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1286 | static psa_status_t psa_get_rsa_public_exponent( |
| 1287 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1289 | { |
| 1290 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1291 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1292 | uint8_t *buffer = NULL; |
| 1293 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1294 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1297 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1298 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1299 | } |
| 1300 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1301 | /* It's the default value, which is reported as an empty string, |
| 1302 | * so there's nothing to do. */ |
| 1303 | goto exit; |
| 1304 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | buflen = mbedtls_mpi_size(&mpi); |
| 1307 | buffer = mbedtls_calloc(1, buflen); |
| 1308 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1309 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1310 | goto exit; |
| 1311 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1312 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1313 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1314 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1316 | attributes->domain_parameters = buffer; |
| 1317 | attributes->domain_parameters_size = buflen; |
| 1318 | |
| 1319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1320 | mbedtls_mpi_free(&mpi); |
| 1321 | if (ret != 0) { |
| 1322 | mbedtls_free(buffer); |
| 1323 | } |
| 1324 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1325 | } |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1326 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1327 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1328 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1329 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1330 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1332 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1333 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1334 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1335 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1336 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1338 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1341 | if (status != PSA_SUCCESS) { |
| 1342 | return status; |
| 1343 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1344 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1345 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1347 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1348 | |
| 1349 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1350 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1351 | psa_set_key_slot_number(attributes, |
| 1352 | psa_key_slot_get_slot_number(slot)); |
| 1353 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1354 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | switch (slot->attr.type) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1357 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1358 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1359 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1360 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1361 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Pengyu Lv | f75893b | 2023-12-08 17:21:39 +0800 | [diff] [blame] | 1362 | /* TODO: This is a temporary situation where domain parameters are deprecated, |
| 1363 | * but we need it for namely generating an RSA key with a non-default exponent. |
| 1364 | * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494. |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1366 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1367 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1368 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1369 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | slot->attr.type, |
| 1371 | slot->key.data, |
| 1372 | slot->key.bytes, |
| 1373 | &rsa); |
| 1374 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1375 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | status = psa_get_rsa_public_exponent(rsa, |
| 1379 | attributes); |
| 1380 | mbedtls_rsa_free(rsa); |
| 1381 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1382 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1383 | break; |
Pengyu Lv | e9efbc2 | 2023-12-08 16:59:08 +0800 | [diff] [blame] | 1384 | #else |
| 1385 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
| 1386 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
| 1387 | attributes->domain_parameters = NULL; |
| 1388 | attributes->domain_parameters_size = SIZE_MAX; |
| 1389 | break; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1390 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1391 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1392 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1393 | default: |
| 1394 | /* Nothing else to do. */ |
| 1395 | break; |
| 1396 | } |
| 1397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | if (status != PSA_SUCCESS) { |
| 1399 | psa_reset_key_attributes(attributes); |
| 1400 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1401 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1402 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1407 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1408 | psa_status_t psa_get_key_slot_number( |
| 1409 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1410 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1411 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1413 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | return PSA_SUCCESS; |
| 1415 | } else { |
| 1416 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1417 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1418 | } |
| 1419 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1422 | size_t key_buffer_size, |
| 1423 | uint8_t *data, |
| 1424 | size_t data_size, |
| 1425 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1426 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | if (key_buffer_size > data_size) { |
| 1428 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1429 | } |
| 1430 | memcpy(data, key_buffer, key_buffer_size); |
| 1431 | memset(data + key_buffer_size, 0, |
| 1432 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1433 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1437 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1438 | const psa_key_attributes_t *attributes, |
| 1439 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1441 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1442 | psa_key_type_t type = attributes->core.type; |
| 1443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | if (key_type_is_raw_bytes(type) || |
| 1445 | PSA_KEY_TYPE_IS_RSA(type) || |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1446 | PSA_KEY_TYPE_IS_ECC(type) || |
| 1447 | PSA_KEY_TYPE_IS_DH(type)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | return psa_export_key_buffer_internal( |
| 1449 | key_buffer, key_buffer_size, |
| 1450 | data, data_size, data_length); |
| 1451 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1452 | /* This shouldn't happen in the reference implementation, but |
| 1453 | it is valid for a special-purpose implementation to omit |
| 1454 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1459 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1460 | uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | size_t data_size, |
| 1462 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1463 | { |
| 1464 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1465 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1466 | psa_key_slot_t *slot; |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1467 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1468 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1469 | /* Reject a zero-length output buffer now, since this can never be a |
| 1470 | * valid key representation. This way we know that data must be a valid |
| 1471 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | if (data_size == 0) { |
| 1473 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1474 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1475 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1476 | /* Set the key to empty now, so that even when there are errors, we always |
| 1477 | * set data_length to a value between 0 and data_size. On error, setting |
| 1478 | * the key to empty is a good choice because an empty key representation is |
| 1479 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1480 | *data_length = 0; |
| 1481 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1482 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1483 | * which don't require any flag, but |
| 1484 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1485 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1487 | PSA_KEY_USAGE_EXPORT, 0); |
| 1488 | if (status != PSA_SUCCESS) { |
| 1489 | return status; |
| 1490 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1491 | |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1492 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1493 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1494 | psa_key_attributes_t attributes = { |
| 1495 | .core = slot->attr |
| 1496 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | status = psa_driver_wrapper_export_key(&attributes, |
| 1498 | slot->key.data, slot->key.bytes, |
| 1499 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1500 | |
Ryan Everett | 35f6853 | 2024-01-25 12:02:03 +0000 | [diff] [blame] | 1501 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1502 | exit: |
Ryan Everett | 35f6853 | 2024-01-25 12:02:03 +0000 | [diff] [blame] | 1503 | #endif |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1504 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1505 | |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1506 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1510 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1511 | const psa_key_attributes_t *attributes, |
| 1512 | const uint8_t *key_buffer, |
| 1513 | size_t key_buffer_size, |
| 1514 | uint8_t *data, |
| 1515 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1516 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1517 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1518 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1519 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1520 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1521 | (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) || |
| 1522 | PSA_KEY_TYPE_IS_DH(type))) { |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1523 | /* Exporting public -> public */ |
| 1524 | return psa_export_key_buffer_internal( |
| 1525 | key_buffer, key_buffer_size, |
| 1526 | data, data_size, data_length); |
| 1527 | } else if (PSA_KEY_TYPE_IS_RSA(type)) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1528 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1529 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1530 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1531 | key_buffer, |
| 1532 | key_buffer_size, |
| 1533 | data, |
| 1534 | data_size, |
| 1535 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1536 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1537 | /* We don't know how to convert a private RSA key to public. */ |
| 1538 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1539 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1540 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1541 | } else if (PSA_KEY_TYPE_IS_ECC(type)) { |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1542 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1543 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1544 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1545 | key_buffer, |
| 1546 | key_buffer_size, |
| 1547 | data, |
| 1548 | data_size, |
| 1549 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1550 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1551 | /* We don't know how to convert a private ECC key to public */ |
| 1552 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1553 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1554 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1555 | } else if (PSA_KEY_TYPE_IS_DH(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1556 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1557 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 1558 | return mbedtls_psa_ffdh_export_public_key(attributes, |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1559 | key_buffer, |
| 1560 | key_buffer_size, |
| 1561 | data, data_size, |
| 1562 | data_length); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1563 | #else |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1564 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1565 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1566 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | } else { |
Przemek Stekiel | 0b11ee0 | 2023-05-16 13:26:06 +0200 | [diff] [blame] | 1568 | (void) key_buffer; |
| 1569 | (void) key_buffer_size; |
| 1570 | (void) data; |
| 1571 | (void) data_size; |
| 1572 | (void) data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1574 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1575 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1578 | uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | size_t data_size, |
| 1580 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1581 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1582 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1583 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1584 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1585 | psa_key_attributes_t attributes; |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1586 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1587 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1588 | /* Reject a zero-length output buffer now, since this can never be a |
| 1589 | * valid key representation. This way we know that data must be a valid |
| 1590 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | if (data_size == 0) { |
| 1592 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1593 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1594 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1595 | /* Set the key to empty now, so that even when there are errors, we always |
| 1596 | * set data_length to a value between 0 and data_size. On error, setting |
| 1597 | * the key to empty is a good choice because an empty key representation is |
| 1598 | * unlikely to be accepted anywhere. */ |
| 1599 | *data_length = 0; |
| 1600 | |
| 1601 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1603 | if (status != PSA_SUCCESS) { |
| 1604 | return status; |
| 1605 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1606 | |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1607 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1609 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1610 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1611 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1612 | } |
| 1613 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1614 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1615 | .core = slot->attr |
| 1616 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1617 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1618 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1620 | |
| 1621 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1622 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1623 | |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1624 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1626 | } |
| 1627 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1628 | MBEDTLS_STATIC_ASSERT( |
| 1629 | (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1630 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1631 | MBEDTLS_STATIC_ASSERT( |
| 1632 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1633 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1634 | MBEDTLS_STATIC_ASSERT( |
| 1635 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1636 | "One or more key attribute flag is listed as both internal-only and external-only") |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1638 | /** Validate that a key policy is internally well-formed. |
| 1639 | * |
| 1640 | * This function only rejects invalid policies. It does not validate the |
| 1641 | * consistency of the policy with respect to other attributes of the key |
| 1642 | * such as the key type. |
| 1643 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1646 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1647 | PSA_KEY_USAGE_COPY | |
| 1648 | PSA_KEY_USAGE_ENCRYPT | |
| 1649 | PSA_KEY_USAGE_DECRYPT | |
| 1650 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1651 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1652 | PSA_KEY_USAGE_SIGN_HASH | |
| 1653 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1654 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1655 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1656 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1657 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1662 | /** Validate the internal consistency of key attributes. |
| 1663 | * |
| 1664 | * This function only rejects invalid attribute values. If does not |
| 1665 | * validate the consistency of the attributes with any key data that may |
| 1666 | * be involved in the creation of the key. |
| 1667 | * |
| 1668 | * Call this function early in the key creation process. |
| 1669 | * |
| 1670 | * \param[in] attributes Key attributes for the new key. |
| 1671 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1672 | * NULL for a transparent key. |
| 1673 | * |
| 1674 | */ |
| 1675 | static psa_status_t psa_validate_key_attributes( |
| 1676 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1678 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1679 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1680 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1681 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | status = psa_validate_key_location(lifetime, p_drv); |
| 1684 | if (status != PSA_SUCCESS) { |
| 1685 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1686 | } |
| 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | status = psa_validate_key_persistence(lifetime); |
| 1689 | if (status != PSA_SUCCESS) { |
| 1690 | return status; |
| 1691 | } |
| 1692 | |
| 1693 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1694 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1695 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1696 | } |
| 1697 | } else { |
| 1698 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1699 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1704 | if (status != PSA_SUCCESS) { |
| 1705 | return status; |
| 1706 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Refuse to create overly large keys. |
| 1709 | * Note that this doesn't trigger on import if the attributes don't |
| 1710 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1711 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1713 | return PSA_ERROR_NOT_SUPPORTED; |
| 1714 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1715 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1716 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1718 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1719 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1720 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1725 | /** Prepare a key slot to receive key material. |
| 1726 | * |
| 1727 | * This function allocates a key slot and sets its metadata. |
| 1728 | * |
| 1729 | * If this function fails, call psa_fail_key_creation(). |
| 1730 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1731 | * This function is intended to be used as follows: |
| 1732 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1733 | * it with the specified attributes, and in case of a volatile key assign it |
| 1734 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1735 | * -# Populate the slot with the key material. |
| 1736 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1737 | * In case of failure at any step, stop the sequence and call |
| 1738 | * psa_fail_key_creation(). |
| 1739 | * |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1740 | * On success, the key slot's state is PSA_SLOT_FILLING. |
| 1741 | * It is the responsibility of the caller to change the slot's state to |
| 1742 | * PSA_SLOT_EMPTY/FULL once key creation has finished. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1743 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1744 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1745 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1746 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1747 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1748 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1749 | * |
| 1750 | * \retval #PSA_SUCCESS |
| 1751 | * The key slot is ready to receive key material. |
| 1752 | * \return If this function fails, the key slot is an invalid state. |
| 1753 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1754 | */ |
| 1755 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1756 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1757 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1758 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1760 | { |
| 1761 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1762 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1763 | psa_key_slot_t *slot; |
| 1764 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1765 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1766 | *p_drv = NULL; |
| 1767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1769 | if (status != PSA_SUCCESS) { |
| 1770 | return status; |
| 1771 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1772 | |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1773 | status = psa_reserve_free_key_slot(&volatile_key_id, p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | if (status != PSA_SUCCESS) { |
| 1775 | return status; |
| 1776 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1777 | slot = *p_slot; |
| 1778 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1779 | /* We're storing the declared bit-size of the key. It's up to each |
| 1780 | * creation mechanism to verify that this information is correct. |
| 1781 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1782 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1783 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1784 | * volatile key identifier associated to the slot returned to contain its |
| 1785 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1786 | |
| 1787 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1789 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1790 | slot->attr.id = volatile_key_id; |
| 1791 | #else |
| 1792 | slot->attr.id.key_id = volatile_key_id; |
| 1793 | #endif |
| 1794 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1795 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1796 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1797 | * external-only flags, query `attributes`. Thanks to the check |
| 1798 | * in psa_validate_key_attributes(), this leaves the dual-use |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1799 | * flags and any internal flag that psa_reserve_free_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1800 | * may have set. */ |
| 1801 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1802 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1803 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1804 | /* For a key in a secure element, we need to do three things |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1805 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1806 | * create the key file in internal storage, create the |
| 1807 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1808 | * persistent data. This is done by starting a transaction that will |
| 1809 | * encompass these three actions. |
| 1810 | * For registering a volatile key, we just need to find an appropriate |
| 1811 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1812 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1813 | /* The first thing to do is to find a slot number for the new key. |
| 1814 | * We save the slot number in persistent storage as part of the |
| 1815 | * transaction data. It will be needed to recover if the power |
| 1816 | * fails during the key creation process, to clean up on the secure |
| 1817 | * element side after restarting. Obtaining a slot number from the |
| 1818 | * secure element driver updates its persistent state, but we do not yet |
| 1819 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1820 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1822 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1824 | &slot_number); |
| 1825 | if (status != PSA_SUCCESS) { |
| 1826 | return status; |
| 1827 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1830 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1831 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1832 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1833 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | status = psa_crypto_save_transaction(); |
| 1835 | if (status != PSA_SUCCESS) { |
| 1836 | (void) psa_crypto_stop_transaction(); |
| 1837 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1838 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1839 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1840 | |
| 1841 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1843 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1846 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1848 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1849 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1852 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1853 | |
| 1854 | /** Finalize the creation of a key once its key material has been set. |
| 1855 | * |
| 1856 | * This entails writing the key to persistent storage. |
| 1857 | * |
| 1858 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1859 | * See the documentation of psa_start_key_creation() for the intended use |
| 1860 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1861 | * |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1862 | * If the finalization succeeds, the function sets the key slot's state to |
| 1863 | * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the |
| 1864 | * key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1865 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1866 | * \param[in,out] slot Pointer to the slot with key material. |
| 1867 | * \param[in] driver The secure element driver for the key, |
| 1868 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1869 | * \param[out] key On success, identifier of the key. Note that the |
| 1870 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1871 | * |
| 1872 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1873 | * The key was successfully created. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1874 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1875 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1876 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1877 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1878 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1879 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1880 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1881 | * \return If this function fails, the key slot is an invalid state. |
| 1882 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1883 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1884 | static psa_status_t psa_finish_key_creation( |
| 1885 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1886 | psa_se_drv_table_entry_t *driver, |
| 1887 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1888 | { |
| 1889 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1890 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1891 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1892 | |
| 1893 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1895 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1897 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1898 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1900 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1901 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1902 | sizeof(data.slot_number), |
| 1903 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1906 | status = psa_save_persistent_key(&slot->attr, |
| 1907 | (uint8_t *) &data, |
| 1908 | sizeof(data)); |
| 1909 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1910 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1911 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1912 | /* Key material is saved in export representation in the slot, so |
| 1913 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1914 | status = psa_save_persistent_key(&slot->attr, |
| 1915 | slot->key.data, |
| 1916 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1917 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1918 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1919 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1920 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1921 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1922 | /* Finish the transaction for a key creation. This does not |
| 1923 | * happen when registering an existing key. Detect this case |
| 1924 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1925 | * creation of a persistent key in a secure element requires a transaction, |
| 1926 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | if (driver != NULL && |
| 1928 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1929 | status = psa_save_se_persistent_data(driver); |
| 1930 | if (status != PSA_SUCCESS) { |
| 1931 | psa_destroy_persistent_key(slot->attr.id); |
| 1932 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1933 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1935 | } |
| 1936 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1939 | *key = slot->attr.id; |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1940 | status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING, |
| 1941 | PSA_SLOT_FULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1943 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1945 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** Abort the creation of a key. |
| 1951 | * |
| 1952 | * You may call this function after calling psa_start_key_creation(), |
| 1953 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1954 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1955 | * See the documentation of psa_start_key_creation() for the intended use |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1956 | * of this function. Sets the slot's state to PSA_SLOT_EMPTY. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1957 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1958 | * \param[in,out] slot Pointer to the slot with key material. |
| 1959 | * \param[in] driver The secure element driver for the key, |
| 1960 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1961 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1963 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1964 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1965 | (void) driver; |
| 1966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1967 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1968 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1970 | |
| 1971 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1972 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1973 | * element, and the failure happened later (when saving metadata |
| 1974 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1975 | * element. |
| 1976 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1977 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1978 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1979 | /* Abort the ongoing transaction if any (there may not be one if |
| 1980 | * the creation process failed before starting one, or if the |
| 1981 | * key creation is a registration of a key in a secure element). |
| 1982 | * Earlier functions must already have done what it takes to undo any |
| 1983 | * partial creation. All that's left is to update the transaction data |
| 1984 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1986 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1991 | /** Validate optional attributes during key creation. |
| 1992 | * |
| 1993 | * Some key attributes are optional during key creation. If they are |
| 1994 | * specified in the attributes structure, check that they are consistent |
| 1995 | * with the data in the slot. |
| 1996 | * |
| 1997 | * This function should be called near the end of key creation, after |
| 1998 | * the slot in memory is fully populated but before saving persistent data. |
| 1999 | */ |
| 2000 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2001 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2002 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2003 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | if (attributes->core.type != 0) { |
| 2005 | if (attributes->core.type != slot->attr.type) { |
| 2006 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2007 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2008 | } |
| 2009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2010 | if (attributes->domain_parameters_size != 0) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2011 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 2012 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2014 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2015 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2016 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2017 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2018 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2019 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | slot->attr.type, |
| 2021 | slot->key.data, |
| 2022 | slot->key.bytes, |
| 2023 | &rsa); |
| 2024 | if (status != PSA_SUCCESS) { |
| 2025 | return status; |
| 2026 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | mbedtls_mpi_init(&actual); |
| 2029 | mbedtls_mpi_init(&required); |
| 2030 | ret = mbedtls_rsa_export(rsa, |
| 2031 | NULL, NULL, NULL, NULL, &actual); |
| 2032 | mbedtls_rsa_free(rsa); |
| 2033 | mbedtls_free(rsa); |
| 2034 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2035 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | } |
| 2037 | ret = mbedtls_mpi_read_binary(&required, |
| 2038 | attributes->domain_parameters, |
| 2039 | attributes->domain_parameters_size); |
| 2040 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2041 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | } |
| 2043 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2044 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | } |
| 2046 | rsa_exit: |
| 2047 | mbedtls_mpi_free(&actual); |
| 2048 | mbedtls_mpi_free(&required); |
| 2049 | if (ret != 0) { |
| 2050 | return mbedtls_to_psa_error(ret); |
| 2051 | } |
| 2052 | } else |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2053 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 2054 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2055 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2056 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | if (attributes->core.bits != 0) { |
| 2062 | if (attributes->core.bits != slot->attr.bits) { |
| 2063 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2064 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2071 | const uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | size_t data_length, |
| 2073 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2074 | { |
| 2075 | psa_status_t status; |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2076 | LOCAL_INPUT_DECLARE(data_external, data); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2077 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2078 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2079 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2080 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2081 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2082 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2083 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2084 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2085 | * This also rejects any key which might be encoded as an empty string, |
| 2086 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | if (data_length == 0) { |
| 2088 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2089 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2090 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2091 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | if (data_length > SIZE_MAX / 8) { |
| 2093 | return PSA_ERROR_NOT_SUPPORTED; |
| 2094 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2095 | |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2096 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2099 | &slot, &driver); |
| 2100 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2101 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2103 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2104 | /* In the case of a transparent key or an opaque key stored in local |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2105 | * storage ( thus not in the case of importing a key in a secure element |
| 2106 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 2107 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2108 | if (slot->key.data == NULL) { |
| 2109 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2110 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | attributes, data, data_length, &storage_size); |
| 2112 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2113 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2115 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 2117 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2118 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2120 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2121 | |
| 2122 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | status = psa_driver_wrapper_import_key(attributes, |
| 2124 | data, data_length, |
| 2125 | slot->key.data, |
| 2126 | slot->key.bytes, |
| 2127 | &slot->key.bytes, &bits); |
| 2128 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2129 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2132 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2133 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2135 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2136 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2137 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2138 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2139 | /* Enforce a size limit, and in particular ensure that the bit |
| 2140 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2142 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2143 | goto exit; |
| 2144 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | status = psa_validate_optional_attributes(slot, attributes); |
| 2146 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2147 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2151 | exit: |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2152 | LOCAL_INPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | if (status != PSA_SUCCESS) { |
| 2154 | psa_fail_key_creation(slot, driver); |
| 2155 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2157 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2158 | } |
| 2159 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2160 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2161 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2163 | { |
| 2164 | psa_status_t status; |
| 2165 | psa_key_slot_t *slot = NULL; |
| 2166 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2167 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2168 | |
| 2169 | /* Leaving attributes unspecified is not currently supported. |
| 2170 | * It could make sense to query the key type and size from the |
| 2171 | * secure element, but not all secure elements support this |
| 2172 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2174 | return PSA_ERROR_NOT_SUPPORTED; |
| 2175 | } |
| 2176 | if (psa_get_key_bits(attributes) == 0) { |
| 2177 | return PSA_ERROR_NOT_SUPPORTED; |
| 2178 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2181 | &slot, &driver); |
| 2182 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2183 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2186 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2187 | |
| 2188 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | if (status != PSA_SUCCESS) { |
| 2190 | psa_fail_key_creation(slot, driver); |
| 2191 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2192 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2193 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | psa_close_key(key); |
| 2195 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2196 | } |
| 2197 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2200 | const psa_key_attributes_t *specified_attributes, |
| 2201 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2202 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2203 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2204 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2205 | psa_key_slot_t *source_slot = NULL; |
| 2206 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2207 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2208 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2209 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2210 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2211 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2212 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2213 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2215 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2216 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | status = psa_validate_optional_attributes(source_slot, |
| 2220 | specified_attributes); |
| 2221 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2222 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2224 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2225 | /* The target key type and number of bits have been validated by |
| 2226 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2227 | * equal to the ones of the source key. So it is safe to inherit |
| 2228 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2229 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2230 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2231 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2232 | |
| 2233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2235 | &actual_attributes.core.policy, |
| 2236 | &source_slot->attr.policy); |
| 2237 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2238 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2242 | &target_slot, &driver); |
| 2243 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2244 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | } |
| 2246 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2247 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2248 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2249 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2250 | * the source key would need to be exported as plaintext and re-imported |
| 2251 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2252 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2253 | * appropriate API invocations from the application, if needed. |
| 2254 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2255 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2256 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2257 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2258 | /* |
| 2259 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2260 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2261 | * - For opaque keys this translates to an invocation of the drivers' |
| 2262 | * copy_key entry point through the dispatch layer. |
| 2263 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2265 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2266 | &storage_size); |
| 2267 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2268 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2271 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2272 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2273 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2277 | source_slot->key.data, |
| 2278 | source_slot->key.bytes, |
| 2279 | target_slot->key.data, |
| 2280 | target_slot->key.bytes, |
| 2281 | &target_slot->key.bytes); |
| 2282 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2283 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | } |
| 2285 | } else { |
| 2286 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2287 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | source_slot->key.bytes); |
| 2289 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2290 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2292 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2294 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | if (status != PSA_SUCCESS) { |
| 2296 | psa_fail_key_creation(target_slot, driver); |
| 2297 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2298 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2299 | unlock_status = psa_unregister_read(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2304 | |
| 2305 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2306 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2307 | /* Message digests */ |
| 2308 | /****************************************************************/ |
| 2309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2311 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2312 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | if (operation->id == 0) { |
| 2314 | return PSA_SUCCESS; |
| 2315 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2318 | operation->id = 0; |
| 2319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2321 | } |
| 2322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2323 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2324 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2325 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2326 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2327 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2328 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2330 | status = PSA_ERROR_BAD_STATE; |
| 2331 | goto exit; |
| 2332 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2335 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2336 | goto exit; |
| 2337 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2338 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2339 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2340 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2344 | |
| 2345 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | if (status != PSA_SUCCESS) { |
| 2347 | psa_hash_abort(operation); |
| 2348 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2349 | |
| 2350 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2351 | } |
| 2352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2354 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2356 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2357 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2358 | LOCAL_INPUT_DECLARE(input_external, input); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2361 | status = PSA_ERROR_BAD_STATE; |
| 2362 | goto exit; |
| 2363 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2364 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2365 | /* Don't require hash implementations to behave correctly on a |
| 2366 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | if (input_length == 0) { |
| 2368 | return PSA_SUCCESS; |
| 2369 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2370 | |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2371 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2372 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2373 | |
| 2374 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | if (status != PSA_SUCCESS) { |
| 2376 | psa_hash_abort(operation); |
| 2377 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2378 | |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2379 | LOCAL_INPUT_FREE(input_external, input); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2381 | } |
| 2382 | |
Thomas Daubney | 31d8c0b | 2024-01-18 15:26:59 +0000 | [diff] [blame] | 2383 | static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation, |
Thomas Daubney | d241156 | 2024-01-25 17:25:07 +0000 | [diff] [blame] | 2384 | uint8_t *hash, |
| 2385 | size_t hash_size, |
| 2386 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2387 | { |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2388 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2389 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2390 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | if (operation->id == 0) { |
| 2392 | return PSA_ERROR_BAD_STATE; |
| 2393 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2394 | |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2395 | status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | operation, hash, hash_size, hash_length); |
| 2397 | psa_hash_abort(operation); |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2398 | |
| 2399 | return status; |
| 2400 | } |
| 2401 | |
| 2402 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2403 | uint8_t *hash_external, |
| 2404 | size_t hash_size, |
| 2405 | size_t *hash_length) |
| 2406 | { |
| 2407 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2408 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2409 | |
| 2410 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2411 | status = psa_hash_finish_internal(operation, hash, hash_size, hash_length); |
| 2412 | |
Thomas Daubney | dedd100 | 2024-01-25 16:52:50 +0000 | [diff] [blame] | 2413 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2414 | exit: |
Thomas Daubney | dedd100 | 2024-01-25 16:52:50 +0000 | [diff] [blame] | 2415 | #endif |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2416 | LOCAL_OUTPUT_FREE(hash_external, hash); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2418 | } |
| 2419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2421 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2423 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2424 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2425 | size_t actual_hash_length; |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2426 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2427 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 2428 | |
| 2429 | status = psa_hash_finish_internal( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | operation, |
| 2431 | actual_hash, sizeof(actual_hash), |
| 2432 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2435 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2439 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2440 | goto exit; |
| 2441 | } |
| 2442 | |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2443 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2444 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2445 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2446 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2447 | |
| 2448 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2450 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2451 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | } |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2453 | LOCAL_INPUT_FREE(hash_external, hash); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2455 | } |
| 2456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2458 | const uint8_t *input_external, size_t input_length, |
| 2459 | uint8_t *hash_external, size_t hash_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2460 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2461 | { |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2462 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2463 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2464 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2465 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2466 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2468 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2469 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2470 | |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2471 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2472 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2473 | status = psa_driver_wrapper_hash_compute(alg, input, input_length, |
Thomas Daubney | d241156 | 2024-01-25 17:25:07 +0000 | [diff] [blame] | 2474 | hash, hash_size, hash_length); |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2475 | |
Thomas Daubney | dedd100 | 2024-01-25 16:52:50 +0000 | [diff] [blame] | 2476 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2477 | exit: |
Thomas Daubney | dedd100 | 2024-01-25 16:52:50 +0000 | [diff] [blame] | 2478 | #endif |
Thomas Daubney | 1c5118e | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2479 | LOCAL_INPUT_FREE(input_external, input); |
| 2480 | LOCAL_OUTPUT_FREE(hash_external, hash); |
| 2481 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2482 | } |
| 2483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2484 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2485 | const uint8_t *input_external, size_t input_length, |
| 2486 | const uint8_t *hash_external, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2487 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2488 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2489 | size_t actual_hash_length; |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2490 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2491 | |
| 2492 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2493 | LOCAL_INPUT_DECLARE(hash_external, hash); |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | if (!PSA_ALG_IS_HASH(alg)) { |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2496 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2497 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2499 | |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2500 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2501 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | alg, input, input_length, |
| 2503 | actual_hash, sizeof(actual_hash), |
| 2504 | &actual_hash_length); |
| 2505 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2506 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | } |
| 2508 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2509 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2510 | goto exit; |
| 2511 | } |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2512 | |
| 2513 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2514 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2515 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2517 | |
| 2518 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2519 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
Thomas Daubney | 51ffac9 | 2024-01-18 15:46:26 +0000 | [diff] [blame] | 2520 | |
| 2521 | LOCAL_INPUT_FREE(input_external, input); |
| 2522 | LOCAL_INPUT_FREE(hash_external, hash); |
| 2523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2525 | } |
| 2526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2527 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2528 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2529 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | if (source_operation->id == 0 || |
| 2531 | target_operation->id != 0) { |
| 2532 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2533 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2536 | target_operation); |
| 2537 | if (status != PSA_SUCCESS) { |
| 2538 | psa_hash_abort(target_operation); |
| 2539 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2542 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2543 | |
| 2544 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2545 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | /* MAC */ |
| 2547 | /****************************************************************/ |
| 2548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2550 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2551 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | if (operation->id == 0) { |
| 2553 | return PSA_SUCCESS; |
| 2554 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2557 | operation->mac_size = 0; |
| 2558 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2559 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2564 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2565 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2566 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2567 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2568 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2569 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2570 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2571 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2574 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2575 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2576 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2577 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | status = psa_mac_key_can_do(alg, key_type); |
| 2579 | if (status != PSA_SUCCESS) { |
| 2580 | return status; |
| 2581 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2582 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2583 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2584 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2586 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2587 | /* A very short MAC is too short for security since it can be |
| 2588 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2589 | * so we make this our minimum, even though 32 bits is still |
| 2590 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2591 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2592 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2595 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2596 | /* It's impossible to "truncate" to a larger length than the full length |
| 2597 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2602 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2603 | * that is disabled in the compile-time configuration. The result can |
| 2604 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2605 | * configuration into account. In this case, force a return of |
| 2606 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2607 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2608 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2609 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2610 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2618 | mbedtls_svc_key_id_t key, |
| 2619 | psa_algorithm_t alg, |
| 2620 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2621 | { |
| 2622 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2623 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2624 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2625 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2626 | |
| 2627 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2629 | status = PSA_ERROR_BAD_STATE; |
| 2630 | goto exit; |
| 2631 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2632 | |
| 2633 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | key, |
| 2635 | &slot, |
| 2636 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2637 | alg); |
| 2638 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2639 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2641 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2642 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2643 | .core = slot->attr |
| 2644 | }; |
| 2645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2647 | &operation->mac_size); |
| 2648 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2649 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2651 | |
| 2652 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2653 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | if (is_sign) { |
| 2655 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2656 | &attributes, |
| 2657 | slot->key.data, |
| 2658 | slot->key.bytes, |
| 2659 | alg); |
| 2660 | } else { |
| 2661 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2662 | &attributes, |
| 2663 | slot->key.data, |
| 2664 | slot->key.bytes, |
| 2665 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2666 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2667 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | if (status != PSA_SUCCESS) { |
| 2670 | psa_mac_abort(operation); |
| 2671 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2672 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2673 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2675 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2676 | } |
| 2677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2679 | mbedtls_svc_key_id_t key, |
| 2680 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2681 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2682 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2686 | mbedtls_svc_key_id_t key, |
| 2687 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2688 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2690 | } |
| 2691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2692 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2693 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2694 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2695 | { |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2696 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2697 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2699 | if (operation->id == 0) { |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2700 | status = PSA_ERROR_BAD_STATE; |
| 2701 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2703 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2704 | /* Don't require hash implementations to behave correctly on a |
| 2705 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | if (input_length == 0) { |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2707 | status = PSA_SUCCESS; |
| 2708 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2710 | |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2711 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2712 | status = psa_driver_wrapper_mac_update(operation, input, input_length); |
| 2713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2714 | if (status != PSA_SUCCESS) { |
| 2715 | psa_mac_abort(operation); |
| 2716 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2717 | |
Thomas Daubney | c6705c6 | 2024-01-30 11:21:47 +0000 | [diff] [blame] | 2718 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2719 | exit: |
Thomas Daubney | c6705c6 | 2024-01-30 11:21:47 +0000 | [diff] [blame] | 2720 | #endif |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2721 | LOCAL_INPUT_FREE(input_external, input); |
| 2722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2727 | uint8_t *mac_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | size_t mac_size, |
| 2729 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2730 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2731 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2732 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2733 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
Thomas Daubney | 1ffc5cb | 2024-01-31 18:09:36 +0000 | [diff] [blame] | 2734 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2737 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2738 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2739 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2742 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2743 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2744 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2745 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2746 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2747 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2749 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2750 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2751 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2754 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2755 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2756 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2757 | |
Thomas Daubney | 1ffc5cb | 2024-01-31 18:09:36 +0000 | [diff] [blame] | 2758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2760 | mac, operation->mac_size, |
| 2761 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2762 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2763 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2764 | /* In case of success, set the potential excess room in the output buffer |
| 2765 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2766 | * In case of error, set the output length and content to a safe default, |
| 2767 | * such that in case the caller misses an error check, the output would be |
| 2768 | * an unachievable MAC. |
| 2769 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2771 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2772 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2773 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2774 | |
Thomas Daubney | 03f1ea3 | 2024-02-01 16:16:27 +0000 | [diff] [blame] | 2775 | if (mac != NULL) { |
Thomas Daubney | 1ffc5cb | 2024-01-31 18:09:36 +0000 | [diff] [blame] | 2776 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
| 2777 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2779 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2780 | LOCAL_OUTPUT_FREE(mac_external, mac); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2786 | const uint8_t *mac_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2788 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2789 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2790 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2791 | LOCAL_INPUT_DECLARE(mac_external, mac); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2793 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2794 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2795 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2796 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2798 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2799 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2800 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2801 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2803 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2804 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2805 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2806 | } |
| 2807 | |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2808 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2810 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2811 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2812 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2814 | LOCAL_INPUT_FREE(mac_external, mac); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2817 | } |
| 2818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2820 | psa_algorithm_t alg, |
| 2821 | const uint8_t *input, |
| 2822 | size_t input_length, |
| 2823 | uint8_t *mac, |
| 2824 | size_t mac_size, |
| 2825 | size_t *mac_length, |
| 2826 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2827 | { |
| 2828 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2829 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2830 | psa_key_slot_t *slot; |
| 2831 | uint8_t operation_mac_size = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2832 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2833 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2834 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | key, |
| 2836 | &slot, |
| 2837 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2838 | alg); |
| 2839 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2840 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2841 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2842 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2843 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2844 | .core = slot->attr |
| 2845 | }; |
| 2846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2848 | &operation_mac_size); |
| 2849 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2850 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2854 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2855 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | &attributes, |
| 2860 | slot->key.data, slot->key.bytes, |
| 2861 | alg, |
| 2862 | input, input_length, |
| 2863 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2864 | |
| 2865 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2866 | /* In case of success, set the potential excess room in the output buffer |
| 2867 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2868 | * In case of error, set the output length and content to a safe default, |
| 2869 | * such that in case the caller misses an error check, the output would be |
| 2870 | * an unachievable MAC. |
| 2871 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2873 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2874 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2875 | } |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2876 | |
| 2877 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2878 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2879 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2881 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2882 | } |
| 2883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2885 | psa_algorithm_t alg, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2886 | const uint8_t *input_external, |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2887 | size_t input_length, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2888 | uint8_t *mac_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | size_t mac_size, |
| 2890 | size_t *mac_length) |
| 2891 | { |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2892 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2893 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2894 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
| 2895 | |
| 2896 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2897 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
| 2898 | status = psa_mac_compute_internal(key, alg, |
Thomas Daubney | 7480a74 | 2024-01-30 11:29:47 +0000 | [diff] [blame] | 2899 | input, input_length, |
| 2900 | mac, mac_size, mac_length, 1); |
Thomas Daubney | c6705c6 | 2024-01-30 11:21:47 +0000 | [diff] [blame] | 2901 | |
| 2902 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2903 | exit: |
Thomas Daubney | c6705c6 | 2024-01-30 11:21:47 +0000 | [diff] [blame] | 2904 | #endif |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2905 | LOCAL_INPUT_FREE(input_external, input); |
| 2906 | LOCAL_OUTPUT_FREE(mac_external, mac); |
| 2907 | |
| 2908 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2912 | psa_algorithm_t alg, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2913 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2914 | size_t input_length, |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2915 | const uint8_t *mac_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2916 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2917 | { |
| 2918 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2919 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2920 | size_t actual_mac_length; |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2921 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2922 | LOCAL_INPUT_DECLARE(mac_external, mac); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2923 | |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2924 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | status = psa_mac_compute_internal(key, alg, |
| 2926 | input, input_length, |
| 2927 | actual_mac, sizeof(actual_mac), |
| 2928 | &actual_mac_length, 0); |
| 2929 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2930 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2934 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2935 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2936 | } |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2937 | |
| 2938 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2939 | if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2940 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2941 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2942 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2943 | |
| 2944 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2945 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
Thomas Daubney | 8db8d1a | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2946 | LOCAL_INPUT_FREE(input_external, input); |
| 2947 | LOCAL_INPUT_FREE(mac_external, mac); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2950 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2951 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2952 | /****************************************************************/ |
| 2953 | /* Asymmetric cryptography */ |
| 2954 | /****************************************************************/ |
| 2955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2957 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2958 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2959 | if (input_is_message) { |
| 2960 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2961 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2962 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2965 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2966 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2967 | } |
| 2968 | } |
| 2969 | } else { |
| 2970 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2971 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2972 | } |
| 2973 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2976 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2979 | int input_is_message, |
| 2980 | psa_algorithm_t alg, |
| 2981 | const uint8_t *input, |
| 2982 | size_t input_length, |
| 2983 | uint8_t *signature, |
| 2984 | size_t signature_size, |
| 2985 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2986 | { |
| 2987 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2988 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2989 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2990 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2991 | |
| 2992 | *signature_length = 0; |
| 2993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2995 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2996 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2998 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2999 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 3000 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3001 | * buffer can in principle be empty since it doesn't actually have |
| 3002 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | if (signature_size == 0) { |
| 3004 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 3005 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3006 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3007 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | key, &slot, |
| 3009 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 3010 | PSA_KEY_USAGE_SIGN_HASH, |
| 3011 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3013 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3014 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3018 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3019 | goto exit; |
| 3020 | } |
| 3021 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3022 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3023 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3024 | }; |
| 3025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3027 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3028 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3029 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | signature, signature_size, signature_length); |
| 3031 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3032 | |
| 3033 | status = psa_driver_wrapper_sign_hash( |
| 3034 | &attributes, slot->key.data, slot->key.bytes, |
| 3035 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3037 | } |
| 3038 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3039 | |
| 3040 | exit: |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3041 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3042 | *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3043 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3044 | unlock_status = psa_unregister_read(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3047 | } |
| 3048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 3050 | int input_is_message, |
| 3051 | psa_algorithm_t alg, |
| 3052 | const uint8_t *input, |
| 3053 | size_t input_length, |
| 3054 | const uint8_t *signature, |
| 3055 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3056 | { |
| 3057 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3058 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3059 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3061 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3062 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3063 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3064 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3065 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3066 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | key, &slot, |
| 3068 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 3069 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3070 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | if (status != PSA_SUCCESS) { |
| 3073 | return status; |
| 3074 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3075 | |
| 3076 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3078 | }; |
| 3079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3081 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3082 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3083 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | signature, signature_length); |
| 3085 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3086 | status = psa_driver_wrapper_verify_hash( |
| 3087 | &attributes, slot->key.data, slot->key.bytes, |
| 3088 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3090 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3091 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3092 | unlock_status = psa_unregister_read(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3095 | |
| 3096 | } |
| 3097 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3098 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3099 | const psa_key_attributes_t *attributes, |
| 3100 | const uint8_t *key_buffer, |
| 3101 | size_t key_buffer_size, |
| 3102 | psa_algorithm_t alg, |
| 3103 | const uint8_t *input, |
| 3104 | size_t input_length, |
| 3105 | uint8_t *signature, |
| 3106 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3107 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3108 | { |
| 3109 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3112 | size_t hash_length; |
| 3113 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3114 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3115 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3117 | input, input_length, |
| 3118 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3121 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3123 | |
| 3124 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | attributes, key_buffer, key_buffer_size, |
| 3126 | alg, hash, hash_length, |
| 3127 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3128 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3131 | } |
| 3132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3134 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3135 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3137 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3138 | size_t signature_size, |
| 3139 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3140 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3141 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3142 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3143 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3144 | |
| 3145 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3146 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3147 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3148 | signature_size, signature_length); |
| 3149 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3150 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3151 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3152 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3153 | LOCAL_INPUT_FREE(input_external, input); |
| 3154 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3155 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3156 | } |
| 3157 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3158 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3159 | const psa_key_attributes_t *attributes, |
| 3160 | const uint8_t *key_buffer, |
| 3161 | size_t key_buffer_size, |
| 3162 | psa_algorithm_t alg, |
| 3163 | const uint8_t *input, |
| 3164 | size_t input_length, |
| 3165 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3166 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3167 | { |
| 3168 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3171 | size_t hash_length; |
| 3172 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3173 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3174 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3176 | input, input_length, |
| 3177 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3180 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3182 | |
| 3183 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | attributes, key_buffer, key_buffer_size, |
| 3185 | alg, hash, hash_length, |
| 3186 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3187 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3193 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3194 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3196 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3197 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3198 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3199 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3200 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3201 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3202 | |
| 3203 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3204 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3205 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3206 | signature_length); |
| 3207 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3208 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3209 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3210 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3211 | LOCAL_INPUT_FREE(input_external, input); |
| 3212 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3213 | |
| 3214 | return status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3217 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3218 | const psa_key_attributes_t *attributes, |
| 3219 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3220 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3222 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3224 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3225 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3226 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3227 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3228 | return mbedtls_psa_rsa_sign_hash( |
| 3229 | attributes, |
| 3230 | key_buffer, key_buffer_size, |
| 3231 | alg, hash, hash_length, |
| 3232 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3233 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3234 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3235 | } else { |
| 3236 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3237 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3238 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3239 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3240 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3242 | return mbedtls_psa_ecdsa_sign_hash( |
| 3243 | attributes, |
| 3244 | key_buffer, key_buffer_size, |
| 3245 | alg, hash, hash_length, |
| 3246 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3247 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3248 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | } else { |
| 3250 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3251 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3252 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | (void) key_buffer; |
| 3255 | (void) key_buffer_size; |
| 3256 | (void) hash; |
| 3257 | (void) hash_length; |
| 3258 | (void) signature; |
| 3259 | (void) signature_size; |
| 3260 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3262 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3263 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3266 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3267 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3268 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3269 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | size_t signature_size, |
| 3271 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3272 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3273 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3274 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3275 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3276 | |
| 3277 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3278 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3279 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3280 | signature_size, signature_length); |
| 3281 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3282 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3283 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3284 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3285 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3286 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3287 | |
| 3288 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3289 | } |
| 3290 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3291 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3292 | const psa_key_attributes_t *attributes, |
| 3293 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3294 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3296 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3298 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3299 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3300 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3302 | return mbedtls_psa_rsa_verify_hash( |
| 3303 | attributes, |
| 3304 | key_buffer, key_buffer_size, |
| 3305 | alg, hash, hash_length, |
| 3306 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3307 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3308 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | } else { |
| 3310 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3311 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3313 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3314 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3315 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3316 | return mbedtls_psa_ecdsa_verify_hash( |
| 3317 | attributes, |
| 3318 | key_buffer, key_buffer_size, |
| 3319 | alg, hash, hash_length, |
| 3320 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3321 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3322 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | } else { |
| 3324 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3325 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3326 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | (void) key_buffer; |
| 3329 | (void) key_buffer_size; |
| 3330 | (void) hash; |
| 3331 | (void) hash_length; |
| 3332 | (void) signature; |
| 3333 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3336 | } |
| 3337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3339 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3340 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3342 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3343 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3344 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3345 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3346 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3347 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3348 | |
| 3349 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3350 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3351 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3352 | signature_length); |
| 3353 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3354 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3355 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3356 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3357 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3358 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3359 | |
| 3360 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3364 | psa_algorithm_t alg, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3365 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | size_t input_length, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3367 | const uint8_t *salt_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | size_t salt_length, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3369 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | size_t output_size, |
| 3371 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3372 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3373 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3374 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3375 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3376 | psa_key_attributes_t attributes; |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3377 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3378 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3379 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3380 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3381 | (void) input; |
| 3382 | (void) input_length; |
| 3383 | (void) salt; |
| 3384 | (void) output; |
| 3385 | (void) output_size; |
| 3386 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3387 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3390 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3391 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3392 | |
Valerio Setti | 5bb454a | 2024-01-15 10:43:16 +0100 | [diff] [blame] | 3393 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3394 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3395 | if (status != PSA_SUCCESS) { |
| 3396 | return status; |
| 3397 | } |
| 3398 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3399 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3400 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3401 | goto exit; |
| 3402 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3403 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3404 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3406 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3407 | |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3408 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3409 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3410 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3411 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3412 | &attributes, slot->key.data, slot->key.bytes, |
| 3413 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3414 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3415 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3416 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3417 | |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3418 | LOCAL_INPUT_FREE(input_external, input); |
| 3419 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3420 | LOCAL_OUTPUT_FREE(output_external, output); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3423 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3425 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3426 | psa_algorithm_t alg, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3427 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | size_t input_length, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3429 | const uint8_t *salt_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3430 | size_t salt_length, |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3431 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | size_t output_size, |
| 3433 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3434 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3435 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3436 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3437 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3438 | psa_key_attributes_t attributes; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3439 | |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3440 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3441 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3442 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3443 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3444 | (void) input; |
| 3445 | (void) input_length; |
| 3446 | (void) salt; |
| 3447 | (void) output; |
| 3448 | (void) output_size; |
| 3449 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3450 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3453 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3454 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3455 | |
Valerio Setti | 5bb454a | 2024-01-15 10:43:16 +0100 | [diff] [blame] | 3456 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3458 | if (status != PSA_SUCCESS) { |
| 3459 | return status; |
| 3460 | } |
| 3461 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3462 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3463 | goto exit; |
| 3464 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3465 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3466 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3467 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3468 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3469 | |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3470 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3471 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3472 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3473 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3474 | &attributes, slot->key.data, slot->key.bytes, |
| 3475 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3476 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3477 | |
| 3478 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3479 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3480 | |
Thomas Daubney | 6adbb2a | 2024-01-18 18:10:32 +0000 | [diff] [blame] | 3481 | LOCAL_INPUT_FREE(input_external, input); |
| 3482 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3483 | LOCAL_OUTPUT_FREE(output_external, output); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3486 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3487 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3488 | /****************************************************************/ |
| 3489 | /* Asymmetric interruptible cryptography */ |
| 3490 | /****************************************************************/ |
| 3491 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3492 | static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3493 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3494 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3495 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3496 | psa_interruptible_max_ops = max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | uint32_t psa_interruptible_get_max_ops(void) |
| 3500 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3501 | return psa_interruptible_max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3504 | uint32_t psa_sign_hash_get_num_ops( |
| 3505 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3506 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3507 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | uint32_t psa_verify_hash_get_num_ops( |
| 3511 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3512 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3513 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3516 | static psa_status_t psa_sign_hash_abort_internal( |
| 3517 | psa_sign_hash_interruptible_operation_t *operation) |
| 3518 | { |
| 3519 | if (operation->id == 0) { |
| 3520 | /* The object has (apparently) been initialized but it is not (yet) |
| 3521 | * in use. It's ok to call abort on such an object, and there's |
| 3522 | * nothing to do. */ |
| 3523 | return PSA_SUCCESS; |
| 3524 | } |
| 3525 | |
| 3526 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3527 | |
| 3528 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3529 | |
| 3530 | operation->id = 0; |
| 3531 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3532 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3533 | * only want to be cleared by the application calling abort, not by abort |
| 3534 | * being called at completion of an operation. */ |
| 3535 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3536 | return status; |
| 3537 | } |
| 3538 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3539 | psa_status_t psa_sign_hash_start( |
| 3540 | psa_sign_hash_interruptible_operation_t *operation, |
| 3541 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3542 | const uint8_t *hash_external, size_t hash_length) |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3543 | { |
| 3544 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3545 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3546 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3547 | psa_key_attributes_t attributes; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3548 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3549 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3550 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3551 | /* Check that start has not been previously called, or operation has not |
| 3552 | * previously errored. */ |
| 3553 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3554 | return PSA_ERROR_BAD_STATE; |
| 3555 | } |
| 3556 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3557 | status = psa_sign_verify_check_alg(0, alg); |
| 3558 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3559 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3560 | return status; |
| 3561 | } |
| 3562 | |
| 3563 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3564 | PSA_KEY_USAGE_SIGN_HASH, |
| 3565 | alg); |
| 3566 | |
| 3567 | if (status != PSA_SUCCESS) { |
| 3568 | goto exit; |
| 3569 | } |
| 3570 | |
| 3571 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3572 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3573 | goto exit; |
| 3574 | } |
| 3575 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3576 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3577 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3578 | attributes = (psa_key_attributes_t) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3579 | .core = slot->attr |
| 3580 | }; |
| 3581 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3582 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3583 | operation->num_ops = 0; |
| 3584 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3585 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3586 | slot->key.data, |
| 3587 | slot->key.bytes, alg, |
| 3588 | hash, hash_length); |
| 3589 | exit: |
| 3590 | |
| 3591 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3592 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3593 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3596 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3597 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3598 | if (unlock_status != PSA_SUCCESS) { |
| 3599 | operation->error_occurred = 1; |
| 3600 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3601 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3602 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3603 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3604 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | |
| 3608 | psa_status_t psa_sign_hash_complete( |
| 3609 | psa_sign_hash_interruptible_operation_t *operation, |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3610 | uint8_t *signature_external, size_t signature_size, |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3611 | size_t *signature_length) |
| 3612 | { |
| 3613 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3614 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3615 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3616 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3617 | *signature_length = 0; |
| 3618 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3619 | /* Check that start has been called first, and that operation has not |
| 3620 | * previously errored. */ |
| 3621 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3622 | status = PSA_ERROR_BAD_STATE; |
| 3623 | goto exit; |
| 3624 | } |
| 3625 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3626 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3627 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3628 | if (signature_size == 0) { |
| 3629 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3630 | goto exit; |
| 3631 | } |
| 3632 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3633 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3634 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3635 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3636 | signature_size, |
| 3637 | signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3638 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3639 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3640 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3641 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3642 | exit: |
| 3643 | |
David Horstmann | c5064c8 | 2024-03-11 17:02:03 +0000 | [diff] [blame] | 3644 | if (signature != NULL) { |
| 3645 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3646 | *signature_length); |
| 3647 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3648 | |
Paul Elliott | 53bb312 | 2023-02-10 14:22:22 +0000 | [diff] [blame] | 3649 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3650 | if (status != PSA_SUCCESS) { |
| 3651 | operation->error_occurred = 1; |
| 3652 | } |
| 3653 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3654 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
David Horstmann | 4a523a6 | 2024-03-11 13:32:16 +0000 | [diff] [blame] | 3657 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3658 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3659 | return status; |
| 3660 | } |
| 3661 | |
| 3662 | psa_status_t psa_sign_hash_abort( |
| 3663 | psa_sign_hash_interruptible_operation_t *operation) |
| 3664 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3665 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3666 | |
| 3667 | status = psa_sign_hash_abort_internal(operation); |
| 3668 | |
| 3669 | /* We clear the number of ops done here, so that it is not cleared when |
| 3670 | * the operation fails or succeeds, only on manual abort. */ |
| 3671 | operation->num_ops = 0; |
| 3672 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3673 | /* Likewise, failure state. */ |
| 3674 | operation->error_occurred = 0; |
| 3675 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3676 | return status; |
| 3677 | } |
| 3678 | |
| 3679 | static psa_status_t psa_verify_hash_abort_internal( |
| 3680 | psa_verify_hash_interruptible_operation_t *operation) |
| 3681 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3682 | if (operation->id == 0) { |
| 3683 | /* The object has (apparently) been initialized but it is not (yet) |
| 3684 | * in use. It's ok to call abort on such an object, and there's |
| 3685 | * nothing to do. */ |
| 3686 | return PSA_SUCCESS; |
| 3687 | } |
| 3688 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3689 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3690 | |
| 3691 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3692 | |
| 3693 | operation->id = 0; |
| 3694 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3695 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3696 | * only want to be cleared by the application calling abort, not by abort |
| 3697 | * being called at completion of an operation. */ |
| 3698 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3699 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | psa_status_t psa_verify_hash_start( |
| 3703 | psa_verify_hash_interruptible_operation_t *operation, |
| 3704 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
David Horstmann | 0fea6a5 | 2024-03-11 13:41:05 +0000 | [diff] [blame] | 3705 | const uint8_t *hash_external, size_t hash_length, |
| 3706 | const uint8_t *signature_external, size_t signature_length) |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3707 | { |
| 3708 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3709 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3710 | psa_key_slot_t *slot; |
| 3711 | |
David Horstmann | 0fea6a5 | 2024-03-11 13:41:05 +0000 | [diff] [blame] | 3712 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3713 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3714 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3715 | /* Check that start has not been previously called, or operation has not |
| 3716 | * previously errored. */ |
| 3717 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3718 | return PSA_ERROR_BAD_STATE; |
| 3719 | } |
| 3720 | |
| 3721 | status = psa_sign_verify_check_alg(0, alg); |
| 3722 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3723 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3724 | return status; |
| 3725 | } |
| 3726 | |
| 3727 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3728 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3729 | alg); |
| 3730 | |
| 3731 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3732 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3733 | return status; |
| 3734 | } |
| 3735 | |
David Horstmann | 0fea6a5 | 2024-03-11 13:41:05 +0000 | [diff] [blame] | 3736 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3737 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3738 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3739 | psa_key_attributes_t attributes = { |
| 3740 | .core = slot->attr |
| 3741 | }; |
| 3742 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3743 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3744 | operation->num_ops = 0; |
| 3745 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3746 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3747 | slot->key.data, |
| 3748 | slot->key.bytes, |
| 3749 | alg, hash, hash_length, |
| 3750 | signature, signature_length); |
David Horstmann | 0fea6a5 | 2024-03-11 13:41:05 +0000 | [diff] [blame] | 3751 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
| 3752 | exit: |
| 3753 | #endif |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3754 | |
| 3755 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3756 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3757 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3760 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3761 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3762 | if (unlock_status != PSA_SUCCESS) { |
| 3763 | operation->error_occurred = 1; |
| 3764 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3765 | |
David Horstmann | 0fea6a5 | 2024-03-11 13:41:05 +0000 | [diff] [blame] | 3766 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3767 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3768 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3769 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | psa_status_t psa_verify_hash_complete( |
| 3773 | psa_verify_hash_interruptible_operation_t *operation) |
| 3774 | { |
| 3775 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3776 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3777 | /* Check that start has been called first, and that operation has not |
| 3778 | * previously errored. */ |
| 3779 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3780 | status = PSA_ERROR_BAD_STATE; |
| 3781 | goto exit; |
| 3782 | } |
| 3783 | |
| 3784 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3785 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3786 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3787 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3788 | operation); |
| 3789 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3790 | exit: |
| 3791 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3792 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3793 | if (status != PSA_SUCCESS) { |
| 3794 | operation->error_occurred = 1; |
| 3795 | } |
| 3796 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3797 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | return status; |
| 3801 | } |
| 3802 | |
| 3803 | psa_status_t psa_verify_hash_abort( |
| 3804 | psa_verify_hash_interruptible_operation_t *operation) |
| 3805 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3806 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3807 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3808 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3809 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3810 | /* We clear the number of ops done here, so that it is not cleared when |
| 3811 | * the operation fails or succeeds, only on manual abort. */ |
| 3812 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3813 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3814 | /* Likewise, failure state. */ |
| 3815 | operation->error_occurred = 0; |
| 3816 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3817 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3820 | /****************************************************************/ |
| 3821 | /* Asymmetric interruptible cryptography internal */ |
| 3822 | /* implementations */ |
| 3823 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3824 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3825 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3826 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3827 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3828 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3829 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3830 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3831 | |
| 3832 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3833 | * therefore avoid this value, and set to minimum possible. */ |
| 3834 | if (max_ops == 0) { |
| 3835 | max_ops = 1; |
| 3836 | } |
| 3837 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3838 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3839 | #else |
| 3840 | (void) max_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3841 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3842 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3843 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3844 | } |
| 3845 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3846 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3847 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3848 | { |
| 3849 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3850 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3851 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3852 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3853 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3854 | #else |
| 3855 | (void) operation; |
| 3856 | return 0; |
| 3857 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3858 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3859 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3860 | } |
| 3861 | |
| 3862 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3863 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3864 | { |
| 3865 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3866 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3867 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3868 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3869 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3870 | #else |
| 3871 | (void) operation; |
| 3872 | return 0; |
| 3873 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3874 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3875 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3876 | } |
| 3877 | |
| 3878 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3879 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3880 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3881 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3882 | const uint8_t *hash, size_t hash_length) |
| 3883 | { |
| 3884 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3885 | size_t required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3886 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3887 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3888 | return PSA_ERROR_NOT_SUPPORTED; |
| 3889 | } |
| 3890 | |
| 3891 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3892 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3893 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3894 | |
| 3895 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3896 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3897 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3898 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3899 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3900 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3901 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3902 | operation->num_ops = 0; |
| 3903 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3904 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3905 | attributes->core.bits, |
| 3906 | key_buffer, |
| 3907 | key_buffer_size, |
| 3908 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3909 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3910 | if (status != PSA_SUCCESS) { |
| 3911 | return status; |
| 3912 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3913 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3914 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3915 | operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3916 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3917 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 3918 | operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3919 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3920 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3921 | /* We only need to store the same length of hash as the private key size |
| 3922 | * here, it would be truncated by the internal implementation anyway. */ |
| 3923 | required_hash_length = (hash_length < operation->coordinate_bytes ? |
| 3924 | hash_length : operation->coordinate_bytes); |
| 3925 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3926 | if (required_hash_length > sizeof(operation->hash)) { |
| 3927 | /* Shouldn't happen, but better safe than sorry. */ |
| 3928 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3929 | } |
| 3930 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3931 | memcpy(operation->hash, hash, required_hash_length); |
| 3932 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3933 | |
| 3934 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3935 | |
| 3936 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3937 | (void) operation; |
| 3938 | (void) key_buffer; |
| 3939 | (void) key_buffer_size; |
| 3940 | (void) alg; |
| 3941 | (void) hash; |
| 3942 | (void) hash_length; |
| 3943 | (void) status; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3944 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3945 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3946 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3947 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3948 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3949 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3953 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3954 | uint8_t *signature, size_t signature_size, |
| 3955 | size_t *signature_length) |
| 3956 | { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3957 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3958 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3959 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3960 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3961 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3962 | mbedtls_mpi r; |
| 3963 | mbedtls_mpi s; |
| 3964 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3965 | mbedtls_mpi_init(&r); |
| 3966 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3967 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3968 | /* Ensure max_ops is set to the current value (or default). */ |
| 3969 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 3970 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3971 | if (signature_size < 2 * operation->coordinate_bytes) { |
| 3972 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3973 | goto exit; |
| 3974 | } |
| 3975 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3976 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3977 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3978 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3979 | status = mbedtls_to_psa_error( |
| 3980 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3981 | &r, |
| 3982 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3983 | &operation->ctx->d, |
| 3984 | operation->hash, |
| 3985 | operation->hash_length, |
| 3986 | operation->md_alg, |
| 3987 | mbedtls_psa_get_random, |
| 3988 | MBEDTLS_PSA_RANDOM_STATE, |
| 3989 | &operation->restart_ctx)); |
| 3990 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3991 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3992 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3993 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3994 | } else { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3995 | status = mbedtls_to_psa_error( |
| 3996 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3997 | &r, |
| 3998 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3999 | &operation->ctx->d, |
| 4000 | operation->hash, |
| 4001 | operation->hash_length, |
| 4002 | mbedtls_psa_get_random, |
| 4003 | MBEDTLS_PSA_RANDOM_STATE, |
| 4004 | mbedtls_psa_get_random, |
| 4005 | MBEDTLS_PSA_RANDOM_STATE, |
| 4006 | &operation->restart_ctx)); |
| 4007 | } |
| 4008 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4009 | /* Hide the fact that the restart context only holds a delta of number of |
| 4010 | * ops done during the last operation, not an absolute value. */ |
| 4011 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 4012 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4013 | if (status == PSA_SUCCESS) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4014 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 4015 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4016 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4017 | operation->coordinate_bytes) |
| 4018 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4019 | |
| 4020 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4021 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 4025 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4026 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4027 | operation->coordinate_bytes, |
| 4028 | operation->coordinate_bytes) |
| 4029 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4032 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4035 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4036 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4037 | status = PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4038 | } |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4039 | |
| 4040 | exit: |
| 4041 | |
| 4042 | mbedtls_mpi_free(&r); |
| 4043 | mbedtls_mpi_free(&s); |
| 4044 | return status; |
| 4045 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4046 | #else |
| 4047 | |
| 4048 | (void) operation; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4049 | (void) signature; |
| 4050 | (void) signature_size; |
| 4051 | (void) signature_length; |
| 4052 | |
| 4053 | return PSA_ERROR_NOT_SUPPORTED; |
| 4054 | |
| 4055 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4056 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4057 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4058 | } |
| 4059 | |
| 4060 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 4061 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 4062 | { |
| 4063 | |
| 4064 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4065 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4066 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4067 | |
| 4068 | if (operation->ctx) { |
| 4069 | mbedtls_ecdsa_free(operation->ctx); |
| 4070 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 4071 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 4075 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4076 | operation->num_ops = 0; |
| 4077 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4078 | return PSA_SUCCESS; |
| 4079 | |
| 4080 | #else |
| 4081 | |
| 4082 | (void) operation; |
| 4083 | |
| 4084 | return PSA_ERROR_NOT_SUPPORTED; |
| 4085 | |
| 4086 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4087 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4088 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4089 | } |
| 4090 | |
| 4091 | psa_status_t mbedtls_psa_verify_hash_start( |
| 4092 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 4093 | const psa_key_attributes_t *attributes, |
| 4094 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 4095 | psa_algorithm_t alg, |
| 4096 | const uint8_t *hash, size_t hash_length, |
| 4097 | const uint8_t *signature, size_t signature_length) |
| 4098 | { |
| 4099 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4100 | size_t coordinate_bytes = 0; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4101 | size_t required_hash_length = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4102 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4103 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 4104 | return PSA_ERROR_NOT_SUPPORTED; |
| 4105 | } |
| 4106 | |
| 4107 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 4108 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4109 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4110 | |
| 4111 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4112 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4113 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4114 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 4115 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 4116 | mbedtls_mpi_init(&operation->r); |
| 4117 | mbedtls_mpi_init(&operation->s); |
| 4118 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4119 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 4120 | operation->num_ops = 0; |
| 4121 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4122 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 4123 | attributes->core.bits, |
| 4124 | key_buffer, |
| 4125 | key_buffer_size, |
| 4126 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4127 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4128 | if (status != PSA_SUCCESS) { |
| 4129 | return status; |
| 4130 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4131 | |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 4132 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4133 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4134 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4135 | return PSA_ERROR_INVALID_SIGNATURE; |
| 4136 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4137 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4138 | status = mbedtls_to_psa_error( |
| 4139 | mbedtls_mpi_read_binary(&operation->r, |
| 4140 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4141 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4142 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4143 | if (status != PSA_SUCCESS) { |
| 4144 | return status; |
| 4145 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4146 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4147 | status = mbedtls_to_psa_error( |
| 4148 | mbedtls_mpi_read_binary(&operation->s, |
| 4149 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4150 | coordinate_bytes, |
| 4151 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4152 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4153 | if (status != PSA_SUCCESS) { |
| 4154 | return status; |
| 4155 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4156 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4157 | status = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4158 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4159 | if (status != PSA_SUCCESS) { |
| 4160 | return status; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4161 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4162 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4163 | /* We only need to store the same length of hash as the private key size |
| 4164 | * here, it would be truncated by the internal implementation anyway. */ |
| 4165 | required_hash_length = (hash_length < coordinate_bytes ? hash_length : |
| 4166 | coordinate_bytes); |
| 4167 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 4168 | if (required_hash_length > sizeof(operation->hash)) { |
| 4169 | /* Shouldn't happen, but better safe than sorry. */ |
| 4170 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4171 | } |
| 4172 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4173 | memcpy(operation->hash, hash, required_hash_length); |
| 4174 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4175 | |
| 4176 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4177 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4178 | (void) operation; |
| 4179 | (void) key_buffer; |
| 4180 | (void) key_buffer_size; |
| 4181 | (void) alg; |
| 4182 | (void) hash; |
| 4183 | (void) hash_length; |
| 4184 | (void) signature; |
| 4185 | (void) signature_length; |
| 4186 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 4187 | (void) coordinate_bytes; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4188 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4189 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4190 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4191 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4192 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4193 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 4197 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4198 | { |
| 4199 | |
| 4200 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4201 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4202 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4203 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4204 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4205 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 4206 | /* Ensure max_ops is set to the current value (or default). */ |
| 4207 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 4208 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4209 | status = mbedtls_to_psa_error( |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4210 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 4211 | operation->hash, |
| 4212 | operation->hash_length, |
| 4213 | &operation->ctx->Q, |
| 4214 | &operation->r, |
| 4215 | &operation->s, |
| 4216 | &operation->restart_ctx)); |
| 4217 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4218 | /* Hide the fact that the restart context only holds a delta of number of |
| 4219 | * ops done during the last operation, not an absolute value. */ |
| 4220 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 4221 | |
| 4222 | return status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4223 | #else |
| 4224 | (void) operation; |
| 4225 | |
| 4226 | return PSA_ERROR_NOT_SUPPORTED; |
| 4227 | |
| 4228 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4229 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4230 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4231 | } |
| 4232 | |
| 4233 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 4234 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4235 | { |
| 4236 | |
| 4237 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4238 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4239 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4240 | |
| 4241 | if (operation->ctx) { |
| 4242 | mbedtls_ecdsa_free(operation->ctx); |
| 4243 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 4244 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 4248 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4249 | operation->num_ops = 0; |
| 4250 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4251 | mbedtls_mpi_free(&operation->r); |
| 4252 | mbedtls_mpi_free(&operation->s); |
| 4253 | |
| 4254 | return PSA_SUCCESS; |
| 4255 | |
| 4256 | #else |
| 4257 | (void) operation; |
| 4258 | |
| 4259 | return PSA_ERROR_NOT_SUPPORTED; |
| 4260 | |
| 4261 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4262 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4263 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4264 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4265 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 4266 | static psa_status_t psa_generate_random_internal(uint8_t *output, |
| 4267 | size_t output_size) |
| 4268 | { |
| 4269 | GUARD_MODULE_INITIALIZED; |
| 4270 | |
| 4271 | psa_status_t status; |
| 4272 | |
| 4273 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4274 | |
| 4275 | size_t output_length = 0; |
| 4276 | status = mbedtls_psa_external_get_random(&global_data.rng, |
| 4277 | output, output_size, |
| 4278 | &output_length); |
| 4279 | if (status != PSA_SUCCESS) { |
| 4280 | goto exit; |
| 4281 | } |
| 4282 | /* Breaking up a request into smaller chunks is currently not supported |
| 4283 | * for the external RNG interface. */ |
| 4284 | if (output_length != output_size) { |
| 4285 | status = PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 4286 | goto exit; |
| 4287 | } |
| 4288 | status = PSA_SUCCESS; |
| 4289 | |
| 4290 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4291 | |
| 4292 | while (output_size > 0) { |
| 4293 | size_t request_size = |
| 4294 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 4295 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 4296 | output_size); |
| 4297 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 4298 | output, request_size); |
| 4299 | if (ret != 0) { |
| 4300 | status = mbedtls_to_psa_error(ret); |
| 4301 | goto exit; |
| 4302 | } |
| 4303 | output_size -= request_size; |
| 4304 | output += request_size; |
| 4305 | } |
| 4306 | status = PSA_SUCCESS; |
| 4307 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4308 | |
| 4309 | exit: |
| 4310 | return status; |
| 4311 | } |
| 4312 | |
| 4313 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4314 | /****************************************************************/ |
| 4315 | /* Symmetric cryptography */ |
| 4316 | /****************************************************************/ |
| 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 4319 | mbedtls_svc_key_id_t key, |
| 4320 | psa_algorithm_t alg, |
| 4321 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4322 | { |
| 4323 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4324 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4325 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4326 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 4327 | PSA_KEY_USAGE_ENCRYPT : |
| 4328 | PSA_KEY_USAGE_DECRYPT); |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4329 | psa_key_attributes_t attributes; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4330 | |
| 4331 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4333 | status = PSA_ERROR_BAD_STATE; |
| 4334 | goto exit; |
| 4335 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4337 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4338 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4339 | goto exit; |
| 4340 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 4343 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4344 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4346 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4347 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4348 | * is used to indicate to psa_cipher_abort that there are resources to free, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4349 | * so we only set it (in the driver wrapper) after resources have been |
| 4350 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4351 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4353 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4354 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4355 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | } |
| 4357 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4358 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4359 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 4361 | }; |
| 4362 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4363 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4364 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 4365 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 4366 | &attributes, |
| 4367 | slot->key.data, |
| 4368 | slot->key.bytes, |
| 4369 | alg); |
| 4370 | } else { |
| 4371 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 4372 | &attributes, |
| 4373 | slot->key.data, |
| 4374 | slot->key.bytes, |
| 4375 | alg); |
| 4376 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4377 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4378 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | if (status != PSA_SUCCESS) { |
| 4380 | psa_cipher_abort(operation); |
| 4381 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4382 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4383 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4386 | } |
| 4387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 4389 | mbedtls_svc_key_id_t key, |
| 4390 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4391 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 4396 | mbedtls_svc_key_id_t key, |
| 4397 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4398 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4403 | uint8_t *iv_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4404 | size_t iv_size, |
| 4405 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4406 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4407 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 4408 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4409 | |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4410 | LOCAL_OUTPUT_DECLARE(iv_external, iv); |
| 4411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4412 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4413 | status = PSA_ERROR_BAD_STATE; |
| 4414 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4418 | status = PSA_ERROR_BAD_STATE; |
| 4419 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4420 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4421 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4422 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4424 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4425 | goto exit; |
| 4426 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4428 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4429 | status = PSA_ERROR_GENERIC_ERROR; |
| 4430 | goto exit; |
| 4431 | } |
| 4432 | |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4433 | LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv); |
| 4434 | |
Gabor Mezei | 1b5b58d | 2024-03-04 17:15:08 +0100 | [diff] [blame] | 4435 | status = psa_generate_random_internal(iv, default_iv_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4437 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4440 | status = psa_driver_wrapper_cipher_set_iv(operation, |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4441 | iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4442 | |
| 4443 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4445 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4446 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4448 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | psa_cipher_abort(operation); |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4450 | if (iv != NULL) { |
| 4451 | mbedtls_platform_zeroize(iv, default_iv_length); |
| 4452 | } |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4453 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4454 | |
Gabor Mezei | 358eb21 | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 4455 | LOCAL_OUTPUT_FREE(iv_external, iv); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4457 | } |
| 4458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
Gabor Mezei | 7abf8ee | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 4460 | const uint8_t *iv_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4462 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4463 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4464 | |
Gabor Mezei | 7abf8ee | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 4465 | LOCAL_INPUT_DECLARE(iv_external, iv); |
| 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4468 | status = PSA_ERROR_BAD_STATE; |
| 4469 | goto exit; |
| 4470 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4473 | status = PSA_ERROR_BAD_STATE; |
| 4474 | goto exit; |
| 4475 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4477 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4478 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4479 | goto exit; |
| 4480 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4481 | |
Gabor Mezei | 7abf8ee | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 4482 | LOCAL_INPUT_ALLOC(iv_external, iv_length, iv); |
| 4483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4485 | iv, |
| 4486 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4487 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4488 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4490 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4491 | } else { |
| 4492 | psa_cipher_abort(operation); |
| 4493 | } |
Gabor Mezei | 7abf8ee | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 4494 | |
| 4495 | LOCAL_INPUT_FREE(iv_external, iv); |
| 4496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4498 | } |
| 4499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4501 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | size_t input_length, |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4503 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | size_t output_size, |
| 4505 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4506 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4507 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4508 | |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4509 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4510 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4512 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4513 | status = PSA_ERROR_BAD_STATE; |
| 4514 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4515 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4518 | status = PSA_ERROR_BAD_STATE; |
| 4519 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4520 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4521 | |
Gabor Mezei | 8b8e485 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4522 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gabor Mezei | 0b04116 | 2024-02-29 10:08:16 +0000 | [diff] [blame] | 4523 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gabor Mezei | 8b8e485 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | status = psa_driver_wrapper_cipher_update(operation, |
| 4526 | input, |
| 4527 | input_length, |
| 4528 | output, |
| 4529 | output_size, |
| 4530 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4531 | |
| 4532 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | if (status != PSA_SUCCESS) { |
| 4534 | psa_cipher_abort(operation); |
| 4535 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4536 | |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4537 | LOCAL_INPUT_FREE(input_external, input); |
| 4538 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4541 | } |
| 4542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4544 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | size_t output_size, |
| 4546 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4547 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4548 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4549 | |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4550 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4553 | status = PSA_ERROR_BAD_STATE; |
| 4554 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4555 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4558 | status = PSA_ERROR_BAD_STATE; |
| 4559 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4560 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4561 | |
Gabor Mezei | 0b04116 | 2024-02-29 10:08:16 +0000 | [diff] [blame] | 4562 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gabor Mezei | 8b8e485 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4565 | output, |
| 4566 | output_size, |
| 4567 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4568 | |
| 4569 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | if (status == PSA_SUCCESS) { |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4571 | status = psa_cipher_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4572 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4573 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | (void) psa_cipher_abort(operation); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4575 | } |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4576 | |
| 4577 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4578 | |
| 4579 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4583 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4585 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4586 | * in use. It's ok to call abort on such an object, and there's |
| 4587 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4589 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4592 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4593 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4594 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4595 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4601 | psa_algorithm_t alg, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4602 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | size_t input_length, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4604 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | size_t output_size, |
| 4606 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4607 | { |
| 4608 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4609 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4610 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4611 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4612 | size_t default_iv_length = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4613 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4614 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4615 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4616 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4618 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4619 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4620 | goto exit; |
| 4621 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4623 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4624 | PSA_KEY_USAGE_ENCRYPT, |
| 4625 | alg); |
| 4626 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4627 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4628 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4629 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4630 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4632 | }; |
| 4633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4635 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4636 | status = PSA_ERROR_GENERIC_ERROR; |
| 4637 | goto exit; |
| 4638 | } |
| 4639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | if (default_iv_length > 0) { |
| 4641 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4642 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4643 | goto exit; |
| 4644 | } |
| 4645 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 4646 | status = psa_generate_random_internal(local_iv, default_iv_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4648 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4649 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4650 | } |
| 4651 | |
Gabor Mezei | 8b8e485 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4652 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 4653 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 4654 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4655 | status = psa_driver_wrapper_cipher_encrypt( |
| 4656 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4657 | alg, local_iv, default_iv_length, input, input_length, |
Ronald Cron | afbc7ed | 2023-01-24 16:02:04 +0100 | [diff] [blame] | 4658 | psa_crypto_buffer_offset(output, default_iv_length), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4660 | |
| 4661 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4662 | unlock_status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4664 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4665 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | if (status == PSA_SUCCESS) { |
| 4668 | if (default_iv_length > 0) { |
| 4669 | memcpy(output, local_iv, default_iv_length); |
| 4670 | } |
| 4671 | *output_length += default_iv_length; |
| 4672 | } else { |
| 4673 | *output_length = 0; |
| 4674 | } |
| 4675 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4676 | LOCAL_INPUT_FREE(input_external, input); |
| 4677 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | c6977b4 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 4678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4679 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4683 | psa_algorithm_t alg, |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4684 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | size_t input_length, |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4686 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4687 | size_t output_size, |
| 4688 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4689 | { |
| 4690 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4691 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4692 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4693 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4694 | |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4695 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4696 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4699 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4700 | goto exit; |
| 4701 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4704 | PSA_KEY_USAGE_DECRYPT, |
| 4705 | alg); |
| 4706 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4707 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4708 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4709 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4710 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4711 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4712 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4715 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4716 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4717 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4719 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4720 | goto exit; |
| 4721 | } |
| 4722 | |
Gabor Mezei | 8b8e485 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4723 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 4724 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 4725 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4726 | status = psa_driver_wrapper_cipher_decrypt( |
| 4727 | &attributes, slot->key.data, slot->key.bytes, |
| 4728 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4730 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4731 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4732 | unlock_status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4734 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4735 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4738 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4740 | |
Gabor Mezei | 212eb08 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4741 | LOCAL_INPUT_FREE(input_external, input); |
| 4742 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4748 | /****************************************************************/ |
| 4749 | /* AEAD */ |
| 4750 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4751 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4752 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg) |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4754 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4760 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4761 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4765 | #if defined(PSA_WANT_ALG_GCM) |
| 4766 | case PSA_ALG_GCM: |
| 4767 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | * arbitrarily large nonces. Please note that we do not generally |
| 4769 | * recommend the usage of nonces of greater length than |
| 4770 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4771 | * size, which can then lead to collisions if you encrypt a very |
| 4772 | * large number of messages.*/ |
| 4773 | if (nonce_length != 0) { |
| 4774 | return PSA_SUCCESS; |
| 4775 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4776 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4777 | #endif /* PSA_WANT_ALG_GCM */ |
| 4778 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4779 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4780 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4781 | return PSA_SUCCESS; |
| 4782 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4783 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4784 | #endif /* PSA_WANT_ALG_CCM */ |
| 4785 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4786 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | if (nonce_length == 12) { |
| 4788 | return PSA_SUCCESS; |
| 4789 | } else if (nonce_length == 8) { |
| 4790 | return PSA_ERROR_NOT_SUPPORTED; |
| 4791 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4792 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4793 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4794 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4795 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4797 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4800 | } |
| 4801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg) |
Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 4803 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4805 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4806 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4812 | psa_algorithm_t alg, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4813 | const uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | size_t nonce_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4815 | const uint8_t *additional_data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | size_t additional_data_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4817 | const uint8_t *plaintext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | size_t plaintext_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4819 | uint8_t *ciphertext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4820 | size_t ciphertext_size, |
| 4821 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4822 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4823 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4824 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4825 | |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4826 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4827 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4828 | LOCAL_INPUT_DECLARE(plaintext_external, plaintext); |
| 4829 | LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext); |
| 4830 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4831 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | status = psa_aead_check_algorithm(alg); |
| 4834 | if (status != PSA_SUCCESS) { |
| 4835 | return status; |
| 4836 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4837 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4838 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4840 | if (status != PSA_SUCCESS) { |
| 4841 | return status; |
| 4842 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4843 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4844 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4846 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4847 | |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4848 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4849 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data); |
| 4850 | LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext); |
| 4851 | LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext); |
| 4852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4853 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4854 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4855 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4856 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4857 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4858 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4859 | &attributes, slot->key.data, slot->key.bytes, |
| 4860 | alg, |
| 4861 | nonce, nonce_length, |
| 4862 | additional_data, additional_data_length, |
| 4863 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4867 | memset(ciphertext, 0, ciphertext_size); |
| 4868 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4869 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4870 | exit: |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4871 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4872 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4873 | LOCAL_INPUT_FREE(plaintext_external, plaintext); |
| 4874 | LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext); |
| 4875 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4876 | psa_unregister_read(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4879 | } |
| 4880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4882 | psa_algorithm_t alg, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4883 | const uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | size_t nonce_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4885 | const uint8_t *additional_data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | size_t additional_data_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4887 | const uint8_t *ciphertext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | size_t ciphertext_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4889 | uint8_t *plaintext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4890 | size_t plaintext_size, |
| 4891 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4892 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4893 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4894 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4895 | |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4896 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4897 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4898 | LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext); |
| 4899 | LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext); |
| 4900 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4901 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | status = psa_aead_check_algorithm(alg); |
| 4904 | if (status != PSA_SUCCESS) { |
| 4905 | return status; |
| 4906 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4907 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4908 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4910 | if (status != PSA_SUCCESS) { |
| 4911 | return status; |
| 4912 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4913 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4914 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4916 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4917 | |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4918 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4919 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, |
| 4920 | additional_data); |
| 4921 | LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext); |
| 4922 | LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext); |
| 4923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4925 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4926 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4928 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4929 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4930 | &attributes, slot->key.data, slot->key.bytes, |
| 4931 | alg, |
| 4932 | nonce, nonce_length, |
| 4933 | additional_data, additional_data_length, |
| 4934 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4938 | memset(plaintext, 0, plaintext_size); |
| 4939 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4940 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4941 | exit: |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4942 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4943 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4944 | LOCAL_INPUT_FREE(ciphertext_external, ciphertext); |
| 4945 | LOCAL_OUTPUT_FREE(plaintext_external, plaintext); |
| 4946 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4947 | psa_unregister_read(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4950 | } |
| 4951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4953 | { |
| 4954 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4957 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4958 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4959 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4961 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4962 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4963 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4964 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4965 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4966 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4968 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4969 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4970 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4971 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4972 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4973 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4974 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4975 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4977 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4978 | if (tag_len != 16) { |
| 4979 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4980 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4981 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4982 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4983 | |
| 4984 | default: |
| 4985 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4987 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4989 | } |
| 4990 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4991 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4992 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4993 | int is_encrypt, |
| 4994 | mbedtls_svc_key_id_t key, |
| 4995 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4996 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4997 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4998 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4999 | psa_key_slot_t *slot = NULL; |
| 5000 | psa_key_usage_t key_usage = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 5001 | psa_key_attributes_t attributes; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5003 | status = psa_aead_check_algorithm(alg); |
| 5004 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5005 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5006 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 5007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5009 | status = PSA_ERROR_BAD_STATE; |
| 5010 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 5011 | } |
| 5012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5013 | if (operation->nonce_set || operation->lengths_set || |
| 5014 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5015 | status = PSA_ERROR_BAD_STATE; |
| 5016 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5020 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5021 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5022 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 5026 | alg); |
| 5027 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5028 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5030 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 5031 | attributes = (psa_key_attributes_t) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5032 | .core = slot->attr |
| 5033 | }; |
| 5034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 5036 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 5038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | if (is_encrypt) { |
| 5040 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 5041 | &attributes, |
| 5042 | slot->key.data, |
| 5043 | slot->key.bytes, |
| 5044 | alg); |
| 5045 | } else { |
| 5046 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 5047 | &attributes, |
| 5048 | slot->key.data, |
| 5049 | slot->key.bytes, |
| 5050 | alg); |
| 5051 | } |
| 5052 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5053 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5057 | |
| 5058 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 5059 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5062 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 5064 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | } else { |
| 5066 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5067 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 5068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 5070 | } |
| 5071 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5072 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 5074 | mbedtls_svc_key_id_t key, |
| 5075 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5076 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 5082 | mbedtls_svc_key_id_t key, |
| 5083 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5084 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5086 | } |
| 5087 | |
David Horstmann | fed2377 | 2023-12-19 17:49:20 +0000 | [diff] [blame] | 5088 | static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation, |
| 5089 | const uint8_t *nonce, |
| 5090 | size_t nonce_length) |
| 5091 | { |
| 5092 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5093 | |
| 5094 | if (operation->id == 0) { |
| 5095 | status = PSA_ERROR_BAD_STATE; |
| 5096 | goto exit; |
| 5097 | } |
| 5098 | |
| 5099 | if (operation->nonce_set) { |
| 5100 | status = PSA_ERROR_BAD_STATE; |
| 5101 | goto exit; |
| 5102 | } |
| 5103 | |
| 5104 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 5105 | if (status != PSA_SUCCESS) { |
| 5106 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5107 | goto exit; |
| 5108 | } |
| 5109 | |
| 5110 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 5111 | nonce_length); |
| 5112 | |
| 5113 | exit: |
| 5114 | if (status == PSA_SUCCESS) { |
| 5115 | operation->nonce_set = 1; |
| 5116 | } else { |
| 5117 | psa_aead_abort(operation); |
| 5118 | } |
| 5119 | |
| 5120 | return status; |
| 5121 | } |
| 5122 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5123 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame] | 5125 | uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5126 | size_t nonce_size, |
| 5127 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5128 | { |
| 5129 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 5130 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 5131 | size_t required_nonce_size = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5132 | |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame] | 5133 | LOCAL_OUTPUT_DECLARE(nonce_external, nonce); |
| 5134 | LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce); |
| 5135 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5136 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5139 | status = PSA_ERROR_BAD_STATE; |
| 5140 | goto exit; |
| 5141 | } |
| 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5144 | status = PSA_ERROR_BAD_STATE; |
| 5145 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5146 | } |
| 5147 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5148 | /* For CCM, this size may not be correct according to the PSA |
| 5149 | * specification. The PSA Crypto 1.0.1 specification states: |
| 5150 | * |
| 5151 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 5152 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 5153 | * |
| 5154 | * However this restriction that L has to be the smallest integer is not |
| 5155 | * applied in practice, and it is not implementable here since the |
| 5156 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 5158 | operation->alg); |
| 5159 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5160 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5161 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5162 | } |
| 5163 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5164 | status = psa_generate_random_internal(local_nonce, required_nonce_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5166 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5168 | |
David Horstmann | fed2377 | 2023-12-19 17:49:20 +0000 | [diff] [blame] | 5169 | status = psa_aead_set_nonce_internal(operation, local_nonce, |
| 5170 | required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5171 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5172 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (status == PSA_SUCCESS) { |
| 5174 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5175 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | } else { |
| 5177 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 5178 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5179 | |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame] | 5180 | LOCAL_OUTPUT_FREE(nonce_external, nonce); |
| 5181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5182 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 5186 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
David Horstmann | 8f0ef51 | 2023-12-11 15:17:11 +0000 | [diff] [blame] | 5188 | const uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5190 | { |
David Horstmann | fed2377 | 2023-12-19 17:49:20 +0000 | [diff] [blame] | 5191 | psa_status_t status; |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5192 | |
David Horstmann | 8f0ef51 | 2023-12-11 15:17:11 +0000 | [diff] [blame] | 5193 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 5194 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5195 | |
David Horstmann | fed2377 | 2023-12-19 17:49:20 +0000 | [diff] [blame] | 5196 | status = psa_aead_set_nonce_internal(operation, nonce, nonce_length); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5197 | |
David Horstmann | 18dc032 | 2023-12-20 16:16:43 +0000 | [diff] [blame] | 5198 | /* Exit label is only needed for buffer copying, prevent unused warnings. */ |
| 5199 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5200 | exit: |
David Horstmann | 18dc032 | 2023-12-20 16:16:43 +0000 | [diff] [blame] | 5201 | #endif |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5202 | |
David Horstmann | 8f0ef51 | 2023-12-11 15:17:11 +0000 | [diff] [blame] | 5203 | LOCAL_INPUT_FREE(nonce_external, nonce); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5209 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 5210 | size_t ad_length, |
| 5211 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5212 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5213 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5216 | status = PSA_ERROR_BAD_STATE; |
| 5217 | goto exit; |
| 5218 | } |
| 5219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5220 | if (operation->lengths_set || operation->ad_started || |
| 5221 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5222 | status = PSA_ERROR_BAD_STATE; |
| 5223 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5224 | } |
| 5225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5226 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5227 | #if defined(PSA_WANT_ALG_GCM) |
| 5228 | case PSA_ALG_GCM: |
| 5229 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | * bits. Without the guard this code will generate warnings on 32bit |
| 5231 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5232 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 5234 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5235 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5236 | goto exit; |
| 5237 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5238 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5239 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5240 | #endif /* PSA_WANT_ALG_GCM */ |
| 5241 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5242 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5244 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5245 | goto exit; |
| 5246 | } |
| 5247 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5248 | #endif /* PSA_WANT_ALG_CCM */ |
| 5249 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5250 | case PSA_ALG_CHACHA20_POLY1305: |
| 5251 | /* No length restrictions for ChaChaPoly. */ |
| 5252 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5253 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5254 | default: |
| 5255 | break; |
| 5256 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 5259 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5260 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5261 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5263 | operation->ad_remaining = ad_length; |
| 5264 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5265 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | } else { |
| 5267 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5268 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5270 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5271 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
David Horstmann | 25dac6e | 2023-12-11 15:23:13 +0000 | [diff] [blame] | 5275 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5277 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5278 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5279 | |
David Horstmann | 25dac6e | 2023-12-11 15:23:13 +0000 | [diff] [blame] | 5280 | LOCAL_INPUT_DECLARE(input_external, input); |
| 5281 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 5282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5283 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5284 | status = PSA_ERROR_BAD_STATE; |
| 5285 | goto exit; |
| 5286 | } |
| 5287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5289 | status = PSA_ERROR_BAD_STATE; |
| 5290 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5291 | } |
| 5292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | if (operation->lengths_set) { |
| 5294 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5295 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5296 | goto exit; |
| 5297 | } |
| 5298 | |
| 5299 | operation->ad_remaining -= input_length; |
| 5300 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5301 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5303 | status = PSA_ERROR_BAD_STATE; |
| 5304 | goto exit; |
| 5305 | } |
| 5306 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 5309 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5310 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5311 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5312 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5313 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | } else { |
| 5315 | psa_aead_abort(operation); |
| 5316 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5317 | |
David Horstmann | 25dac6e | 2023-12-11 15:23:13 +0000 | [diff] [blame] | 5318 | LOCAL_INPUT_FREE(input_external, input); |
| 5319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 5324 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5325 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
David Horstmann | 2914fac | 2023-12-11 15:28:37 +0000 | [diff] [blame] | 5326 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | size_t input_length, |
David Horstmann | 2914fac | 2023-12-11 15:28:37 +0000 | [diff] [blame] | 5328 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | size_t output_size, |
| 5330 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5331 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5332 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5333 | |
David Horstmann | 2914fac | 2023-12-11 15:28:37 +0000 | [diff] [blame] | 5334 | |
| 5335 | LOCAL_INPUT_DECLARE(input_external, input); |
| 5336 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 5337 | |
| 5338 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 5339 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 5340 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5341 | *output_length = 0; |
| 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5344 | status = PSA_ERROR_BAD_STATE; |
| 5345 | goto exit; |
| 5346 | } |
| 5347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5349 | status = PSA_ERROR_BAD_STATE; |
| 5350 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5351 | } |
| 5352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5354 | /* Additional data length was supplied, but not all the additional |
| 5355 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5357 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5358 | goto exit; |
| 5359 | } |
| 5360 | |
| 5361 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5363 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5364 | goto exit; |
| 5365 | } |
| 5366 | |
| 5367 | operation->body_remaining -= input_length; |
| 5368 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5369 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5371 | status = PSA_ERROR_BAD_STATE; |
| 5372 | goto exit; |
| 5373 | } |
| 5374 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 5377 | output, output_size, |
| 5378 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5379 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5380 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5382 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | } else { |
| 5384 | psa_aead_abort(operation); |
| 5385 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5386 | |
David Horstmann | 2914fac | 2023-12-11 15:28:37 +0000 | [diff] [blame] | 5387 | LOCAL_INPUT_FREE(input_external, input); |
| 5388 | LOCAL_OUTPUT_FREE(output_external, output); |
| 5389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5391 | } |
| 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5394 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | if (operation->id == 0 || !operation->nonce_set) { |
| 5396 | return PSA_ERROR_BAD_STATE; |
| 5397 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 5400 | operation->body_remaining != 0)) { |
| 5401 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5402 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5405 | } |
| 5406 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5407 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
David Horstmann | 6db0e73 | 2023-12-11 15:35:59 +0000 | [diff] [blame] | 5409 | uint8_t *ciphertext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | size_t ciphertext_size, |
| 5411 | size_t *ciphertext_length, |
David Horstmann | 6db0e73 | 2023-12-11 15:35:59 +0000 | [diff] [blame] | 5412 | uint8_t *tag_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | size_t tag_size, |
| 5414 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5415 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5416 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5417 | |
David Horstmann | 6db0e73 | 2023-12-11 15:35:59 +0000 | [diff] [blame] | 5418 | LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext); |
| 5419 | LOCAL_OUTPUT_DECLARE(tag_external, tag); |
| 5420 | |
| 5421 | LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext); |
| 5422 | LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag); |
| 5423 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5424 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 5425 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | status = psa_aead_final_checks(operation); |
| 5428 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5429 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5433 | status = PSA_ERROR_BAD_STATE; |
| 5434 | goto exit; |
| 5435 | } |
| 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 5438 | ciphertext_size, |
| 5439 | ciphertext_length, |
| 5440 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5441 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5442 | exit: |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5443 | |
| 5444 | |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5445 | /* In case the operation fails and the user fails to check for failure or |
Paul Elliott | 4c916e8 | 2021-09-19 18:34:50 +0100 | [diff] [blame] | 5446 | * the zero tag size, make sure the tag is set to something implausible. |
| 5447 | * Even if the operation succeeds, make sure we clear the rest of the |
| 5448 | * buffer to prevent potential leakage of anything previously placed in |
| 5449 | * the same buffer.*/ |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5450 | psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length); |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5453 | |
David Horstmann | 6db0e73 | 2023-12-11 15:35:59 +0000 | [diff] [blame] | 5454 | LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext); |
| 5455 | LOCAL_OUTPUT_FREE(tag_external, tag); |
| 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 5461 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
David Horstmann | e000a0a | 2023-12-11 16:37:04 +0000 | [diff] [blame] | 5463 | uint8_t *plaintext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | size_t plaintext_size, |
| 5465 | size_t *plaintext_length, |
David Horstmann | e000a0a | 2023-12-11 16:37:04 +0000 | [diff] [blame] | 5466 | const uint8_t *tag_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5467 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5468 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5469 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5470 | |
David Horstmann | e000a0a | 2023-12-11 16:37:04 +0000 | [diff] [blame] | 5471 | LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext); |
| 5472 | LOCAL_INPUT_DECLARE(tag_external, tag); |
| 5473 | |
| 5474 | LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext); |
| 5475 | LOCAL_INPUT_ALLOC(tag_external, tag_length, tag); |
| 5476 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5477 | *plaintext_length = 0; |
| 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | status = psa_aead_final_checks(operation); |
| 5480 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5481 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5485 | status = PSA_ERROR_BAD_STATE; |
| 5486 | goto exit; |
| 5487 | } |
| 5488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 5490 | plaintext_size, |
| 5491 | plaintext_length, |
| 5492 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5493 | |
| 5494 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5496 | |
David Horstmann | e000a0a | 2023-12-11 16:37:04 +0000 | [diff] [blame] | 5497 | LOCAL_OUTPUT_FREE(plaintext_external, plaintext); |
| 5498 | LOCAL_INPUT_FREE(tag_external, tag); |
| 5499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5505 | { |
| 5506 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5509 | /* The object has (apparently) been initialized but it is not (yet) |
| 5510 | * in use. It's ok to call abort on such an object, and there's |
| 5511 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5513 | } |
| 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5520 | } |
| 5521 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5522 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5523 | /* Generators */ |
| 5524 | /****************************************************************/ |
| 5525 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5526 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5527 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5528 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
Kusumit Ghoderao | af0b534 | 2023-05-03 11:58:46 +0530 | [diff] [blame] | 5529 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5530 | defined(PSA_HAVE_SOFT_PBKDF2) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5531 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5532 | #endif /* At least one builtin KDF */ |
| 5533 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5534 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5535 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5536 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5537 | static psa_status_t psa_key_derivation_start_hmac( |
| 5538 | psa_mac_operation_t *operation, |
| 5539 | psa_algorithm_t hash_alg, |
| 5540 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5542 | { |
| 5543 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 5546 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 5547 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5548 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5549 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 5553 | &attributes, |
| 5554 | hmac_key, hmac_key_length, |
| 5555 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | psa_reset_key_attributes(&attributes); |
| 5558 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5559 | } |
| 5560 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5561 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5562 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5563 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5564 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5565 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5566 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5567 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5569 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5571 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5572 | } else { |
| 5573 | return operation->alg; |
| 5574 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5575 | } |
| 5576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5578 | { |
| 5579 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5581 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5582 | /* The object has (apparently) been initialized but it is not |
| 5583 | * in use. It's ok to call abort on such an object, and there's |
| 5584 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5586 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5588 | mbedtls_free(operation->ctx.hkdf.info); |
| 5589 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5590 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5591 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5592 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5593 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5595 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5596 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5597 | if (operation->ctx.tls12_prf.secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5598 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | operation->ctx.tls12_prf.secret_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5600 | } |
| 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | if (operation->ctx.tls12_prf.seed != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5603 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | operation->ctx.tls12_prf.seed_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5605 | } |
| 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | if (operation->ctx.tls12_prf.label != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5608 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | operation->ctx.tls12_prf.label_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5610 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5611 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5612 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5613 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5614 | operation->ctx.tls12_prf.other_secret_length); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5615 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5616 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5617 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5618 | |
| 5619 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5620 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5622 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5623 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5626 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5627 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5628 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5629 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5630 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 5631 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5632 | if (operation->ctx.pbkdf2.salt != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5633 | mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt, |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5634 | operation->ctx.pbkdf2.salt_length); |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5635 | } |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5636 | |
| 5637 | status = PSA_SUCCESS; |
| 5638 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5639 | #endif /* defined(PSA_HAVE_SOFT_PBKDF2) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5640 | { |
| 5641 | status = PSA_ERROR_BAD_STATE; |
| 5642 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5644 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5645 | } |
| 5646 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5647 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5649 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5651 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5652 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5655 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5660 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5661 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | if (operation->alg == 0) { |
| 5663 | return PSA_ERROR_BAD_STATE; |
| 5664 | } |
| 5665 | if (capacity > operation->capacity) { |
| 5666 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5667 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5668 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5670 | } |
| 5671 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5672 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5673 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5675 | psa_algorithm_t kdf_alg, |
| 5676 | uint8_t *output, |
| 5677 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5678 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5680 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5681 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5682 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5683 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5685 | #else |
| 5686 | const uint8_t last_block = 0xff; |
| 5687 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5690 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5691 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5693 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5694 | )) { |
| 5695 | return PSA_ERROR_BAD_STATE; |
| 5696 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5697 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5700 | /* Copy what remains of the current block */ |
| 5701 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5703 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | } |
| 5705 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5706 | output += n; |
| 5707 | output_length -= n; |
| 5708 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5710 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5712 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5713 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5714 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5715 | * object was corrupted or if this function is called directly |
| 5716 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | if (hkdf->block_number == last_block) { |
| 5718 | return PSA_ERROR_BAD_STATE; |
| 5719 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5720 | |
| 5721 | /* We need a new block */ |
| 5722 | ++hkdf->block_number; |
| 5723 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5726 | hash_alg, |
| 5727 | hkdf->prk, |
| 5728 | hash_length); |
| 5729 | if (status != PSA_SUCCESS) { |
| 5730 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5731 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | |
| 5733 | if (hkdf->block_number != 1) { |
| 5734 | status = psa_mac_update(&hkdf->hmac, |
| 5735 | hkdf->output_block, |
| 5736 | hash_length); |
| 5737 | if (status != PSA_SUCCESS) { |
| 5738 | return status; |
| 5739 | } |
| 5740 | } |
| 5741 | status = psa_mac_update(&hkdf->hmac, |
| 5742 | hkdf->info, |
| 5743 | hkdf->info_length); |
| 5744 | if (status != PSA_SUCCESS) { |
| 5745 | return status; |
| 5746 | } |
| 5747 | status = psa_mac_update(&hkdf->hmac, |
| 5748 | &hkdf->block_number, 1); |
| 5749 | if (status != PSA_SUCCESS) { |
| 5750 | return status; |
| 5751 | } |
| 5752 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5753 | hkdf->output_block, |
| 5754 | sizeof(hkdf->output_block), |
| 5755 | &hmac_output_length); |
| 5756 | if (status != PSA_SUCCESS) { |
| 5757 | return status; |
| 5758 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5759 | } |
| 5760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5762 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5763 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5764 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5765 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5766 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5767 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5768 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5770 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5772 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5773 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5774 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5775 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5776 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5777 | /* We can't be wanting more output after block 0xff, otherwise |
| 5778 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5779 | * prevented this call. It could happen only if the operation |
| 5780 | * object was corrupted or if this function is called directly |
| 5781 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | if (tls12_prf->block_number == 0xff) { |
| 5783 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5784 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5785 | |
| 5786 | /* We need a new block */ |
| 5787 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5788 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5789 | |
| 5790 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5791 | * |
| 5792 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5793 | * |
| 5794 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5795 | * HMAC_hash(secret, A(2) + seed) + |
| 5796 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5797 | * |
| 5798 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5799 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5800 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5801 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5802 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5803 | * is currently extracted as `output_block` and where i is |
| 5804 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5805 | */ |
| 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | status = psa_key_derivation_start_hmac(&hmac, |
| 5808 | hash_alg, |
| 5809 | tls12_prf->secret, |
| 5810 | tls12_prf->secret_length); |
| 5811 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5812 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5817 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5818 | * the variable seed and in this instance means it in the context of the |
| 5819 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | status = psa_mac_update(&hmac, |
| 5821 | tls12_prf->label, |
| 5822 | tls12_prf->label_length); |
| 5823 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5824 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | } |
| 5826 | status = psa_mac_update(&hmac, |
| 5827 | tls12_prf->seed, |
| 5828 | tls12_prf->seed_length); |
| 5829 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5830 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | } |
| 5832 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5833 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5835 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5836 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5838 | } |
| 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | status = psa_mac_sign_finish(&hmac, |
| 5841 | tls12_prf->Ai, hash_length, |
| 5842 | &hmac_output_length); |
| 5843 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5844 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | } |
| 5846 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5847 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5849 | |
| 5850 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | status = psa_key_derivation_start_hmac(&hmac, |
| 5852 | hash_alg, |
| 5853 | tls12_prf->secret, |
| 5854 | tls12_prf->secret_length); |
| 5855 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5856 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | } |
| 5858 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5859 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5860 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | } |
| 5862 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5863 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5864 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | } |
| 5866 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5867 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5868 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | } |
| 5870 | status = psa_mac_sign_finish(&hmac, |
| 5871 | tls12_prf->output_block, hash_length, |
| 5872 | &hmac_output_length); |
| 5873 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5874 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5876 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5877 | |
| 5878 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | cleanup_status = psa_mac_abort(&hmac); |
| 5880 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5881 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5885 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5886 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5887 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5888 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5889 | psa_algorithm_t alg, |
| 5890 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5892 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5894 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5895 | psa_status_t status; |
| 5896 | uint8_t offset, length; |
| 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5899 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5900 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5901 | break; |
| 5902 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5903 | break; |
| 5904 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5906 | } |
| 5907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5909 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | if (tls12_prf->left_in_block == 0) { |
| 5911 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5912 | alg); |
| 5913 | if (status != PSA_SUCCESS) { |
| 5914 | return status; |
| 5915 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5916 | |
| 5917 | continue; |
| 5918 | } |
| 5919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5921 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5923 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5924 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5925 | |
| 5926 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5928 | output += length; |
| 5929 | output_length -= length; |
| 5930 | tls12_prf->left_in_block -= length; |
| 5931 | } |
| 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5934 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5935 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5936 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5937 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5938 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5939 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5940 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5941 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5943 | { |
| 5944 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5945 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | if (output_length != 32) { |
| 5948 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5949 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5952 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5953 | &output_size); |
| 5954 | if (status != PSA_SUCCESS) { |
| 5955 | return status; |
| 5956 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | if (output_size != output_length) { |
| 5959 | return PSA_ERROR_GENERIC_ERROR; |
| 5960 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5963 | } |
| 5964 | #endif |
| 5965 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5966 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5967 | static psa_status_t psa_key_derivation_pbkdf2_generate_block( |
| 5968 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 5969 | psa_algorithm_t prf_alg, |
| 5970 | uint8_t prf_output_length, |
| 5971 | psa_key_attributes_t *attributes) |
| 5972 | { |
| 5973 | psa_status_t status; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5974 | psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5975 | size_t mac_output_length; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5976 | uint8_t U_i[PSA_MAC_MAX_SIZE]; |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5977 | uint8_t *U_accumulator = pbkdf2->output_block; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5978 | uint64_t i; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5979 | uint8_t block_counter[4]; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5980 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5981 | mac_operation.is_sign = 1; |
| 5982 | mac_operation.mac_size = prf_output_length; |
| 5983 | MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5984 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5985 | status = psa_driver_wrapper_mac_sign_setup(&mac_operation, |
| 5986 | attributes, |
| 5987 | pbkdf2->password, |
| 5988 | pbkdf2->password_length, |
| 5989 | prf_alg); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5990 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5991 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5992 | } |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5993 | status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length); |
| 5994 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5995 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5996 | } |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5997 | status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter)); |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5998 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5999 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 6000 | } |
| 6001 | status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i), |
| 6002 | &mac_output_length); |
| 6003 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 6004 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | if (mac_output_length != prf_output_length) { |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 6008 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 6009 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 6010 | } |
| 6011 | |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 6012 | memcpy(U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6013 | |
| 6014 | for (i = 1; i < pbkdf2->input_cost; i++) { |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 6015 | /* We are passing prf_output_length as mac_size because the driver |
| 6016 | * function directly sets mac_output_length as mac_size upon success. |
Kusumit Ghoderao | 5f3345a | 2023-07-27 21:21:38 +0530 | [diff] [blame] | 6017 | * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6018 | status = psa_driver_wrapper_mac_compute(attributes, |
| 6019 | pbkdf2->password, |
| 6020 | pbkdf2->password_length, |
| 6021 | prf_alg, U_i, prf_output_length, |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 6022 | U_i, prf_output_length, |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6023 | &mac_output_length); |
| 6024 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 6025 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6026 | } |
| 6027 | |
Kusumit Ghoderao | 109ee3d | 2023-06-08 16:36:45 +0530 | [diff] [blame] | 6028 | mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6029 | } |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 6030 | |
| 6031 | cleanup: |
| 6032 | /* Zeroise buffers to clear sensitive data from memory. */ |
| 6033 | mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE); |
| 6034 | return status; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6035 | } |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 6036 | |
| 6037 | static psa_status_t psa_key_derivation_pbkdf2_read( |
| 6038 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 6039 | psa_algorithm_t kdf_alg, |
| 6040 | uint8_t *output, |
| 6041 | size_t output_length) |
| 6042 | { |
| 6043 | psa_status_t status; |
| 6044 | psa_algorithm_t prf_alg; |
| 6045 | uint8_t prf_output_length; |
| 6046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6047 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length)); |
| 6048 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 6049 | |
| 6050 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 6051 | prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg)); |
| 6052 | prf_output_length = PSA_HASH_LENGTH(prf_alg); |
| 6053 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Kusumit Ghoderao | a2520a5 | 2023-06-22 15:42:19 +0530 | [diff] [blame] | 6054 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 6055 | prf_alg = PSA_ALG_CMAC; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6056 | prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC); |
Kusumit Ghoderao | a2520a5 | 2023-06-22 15:42:19 +0530 | [diff] [blame] | 6057 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
Kusumit Ghoderao | d9ec1af | 2023-06-08 20:19:51 +0530 | [diff] [blame] | 6058 | } else { |
| 6059 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 6060 | } |
| 6061 | |
| 6062 | switch (pbkdf2->state) { |
| 6063 | case PSA_PBKDF2_STATE_PASSWORD_SET: |
| 6064 | /* Initially we need a new block so bytes_used is equal to block size*/ |
| 6065 | pbkdf2->bytes_used = prf_output_length; |
| 6066 | pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT; |
| 6067 | break; |
| 6068 | case PSA_PBKDF2_STATE_OUTPUT: |
| 6069 | break; |
| 6070 | default: |
| 6071 | return PSA_ERROR_BAD_STATE; |
| 6072 | } |
| 6073 | |
| 6074 | while (output_length != 0) { |
| 6075 | uint8_t n = prf_output_length - pbkdf2->bytes_used; |
| 6076 | if (n > output_length) { |
| 6077 | n = (uint8_t) output_length; |
| 6078 | } |
| 6079 | memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n); |
| 6080 | output += n; |
| 6081 | output_length -= n; |
| 6082 | pbkdf2->bytes_used += n; |
| 6083 | |
| 6084 | if (output_length == 0) { |
| 6085 | break; |
| 6086 | } |
| 6087 | |
| 6088 | /* We need a new block */ |
| 6089 | pbkdf2->bytes_used = 0; |
| 6090 | pbkdf2->block_number++; |
| 6091 | |
| 6092 | status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg, |
| 6093 | prf_output_length, |
| 6094 | &attributes); |
| 6095 | if (status != PSA_SUCCESS) { |
| 6096 | return status; |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | return PSA_SUCCESS; |
| 6101 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6102 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 6103 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 6104 | psa_status_t psa_key_derivation_output_bytes( |
| 6105 | psa_key_derivation_operation_t *operation, |
Ryan Everett | f943e22 | 2024-01-19 14:46:39 +0000 | [diff] [blame] | 6106 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6108 | { |
| 6109 | psa_status_t status; |
Ryan Everett | f943e22 | 2024-01-19 14:46:39 +0000 | [diff] [blame] | 6110 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6115 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6120 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6121 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6122 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 6123 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6124 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6125 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6127 | } |
Ryan Everett | f943e22 | 2024-01-19 14:46:39 +0000 | [diff] [blame] | 6128 | |
| 6129 | LOCAL_OUTPUT_ALLOC(output_external, output_length, output); |
Ryan Everett | da9227d | 2024-01-25 11:37:22 +0000 | [diff] [blame] | 6130 | if (output_length > operation->capacity) { |
| 6131 | operation->capacity = 0; |
| 6132 | /* Go through the error path to wipe all confidential data now |
| 6133 | * that the operation object is useless. */ |
| 6134 | status = PSA_ERROR_INSUFFICIENT_DATA; |
| 6135 | goto exit; |
| 6136 | } |
| 6137 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6138 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6139 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6140 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 6142 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 6143 | output, output_length); |
| 6144 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6145 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6146 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6147 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 6149 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6150 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 6151 | kdf_alg, output, |
| 6152 | output_length); |
| 6153 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6154 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 6155 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6156 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6158 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 6160 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6161 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6162 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 6163 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 6164 | status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg, |
| 6165 | output, output_length); |
Kusumit Ghoderao | 056f0c5 | 2023-05-03 15:58:34 +0530 | [diff] [blame] | 6166 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6167 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6168 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6169 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 6170 | (void) kdf_alg; |
Ryan Everett | f943e22 | 2024-01-19 14:46:39 +0000 | [diff] [blame] | 6171 | status = PSA_ERROR_BAD_STATE; |
| 6172 | LOCAL_OUTPUT_FREE(output_external, output); |
| 6173 | |
| 6174 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6179 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6180 | * This allows us to differentiate between exhausted operations and |
| 6181 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 6182 | * operations. */ |
| 6183 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6185 | operation->alg = alg; |
Ryan Everett | ee5920a | 2024-02-09 15:09:28 +0000 | [diff] [blame] | 6186 | if (output != NULL) { |
| 6187 | memset(output, '!', output_length); |
| 6188 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6189 | } |
Ryan Everett | da9227d | 2024-01-25 11:37:22 +0000 | [diff] [blame] | 6190 | |
| 6191 | LOCAL_OUTPUT_FREE(output_external, output); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6193 | } |
| 6194 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 6195 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6196 | static void psa_des_set_key_parity(uint8_t *data, size_t data_size) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 6197 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | if (data_size >= 8) { |
| 6199 | mbedtls_des_key_set_parity(data); |
| 6200 | } |
| 6201 | if (data_size >= 16) { |
| 6202 | mbedtls_des_key_set_parity(data + 8); |
| 6203 | } |
| 6204 | if (data_size >= 24) { |
| 6205 | mbedtls_des_key_set_parity(data + 16); |
| 6206 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 6207 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 6208 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 6209 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6210 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 6212 | * of a private key which is an integer |
| 6213 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 6214 | * N is the prime p for Diffie-Hellman, or the order of the |
| 6215 | * curve’s base point for ECC. |
| 6216 | * |
| 6217 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 6218 | * This function generates the private key using the following process: |
| 6219 | * |
| 6220 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 6221 | * 2. If m is not a multiple of 8, set the most significant |
| 6222 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 6223 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 6224 | * 4. If k > N - 2, discard the result and return to step 1. |
| 6225 | * 5. Output k + 1 as the private key. |
| 6226 | * |
| 6227 | * This method allows compliance to NIST standards, specifically the methods titled |
| 6228 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 6229 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 6230 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 6231 | * Diffie-Hellman keys. |
| 6232 | * |
| 6233 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 6234 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 6235 | * |
| 6236 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 6237 | * always NULL. |
| 6238 | */ |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 6239 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
| 6240 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6241 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6242 | psa_key_slot_t *slot, |
| 6243 | size_t bits, |
| 6244 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6245 | uint8_t **data |
| 6246 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6247 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6248 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6249 | mbedtls_mpi k; |
| 6250 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6253 | size_t m; |
| 6254 | size_t m_bytes; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | mbedtls_mpi_init(&k); |
| 6257 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 6258 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6259 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6261 | mbedtls_ecp_group_id grp_id = |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 6262 | mbedtls_ecc_group_from_psa(curve, bits); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6265 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 6266 | goto cleanup; |
| 6267 | } |
| 6268 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6269 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6273 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6274 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6275 | /* Let m be the bit size of N. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6276 | m = ecp_group.nbits; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6277 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6278 | m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6279 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6280 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2)); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6282 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6283 | /* Note: This function is always called with *data == NULL and it |
| 6284 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | *data = mbedtls_calloc(1, m_bytes); |
| 6286 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6287 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6288 | goto cleanup; |
| 6289 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6292 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6294 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6295 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6296 | |
| 6297 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6299 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6300 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 6301 | * the string to zero. |
| 6302 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6304 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | * big-endian byte string. |
| 6309 | */ |
| 6310 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6311 | |
| 6312 | /* 4. If k > N - 2, discard the result and return to step 1. |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | * Result of comparison is returned. When it indicates error |
| 6314 | * then this function is called again. |
| 6315 | */ |
| 6316 | MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6317 | } |
| 6318 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6319 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 6321 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6322 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | if (ret != 0) { |
| 6324 | status = mbedtls_to_psa_error(ret); |
| 6325 | } |
| 6326 | if (status != PSA_SUCCESS) { |
| 6327 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6328 | *data = NULL; |
| 6329 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | mbedtls_mpi_free(&k); |
| 6331 | mbedtls_mpi_free(&diff_N_2); |
| 6332 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6333 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6334 | |
| 6335 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 6336 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 6337 | * |
| 6338 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 6339 | * draw a 32-byte string and process it as specified in |
| 6340 | * Elliptic Curves for Security [RFC7748] §5. |
| 6341 | * |
| 6342 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 6343 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 6344 | * |
| 6345 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 6346 | * always NULL. |
| 6347 | */ |
| 6348 | |
| 6349 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6350 | size_t bits, |
| 6351 | psa_key_derivation_operation_t *operation, |
| 6352 | uint8_t **data |
| 6353 | ) |
| 6354 | { |
| 6355 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6356 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6359 | case 255: |
| 6360 | output_length = 32; |
| 6361 | break; |
| 6362 | case 448: |
| 6363 | output_length = 56; |
| 6364 | break; |
| 6365 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6367 | break; |
| 6368 | } |
| 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | if (*data == NULL) { |
| 6373 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6374 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6379 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6383 | case 255: |
| 6384 | (*data)[0] &= 248; |
| 6385 | (*data)[31] &= 127; |
| 6386 | (*data)[31] |= 64; |
| 6387 | break; |
| 6388 | case 448: |
| 6389 | (*data)[0] &= 252; |
| 6390 | (*data)[55] |= 128; |
| 6391 | break; |
| 6392 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6394 | break; |
| 6395 | } |
| 6396 | |
| 6397 | return status; |
| 6398 | } |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 6399 | #else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6400 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
| 6401 | psa_key_slot_t *slot, size_t bits, |
| 6402 | psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6403 | { |
| 6404 | (void) slot; |
| 6405 | (void) bits; |
| 6406 | (void) operation; |
| 6407 | (void) data; |
| 6408 | return PSA_ERROR_NOT_SUPPORTED; |
| 6409 | } |
| 6410 | |
| 6411 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6412 | size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6413 | { |
| 6414 | (void) bits; |
| 6415 | (void) operation; |
| 6416 | (void) data; |
| 6417 | return PSA_ERROR_NOT_SUPPORTED; |
| 6418 | } |
| 6419 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6420 | #endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6421 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 6422 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6423 | psa_key_slot_t *slot, |
| 6424 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6426 | { |
| 6427 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6428 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6429 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6430 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6431 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 6434 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6435 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6436 | |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6437 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \ |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6438 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 6440 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 6441 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6442 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 6444 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6445 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | } |
| 6447 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6448 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 6450 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6451 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6453 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 6454 | } else |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6455 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6456 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6457 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 6458 | if (bits % 8 != 0) { |
| 6459 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6460 | } |
| 6461 | data = mbedtls_calloc(1, bytes); |
| 6462 | if (data == NULL) { |
| 6463 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6464 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6466 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 6467 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6468 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6470 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 6472 | psa_des_set_key_parity(data, bytes); |
| 6473 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 6474 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | } else { |
| 6476 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6477 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 6478 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6479 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6480 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6481 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 6482 | }; |
| 6483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 6485 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 6486 | &storage_size); |
| 6487 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6488 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6490 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 6492 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6493 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6494 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | status = psa_driver_wrapper_import_key(&attributes, |
| 6497 | data, bytes, |
| 6498 | slot->key.data, |
| 6499 | slot->key.bytes, |
| 6500 | &slot->key.bytes, &bits); |
| 6501 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6502 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6503 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6504 | |
| 6505 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6506 | mbedtls_free(data); |
| 6507 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6508 | } |
| 6509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 6511 | psa_key_derivation_operation_t *operation, |
| 6512 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6513 | { |
| 6514 | psa_status_t status; |
| 6515 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6516 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6517 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6518 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6519 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6520 | /* Reject any attempt to create a zero-length key so that we don't |
| 6521 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6522 | if (psa_get_key_bits(attributes) == 0) { |
| 6523 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6524 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6526 | if (operation->alg == PSA_ALG_NONE) { |
| 6527 | return PSA_ERROR_BAD_STATE; |
| 6528 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 6529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6530 | if (!operation->can_output_key) { |
| 6531 | return PSA_ERROR_NOT_PERMITTED; |
| 6532 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6534 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 6535 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6536 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6537 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6538 | /* Deriving a key in a secure element is not implemented yet. */ |
| 6539 | status = PSA_ERROR_NOT_SUPPORTED; |
| 6540 | } |
| 6541 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | if (status == PSA_SUCCESS) { |
| 6543 | status = psa_generate_derived_key_internal(slot, |
| 6544 | attributes->core.bits, |
| 6545 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6546 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6547 | if (status == PSA_SUCCESS) { |
| 6548 | status = psa_finish_key_creation(slot, driver, key); |
| 6549 | } |
| 6550 | if (status != PSA_SUCCESS) { |
| 6551 | psa_fail_key_creation(slot, driver); |
| 6552 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6554 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6555 | } |
| 6556 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6557 | |
| 6558 | |
| 6559 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6560 | /* Key derivation */ |
| 6561 | /****************************************************************/ |
| 6562 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 6563 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6565 | { |
| 6566 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6567 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 6568 | return 1; |
| 6569 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6570 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6571 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6572 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6573 | return 1; |
| 6574 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6575 | #endif |
| 6576 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6577 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6578 | return 1; |
| 6579 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6580 | #endif |
| 6581 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6583 | return 1; |
| 6584 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6585 | #endif |
| 6586 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6587 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6588 | return 1; |
| 6589 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6590 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6591 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6592 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6593 | return 1; |
| 6594 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6595 | #endif |
Kusumit Ghoderao | d132cac | 2023-05-03 12:00:27 +0530 | [diff] [blame] | 6596 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
| 6597 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 6598 | return 1; |
| 6599 | } |
| 6600 | #endif |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6601 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 6602 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 6603 | return 1; |
| 6604 | } |
| 6605 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6609 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6610 | { |
| 6611 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6612 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 6613 | psa_hash_abort(&operation); |
| 6614 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6615 | } |
| 6616 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6617 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6618 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6619 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6620 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6621 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 6622 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6624 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6625 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 6627 | return PSA_ERROR_NOT_SUPPORTED; |
| 6628 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6629 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6630 | /* All currently supported key derivation algorithms (apart from |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6631 | * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6632 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 6633 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6634 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6635 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
| 6636 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6637 | hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC); |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6638 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | if (hash_size == 0) { |
| 6640 | return PSA_ERROR_NOT_SUPPORTED; |
| 6641 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6642 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6643 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 6644 | * we might fail later, which is somewhat unfriendly and potentially |
| 6645 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 6647 | if (status != PSA_SUCCESS) { |
| 6648 | return status; |
| 6649 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6650 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 6653 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 6654 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 6655 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6656 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 6657 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6658 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6659 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 6660 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6661 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6663 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 6664 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | operation->capacity = 255 * hash_size; |
| 6666 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6667 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6670 | { |
| 6671 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | if (alg == PSA_ALG_ECDH) { |
| 6673 | return PSA_SUCCESS; |
| 6674 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6675 | #endif |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 6676 | #if defined(PSA_WANT_ALG_FFDH) |
| 6677 | if (alg == PSA_ALG_FFDH) { |
| 6678 | return PSA_SUCCESS; |
| 6679 | } |
| 6680 | #endif |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6681 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6682 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6683 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6684 | |
| 6685 | static int psa_key_derivation_allows_free_form_secret_input( |
| 6686 | psa_algorithm_t kdf_alg) |
| 6687 | { |
| 6688 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6689 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6690 | return 0; |
| 6691 | } |
| 6692 | #endif |
| 6693 | (void) kdf_alg; |
| 6694 | return 1; |
| 6695 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6696 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6698 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 6699 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6700 | { |
| 6701 | psa_status_t status; |
| 6702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6703 | if (operation->alg != 0) { |
| 6704 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6705 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6707 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6708 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6709 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 6710 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6711 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 6712 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 6713 | status = psa_key_agreement_try_support(ka_alg); |
| 6714 | if (status != PSA_SUCCESS) { |
| 6715 | return status; |
| 6716 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6717 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 6718 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6719 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6720 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 6721 | #else |
| 6722 | return PSA_ERROR_NOT_SUPPORTED; |
| 6723 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6724 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 6725 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6726 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 6727 | #else |
| 6728 | return PSA_ERROR_NOT_SUPPORTED; |
| 6729 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6730 | } else { |
| 6731 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6732 | } |
| 6733 | |
| 6734 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6735 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6736 | } |
| 6737 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6738 | } |
| 6739 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6740 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6741 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 6742 | psa_algorithm_t kdf_alg, |
| 6743 | psa_key_derivation_step_t step, |
| 6744 | const uint8_t *data, |
| 6745 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6746 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6747 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6748 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6749 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6750 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6751 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6753 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6754 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6755 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6757 | return PSA_ERROR_BAD_STATE; |
| 6758 | } else { |
| 6759 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6760 | hash_alg, |
| 6761 | data, data_length); |
| 6762 | if (status != PSA_SUCCESS) { |
| 6763 | return status; |
| 6764 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6765 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6766 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6767 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6768 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6769 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6770 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6771 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6772 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6773 | * the state. It could happen only if the hkdf |
| 6774 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6775 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6776 | return PSA_ERROR_BAD_STATE; |
| 6777 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6778 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6779 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6780 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6781 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6782 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6784 | memcpy(hkdf->prk, data, data_length); |
| 6785 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6786 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6787 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6788 | /* HKDF: If no salt was provided, use an empty salt. |
| 6789 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6790 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6791 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6792 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6793 | return PSA_ERROR_BAD_STATE; |
| 6794 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6795 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6796 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6797 | hash_alg, |
| 6798 | NULL, 0); |
| 6799 | if (status != PSA_SUCCESS) { |
| 6800 | return status; |
| 6801 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6802 | hkdf->state = HKDF_STATE_STARTED; |
| 6803 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6804 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6805 | return PSA_ERROR_BAD_STATE; |
| 6806 | } |
| 6807 | status = psa_mac_update(&hkdf->hmac, |
| 6808 | data, data_length); |
| 6809 | if (status != PSA_SUCCESS) { |
| 6810 | return status; |
| 6811 | } |
| 6812 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6813 | hkdf->prk, |
| 6814 | sizeof(hkdf->prk), |
| 6815 | &data_length); |
| 6816 | if (status != PSA_SUCCESS) { |
| 6817 | return status; |
| 6818 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6819 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6820 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6821 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6822 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6823 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6824 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6825 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6827 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6828 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6829 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6830 | { |
| 6831 | /* Block 0 is empty, and the next block will be |
| 6832 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6833 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6834 | } |
| 6835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6836 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6837 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6838 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6839 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6840 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6841 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6842 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6843 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6845 | hkdf->state == HKDF_STATE_INIT) { |
| 6846 | return PSA_ERROR_BAD_STATE; |
| 6847 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6848 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6850 | return PSA_ERROR_BAD_STATE; |
| 6851 | } |
| 6852 | if (hkdf->info_set) { |
| 6853 | return PSA_ERROR_BAD_STATE; |
| 6854 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6855 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | if (data_length != 0) { |
| 6857 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6858 | if (hkdf->info == NULL) { |
| 6859 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6860 | } |
| 6861 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6862 | } |
| 6863 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6864 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6865 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6866 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6867 | } |
| 6868 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6869 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6870 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6871 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6872 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6873 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6874 | const uint8_t *data, |
| 6875 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6876 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6877 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6878 | return PSA_ERROR_BAD_STATE; |
| 6879 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | if (data_length != 0) { |
| 6882 | prf->seed = mbedtls_calloc(1, data_length); |
| 6883 | if (prf->seed == NULL) { |
| 6884 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6885 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6887 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6888 | prf->seed_length = data_length; |
| 6889 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6890 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6891 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6894 | } |
| 6895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6896 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6897 | const uint8_t *data, |
| 6898 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6899 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6900 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6901 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6902 | return PSA_ERROR_BAD_STATE; |
| 6903 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6905 | if (data_length != 0) { |
| 6906 | prf->secret = mbedtls_calloc(1, data_length); |
| 6907 | if (prf->secret == NULL) { |
| 6908 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6909 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6911 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6912 | prf->secret_length = data_length; |
| 6913 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6914 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6915 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6917 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6918 | } |
| 6919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6920 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6921 | const uint8_t *data, |
| 6922 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6923 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6924 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6925 | return PSA_ERROR_BAD_STATE; |
| 6926 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6928 | if (data_length != 0) { |
| 6929 | prf->label = mbedtls_calloc(1, data_length); |
| 6930 | if (prf->label == NULL) { |
| 6931 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6932 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6934 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6935 | prf->label_length = data_length; |
| 6936 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6937 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6938 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6940 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6941 | } |
| 6942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6943 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6944 | psa_key_derivation_step_t step, |
| 6945 | const uint8_t *data, |
| 6946 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6947 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6948 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6949 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6950 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6951 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6952 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6953 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6954 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6955 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6956 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6957 | } |
| 6958 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6959 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6960 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6961 | |
| 6962 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6963 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6964 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6965 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6966 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6967 | { |
| 6968 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6969 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6970 | 4 + data_length + prf->other_secret_length : |
| 6971 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6973 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6974 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6975 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6977 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6978 | if (pms == NULL) { |
| 6979 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6980 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6981 | uint8_t *cur = pms; |
| 6982 | |
| 6983 | /* pure-PSK: |
| 6984 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6985 | * |
| 6986 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6987 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6988 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6989 | * |
| 6990 | * mixed-PSK: |
| 6991 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6992 | * follows: concatenate a uint16 with the length of the other secret, |
| 6993 | * the other secret itself, uint16 with the length of PSK, and the |
| 6994 | * PSK itself. |
| 6995 | * For details please check: |
| 6996 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6997 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6998 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6999 | */ |
| 7000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7001 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 7002 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 7003 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 7004 | if (prf->other_secret_length != 0) { |
| 7005 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 7006 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 7007 | cur += prf->other_secret_length; |
| 7008 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7009 | } else { |
| 7010 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 7011 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 7012 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 7013 | cur += data_length; |
| 7014 | } |
| 7015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7016 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 7017 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 7018 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7019 | cur += data_length; |
| 7020 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7021 | status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms)); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7022 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 7023 | mbedtls_zeroize_and_free(pms, pms_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7024 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7025 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7026 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7027 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 7028 | psa_tls12_prf_key_derivation_t *prf, |
| 7029 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7030 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7031 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7032 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 7033 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7034 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7035 | |
| 7036 | if (data_length != 0) { |
| 7037 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 7038 | if (prf->other_secret == NULL) { |
| 7039 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 7040 | } |
| 7041 | |
| 7042 | memcpy(prf->other_secret, data, data_length); |
| 7043 | prf->other_secret_length = data_length; |
| 7044 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7045 | prf->other_secret_length = 0; |
| 7046 | } |
| 7047 | |
| 7048 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 7049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7050 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7051 | } |
| 7052 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7053 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 7054 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7055 | psa_key_derivation_step_t step, |
| 7056 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7057 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7058 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7059 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7060 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7061 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 7062 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7063 | break; |
| 7064 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7065 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 7066 | data, |
| 7067 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7068 | break; |
| 7069 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7070 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7071 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7072 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 7073 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 7074 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7075 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7076 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7077 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 7078 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 7079 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 7080 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7081 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7083 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 7085 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 7086 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 7087 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7088 | |
| 7089 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7090 | if (data[0] != 0x04) { |
| 7091 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7092 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7093 | |
| 7094 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7097 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7098 | } |
| 7099 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7100 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7101 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7102 | static psa_status_t psa_pbkdf2_set_input_cost( |
| 7103 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 7104 | psa_key_derivation_step_t step, |
| 7105 | uint64_t data) |
| 7106 | { |
| 7107 | if (step != PSA_KEY_DERIVATION_INPUT_COST) { |
| 7108 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7109 | } |
| 7110 | |
| 7111 | if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) { |
| 7112 | return PSA_ERROR_BAD_STATE; |
| 7113 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 7114 | |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 7115 | if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) { |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 7116 | return PSA_ERROR_NOT_SUPPORTED; |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7117 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 7118 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7119 | if (data == 0) { |
| 7120 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7121 | } |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 7122 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7123 | pbkdf2->input_cost = data; |
| 7124 | pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET; |
| 7125 | |
| 7126 | return PSA_SUCCESS; |
| 7127 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 7128 | |
| 7129 | static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2, |
| 7130 | const uint8_t *data, |
| 7131 | size_t data_length) |
| 7132 | { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 7133 | if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) { |
| 7134 | pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET; |
| 7135 | } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) { |
| 7136 | /* Appending to existing salt. No state change. */ |
| 7137 | } else { |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 7138 | return PSA_ERROR_BAD_STATE; |
| 7139 | } |
| 7140 | |
Gilles Peskine | ca57d78 | 2023-07-20 19:08:06 +0200 | [diff] [blame] | 7141 | if (data_length == 0) { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 7142 | /* Appending an empty string, nothing to do. */ |
| 7143 | } else { |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 7144 | uint8_t *next_salt; |
Kusumit Ghoderao | b538bb7 | 2023-05-24 12:32:14 +0530 | [diff] [blame] | 7145 | |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 7146 | next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length); |
| 7147 | if (next_salt == NULL) { |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 7148 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 7149 | } |
| 7150 | |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 7151 | if (pbkdf2->salt_length != 0) { |
| 7152 | memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length); |
| 7153 | } |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 7154 | memcpy(next_salt + pbkdf2->salt_length, data, data_length); |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 7155 | pbkdf2->salt_length += data_length; |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 7156 | mbedtls_free(pbkdf2->salt); |
| 7157 | pbkdf2->salt = next_salt; |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 7158 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 7159 | return PSA_SUCCESS; |
| 7160 | } |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7161 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 7162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7163 | static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg, |
Kusumit Ghoderao | aac9a58 | 2023-05-24 14:19:17 +0530 | [diff] [blame] | 7164 | const uint8_t *input, |
| 7165 | size_t input_len, |
| 7166 | uint8_t *output, |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 7167 | size_t *output_len) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7168 | { |
| 7169 | psa_status_t status = PSA_SUCCESS; |
| 7170 | if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) { |
Ryan Everett | 5d2e82f | 2024-02-07 17:24:59 +0000 | [diff] [blame] | 7171 | return psa_hash_compute(hash_alg, input, input_len, output, |
| 7172 | PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len); |
| 7173 | } else if (input_len > 0) { |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7174 | memcpy(output, input, input_len); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7175 | } |
Ryan Everett | 5d2e82f | 2024-02-07 17:24:59 +0000 | [diff] [blame] | 7176 | *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7177 | return status; |
| 7178 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 7179 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7180 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 7181 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7182 | static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input, |
| 7183 | size_t input_len, |
| 7184 | uint8_t *output, |
| 7185 | size_t *output_len) |
| 7186 | { |
| 7187 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 7188 | if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) { |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7189 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Kusumit Ghoderao | 3fde8fe | 2023-06-27 10:41:43 +0530 | [diff] [blame] | 7190 | uint8_t zeros[16] = { 0 }; |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7191 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 7192 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros))); |
| 7193 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 7194 | /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as |
Kusumit Ghoderao | 5f3345a | 2023-07-27 21:21:38 +0530 | [diff] [blame] | 7195 | * mac_size as the driver function sets mac_output_length = mac_size |
| 7196 | * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7197 | status = psa_driver_wrapper_mac_compute(&attributes, |
| 7198 | zeros, sizeof(zeros), |
| 7199 | PSA_ALG_CMAC, input, input_len, |
| 7200 | output, |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 7201 | PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, |
| 7202 | 128U, |
| 7203 | PSA_ALG_CMAC), |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7204 | output_len); |
| 7205 | } else { |
| 7206 | memcpy(output, input, input_len); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 7207 | *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC); |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7208 | } |
| 7209 | return status; |
| 7210 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 7211 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7212 | |
| 7213 | static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2, |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7214 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7215 | const uint8_t *data, |
| 7216 | size_t data_length) |
| 7217 | { |
Kusumit Ghoderao | aac9a58 | 2023-05-24 14:19:17 +0530 | [diff] [blame] | 7218 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7219 | if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) { |
| 7220 | return PSA_ERROR_BAD_STATE; |
| 7221 | } |
| 7222 | |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7224 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 7225 | psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg); |
| 7226 | status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length, |
| 7227 | pbkdf2->password, |
| 7228 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7229 | } else |
| 7230 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
| 7231 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 7232 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7233 | status = psa_pbkdf2_cmac_set_password(data, data_length, |
| 7234 | pbkdf2->password, |
| 7235 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7236 | } else |
| 7237 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
| 7238 | { |
| 7239 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7240 | } |
| 7241 | |
| 7242 | pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET; |
| 7243 | |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7244 | return status; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7245 | } |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7246 | |
| 7247 | static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2, |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7248 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7249 | psa_key_derivation_step_t step, |
| 7250 | const uint8_t *data, |
| 7251 | size_t data_length) |
| 7252 | { |
| 7253 | switch (step) { |
| 7254 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 7255 | return psa_pbkdf2_set_salt(pbkdf2, data, data_length); |
| 7256 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7257 | return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7258 | default: |
| 7259 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7260 | } |
| 7261 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7262 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7263 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 7264 | /** Check whether the given key type is acceptable for the given |
| 7265 | * input step of a key derivation. |
| 7266 | * |
| 7267 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 7268 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 7269 | * Both secret and non-secret inputs can alternatively have the type |
| 7270 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 7271 | * that the input was passed as a buffer rather than via a key object. |
| 7272 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7273 | static int psa_key_derivation_check_input_type( |
| 7274 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7275 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7276 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7277 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7278 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7279 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7280 | return PSA_SUCCESS; |
| 7281 | } |
| 7282 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7283 | return PSA_SUCCESS; |
| 7284 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7285 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7286 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7287 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7288 | return PSA_SUCCESS; |
| 7289 | } |
| 7290 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7291 | return PSA_SUCCESS; |
| 7292 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7293 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7294 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 7295 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 7296 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 7297 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7298 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 7299 | return PSA_SUCCESS; |
| 7300 | } |
| 7301 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7302 | return PSA_SUCCESS; |
| 7303 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7304 | break; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7305 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
| 7306 | if (key_type == PSA_KEY_TYPE_PASSWORD) { |
| 7307 | return PSA_SUCCESS; |
| 7308 | } |
| 7309 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7310 | return PSA_SUCCESS; |
| 7311 | } |
| 7312 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7313 | return PSA_SUCCESS; |
| 7314 | } |
| 7315 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7316 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7317 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7318 | } |
| 7319 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 7320 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7321 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7322 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7323 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7324 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7325 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7326 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7327 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7328 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7330 | status = psa_key_derivation_check_input_type(step, key_type); |
| 7331 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7332 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7333 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7334 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7335 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7336 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 7337 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 7338 | step, data, data_length); |
| 7339 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7340 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7341 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7342 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 7343 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 7344 | step, data, data_length); |
| 7345 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7346 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 7347 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7348 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 7349 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 7350 | step, data, data_length); |
| 7351 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7352 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7353 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7354 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7355 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7356 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 7357 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7358 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7359 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7360 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7361 | status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg, |
| 7362 | step, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7363 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7364 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7365 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7366 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 7367 | (void) data; |
| 7368 | (void) data_length; |
| 7369 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7370 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7371 | } |
| 7372 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7373 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7374 | if (status != PSA_SUCCESS) { |
| 7375 | psa_key_derivation_abort(operation); |
| 7376 | } |
| 7377 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7378 | } |
| 7379 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7380 | static psa_status_t psa_key_derivation_input_integer_internal( |
| 7381 | psa_key_derivation_operation_t *operation, |
| 7382 | psa_key_derivation_step_t step, |
| 7383 | uint64_t value) |
| 7384 | { |
| 7385 | psa_status_t status; |
| 7386 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 7387 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7388 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7389 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7390 | status = psa_pbkdf2_set_input_cost( |
| 7391 | &operation->ctx.pbkdf2, step, value); |
| 7392 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7393 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7394 | { |
Kusumit Ghoderao | 3a18dee | 2023-04-07 16:16:27 +0530 | [diff] [blame] | 7395 | (void) step; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7396 | (void) value; |
| 7397 | (void) kdf_alg; |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 7398 | status = PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | if (status != PSA_SUCCESS) { |
| 7402 | psa_key_derivation_abort(operation); |
| 7403 | } |
| 7404 | return status; |
| 7405 | } |
| 7406 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7407 | psa_status_t psa_key_derivation_input_bytes( |
| 7408 | psa_key_derivation_operation_t *operation, |
| 7409 | psa_key_derivation_step_t step, |
Ryan Everett | d1e398c | 2024-01-19 14:46:00 +0000 | [diff] [blame] | 7410 | const uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7411 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7412 | { |
Ryan Everett | d1e398c | 2024-01-19 14:46:00 +0000 | [diff] [blame] | 7413 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7414 | LOCAL_INPUT_DECLARE(data_external, data); |
| 7415 | |
| 7416 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 7417 | |
| 7418 | status = psa_key_derivation_input_internal(operation, step, |
| 7419 | PSA_KEY_TYPE_NONE, |
| 7420 | data, data_length); |
Ryan Everett | b41c3c9 | 2024-01-25 11:56:35 +0000 | [diff] [blame] | 7421 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Ryan Everett | d1e398c | 2024-01-19 14:46:00 +0000 | [diff] [blame] | 7422 | exit: |
Ryan Everett | b41c3c9 | 2024-01-25 11:56:35 +0000 | [diff] [blame] | 7423 | #endif |
Ryan Everett | d1e398c | 2024-01-19 14:46:00 +0000 | [diff] [blame] | 7424 | LOCAL_INPUT_FREE(data_external, data); |
| 7425 | return status; |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7426 | } |
| 7427 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7428 | psa_status_t psa_key_derivation_input_integer( |
| 7429 | psa_key_derivation_operation_t *operation, |
| 7430 | psa_key_derivation_step_t step, |
| 7431 | uint64_t value) |
| 7432 | { |
| 7433 | return psa_key_derivation_input_integer_internal(operation, step, value); |
| 7434 | } |
| 7435 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7436 | psa_status_t psa_key_derivation_input_key( |
| 7437 | psa_key_derivation_operation_t *operation, |
| 7438 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7439 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7440 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7441 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7442 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7443 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7444 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7445 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7446 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7447 | if (status != PSA_SUCCESS) { |
| 7448 | psa_key_derivation_abort(operation); |
| 7449 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 7450 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7451 | |
Kusumit Ghoderao | 3128c5d | 2023-05-03 12:27:57 +0530 | [diff] [blame] | 7452 | /* Passing a key object as a SECRET or PASSWORD input unlocks the |
| 7453 | * permission to output to a key object. */ |
| 7454 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET || |
| 7455 | step == PSA_KEY_DERIVATION_INPUT_PASSWORD) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7456 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7457 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7459 | status = psa_key_derivation_input_internal(operation, |
| 7460 | step, slot->attr.type, |
| 7461 | slot->key.data, |
| 7462 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7463 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7464 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7466 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7467 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7468 | |
| 7469 | |
| 7470 | |
| 7471 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7472 | /* Key agreement */ |
| 7473 | /****************************************************************/ |
| 7474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7475 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 7476 | const uint8_t *key_buffer, |
| 7477 | size_t key_buffer_size, |
| 7478 | psa_algorithm_t alg, |
| 7479 | const uint8_t *peer_key, |
| 7480 | size_t peer_key_length, |
| 7481 | uint8_t *shared_secret, |
| 7482 | size_t shared_secret_size, |
| 7483 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 7484 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7485 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 7486 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7487 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7488 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 7489 | key_buffer_size, alg, |
| 7490 | peer_key, peer_key_length, |
| 7491 | shared_secret, |
| 7492 | shared_secret_size, |
| 7493 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7494 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7495 | |
| 7496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH) |
| 7497 | case PSA_ALG_FFDH: |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 7498 | return mbedtls_psa_ffdh_key_agreement(attributes, |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 7499 | peer_key, |
| 7500 | peer_key_length, |
| 7501 | key_buffer, |
| 7502 | key_buffer_size, |
| 7503 | shared_secret, |
| 7504 | shared_secret_size, |
| 7505 | shared_secret_length); |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7506 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */ |
| 7507 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7508 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7509 | (void) attributes; |
| 7510 | (void) key_buffer; |
| 7511 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 7512 | (void) peer_key; |
| 7513 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7514 | (void) shared_secret; |
| 7515 | (void) shared_secret_size; |
| 7516 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7517 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7518 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7519 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7520 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7521 | /** Internal function for raw key agreement |
| 7522 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 7523 | * to the driver's implementation if a driver is present. |
| 7524 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7525 | * (psa_key_agreement_raw_builtin). |
| 7526 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7527 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 7528 | psa_key_slot_t *private_key, |
| 7529 | const uint8_t *peer_key, |
| 7530 | size_t peer_key_length, |
| 7531 | uint8_t *shared_secret, |
| 7532 | size_t shared_secret_size, |
| 7533 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7534 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7535 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 7536 | return PSA_ERROR_NOT_SUPPORTED; |
| 7537 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7538 | |
| 7539 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7540 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7541 | }; |
| 7542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7543 | return psa_driver_wrapper_key_agreement(&attributes, |
| 7544 | private_key->key.data, |
| 7545 | private_key->key.bytes, alg, |
| 7546 | peer_key, peer_key_length, |
| 7547 | shared_secret, |
| 7548 | shared_secret_size, |
| 7549 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7552 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7553 | * to potentially free embedded data structures and wipe confidential data. |
| 7554 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7555 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 7556 | psa_key_derivation_step_t step, |
| 7557 | psa_key_slot_t *private_key, |
| 7558 | const uint8_t *peer_key, |
| 7559 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7560 | { |
| 7561 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 7562 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7563 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7564 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7565 | |
| 7566 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 7567 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7568 | status = psa_key_agreement_raw_internal(ka_alg, |
| 7569 | private_key, |
| 7570 | peer_key, peer_key_length, |
| 7571 | shared_secret, |
| 7572 | sizeof(shared_secret), |
| 7573 | &shared_secret_length); |
| 7574 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7575 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7576 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7577 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 7578 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7579 | * the shared secret. A shared secret is permitted wherever a key |
| 7580 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7581 | status = psa_key_derivation_input_internal(operation, step, |
| 7582 | PSA_KEY_TYPE_DERIVE, |
| 7583 | shared_secret, |
| 7584 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7585 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7586 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 7587 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7588 | } |
| 7589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 7591 | psa_key_derivation_step_t step, |
| 7592 | mbedtls_svc_key_id_t private_key, |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7593 | const uint8_t *peer_key_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7594 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7595 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7596 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7597 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 7598 | psa_key_slot_t *slot; |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7599 | LOCAL_INPUT_DECLARE(peer_key_external, peer_key); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7601 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 7602 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7603 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7604 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7605 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7606 | if (status != PSA_SUCCESS) { |
| 7607 | return status; |
| 7608 | } |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7609 | |
Thomas Daubney | 692fb3c | 2024-03-12 16:20:41 +0000 | [diff] [blame] | 7610 | LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7611 | status = psa_key_agreement_internal(operation, step, |
| 7612 | slot, |
| 7613 | peer_key, peer_key_length); |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7614 | |
Thomas Daubney | 50f58fc | 2024-02-15 14:24:03 +0000 | [diff] [blame] | 7615 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7616 | exit: |
Thomas Daubney | 50f58fc | 2024-02-15 14:24:03 +0000 | [diff] [blame] | 7617 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | if (status != PSA_SUCCESS) { |
| 7619 | psa_key_derivation_abort(operation); |
| 7620 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7621 | /* If a private key has been added as SECRET, we allow the derived |
| 7622 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7623 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7624 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7625 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7626 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7627 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7628 | unlock_status = psa_unregister_read(slot); |
Thomas Daubney | 9739ac0 | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 7629 | LOCAL_INPUT_FREE(peer_key_external, peer_key); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7630 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 7631 | } |
| 7632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7633 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 7634 | mbedtls_svc_key_id_t private_key, |
Thomas Daubney | 81899ab | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 7635 | const uint8_t *peer_key_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7636 | size_t peer_key_length, |
Thomas Daubney | 81899ab | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 7637 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7638 | size_t output_size, |
| 7639 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7640 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7641 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7642 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7643 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7644 | size_t expected_length; |
Thomas Daubney | 81899ab | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 7645 | LOCAL_INPUT_DECLARE(peer_key_external, peer_key); |
| 7646 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Thomas Daubney | 89d8c2a | 2024-02-21 11:16:16 +0000 | [diff] [blame] | 7647 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7649 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7650 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 7651 | goto exit; |
| 7652 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7653 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7654 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 7655 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7656 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7657 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7658 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7659 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 7660 | * for the output size. The PSA specification only guarantees that this |
| 7661 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 7662 | * but it might be nice to allow smaller buffers if the output fits. |
| 7663 | * At the time of writing this comment, with only ECDH implemented, |
| 7664 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 7665 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 7666 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7667 | expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7668 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 7669 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7670 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 7671 | goto exit; |
| 7672 | } |
| 7673 | |
Thomas Daubney | 81899ab | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 7674 | LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7675 | status = psa_key_agreement_raw_internal(alg, slot, |
| 7676 | peer_key, peer_key_length, |
| 7677 | output, output_size, |
| 7678 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7679 | |
| 7680 | exit: |
Thomas Daubney | 5390aca | 2024-02-22 11:06:04 +0000 | [diff] [blame] | 7681 | /* Check for successful allocation of output, |
| 7682 | * with an unsuccessful status. */ |
Thomas Daubney | 0576a6a | 2024-02-21 15:15:00 +0000 | [diff] [blame] | 7683 | if (output != NULL && status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7684 | /* If an error happens and is not handled properly, the output |
| 7685 | * may be used as a key to protect sensitive data. Arrange for such |
| 7686 | * a key to be random, which is likely to result in decryption or |
| 7687 | * verification errors. This is better than filling the buffer with |
| 7688 | * some constant data such as zeros, which would result in the data |
| 7689 | * being protected with a reproducible, easily knowable key. |
| 7690 | */ |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7691 | psa_generate_random_internal(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7692 | *output_length = output_size; |
| 7693 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7694 | |
Thomas Daubney | 5390aca | 2024-02-22 11:06:04 +0000 | [diff] [blame] | 7695 | if (output == NULL) { |
Thomas Daubney | 89d8c2a | 2024-02-21 11:16:16 +0000 | [diff] [blame] | 7696 | /* output allocation failed. */ |
| 7697 | *output_length = 0; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7698 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7699 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7700 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7701 | |
Thomas Daubney | 81899ab | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 7702 | LOCAL_INPUT_FREE(peer_key_external, peer_key); |
| 7703 | LOCAL_OUTPUT_FREE(output_external, output); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7704 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7705 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 7706 | |
| 7707 | |
| 7708 | /****************************************************************/ |
| 7709 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 7710 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7711 | |
Gilles Peskine | 672a771 | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 7712 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 7713 | #include "entropy_poll.h" |
| 7714 | #endif |
| 7715 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7716 | /** Initialize the PSA random generator. |
| 7717 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7718 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7719 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7720 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7721 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7722 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7723 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7724 | /* Set default configuration if |
| 7725 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7726 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7727 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7728 | } |
| 7729 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7730 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7731 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7733 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7734 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 7735 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 7736 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 7737 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7738 | mbedtls_entropy_add_source(&rng->entropy, |
| 7739 | mbedtls_nv_seed_poll, NULL, |
| 7740 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 7741 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7742 | #endif |
| 7743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7744 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7745 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | /** Deinitialize the PSA random generator. |
| 7749 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7750 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7751 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7752 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7753 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7754 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7755 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 7756 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7757 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | /** Seed the PSA random generator. |
| 7761 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7762 | static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7763 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7764 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7765 | /* Do nothing: the external RNG seeds itself. */ |
| 7766 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7767 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7768 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7769 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7770 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 7771 | drbg_seed, sizeof(drbg_seed) - 1); |
| 7772 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7773 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7774 | } |
| 7775 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7776 | psa_status_t psa_generate_random(uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7777 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7778 | { |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7779 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7780 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7781 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 7782 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7783 | |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7784 | status = psa_generate_random_internal(output, output_size); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7785 | |
David Horstmann | e097bbd | 2024-02-28 14:17:10 +0000 | [diff] [blame] | 7786 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7787 | exit: |
David Horstmann | e097bbd | 2024-02-28 14:17:10 +0000 | [diff] [blame] | 7788 | #endif |
David Horstmann | 6e99bb2 | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 7789 | LOCAL_OUTPUT_FREE(output_external, output); |
| 7790 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7791 | } |
| 7792 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7793 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7794 | * |
| 7795 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 7796 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 7797 | * PSA API doesn't support passing an explicit state. |
| 7798 | * |
| 7799 | * In the non-external case, psa_generate_random() calls an |
| 7800 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 7801 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 7802 | * instead of doing this back-and-forth between the PSA API and the |
| 7803 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 7804 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7805 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7806 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7807 | int mbedtls_psa_get_random(void *p_rng, |
| 7808 | unsigned char *output, |
| 7809 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7810 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7811 | /* This function takes a pointer to the RNG state because that's what |
| 7812 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 7813 | * its own state internally and doesn't let the caller access that state. |
| 7814 | * So we just ignore the state parameter, and in practice we'll pass |
| 7815 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7816 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7817 | psa_status_t status = psa_generate_random(output, output_size); |
| 7818 | if (status == PSA_SUCCESS) { |
| 7819 | return 0; |
| 7820 | } else { |
| 7821 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 7822 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7823 | } |
| 7824 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7825 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7826 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7827 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 7828 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7829 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7830 | if (global_data.initialized) { |
| 7831 | return PSA_ERROR_NOT_PERMITTED; |
| 7832 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7834 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 7835 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 7836 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 7837 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7838 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7840 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7841 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7842 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7843 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7844 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7845 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7846 | * \param type The key type |
| 7847 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7848 | * |
| 7849 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7850 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7851 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 7852 | * The size in bits of the key is not valid. |
| 7853 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 7854 | * The type and/or the size in bits of the key or the combination of |
| 7855 | * the two is not supported. |
| 7856 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7857 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7859 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 7860 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7862 | if (key_type_is_raw_bytes(type)) { |
| 7863 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 7864 | if (status != PSA_SUCCESS) { |
| 7865 | return status; |
| 7866 | } |
| 7867 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7868 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7869 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7870 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 7871 | return PSA_ERROR_NOT_SUPPORTED; |
| 7872 | } |
Waleed Elmelegy | d7bdbbe | 2023-07-20 16:26:58 +0000 | [diff] [blame] | 7873 | if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) { |
Waleed Elmelegy | ab57071 | 2023-07-05 16:40:58 +0000 | [diff] [blame] | 7874 | return PSA_ERROR_NOT_SUPPORTED; |
| 7875 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7876 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7877 | /* Accept only byte-aligned keys, for the same reasons as |
| 7878 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7879 | if (bits % 8 != 0) { |
| 7880 | return PSA_ERROR_NOT_SUPPORTED; |
| 7881 | } |
| 7882 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7883 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7884 | |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7885 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7886 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 7887 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7888 | return PSA_SUCCESS; |
| 7889 | } else |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7890 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7891 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7892 | #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7893 | if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 7894 | if (psa_is_dh_key_size_valid(bits) == 0) { |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7895 | return PSA_ERROR_NOT_SUPPORTED; |
| 7896 | } |
| 7897 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7898 | #endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7899 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7901 | } |
| 7902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7903 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7904 | } |
| 7905 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 7906 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7907 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7909 | { |
| 7910 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7911 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7913 | if ((attributes->domain_parameters == NULL) && |
| 7914 | (attributes->domain_parameters_size != 0)) { |
| 7915 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7916 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | if (key_type_is_raw_bytes(type)) { |
| 7919 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 7920 | if (status != PSA_SUCCESS) { |
| 7921 | return status; |
| 7922 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7923 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 7924 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | if (type == PSA_KEY_TYPE_DES) { |
| 7926 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 7927 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 7928 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7929 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7930 | |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7931 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7932 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 7933 | return mbedtls_psa_rsa_generate_key(attributes, |
| 7934 | key_buffer, |
| 7935 | key_buffer_size, |
| 7936 | key_buffer_length); |
| 7937 | } else |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7938 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7939 | |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7940 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7941 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7942 | return mbedtls_psa_ecp_generate_key(attributes, |
| 7943 | key_buffer, |
| 7944 | key_buffer_size, |
| 7945 | key_buffer_length); |
| 7946 | } else |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7947 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7948 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7949 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7950 | if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7951 | return mbedtls_psa_ffdh_generate_key(attributes, |
| 7952 | key_buffer, |
| 7953 | key_buffer_size, |
| 7954 | key_buffer_length); |
| 7955 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7956 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7957 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7958 | (void) key_buffer_length; |
| 7959 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7960 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7963 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7965 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 7966 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7967 | { |
| 7968 | psa_status_t status; |
| 7969 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 7970 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7971 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7972 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 7973 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7974 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7975 | /* Reject any attempt to create a zero-length key so that we don't |
| 7976 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7977 | if (psa_get_key_bits(attributes) == 0) { |
| 7978 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7979 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7980 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7981 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7982 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 7983 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7984 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7986 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 7987 | &slot, &driver); |
| 7988 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7989 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7990 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7991 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7992 | /* In the case of a transparent key or an opaque key stored in local |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 7993 | * storage ( thus not in the case of generating a key in a secure element |
| 7994 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 7995 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7996 | if (slot->key.data == NULL) { |
| 7997 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 7998 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7999 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8000 | attributes->core.type, attributes->core.bits); |
| 8001 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 8002 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8003 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 8004 | |
| 8005 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8006 | attributes->core.type, |
| 8007 | attributes->core.bits); |
| 8008 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 8009 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8010 | attributes, &key_buffer_size); |
| 8011 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 8012 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8013 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 8014 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 8015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8016 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 8017 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 8018 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8019 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 8020 | } |
| 8021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8022 | status = psa_driver_wrapper_generate_key(attributes, |
| 8023 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 8024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8025 | if (status != PSA_SUCCESS) { |
| 8026 | psa_remove_key_data_from_memory(slot); |
| 8027 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 8028 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 8029 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8030 | if (status == PSA_SUCCESS) { |
| 8031 | status = psa_finish_key_creation(slot, driver, key); |
| 8032 | } |
| 8033 | if (status != PSA_SUCCESS) { |
| 8034 | psa_fail_key_creation(slot, driver); |
| 8035 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 8036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8037 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8038 | } |
| 8039 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8040 | /****************************************************************/ |
| 8041 | /* Module setup */ |
| 8042 | /****************************************************************/ |
| 8043 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 8044 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 8045 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8046 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 8047 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 8048 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8049 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 8050 | return PSA_ERROR_BAD_STATE; |
| 8051 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 8052 | global_data.rng.entropy_init = entropy_init; |
| 8053 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8054 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 8055 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 8056 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 8057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8058 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8059 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8060 | psa_wipe_all_key_slots(); |
| 8061 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 8062 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 8063 | } |
| 8064 | /* Wipe all remaining data, including configuration. |
| 8065 | * In particular, this sets all state indicator to the value |
| 8066 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8067 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 8068 | |
| 8069 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8070 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8071 | } |
| 8072 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8073 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 8074 | /** Recover a transaction that was interrupted by a power failure. |
| 8075 | * |
| 8076 | * This function is called during initialization, before psa_crypto_init() |
| 8077 | * returns. If this function returns a failure status, the initialization |
| 8078 | * fails. |
| 8079 | */ |
| 8080 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8081 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8082 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8083 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8084 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 8085 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8086 | /* TODO - fall through to the failure case until this |
| 8087 | * is implemented. |
| 8088 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 8089 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8090 | default: |
| 8091 | /* We found an unsupported transaction in the storage. |
| 8092 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8094 | } |
| 8095 | } |
| 8096 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 8097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8098 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8099 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 8100 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8101 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 8102 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | if (global_data.initialized != 0) { |
| 8104 | return PSA_SUCCESS; |
| 8105 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8106 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 8107 | /* Init drivers */ |
| 8108 | status = psa_driver_wrapper_init(); |
| 8109 | if (status != PSA_SUCCESS) { |
| 8110 | goto exit; |
| 8111 | } |
| 8112 | global_data.drivers_initialized = 1; |
| 8113 | |
Valerio Setti | 402cfba | 2023-11-13 10:24:32 +0100 | [diff] [blame] | 8114 | status = psa_initialize_key_slots(); |
| 8115 | if (status != PSA_SUCCESS) { |
| 8116 | goto exit; |
| 8117 | } |
| 8118 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 8119 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8120 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 8121 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 8123 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8124 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8125 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 8126 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8127 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 8128 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8129 | status = psa_crypto_load_transaction(); |
| 8130 | if (status == PSA_SUCCESS) { |
| 8131 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 8132 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 8133 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8134 | } |
| 8135 | status = psa_crypto_stop_transaction(); |
| 8136 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 8137 | /* There's no transaction to complete. It's all good. */ |
| 8138 | status = PSA_SUCCESS; |
| 8139 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 8140 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 8141 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 8142 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8143 | global_data.initialized = 1; |
| 8144 | |
| 8145 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | if (status != PSA_SUCCESS) { |
| 8147 | mbedtls_psa_crypto_free(); |
| 8148 | } |
| 8149 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8150 | } |
| 8151 | |
Yanray Wang | ffc3c48 | 2023-07-11 12:01:04 +0800 | [diff] [blame] | 8152 | #if defined(PSA_WANT_ALG_SOME_PAKE) |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 8153 | psa_status_t psa_crypto_driver_pake_get_password_len( |
| 8154 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8155 | size_t *password_len) |
| 8156 | { |
| 8157 | if (inputs->password_len == 0) { |
| 8158 | return PSA_ERROR_BAD_STATE; |
| 8159 | } |
| 8160 | |
| 8161 | *password_len = inputs->password_len; |
| 8162 | |
| 8163 | return PSA_SUCCESS; |
| 8164 | } |
| 8165 | |
| 8166 | psa_status_t psa_crypto_driver_pake_get_password( |
| 8167 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8168 | uint8_t *buffer, size_t buffer_size, size_t *buffer_length) |
| 8169 | { |
| 8170 | if (inputs->password_len == 0) { |
| 8171 | return PSA_ERROR_BAD_STATE; |
| 8172 | } |
| 8173 | |
| 8174 | if (buffer_size < inputs->password_len) { |
| 8175 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 8176 | } |
| 8177 | |
| 8178 | memcpy(buffer, inputs->password, inputs->password_len); |
| 8179 | *buffer_length = inputs->password_len; |
| 8180 | |
| 8181 | return PSA_SUCCESS; |
| 8182 | } |
| 8183 | |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8184 | psa_status_t psa_crypto_driver_pake_get_user_len( |
| 8185 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8186 | size_t *user_len) |
| 8187 | { |
| 8188 | if (inputs->user_len == 0) { |
| 8189 | return PSA_ERROR_BAD_STATE; |
| 8190 | } |
| 8191 | |
| 8192 | *user_len = inputs->user_len; |
| 8193 | |
| 8194 | return PSA_SUCCESS; |
| 8195 | } |
| 8196 | |
| 8197 | psa_status_t psa_crypto_driver_pake_get_user( |
| 8198 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8199 | uint8_t *user_id, size_t user_id_size, size_t *user_id_len) |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8200 | { |
| 8201 | if (inputs->user_len == 0) { |
| 8202 | return PSA_ERROR_BAD_STATE; |
| 8203 | } |
| 8204 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8205 | if (user_id_size < inputs->user_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8206 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 8207 | } |
| 8208 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8209 | memcpy(user_id, inputs->user, inputs->user_len); |
| 8210 | *user_id_len = inputs->user_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8211 | |
| 8212 | return PSA_SUCCESS; |
| 8213 | } |
| 8214 | |
| 8215 | psa_status_t psa_crypto_driver_pake_get_peer_len( |
| 8216 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8217 | size_t *peer_len) |
| 8218 | { |
| 8219 | if (inputs->peer_len == 0) { |
| 8220 | return PSA_ERROR_BAD_STATE; |
| 8221 | } |
| 8222 | |
| 8223 | *peer_len = inputs->peer_len; |
| 8224 | |
| 8225 | return PSA_SUCCESS; |
| 8226 | } |
| 8227 | |
| 8228 | psa_status_t psa_crypto_driver_pake_get_peer( |
| 8229 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8230 | uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length) |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8231 | { |
| 8232 | if (inputs->peer_len == 0) { |
| 8233 | return PSA_ERROR_BAD_STATE; |
| 8234 | } |
| 8235 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8236 | if (peer_id_size < inputs->peer_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8237 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 8238 | } |
| 8239 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8240 | memcpy(peer_id, inputs->peer, inputs->peer_len); |
| 8241 | *peer_id_length = inputs->peer_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8242 | |
| 8243 | return PSA_SUCCESS; |
| 8244 | } |
| 8245 | |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 8246 | psa_status_t psa_crypto_driver_pake_get_cipher_suite( |
| 8247 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8248 | psa_pake_cipher_suite_t *cipher_suite) |
| 8249 | { |
| 8250 | if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) { |
| 8251 | return PSA_ERROR_BAD_STATE; |
| 8252 | } |
| 8253 | |
| 8254 | *cipher_suite = inputs->cipher_suite; |
| 8255 | |
| 8256 | return PSA_SUCCESS; |
| 8257 | } |
| 8258 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8259 | psa_status_t psa_pake_setup( |
| 8260 | psa_pake_operation_t *operation, |
| 8261 | const psa_pake_cipher_suite_t *cipher_suite) |
| 8262 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8263 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8264 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 8265 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8266 | status = PSA_ERROR_BAD_STATE; |
| 8267 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8268 | } |
| 8269 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8270 | if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 || |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8271 | PSA_ALG_IS_HASH(cipher_suite->hash) == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8272 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8273 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8274 | } |
| 8275 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8276 | memset(&operation->data.inputs, 0, sizeof(operation->data.inputs)); |
| 8277 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8278 | operation->alg = cipher_suite->algorithm; |
Przemek Stekiel | 656b259 | 2023-03-22 13:15:33 +0100 | [diff] [blame] | 8279 | operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type, |
| 8280 | cipher_suite->family, cipher_suite->bits); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8281 | operation->data.inputs.cipher_suite = *cipher_suite; |
| 8282 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8283 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8284 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8285 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | dde6a91 | 2023-01-26 08:46:37 +0100 | [diff] [blame] | 8286 | &operation->computation_stage.jpake; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8287 | |
David Horstmann | 16f0151 | 2023-06-14 17:21:07 +0100 | [diff] [blame] | 8288 | memset(computation_stage, 0, sizeof(*computation_stage)); |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8289 | computation_stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8290 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8291 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8292 | { |
| 8293 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8294 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8295 | } |
| 8296 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 8297 | operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS; |
| 8298 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8299 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8300 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8301 | psa_pake_abort(operation); |
| 8302 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8303 | } |
| 8304 | |
| 8305 | psa_status_t psa_pake_set_password_key( |
| 8306 | psa_pake_operation_t *operation, |
| 8307 | mbedtls_svc_key_id_t password) |
| 8308 | { |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8309 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8310 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8311 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8312 | psa_key_attributes_t attributes; |
| 8313 | psa_key_type_t type; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8314 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8315 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8316 | status = PSA_ERROR_BAD_STATE; |
| 8317 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8318 | } |
| 8319 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8320 | status = psa_get_and_lock_key_slot_with_policy(password, &slot, |
| 8321 | PSA_KEY_USAGE_DERIVE, |
Przemek Stekiel | d5d28a2 | 2023-01-26 10:46:05 +0100 | [diff] [blame] | 8322 | operation->alg); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8323 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8324 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8325 | } |
| 8326 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8327 | attributes = (psa_key_attributes_t) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8328 | .core = slot->attr |
| 8329 | }; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8330 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8331 | type = psa_get_key_type(&attributes); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8332 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8333 | if (type != PSA_KEY_TYPE_PASSWORD && |
| 8334 | type != PSA_KEY_TYPE_PASSWORD_HASH) { |
| 8335 | status = PSA_ERROR_INVALID_ARGUMENT; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8336 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8337 | } |
| 8338 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8339 | operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes); |
| 8340 | if (operation->data.inputs.password == NULL) { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8341 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8342 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8343 | } |
| 8344 | |
| 8345 | memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes); |
| 8346 | operation->data.inputs.password_len = slot->key.bytes; |
Przemek Stekiel | 9dd2440 | 2023-01-26 15:06:09 +0100 | [diff] [blame] | 8347 | operation->data.inputs.attributes = attributes; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8348 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8349 | if (status != PSA_SUCCESS) { |
| 8350 | psa_pake_abort(operation); |
| 8351 | } |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 8352 | unlock_status = psa_unregister_read(slot); |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8353 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8354 | } |
| 8355 | |
| 8356 | psa_status_t psa_pake_set_user( |
| 8357 | psa_pake_operation_t *operation, |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8358 | const uint8_t *user_id_external, |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8359 | size_t user_id_len) |
| 8360 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8361 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8362 | LOCAL_INPUT_DECLARE(user_id_external, user_id); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8363 | |
| 8364 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8365 | status = PSA_ERROR_BAD_STATE; |
| 8366 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8367 | } |
| 8368 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8369 | if (user_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8370 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8371 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8372 | } |
| 8373 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8374 | if (operation->data.inputs.user_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8375 | status = PSA_ERROR_BAD_STATE; |
| 8376 | goto exit; |
| 8377 | } |
| 8378 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8379 | operation->data.inputs.user = mbedtls_calloc(1, user_id_len); |
| 8380 | if (operation->data.inputs.user == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8381 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8382 | goto exit; |
| 8383 | } |
| 8384 | |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8385 | LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id); |
| 8386 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8387 | memcpy(operation->data.inputs.user, user_id, user_id_len); |
| 8388 | operation->data.inputs.user_len = user_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8389 | |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8390 | status = PSA_SUCCESS; |
| 8391 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8392 | exit: |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8393 | LOCAL_INPUT_FREE(user_id_external, user_id); |
| 8394 | if (status != PSA_SUCCESS) { |
| 8395 | psa_pake_abort(operation); |
| 8396 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8397 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8398 | } |
| 8399 | |
| 8400 | psa_status_t psa_pake_set_peer( |
| 8401 | psa_pake_operation_t *operation, |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8402 | const uint8_t *peer_id_external, |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8403 | size_t peer_id_len) |
| 8404 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8405 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8406 | LOCAL_INPUT_DECLARE(peer_id_external, peer_id); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8407 | |
| 8408 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8409 | status = PSA_ERROR_BAD_STATE; |
| 8410 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8411 | } |
| 8412 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8413 | if (peer_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8414 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8415 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8416 | } |
| 8417 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8418 | if (operation->data.inputs.peer_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8419 | status = PSA_ERROR_BAD_STATE; |
| 8420 | goto exit; |
| 8421 | } |
| 8422 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8423 | operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len); |
| 8424 | if (operation->data.inputs.peer == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8425 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8426 | goto exit; |
| 8427 | } |
| 8428 | |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8429 | LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id); |
| 8430 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8431 | memcpy(operation->data.inputs.peer, peer_id, peer_id_len); |
| 8432 | operation->data.inputs.peer_len = peer_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8433 | |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8434 | status = PSA_SUCCESS; |
| 8435 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8436 | exit: |
David Horstmann | 4f534ae | 2024-01-22 17:35:59 +0000 | [diff] [blame] | 8437 | LOCAL_INPUT_FREE(peer_id_external, peer_id); |
| 8438 | if (status != PSA_SUCCESS) { |
| 8439 | psa_pake_abort(operation); |
| 8440 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8441 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8442 | } |
| 8443 | |
| 8444 | psa_status_t psa_pake_set_role( |
| 8445 | psa_pake_operation_t *operation, |
| 8446 | psa_pake_role_t role) |
| 8447 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8448 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8449 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8450 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8451 | status = PSA_ERROR_BAD_STATE; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8452 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8453 | } |
| 8454 | |
Przemek Stekiel | 09104b8 | 2023-03-06 14:11:51 +0100 | [diff] [blame] | 8455 | switch (operation->alg) { |
| 8456 | #if defined(PSA_WANT_ALG_JPAKE) |
| 8457 | case PSA_ALG_JPAKE: |
| 8458 | if (role == PSA_PAKE_ROLE_NONE) { |
| 8459 | return PSA_SUCCESS; |
| 8460 | } |
| 8461 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8462 | break; |
| 8463 | #endif |
| 8464 | default: |
| 8465 | (void) role; |
| 8466 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8467 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8468 | } |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8469 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8470 | psa_pake_abort(operation); |
| 8471 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8472 | } |
| 8473 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8474 | /* Auxiliary function to convert core computation stage to single driver step. */ |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8475 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8476 | static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step( |
Przemek Stekiel | dde6a91 | 2023-01-26 08:46:37 +0100 | [diff] [blame] | 8477 | psa_jpake_computation_stage_t *stage) |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8478 | { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8479 | psa_crypto_driver_pake_step_t key_share_step; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8480 | if (stage->round == PSA_JPAKE_FIRST) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8481 | int is_x1; |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8482 | |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8483 | if (stage->io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8484 | is_x1 = (stage->outputs < 1); |
| 8485 | } else { |
| 8486 | is_x1 = (stage->inputs < 1); |
| 8487 | } |
| 8488 | |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8489 | key_share_step = is_x1 ? |
| 8490 | PSA_JPAKE_X1_STEP_KEY_SHARE : |
| 8491 | PSA_JPAKE_X2_STEP_KEY_SHARE; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8492 | } else if (stage->round == PSA_JPAKE_SECOND) { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8493 | key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ? |
| 8494 | PSA_JPAKE_X2S_STEP_KEY_SHARE : |
| 8495 | PSA_JPAKE_X4S_STEP_KEY_SHARE; |
| 8496 | } else { |
| 8497 | return PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8498 | } |
Agathiyan Bragadeesh | 387bfa5 | 2023-07-17 17:01:33 +0100 | [diff] [blame] | 8499 | return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE); |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8500 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8501 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8502 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8503 | static psa_status_t psa_pake_complete_inputs( |
| 8504 | psa_pake_operation_t *operation) |
| 8505 | { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8506 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8507 | /* Create copy of the inputs on stack as inputs share memory |
| 8508 | with the driver context which will be setup by the driver. */ |
| 8509 | psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8510 | |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8511 | if (inputs.password_len == 0) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8512 | return PSA_ERROR_BAD_STATE; |
| 8513 | } |
| 8514 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8515 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8516 | if (inputs.user_len == 0 || inputs.peer_len == 0) { |
| 8517 | return PSA_ERROR_BAD_STATE; |
| 8518 | } |
Przemek Stekiel | dff21d3 | 2023-02-14 20:09:10 +0100 | [diff] [blame] | 8519 | } |
| 8520 | |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8521 | /* Clear driver context */ |
| 8522 | mbedtls_platform_zeroize(&operation->data, sizeof(operation->data)); |
| 8523 | |
| 8524 | status = psa_driver_wrapper_pake_setup(operation, &inputs); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8525 | |
| 8526 | /* Driver is responsible for creating its own copy of the password. */ |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8527 | mbedtls_zeroize_and_free(inputs.password, inputs.password_len); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8528 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8529 | /* User and peer are translated to role. */ |
| 8530 | mbedtls_free(inputs.user); |
| 8531 | mbedtls_free(inputs.peer); |
| 8532 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8533 | if (status == PSA_SUCCESS) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8534 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8535 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | d93de32 | 2023-02-24 08:39:04 +0100 | [diff] [blame] | 8536 | operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8537 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8538 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8539 | { |
| 8540 | status = PSA_ERROR_NOT_SUPPORTED; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8541 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8542 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8543 | return status; |
| 8544 | } |
| 8545 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8546 | #if defined(PSA_WANT_ALG_JPAKE) |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8547 | static psa_status_t psa_jpake_prologue( |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8548 | psa_pake_operation_t *operation, |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8549 | psa_pake_step_t step, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8550 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8551 | { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8552 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
| 8553 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 8554 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 8555 | return PSA_ERROR_INVALID_ARGUMENT; |
| 8556 | } |
| 8557 | |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8558 | psa_jpake_computation_stage_t *computation_stage = |
| 8559 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8560 | |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8561 | if (computation_stage->round != PSA_JPAKE_FIRST && |
| 8562 | computation_stage->round != PSA_JPAKE_SECOND) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8563 | return PSA_ERROR_BAD_STATE; |
| 8564 | } |
| 8565 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8566 | /* Check that the step we are given is the one we were expecting */ |
| 8567 | if (step != computation_stage->step) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8568 | return PSA_ERROR_BAD_STATE; |
| 8569 | } |
| 8570 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8571 | if (step == PSA_PAKE_STEP_KEY_SHARE && |
| 8572 | computation_stage->inputs == 0 && |
| 8573 | computation_stage->outputs == 0) { |
| 8574 | /* Start of the round, so function decides whether we are inputting |
| 8575 | * or outputting */ |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8576 | computation_stage->io_mode = io_mode; |
| 8577 | } else if (computation_stage->io_mode != io_mode) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8578 | /* Middle of the round so the mode we are in must match the function |
| 8579 | * called by the user */ |
| 8580 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8581 | } |
| 8582 | |
| 8583 | return PSA_SUCCESS; |
| 8584 | } |
| 8585 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8586 | static psa_status_t psa_jpake_epilogue( |
| 8587 | psa_pake_operation_t *operation, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8588 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8589 | { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8590 | psa_jpake_computation_stage_t *stage = |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8591 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8592 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8593 | if (stage->step == PSA_PAKE_STEP_ZK_PROOF) { |
| 8594 | /* End of an input/output */ |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8595 | if (io_mode == PSA_JPAKE_INPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8596 | stage->inputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8597 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8598 | stage->io_mode = PSA_JPAKE_OUTPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8599 | } |
| 8600 | } |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8601 | if (io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8602 | stage->outputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8603 | if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8604 | stage->io_mode = PSA_JPAKE_INPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8605 | } |
| 8606 | } |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8607 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) && |
| 8608 | stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8609 | /* End of a round, move to the next round */ |
| 8610 | stage->inputs = 0; |
| 8611 | stage->outputs = 0; |
| 8612 | stage->round++; |
| 8613 | } |
| 8614 | stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8615 | } else { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8616 | stage->step++; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8617 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8618 | return PSA_SUCCESS; |
| 8619 | } |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8620 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8621 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8622 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8623 | psa_status_t psa_pake_output( |
| 8624 | psa_pake_operation_t *operation, |
| 8625 | psa_pake_step_t step, |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8626 | uint8_t *output_external, |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8627 | size_t output_size, |
| 8628 | size_t *output_length) |
| 8629 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8630 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8631 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8632 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8633 | *output_length = 0; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8634 | |
| 8635 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8636 | status = psa_pake_complete_inputs(operation); |
| 8637 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8638 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8639 | } |
| 8640 | } |
| 8641 | |
| 8642 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8643 | status = PSA_ERROR_BAD_STATE; |
| 8644 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8645 | } |
| 8646 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8647 | if (output_size == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8648 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8649 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8650 | } |
| 8651 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8652 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8653 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8654 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8655 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8656 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8657 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8658 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8659 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8660 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8661 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8662 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8663 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8664 | (void) step; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8665 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8666 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8667 | } |
| 8668 | |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8669 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 8670 | |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8671 | status = psa_driver_wrapper_pake_output(operation, driver_step, |
| 8672 | output, output_size, output_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8673 | |
| 8674 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8675 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8679 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8680 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8681 | status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8682 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8683 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8684 | } |
| 8685 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8686 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8687 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8688 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8689 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8690 | } |
| 8691 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8692 | exit: |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8693 | LOCAL_OUTPUT_FREE(output_external, output); |
| 8694 | if (status != PSA_SUCCESS) { |
| 8695 | psa_pake_abort(operation); |
| 8696 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8697 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8698 | } |
| 8699 | |
| 8700 | psa_status_t psa_pake_input( |
| 8701 | psa_pake_operation_t *operation, |
| 8702 | psa_pake_step_t step, |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8703 | const uint8_t *input_external, |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8704 | size_t input_length) |
| 8705 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8706 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8707 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8708 | const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg, |
| 8709 | operation->primitive, |
| 8710 | step); |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8711 | LOCAL_INPUT_DECLARE(input_external, input); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8712 | |
| 8713 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8714 | status = psa_pake_complete_inputs(operation); |
| 8715 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8716 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8717 | } |
| 8718 | } |
| 8719 | |
| 8720 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8721 | status = PSA_ERROR_BAD_STATE; |
| 8722 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8723 | } |
| 8724 | |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8725 | if (input_length == 0 || input_length > max_input_length) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8726 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8727 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8728 | } |
| 8729 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8730 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8731 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8732 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8733 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8734 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8735 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8736 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8737 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8738 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8739 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8740 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8741 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8742 | (void) step; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8743 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8744 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8745 | } |
| 8746 | |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8747 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8748 | status = psa_driver_wrapper_pake_input(operation, driver_step, |
| 8749 | input, input_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8750 | |
| 8751 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8752 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8753 | } |
| 8754 | |
| 8755 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8756 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8757 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8758 | status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8759 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8760 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8761 | } |
| 8762 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8763 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8764 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8765 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8766 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8767 | } |
| 8768 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8769 | exit: |
David Horstmann | c75639d | 2024-01-22 17:56:39 +0000 | [diff] [blame] | 8770 | LOCAL_INPUT_FREE(input_external, input); |
| 8771 | if (status != PSA_SUCCESS) { |
| 8772 | psa_pake_abort(operation); |
| 8773 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8774 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8775 | } |
| 8776 | |
| 8777 | psa_status_t psa_pake_get_implicit_key( |
| 8778 | psa_pake_operation_t *operation, |
| 8779 | psa_key_derivation_operation_t *output) |
| 8780 | { |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8781 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8782 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8783 | uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8784 | size_t shared_key_len = 0; |
| 8785 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8786 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8787 | status = PSA_ERROR_BAD_STATE; |
| 8788 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8789 | } |
| 8790 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8791 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8792 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8793 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | 083745e | 2023-02-23 17:28:23 +0100 | [diff] [blame] | 8794 | &operation->computation_stage.jpake; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8795 | if (computation_stage->round != PSA_JPAKE_FINISHED) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8796 | status = PSA_ERROR_BAD_STATE; |
| 8797 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8798 | } |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8799 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8800 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8801 | { |
| 8802 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8803 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8806 | status = psa_driver_wrapper_pake_get_implicit_key(operation, |
| 8807 | shared_key, |
Przemek Stekiel | 6b64862 | 2023-02-19 22:55:33 +0100 | [diff] [blame] | 8808 | sizeof(shared_key), |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8809 | &shared_key_len); |
| 8810 | |
| 8811 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8812 | goto exit; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8813 | } |
| 8814 | |
| 8815 | status = psa_key_derivation_input_bytes(output, |
| 8816 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8817 | shared_key, |
| 8818 | shared_key_len); |
| 8819 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8820 | mbedtls_platform_zeroize(shared_key, sizeof(shared_key)); |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8821 | exit: |
| 8822 | abort_status = psa_pake_abort(operation); |
| 8823 | return status == PSA_SUCCESS ? abort_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8824 | } |
| 8825 | |
| 8826 | psa_status_t psa_pake_abort( |
| 8827 | psa_pake_operation_t *operation) |
| 8828 | { |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8829 | psa_status_t status = PSA_SUCCESS; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8830 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8831 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8832 | status = psa_driver_wrapper_pake_abort(operation); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8833 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8834 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8835 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
| 8836 | if (operation->data.inputs.password != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8837 | mbedtls_zeroize_and_free(operation->data.inputs.password, |
Przemek Stekiel | aa18342 | 2023-03-06 14:21:44 +0100 | [diff] [blame] | 8838 | operation->data.inputs.password_len); |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8839 | } |
| 8840 | if (operation->data.inputs.user != NULL) { |
| 8841 | mbedtls_free(operation->data.inputs.user); |
| 8842 | } |
| 8843 | if (operation->data.inputs.peer != NULL) { |
| 8844 | mbedtls_free(operation->data.inputs.peer); |
| 8845 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8846 | } |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8847 | memset(operation, 0, sizeof(psa_pake_operation_t)); |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8848 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8849 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8850 | } |
Tom Cosgrove | 6d62fac | 2023-05-10 14:40:05 +0100 | [diff] [blame] | 8851 | #endif /* PSA_WANT_ALG_SOME_PAKE */ |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8852 | |
David Horstmann | 00d7a04 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 8853 | /* Memory copying test hooks. These are called before input copy, after input |
| 8854 | * copy, before output copy and after output copy, respectively. |
| 8855 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 8856 | * while they are copied. */ |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8857 | #if defined(MBEDTLS_TEST_HOOKS) |
| 8858 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8859 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8860 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8861 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8862 | #endif |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8863 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8864 | /** Copy from an input buffer to a local copy. |
| 8865 | * |
| 8866 | * \param[in] input Pointer to input buffer. |
| 8867 | * \param[in] input_len Length of the input buffer. |
| 8868 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 8869 | * \param[out] input_copy_len Length of the local copy buffer. |
| 8870 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8871 | * copied. |
| 8872 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 8873 | * copy is too small to hold contents of the |
| 8874 | * input buffer. |
| 8875 | */ |
| 8876 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8877 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 8878 | uint8_t *input_copy, size_t input_copy_len) |
| 8879 | { |
| 8880 | if (input_len > input_copy_len) { |
David Horstmann | 49a7276 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 8881 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8882 | } |
| 8883 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8884 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8885 | if (psa_input_pre_copy_hook != NULL) { |
| 8886 | psa_input_pre_copy_hook(input, input_len); |
| 8887 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8888 | #endif |
| 8889 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8890 | if (input_len > 0) { |
| 8891 | memcpy(input_copy, input, input_len); |
| 8892 | } |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8893 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8894 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8895 | if (psa_input_post_copy_hook != NULL) { |
| 8896 | psa_input_post_copy_hook(input, input_len); |
| 8897 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8898 | #endif |
| 8899 | |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8900 | return PSA_SUCCESS; |
| 8901 | } |
| 8902 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8903 | /** Copy from a local output buffer into a user-supplied one. |
| 8904 | * |
| 8905 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 8906 | * \param[in] output_copy_len Length of the local buffer. |
| 8907 | * \param[out] output Pointer to user-supplied output buffer. |
| 8908 | * \param[out] output_len Length of the user-supplied output buffer. |
| 8909 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8910 | * copied. |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8911 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8912 | * user-supplied output buffer is too small to |
| 8913 | * hold the contents of the local buffer. |
| 8914 | */ |
| 8915 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8916 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 8917 | uint8_t *output, size_t output_len) |
| 8918 | { |
| 8919 | if (output_len < output_copy_len) { |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8920 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8921 | } |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8922 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8923 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8924 | if (psa_output_pre_copy_hook != NULL) { |
| 8925 | psa_output_pre_copy_hook(output, output_len); |
| 8926 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8927 | #endif |
| 8928 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8929 | if (output_copy_len > 0) { |
| 8930 | memcpy(output, output_copy, output_copy_len); |
| 8931 | } |
| 8932 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8933 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8934 | if (psa_output_post_copy_hook != NULL) { |
| 8935 | psa_output_post_copy_hook(output, output_len); |
| 8936 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8937 | #endif |
| 8938 | |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8939 | return PSA_SUCCESS; |
| 8940 | } |
| 8941 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8942 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 8943 | psa_crypto_local_input_t *local_input) |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8944 | { |
| 8945 | psa_status_t status; |
| 8946 | |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8947 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8948 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8949 | if (input_len == 0) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8950 | return PSA_SUCCESS; |
| 8951 | } |
| 8952 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8953 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 8954 | if (local_input->buffer == NULL) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8955 | /* Since we dealt with the zero-length case above, we know that |
| 8956 | * a NULL return value means a failure of allocation. */ |
| 8957 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8958 | } |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8959 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8960 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8961 | local_input->length = input_len; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8962 | |
| 8963 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8964 | local_input->buffer, local_input->length); |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8965 | if (status != PSA_SUCCESS) { |
| 8966 | goto error; |
| 8967 | } |
| 8968 | |
| 8969 | return PSA_SUCCESS; |
| 8970 | |
| 8971 | error: |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8972 | mbedtls_free(local_input->buffer); |
| 8973 | local_input->buffer = NULL; |
| 8974 | local_input->length = 0; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8975 | return status; |
| 8976 | } |
| 8977 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8978 | void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input) |
David Horstmann | e6042ff | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 8979 | { |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8980 | mbedtls_free(local_input->buffer); |
| 8981 | local_input->buffer = NULL; |
| 8982 | local_input->length = 0; |
David Horstmann | e6042ff | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8985 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 8986 | psa_crypto_local_output_t *local_output) |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8987 | { |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8988 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8989 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8990 | if (output_len == 0) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8991 | return PSA_SUCCESS; |
| 8992 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8993 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 8994 | if (local_output->buffer == NULL) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8995 | /* Since we dealt with the zero-length case above, we know that |
| 8996 | * a NULL return value means a failure of allocation. */ |
| 8997 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8998 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8999 | local_output->length = output_len; |
| 9000 | local_output->original = output; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 9001 | |
| 9002 | return PSA_SUCCESS; |
| 9003 | } |
| 9004 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 9005 | psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output) |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 9006 | { |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 9007 | psa_status_t status; |
| 9008 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 9009 | if (local_output->buffer == NULL) { |
| 9010 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 9011 | return PSA_SUCCESS; |
| 9012 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 9013 | if (local_output->original == NULL) { |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 9014 | /* We have an internal copy but nothing to copy back to. */ |
| 9015 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 9016 | } |
| 9017 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 9018 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 9019 | local_output->original, local_output->length); |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 9020 | if (status != PSA_SUCCESS) { |
| 9021 | return status; |
| 9022 | } |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 9023 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 9024 | mbedtls_free(local_output->buffer); |
| 9025 | local_output->buffer = NULL; |
| 9026 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 9027 | |
| 9028 | return PSA_SUCCESS; |
| 9029 | } |
| 9030 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 9031 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |