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 | |
Gabor Mezei | 1882c51 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 189 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 190 | * point to the start of the copy. |
| 191 | * |
| 192 | * Assumptions: |
| 193 | * - psa_status_t status exists |
| 194 | * - An exit label is declared |
| 195 | * - output is the name of a pointer to the buffer to be copied |
| 196 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
| 197 | */ |
| 198 | #define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \ |
| 199 | status = psa_crypto_local_output_alloc_with_copy(output, length, \ |
| 200 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
| 201 | if (status != PSA_SUCCESS) { \ |
| 202 | goto exit; \ |
| 203 | } \ |
| 204 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
| 205 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 206 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 207 | * after first copying back its contents to the original buffer. |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 208 | * |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 209 | * Assumptions: |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 210 | * - psa_status_t status exists |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 211 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 212 | * - output is the name of the original buffer that was copied |
David Horstmann | 3e72db4 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 213 | */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 214 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 215 | output_copy = NULL; \ |
David Horstmann | 5a945f5 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 216 | do { \ |
| 217 | psa_status_t local_output_status; \ |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 218 | 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] | 219 | if (local_output_status != PSA_SUCCESS) { \ |
| 220 | /* Since this error case is an internal error, it's more serious than \ |
| 221 | * any existing error code and so it's fine to overwrite the existing \ |
| 222 | * status. */ \ |
| 223 | status = local_output_status; \ |
| 224 | } \ |
| 225 | } while (0) |
David Horstmann | 513101b | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 226 | #else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 227 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 228 | const uint8_t *input_copy_name = NULL; |
| 229 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 230 | input_copy = input; |
| 231 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 232 | input_copy = NULL; |
| 233 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 234 | uint8_t *output_copy_name = NULL; |
| 235 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 236 | output_copy = output; |
| 237 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 238 | output_copy = NULL; |
David Horstmann | 513101b | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 239 | #endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | e9a88ab | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 240 | |
| 241 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 242 | int psa_can_do_hash(psa_algorithm_t hash_alg) |
| 243 | { |
| 244 | (void) hash_alg; |
| 245 | return global_data.drivers_initialized; |
| 246 | } |
Valerio Setti | c6f004f | 2023-12-12 11:27:36 +0100 | [diff] [blame] | 247 | |
Valerio Setti | 1fff4f2 | 2023-12-28 14:19:34 +0100 | [diff] [blame] | 248 | 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] | 249 | { |
Valerio Setti | 1fff4f2 | 2023-12-28 14:19:34 +0100 | [diff] [blame] | 250 | (void) key_type; |
Valerio Setti | c6f004f | 2023-12-12 11:27:36 +0100 | [diff] [blame] | 251 | (void) cipher_alg; |
| 252 | return global_data.drivers_initialized; |
| 253 | } |
| 254 | |
| 255 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 256 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 257 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \ |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 258 | defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 259 | static int psa_is_dh_key_size_valid(size_t bits) |
| 260 | { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 261 | if (bits != 2048 && bits != 3072 && bits != 4096 && |
| 262 | bits != 6144 && bits != 8192) { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | return 1; |
| 267 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 268 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT || |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 269 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY || |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 270 | PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 273 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 274 | /* Mbed TLS error codes can combine a high-level error code and a |
| 275 | * low-level error code. The low-level error usually reflects the |
| 276 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | int low_level_ret = -(-ret & 0x007f); |
| 278 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 279 | case 0: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 282 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 284 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 09a9e58 | 2023-08-31 11:05:22 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_AES_BAD_INPUT_DATA: |
| 287 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 288 | #endif |
| 289 | |
| 290 | #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 291 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 292 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 293 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 294 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 295 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 297 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 299 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 301 | #endif |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 302 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 303 | #if defined(MBEDTLS_CAMELLIA_C) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 304 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 307 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 308 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 309 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 310 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 312 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 314 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 316 | #if defined(MBEDTLS_CHACHA20_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 317 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 319 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 320 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 321 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 322 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 324 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 326 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 327 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 328 | #if defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 329 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 331 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 333 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 335 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 337 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 339 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 341 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | return PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 343 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 346 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 347 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 348 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 349 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 351 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 352 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 354 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 356 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 357 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 358 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 359 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 361 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 362 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 363 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 364 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 365 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 367 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 368 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 369 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | return PSA_ERROR_INVALID_SIGNATURE; |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 371 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 373 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 375 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 376 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 377 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 379 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 380 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 381 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 383 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 384 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 386 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 388 | #endif |
| 389 | |
Dave Rodgman | dea266f | 2023-08-31 11:52:43 +0100 | [diff] [blame] | 390 | #if defined(MBEDTLS_MD_LIGHT) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 391 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 393 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 395 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 397 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 398 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 400 | #endif |
| 401 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 402 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 403 | #if defined(MBEDTLS_BIGNUM_C) |
| 404 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 405 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return PSA_ERROR_STORAGE_FAILURE; |
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 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 410 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 412 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 414 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 416 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 418 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 420 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 422 | #endif |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 423 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 424 | #if defined(MBEDTLS_PK_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 425 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 427 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 428 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 430 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \ |
| 431 | defined(MBEDTLS_PSA_ITS_FILE_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 432 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 434 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 435 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 436 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 438 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 440 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 441 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 443 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 445 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 446 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 447 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 449 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 451 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 453 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 455 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 457 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 459 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 460 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 461 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 463 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 465 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 467 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 469 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 470 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 472 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 474 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 476 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 478 | #endif |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 479 | |
Dave Rodgman | 1dab445 | 2023-09-01 09:59:51 +0100 | [diff] [blame] | 480 | #if defined(MBEDTLS_ECP_LIGHT) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 481 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 482 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 484 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 486 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 488 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 489 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 491 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 493 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 495 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 496 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 497 | case MBEDTLS_ERR_ECP_IN_PROGRESS: |
| 498 | return PSA_OPERATION_INCOMPLETE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 499 | #endif |
| 500 | #endif |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 501 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 502 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 504 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 505 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 510 | /** |
| 511 | * \brief For output buffers which contain "tags" |
| 512 | * (outputs that may be checked for validity like |
| 513 | * hashes, MACs and signatures), fill the unused |
| 514 | * part of the output buffer (the whole buffer on |
| 515 | * error, the trailing part on success) with |
| 516 | * something that isn't a valid tag (barring an |
| 517 | * attack on the tag and deliberately-crafted |
| 518 | * input), in case the caller doesn't check the |
| 519 | * return status properly. |
| 520 | * |
| 521 | * \param output_buffer Pointer to buffer to wipe. May not be NULL |
| 522 | * unless \p output_buffer_size is zero. |
| 523 | * \param status Status of function called to generate |
| 524 | * output_buffer originally |
| 525 | * \param output_buffer_size Size of output buffer. If zero, \p output_buffer |
| 526 | * could be NULL. |
| 527 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 528 | * less than \p output_buffer_size |
| 529 | */ |
| 530 | 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] | 531 | size_t output_buffer_size, size_t output_buffer_length) |
| 532 | { |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 533 | size_t offset = 0; |
| 534 | |
| 535 | if (output_buffer_size == 0) { |
| 536 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 537 | call memset because output_buffer may be NULL in this case */ |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | if (status == PSA_SUCCESS) { |
| 542 | offset = output_buffer_length; |
| 543 | } |
| 544 | |
| 545 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 546 | } |
| 547 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 550 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 551 | { |
| 552 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 554 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 555 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 556 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 557 | case PSA_KEY_TYPE_PASSWORD: |
| 558 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 559 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 560 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 561 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | if (bits != 128 && bits != 192 && bits != 256) { |
| 563 | return PSA_ERROR_INVALID_ARGUMENT; |
| 564 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 565 | break; |
| 566 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 567 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 568 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | if (bits != 128 && bits != 192 && bits != 256) { |
| 570 | return PSA_ERROR_INVALID_ARGUMENT; |
| 571 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 572 | break; |
| 573 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 574 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 575 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | if (bits != 128 && bits != 192 && bits != 256) { |
| 577 | return PSA_ERROR_INVALID_ARGUMENT; |
| 578 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 579 | break; |
| 580 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 581 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 582 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | if (bits != 64 && bits != 128 && bits != 192) { |
| 584 | return PSA_ERROR_INVALID_ARGUMENT; |
| 585 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 586 | break; |
| 587 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 588 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 589 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | if (bits != 256) { |
| 591 | return PSA_ERROR_INVALID_ARGUMENT; |
| 592 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 593 | break; |
| 594 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 595 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 597 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | if (bits % 8 != 0) { |
| 599 | return PSA_ERROR_INVALID_ARGUMENT; |
| 600 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 605 | /** 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] | 606 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 607 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 608 | * 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] | 609 | * |
| 610 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 611 | * \param[in] key_type The key type of the key to be used with the |
| 612 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 613 | * |
| 614 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 615 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 616 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 617 | * 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] | 618 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 619 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 620 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 622 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 624 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 625 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 626 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 627 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 630 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 631 | * key. */ |
| 632 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 633 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 634 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 635 | * the block length (larger than 1) for block ciphers. */ |
| 636 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 637 | return PSA_SUCCESS; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 646 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 647 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | if (slot->key.data != NULL) { |
| 649 | return PSA_ERROR_ALREADY_EXISTS; |
| 650 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 653 | if (slot->key.data == NULL) { |
| 654 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 655 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 656 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 657 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 662 | const uint8_t *data, |
| 663 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 664 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 666 | data_length); |
| 667 | if (status != PSA_SUCCESS) { |
| 668 | return status; |
| 669 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | memcpy(slot->key.data, data, data_length); |
| 672 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 675 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 676 | const psa_key_attributes_t *attributes, |
| 677 | const uint8_t *data, size_t data_length, |
| 678 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 680 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 681 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 682 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 683 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 684 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 685 | if (data_length == 0) { |
| 686 | return PSA_ERROR_NOT_SUPPORTED; |
| 687 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | if (key_type_is_raw_bytes(type)) { |
| 690 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 693 | *bits); |
| 694 | if (status != PSA_SUCCESS) { |
| 695 | return status; |
| 696 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 697 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 698 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 700 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | return PSA_SUCCESS; |
| 704 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 705 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 706 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 707 | if (PSA_KEY_TYPE_IS_DH(type)) { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 708 | 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] | 709 | return PSA_ERROR_INVALID_ARGUMENT; |
| 710 | } |
Przemek Stekiel | 33c91eb | 2023-05-30 15:16:35 +0200 | [diff] [blame] | 711 | return mbedtls_psa_ffdh_import_key(attributes, |
| 712 | data, data_length, |
| 713 | key_buffer, key_buffer_size, |
| 714 | key_buffer_length, |
| 715 | bits); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 716 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 717 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 718 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 719 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 720 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 721 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 722 | return mbedtls_psa_ecp_import_key(attributes, |
| 723 | data, data_length, |
| 724 | key_buffer, key_buffer_size, |
| 725 | key_buffer_length, |
| 726 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 727 | } |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 728 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 729 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 730 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 731 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 733 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 734 | return mbedtls_psa_rsa_import_key(attributes, |
| 735 | data, data_length, |
| 736 | key_buffer, key_buffer_size, |
| 737 | key_buffer_length, |
| 738 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 739 | } |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 740 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 741 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 742 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 743 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 748 | /** Calculate the intersection of two algorithm usage policies. |
| 749 | * |
| 750 | * Return 0 (which allows no operation) on incompatibility. |
| 751 | */ |
| 752 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 753 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 754 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 756 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 757 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | if (alg1 == alg2) { |
| 759 | return alg1; |
| 760 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 761 | /* If the policies are from the same hash-and-sign family, check |
| 762 | * 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] | 763 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 764 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 765 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 766 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 767 | return alg2; |
| 768 | } |
| 769 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 770 | return alg1; |
| 771 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 772 | } |
| 773 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 774 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 775 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 777 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 778 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 779 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 780 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 781 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 782 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 783 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 784 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 785 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 786 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 787 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 788 | } |
| 789 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 791 | (alg1_len <= alg2_len)) { |
| 792 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 793 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 795 | (alg2_len <= alg1_len)) { |
| 796 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | /* If the policies are from the same MAC family, check whether one |
| 800 | * of them is a minimum-MAC-length policy. Calculate the most |
| 801 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 803 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 804 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 805 | /* Validate the combination of key type and algorithm. Since the base |
| 806 | * 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] | 807 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 808 | return 0; |
| 809 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 810 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 811 | /* Get the (exact or at-least) output lengths for both sides of the |
| 812 | * requested intersection. None of the currently supported algorithms |
| 813 | * have an output length dependent on the actual key size, so setting it |
| 814 | * to a bogus value of 0 is currently OK. |
| 815 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 816 | * Note that for at-least-this-length wildcard algorithms, the output |
| 817 | * length is set to the shortest allowed length, which allows us to |
| 818 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 820 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 821 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 822 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 823 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 825 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 826 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 827 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 828 | |
| 829 | /* If only one is an at-least-this-length policy, the intersection would |
| 830 | * be the other (fixed-length) policy as long as said fixed length is |
| 831 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 833 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 834 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 836 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 837 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 838 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 839 | /* If none of them are wildcards, check whether they define the same tag |
| 840 | * length. This is still possible here when one is default-length and |
| 841 | * the other specific-length. Ensure to always return the |
| 842 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | if (alg1_len == alg2_len) { |
| 844 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 845 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 846 | } |
| 847 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 852 | psa_algorithm_t policy_alg, |
| 853 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 854 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 855 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | if (requested_alg == policy_alg) { |
| 857 | return 1; |
| 858 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 859 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 860 | * and requested_alg is the same hash-and-sign family with any hash, |
| 861 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 863 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 864 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 865 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 866 | } |
| 867 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 868 | * the requested algorithm, check the requested tag length to be |
| 869 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 871 | PSA_ALG_IS_AEAD(requested_alg) && |
| 872 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 873 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 874 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 875 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 876 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 877 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 878 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 879 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 881 | PSA_ALG_IS_MAC(requested_alg) && |
| 882 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 883 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 884 | /* Validate the combination of key type and algorithm. Since the policy |
| 885 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 887 | return 0; |
| 888 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 889 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 890 | /* Get both the requested output length for the algorithm which is to be |
| 891 | * verified, and the default output length for the base algorithm. |
| 892 | * Note that none of the currently supported algorithms have an output |
| 893 | * length dependent on actual key size, so setting it to a bogus value |
| 894 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 895 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 897 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | key_type, 0, |
| 899 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 900 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 901 | /* If the policy is default-length, only allow an algorithm with |
| 902 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 904 | return requested_output_length == default_output_length; |
| 905 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 906 | |
| 907 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 908 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 910 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 911 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 914 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 915 | * check for the requested MAC length to be equal to or longer than the |
| 916 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 918 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 919 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 920 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 921 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 922 | /* 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] | 923 | * a key derivation with that key agreement should also be allowed. This |
| 924 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 926 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 927 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 928 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 929 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 930 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 934 | /** Test whether a policy permits an algorithm. |
| 935 | * |
| 936 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 937 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 938 | * \note This function requires providing the key type for which the policy is |
| 939 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 940 | * have different properties depending on what kind of cipher it is |
| 941 | * combined with. |
| 942 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 943 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 944 | * allowed by the \p policy. |
| 945 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 946 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 947 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 948 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 950 | psa_key_type_t key_type, |
| 951 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 952 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 953 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | if (alg == 0) { |
| 955 | return PSA_ERROR_INVALID_ARGUMENT; |
| 956 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 957 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 958 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 959 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 960 | return PSA_ERROR_INVALID_ARGUMENT; |
| 961 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 964 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 965 | return PSA_SUCCESS; |
| 966 | } else { |
| 967 | return PSA_ERROR_NOT_PERMITTED; |
| 968 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 971 | /** Restrict a key policy based on a constraint. |
| 972 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 973 | * \note This function requires providing the key type for which the policy is |
| 974 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 975 | * have different properties depending on what kind of cipher it is |
| 976 | * combined with. |
| 977 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 978 | * \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] | 979 | * \param[in,out] policy The policy to restrict. |
| 980 | * \param[in] constraint The policy constraint to apply. |
| 981 | * |
| 982 | * \retval #PSA_SUCCESS |
| 983 | * \c *policy contains the intersection of the original value of |
| 984 | * \c *policy and \c *constraint. |
| 985 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 986 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 987 | * \c *policy is unchanged. |
| 988 | */ |
| 989 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 990 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 991 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 993 | { |
| 994 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 996 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 997 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 999 | constraint->alg2); |
| 1000 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1001 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1002 | } |
| 1003 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1004 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1005 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1006 | policy->usage &= constraint->usage; |
| 1007 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1008 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1009 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
Przemek Stekiel | 061f694 | 2022-11-30 10:51:35 +0100 | [diff] [blame] | 1012 | /** Get the description of a key given its identifier and policy constraints |
| 1013 | * and lock it. |
| 1014 | * |
| 1015 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1016 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1017 | * zero, the algorithm is not checked. |
| 1018 | * |
| 1019 | * In case of a persistent key, the function loads the description of the key |
| 1020 | * into a key slot if not already done. |
| 1021 | * |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1022 | * On success, the returned key slot has been registered for reading. |
| 1023 | * It is the responsibility of the caller to call psa_unregister_read(slot) |
| 1024 | * when they have finished reading the contents of the slot. |
Przemek Stekiel | 061f694 | 2022-11-30 10:51:35 +0100 | [diff] [blame] | 1025 | */ |
| 1026 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1027 | mbedtls_svc_key_id_t key, |
| 1028 | psa_key_slot_t **p_slot, |
| 1029 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1031 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1032 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 1033 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1036 | if (status != PSA_SUCCESS) { |
| 1037 | return status; |
| 1038 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1039 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1040 | |
| 1041 | /* Enforce that usage policy for the key slot contains all the flags |
| 1042 | * required by the usage parameter. There is one exception: public |
| 1043 | * keys can always be exported, so we treat public key objects as |
| 1044 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1046 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1050 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1051 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1052 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1053 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1054 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1055 | if (alg != 0) { |
| 1056 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1057 | slot->attr.type, |
| 1058 | alg); |
| 1059 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1060 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1062 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1065 | |
| 1066 | error: |
| 1067 | *p_slot = NULL; |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1068 | psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1071 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1072 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1073 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1074 | * |
| 1075 | * 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] | 1076 | * available, as opposed to a key in a secure element and/or to be used |
| 1077 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1078 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1079 | * This is a temporary function that may be used instead of |
| 1080 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1081 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1082 | * |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1083 | * On success, the returned key slot has been registered for reading. |
| 1084 | * It is the responsibility of the caller to call psa_unregister_read(slot) |
| 1085 | * when they have finished reading the contents of the slot. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1086 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1087 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1088 | mbedtls_svc_key_id_t key, |
| 1089 | psa_key_slot_t **p_slot, |
| 1090 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | psa_algorithm_t alg) |
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 status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1094 | usage, alg); |
| 1095 | if (status != PSA_SUCCESS) { |
| 1096 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1097 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1099 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 1100 | psa_unregister_read(*p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | *p_slot = NULL; |
| 1102 | return PSA_ERROR_NOT_SUPPORTED; |
| 1103 | } |
| 1104 | |
| 1105 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1106 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | 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] | 1109 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | if (slot->key.data != NULL) { |
Tom Cosgrove | 52f7e18 | 2023-08-01 09:08:48 +0100 | [diff] [blame] | 1111 | mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1113 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1114 | slot->key.data = NULL; |
| 1115 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1120 | /** Completely wipe a slot in memory, including its policy. |
| 1121 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1123 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | /* |
| 1127 | * 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] | 1128 | * do our best to report an unexpected amount of registered readers or |
| 1129 | * an unexpected state. |
| 1130 | * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for |
| 1131 | * wiping. |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1133 | * function is called as part of the execution of a test suite, the |
| 1134 | * execution of the test suite is stopped in error if the assertion fails. |
| 1135 | */ |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1136 | switch (slot->state) { |
| 1137 | case PSA_SLOT_FULL: |
| 1138 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1139 | * caller is the last reader. */ |
| 1140 | case PSA_SLOT_PENDING_DELETION: |
| 1141 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1142 | * caller is the last reader. */ |
| 1143 | if (slot->registered_readers != 1) { |
| 1144 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1); |
| 1145 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1146 | } |
| 1147 | break; |
| 1148 | case PSA_SLOT_FILLING: |
| 1149 | /* In this state registered_readers must be 0. */ |
| 1150 | if (slot->registered_readers != 0) { |
| 1151 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0); |
| 1152 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1153 | } |
| 1154 | break; |
| 1155 | case PSA_SLOT_EMPTY: |
| 1156 | /* The slot is already empty, it cannot be wiped. */ |
| 1157 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY); |
| 1158 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1159 | break; |
| 1160 | default: |
| 1161 | /* The slot's state is invalid. */ |
| 1162 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1165 | /* Multipart operations may still be using the key. This is safe |
| 1166 | * because all multipart operation objects are independent from |
| 1167 | * the key slot: if they need to access the key after the setup |
| 1168 | * phase, they have a copy of the key. Note that this means that |
| 1169 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1170 | /* At this point, key material and other type-specific content has |
| 1171 | * been wiped. Clear remaining metadata. We can call memset and not |
Ryan Everett | aa33c51 | 2023-12-21 17:32:07 +0000 | [diff] [blame] | 1172 | * zeroize because the metadata is not particularly sensitive. |
| 1173 | * This memset also sets the slot's state to PSA_SLOT_EMPTY. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | memset(slot, 0, sizeof(*slot)); |
| 1175 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1179 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1180 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1181 | psa_status_t status; /* status of the last operation */ |
| 1182 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1183 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1184 | psa_se_drv_table_entry_t *driver; |
| 1185 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1188 | return PSA_SUCCESS; |
| 1189 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1190 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1191 | /* |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1192 | * Get the description of the key in a key slot, and register to read it. |
| 1193 | * In the case of a persistent key, this will load the key description |
| 1194 | * from persistent memory if not done yet. |
| 1195 | * 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] | 1196 | * 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] | 1197 | * the current implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1199 | if (status != PSA_SUCCESS) { |
| 1200 | return status; |
| 1201 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1202 | |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1203 | /* Set the key slot containing the key description's state to |
| 1204 | * PENDING_DELETION. This stops new operations from registering |
| 1205 | * to read the slot. Current readers can safely continue to access |
| 1206 | * the key within the slot; the last registered reader will |
| 1207 | * automatically wipe the slot when they call psa_unregister_read(). |
| 1208 | * If the key is persistent, we can now delete the copy of the key |
| 1209 | * from memory. If the key is opaque, we require the driver to |
| 1210 | * deal with the deletion. */ |
Ryan Everett | c70ce57 | 2024-01-03 16:04:33 +0000 | [diff] [blame] | 1211 | slot->state = PSA_SLOT_PENDING_DELETION; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1213 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1214 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1215 | * if we attempt it, depending on whether the key is merely read-only |
| 1216 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1217 | * Just do the best we can, which is to wipe the copy in memory |
| 1218 | * (done in this function's cleanup code). */ |
| 1219 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1220 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1223 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1225 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1226 | /* For a key in a secure element, we need to do three things: |
| 1227 | * remove the key file in internal storage, destroy the |
| 1228 | * key inside the secure element, and update the driver's |
| 1229 | * persistent data. Start a transaction that will encompass these |
| 1230 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1232 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1234 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | status = psa_crypto_save_transaction(); |
| 1236 | if (status != PSA_SUCCESS) { |
| 1237 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1238 | /* We should still try to destroy the key in the secure |
| 1239 | * element and the key metadata in storage. This is especially |
| 1240 | * important if the error is that the storage is full. |
| 1241 | * But how to do it exactly without risking an inconsistent |
| 1242 | * state after a reset? |
| 1243 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1244 | */ |
| 1245 | overall_status = status; |
| 1246 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1249 | status = psa_destroy_se_key(driver, |
| 1250 | psa_key_slot_get_slot_number(slot)); |
| 1251 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1252 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1253 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1254 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1255 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1256 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1257 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1258 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ryan Everett | 4a0ba80 | 2024-01-17 14:12:33 +0000 | [diff] [blame] | 1259 | /* Destroy the copy of the persistent key from storage. |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1260 | * The slot will still hold a copy of the key until the last reader |
| 1261 | * unregisters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1262 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1263 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1264 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1265 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1266 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1267 | /* TODO: other slots may have a copy of the same key. We should |
| 1268 | * invalidate them. |
| 1269 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1270 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1271 | } |
| 1272 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1273 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1274 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1275 | if (driver != NULL) { |
| 1276 | status = psa_save_se_persistent_data(driver); |
| 1277 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1278 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1279 | } |
| 1280 | status = psa_crypto_stop_transaction(); |
| 1281 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1282 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1284 | } |
| 1285 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1286 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1287 | exit: |
Ryan Everett | 7ed542e | 2024-01-17 11:39:09 +0000 | [diff] [blame] | 1288 | /* Unregister from reading the slot. If we are the last active reader |
| 1289 | * then this will wipe the slot. */ |
| 1290 | status = psa_unregister_read(slot); |
| 1291 | /* Prioritize CORRUPTION_DETECTED from unregistering over |
| 1292 | * a storage error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1293 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1294 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | } |
| 1296 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1300 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1301 | static psa_status_t psa_get_rsa_public_exponent( |
| 1302 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1304 | { |
| 1305 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1306 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1307 | uint8_t *buffer = NULL; |
| 1308 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1309 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1312 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1313 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | } |
| 1315 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1316 | /* It's the default value, which is reported as an empty string, |
| 1317 | * so there's nothing to do. */ |
| 1318 | goto exit; |
| 1319 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | buflen = mbedtls_mpi_size(&mpi); |
| 1322 | buffer = mbedtls_calloc(1, buflen); |
| 1323 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1324 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1325 | goto exit; |
| 1326 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1327 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1328 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1329 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1330 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1331 | attributes->domain_parameters = buffer; |
| 1332 | attributes->domain_parameters_size = buflen; |
| 1333 | |
| 1334 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | mbedtls_mpi_free(&mpi); |
| 1336 | if (ret != 0) { |
| 1337 | mbedtls_free(buffer); |
| 1338 | } |
| 1339 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1340 | } |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1341 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1342 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1343 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1344 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1345 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1347 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1348 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1349 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1350 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1351 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1353 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1356 | if (status != PSA_SUCCESS) { |
| 1357 | return status; |
| 1358 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1359 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1360 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1361 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1362 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1363 | |
| 1364 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1366 | psa_set_key_slot_number(attributes, |
| 1367 | psa_key_slot_get_slot_number(slot)); |
| 1368 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1369 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1371 | switch (slot->attr.type) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1372 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1373 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1374 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1375 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1376 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Pengyu Lv | f75893b | 2023-12-08 17:21:39 +0800 | [diff] [blame] | 1377 | /* TODO: This is a temporary situation where domain parameters are deprecated, |
| 1378 | * but we need it for namely generating an RSA key with a non-default exponent. |
| 1379 | * 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] | 1380 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1382 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1383 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1384 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | slot->attr.type, |
| 1386 | slot->key.data, |
| 1387 | slot->key.bytes, |
| 1388 | &rsa); |
| 1389 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1390 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | status = psa_get_rsa_public_exponent(rsa, |
| 1394 | attributes); |
| 1395 | mbedtls_rsa_free(rsa); |
| 1396 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1397 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1398 | break; |
Pengyu Lv | e9efbc2 | 2023-12-08 16:59:08 +0800 | [diff] [blame] | 1399 | #else |
| 1400 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
| 1401 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
| 1402 | attributes->domain_parameters = NULL; |
| 1403 | attributes->domain_parameters_size = SIZE_MAX; |
| 1404 | break; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1405 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1406 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1407 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1408 | default: |
| 1409 | /* Nothing else to do. */ |
| 1410 | break; |
| 1411 | } |
| 1412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1413 | if (status != PSA_SUCCESS) { |
| 1414 | psa_reset_key_attributes(attributes); |
| 1415 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1416 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1417 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1422 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1423 | psa_status_t psa_get_key_slot_number( |
| 1424 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1426 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1428 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | return PSA_SUCCESS; |
| 1430 | } else { |
| 1431 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1432 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1433 | } |
| 1434 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1437 | size_t key_buffer_size, |
| 1438 | uint8_t *data, |
| 1439 | size_t data_size, |
| 1440 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1441 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1442 | if (key_buffer_size > data_size) { |
| 1443 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1444 | } |
| 1445 | memcpy(data, key_buffer, key_buffer_size); |
| 1446 | memset(data + key_buffer_size, 0, |
| 1447 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1448 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1452 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1453 | const psa_key_attributes_t *attributes, |
| 1454 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1456 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1457 | psa_key_type_t type = attributes->core.type; |
| 1458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1459 | if (key_type_is_raw_bytes(type) || |
| 1460 | PSA_KEY_TYPE_IS_RSA(type) || |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1461 | PSA_KEY_TYPE_IS_ECC(type) || |
| 1462 | PSA_KEY_TYPE_IS_DH(type)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | return psa_export_key_buffer_internal( |
| 1464 | key_buffer, key_buffer_size, |
| 1465 | data, data_size, data_length); |
| 1466 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1467 | /* This shouldn't happen in the reference implementation, but |
| 1468 | it is valid for a special-purpose implementation to omit |
| 1469 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1475 | uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1476 | size_t data_size, |
| 1477 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1478 | { |
| 1479 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1480 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1481 | psa_key_slot_t *slot; |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1482 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1483 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1484 | /* Reject a zero-length output buffer now, since this can never be a |
| 1485 | * valid key representation. This way we know that data must be a valid |
| 1486 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | if (data_size == 0) { |
| 1488 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1489 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1490 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1491 | /* Set the key to empty now, so that even when there are errors, we always |
| 1492 | * set data_length to a value between 0 and data_size. On error, setting |
| 1493 | * the key to empty is a good choice because an empty key representation is |
| 1494 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1495 | *data_length = 0; |
| 1496 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1497 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1498 | * which don't require any flag, but |
| 1499 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1500 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1502 | PSA_KEY_USAGE_EXPORT, 0); |
| 1503 | if (status != PSA_SUCCESS) { |
| 1504 | return status; |
| 1505 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1506 | |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1507 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1508 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1509 | psa_key_attributes_t attributes = { |
| 1510 | .core = slot->attr |
| 1511 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1512 | status = psa_driver_wrapper_export_key(&attributes, |
| 1513 | slot->key.data, slot->key.bytes, |
| 1514 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1515 | |
Ryan Everett | 35f6853 | 2024-01-25 12:02:03 +0000 | [diff] [blame] | 1516 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1517 | exit: |
Ryan Everett | 35f6853 | 2024-01-25 12:02:03 +0000 | [diff] [blame] | 1518 | #endif |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1519 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1520 | |
Ryan Everett | 45ac526 | 2024-01-08 17:15:19 +0000 | [diff] [blame] | 1521 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1525 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1526 | const psa_key_attributes_t *attributes, |
| 1527 | const uint8_t *key_buffer, |
| 1528 | size_t key_buffer_size, |
| 1529 | uint8_t *data, |
| 1530 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1532 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1533 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1534 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1535 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1536 | (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) || |
| 1537 | PSA_KEY_TYPE_IS_DH(type))) { |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1538 | /* Exporting public -> public */ |
| 1539 | return psa_export_key_buffer_internal( |
| 1540 | key_buffer, key_buffer_size, |
| 1541 | data, data_size, data_length); |
| 1542 | } else if (PSA_KEY_TYPE_IS_RSA(type)) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1543 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1544 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1545 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1546 | key_buffer, |
| 1547 | key_buffer_size, |
| 1548 | data, |
| 1549 | data_size, |
| 1550 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1551 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1552 | /* We don't know how to convert a private RSA key to public. */ |
| 1553 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1554 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1555 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1556 | } else if (PSA_KEY_TYPE_IS_ECC(type)) { |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1557 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1558 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1559 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1560 | key_buffer, |
| 1561 | key_buffer_size, |
| 1562 | data, |
| 1563 | data_size, |
| 1564 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1565 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1566 | /* We don't know how to convert a private ECC key to public */ |
| 1567 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1568 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1569 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1570 | } else if (PSA_KEY_TYPE_IS_DH(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1571 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1572 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 1573 | return mbedtls_psa_ffdh_export_public_key(attributes, |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1574 | key_buffer, |
| 1575 | key_buffer_size, |
| 1576 | data, data_size, |
| 1577 | data_length); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1578 | #else |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1579 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1580 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1581 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1582 | } else { |
Przemek Stekiel | 0b11ee0 | 2023-05-16 13:26:06 +0200 | [diff] [blame] | 1583 | (void) key_buffer; |
| 1584 | (void) key_buffer_size; |
| 1585 | (void) data; |
| 1586 | (void) data_size; |
| 1587 | (void) data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1588 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1589 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | 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] | 1593 | uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | size_t data_size, |
| 1595 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1596 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1597 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1598 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1599 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1600 | psa_key_attributes_t attributes; |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1601 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1602 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1603 | /* Reject a zero-length output buffer now, since this can never be a |
| 1604 | * valid key representation. This way we know that data must be a valid |
| 1605 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | if (data_size == 0) { |
| 1607 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1608 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1609 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1610 | /* Set the key to empty now, so that even when there are errors, we always |
| 1611 | * set data_length to a value between 0 and data_size. On error, setting |
| 1612 | * the key to empty is a good choice because an empty key representation is |
| 1613 | * unlikely to be accepted anywhere. */ |
| 1614 | *data_length = 0; |
| 1615 | |
| 1616 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1618 | if (status != PSA_SUCCESS) { |
| 1619 | return status; |
| 1620 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1621 | |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1622 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1624 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1625 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1626 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1629 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1630 | .core = slot->attr |
| 1631 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1632 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1633 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1634 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1635 | |
| 1636 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 1637 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1638 | |
Ryan Everett | b1d2c67 | 2024-01-08 17:19:30 +0000 | [diff] [blame] | 1639 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1641 | } |
| 1642 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1643 | MBEDTLS_STATIC_ASSERT( |
| 1644 | (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1645 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1646 | MBEDTLS_STATIC_ASSERT( |
| 1647 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1648 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1649 | MBEDTLS_STATIC_ASSERT( |
| 1650 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1651 | "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] | 1652 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1653 | /** Validate that a key policy is internally well-formed. |
| 1654 | * |
| 1655 | * This function only rejects invalid policies. It does not validate the |
| 1656 | * consistency of the policy with respect to other attributes of the key |
| 1657 | * such as the key type. |
| 1658 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | 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] | 1660 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1662 | PSA_KEY_USAGE_COPY | |
| 1663 | PSA_KEY_USAGE_ENCRYPT | |
| 1664 | PSA_KEY_USAGE_DECRYPT | |
| 1665 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1666 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1667 | PSA_KEY_USAGE_SIGN_HASH | |
| 1668 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1669 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1670 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1671 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1672 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1677 | /** Validate the internal consistency of key attributes. |
| 1678 | * |
| 1679 | * This function only rejects invalid attribute values. If does not |
| 1680 | * validate the consistency of the attributes with any key data that may |
| 1681 | * be involved in the creation of the key. |
| 1682 | * |
| 1683 | * Call this function early in the key creation process. |
| 1684 | * |
| 1685 | * \param[in] attributes Key attributes for the new key. |
| 1686 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1687 | * NULL for a transparent key. |
| 1688 | * |
| 1689 | */ |
| 1690 | static psa_status_t psa_validate_key_attributes( |
| 1691 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1693 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1694 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1696 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | status = psa_validate_key_location(lifetime, p_drv); |
| 1699 | if (status != PSA_SUCCESS) { |
| 1700 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | status = psa_validate_key_persistence(lifetime); |
| 1704 | if (status != PSA_SUCCESS) { |
| 1705 | return status; |
| 1706 | } |
| 1707 | |
| 1708 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1709 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1710 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1711 | } |
| 1712 | } else { |
| 1713 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1714 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1719 | if (status != PSA_SUCCESS) { |
| 1720 | return status; |
| 1721 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* Refuse to create overly large keys. |
| 1724 | * Note that this doesn't trigger on import if the attributes don't |
| 1725 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1726 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1728 | return PSA_ERROR_NOT_SUPPORTED; |
| 1729 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1730 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1731 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1733 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1734 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1735 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1740 | /** Prepare a key slot to receive key material. |
| 1741 | * |
| 1742 | * This function allocates a key slot and sets its metadata. |
| 1743 | * |
| 1744 | * If this function fails, call psa_fail_key_creation(). |
| 1745 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1746 | * This function is intended to be used as follows: |
| 1747 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1748 | * it with the specified attributes, and in case of a volatile key assign it |
| 1749 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1750 | * -# Populate the slot with the key material. |
| 1751 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1752 | * In case of failure at any step, stop the sequence and call |
| 1753 | * psa_fail_key_creation(). |
| 1754 | * |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1755 | * On success, the key slot's state is PSA_SLOT_FILLING. |
| 1756 | * It is the responsibility of the caller to change the slot's state to |
| 1757 | * PSA_SLOT_EMPTY/FULL once key creation has finished. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1758 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1759 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1760 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1761 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1762 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1763 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1764 | * |
| 1765 | * \retval #PSA_SUCCESS |
| 1766 | * The key slot is ready to receive key material. |
| 1767 | * \return If this function fails, the key slot is an invalid state. |
| 1768 | * 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] | 1769 | */ |
| 1770 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1771 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1772 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1773 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1775 | { |
| 1776 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1777 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1778 | psa_key_slot_t *slot; |
| 1779 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1780 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1781 | *p_drv = NULL; |
| 1782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1784 | if (status != PSA_SUCCESS) { |
| 1785 | return status; |
| 1786 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1787 | |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1788 | status = psa_reserve_free_key_slot(&volatile_key_id, p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | if (status != PSA_SUCCESS) { |
| 1790 | return status; |
| 1791 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1792 | slot = *p_slot; |
| 1793 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1794 | /* We're storing the declared bit-size of the key. It's up to each |
| 1795 | * creation mechanism to verify that this information is correct. |
| 1796 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1797 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1798 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1799 | * volatile key identifier associated to the slot returned to contain its |
| 1800 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1801 | |
| 1802 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1804 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1805 | slot->attr.id = volatile_key_id; |
| 1806 | #else |
| 1807 | slot->attr.id.key_id = volatile_key_id; |
| 1808 | #endif |
| 1809 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1810 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1811 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1812 | * external-only flags, query `attributes`. Thanks to the check |
| 1813 | * in psa_validate_key_attributes(), this leaves the dual-use |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1814 | * flags and any internal flag that psa_reserve_free_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1815 | * may have set. */ |
| 1816 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1817 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1818 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1819 | /* 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] | 1820 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1821 | * create the key file in internal storage, create the |
| 1822 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1823 | * persistent data. This is done by starting a transaction that will |
| 1824 | * encompass these three actions. |
| 1825 | * For registering a volatile key, we just need to find an appropriate |
| 1826 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1827 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1828 | /* The first thing to do is to find a slot number for the new key. |
| 1829 | * We save the slot number in persistent storage as part of the |
| 1830 | * transaction data. It will be needed to recover if the power |
| 1831 | * fails during the key creation process, to clean up on the secure |
| 1832 | * element side after restarting. Obtaining a slot number from the |
| 1833 | * secure element driver updates its persistent state, but we do not yet |
| 1834 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1835 | * 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] | 1836 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1837 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1839 | &slot_number); |
| 1840 | if (status != PSA_SUCCESS) { |
| 1841 | return status; |
| 1842 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1845 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1846 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1847 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1848 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | status = psa_crypto_save_transaction(); |
| 1850 | if (status != PSA_SUCCESS) { |
| 1851 | (void) psa_crypto_stop_transaction(); |
| 1852 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1853 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1854 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1855 | |
| 1856 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1858 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1861 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1863 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1864 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1866 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1867 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1868 | |
| 1869 | /** Finalize the creation of a key once its key material has been set. |
| 1870 | * |
| 1871 | * This entails writing the key to persistent storage. |
| 1872 | * |
| 1873 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1874 | * See the documentation of psa_start_key_creation() for the intended use |
| 1875 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1876 | * |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1877 | * If the finalization succeeds, the function sets the key slot's state to |
| 1878 | * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the |
| 1879 | * key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1880 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1881 | * \param[in,out] slot Pointer to the slot with key material. |
| 1882 | * \param[in] driver The secure element driver for the key, |
| 1883 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1884 | * \param[out] key On success, identifier of the key. Note that the |
| 1885 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1886 | * |
| 1887 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1888 | * The key was successfully created. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1889 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1890 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1891 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1892 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1893 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1894 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1895 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1896 | * \return If this function fails, the key slot is an invalid state. |
| 1897 | * 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] | 1898 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1899 | static psa_status_t psa_finish_key_creation( |
| 1900 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1901 | psa_se_drv_table_entry_t *driver, |
| 1902 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1903 | { |
| 1904 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1905 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1906 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1907 | |
| 1908 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1910 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1912 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1913 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1914 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1915 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1916 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1917 | sizeof(data.slot_number), |
| 1918 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1921 | status = psa_save_persistent_key(&slot->attr, |
| 1922 | (uint8_t *) &data, |
| 1923 | sizeof(data)); |
| 1924 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1925 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1926 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1927 | /* Key material is saved in export representation in the slot, so |
| 1928 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | status = psa_save_persistent_key(&slot->attr, |
| 1930 | slot->key.data, |
| 1931 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1932 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1933 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1934 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1935 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1936 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1937 | /* Finish the transaction for a key creation. This does not |
| 1938 | * happen when registering an existing key. Detect this case |
| 1939 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1940 | * creation of a persistent key in a secure element requires a transaction, |
| 1941 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | if (driver != NULL && |
| 1943 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1944 | status = psa_save_se_persistent_data(driver); |
| 1945 | if (status != PSA_SUCCESS) { |
| 1946 | psa_destroy_persistent_key(slot->attr.id); |
| 1947 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1948 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1950 | } |
| 1951 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1954 | *key = slot->attr.id; |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1955 | status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING, |
| 1956 | PSA_SLOT_FULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1958 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1960 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /** Abort the creation of a key. |
| 1966 | * |
| 1967 | * You may call this function after calling psa_start_key_creation(), |
| 1968 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1969 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1970 | * See the documentation of psa_start_key_creation() for the intended use |
Ryan Everett | b69118e | 2024-01-02 15:54:32 +0000 | [diff] [blame] | 1971 | * of this function. Sets the slot's state to PSA_SLOT_EMPTY. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1972 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1973 | * \param[in,out] slot Pointer to the slot with key material. |
| 1974 | * \param[in] driver The secure element driver for the key, |
| 1975 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1976 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1978 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1979 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1980 | (void) driver; |
| 1981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1982 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1983 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1985 | |
| 1986 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1987 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1988 | * element, and the failure happened later (when saving metadata |
| 1989 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1990 | * element. |
| 1991 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1992 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1993 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1994 | /* Abort the ongoing transaction if any (there may not be one if |
| 1995 | * the creation process failed before starting one, or if the |
| 1996 | * key creation is a registration of a key in a secure element). |
| 1997 | * Earlier functions must already have done what it takes to undo any |
| 1998 | * partial creation. All that's left is to update the transaction data |
| 1999 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2001 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2006 | /** Validate optional attributes during key creation. |
| 2007 | * |
| 2008 | * Some key attributes are optional during key creation. If they are |
| 2009 | * specified in the attributes structure, check that they are consistent |
| 2010 | * with the data in the slot. |
| 2011 | * |
| 2012 | * This function should be called near the end of key creation, after |
| 2013 | * the slot in memory is fully populated but before saving persistent data. |
| 2014 | */ |
| 2015 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2016 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2018 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | if (attributes->core.type != 0) { |
| 2020 | if (attributes->core.type != slot->attr.type) { |
| 2021 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2022 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | if (attributes->domain_parameters_size != 0) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2026 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 2027 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2029 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2030 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2031 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2032 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2033 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2034 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | slot->attr.type, |
| 2036 | slot->key.data, |
| 2037 | slot->key.bytes, |
| 2038 | &rsa); |
| 2039 | if (status != PSA_SUCCESS) { |
| 2040 | return status; |
| 2041 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | mbedtls_mpi_init(&actual); |
| 2044 | mbedtls_mpi_init(&required); |
| 2045 | ret = mbedtls_rsa_export(rsa, |
| 2046 | NULL, NULL, NULL, NULL, &actual); |
| 2047 | mbedtls_rsa_free(rsa); |
| 2048 | mbedtls_free(rsa); |
| 2049 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2050 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | } |
| 2052 | ret = mbedtls_mpi_read_binary(&required, |
| 2053 | attributes->domain_parameters, |
| 2054 | attributes->domain_parameters_size); |
| 2055 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2056 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | } |
| 2058 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2059 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | } |
| 2061 | rsa_exit: |
| 2062 | mbedtls_mpi_free(&actual); |
| 2063 | mbedtls_mpi_free(&required); |
| 2064 | if (ret != 0) { |
| 2065 | return mbedtls_to_psa_error(ret); |
| 2066 | } |
| 2067 | } else |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2068 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 2069 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2070 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2071 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | if (attributes->core.bits != 0) { |
| 2077 | if (attributes->core.bits != slot->attr.bits) { |
| 2078 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2079 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2080 | } |
| 2081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2083 | } |
| 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2086 | const uint8_t *data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | size_t data_length, |
| 2088 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2089 | { |
| 2090 | psa_status_t status; |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2091 | LOCAL_INPUT_DECLARE(data_external, data); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2092 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2093 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2094 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2095 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2096 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2097 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2098 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2099 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2100 | * This also rejects any key which might be encoded as an empty string, |
| 2101 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | if (data_length == 0) { |
| 2103 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2104 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2105 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2106 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | if (data_length > SIZE_MAX / 8) { |
| 2108 | return PSA_ERROR_NOT_SUPPORTED; |
| 2109 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2110 | |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2111 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2114 | &slot, &driver); |
| 2115 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2116 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2118 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2119 | /* 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] | 2120 | * storage ( thus not in the case of importing a key in a secure element |
| 2121 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 2122 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | if (slot->key.data == NULL) { |
| 2124 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2125 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | attributes, data, data_length, &storage_size); |
| 2127 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2128 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2130 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 2132 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2133 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2135 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2136 | |
| 2137 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | status = psa_driver_wrapper_import_key(attributes, |
| 2139 | data, data_length, |
| 2140 | slot->key.data, |
| 2141 | slot->key.bytes, |
| 2142 | &slot->key.bytes, &bits); |
| 2143 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2144 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2147 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2148 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2150 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2151 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2152 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2153 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2154 | /* Enforce a size limit, and in particular ensure that the bit |
| 2155 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2157 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2158 | goto exit; |
| 2159 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | status = psa_validate_optional_attributes(slot, attributes); |
| 2161 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2162 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2166 | exit: |
Ryan Everett | f028fe1 | 2024-01-08 17:14:44 +0000 | [diff] [blame] | 2167 | LOCAL_INPUT_FREE(data_external, data); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2168 | if (status != PSA_SUCCESS) { |
| 2169 | psa_fail_key_creation(slot, driver); |
| 2170 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2173 | } |
| 2174 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2175 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2176 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2178 | { |
| 2179 | psa_status_t status; |
| 2180 | psa_key_slot_t *slot = NULL; |
| 2181 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2183 | |
| 2184 | /* Leaving attributes unspecified is not currently supported. |
| 2185 | * It could make sense to query the key type and size from the |
| 2186 | * secure element, but not all secure elements support this |
| 2187 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2189 | return PSA_ERROR_NOT_SUPPORTED; |
| 2190 | } |
| 2191 | if (psa_get_key_bits(attributes) == 0) { |
| 2192 | return PSA_ERROR_NOT_SUPPORTED; |
| 2193 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2196 | &slot, &driver); |
| 2197 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2198 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2202 | |
| 2203 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | if (status != PSA_SUCCESS) { |
| 2205 | psa_fail_key_creation(slot, driver); |
| 2206 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2208 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | psa_close_key(key); |
| 2210 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2211 | } |
| 2212 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2215 | const psa_key_attributes_t *specified_attributes, |
| 2216 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2217 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2218 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2219 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2220 | psa_key_slot_t *source_slot = NULL; |
| 2221 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2222 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2223 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2224 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2225 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2226 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2227 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2228 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2230 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2231 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | status = psa_validate_optional_attributes(source_slot, |
| 2235 | specified_attributes); |
| 2236 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2237 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2239 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2240 | /* The target key type and number of bits have been validated by |
| 2241 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2242 | * equal to the ones of the source key. So it is safe to inherit |
| 2243 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2244 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2245 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2246 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2247 | |
| 2248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2250 | &actual_attributes.core.policy, |
| 2251 | &source_slot->attr.policy); |
| 2252 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2253 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2254 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2257 | &target_slot, &driver); |
| 2258 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2259 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | } |
| 2261 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2262 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2263 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2264 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2265 | * the source key would need to be exported as plaintext and re-imported |
| 2266 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2267 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2268 | * appropriate API invocations from the application, if needed. |
| 2269 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2270 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2271 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2272 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2273 | /* |
| 2274 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2275 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2276 | * - For opaque keys this translates to an invocation of the drivers' |
| 2277 | * copy_key entry point through the dispatch layer. |
| 2278 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2280 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2281 | &storage_size); |
| 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 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2287 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2288 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2292 | source_slot->key.data, |
| 2293 | source_slot->key.bytes, |
| 2294 | target_slot->key.data, |
| 2295 | target_slot->key.bytes, |
| 2296 | &target_slot->key.bytes); |
| 2297 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2298 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | } |
| 2300 | } else { |
| 2301 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2302 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | source_slot->key.bytes); |
| 2304 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2305 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2306 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2307 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2309 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | if (status != PSA_SUCCESS) { |
| 2311 | psa_fail_key_creation(target_slot, driver); |
| 2312 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2313 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2314 | unlock_status = psa_unregister_read(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2319 | |
| 2320 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2321 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2322 | /* Message digests */ |
| 2323 | /****************************************************************/ |
| 2324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2326 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2327 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2328 | if (operation->id == 0) { |
| 2329 | return PSA_SUCCESS; |
| 2330 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2333 | operation->id = 0; |
| 2334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2339 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2340 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2341 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2342 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2343 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2345 | status = PSA_ERROR_BAD_STATE; |
| 2346 | goto exit; |
| 2347 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2350 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2351 | goto exit; |
| 2352 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2353 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2354 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2355 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2359 | |
| 2360 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | if (status != PSA_SUCCESS) { |
| 2362 | psa_hash_abort(operation); |
| 2363 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2364 | |
| 2365 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2369 | const uint8_t *input, |
| 2370 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2371 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2372 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2375 | status = PSA_ERROR_BAD_STATE; |
| 2376 | goto exit; |
| 2377 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2378 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2379 | /* Don't require hash implementations to behave correctly on a |
| 2380 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | if (input_length == 0) { |
| 2382 | return PSA_SUCCESS; |
| 2383 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2386 | |
| 2387 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | if (status != PSA_SUCCESS) { |
| 2389 | psa_hash_abort(operation); |
| 2390 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2393 | } |
| 2394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2396 | uint8_t *hash, |
| 2397 | size_t hash_size, |
| 2398 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2399 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2400 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | if (operation->id == 0) { |
| 2402 | return PSA_ERROR_BAD_STATE; |
| 2403 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2404 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2405 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | operation, hash, hash_size, hash_length); |
| 2407 | psa_hash_abort(operation); |
| 2408 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2409 | } |
| 2410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2412 | const uint8_t *hash, |
| 2413 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2414 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2415 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2416 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2417 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | operation, |
| 2419 | actual_hash, sizeof(actual_hash), |
| 2420 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2423 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2427 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2428 | goto exit; |
| 2429 | } |
| 2430 | |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2431 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2432 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2434 | |
| 2435 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2437 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2438 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2439 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2441 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2445 | const uint8_t *input, size_t input_length, |
| 2446 | uint8_t *hash, size_t hash_size, |
| 2447 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2448 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2449 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2451 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2452 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2455 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2456 | } |
| 2457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2459 | const uint8_t *input, size_t input_length, |
| 2460 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2461 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2462 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2463 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2466 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2467 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2468 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2469 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | alg, input, input_length, |
| 2471 | actual_hash, sizeof(actual_hash), |
| 2472 | &actual_hash_length); |
| 2473 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2474 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | } |
| 2476 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2477 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2478 | goto exit; |
| 2479 | } |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2480 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2481 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2482 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2483 | |
| 2484 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2486 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2487 | } |
| 2488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2490 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2491 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2492 | if (source_operation->id == 0 || |
| 2493 | target_operation->id != 0) { |
| 2494 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2495 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2498 | target_operation); |
| 2499 | if (status != PSA_SUCCESS) { |
| 2500 | psa_hash_abort(target_operation); |
| 2501 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2503 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2504 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2505 | |
| 2506 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2507 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2508 | /* MAC */ |
| 2509 | /****************************************************************/ |
| 2510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2512 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2513 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | if (operation->id == 0) { |
| 2515 | return PSA_SUCCESS; |
| 2516 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2519 | operation->mac_size = 0; |
| 2520 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2521 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2526 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2527 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2528 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2530 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2531 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2532 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2533 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2536 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2537 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2538 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2539 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | status = psa_mac_key_can_do(alg, key_type); |
| 2541 | if (status != PSA_SUCCESS) { |
| 2542 | return status; |
| 2543 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2544 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2545 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2549 | /* A very short MAC is too short for security since it can be |
| 2550 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2551 | * so we make this our minimum, even though 32 bits is still |
| 2552 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2554 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2557 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2558 | /* It's impossible to "truncate" to a larger length than the full length |
| 2559 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2560 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2561 | } |
| 2562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2564 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2565 | * that is disabled in the compile-time configuration. The result can |
| 2566 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2567 | * configuration into account. In this case, force a return of |
| 2568 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2569 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2570 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2571 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2572 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2580 | mbedtls_svc_key_id_t key, |
| 2581 | psa_algorithm_t alg, |
| 2582 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2583 | { |
| 2584 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2585 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2586 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2587 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2588 | |
| 2589 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2591 | status = PSA_ERROR_BAD_STATE; |
| 2592 | goto exit; |
| 2593 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2594 | |
| 2595 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | key, |
| 2597 | &slot, |
| 2598 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2599 | alg); |
| 2600 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2601 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2603 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2604 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | .core = slot->attr |
| 2606 | }; |
| 2607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2608 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2609 | &operation->mac_size); |
| 2610 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2611 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2613 | |
| 2614 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2615 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2616 | if (is_sign) { |
| 2617 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2618 | &attributes, |
| 2619 | slot->key.data, |
| 2620 | slot->key.bytes, |
| 2621 | alg); |
| 2622 | } else { |
| 2623 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2624 | &attributes, |
| 2625 | slot->key.data, |
| 2626 | slot->key.bytes, |
| 2627 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2628 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2629 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2630 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | if (status != PSA_SUCCESS) { |
| 2632 | psa_mac_abort(operation); |
| 2633 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2634 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2635 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2641 | mbedtls_svc_key_id_t key, |
| 2642 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2643 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2648 | mbedtls_svc_key_id_t key, |
| 2649 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2650 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2655 | const uint8_t *input, |
| 2656 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2657 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | if (operation->id == 0) { |
| 2659 | return PSA_ERROR_BAD_STATE; |
| 2660 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2661 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2662 | /* Don't require hash implementations to behave correctly on a |
| 2663 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | if (input_length == 0) { |
| 2665 | return PSA_SUCCESS; |
| 2666 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2669 | input, input_length); |
| 2670 | if (status != PSA_SUCCESS) { |
| 2671 | psa_mac_abort(operation); |
| 2672 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | } |
| 2676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2678 | uint8_t *mac, |
| 2679 | size_t mac_size, |
| 2680 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2681 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2682 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2683 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2686 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2687 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2688 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2691 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2692 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2693 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2694 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2695 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2696 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2698 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2699 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2700 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2703 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2704 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2705 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2708 | mac, operation->mac_size, |
| 2709 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2710 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2711 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2712 | /* In case of success, set the potential excess room in the output buffer |
| 2713 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2714 | * In case of error, set the output length and content to a safe default, |
| 2715 | * such that in case the caller misses an error check, the output would be |
| 2716 | * an unachievable MAC. |
| 2717 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2719 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2720 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2721 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2722 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2723 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2725 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2727 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2730 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2731 | const uint8_t *mac, |
| 2732 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2733 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2734 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2735 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2738 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2739 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2740 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2742 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2743 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2744 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2745 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2747 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2748 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2749 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2753 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2754 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2755 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2758 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2759 | } |
| 2760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2762 | psa_algorithm_t alg, |
| 2763 | const uint8_t *input, |
| 2764 | size_t input_length, |
| 2765 | uint8_t *mac, |
| 2766 | size_t mac_size, |
| 2767 | size_t *mac_length, |
| 2768 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2769 | { |
| 2770 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2771 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2772 | psa_key_slot_t *slot; |
| 2773 | uint8_t operation_mac_size = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2774 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2775 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2776 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | key, |
| 2778 | &slot, |
| 2779 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2780 | alg); |
| 2781 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2782 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2784 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2785 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2786 | .core = slot->attr |
| 2787 | }; |
| 2788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2790 | &operation_mac_size); |
| 2791 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2792 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2793 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2796 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2797 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | &attributes, |
| 2802 | slot->key.data, slot->key.bytes, |
| 2803 | alg, |
| 2804 | input, input_length, |
| 2805 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2806 | |
| 2807 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2808 | /* In case of success, set the potential excess room in the output buffer |
| 2809 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2810 | * In case of error, set the output length and content to a safe default, |
| 2811 | * such that in case the caller misses an error check, the output would be |
| 2812 | * an unachievable MAC. |
| 2813 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2815 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2816 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2817 | } |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2818 | |
| 2819 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2820 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2821 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2823 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2826 | 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] | 2827 | psa_algorithm_t alg, |
| 2828 | const uint8_t *input, |
| 2829 | size_t input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | uint8_t *mac, |
| 2831 | size_t mac_size, |
| 2832 | size_t *mac_length) |
| 2833 | { |
| 2834 | return psa_mac_compute_internal(key, alg, |
| 2835 | input, input_length, |
| 2836 | mac, mac_size, mac_length, 1); |
| 2837 | } |
| 2838 | |
| 2839 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2840 | psa_algorithm_t alg, |
| 2841 | const uint8_t *input, |
| 2842 | size_t input_length, |
| 2843 | const uint8_t *mac, |
| 2844 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2845 | { |
| 2846 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2847 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2848 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | status = psa_mac_compute_internal(key, alg, |
| 2851 | input, input_length, |
| 2852 | actual_mac, sizeof(actual_mac), |
| 2853 | &actual_mac_length, 0); |
| 2854 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2855 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2859 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2860 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2861 | } |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2862 | if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2863 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2864 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2865 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2866 | |
| 2867 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2871 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2872 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2873 | /****************************************************************/ |
| 2874 | /* Asymmetric cryptography */ |
| 2875 | /****************************************************************/ |
| 2876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2878 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2879 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | if (input_is_message) { |
| 2881 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2882 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2883 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2886 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2887 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2888 | } |
| 2889 | } |
| 2890 | } else { |
| 2891 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2892 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2893 | } |
| 2894 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2897 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2900 | int input_is_message, |
| 2901 | psa_algorithm_t alg, |
| 2902 | const uint8_t *input, |
| 2903 | size_t input_length, |
| 2904 | uint8_t *signature, |
| 2905 | size_t signature_size, |
| 2906 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2907 | { |
| 2908 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2909 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2910 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2911 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2912 | |
| 2913 | *signature_length = 0; |
| 2914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2916 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2917 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2919 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2920 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2921 | * 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] | 2922 | * buffer can in principle be empty since it doesn't actually have |
| 2923 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | if (signature_size == 0) { |
| 2925 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2926 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2927 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2928 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | key, &slot, |
| 2930 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2931 | PSA_KEY_USAGE_SIGN_HASH, |
| 2932 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2935 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2936 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2939 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2940 | goto exit; |
| 2941 | } |
| 2942 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2943 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2945 | }; |
| 2946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2948 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2949 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2950 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | signature, signature_size, signature_length); |
| 2952 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2953 | |
| 2954 | status = psa_driver_wrapper_sign_hash( |
| 2955 | &attributes, slot->key.data, slot->key.bytes, |
| 2956 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2960 | |
| 2961 | exit: |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 2962 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 2963 | *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2964 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 2965 | unlock_status = psa_unregister_read(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2968 | } |
| 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2971 | int input_is_message, |
| 2972 | psa_algorithm_t alg, |
| 2973 | const uint8_t *input, |
| 2974 | size_t input_length, |
| 2975 | const uint8_t *signature, |
| 2976 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2977 | { |
| 2978 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2979 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2980 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2983 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2984 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2986 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2987 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | key, &slot, |
| 2989 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2990 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2991 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | if (status != PSA_SUCCESS) { |
| 2994 | return status; |
| 2995 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2996 | |
| 2997 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2999 | }; |
| 3000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3001 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3002 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3003 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3004 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | signature, signature_length); |
| 3006 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3007 | status = psa_driver_wrapper_verify_hash( |
| 3008 | &attributes, slot->key.data, slot->key.bytes, |
| 3009 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3011 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3012 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3013 | unlock_status = psa_unregister_read(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3016 | |
| 3017 | } |
| 3018 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3019 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3020 | const psa_key_attributes_t *attributes, |
| 3021 | const uint8_t *key_buffer, |
| 3022 | size_t key_buffer_size, |
| 3023 | psa_algorithm_t alg, |
| 3024 | const uint8_t *input, |
| 3025 | size_t input_length, |
| 3026 | uint8_t *signature, |
| 3027 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3029 | { |
| 3030 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3033 | size_t hash_length; |
| 3034 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3035 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3036 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3037 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3038 | input, input_length, |
| 3039 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3042 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3043 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3044 | |
| 3045 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | attributes, key_buffer, key_buffer_size, |
| 3047 | alg, hash, hash_length, |
| 3048 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3049 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3052 | } |
| 3053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3054 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3055 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3056 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3058 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | size_t signature_size, |
| 3060 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3061 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3062 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3063 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3064 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3065 | |
| 3066 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3067 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3068 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3069 | signature_size, signature_length); |
| 3070 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3071 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3072 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3073 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3074 | LOCAL_INPUT_FREE(input_external, input); |
| 3075 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3076 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3077 | } |
| 3078 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3079 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3080 | const psa_key_attributes_t *attributes, |
| 3081 | const uint8_t *key_buffer, |
| 3082 | size_t key_buffer_size, |
| 3083 | psa_algorithm_t alg, |
| 3084 | const uint8_t *input, |
| 3085 | size_t input_length, |
| 3086 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3088 | { |
| 3089 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3092 | size_t hash_length; |
| 3093 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3094 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3095 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3097 | input, input_length, |
| 3098 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3101 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3103 | |
| 3104 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | attributes, key_buffer, key_buffer_size, |
| 3106 | alg, hash, hash_length, |
| 3107 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3108 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3110 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3111 | } |
| 3112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3114 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3115 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3117 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3119 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3120 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3121 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3122 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3123 | |
| 3124 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3125 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3126 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3127 | signature_length); |
| 3128 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3129 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3130 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3131 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3132 | LOCAL_INPUT_FREE(input_external, input); |
| 3133 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3134 | |
| 3135 | return status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3138 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3139 | const psa_key_attributes_t *attributes, |
| 3140 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3141 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3142 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3143 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3145 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3146 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3147 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3149 | return mbedtls_psa_rsa_sign_hash( |
| 3150 | attributes, |
| 3151 | key_buffer, key_buffer_size, |
| 3152 | alg, hash, hash_length, |
| 3153 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3154 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3155 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | } else { |
| 3157 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3158 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3160 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3161 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3162 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3163 | return mbedtls_psa_ecdsa_sign_hash( |
| 3164 | attributes, |
| 3165 | key_buffer, key_buffer_size, |
| 3166 | alg, hash, hash_length, |
| 3167 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3168 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3169 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | } else { |
| 3171 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3172 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3173 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | (void) key_buffer; |
| 3176 | (void) key_buffer_size; |
| 3177 | (void) hash; |
| 3178 | (void) hash_length; |
| 3179 | (void) signature; |
| 3180 | (void) signature_size; |
| 3181 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3183 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3184 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3187 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3188 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3190 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | size_t signature_size, |
| 3192 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3193 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3194 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3195 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3196 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3197 | |
| 3198 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3199 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3200 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3201 | signature_size, signature_length); |
| 3202 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3203 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3204 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3205 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3206 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3207 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3208 | |
| 3209 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3210 | } |
| 3211 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3212 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3213 | const psa_key_attributes_t *attributes, |
| 3214 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3215 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3217 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3219 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3220 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3221 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3223 | return mbedtls_psa_rsa_verify_hash( |
| 3224 | attributes, |
| 3225 | key_buffer, key_buffer_size, |
| 3226 | alg, hash, hash_length, |
| 3227 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3228 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3229 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | } else { |
| 3231 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3232 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3234 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3235 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3237 | return mbedtls_psa_ecdsa_verify_hash( |
| 3238 | attributes, |
| 3239 | key_buffer, key_buffer_size, |
| 3240 | alg, hash, hash_length, |
| 3241 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3242 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3243 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3244 | } else { |
| 3245 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3246 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3247 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | (void) key_buffer; |
| 3250 | (void) key_buffer_size; |
| 3251 | (void) hash; |
| 3252 | (void) hash_length; |
| 3253 | (void) signature; |
| 3254 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3256 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3260 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3261 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3262 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3263 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3265 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3266 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3267 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3268 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3269 | |
| 3270 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3271 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3272 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3273 | signature_length); |
| 3274 | |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3275 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3276 | exit: |
Thomas Daubney | 3e65f52 | 2024-01-30 12:37:25 +0000 | [diff] [blame] | 3277 | #endif |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3278 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3279 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3280 | |
| 3281 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3282 | } |
| 3283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3284 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3285 | psa_algorithm_t alg, |
| 3286 | const uint8_t *input, |
| 3287 | size_t input_length, |
| 3288 | const uint8_t *salt, |
| 3289 | size_t salt_length, |
| 3290 | uint8_t *output, |
| 3291 | size_t output_size, |
| 3292 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3293 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3295 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3296 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3297 | psa_key_attributes_t attributes; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3298 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3299 | (void) input; |
| 3300 | (void) input_length; |
| 3301 | (void) salt; |
| 3302 | (void) output; |
| 3303 | (void) output_size; |
| 3304 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3305 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3307 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3308 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3309 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3310 | |
Valerio Setti | 5bb454a | 2024-01-15 10:43:16 +0100 | [diff] [blame] | 3311 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3313 | if (status != PSA_SUCCESS) { |
| 3314 | return status; |
| 3315 | } |
| 3316 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3317 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3318 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3319 | goto exit; |
| 3320 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3321 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3322 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3324 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3325 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3326 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3327 | &attributes, slot->key.data, slot->key.bytes, |
| 3328 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3330 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3331 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3334 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3337 | psa_algorithm_t alg, |
| 3338 | const uint8_t *input, |
| 3339 | size_t input_length, |
| 3340 | const uint8_t *salt, |
| 3341 | size_t salt_length, |
| 3342 | uint8_t *output, |
| 3343 | size_t output_size, |
| 3344 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3345 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3346 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3347 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3348 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3349 | psa_key_attributes_t attributes; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3350 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3351 | (void) input; |
| 3352 | (void) input_length; |
| 3353 | (void) salt; |
| 3354 | (void) output; |
| 3355 | (void) output_size; |
| 3356 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3357 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3360 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3361 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3362 | |
Valerio Setti | 5bb454a | 2024-01-15 10:43:16 +0100 | [diff] [blame] | 3363 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3365 | if (status != PSA_SUCCESS) { |
| 3366 | return status; |
| 3367 | } |
| 3368 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3369 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3370 | goto exit; |
| 3371 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3372 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3373 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3375 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3376 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3377 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3378 | &attributes, slot->key.data, slot->key.bytes, |
| 3379 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3381 | |
| 3382 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3383 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3386 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3387 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3388 | /****************************************************************/ |
| 3389 | /* Asymmetric interruptible cryptography */ |
| 3390 | /****************************************************************/ |
| 3391 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3392 | static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3393 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3394 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3395 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3396 | psa_interruptible_max_ops = max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | uint32_t psa_interruptible_get_max_ops(void) |
| 3400 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3401 | return psa_interruptible_max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3404 | uint32_t psa_sign_hash_get_num_ops( |
| 3405 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3406 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3407 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | uint32_t psa_verify_hash_get_num_ops( |
| 3411 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3412 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3413 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3416 | static psa_status_t psa_sign_hash_abort_internal( |
| 3417 | psa_sign_hash_interruptible_operation_t *operation) |
| 3418 | { |
| 3419 | if (operation->id == 0) { |
| 3420 | /* The object has (apparently) been initialized but it is not (yet) |
| 3421 | * in use. It's ok to call abort on such an object, and there's |
| 3422 | * nothing to do. */ |
| 3423 | return PSA_SUCCESS; |
| 3424 | } |
| 3425 | |
| 3426 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3427 | |
| 3428 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3429 | |
| 3430 | operation->id = 0; |
| 3431 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3432 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3433 | * only want to be cleared by the application calling abort, not by abort |
| 3434 | * being called at completion of an operation. */ |
| 3435 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3436 | return status; |
| 3437 | } |
| 3438 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3439 | psa_status_t psa_sign_hash_start( |
| 3440 | psa_sign_hash_interruptible_operation_t *operation, |
| 3441 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3442 | const uint8_t *hash, size_t hash_length) |
| 3443 | { |
| 3444 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3445 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3446 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3447 | psa_key_attributes_t attributes; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3448 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3449 | /* Check that start has not been previously called, or operation has not |
| 3450 | * previously errored. */ |
| 3451 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3452 | return PSA_ERROR_BAD_STATE; |
| 3453 | } |
| 3454 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3455 | status = psa_sign_verify_check_alg(0, alg); |
| 3456 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3457 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3458 | return status; |
| 3459 | } |
| 3460 | |
| 3461 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3462 | PSA_KEY_USAGE_SIGN_HASH, |
| 3463 | alg); |
| 3464 | |
| 3465 | if (status != PSA_SUCCESS) { |
| 3466 | goto exit; |
| 3467 | } |
| 3468 | |
| 3469 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3470 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3471 | goto exit; |
| 3472 | } |
| 3473 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3474 | attributes = (psa_key_attributes_t) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3475 | .core = slot->attr |
| 3476 | }; |
| 3477 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3478 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3479 | operation->num_ops = 0; |
| 3480 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3481 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3482 | slot->key.data, |
| 3483 | slot->key.bytes, alg, |
| 3484 | hash, hash_length); |
| 3485 | exit: |
| 3486 | |
| 3487 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3488 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3489 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3492 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3493 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3494 | if (unlock_status != PSA_SUCCESS) { |
| 3495 | operation->error_occurred = 1; |
| 3496 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3497 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3498 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | |
| 3502 | psa_status_t psa_sign_hash_complete( |
| 3503 | psa_sign_hash_interruptible_operation_t *operation, |
| 3504 | uint8_t *signature, size_t signature_size, |
| 3505 | size_t *signature_length) |
| 3506 | { |
| 3507 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3508 | |
| 3509 | *signature_length = 0; |
| 3510 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3511 | /* Check that start has been called first, and that operation has not |
| 3512 | * previously errored. */ |
| 3513 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3514 | status = PSA_ERROR_BAD_STATE; |
| 3515 | goto exit; |
| 3516 | } |
| 3517 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3518 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3519 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3520 | if (signature_size == 0) { |
| 3521 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3522 | goto exit; |
| 3523 | } |
| 3524 | |
| 3525 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3526 | signature_size, |
| 3527 | signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3528 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3529 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3530 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3531 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3532 | exit: |
| 3533 | |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3534 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3535 | *signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3536 | |
Paul Elliott | 53bb312 | 2023-02-10 14:22:22 +0000 | [diff] [blame] | 3537 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3538 | if (status != PSA_SUCCESS) { |
| 3539 | operation->error_occurred = 1; |
| 3540 | } |
| 3541 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3542 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | return status; |
| 3546 | } |
| 3547 | |
| 3548 | psa_status_t psa_sign_hash_abort( |
| 3549 | psa_sign_hash_interruptible_operation_t *operation) |
| 3550 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3551 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3552 | |
| 3553 | status = psa_sign_hash_abort_internal(operation); |
| 3554 | |
| 3555 | /* We clear the number of ops done here, so that it is not cleared when |
| 3556 | * the operation fails or succeeds, only on manual abort. */ |
| 3557 | operation->num_ops = 0; |
| 3558 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3559 | /* Likewise, failure state. */ |
| 3560 | operation->error_occurred = 0; |
| 3561 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3562 | return status; |
| 3563 | } |
| 3564 | |
| 3565 | static psa_status_t psa_verify_hash_abort_internal( |
| 3566 | psa_verify_hash_interruptible_operation_t *operation) |
| 3567 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3568 | if (operation->id == 0) { |
| 3569 | /* The object has (apparently) been initialized but it is not (yet) |
| 3570 | * in use. It's ok to call abort on such an object, and there's |
| 3571 | * nothing to do. */ |
| 3572 | return PSA_SUCCESS; |
| 3573 | } |
| 3574 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3575 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3576 | |
| 3577 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3578 | |
| 3579 | operation->id = 0; |
| 3580 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3581 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3582 | * only want to be cleared by the application calling abort, not by abort |
| 3583 | * being called at completion of an operation. */ |
| 3584 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3585 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | psa_status_t psa_verify_hash_start( |
| 3589 | psa_verify_hash_interruptible_operation_t *operation, |
| 3590 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3591 | const uint8_t *hash, size_t hash_length, |
| 3592 | const uint8_t *signature, size_t signature_length) |
| 3593 | { |
| 3594 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3595 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3596 | psa_key_slot_t *slot; |
| 3597 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3598 | /* Check that start has not been previously called, or operation has not |
| 3599 | * previously errored. */ |
| 3600 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3601 | return PSA_ERROR_BAD_STATE; |
| 3602 | } |
| 3603 | |
| 3604 | status = psa_sign_verify_check_alg(0, alg); |
| 3605 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3606 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3607 | return status; |
| 3608 | } |
| 3609 | |
| 3610 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3611 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3612 | alg); |
| 3613 | |
| 3614 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3615 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3616 | return status; |
| 3617 | } |
| 3618 | |
| 3619 | psa_key_attributes_t attributes = { |
| 3620 | .core = slot->attr |
| 3621 | }; |
| 3622 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3623 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3624 | operation->num_ops = 0; |
| 3625 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3626 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3627 | slot->key.data, |
| 3628 | slot->key.bytes, |
| 3629 | alg, hash, hash_length, |
| 3630 | signature, signature_length); |
| 3631 | |
| 3632 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3633 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3634 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 3637 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3638 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3639 | if (unlock_status != PSA_SUCCESS) { |
| 3640 | operation->error_occurred = 1; |
| 3641 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3642 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3643 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | psa_status_t psa_verify_hash_complete( |
| 3647 | psa_verify_hash_interruptible_operation_t *operation) |
| 3648 | { |
| 3649 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3650 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3651 | /* Check that start has been called first, and that operation has not |
| 3652 | * previously errored. */ |
| 3653 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3654 | status = PSA_ERROR_BAD_STATE; |
| 3655 | goto exit; |
| 3656 | } |
| 3657 | |
| 3658 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3659 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3660 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3661 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3662 | operation); |
| 3663 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3664 | exit: |
| 3665 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3666 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3667 | if (status != PSA_SUCCESS) { |
| 3668 | operation->error_occurred = 1; |
| 3669 | } |
| 3670 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3671 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | return status; |
| 3675 | } |
| 3676 | |
| 3677 | psa_status_t psa_verify_hash_abort( |
| 3678 | psa_verify_hash_interruptible_operation_t *operation) |
| 3679 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3680 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3681 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3682 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3683 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3684 | /* We clear the number of ops done here, so that it is not cleared when |
| 3685 | * the operation fails or succeeds, only on manual abort. */ |
| 3686 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3687 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3688 | /* Likewise, failure state. */ |
| 3689 | operation->error_occurred = 0; |
| 3690 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3691 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3694 | /****************************************************************/ |
| 3695 | /* Asymmetric interruptible cryptography internal */ |
| 3696 | /* implementations */ |
| 3697 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3698 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3699 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3700 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3701 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3702 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3703 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3704 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3705 | |
| 3706 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3707 | * therefore avoid this value, and set to minimum possible. */ |
| 3708 | if (max_ops == 0) { |
| 3709 | max_ops = 1; |
| 3710 | } |
| 3711 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3712 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3713 | #else |
| 3714 | (void) max_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3715 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3716 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3717 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3718 | } |
| 3719 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3720 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3721 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3722 | { |
| 3723 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3724 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3725 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3726 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3727 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3728 | #else |
| 3729 | (void) operation; |
| 3730 | return 0; |
| 3731 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3732 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3733 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3734 | } |
| 3735 | |
| 3736 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3737 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3738 | { |
| 3739 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3740 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3741 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3742 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3743 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3744 | #else |
| 3745 | (void) operation; |
| 3746 | return 0; |
| 3747 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3748 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3749 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3750 | } |
| 3751 | |
| 3752 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3753 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3754 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3755 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3756 | const uint8_t *hash, size_t hash_length) |
| 3757 | { |
| 3758 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3759 | size_t required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3760 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3761 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3762 | return PSA_ERROR_NOT_SUPPORTED; |
| 3763 | } |
| 3764 | |
| 3765 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3766 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3767 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3768 | |
| 3769 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3770 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3771 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3772 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3773 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3774 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3775 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3776 | operation->num_ops = 0; |
| 3777 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3778 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3779 | attributes->core.bits, |
| 3780 | key_buffer, |
| 3781 | key_buffer_size, |
| 3782 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3783 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3784 | if (status != PSA_SUCCESS) { |
| 3785 | return status; |
| 3786 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3787 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3788 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3789 | operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3790 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3791 | 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] | 3792 | operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3793 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3794 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3795 | /* We only need to store the same length of hash as the private key size |
| 3796 | * here, it would be truncated by the internal implementation anyway. */ |
| 3797 | required_hash_length = (hash_length < operation->coordinate_bytes ? |
| 3798 | hash_length : operation->coordinate_bytes); |
| 3799 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3800 | if (required_hash_length > sizeof(operation->hash)) { |
| 3801 | /* Shouldn't happen, but better safe than sorry. */ |
| 3802 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3803 | } |
| 3804 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3805 | memcpy(operation->hash, hash, required_hash_length); |
| 3806 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3807 | |
| 3808 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3809 | |
| 3810 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3811 | (void) operation; |
| 3812 | (void) key_buffer; |
| 3813 | (void) key_buffer_size; |
| 3814 | (void) alg; |
| 3815 | (void) hash; |
| 3816 | (void) hash_length; |
| 3817 | (void) status; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3818 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3819 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3820 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3821 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3822 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3823 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3827 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3828 | uint8_t *signature, size_t signature_size, |
| 3829 | size_t *signature_length) |
| 3830 | { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3831 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3832 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3833 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3834 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3835 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3836 | mbedtls_mpi r; |
| 3837 | mbedtls_mpi s; |
| 3838 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3839 | mbedtls_mpi_init(&r); |
| 3840 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3841 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3842 | /* Ensure max_ops is set to the current value (or default). */ |
| 3843 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 3844 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3845 | if (signature_size < 2 * operation->coordinate_bytes) { |
| 3846 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3847 | goto exit; |
| 3848 | } |
| 3849 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3850 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3851 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3852 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3853 | status = mbedtls_to_psa_error( |
| 3854 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3855 | &r, |
| 3856 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3857 | &operation->ctx->d, |
| 3858 | operation->hash, |
| 3859 | operation->hash_length, |
| 3860 | operation->md_alg, |
| 3861 | mbedtls_psa_get_random, |
| 3862 | MBEDTLS_PSA_RANDOM_STATE, |
| 3863 | &operation->restart_ctx)); |
| 3864 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3865 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3866 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3867 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3868 | } else { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3869 | status = mbedtls_to_psa_error( |
| 3870 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3871 | &r, |
| 3872 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3873 | &operation->ctx->d, |
| 3874 | operation->hash, |
| 3875 | operation->hash_length, |
| 3876 | mbedtls_psa_get_random, |
| 3877 | MBEDTLS_PSA_RANDOM_STATE, |
| 3878 | mbedtls_psa_get_random, |
| 3879 | MBEDTLS_PSA_RANDOM_STATE, |
| 3880 | &operation->restart_ctx)); |
| 3881 | } |
| 3882 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3883 | /* Hide the fact that the restart context only holds a delta of number of |
| 3884 | * ops done during the last operation, not an absolute value. */ |
| 3885 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3886 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3887 | if (status == PSA_SUCCESS) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3888 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3889 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3890 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3891 | operation->coordinate_bytes) |
| 3892 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3893 | |
| 3894 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3895 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3899 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3900 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3901 | operation->coordinate_bytes, |
| 3902 | operation->coordinate_bytes) |
| 3903 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3904 | |
| 3905 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3906 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3909 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3910 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3911 | status = PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3912 | } |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3913 | |
| 3914 | exit: |
| 3915 | |
| 3916 | mbedtls_mpi_free(&r); |
| 3917 | mbedtls_mpi_free(&s); |
| 3918 | return status; |
| 3919 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3920 | #else |
| 3921 | |
| 3922 | (void) operation; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3923 | (void) signature; |
| 3924 | (void) signature_size; |
| 3925 | (void) signature_length; |
| 3926 | |
| 3927 | return PSA_ERROR_NOT_SUPPORTED; |
| 3928 | |
| 3929 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3930 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3931 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3932 | } |
| 3933 | |
| 3934 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 3935 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 3936 | { |
| 3937 | |
| 3938 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3939 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3940 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3941 | |
| 3942 | if (operation->ctx) { |
| 3943 | mbedtls_ecdsa_free(operation->ctx); |
| 3944 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3945 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3949 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3950 | operation->num_ops = 0; |
| 3951 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3952 | return PSA_SUCCESS; |
| 3953 | |
| 3954 | #else |
| 3955 | |
| 3956 | (void) operation; |
| 3957 | |
| 3958 | return PSA_ERROR_NOT_SUPPORTED; |
| 3959 | |
| 3960 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3961 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3962 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3963 | } |
| 3964 | |
| 3965 | psa_status_t mbedtls_psa_verify_hash_start( |
| 3966 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 3967 | const psa_key_attributes_t *attributes, |
| 3968 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3969 | psa_algorithm_t alg, |
| 3970 | const uint8_t *hash, size_t hash_length, |
| 3971 | const uint8_t *signature, size_t signature_length) |
| 3972 | { |
| 3973 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3974 | size_t coordinate_bytes = 0; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3975 | size_t required_hash_length = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3976 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3977 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3978 | return PSA_ERROR_NOT_SUPPORTED; |
| 3979 | } |
| 3980 | |
| 3981 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3982 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3983 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3984 | |
| 3985 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3986 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3987 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3988 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3989 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3990 | mbedtls_mpi_init(&operation->r); |
| 3991 | mbedtls_mpi_init(&operation->s); |
| 3992 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3993 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3994 | operation->num_ops = 0; |
| 3995 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3996 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3997 | attributes->core.bits, |
| 3998 | key_buffer, |
| 3999 | key_buffer_size, |
| 4000 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4001 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4002 | if (status != PSA_SUCCESS) { |
| 4003 | return status; |
| 4004 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4005 | |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 4006 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4007 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4008 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4009 | return PSA_ERROR_INVALID_SIGNATURE; |
| 4010 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4011 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4012 | status = mbedtls_to_psa_error( |
| 4013 | mbedtls_mpi_read_binary(&operation->r, |
| 4014 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4015 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4016 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4017 | if (status != PSA_SUCCESS) { |
| 4018 | return status; |
| 4019 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4020 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4021 | status = mbedtls_to_psa_error( |
| 4022 | mbedtls_mpi_read_binary(&operation->s, |
| 4023 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4024 | coordinate_bytes, |
| 4025 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4026 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4027 | if (status != PSA_SUCCESS) { |
| 4028 | return status; |
| 4029 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4030 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4031 | status = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4032 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4033 | if (status != PSA_SUCCESS) { |
| 4034 | return status; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4035 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4036 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4037 | /* We only need to store the same length of hash as the private key size |
| 4038 | * here, it would be truncated by the internal implementation anyway. */ |
| 4039 | required_hash_length = (hash_length < coordinate_bytes ? hash_length : |
| 4040 | coordinate_bytes); |
| 4041 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 4042 | if (required_hash_length > sizeof(operation->hash)) { |
| 4043 | /* Shouldn't happen, but better safe than sorry. */ |
| 4044 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4045 | } |
| 4046 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4047 | memcpy(operation->hash, hash, required_hash_length); |
| 4048 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4049 | |
| 4050 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4051 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4052 | (void) operation; |
| 4053 | (void) key_buffer; |
| 4054 | (void) key_buffer_size; |
| 4055 | (void) alg; |
| 4056 | (void) hash; |
| 4057 | (void) hash_length; |
| 4058 | (void) signature; |
| 4059 | (void) signature_length; |
| 4060 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 4061 | (void) coordinate_bytes; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4062 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4063 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4064 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4065 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4066 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4067 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 4071 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4072 | { |
| 4073 | |
| 4074 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4075 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4076 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4077 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4078 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4079 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 4080 | /* Ensure max_ops is set to the current value (or default). */ |
| 4081 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 4082 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4083 | status = mbedtls_to_psa_error( |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4084 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 4085 | operation->hash, |
| 4086 | operation->hash_length, |
| 4087 | &operation->ctx->Q, |
| 4088 | &operation->r, |
| 4089 | &operation->s, |
| 4090 | &operation->restart_ctx)); |
| 4091 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4092 | /* Hide the fact that the restart context only holds a delta of number of |
| 4093 | * ops done during the last operation, not an absolute value. */ |
| 4094 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 4095 | |
| 4096 | return status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4097 | #else |
| 4098 | (void) operation; |
| 4099 | |
| 4100 | return PSA_ERROR_NOT_SUPPORTED; |
| 4101 | |
| 4102 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4103 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4104 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4105 | } |
| 4106 | |
| 4107 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 4108 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4109 | { |
| 4110 | |
| 4111 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4112 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4113 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4114 | |
| 4115 | if (operation->ctx) { |
| 4116 | mbedtls_ecdsa_free(operation->ctx); |
| 4117 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 4118 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 4122 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4123 | operation->num_ops = 0; |
| 4124 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4125 | mbedtls_mpi_free(&operation->r); |
| 4126 | mbedtls_mpi_free(&operation->s); |
| 4127 | |
| 4128 | return PSA_SUCCESS; |
| 4129 | |
| 4130 | #else |
| 4131 | (void) operation; |
| 4132 | |
| 4133 | return PSA_ERROR_NOT_SUPPORTED; |
| 4134 | |
| 4135 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4136 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4137 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4138 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4139 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4140 | /****************************************************************/ |
| 4141 | /* Symmetric cryptography */ |
| 4142 | /****************************************************************/ |
| 4143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4144 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 4145 | mbedtls_svc_key_id_t key, |
| 4146 | psa_algorithm_t alg, |
| 4147 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4148 | { |
| 4149 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4150 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4151 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 4153 | PSA_KEY_USAGE_ENCRYPT : |
| 4154 | PSA_KEY_USAGE_DECRYPT); |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4155 | psa_key_attributes_t attributes; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4156 | |
| 4157 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4158 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4159 | status = PSA_ERROR_BAD_STATE; |
| 4160 | goto exit; |
| 4161 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4164 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4165 | goto exit; |
| 4166 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 4169 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4170 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4172 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4173 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4174 | * 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] | 4175 | * so we only set it (in the driver wrapper) after resources have been |
| 4176 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4177 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4179 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4181 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4182 | } |
| 4183 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4184 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4185 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4186 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 4187 | }; |
| 4188 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4189 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4190 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 4191 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 4192 | &attributes, |
| 4193 | slot->key.data, |
| 4194 | slot->key.bytes, |
| 4195 | alg); |
| 4196 | } else { |
| 4197 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 4198 | &attributes, |
| 4199 | slot->key.data, |
| 4200 | slot->key.bytes, |
| 4201 | alg); |
| 4202 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4203 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4204 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4205 | if (status != PSA_SUCCESS) { |
| 4206 | psa_cipher_abort(operation); |
| 4207 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4208 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4209 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4212 | } |
| 4213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4214 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 4215 | mbedtls_svc_key_id_t key, |
| 4216 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4217 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4218 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4219 | } |
| 4220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 4222 | mbedtls_svc_key_id_t key, |
| 4223 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4224 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 4229 | uint8_t *iv, |
| 4230 | size_t iv_size, |
| 4231 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4232 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4233 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4234 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 4235 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4238 | status = PSA_ERROR_BAD_STATE; |
| 4239 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4240 | } |
| 4241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4243 | status = PSA_ERROR_BAD_STATE; |
| 4244 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4245 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4246 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4247 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4248 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4249 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4250 | goto exit; |
| 4251 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4253 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4254 | status = PSA_ERROR_GENERIC_ERROR; |
| 4255 | goto exit; |
| 4256 | } |
| 4257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4258 | status = psa_generate_random(local_iv, default_iv_length); |
| 4259 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4260 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4264 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4265 | |
| 4266 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | if (status == PSA_SUCCESS) { |
| 4268 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4269 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4270 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4272 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4273 | psa_cipher_abort(operation); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4274 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4276 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4277 | } |
| 4278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4279 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 4280 | const uint8_t *iv, |
| 4281 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4282 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4283 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4285 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4286 | status = PSA_ERROR_BAD_STATE; |
| 4287 | goto exit; |
| 4288 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4291 | status = PSA_ERROR_BAD_STATE; |
| 4292 | goto exit; |
| 4293 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4295 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4296 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4297 | goto exit; |
| 4298 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4301 | iv, |
| 4302 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4303 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4304 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4305 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4306 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4307 | } else { |
| 4308 | psa_cipher_abort(operation); |
| 4309 | } |
| 4310 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 4314 | const uint8_t *input, |
| 4315 | size_t input_length, |
| 4316 | uint8_t *output, |
| 4317 | size_t output_size, |
| 4318 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4319 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4320 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4323 | status = PSA_ERROR_BAD_STATE; |
| 4324 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4325 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4328 | status = PSA_ERROR_BAD_STATE; |
| 4329 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4330 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | status = psa_driver_wrapper_cipher_update(operation, |
| 4333 | input, |
| 4334 | input_length, |
| 4335 | output, |
| 4336 | output_size, |
| 4337 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4338 | |
| 4339 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | if (status != PSA_SUCCESS) { |
| 4341 | psa_cipher_abort(operation); |
| 4342 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4344 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4345 | } |
| 4346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4347 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 4348 | uint8_t *output, |
| 4349 | size_t output_size, |
| 4350 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4351 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4352 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4354 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4355 | status = PSA_ERROR_BAD_STATE; |
| 4356 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4357 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4359 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4360 | status = PSA_ERROR_BAD_STATE; |
| 4361 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4362 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4364 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4365 | output, |
| 4366 | output_size, |
| 4367 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4368 | |
| 4369 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | if (status == PSA_SUCCESS) { |
| 4371 | return psa_cipher_abort(operation); |
| 4372 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4373 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4376 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4377 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4378 | } |
| 4379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4381 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4383 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4384 | * in use. It's ok to call abort on such an object, and there's |
| 4385 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4387 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4390 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4391 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4392 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4393 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4396 | } |
| 4397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4399 | psa_algorithm_t alg, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4400 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | size_t input_length, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4402 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | size_t output_size, |
| 4404 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4405 | { |
| 4406 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4407 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4408 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4409 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4410 | size_t default_iv_length = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4411 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4412 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4413 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4414 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4415 | |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4416 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4417 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
David Horstmann | c6977b4 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 4418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4420 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4421 | goto exit; |
| 4422 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4425 | PSA_KEY_USAGE_ENCRYPT, |
| 4426 | alg); |
| 4427 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4428 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4429 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4430 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4431 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4432 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4433 | }; |
| 4434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4436 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4437 | status = PSA_ERROR_GENERIC_ERROR; |
| 4438 | goto exit; |
| 4439 | } |
| 4440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | if (default_iv_length > 0) { |
| 4442 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4443 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4444 | goto exit; |
| 4445 | } |
| 4446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | status = psa_generate_random(local_iv, default_iv_length); |
| 4448 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4449 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4453 | status = psa_driver_wrapper_cipher_encrypt( |
| 4454 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4455 | alg, local_iv, default_iv_length, input, input_length, |
Ronald Cron | afbc7ed | 2023-01-24 16:02:04 +0100 | [diff] [blame] | 4456 | psa_crypto_buffer_offset(output, default_iv_length), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4458 | |
| 4459 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4460 | unlock_status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4462 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4463 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | if (status == PSA_SUCCESS) { |
| 4466 | if (default_iv_length > 0) { |
| 4467 | memcpy(output, local_iv, default_iv_length); |
| 4468 | } |
| 4469 | *output_length += default_iv_length; |
| 4470 | } else { |
| 4471 | *output_length = 0; |
| 4472 | } |
| 4473 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4474 | LOCAL_INPUT_FREE(input_external, input); |
| 4475 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | c6977b4 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 4476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4477 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4478 | } |
| 4479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4481 | psa_algorithm_t alg, |
| 4482 | const uint8_t *input, |
| 4483 | size_t input_length, |
| 4484 | uint8_t *output, |
| 4485 | size_t output_size, |
| 4486 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4487 | { |
| 4488 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4489 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4490 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4491 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4494 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4495 | goto exit; |
| 4496 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4499 | PSA_KEY_USAGE_DECRYPT, |
| 4500 | alg); |
| 4501 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4502 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4503 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4504 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4505 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4507 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4510 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4511 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4512 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | } 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] | 4514 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4515 | goto exit; |
| 4516 | } |
| 4517 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4518 | status = psa_driver_wrapper_cipher_decrypt( |
| 4519 | &attributes, slot->key.data, slot->key.bytes, |
| 4520 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4522 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4523 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4524 | unlock_status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4526 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4527 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4530 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4537 | /****************************************************************/ |
| 4538 | /* AEAD */ |
| 4539 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4540 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4541 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | 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] | 4543 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4544 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4549 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4550 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4554 | #if defined(PSA_WANT_ALG_GCM) |
| 4555 | case PSA_ALG_GCM: |
| 4556 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | * arbitrarily large nonces. Please note that we do not generally |
| 4558 | * recommend the usage of nonces of greater length than |
| 4559 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4560 | * size, which can then lead to collisions if you encrypt a very |
| 4561 | * large number of messages.*/ |
| 4562 | if (nonce_length != 0) { |
| 4563 | return PSA_SUCCESS; |
| 4564 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4565 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4566 | #endif /* PSA_WANT_ALG_GCM */ |
| 4567 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4568 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4570 | return PSA_SUCCESS; |
| 4571 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4572 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4573 | #endif /* PSA_WANT_ALG_CCM */ |
| 4574 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4575 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | if (nonce_length == 12) { |
| 4577 | return PSA_SUCCESS; |
| 4578 | } else if (nonce_length == 8) { |
| 4579 | return PSA_ERROR_NOT_SUPPORTED; |
| 4580 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4581 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4582 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4583 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4584 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4586 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4589 | } |
| 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | 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] | 4592 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4594 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4595 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4601 | psa_algorithm_t alg, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4602 | const uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | size_t nonce_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4604 | const uint8_t *additional_data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | size_t additional_data_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4606 | const uint8_t *plaintext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | size_t plaintext_length, |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4608 | uint8_t *ciphertext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | size_t ciphertext_size, |
| 4610 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4611 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4613 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4614 | |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4615 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4616 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4617 | LOCAL_INPUT_DECLARE(plaintext_external, plaintext); |
| 4618 | LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext); |
| 4619 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4620 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | status = psa_aead_check_algorithm(alg); |
| 4623 | if (status != PSA_SUCCESS) { |
| 4624 | return status; |
| 4625 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4626 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4627 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4628 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4629 | if (status != PSA_SUCCESS) { |
| 4630 | return status; |
| 4631 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4632 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4633 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4635 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4636 | |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4637 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4638 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data); |
| 4639 | LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext); |
| 4640 | LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext); |
| 4641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4643 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4644 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4646 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4647 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4648 | &attributes, slot->key.data, slot->key.bytes, |
| 4649 | alg, |
| 4650 | nonce, nonce_length, |
| 4651 | additional_data, additional_data_length, |
| 4652 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4656 | memset(ciphertext, 0, ciphertext_size); |
| 4657 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4658 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4659 | exit: |
David Horstmann | 9d09a02 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4660 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4661 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4662 | LOCAL_INPUT_FREE(plaintext_external, plaintext); |
| 4663 | LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext); |
| 4664 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4665 | psa_unregister_read(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4671 | psa_algorithm_t alg, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4672 | const uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | size_t nonce_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4674 | const uint8_t *additional_data_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4675 | size_t additional_data_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4676 | const uint8_t *ciphertext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4677 | size_t ciphertext_length, |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4678 | uint8_t *plaintext_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4679 | size_t plaintext_size, |
| 4680 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4681 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4682 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4683 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4684 | |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4685 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4686 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4687 | LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext); |
| 4688 | LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext); |
| 4689 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4690 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | status = psa_aead_check_algorithm(alg); |
| 4693 | if (status != PSA_SUCCESS) { |
| 4694 | return status; |
| 4695 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4696 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4697 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4699 | if (status != PSA_SUCCESS) { |
| 4700 | return status; |
| 4701 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4702 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4703 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4705 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4706 | |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4707 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4708 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, |
| 4709 | additional_data); |
| 4710 | LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext); |
| 4711 | LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext); |
| 4712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4714 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4715 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4717 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4718 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4719 | &attributes, slot->key.data, slot->key.bytes, |
| 4720 | alg, |
| 4721 | nonce, nonce_length, |
| 4722 | additional_data, additional_data_length, |
| 4723 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4727 | memset(plaintext, 0, plaintext_size); |
| 4728 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4729 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4730 | exit: |
David Horstmann | 7f2e040 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4731 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4732 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4733 | LOCAL_INPUT_FREE(ciphertext_external, ciphertext); |
| 4734 | LOCAL_OUTPUT_FREE(plaintext_external, plaintext); |
| 4735 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4736 | psa_unregister_read(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4739 | } |
| 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4742 | { |
| 4743 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4746 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4748 | /* 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] | 4749 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4750 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4751 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4752 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4753 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4754 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4755 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4757 | /* 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] | 4758 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4759 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4760 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4761 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4762 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4763 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4764 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4766 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | if (tag_len != 16) { |
| 4768 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4769 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4770 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4771 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4772 | |
| 4773 | default: |
| 4774 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4775 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4776 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4778 | } |
| 4779 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4780 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4782 | int is_encrypt, |
| 4783 | mbedtls_svc_key_id_t key, |
| 4784 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4785 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4786 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4787 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4788 | psa_key_slot_t *slot = NULL; |
| 4789 | psa_key_usage_t key_usage = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4790 | psa_key_attributes_t attributes; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4792 | status = psa_aead_check_algorithm(alg); |
| 4793 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4794 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4798 | status = PSA_ERROR_BAD_STATE; |
| 4799 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4800 | } |
| 4801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | if (operation->nonce_set || operation->lengths_set || |
| 4803 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4804 | status = PSA_ERROR_BAD_STATE; |
| 4805 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4806 | } |
| 4807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4809 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4811 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 4815 | alg); |
| 4816 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4817 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4819 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4820 | attributes = (psa_key_attributes_t) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4821 | .core = slot->attr |
| 4822 | }; |
| 4823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4825 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | if (is_encrypt) { |
| 4829 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 4830 | &attributes, |
| 4831 | slot->key.data, |
| 4832 | slot->key.bytes, |
| 4833 | alg); |
| 4834 | } else { |
| 4835 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 4836 | &attributes, |
| 4837 | slot->key.data, |
| 4838 | slot->key.bytes, |
| 4839 | alg); |
| 4840 | } |
| 4841 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4842 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4846 | |
| 4847 | exit: |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 4848 | unlock_status = psa_unregister_read(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4851 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 4853 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | } else { |
| 4855 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4856 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4858 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4859 | } |
| 4860 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4861 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 4863 | mbedtls_svc_key_id_t key, |
| 4864 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4865 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 4871 | mbedtls_svc_key_id_t key, |
| 4872 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4873 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame^] | 4879 | uint8_t *nonce_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | size_t nonce_size, |
| 4881 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4882 | { |
| 4883 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4884 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 4885 | size_t required_nonce_size = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4886 | |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame^] | 4887 | LOCAL_OUTPUT_DECLARE(nonce_external, nonce); |
| 4888 | LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce); |
| 4889 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4890 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4893 | status = PSA_ERROR_BAD_STATE; |
| 4894 | goto exit; |
| 4895 | } |
| 4896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4898 | status = PSA_ERROR_BAD_STATE; |
| 4899 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4902 | /* For CCM, this size may not be correct according to the PSA |
| 4903 | * specification. The PSA Crypto 1.0.1 specification states: |
| 4904 | * |
| 4905 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 4906 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 4907 | * |
| 4908 | * However this restriction that L has to be the smallest integer is not |
| 4909 | * applied in practice, and it is not implementable here since the |
| 4910 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 4912 | operation->alg); |
| 4913 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4914 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4915 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4916 | } |
| 4917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | status = psa_generate_random(local_nonce, required_nonce_size); |
| 4919 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4920 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4924 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4925 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | if (status == PSA_SUCCESS) { |
| 4927 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4928 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | } else { |
| 4930 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4931 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4932 | |
David Horstmann | d3cad8b | 2023-12-11 14:46:04 +0000 | [diff] [blame^] | 4933 | LOCAL_OUTPUT_FREE(nonce_external, nonce); |
| 4934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 4939 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 4941 | const uint8_t *nonce, |
| 4942 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4943 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4944 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4947 | status = PSA_ERROR_BAD_STATE; |
| 4948 | goto exit; |
| 4949 | } |
| 4950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4951 | if (operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4952 | status = PSA_ERROR_BAD_STATE; |
| 4953 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 4957 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4958 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4959 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 4960 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 4961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4962 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 4963 | nonce_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4964 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4965 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4966 | if (status == PSA_SUCCESS) { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4967 | operation->nonce_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4968 | } else { |
| 4969 | psa_aead_abort(operation); |
| 4970 | } |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4972 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 4977 | size_t ad_length, |
| 4978 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4979 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4980 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4983 | status = PSA_ERROR_BAD_STATE; |
| 4984 | goto exit; |
| 4985 | } |
| 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | if (operation->lengths_set || operation->ad_started || |
| 4988 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4989 | status = PSA_ERROR_BAD_STATE; |
| 4990 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4991 | } |
| 4992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4994 | #if defined(PSA_WANT_ALG_GCM) |
| 4995 | case PSA_ALG_GCM: |
| 4996 | /* 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] | 4997 | * bits. Without the guard this code will generate warnings on 32bit |
| 4998 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4999 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 5001 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5002 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5003 | goto exit; |
| 5004 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5005 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5006 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5007 | #endif /* PSA_WANT_ALG_GCM */ |
| 5008 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5009 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5011 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5012 | goto exit; |
| 5013 | } |
| 5014 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5015 | #endif /* PSA_WANT_ALG_CCM */ |
| 5016 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5017 | case PSA_ALG_CHACHA20_POLY1305: |
| 5018 | /* No length restrictions for ChaChaPoly. */ |
| 5019 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5020 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5021 | default: |
| 5022 | break; |
| 5023 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 5026 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5027 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5028 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5030 | operation->ad_remaining = ad_length; |
| 5031 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5032 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | } else { |
| 5034 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5035 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5038 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 5039 | |
| 5040 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 5042 | const uint8_t *input, |
| 5043 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5044 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5045 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5048 | status = PSA_ERROR_BAD_STATE; |
| 5049 | goto exit; |
| 5050 | } |
| 5051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5053 | status = PSA_ERROR_BAD_STATE; |
| 5054 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5055 | } |
| 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | if (operation->lengths_set) { |
| 5058 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5059 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5060 | goto exit; |
| 5061 | } |
| 5062 | |
| 5063 | operation->ad_remaining -= input_length; |
| 5064 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5065 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5067 | status = PSA_ERROR_BAD_STATE; |
| 5068 | goto exit; |
| 5069 | } |
| 5070 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5072 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 5073 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5074 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5075 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5077 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | } else { |
| 5079 | psa_aead_abort(operation); |
| 5080 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 5086 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5087 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 5088 | const uint8_t *input, |
| 5089 | size_t input_length, |
| 5090 | uint8_t *output, |
| 5091 | size_t output_size, |
| 5092 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5093 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5094 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5095 | |
| 5096 | *output_length = 0; |
| 5097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5099 | status = PSA_ERROR_BAD_STATE; |
| 5100 | goto exit; |
| 5101 | } |
| 5102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5104 | status = PSA_ERROR_BAD_STATE; |
| 5105 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5106 | } |
| 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5109 | /* Additional data length was supplied, but not all the additional |
| 5110 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5112 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5113 | goto exit; |
| 5114 | } |
| 5115 | |
| 5116 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5118 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5119 | goto exit; |
| 5120 | } |
| 5121 | |
| 5122 | operation->body_remaining -= input_length; |
| 5123 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5124 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5126 | status = PSA_ERROR_BAD_STATE; |
| 5127 | goto exit; |
| 5128 | } |
| 5129 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 5132 | output, output_size, |
| 5133 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5134 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5135 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5136 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5137 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | } else { |
| 5139 | psa_aead_abort(operation); |
| 5140 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5143 | } |
| 5144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | 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] | 5146 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | if (operation->id == 0 || !operation->nonce_set) { |
| 5148 | return PSA_ERROR_BAD_STATE; |
| 5149 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 5152 | operation->body_remaining != 0)) { |
| 5153 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5154 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5157 | } |
| 5158 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5159 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 5161 | uint8_t *ciphertext, |
| 5162 | size_t ciphertext_size, |
| 5163 | size_t *ciphertext_length, |
| 5164 | uint8_t *tag, |
| 5165 | size_t tag_size, |
| 5166 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5167 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5168 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5169 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5170 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 5171 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | status = psa_aead_final_checks(operation); |
| 5174 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5175 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5179 | status = PSA_ERROR_BAD_STATE; |
| 5180 | goto exit; |
| 5181 | } |
| 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 5184 | ciphertext_size, |
| 5185 | ciphertext_length, |
| 5186 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5187 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5188 | exit: |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5189 | |
| 5190 | |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5191 | /* 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] | 5192 | * the zero tag size, make sure the tag is set to something implausible. |
| 5193 | * Even if the operation succeeds, make sure we clear the rest of the |
| 5194 | * buffer to prevent potential leakage of anything previously placed in |
| 5195 | * the same buffer.*/ |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5196 | psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length); |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 5204 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 5206 | uint8_t *plaintext, |
| 5207 | size_t plaintext_size, |
| 5208 | size_t *plaintext_length, |
| 5209 | const uint8_t *tag, |
| 5210 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5211 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5212 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5213 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5214 | *plaintext_length = 0; |
| 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | status = psa_aead_final_checks(operation); |
| 5217 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5218 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5222 | status = PSA_ERROR_BAD_STATE; |
| 5223 | goto exit; |
| 5224 | } |
| 5225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5226 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 5227 | plaintext_size, |
| 5228 | plaintext_length, |
| 5229 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5230 | |
| 5231 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5239 | { |
| 5240 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5243 | /* The object has (apparently) been initialized but it is not (yet) |
| 5244 | * in use. It's ok to call abort on such an object, and there's |
| 5245 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5247 | } |
| 5248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5254 | } |
| 5255 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5256 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5257 | /* Generators */ |
| 5258 | /****************************************************************/ |
| 5259 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5260 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5261 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5262 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
Kusumit Ghoderao | af0b534 | 2023-05-03 11:58:46 +0530 | [diff] [blame] | 5263 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5264 | defined(PSA_HAVE_SOFT_PBKDF2) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5265 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5266 | #endif /* At least one builtin KDF */ |
| 5267 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5268 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5269 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5270 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5271 | static psa_status_t psa_key_derivation_start_hmac( |
| 5272 | psa_mac_operation_t *operation, |
| 5273 | psa_algorithm_t hash_alg, |
| 5274 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5276 | { |
| 5277 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5278 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 5280 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 5281 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5282 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5283 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5286 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 5287 | &attributes, |
| 5288 | hmac_key, hmac_key_length, |
| 5289 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | psa_reset_key_attributes(&attributes); |
| 5292 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5293 | } |
| 5294 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5295 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5296 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5297 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5298 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5299 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5300 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5301 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5303 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5305 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5306 | } else { |
| 5307 | return operation->alg; |
| 5308 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5309 | } |
| 5310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | 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] | 5312 | { |
| 5313 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5315 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5316 | /* The object has (apparently) been initialized but it is not |
| 5317 | * in use. It's ok to call abort on such an object, and there's |
| 5318 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5319 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5320 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5322 | mbedtls_free(operation->ctx.hkdf.info); |
| 5323 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5324 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5325 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5326 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5327 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5329 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5330 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5331 | if (operation->ctx.tls12_prf.secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5332 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | operation->ctx.tls12_prf.secret_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5334 | } |
| 5335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | if (operation->ctx.tls12_prf.seed != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5337 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | operation->ctx.tls12_prf.seed_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5339 | } |
| 5340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | if (operation->ctx.tls12_prf.label != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5342 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | operation->ctx.tls12_prf.label_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5344 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5345 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5347 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | operation->ctx.tls12_prf.other_secret_length); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5349 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5350 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5351 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5352 | |
| 5353 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5354 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5355 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5356 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5357 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5358 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5360 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5361 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5362 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5363 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5364 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 5365 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5366 | if (operation->ctx.pbkdf2.salt != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5367 | mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt, |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5368 | operation->ctx.pbkdf2.salt_length); |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5369 | } |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5370 | |
| 5371 | status = PSA_SUCCESS; |
| 5372 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5373 | #endif /* defined(PSA_HAVE_SOFT_PBKDF2) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5374 | { |
| 5375 | status = PSA_ERROR_BAD_STATE; |
| 5376 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5378 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5379 | } |
| 5380 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5381 | 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] | 5382 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5383 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5385 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5389 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5391 | } |
| 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5394 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5395 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | if (operation->alg == 0) { |
| 5397 | return PSA_ERROR_BAD_STATE; |
| 5398 | } |
| 5399 | if (capacity > operation->capacity) { |
| 5400 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5401 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5402 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5404 | } |
| 5405 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5406 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5407 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5409 | psa_algorithm_t kdf_alg, |
| 5410 | uint8_t *output, |
| 5411 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5412 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5414 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5415 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5416 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5417 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | 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] | 5419 | #else |
| 5420 | const uint8_t last_block = 0xff; |
| 5421 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5424 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5425 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5427 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | )) { |
| 5429 | return PSA_ERROR_BAD_STATE; |
| 5430 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5431 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5434 | /* Copy what remains of the current block */ |
| 5435 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5437 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | } |
| 5439 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5440 | output += n; |
| 5441 | output_length -= n; |
| 5442 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5443 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5444 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5446 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5447 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5448 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5449 | * object was corrupted or if this function is called directly |
| 5450 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | if (hkdf->block_number == last_block) { |
| 5452 | return PSA_ERROR_BAD_STATE; |
| 5453 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5454 | |
| 5455 | /* We need a new block */ |
| 5456 | ++hkdf->block_number; |
| 5457 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5460 | hash_alg, |
| 5461 | hkdf->prk, |
| 5462 | hash_length); |
| 5463 | if (status != PSA_SUCCESS) { |
| 5464 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5465 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | |
| 5467 | if (hkdf->block_number != 1) { |
| 5468 | status = psa_mac_update(&hkdf->hmac, |
| 5469 | hkdf->output_block, |
| 5470 | hash_length); |
| 5471 | if (status != PSA_SUCCESS) { |
| 5472 | return status; |
| 5473 | } |
| 5474 | } |
| 5475 | status = psa_mac_update(&hkdf->hmac, |
| 5476 | hkdf->info, |
| 5477 | hkdf->info_length); |
| 5478 | if (status != PSA_SUCCESS) { |
| 5479 | return status; |
| 5480 | } |
| 5481 | status = psa_mac_update(&hkdf->hmac, |
| 5482 | &hkdf->block_number, 1); |
| 5483 | if (status != PSA_SUCCESS) { |
| 5484 | return status; |
| 5485 | } |
| 5486 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5487 | hkdf->output_block, |
| 5488 | sizeof(hkdf->output_block), |
| 5489 | &hmac_output_length); |
| 5490 | if (status != PSA_SUCCESS) { |
| 5491 | return status; |
| 5492 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5493 | } |
| 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5496 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5497 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5498 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5499 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5500 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5501 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5502 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5504 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5506 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5507 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5508 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5509 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5510 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5511 | /* We can't be wanting more output after block 0xff, otherwise |
| 5512 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5513 | * prevented this call. It could happen only if the operation |
| 5514 | * object was corrupted or if this function is called directly |
| 5515 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | if (tls12_prf->block_number == 0xff) { |
| 5517 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5518 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5519 | |
| 5520 | /* We need a new block */ |
| 5521 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5522 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5523 | |
| 5524 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5525 | * |
| 5526 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5527 | * |
| 5528 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5529 | * HMAC_hash(secret, A(2) + seed) + |
| 5530 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5531 | * |
| 5532 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5533 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5534 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5535 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5536 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5537 | * is currently extracted as `output_block` and where i is |
| 5538 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5539 | */ |
| 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | status = psa_key_derivation_start_hmac(&hmac, |
| 5542 | hash_alg, |
| 5543 | tls12_prf->secret, |
| 5544 | tls12_prf->secret_length); |
| 5545 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5546 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5548 | |
| 5549 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5551 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5552 | * the variable seed and in this instance means it in the context of the |
| 5553 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | status = psa_mac_update(&hmac, |
| 5555 | tls12_prf->label, |
| 5556 | tls12_prf->label_length); |
| 5557 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5558 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | } |
| 5560 | status = psa_mac_update(&hmac, |
| 5561 | tls12_prf->seed, |
| 5562 | tls12_prf->seed_length); |
| 5563 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5564 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | } |
| 5566 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5567 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5569 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5570 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5572 | } |
| 5573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | status = psa_mac_sign_finish(&hmac, |
| 5575 | tls12_prf->Ai, hash_length, |
| 5576 | &hmac_output_length); |
| 5577 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5578 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | } |
| 5580 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5581 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5583 | |
| 5584 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | status = psa_key_derivation_start_hmac(&hmac, |
| 5586 | hash_alg, |
| 5587 | tls12_prf->secret, |
| 5588 | tls12_prf->secret_length); |
| 5589 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5590 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | } |
| 5592 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5593 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5594 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | } |
| 5596 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5597 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5598 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | } |
| 5600 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5601 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5602 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5603 | } |
| 5604 | status = psa_mac_sign_finish(&hmac, |
| 5605 | tls12_prf->output_block, hash_length, |
| 5606 | &hmac_output_length); |
| 5607 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5608 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5610 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5611 | |
| 5612 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | cleanup_status = psa_mac_abort(&hmac); |
| 5614 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5615 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5619 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5620 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5621 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5622 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5623 | psa_algorithm_t alg, |
| 5624 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5626 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5628 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5629 | psa_status_t status; |
| 5630 | uint8_t offset, length; |
| 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5633 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5634 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5635 | break; |
| 5636 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5637 | break; |
| 5638 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5640 | } |
| 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5643 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | if (tls12_prf->left_in_block == 0) { |
| 5645 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5646 | alg); |
| 5647 | if (status != PSA_SUCCESS) { |
| 5648 | return status; |
| 5649 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5650 | |
| 5651 | continue; |
| 5652 | } |
| 5653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5655 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5657 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5658 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5659 | |
| 5660 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5662 | output += length; |
| 5663 | output_length -= length; |
| 5664 | tls12_prf->left_in_block -= length; |
| 5665 | } |
| 5666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5668 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5669 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5670 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5671 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5672 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5673 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5674 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5675 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5677 | { |
| 5678 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5679 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | if (output_length != 32) { |
| 5682 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5683 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5686 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5687 | &output_size); |
| 5688 | if (status != PSA_SUCCESS) { |
| 5689 | return status; |
| 5690 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | if (output_size != output_length) { |
| 5693 | return PSA_ERROR_GENERIC_ERROR; |
| 5694 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5697 | } |
| 5698 | #endif |
| 5699 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5700 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5701 | static psa_status_t psa_key_derivation_pbkdf2_generate_block( |
| 5702 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 5703 | psa_algorithm_t prf_alg, |
| 5704 | uint8_t prf_output_length, |
| 5705 | psa_key_attributes_t *attributes) |
| 5706 | { |
| 5707 | psa_status_t status; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5708 | psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5709 | size_t mac_output_length; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5710 | uint8_t U_i[PSA_MAC_MAX_SIZE]; |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5711 | uint8_t *U_accumulator = pbkdf2->output_block; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5712 | uint64_t i; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5713 | uint8_t block_counter[4]; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5714 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5715 | mac_operation.is_sign = 1; |
| 5716 | mac_operation.mac_size = prf_output_length; |
| 5717 | MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5718 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5719 | status = psa_driver_wrapper_mac_sign_setup(&mac_operation, |
| 5720 | attributes, |
| 5721 | pbkdf2->password, |
| 5722 | pbkdf2->password_length, |
| 5723 | prf_alg); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5724 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5725 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5726 | } |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5727 | status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length); |
| 5728 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5729 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5730 | } |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5731 | status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter)); |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5732 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5733 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5734 | } |
| 5735 | status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i), |
| 5736 | &mac_output_length); |
| 5737 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5738 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5739 | } |
| 5740 | |
| 5741 | if (mac_output_length != prf_output_length) { |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5742 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5743 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5744 | } |
| 5745 | |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5746 | memcpy(U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5747 | |
| 5748 | for (i = 1; i < pbkdf2->input_cost; i++) { |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 5749 | /* We are passing prf_output_length as mac_size because the driver |
| 5750 | * function directly sets mac_output_length as mac_size upon success. |
Kusumit Ghoderao | 5f3345a | 2023-07-27 21:21:38 +0530 | [diff] [blame] | 5751 | * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5752 | status = psa_driver_wrapper_mac_compute(attributes, |
| 5753 | pbkdf2->password, |
| 5754 | pbkdf2->password_length, |
| 5755 | prf_alg, U_i, prf_output_length, |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 5756 | U_i, prf_output_length, |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5757 | &mac_output_length); |
| 5758 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5759 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5760 | } |
| 5761 | |
Kusumit Ghoderao | 109ee3d | 2023-06-08 16:36:45 +0530 | [diff] [blame] | 5762 | mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5763 | } |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5764 | |
| 5765 | cleanup: |
| 5766 | /* Zeroise buffers to clear sensitive data from memory. */ |
| 5767 | mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE); |
| 5768 | return status; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5769 | } |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5770 | |
| 5771 | static psa_status_t psa_key_derivation_pbkdf2_read( |
| 5772 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 5773 | psa_algorithm_t kdf_alg, |
| 5774 | uint8_t *output, |
| 5775 | size_t output_length) |
| 5776 | { |
| 5777 | psa_status_t status; |
| 5778 | psa_algorithm_t prf_alg; |
| 5779 | uint8_t prf_output_length; |
| 5780 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5781 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length)); |
| 5782 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 5783 | |
| 5784 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 5785 | prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg)); |
| 5786 | prf_output_length = PSA_HASH_LENGTH(prf_alg); |
| 5787 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Kusumit Ghoderao | a2520a5 | 2023-06-22 15:42:19 +0530 | [diff] [blame] | 5788 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 5789 | prf_alg = PSA_ALG_CMAC; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 5790 | 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] | 5791 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
Kusumit Ghoderao | d9ec1af | 2023-06-08 20:19:51 +0530 | [diff] [blame] | 5792 | } else { |
| 5793 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | switch (pbkdf2->state) { |
| 5797 | case PSA_PBKDF2_STATE_PASSWORD_SET: |
| 5798 | /* Initially we need a new block so bytes_used is equal to block size*/ |
| 5799 | pbkdf2->bytes_used = prf_output_length; |
| 5800 | pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT; |
| 5801 | break; |
| 5802 | case PSA_PBKDF2_STATE_OUTPUT: |
| 5803 | break; |
| 5804 | default: |
| 5805 | return PSA_ERROR_BAD_STATE; |
| 5806 | } |
| 5807 | |
| 5808 | while (output_length != 0) { |
| 5809 | uint8_t n = prf_output_length - pbkdf2->bytes_used; |
| 5810 | if (n > output_length) { |
| 5811 | n = (uint8_t) output_length; |
| 5812 | } |
| 5813 | memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n); |
| 5814 | output += n; |
| 5815 | output_length -= n; |
| 5816 | pbkdf2->bytes_used += n; |
| 5817 | |
| 5818 | if (output_length == 0) { |
| 5819 | break; |
| 5820 | } |
| 5821 | |
| 5822 | /* We need a new block */ |
| 5823 | pbkdf2->bytes_used = 0; |
| 5824 | pbkdf2->block_number++; |
| 5825 | |
| 5826 | status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg, |
| 5827 | prf_output_length, |
| 5828 | &attributes); |
| 5829 | if (status != PSA_SUCCESS) { |
| 5830 | return status; |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | return PSA_SUCCESS; |
| 5835 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5836 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5837 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5838 | psa_status_t psa_key_derivation_output_bytes( |
| 5839 | psa_key_derivation_operation_t *operation, |
| 5840 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5842 | { |
| 5843 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5847 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5849 | } |
| 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5852 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5853 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5854 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5855 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5856 | goto exit; |
| 5857 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5859 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5860 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5861 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5862 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5863 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5864 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5866 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5867 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5868 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5869 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5871 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 5872 | output, output_length); |
| 5873 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5874 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5875 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5876 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5877 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5878 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5879 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 5880 | kdf_alg, output, |
| 5881 | output_length); |
| 5882 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5883 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5884 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5885 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5887 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 5889 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5890 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5891 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 5892 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5893 | status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg, |
| 5894 | output, output_length); |
Kusumit Ghoderao | 056f0c5 | 2023-05-03 15:58:34 +0530 | [diff] [blame] | 5895 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5896 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5897 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5898 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5899 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5905 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5906 | * This allows us to differentiate between exhausted operations and |
| 5907 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5908 | * operations. */ |
| 5909 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5911 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5913 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5915 | } |
| 5916 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5917 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | 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] | 5919 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | if (data_size >= 8) { |
| 5921 | mbedtls_des_key_set_parity(data); |
| 5922 | } |
| 5923 | if (data_size >= 16) { |
| 5924 | mbedtls_des_key_set_parity(data + 8); |
| 5925 | } |
| 5926 | if (data_size >= 24) { |
| 5927 | mbedtls_des_key_set_parity(data + 16); |
| 5928 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5929 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5930 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5931 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5932 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 5934 | * of a private key which is an integer |
| 5935 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 5936 | * N is the prime p for Diffie-Hellman, or the order of the |
| 5937 | * curve’s base point for ECC. |
| 5938 | * |
| 5939 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 5940 | * This function generates the private key using the following process: |
| 5941 | * |
| 5942 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 5943 | * 2. If m is not a multiple of 8, set the most significant |
| 5944 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 5945 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 5946 | * 4. If k > N - 2, discard the result and return to step 1. |
| 5947 | * 5. Output k + 1 as the private key. |
| 5948 | * |
| 5949 | * This method allows compliance to NIST standards, specifically the methods titled |
| 5950 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 5951 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 5952 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 5953 | * Diffie-Hellman keys. |
| 5954 | * |
| 5955 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 5956 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 5957 | * |
| 5958 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5959 | * always NULL. |
| 5960 | */ |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 5961 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
| 5962 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5963 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5964 | psa_key_slot_t *slot, |
| 5965 | size_t bits, |
| 5966 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5967 | uint8_t **data |
| 5968 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5969 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5970 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5971 | mbedtls_mpi k; |
| 5972 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5973 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5974 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 5975 | size_t m; |
| 5976 | size_t m_bytes; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | mbedtls_mpi_init(&k); |
| 5979 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5980 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5981 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5983 | mbedtls_ecp_group_id grp_id = |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 5984 | mbedtls_ecc_group_from_psa(curve, bits); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5987 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 5988 | goto cleanup; |
| 5989 | } |
| 5990 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5991 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5992 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5995 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5996 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5997 | /* Let m be the bit size of N. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 5998 | m = ecp_group.nbits; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5999 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6000 | m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6001 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6002 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | 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] | 6004 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6005 | /* Note: This function is always called with *data == NULL and it |
| 6006 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | *data = mbedtls_calloc(1, m_bytes); |
| 6008 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6009 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6010 | goto cleanup; |
| 6011 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6014 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6015 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6016 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6018 | |
| 6019 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6021 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6022 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 6023 | * the string to zero. |
| 6024 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6026 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | * big-endian byte string. |
| 6031 | */ |
| 6032 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6033 | |
| 6034 | /* 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] | 6035 | * Result of comparison is returned. When it indicates error |
| 6036 | * then this function is called again. |
| 6037 | */ |
| 6038 | 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] | 6039 | } |
| 6040 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6041 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 6043 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6044 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | if (ret != 0) { |
| 6046 | status = mbedtls_to_psa_error(ret); |
| 6047 | } |
| 6048 | if (status != PSA_SUCCESS) { |
| 6049 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6050 | *data = NULL; |
| 6051 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | mbedtls_mpi_free(&k); |
| 6053 | mbedtls_mpi_free(&diff_N_2); |
| 6054 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6055 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6056 | |
| 6057 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 6058 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 6059 | * |
| 6060 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 6061 | * draw a 32-byte string and process it as specified in |
| 6062 | * Elliptic Curves for Security [RFC7748] §5. |
| 6063 | * |
| 6064 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 6065 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 6066 | * |
| 6067 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 6068 | * always NULL. |
| 6069 | */ |
| 6070 | |
| 6071 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6072 | size_t bits, |
| 6073 | psa_key_derivation_operation_t *operation, |
| 6074 | uint8_t **data |
| 6075 | ) |
| 6076 | { |
| 6077 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6078 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6081 | case 255: |
| 6082 | output_length = 32; |
| 6083 | break; |
| 6084 | case 448: |
| 6085 | output_length = 56; |
| 6086 | break; |
| 6087 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6089 | break; |
| 6090 | } |
| 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | if (*data == NULL) { |
| 6095 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6096 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6101 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6105 | case 255: |
| 6106 | (*data)[0] &= 248; |
| 6107 | (*data)[31] &= 127; |
| 6108 | (*data)[31] |= 64; |
| 6109 | break; |
| 6110 | case 448: |
| 6111 | (*data)[0] &= 252; |
| 6112 | (*data)[55] |= 128; |
| 6113 | break; |
| 6114 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6116 | break; |
| 6117 | } |
| 6118 | |
| 6119 | return status; |
| 6120 | } |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 6121 | #else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6122 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
| 6123 | psa_key_slot_t *slot, size_t bits, |
| 6124 | psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6125 | { |
| 6126 | (void) slot; |
| 6127 | (void) bits; |
| 6128 | (void) operation; |
| 6129 | (void) data; |
| 6130 | return PSA_ERROR_NOT_SUPPORTED; |
| 6131 | } |
| 6132 | |
| 6133 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6134 | size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6135 | { |
| 6136 | (void) bits; |
| 6137 | (void) operation; |
| 6138 | (void) data; |
| 6139 | return PSA_ERROR_NOT_SUPPORTED; |
| 6140 | } |
| 6141 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6142 | #endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6143 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 6144 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6145 | psa_key_slot_t *slot, |
| 6146 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6148 | { |
| 6149 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6151 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6152 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6153 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 6156 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6157 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6158 | |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6159 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \ |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6160 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 6162 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 6163 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6164 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 6166 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6167 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | } |
| 6169 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6170 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 6172 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6173 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6175 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 6176 | } else |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6177 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6178 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 6180 | if (bits % 8 != 0) { |
| 6181 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6182 | } |
| 6183 | data = mbedtls_calloc(1, bytes); |
| 6184 | if (data == NULL) { |
| 6185 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6186 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 6189 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6190 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6192 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 6194 | psa_des_set_key_parity(data, bytes); |
| 6195 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 6196 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | } else { |
| 6198 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6199 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 6200 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6201 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6202 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 6204 | }; |
| 6205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 6207 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 6208 | &storage_size); |
| 6209 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6210 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6212 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 6214 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6215 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | status = psa_driver_wrapper_import_key(&attributes, |
| 6219 | data, bytes, |
| 6220 | slot->key.data, |
| 6221 | slot->key.bytes, |
| 6222 | &slot->key.bytes, &bits); |
| 6223 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6224 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6226 | |
| 6227 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | mbedtls_free(data); |
| 6229 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6230 | } |
| 6231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 6233 | psa_key_derivation_operation_t *operation, |
| 6234 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6235 | { |
| 6236 | psa_status_t status; |
| 6237 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6238 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6239 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6240 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6241 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6242 | /* Reject any attempt to create a zero-length key so that we don't |
| 6243 | * 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] | 6244 | if (psa_get_key_bits(attributes) == 0) { |
| 6245 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6246 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | if (operation->alg == PSA_ALG_NONE) { |
| 6249 | return PSA_ERROR_BAD_STATE; |
| 6250 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 6251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6252 | if (!operation->can_output_key) { |
| 6253 | return PSA_ERROR_NOT_PERMITTED; |
| 6254 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 6257 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6258 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6260 | /* Deriving a key in a secure element is not implemented yet. */ |
| 6261 | status = PSA_ERROR_NOT_SUPPORTED; |
| 6262 | } |
| 6263 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | if (status == PSA_SUCCESS) { |
| 6265 | status = psa_generate_derived_key_internal(slot, |
| 6266 | attributes->core.bits, |
| 6267 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6268 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6269 | if (status == PSA_SUCCESS) { |
| 6270 | status = psa_finish_key_creation(slot, driver, key); |
| 6271 | } |
| 6272 | if (status != PSA_SUCCESS) { |
| 6273 | psa_fail_key_creation(slot, driver); |
| 6274 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6277 | } |
| 6278 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6279 | |
| 6280 | |
| 6281 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6282 | /* Key derivation */ |
| 6283 | /****************************************************************/ |
| 6284 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 6285 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6287 | { |
| 6288 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 6290 | return 1; |
| 6291 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6292 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6293 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6295 | return 1; |
| 6296 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6297 | #endif |
| 6298 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6300 | return 1; |
| 6301 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6302 | #endif |
| 6303 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6305 | return 1; |
| 6306 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6307 | #endif |
| 6308 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6310 | return 1; |
| 6311 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6312 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6313 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6315 | return 1; |
| 6316 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6317 | #endif |
Kusumit Ghoderao | d132cac | 2023-05-03 12:00:27 +0530 | [diff] [blame] | 6318 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
| 6319 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 6320 | return 1; |
| 6321 | } |
| 6322 | #endif |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6323 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 6324 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 6325 | return 1; |
| 6326 | } |
| 6327 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6329 | } |
| 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6332 | { |
| 6333 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 6335 | psa_hash_abort(&operation); |
| 6336 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6337 | } |
| 6338 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6339 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6340 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6342 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6343 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 6344 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6347 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 6349 | return PSA_ERROR_NOT_SUPPORTED; |
| 6350 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6351 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6352 | /* All currently supported key derivation algorithms (apart from |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6353 | * 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] | 6354 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 6355 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6356 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6357 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
| 6358 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6359 | 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] | 6360 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | if (hash_size == 0) { |
| 6362 | return PSA_ERROR_NOT_SUPPORTED; |
| 6363 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6364 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6365 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 6366 | * we might fail later, which is somewhat unfriendly and potentially |
| 6367 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 6369 | if (status != PSA_SUCCESS) { |
| 6370 | return status; |
| 6371 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6372 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 6375 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 6376 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 6377 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6378 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 6379 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6380 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 6382 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6383 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6385 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 6386 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6387 | operation->capacity = 255 * hash_size; |
| 6388 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6389 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6391 | 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] | 6392 | { |
| 6393 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | if (alg == PSA_ALG_ECDH) { |
| 6395 | return PSA_SUCCESS; |
| 6396 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6397 | #endif |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 6398 | #if defined(PSA_WANT_ALG_FFDH) |
| 6399 | if (alg == PSA_ALG_FFDH) { |
| 6400 | return PSA_SUCCESS; |
| 6401 | } |
| 6402 | #endif |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6403 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6404 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6405 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6406 | |
| 6407 | static int psa_key_derivation_allows_free_form_secret_input( |
| 6408 | psa_algorithm_t kdf_alg) |
| 6409 | { |
| 6410 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6411 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6412 | return 0; |
| 6413 | } |
| 6414 | #endif |
| 6415 | (void) kdf_alg; |
| 6416 | return 1; |
| 6417 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6418 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 6421 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6422 | { |
| 6423 | psa_status_t status; |
| 6424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | if (operation->alg != 0) { |
| 6426 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6427 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6430 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6431 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 6432 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6433 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 6434 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 6435 | status = psa_key_agreement_try_support(ka_alg); |
| 6436 | if (status != PSA_SUCCESS) { |
| 6437 | return status; |
| 6438 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6439 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 6440 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6441 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 6443 | #else |
| 6444 | return PSA_ERROR_NOT_SUPPORTED; |
| 6445 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6446 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 6447 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6448 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 6449 | #else |
| 6450 | return PSA_ERROR_NOT_SUPPORTED; |
| 6451 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6452 | } else { |
| 6453 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6454 | } |
| 6455 | |
| 6456 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6457 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6458 | } |
| 6459 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6460 | } |
| 6461 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6462 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 6464 | psa_algorithm_t kdf_alg, |
| 6465 | psa_key_derivation_step_t step, |
| 6466 | const uint8_t *data, |
| 6467 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6468 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6470 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6472 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6473 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6474 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6475 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6476 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6477 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6478 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6479 | return PSA_ERROR_BAD_STATE; |
| 6480 | } else { |
| 6481 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6482 | hash_alg, |
| 6483 | data, data_length); |
| 6484 | if (status != PSA_SUCCESS) { |
| 6485 | return status; |
| 6486 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6487 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6489 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6490 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6491 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6492 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6493 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6494 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6495 | * the state. It could happen only if the hkdf |
| 6496 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6498 | return PSA_ERROR_BAD_STATE; |
| 6499 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6500 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6501 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6503 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6504 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6506 | memcpy(hkdf->prk, data, data_length); |
| 6507 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6508 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6509 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6510 | /* HKDF: If no salt was provided, use an empty salt. |
| 6511 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6512 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6513 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6514 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6515 | return PSA_ERROR_BAD_STATE; |
| 6516 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6517 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6519 | hash_alg, |
| 6520 | NULL, 0); |
| 6521 | if (status != PSA_SUCCESS) { |
| 6522 | return status; |
| 6523 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6524 | hkdf->state = HKDF_STATE_STARTED; |
| 6525 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6526 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6527 | return PSA_ERROR_BAD_STATE; |
| 6528 | } |
| 6529 | status = psa_mac_update(&hkdf->hmac, |
| 6530 | data, data_length); |
| 6531 | if (status != PSA_SUCCESS) { |
| 6532 | return status; |
| 6533 | } |
| 6534 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6535 | hkdf->prk, |
| 6536 | sizeof(hkdf->prk), |
| 6537 | &data_length); |
| 6538 | if (status != PSA_SUCCESS) { |
| 6539 | return status; |
| 6540 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6541 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6542 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6543 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6544 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6545 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6546 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6547 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6548 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6549 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6550 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6551 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6552 | { |
| 6553 | /* Block 0 is empty, and the next block will be |
| 6554 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6555 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6556 | } |
| 6557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6559 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6560 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6561 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6562 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6563 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6564 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6565 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6566 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6567 | hkdf->state == HKDF_STATE_INIT) { |
| 6568 | return PSA_ERROR_BAD_STATE; |
| 6569 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6570 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6571 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6572 | return PSA_ERROR_BAD_STATE; |
| 6573 | } |
| 6574 | if (hkdf->info_set) { |
| 6575 | return PSA_ERROR_BAD_STATE; |
| 6576 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6577 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6578 | if (data_length != 0) { |
| 6579 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6580 | if (hkdf->info == NULL) { |
| 6581 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6582 | } |
| 6583 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6584 | } |
| 6585 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6587 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6588 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6589 | } |
| 6590 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6591 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6592 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6593 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6594 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6596 | const uint8_t *data, |
| 6597 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6598 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6599 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6600 | return PSA_ERROR_BAD_STATE; |
| 6601 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | if (data_length != 0) { |
| 6604 | prf->seed = mbedtls_calloc(1, data_length); |
| 6605 | if (prf->seed == NULL) { |
| 6606 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6607 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6609 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6610 | prf->seed_length = data_length; |
| 6611 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6612 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6613 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6615 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6616 | } |
| 6617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6618 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6619 | const uint8_t *data, |
| 6620 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6621 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6622 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6623 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6624 | return PSA_ERROR_BAD_STATE; |
| 6625 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | if (data_length != 0) { |
| 6628 | prf->secret = mbedtls_calloc(1, data_length); |
| 6629 | if (prf->secret == NULL) { |
| 6630 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6631 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6633 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6634 | prf->secret_length = data_length; |
| 6635 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6636 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6637 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6640 | } |
| 6641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6643 | const uint8_t *data, |
| 6644 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6645 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6647 | return PSA_ERROR_BAD_STATE; |
| 6648 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6650 | if (data_length != 0) { |
| 6651 | prf->label = mbedtls_calloc(1, data_length); |
| 6652 | if (prf->label == NULL) { |
| 6653 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6654 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6657 | prf->label_length = data_length; |
| 6658 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6659 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6660 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6663 | } |
| 6664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6666 | psa_key_derivation_step_t step, |
| 6667 | const uint8_t *data, |
| 6668 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6669 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6670 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6671 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6673 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6675 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6676 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6677 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6678 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6679 | } |
| 6680 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6681 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6682 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6683 | |
| 6684 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6685 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6686 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6687 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6688 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6689 | { |
| 6690 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6691 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6692 | 4 + data_length + prf->other_secret_length : |
| 6693 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6695 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6696 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6697 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6700 | if (pms == NULL) { |
| 6701 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6702 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6703 | uint8_t *cur = pms; |
| 6704 | |
| 6705 | /* pure-PSK: |
| 6706 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6707 | * |
| 6708 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6709 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6710 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6711 | * |
| 6712 | * mixed-PSK: |
| 6713 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6714 | * follows: concatenate a uint16 with the length of the other secret, |
| 6715 | * the other secret itself, uint16 with the length of PSK, and the |
| 6716 | * PSK itself. |
| 6717 | * For details please check: |
| 6718 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6719 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6720 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6721 | */ |
| 6722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6723 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6724 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 6725 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 6726 | if (prf->other_secret_length != 0) { |
| 6727 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 6728 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 6729 | cur += prf->other_secret_length; |
| 6730 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6731 | } else { |
| 6732 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6733 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6734 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6735 | cur += data_length; |
| 6736 | } |
| 6737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6738 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6739 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6740 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6741 | cur += data_length; |
| 6742 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 6743 | status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms)); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6744 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6745 | mbedtls_zeroize_and_free(pms, pms_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6746 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6747 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6748 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6749 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 6750 | psa_tls12_prf_key_derivation_t *prf, |
| 6751 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6753 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6754 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 6755 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6756 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6757 | |
| 6758 | if (data_length != 0) { |
| 6759 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 6760 | if (prf->other_secret == NULL) { |
| 6761 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6762 | } |
| 6763 | |
| 6764 | memcpy(prf->other_secret, data, data_length); |
| 6765 | prf->other_secret_length = data_length; |
| 6766 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6767 | prf->other_secret_length = 0; |
| 6768 | } |
| 6769 | |
| 6770 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 6771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6772 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6773 | } |
| 6774 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6775 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6776 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6777 | psa_key_derivation_step_t step, |
| 6778 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6779 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6780 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6781 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6782 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 6784 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6785 | break; |
| 6786 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6787 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 6788 | data, |
| 6789 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6790 | break; |
| 6791 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6792 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6793 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6794 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6795 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6796 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6797 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6798 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6799 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6800 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 6801 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 6802 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6803 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6804 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6805 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6806 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 6807 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 6808 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 6809 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6810 | |
| 6811 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6812 | if (data[0] != 0x04) { |
| 6813 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6814 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6815 | |
| 6816 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6817 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6820 | } |
| 6821 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6822 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6823 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6824 | static psa_status_t psa_pbkdf2_set_input_cost( |
| 6825 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 6826 | psa_key_derivation_step_t step, |
| 6827 | uint64_t data) |
| 6828 | { |
| 6829 | if (step != PSA_KEY_DERIVATION_INPUT_COST) { |
| 6830 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6831 | } |
| 6832 | |
| 6833 | if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) { |
| 6834 | return PSA_ERROR_BAD_STATE; |
| 6835 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6836 | |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 6837 | if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) { |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6838 | return PSA_ERROR_NOT_SUPPORTED; |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6839 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6840 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6841 | if (data == 0) { |
| 6842 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6843 | } |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 6844 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6845 | pbkdf2->input_cost = data; |
| 6846 | pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET; |
| 6847 | |
| 6848 | return PSA_SUCCESS; |
| 6849 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6850 | |
| 6851 | static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2, |
| 6852 | const uint8_t *data, |
| 6853 | size_t data_length) |
| 6854 | { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6855 | if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) { |
| 6856 | pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET; |
| 6857 | } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) { |
| 6858 | /* Appending to existing salt. No state change. */ |
| 6859 | } else { |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6860 | return PSA_ERROR_BAD_STATE; |
| 6861 | } |
| 6862 | |
Gilles Peskine | ca57d78 | 2023-07-20 19:08:06 +0200 | [diff] [blame] | 6863 | if (data_length == 0) { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6864 | /* Appending an empty string, nothing to do. */ |
| 6865 | } else { |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6866 | uint8_t *next_salt; |
Kusumit Ghoderao | b538bb7 | 2023-05-24 12:32:14 +0530 | [diff] [blame] | 6867 | |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6868 | next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length); |
| 6869 | if (next_salt == NULL) { |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 6870 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6871 | } |
| 6872 | |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6873 | if (pbkdf2->salt_length != 0) { |
| 6874 | memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length); |
| 6875 | } |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6876 | memcpy(next_salt + pbkdf2->salt_length, data, data_length); |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 6877 | pbkdf2->salt_length += data_length; |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6878 | mbedtls_free(pbkdf2->salt); |
| 6879 | pbkdf2->salt = next_salt; |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6880 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6881 | return PSA_SUCCESS; |
| 6882 | } |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6883 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6884 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6885 | 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] | 6886 | const uint8_t *input, |
| 6887 | size_t input_len, |
| 6888 | uint8_t *output, |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6889 | size_t *output_len) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6890 | { |
| 6891 | psa_status_t status = PSA_SUCCESS; |
| 6892 | if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) { |
| 6893 | status = psa_hash_compute(hash_alg, input, input_len, output, |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6894 | PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6895 | } else { |
| 6896 | memcpy(output, input, input_len); |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6897 | *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6898 | } |
| 6899 | return status; |
| 6900 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6901 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6902 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6903 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6904 | static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input, |
| 6905 | size_t input_len, |
| 6906 | uint8_t *output, |
| 6907 | size_t *output_len) |
| 6908 | { |
| 6909 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6910 | 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] | 6911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Kusumit Ghoderao | 3fde8fe | 2023-06-27 10:41:43 +0530 | [diff] [blame] | 6912 | uint8_t zeros[16] = { 0 }; |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6913 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 6914 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros))); |
| 6915 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6916 | /* 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] | 6917 | * mac_size as the driver function sets mac_output_length = mac_size |
| 6918 | * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6919 | status = psa_driver_wrapper_mac_compute(&attributes, |
| 6920 | zeros, sizeof(zeros), |
| 6921 | PSA_ALG_CMAC, input, input_len, |
| 6922 | output, |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6923 | PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, |
| 6924 | 128U, |
| 6925 | PSA_ALG_CMAC), |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6926 | output_len); |
| 6927 | } else { |
| 6928 | memcpy(output, input, input_len); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6929 | *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] | 6930 | } |
| 6931 | return status; |
| 6932 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6933 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6934 | |
| 6935 | 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] | 6936 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6937 | const uint8_t *data, |
| 6938 | size_t data_length) |
| 6939 | { |
Kusumit Ghoderao | aac9a58 | 2023-05-24 14:19:17 +0530 | [diff] [blame] | 6940 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6941 | if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) { |
| 6942 | return PSA_ERROR_BAD_STATE; |
| 6943 | } |
| 6944 | |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 6945 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6946 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 6947 | psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg); |
| 6948 | status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length, |
| 6949 | pbkdf2->password, |
| 6950 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 6951 | } else |
| 6952 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
| 6953 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 6954 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6955 | status = psa_pbkdf2_cmac_set_password(data, data_length, |
| 6956 | pbkdf2->password, |
| 6957 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 6958 | } else |
| 6959 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
| 6960 | { |
| 6961 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET; |
| 6965 | |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6966 | return status; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6967 | } |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 6968 | |
| 6969 | 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] | 6970 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 6971 | psa_key_derivation_step_t step, |
| 6972 | const uint8_t *data, |
| 6973 | size_t data_length) |
| 6974 | { |
| 6975 | switch (step) { |
| 6976 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6977 | return psa_pbkdf2_set_salt(pbkdf2, data, data_length); |
| 6978 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6979 | return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 6980 | default: |
| 6981 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6982 | } |
| 6983 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6984 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6985 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6986 | /** Check whether the given key type is acceptable for the given |
| 6987 | * input step of a key derivation. |
| 6988 | * |
| 6989 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6990 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6991 | * Both secret and non-secret inputs can alternatively have the type |
| 6992 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6993 | * that the input was passed as a buffer rather than via a key object. |
| 6994 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6995 | static int psa_key_derivation_check_input_type( |
| 6996 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6997 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6998 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6999 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7000 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7001 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7002 | return PSA_SUCCESS; |
| 7003 | } |
| 7004 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7005 | return PSA_SUCCESS; |
| 7006 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7007 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7008 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7009 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7010 | return PSA_SUCCESS; |
| 7011 | } |
| 7012 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7013 | return PSA_SUCCESS; |
| 7014 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7015 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7016 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 7017 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 7018 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 7019 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7020 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 7021 | return PSA_SUCCESS; |
| 7022 | } |
| 7023 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7024 | return PSA_SUCCESS; |
| 7025 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7026 | break; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7027 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
| 7028 | if (key_type == PSA_KEY_TYPE_PASSWORD) { |
| 7029 | return PSA_SUCCESS; |
| 7030 | } |
| 7031 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7032 | return PSA_SUCCESS; |
| 7033 | } |
| 7034 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7035 | return PSA_SUCCESS; |
| 7036 | } |
| 7037 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7038 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7039 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7040 | } |
| 7041 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 7042 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7043 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7044 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7045 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7046 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7047 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7048 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7049 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7050 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7052 | status = psa_key_derivation_check_input_type(step, key_type); |
| 7053 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7054 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7055 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7056 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7057 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7058 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 7059 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 7060 | step, data, data_length); |
| 7061 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7062 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7063 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7064 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 7065 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 7066 | step, data, data_length); |
| 7067 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7068 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 7069 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7070 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 7071 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 7072 | step, data, data_length); |
| 7073 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7074 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7075 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7076 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7077 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7078 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 7079 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7080 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7081 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7082 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7083 | status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg, |
| 7084 | step, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7085 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7086 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7087 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7088 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 7089 | (void) data; |
| 7090 | (void) data_length; |
| 7091 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7092 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7093 | } |
| 7094 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7095 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7096 | if (status != PSA_SUCCESS) { |
| 7097 | psa_key_derivation_abort(operation); |
| 7098 | } |
| 7099 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7100 | } |
| 7101 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7102 | static psa_status_t psa_key_derivation_input_integer_internal( |
| 7103 | psa_key_derivation_operation_t *operation, |
| 7104 | psa_key_derivation_step_t step, |
| 7105 | uint64_t value) |
| 7106 | { |
| 7107 | psa_status_t status; |
| 7108 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 7109 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7110 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7111 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7112 | status = psa_pbkdf2_set_input_cost( |
| 7113 | &operation->ctx.pbkdf2, step, value); |
| 7114 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7115 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7116 | { |
Kusumit Ghoderao | 3a18dee | 2023-04-07 16:16:27 +0530 | [diff] [blame] | 7117 | (void) step; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7118 | (void) value; |
| 7119 | (void) kdf_alg; |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 7120 | status = PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | if (status != PSA_SUCCESS) { |
| 7124 | psa_key_derivation_abort(operation); |
| 7125 | } |
| 7126 | return status; |
| 7127 | } |
| 7128 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7129 | psa_status_t psa_key_derivation_input_bytes( |
| 7130 | psa_key_derivation_operation_t *operation, |
| 7131 | psa_key_derivation_step_t step, |
| 7132 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7133 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7134 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7135 | return psa_key_derivation_input_internal(operation, step, |
| 7136 | PSA_KEY_TYPE_NONE, |
| 7137 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7138 | } |
| 7139 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7140 | psa_status_t psa_key_derivation_input_integer( |
| 7141 | psa_key_derivation_operation_t *operation, |
| 7142 | psa_key_derivation_step_t step, |
| 7143 | uint64_t value) |
| 7144 | { |
| 7145 | return psa_key_derivation_input_integer_internal(operation, step, value); |
| 7146 | } |
| 7147 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7148 | psa_status_t psa_key_derivation_input_key( |
| 7149 | psa_key_derivation_operation_t *operation, |
| 7150 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7151 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7152 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7153 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7154 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7155 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7156 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7157 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7158 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7159 | if (status != PSA_SUCCESS) { |
| 7160 | psa_key_derivation_abort(operation); |
| 7161 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 7162 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7163 | |
Kusumit Ghoderao | 3128c5d | 2023-05-03 12:27:57 +0530 | [diff] [blame] | 7164 | /* Passing a key object as a SECRET or PASSWORD input unlocks the |
| 7165 | * permission to output to a key object. */ |
| 7166 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET || |
| 7167 | step == PSA_KEY_DERIVATION_INPUT_PASSWORD) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7168 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7169 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7171 | status = psa_key_derivation_input_internal(operation, |
| 7172 | step, slot->attr.type, |
| 7173 | slot->key.data, |
| 7174 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7175 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7176 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7178 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7179 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7180 | |
| 7181 | |
| 7182 | |
| 7183 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7184 | /* Key agreement */ |
| 7185 | /****************************************************************/ |
| 7186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7187 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 7188 | const uint8_t *key_buffer, |
| 7189 | size_t key_buffer_size, |
| 7190 | psa_algorithm_t alg, |
| 7191 | const uint8_t *peer_key, |
| 7192 | size_t peer_key_length, |
| 7193 | uint8_t *shared_secret, |
| 7194 | size_t shared_secret_size, |
| 7195 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 7196 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7197 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 7198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7199 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7200 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 7201 | key_buffer_size, alg, |
| 7202 | peer_key, peer_key_length, |
| 7203 | shared_secret, |
| 7204 | shared_secret_size, |
| 7205 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7206 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7207 | |
| 7208 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH) |
| 7209 | case PSA_ALG_FFDH: |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 7210 | return mbedtls_psa_ffdh_key_agreement(attributes, |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 7211 | peer_key, |
| 7212 | peer_key_length, |
| 7213 | key_buffer, |
| 7214 | key_buffer_size, |
| 7215 | shared_secret, |
| 7216 | shared_secret_size, |
| 7217 | shared_secret_length); |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7218 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */ |
| 7219 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7220 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7221 | (void) attributes; |
| 7222 | (void) key_buffer; |
| 7223 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 7224 | (void) peer_key; |
| 7225 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7226 | (void) shared_secret; |
| 7227 | (void) shared_secret_size; |
| 7228 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7229 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7230 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7231 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7232 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7233 | /** Internal function for raw key agreement |
| 7234 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 7235 | * to the driver's implementation if a driver is present. |
| 7236 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7237 | * (psa_key_agreement_raw_builtin). |
| 7238 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7239 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 7240 | psa_key_slot_t *private_key, |
| 7241 | const uint8_t *peer_key, |
| 7242 | size_t peer_key_length, |
| 7243 | uint8_t *shared_secret, |
| 7244 | size_t shared_secret_size, |
| 7245 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7246 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7247 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 7248 | return PSA_ERROR_NOT_SUPPORTED; |
| 7249 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7250 | |
| 7251 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7252 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7253 | }; |
| 7254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7255 | return psa_driver_wrapper_key_agreement(&attributes, |
| 7256 | private_key->key.data, |
| 7257 | private_key->key.bytes, alg, |
| 7258 | peer_key, peer_key_length, |
| 7259 | shared_secret, |
| 7260 | shared_secret_size, |
| 7261 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7262 | } |
| 7263 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7264 | /* 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] | 7265 | * to potentially free embedded data structures and wipe confidential data. |
| 7266 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 7268 | psa_key_derivation_step_t step, |
| 7269 | psa_key_slot_t *private_key, |
| 7270 | const uint8_t *peer_key, |
| 7271 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7272 | { |
| 7273 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 7274 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7275 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7276 | 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] | 7277 | |
| 7278 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 7279 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7280 | status = psa_key_agreement_raw_internal(ka_alg, |
| 7281 | private_key, |
| 7282 | peer_key, peer_key_length, |
| 7283 | shared_secret, |
| 7284 | sizeof(shared_secret), |
| 7285 | &shared_secret_length); |
| 7286 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7287 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7288 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7289 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 7290 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7291 | * the shared secret. A shared secret is permitted wherever a key |
| 7292 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7293 | status = psa_key_derivation_input_internal(operation, step, |
| 7294 | PSA_KEY_TYPE_DERIVE, |
| 7295 | shared_secret, |
| 7296 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7297 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7298 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 7299 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7300 | } |
| 7301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7302 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 7303 | psa_key_derivation_step_t step, |
| 7304 | mbedtls_svc_key_id_t private_key, |
| 7305 | const uint8_t *peer_key, |
| 7306 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7307 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7309 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 7310 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7312 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 7313 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7314 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7315 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7316 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7317 | if (status != PSA_SUCCESS) { |
| 7318 | return status; |
| 7319 | } |
| 7320 | status = psa_key_agreement_internal(operation, step, |
| 7321 | slot, |
| 7322 | peer_key, peer_key_length); |
| 7323 | if (status != PSA_SUCCESS) { |
| 7324 | psa_key_derivation_abort(operation); |
| 7325 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7326 | /* If a private key has been added as SECRET, we allow the derived |
| 7327 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7328 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7329 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7330 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7331 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7332 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7333 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7335 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 7336 | } |
| 7337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7338 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 7339 | mbedtls_svc_key_id_t private_key, |
| 7340 | const uint8_t *peer_key, |
| 7341 | size_t peer_key_length, |
| 7342 | uint8_t *output, |
| 7343 | size_t output_size, |
| 7344 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7345 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7346 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7347 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7348 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7349 | size_t expected_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7351 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7352 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 7353 | goto exit; |
| 7354 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7355 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7356 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 7357 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7358 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7359 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7360 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7361 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 7362 | * for the output size. The PSA specification only guarantees that this |
| 7363 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 7364 | * but it might be nice to allow smaller buffers if the output fits. |
| 7365 | * At the time of writing this comment, with only ECDH implemented, |
| 7366 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 7367 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 7368 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7369 | expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7370 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 7371 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7372 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 7373 | goto exit; |
| 7374 | } |
| 7375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7376 | status = psa_key_agreement_raw_internal(alg, slot, |
| 7377 | peer_key, peer_key_length, |
| 7378 | output, output_size, |
| 7379 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7380 | |
| 7381 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7382 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7383 | /* If an error happens and is not handled properly, the output |
| 7384 | * may be used as a key to protect sensitive data. Arrange for such |
| 7385 | * a key to be random, which is likely to result in decryption or |
| 7386 | * verification errors. This is better than filling the buffer with |
| 7387 | * some constant data such as zeros, which would result in the data |
| 7388 | * being protected with a reproducible, easily knowable key. |
| 7389 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7390 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7391 | *output_length = output_size; |
| 7392 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7393 | |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 7394 | unlock_status = psa_unregister_read(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7397 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 7398 | |
| 7399 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7400 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 7401 | /****************************************************************/ |
| 7402 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 7403 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7404 | |
Gilles Peskine | 672a771 | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 7405 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 7406 | #include "entropy_poll.h" |
| 7407 | #endif |
| 7408 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7409 | /** Initialize the PSA random generator. |
| 7410 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7411 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7412 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7413 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7414 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7415 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7416 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7417 | /* Set default configuration if |
| 7418 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7419 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7420 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7421 | } |
| 7422 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7423 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7424 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7426 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7427 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 7428 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 7429 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 7430 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | mbedtls_entropy_add_source(&rng->entropy, |
| 7432 | mbedtls_nv_seed_poll, NULL, |
| 7433 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 7434 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7435 | #endif |
| 7436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7437 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7438 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7439 | } |
| 7440 | |
| 7441 | /** Deinitialize the PSA random generator. |
| 7442 | */ |
Valerio Setti | 83e0de8 | 2023-11-24 12:13:05 +0100 | [diff] [blame] | 7443 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7444 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7445 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Valerio Setti | 83e0de8 | 2023-11-24 12:13:05 +0100 | [diff] [blame] | 7446 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7447 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Valerio Setti | 83e0de8 | 2023-11-24 12:13:05 +0100 | [diff] [blame] | 7448 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 7449 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7450 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7451 | } |
| 7452 | |
| 7453 | /** Seed the PSA random generator. |
| 7454 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7455 | 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] | 7456 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7457 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7458 | /* Do nothing: the external RNG seeds itself. */ |
| 7459 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7460 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7461 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7462 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7463 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 7464 | drbg_seed, sizeof(drbg_seed) - 1); |
| 7465 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7466 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7467 | } |
| 7468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7469 | psa_status_t psa_generate_random(uint8_t *output, |
| 7470 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7471 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7472 | GUARD_MODULE_INITIALIZED; |
| 7473 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7474 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7475 | |
| 7476 | size_t output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7477 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 7478 | output, output_size, |
| 7479 | &output_length); |
| 7480 | if (status != PSA_SUCCESS) { |
| 7481 | return status; |
| 7482 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7483 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 7484 | * for the external RNG interface. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7485 | if (output_length != output_size) { |
| 7486 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 7487 | } |
| 7488 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7489 | |
| 7490 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7492 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 7493 | size_t request_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7494 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 7495 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 7496 | output_size); |
| 7497 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 7498 | output, request_size); |
| 7499 | if (ret != 0) { |
| 7500 | return mbedtls_to_psa_error(ret); |
| 7501 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 7502 | output_size -= request_size; |
| 7503 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 7504 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7505 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7506 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7507 | } |
| 7508 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7509 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7510 | * |
| 7511 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 7512 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 7513 | * PSA API doesn't support passing an explicit state. |
| 7514 | * |
| 7515 | * In the non-external case, psa_generate_random() calls an |
| 7516 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 7517 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 7518 | * instead of doing this back-and-forth between the PSA API and the |
| 7519 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 7520 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7521 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7522 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7523 | int mbedtls_psa_get_random(void *p_rng, |
| 7524 | unsigned char *output, |
| 7525 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7526 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7527 | /* This function takes a pointer to the RNG state because that's what |
| 7528 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 7529 | * its own state internally and doesn't let the caller access that state. |
| 7530 | * So we just ignore the state parameter, and in practice we'll pass |
| 7531 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7532 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7533 | psa_status_t status = psa_generate_random(output, output_size); |
| 7534 | if (status == PSA_SUCCESS) { |
| 7535 | return 0; |
| 7536 | } else { |
| 7537 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 7538 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7539 | } |
| 7540 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7541 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7542 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7543 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 7544 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7545 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7546 | if (global_data.initialized) { |
| 7547 | return PSA_ERROR_NOT_PERMITTED; |
| 7548 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7550 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 7551 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 7552 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 7553 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7554 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7556 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7557 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7558 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7559 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7560 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7561 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7562 | * \param type The key type |
| 7563 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7564 | * |
| 7565 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7566 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7567 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 7568 | * The size in bits of the key is not valid. |
| 7569 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 7570 | * The type and/or the size in bits of the key or the combination of |
| 7571 | * the two is not supported. |
| 7572 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7573 | 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] | 7574 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7575 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 7576 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7578 | if (key_type_is_raw_bytes(type)) { |
| 7579 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 7580 | if (status != PSA_SUCCESS) { |
| 7581 | return status; |
| 7582 | } |
| 7583 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7584 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7585 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7586 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 7587 | return PSA_ERROR_NOT_SUPPORTED; |
| 7588 | } |
Waleed Elmelegy | d7bdbbe | 2023-07-20 16:26:58 +0000 | [diff] [blame] | 7589 | if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) { |
Waleed Elmelegy | ab57071 | 2023-07-05 16:40:58 +0000 | [diff] [blame] | 7590 | return PSA_ERROR_NOT_SUPPORTED; |
| 7591 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7592 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7593 | /* Accept only byte-aligned keys, for the same reasons as |
| 7594 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7595 | if (bits % 8 != 0) { |
| 7596 | return PSA_ERROR_NOT_SUPPORTED; |
| 7597 | } |
| 7598 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7599 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7600 | |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7601 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7602 | 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] | 7603 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7604 | return PSA_SUCCESS; |
| 7605 | } else |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7606 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7607 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7608 | #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7609 | 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] | 7610 | if (psa_is_dh_key_size_valid(bits) == 0) { |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7611 | return PSA_ERROR_NOT_SUPPORTED; |
| 7612 | } |
| 7613 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7614 | #endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7615 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7616 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7617 | } |
| 7618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7619 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7620 | } |
| 7621 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 7622 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7623 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7624 | 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] | 7625 | { |
| 7626 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7627 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7629 | if ((attributes->domain_parameters == NULL) && |
| 7630 | (attributes->domain_parameters_size != 0)) { |
| 7631 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7632 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7634 | if (key_type_is_raw_bytes(type)) { |
| 7635 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 7636 | if (status != PSA_SUCCESS) { |
| 7637 | return status; |
| 7638 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7639 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 7640 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7641 | if (type == PSA_KEY_TYPE_DES) { |
| 7642 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 7643 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 7644 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7645 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7646 | |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7647 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7648 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 7649 | return mbedtls_psa_rsa_generate_key(attributes, |
| 7650 | key_buffer, |
| 7651 | key_buffer_size, |
| 7652 | key_buffer_length); |
| 7653 | } else |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7654 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7655 | |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7656 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7657 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7658 | return mbedtls_psa_ecp_generate_key(attributes, |
| 7659 | key_buffer, |
| 7660 | key_buffer_size, |
| 7661 | key_buffer_length); |
| 7662 | } else |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7663 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7664 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7665 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7666 | if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7667 | return mbedtls_psa_ffdh_generate_key(attributes, |
| 7668 | key_buffer, |
| 7669 | key_buffer_size, |
| 7670 | key_buffer_length); |
| 7671 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7672 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7673 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7674 | (void) key_buffer_length; |
| 7675 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7676 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7678 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7679 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7681 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 7682 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7683 | { |
| 7684 | psa_status_t status; |
| 7685 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 7686 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7687 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7688 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 7689 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7690 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7691 | /* Reject any attempt to create a zero-length key so that we don't |
| 7692 | * 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] | 7693 | if (psa_get_key_bits(attributes) == 0) { |
| 7694 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7695 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7696 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7697 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7698 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 7699 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7700 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7702 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 7703 | &slot, &driver); |
| 7704 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7705 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7706 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7707 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7708 | /* 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] | 7709 | * storage ( thus not in the case of generating a key in a secure element |
| 7710 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 7711 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7712 | if (slot->key.data == NULL) { |
| 7713 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 7714 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7715 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7716 | attributes->core.type, attributes->core.bits); |
| 7717 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7718 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7719 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7720 | |
| 7721 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7722 | attributes->core.type, |
| 7723 | attributes->core.bits); |
| 7724 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7725 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7726 | attributes, &key_buffer_size); |
| 7727 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7728 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7729 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7730 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7732 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 7733 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7734 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7735 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7736 | } |
| 7737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7738 | status = psa_driver_wrapper_generate_key(attributes, |
| 7739 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7741 | if (status != PSA_SUCCESS) { |
| 7742 | psa_remove_key_data_from_memory(slot); |
| 7743 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7744 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7745 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7746 | if (status == PSA_SUCCESS) { |
| 7747 | status = psa_finish_key_creation(slot, driver, key); |
| 7748 | } |
| 7749 | if (status != PSA_SUCCESS) { |
| 7750 | psa_fail_key_creation(slot, driver); |
| 7751 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7753 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7754 | } |
| 7755 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7756 | /****************************************************************/ |
| 7757 | /* Module setup */ |
| 7758 | /****************************************************************/ |
| 7759 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7760 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7761 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7762 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 7763 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7764 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7765 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7766 | return PSA_ERROR_BAD_STATE; |
| 7767 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7768 | global_data.rng.entropy_init = entropy_init; |
| 7769 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7770 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7771 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7772 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7775 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7776 | psa_wipe_all_key_slots(); |
Valerio Setti | 83e0de8 | 2023-11-24 12:13:05 +0100 | [diff] [blame] | 7777 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7778 | mbedtls_psa_random_free(&global_data.rng); |
| 7779 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7780 | /* Wipe all remaining data, including configuration. |
| 7781 | * In particular, this sets all state indicator to the value |
| 7782 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7783 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7784 | |
| 7785 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7786 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7787 | } |
| 7788 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7789 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 7790 | /** Recover a transaction that was interrupted by a power failure. |
| 7791 | * |
| 7792 | * This function is called during initialization, before psa_crypto_init() |
| 7793 | * returns. If this function returns a failure status, the initialization |
| 7794 | * fails. |
| 7795 | */ |
| 7796 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7797 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7798 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7799 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7800 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 7801 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7802 | /* TODO - fall through to the failure case until this |
| 7803 | * is implemented. |
| 7804 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 7805 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7806 | default: |
| 7807 | /* We found an unsupported transaction in the storage. |
| 7808 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7809 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7810 | } |
| 7811 | } |
| 7812 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 7813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7814 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7815 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7816 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7817 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7818 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7819 | if (global_data.initialized != 0) { |
| 7820 | return PSA_SUCCESS; |
| 7821 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7822 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 7823 | /* Init drivers */ |
| 7824 | status = psa_driver_wrapper_init(); |
| 7825 | if (status != PSA_SUCCESS) { |
| 7826 | goto exit; |
| 7827 | } |
| 7828 | global_data.drivers_initialized = 1; |
| 7829 | |
Valerio Setti | 402cfba | 2023-11-13 10:24:32 +0100 | [diff] [blame] | 7830 | status = psa_initialize_key_slots(); |
| 7831 | if (status != PSA_SUCCESS) { |
| 7832 | goto exit; |
| 7833 | } |
| 7834 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7835 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7836 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7837 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 7839 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7840 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7841 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7842 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7843 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7844 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7845 | status = psa_crypto_load_transaction(); |
| 7846 | if (status == PSA_SUCCESS) { |
| 7847 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 7848 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7849 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7850 | } |
| 7851 | status = psa_crypto_stop_transaction(); |
| 7852 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7853 | /* There's no transaction to complete. It's all good. */ |
| 7854 | status = PSA_SUCCESS; |
| 7855 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7856 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7857 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7858 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7859 | global_data.initialized = 1; |
| 7860 | |
| 7861 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7862 | if (status != PSA_SUCCESS) { |
| 7863 | mbedtls_psa_crypto_free(); |
| 7864 | } |
| 7865 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7866 | } |
| 7867 | |
Yanray Wang | ffc3c48 | 2023-07-11 12:01:04 +0800 | [diff] [blame] | 7868 | #if defined(PSA_WANT_ALG_SOME_PAKE) |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 7869 | psa_status_t psa_crypto_driver_pake_get_password_len( |
| 7870 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7871 | size_t *password_len) |
| 7872 | { |
| 7873 | if (inputs->password_len == 0) { |
| 7874 | return PSA_ERROR_BAD_STATE; |
| 7875 | } |
| 7876 | |
| 7877 | *password_len = inputs->password_len; |
| 7878 | |
| 7879 | return PSA_SUCCESS; |
| 7880 | } |
| 7881 | |
| 7882 | psa_status_t psa_crypto_driver_pake_get_password( |
| 7883 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7884 | uint8_t *buffer, size_t buffer_size, size_t *buffer_length) |
| 7885 | { |
| 7886 | if (inputs->password_len == 0) { |
| 7887 | return PSA_ERROR_BAD_STATE; |
| 7888 | } |
| 7889 | |
| 7890 | if (buffer_size < inputs->password_len) { |
| 7891 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 7892 | } |
| 7893 | |
| 7894 | memcpy(buffer, inputs->password, inputs->password_len); |
| 7895 | *buffer_length = inputs->password_len; |
| 7896 | |
| 7897 | return PSA_SUCCESS; |
| 7898 | } |
| 7899 | |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7900 | psa_status_t psa_crypto_driver_pake_get_user_len( |
| 7901 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7902 | size_t *user_len) |
| 7903 | { |
| 7904 | if (inputs->user_len == 0) { |
| 7905 | return PSA_ERROR_BAD_STATE; |
| 7906 | } |
| 7907 | |
| 7908 | *user_len = inputs->user_len; |
| 7909 | |
| 7910 | return PSA_SUCCESS; |
| 7911 | } |
| 7912 | |
| 7913 | psa_status_t psa_crypto_driver_pake_get_user( |
| 7914 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7915 | 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] | 7916 | { |
| 7917 | if (inputs->user_len == 0) { |
| 7918 | return PSA_ERROR_BAD_STATE; |
| 7919 | } |
| 7920 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7921 | if (user_id_size < inputs->user_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7922 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 7923 | } |
| 7924 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7925 | memcpy(user_id, inputs->user, inputs->user_len); |
| 7926 | *user_id_len = inputs->user_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7927 | |
| 7928 | return PSA_SUCCESS; |
| 7929 | } |
| 7930 | |
| 7931 | psa_status_t psa_crypto_driver_pake_get_peer_len( |
| 7932 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7933 | size_t *peer_len) |
| 7934 | { |
| 7935 | if (inputs->peer_len == 0) { |
| 7936 | return PSA_ERROR_BAD_STATE; |
| 7937 | } |
| 7938 | |
| 7939 | *peer_len = inputs->peer_len; |
| 7940 | |
| 7941 | return PSA_SUCCESS; |
| 7942 | } |
| 7943 | |
| 7944 | psa_status_t psa_crypto_driver_pake_get_peer( |
| 7945 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7946 | 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] | 7947 | { |
| 7948 | if (inputs->peer_len == 0) { |
| 7949 | return PSA_ERROR_BAD_STATE; |
| 7950 | } |
| 7951 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7952 | if (peer_id_size < inputs->peer_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7953 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 7954 | } |
| 7955 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7956 | memcpy(peer_id, inputs->peer, inputs->peer_len); |
| 7957 | *peer_id_length = inputs->peer_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7958 | |
| 7959 | return PSA_SUCCESS; |
| 7960 | } |
| 7961 | |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 7962 | psa_status_t psa_crypto_driver_pake_get_cipher_suite( |
| 7963 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7964 | psa_pake_cipher_suite_t *cipher_suite) |
| 7965 | { |
| 7966 | if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) { |
| 7967 | return PSA_ERROR_BAD_STATE; |
| 7968 | } |
| 7969 | |
| 7970 | *cipher_suite = inputs->cipher_suite; |
| 7971 | |
| 7972 | return PSA_SUCCESS; |
| 7973 | } |
| 7974 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 7975 | psa_status_t psa_pake_setup( |
| 7976 | psa_pake_operation_t *operation, |
| 7977 | const psa_pake_cipher_suite_t *cipher_suite) |
| 7978 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 7979 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 7980 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 7981 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 7982 | status = PSA_ERROR_BAD_STATE; |
| 7983 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 7986 | if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 || |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 7987 | PSA_ALG_IS_HASH(cipher_suite->hash) == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 7988 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 7989 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 7990 | } |
| 7991 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 7992 | memset(&operation->data.inputs, 0, sizeof(operation->data.inputs)); |
| 7993 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 7994 | operation->alg = cipher_suite->algorithm; |
Przemek Stekiel | 656b259 | 2023-03-22 13:15:33 +0100 | [diff] [blame] | 7995 | operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type, |
| 7996 | cipher_suite->family, cipher_suite->bits); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 7997 | operation->data.inputs.cipher_suite = *cipher_suite; |
| 7998 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 7999 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8000 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8001 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | dde6a91 | 2023-01-26 08:46:37 +0100 | [diff] [blame] | 8002 | &operation->computation_stage.jpake; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8003 | |
David Horstmann | 16f0151 | 2023-06-14 17:21:07 +0100 | [diff] [blame] | 8004 | memset(computation_stage, 0, sizeof(*computation_stage)); |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8005 | computation_stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8006 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8007 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8008 | { |
| 8009 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8010 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8011 | } |
| 8012 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 8013 | operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS; |
| 8014 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8015 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8016 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8017 | psa_pake_abort(operation); |
| 8018 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8019 | } |
| 8020 | |
| 8021 | psa_status_t psa_pake_set_password_key( |
| 8022 | psa_pake_operation_t *operation, |
| 8023 | mbedtls_svc_key_id_t password) |
| 8024 | { |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8025 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8026 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8027 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8028 | psa_key_attributes_t attributes; |
| 8029 | psa_key_type_t type; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8030 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8031 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8032 | status = PSA_ERROR_BAD_STATE; |
| 8033 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8034 | } |
| 8035 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8036 | status = psa_get_and_lock_key_slot_with_policy(password, &slot, |
| 8037 | PSA_KEY_USAGE_DERIVE, |
Przemek Stekiel | d5d28a2 | 2023-01-26 10:46:05 +0100 | [diff] [blame] | 8038 | operation->alg); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8039 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8040 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8041 | } |
| 8042 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8043 | attributes = (psa_key_attributes_t) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8044 | .core = slot->attr |
| 8045 | }; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8046 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8047 | type = psa_get_key_type(&attributes); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8048 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8049 | if (type != PSA_KEY_TYPE_PASSWORD && |
| 8050 | type != PSA_KEY_TYPE_PASSWORD_HASH) { |
| 8051 | status = PSA_ERROR_INVALID_ARGUMENT; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8052 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8053 | } |
| 8054 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8055 | operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes); |
| 8056 | if (operation->data.inputs.password == NULL) { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8057 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8058 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes); |
| 8062 | operation->data.inputs.password_len = slot->key.bytes; |
Przemek Stekiel | 9dd2440 | 2023-01-26 15:06:09 +0100 | [diff] [blame] | 8063 | operation->data.inputs.attributes = attributes; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8064 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8065 | if (status != PSA_SUCCESS) { |
| 8066 | psa_pake_abort(operation); |
| 8067 | } |
Ryan Everett | 1b70a07 | 2024-01-04 10:32:49 +0000 | [diff] [blame] | 8068 | unlock_status = psa_unregister_read(slot); |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8069 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8070 | } |
| 8071 | |
| 8072 | psa_status_t psa_pake_set_user( |
| 8073 | psa_pake_operation_t *operation, |
| 8074 | const uint8_t *user_id, |
| 8075 | size_t user_id_len) |
| 8076 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8077 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8078 | |
| 8079 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8080 | status = PSA_ERROR_BAD_STATE; |
| 8081 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8082 | } |
| 8083 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8084 | if (user_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8085 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8086 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8087 | } |
| 8088 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8089 | if (operation->data.inputs.user_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8090 | status = PSA_ERROR_BAD_STATE; |
| 8091 | goto exit; |
| 8092 | } |
| 8093 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8094 | operation->data.inputs.user = mbedtls_calloc(1, user_id_len); |
| 8095 | if (operation->data.inputs.user == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8096 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8097 | goto exit; |
| 8098 | } |
| 8099 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8100 | memcpy(operation->data.inputs.user, user_id, user_id_len); |
| 8101 | operation->data.inputs.user_len = user_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8102 | |
| 8103 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8104 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8105 | psa_pake_abort(operation); |
| 8106 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8107 | } |
| 8108 | |
| 8109 | psa_status_t psa_pake_set_peer( |
| 8110 | psa_pake_operation_t *operation, |
| 8111 | const uint8_t *peer_id, |
| 8112 | size_t peer_id_len) |
| 8113 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8114 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8115 | |
| 8116 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8117 | status = PSA_ERROR_BAD_STATE; |
| 8118 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8119 | } |
| 8120 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8121 | if (peer_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8122 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8123 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8124 | } |
| 8125 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8126 | if (operation->data.inputs.peer_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8127 | status = PSA_ERROR_BAD_STATE; |
| 8128 | goto exit; |
| 8129 | } |
| 8130 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8131 | operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len); |
| 8132 | if (operation->data.inputs.peer == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8133 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8134 | goto exit; |
| 8135 | } |
| 8136 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8137 | memcpy(operation->data.inputs.peer, peer_id, peer_id_len); |
| 8138 | operation->data.inputs.peer_len = peer_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8139 | |
| 8140 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8141 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8142 | psa_pake_abort(operation); |
| 8143 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | psa_status_t psa_pake_set_role( |
| 8147 | psa_pake_operation_t *operation, |
| 8148 | psa_pake_role_t role) |
| 8149 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8150 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8151 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8152 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8153 | status = PSA_ERROR_BAD_STATE; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8154 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8155 | } |
| 8156 | |
Przemek Stekiel | 09104b8 | 2023-03-06 14:11:51 +0100 | [diff] [blame] | 8157 | switch (operation->alg) { |
| 8158 | #if defined(PSA_WANT_ALG_JPAKE) |
| 8159 | case PSA_ALG_JPAKE: |
| 8160 | if (role == PSA_PAKE_ROLE_NONE) { |
| 8161 | return PSA_SUCCESS; |
| 8162 | } |
| 8163 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8164 | break; |
| 8165 | #endif |
| 8166 | default: |
| 8167 | (void) role; |
| 8168 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8169 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8170 | } |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8171 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8172 | psa_pake_abort(operation); |
| 8173 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8174 | } |
| 8175 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8176 | /* Auxiliary function to convert core computation stage to single driver step. */ |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8177 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8178 | 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] | 8179 | psa_jpake_computation_stage_t *stage) |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8180 | { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8181 | psa_crypto_driver_pake_step_t key_share_step; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8182 | if (stage->round == PSA_JPAKE_FIRST) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8183 | int is_x1; |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8184 | |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8185 | if (stage->io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8186 | is_x1 = (stage->outputs < 1); |
| 8187 | } else { |
| 8188 | is_x1 = (stage->inputs < 1); |
| 8189 | } |
| 8190 | |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8191 | key_share_step = is_x1 ? |
| 8192 | PSA_JPAKE_X1_STEP_KEY_SHARE : |
| 8193 | PSA_JPAKE_X2_STEP_KEY_SHARE; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8194 | } else if (stage->round == PSA_JPAKE_SECOND) { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8195 | key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ? |
| 8196 | PSA_JPAKE_X2S_STEP_KEY_SHARE : |
| 8197 | PSA_JPAKE_X4S_STEP_KEY_SHARE; |
| 8198 | } else { |
| 8199 | return PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8200 | } |
Agathiyan Bragadeesh | 387bfa5 | 2023-07-17 17:01:33 +0100 | [diff] [blame] | 8201 | 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] | 8202 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8203 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8204 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8205 | static psa_status_t psa_pake_complete_inputs( |
| 8206 | psa_pake_operation_t *operation) |
| 8207 | { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8208 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8209 | /* Create copy of the inputs on stack as inputs share memory |
| 8210 | with the driver context which will be setup by the driver. */ |
| 8211 | psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8212 | |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8213 | if (inputs.password_len == 0) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8214 | return PSA_ERROR_BAD_STATE; |
| 8215 | } |
| 8216 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8217 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8218 | if (inputs.user_len == 0 || inputs.peer_len == 0) { |
| 8219 | return PSA_ERROR_BAD_STATE; |
| 8220 | } |
Przemek Stekiel | dff21d3 | 2023-02-14 20:09:10 +0100 | [diff] [blame] | 8221 | } |
| 8222 | |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8223 | /* Clear driver context */ |
| 8224 | mbedtls_platform_zeroize(&operation->data, sizeof(operation->data)); |
| 8225 | |
| 8226 | status = psa_driver_wrapper_pake_setup(operation, &inputs); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8227 | |
| 8228 | /* Driver is responsible for creating its own copy of the password. */ |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8229 | mbedtls_zeroize_and_free(inputs.password, inputs.password_len); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8230 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8231 | /* User and peer are translated to role. */ |
| 8232 | mbedtls_free(inputs.user); |
| 8233 | mbedtls_free(inputs.peer); |
| 8234 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8235 | if (status == PSA_SUCCESS) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8236 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8237 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | d93de32 | 2023-02-24 08:39:04 +0100 | [diff] [blame] | 8238 | operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8239 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8240 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8241 | { |
| 8242 | status = PSA_ERROR_NOT_SUPPORTED; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8243 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8244 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8245 | return status; |
| 8246 | } |
| 8247 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8248 | #if defined(PSA_WANT_ALG_JPAKE) |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8249 | static psa_status_t psa_jpake_prologue( |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8250 | psa_pake_operation_t *operation, |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8251 | psa_pake_step_t step, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8252 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8253 | { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8254 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
| 8255 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 8256 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 8257 | return PSA_ERROR_INVALID_ARGUMENT; |
| 8258 | } |
| 8259 | |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8260 | psa_jpake_computation_stage_t *computation_stage = |
| 8261 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8262 | |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8263 | if (computation_stage->round != PSA_JPAKE_FIRST && |
| 8264 | computation_stage->round != PSA_JPAKE_SECOND) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8265 | return PSA_ERROR_BAD_STATE; |
| 8266 | } |
| 8267 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8268 | /* Check that the step we are given is the one we were expecting */ |
| 8269 | if (step != computation_stage->step) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8270 | return PSA_ERROR_BAD_STATE; |
| 8271 | } |
| 8272 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8273 | if (step == PSA_PAKE_STEP_KEY_SHARE && |
| 8274 | computation_stage->inputs == 0 && |
| 8275 | computation_stage->outputs == 0) { |
| 8276 | /* Start of the round, so function decides whether we are inputting |
| 8277 | * or outputting */ |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8278 | computation_stage->io_mode = io_mode; |
| 8279 | } else if (computation_stage->io_mode != io_mode) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8280 | /* Middle of the round so the mode we are in must match the function |
| 8281 | * called by the user */ |
| 8282 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8283 | } |
| 8284 | |
| 8285 | return PSA_SUCCESS; |
| 8286 | } |
| 8287 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8288 | static psa_status_t psa_jpake_epilogue( |
| 8289 | psa_pake_operation_t *operation, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8290 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8291 | { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8292 | psa_jpake_computation_stage_t *stage = |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8293 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8294 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8295 | if (stage->step == PSA_PAKE_STEP_ZK_PROOF) { |
| 8296 | /* End of an input/output */ |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8297 | if (io_mode == PSA_JPAKE_INPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8298 | stage->inputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8299 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8300 | stage->io_mode = PSA_JPAKE_OUTPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8301 | } |
| 8302 | } |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8303 | if (io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8304 | stage->outputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8305 | if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8306 | stage->io_mode = PSA_JPAKE_INPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8307 | } |
| 8308 | } |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8309 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) && |
| 8310 | stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8311 | /* End of a round, move to the next round */ |
| 8312 | stage->inputs = 0; |
| 8313 | stage->outputs = 0; |
| 8314 | stage->round++; |
| 8315 | } |
| 8316 | stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8317 | } else { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8318 | stage->step++; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8319 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8320 | return PSA_SUCCESS; |
| 8321 | } |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8322 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8323 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8324 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8325 | psa_status_t psa_pake_output( |
| 8326 | psa_pake_operation_t *operation, |
| 8327 | psa_pake_step_t step, |
| 8328 | uint8_t *output, |
| 8329 | size_t output_size, |
| 8330 | size_t *output_length) |
| 8331 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8332 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8333 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8334 | *output_length = 0; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8335 | |
| 8336 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8337 | status = psa_pake_complete_inputs(operation); |
| 8338 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8339 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8340 | } |
| 8341 | } |
| 8342 | |
| 8343 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8344 | status = PSA_ERROR_BAD_STATE; |
| 8345 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8346 | } |
| 8347 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8348 | if (output_size == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8349 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8350 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8351 | } |
| 8352 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8353 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8354 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8355 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8356 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8357 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8358 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8359 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8360 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8361 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8362 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8363 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8364 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8365 | (void) step; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8366 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8367 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8368 | } |
| 8369 | |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8370 | status = psa_driver_wrapper_pake_output(operation, driver_step, |
| 8371 | output, output_size, output_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8372 | |
| 8373 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8374 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8375 | } |
| 8376 | |
| 8377 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8378 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8379 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8380 | status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8381 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8382 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8383 | } |
| 8384 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8385 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8386 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8387 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8388 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8389 | } |
| 8390 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8391 | return PSA_SUCCESS; |
| 8392 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8393 | psa_pake_abort(operation); |
| 8394 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8395 | } |
| 8396 | |
| 8397 | psa_status_t psa_pake_input( |
| 8398 | psa_pake_operation_t *operation, |
| 8399 | psa_pake_step_t step, |
| 8400 | const uint8_t *input, |
| 8401 | size_t input_length) |
| 8402 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8403 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8404 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8405 | const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg, |
| 8406 | operation->primitive, |
| 8407 | step); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8408 | |
| 8409 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8410 | status = psa_pake_complete_inputs(operation); |
| 8411 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8412 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8413 | } |
| 8414 | } |
| 8415 | |
| 8416 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8417 | status = PSA_ERROR_BAD_STATE; |
| 8418 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8419 | } |
| 8420 | |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8421 | if (input_length == 0 || input_length > max_input_length) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8422 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8423 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8424 | } |
| 8425 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8426 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8427 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8428 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8429 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8430 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8431 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8432 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8433 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8434 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8435 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8436 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8437 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8438 | (void) step; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8439 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8440 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8441 | } |
| 8442 | |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8443 | status = psa_driver_wrapper_pake_input(operation, driver_step, |
| 8444 | input, input_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8445 | |
| 8446 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8447 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8448 | } |
| 8449 | |
| 8450 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8451 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8452 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8453 | status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8454 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8455 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8456 | } |
| 8457 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8458 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8459 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8460 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8461 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8462 | } |
| 8463 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8464 | return PSA_SUCCESS; |
| 8465 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8466 | psa_pake_abort(operation); |
| 8467 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8468 | } |
| 8469 | |
| 8470 | psa_status_t psa_pake_get_implicit_key( |
| 8471 | psa_pake_operation_t *operation, |
| 8472 | psa_key_derivation_operation_t *output) |
| 8473 | { |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8474 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8475 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8476 | uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8477 | size_t shared_key_len = 0; |
| 8478 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8479 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8480 | status = PSA_ERROR_BAD_STATE; |
| 8481 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8482 | } |
| 8483 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8484 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8485 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8486 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | 083745e | 2023-02-23 17:28:23 +0100 | [diff] [blame] | 8487 | &operation->computation_stage.jpake; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8488 | if (computation_stage->round != PSA_JPAKE_FINISHED) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8489 | status = PSA_ERROR_BAD_STATE; |
| 8490 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8491 | } |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8492 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8493 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8494 | { |
| 8495 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8496 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8497 | } |
| 8498 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8499 | status = psa_driver_wrapper_pake_get_implicit_key(operation, |
| 8500 | shared_key, |
Przemek Stekiel | 6b64862 | 2023-02-19 22:55:33 +0100 | [diff] [blame] | 8501 | sizeof(shared_key), |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8502 | &shared_key_len); |
| 8503 | |
| 8504 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8505 | goto exit; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8506 | } |
| 8507 | |
| 8508 | status = psa_key_derivation_input_bytes(output, |
| 8509 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8510 | shared_key, |
| 8511 | shared_key_len); |
| 8512 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8513 | mbedtls_platform_zeroize(shared_key, sizeof(shared_key)); |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8514 | exit: |
| 8515 | abort_status = psa_pake_abort(operation); |
| 8516 | return status == PSA_SUCCESS ? abort_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | psa_status_t psa_pake_abort( |
| 8520 | psa_pake_operation_t *operation) |
| 8521 | { |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8522 | psa_status_t status = PSA_SUCCESS; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8523 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8524 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8525 | status = psa_driver_wrapper_pake_abort(operation); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8526 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8527 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8528 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
| 8529 | if (operation->data.inputs.password != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8530 | mbedtls_zeroize_and_free(operation->data.inputs.password, |
Przemek Stekiel | aa18342 | 2023-03-06 14:21:44 +0100 | [diff] [blame] | 8531 | operation->data.inputs.password_len); |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8532 | } |
| 8533 | if (operation->data.inputs.user != NULL) { |
| 8534 | mbedtls_free(operation->data.inputs.user); |
| 8535 | } |
| 8536 | if (operation->data.inputs.peer != NULL) { |
| 8537 | mbedtls_free(operation->data.inputs.peer); |
| 8538 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8539 | } |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8540 | memset(operation, 0, sizeof(psa_pake_operation_t)); |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8541 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8542 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8543 | } |
Tom Cosgrove | 6d62fac | 2023-05-10 14:40:05 +0100 | [diff] [blame] | 8544 | #endif /* PSA_WANT_ALG_SOME_PAKE */ |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8545 | |
David Horstmann | 00d7a04 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 8546 | /* Memory copying test hooks. These are called before input copy, after input |
| 8547 | * copy, before output copy and after output copy, respectively. |
| 8548 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 8549 | * while they are copied. */ |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8550 | #if defined(MBEDTLS_TEST_HOOKS) |
| 8551 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8552 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8553 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8554 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8555 | #endif |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8556 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8557 | /** Copy from an input buffer to a local copy. |
| 8558 | * |
| 8559 | * \param[in] input Pointer to input buffer. |
| 8560 | * \param[in] input_len Length of the input buffer. |
| 8561 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 8562 | * \param[out] input_copy_len Length of the local copy buffer. |
| 8563 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8564 | * copied. |
| 8565 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 8566 | * copy is too small to hold contents of the |
| 8567 | * input buffer. |
| 8568 | */ |
| 8569 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8570 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 8571 | uint8_t *input_copy, size_t input_copy_len) |
| 8572 | { |
| 8573 | if (input_len > input_copy_len) { |
David Horstmann | 49a7276 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 8574 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8575 | } |
| 8576 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8577 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8578 | if (psa_input_pre_copy_hook != NULL) { |
| 8579 | psa_input_pre_copy_hook(input, input_len); |
| 8580 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8581 | #endif |
| 8582 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8583 | if (input_len > 0) { |
| 8584 | memcpy(input_copy, input, input_len); |
| 8585 | } |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8586 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8587 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8588 | if (psa_input_post_copy_hook != NULL) { |
| 8589 | psa_input_post_copy_hook(input, input_len); |
| 8590 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8591 | #endif |
| 8592 | |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8593 | return PSA_SUCCESS; |
| 8594 | } |
| 8595 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8596 | /** Copy from a local output buffer into a user-supplied one. |
| 8597 | * |
| 8598 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 8599 | * \param[in] output_copy_len Length of the local buffer. |
| 8600 | * \param[out] output Pointer to user-supplied output buffer. |
| 8601 | * \param[out] output_len Length of the user-supplied output buffer. |
| 8602 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8603 | * copied. |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8604 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8605 | * user-supplied output buffer is too small to |
| 8606 | * hold the contents of the local buffer. |
| 8607 | */ |
| 8608 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8609 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 8610 | uint8_t *output, size_t output_len) |
| 8611 | { |
| 8612 | if (output_len < output_copy_len) { |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8613 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8614 | } |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8615 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8616 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8617 | if (psa_output_pre_copy_hook != NULL) { |
| 8618 | psa_output_pre_copy_hook(output, output_len); |
| 8619 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8622 | if (output_copy_len > 0) { |
| 8623 | memcpy(output, output_copy, output_copy_len); |
| 8624 | } |
| 8625 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8626 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8627 | if (psa_output_post_copy_hook != NULL) { |
| 8628 | psa_output_post_copy_hook(output, output_len); |
| 8629 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8630 | #endif |
| 8631 | |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8632 | return PSA_SUCCESS; |
| 8633 | } |
| 8634 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8635 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 8636 | psa_crypto_local_input_t *local_input) |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8637 | { |
| 8638 | psa_status_t status; |
| 8639 | |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8640 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8641 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8642 | if (input_len == 0) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8643 | return PSA_SUCCESS; |
| 8644 | } |
| 8645 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8646 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 8647 | if (local_input->buffer == NULL) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8648 | /* Since we dealt with the zero-length case above, we know that |
| 8649 | * a NULL return value means a failure of allocation. */ |
| 8650 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8651 | } |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8652 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8653 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8654 | local_input->length = input_len; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8655 | |
| 8656 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8657 | local_input->buffer, local_input->length); |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8658 | if (status != PSA_SUCCESS) { |
| 8659 | goto error; |
| 8660 | } |
| 8661 | |
| 8662 | return PSA_SUCCESS; |
| 8663 | |
| 8664 | error: |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8665 | mbedtls_free(local_input->buffer); |
| 8666 | local_input->buffer = NULL; |
| 8667 | local_input->length = 0; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8668 | return status; |
| 8669 | } |
| 8670 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8671 | 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] | 8672 | { |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8673 | mbedtls_free(local_input->buffer); |
| 8674 | local_input->buffer = NULL; |
| 8675 | local_input->length = 0; |
David Horstmann | e6042ff | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8678 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 8679 | psa_crypto_local_output_t *local_output) |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8680 | { |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8681 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8682 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8683 | if (output_len == 0) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8684 | return PSA_SUCCESS; |
| 8685 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8686 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 8687 | if (local_output->buffer == NULL) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8688 | /* Since we dealt with the zero-length case above, we know that |
| 8689 | * a NULL return value means a failure of allocation. */ |
| 8690 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8691 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8692 | local_output->length = output_len; |
| 8693 | local_output->original = output; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8694 | |
| 8695 | return PSA_SUCCESS; |
| 8696 | } |
| 8697 | |
Gabor Mezei | 1882c51 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 8698 | psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len, |
| 8699 | psa_crypto_local_output_t *local_output) |
| 8700 | { |
| 8701 | psa_status_t status; |
| 8702 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
| 8703 | |
| 8704 | if (output_len == 0) { |
| 8705 | return PSA_SUCCESS; |
| 8706 | } |
| 8707 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 8708 | if (local_output->buffer == NULL) { |
| 8709 | /* Since we dealt with the zero-length case above, we know that |
| 8710 | * a NULL return value means a failure of allocation. */ |
| 8711 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8712 | } |
| 8713 | local_output->length = output_len; |
| 8714 | local_output->original = output; |
| 8715 | |
| 8716 | status = psa_crypto_copy_input(output, output_len, |
| 8717 | local_output->buffer, local_output->length); |
| 8718 | if (status != PSA_SUCCESS) { |
| 8719 | goto error; |
| 8720 | } |
| 8721 | |
| 8722 | return PSA_SUCCESS; |
| 8723 | |
| 8724 | error: |
| 8725 | mbedtls_free(local_output->buffer); |
| 8726 | local_output->buffer = NULL; |
| 8727 | local_output->length = 0; |
| 8728 | return status; |
| 8729 | } |
| 8730 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8731 | 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] | 8732 | { |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 8733 | psa_status_t status; |
| 8734 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8735 | if (local_output->buffer == NULL) { |
| 8736 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8737 | return PSA_SUCCESS; |
| 8738 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8739 | if (local_output->original == NULL) { |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8740 | /* We have an internal copy but nothing to copy back to. */ |
| 8741 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 8742 | } |
| 8743 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8744 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 8745 | local_output->original, local_output->length); |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 8746 | if (status != PSA_SUCCESS) { |
| 8747 | return status; |
| 8748 | } |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8749 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8750 | mbedtls_free(local_output->buffer); |
| 8751 | local_output->buffer = NULL; |
| 8752 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8753 | |
| 8754 | return PSA_SUCCESS; |
| 8755 | } |
| 8756 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8757 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |