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" |
Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 73 | #include "md_psa.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 | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 247 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 248 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \ |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 249 | defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 250 | static int psa_is_dh_key_size_valid(size_t bits) |
| 251 | { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 252 | if (bits != 2048 && bits != 3072 && bits != 4096 && |
| 253 | bits != 6144 && bits != 8192) { |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | return 1; |
| 258 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 259 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT || |
Przemek Stekiel | 5341050 | 2023-04-28 13:18:43 +0200 | [diff] [blame] | 260 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY || |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 261 | PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 264 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 265 | /* Mbed TLS error codes can combine a high-level error code and a |
| 266 | * low-level error code. The low-level error usually reflects the |
| 267 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | int low_level_ret = -(-ret & 0x007f); |
| 269 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 270 | case 0: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 272 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 273 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 275 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 09a9e58 | 2023-08-31 11:05:22 +0100 | [diff] [blame] | 277 | case MBEDTLS_ERR_AES_BAD_INPUT_DATA: |
| 278 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 279 | #endif |
| 280 | |
| 281 | #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 282 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 283 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 284 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 285 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 286 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 288 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 290 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 292 | #endif |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 293 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 294 | #if defined(MBEDTLS_CAMELLIA_C) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 295 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 298 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 299 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 300 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 301 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 303 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 305 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 306 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 307 | #if defined(MBEDTLS_CHACHA20_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 308 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 310 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 311 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 312 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 313 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 315 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | return PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 317 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 318 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 319 | #if defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 322 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 324 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 326 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 328 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 330 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 332 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | return PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 334 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 337 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 338 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 339 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 340 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 342 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 343 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 345 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 347 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 348 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 349 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 350 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | return PSA_ERROR_NOT_SUPPORTED; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 352 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 353 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 354 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 355 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 356 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 358 | |
Dave Rodgman | 4f47f3d | 2023-08-31 12:10:00 +0100 | [diff] [blame] | 359 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 360 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | return PSA_ERROR_INVALID_SIGNATURE; |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 362 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 364 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 366 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 367 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 368 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 370 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 371 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 372 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 374 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 375 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 379 | #endif |
| 380 | |
Dave Rodgman | dea266f | 2023-08-31 11:52:43 +0100 | [diff] [blame] | 381 | #if defined(MBEDTLS_MD_LIGHT) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 382 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 384 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 386 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 388 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 389 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 391 | #endif |
| 392 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 393 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 394 | #if defined(MBEDTLS_BIGNUM_C) |
| 395 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 396 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 398 | #endif |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 399 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 401 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 403 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 405 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 407 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 409 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 411 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 413 | #endif |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 414 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 415 | #if defined(MBEDTLS_PK_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 416 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 418 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 419 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | return PSA_ERROR_INVALID_ARGUMENT; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 421 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \ |
| 422 | defined(MBEDTLS_PSA_ITS_FILE_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 423 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | return PSA_ERROR_STORAGE_FAILURE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 425 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 426 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 427 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 429 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 431 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 432 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 434 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 436 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 437 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 438 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
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_SIG_LEN_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 442 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 444 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 445 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 446 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 448 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 450 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 451 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 452 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 454 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 456 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 458 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 460 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 461 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 463 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 465 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 467 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 469 | #endif |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 470 | |
Dave Rodgman | 1dab445 | 2023-09-01 09:59:51 +0100 | [diff] [blame] | 471 | #if defined(MBEDTLS_ECP_LIGHT) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 472 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 473 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 475 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 477 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 479 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 480 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 482 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 484 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 486 | |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 487 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 488 | case MBEDTLS_ERR_ECP_IN_PROGRESS: |
| 489 | return PSA_OPERATION_INCOMPLETE; |
Dave Rodgman | 509b567 | 2023-08-16 19:26:23 +0100 | [diff] [blame] | 490 | #endif |
| 491 | #endif |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 492 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 493 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 495 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 496 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 501 | /** |
| 502 | * \brief For output buffers which contain "tags" |
| 503 | * (outputs that may be checked for validity like |
| 504 | * hashes, MACs and signatures), fill the unused |
| 505 | * part of the output buffer (the whole buffer on |
| 506 | * error, the trailing part on success) with |
| 507 | * something that isn't a valid tag (barring an |
| 508 | * attack on the tag and deliberately-crafted |
| 509 | * input), in case the caller doesn't check the |
| 510 | * return status properly. |
| 511 | * |
| 512 | * \param output_buffer Pointer to buffer to wipe. May not be NULL |
| 513 | * unless \p output_buffer_size is zero. |
| 514 | * \param status Status of function called to generate |
| 515 | * output_buffer originally |
| 516 | * \param output_buffer_size Size of output buffer. If zero, \p output_buffer |
| 517 | * could be NULL. |
| 518 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 519 | * less than \p output_buffer_size |
| 520 | */ |
| 521 | 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] | 522 | size_t output_buffer_size, size_t output_buffer_length) |
| 523 | { |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 524 | size_t offset = 0; |
| 525 | |
| 526 | if (output_buffer_size == 0) { |
| 527 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 528 | call memset because output_buffer may be NULL in this case */ |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (status == PSA_SUCCESS) { |
| 533 | offset = output_buffer_length; |
| 534 | } |
| 535 | |
| 536 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 537 | } |
| 538 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 539 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 540 | |
| 541 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 542 | /****************************************************************/ |
| 543 | /* Key management */ |
| 544 | /****************************************************************/ |
| 545 | |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 546 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 547 | psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid, |
| 548 | size_t *bits) |
| 549 | { |
| 550 | switch (grpid) { |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 551 | #if defined(MBEDTLS_ECP_HAVE_SECP192R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 552 | case MBEDTLS_ECP_DP_SECP192R1: |
| 553 | *bits = 192; |
| 554 | return PSA_ECC_FAMILY_SECP_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 555 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_ECP_HAVE_SECP224R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 557 | case MBEDTLS_ECP_DP_SECP224R1: |
| 558 | *bits = 224; |
| 559 | return PSA_ECC_FAMILY_SECP_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 560 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 561 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 562 | case MBEDTLS_ECP_DP_SECP256R1: |
| 563 | *bits = 256; |
| 564 | return PSA_ECC_FAMILY_SECP_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 565 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 566 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 567 | case MBEDTLS_ECP_DP_SECP384R1: |
| 568 | *bits = 384; |
| 569 | return PSA_ECC_FAMILY_SECP_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 570 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 571 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 572 | case MBEDTLS_ECP_DP_SECP521R1: |
| 573 | *bits = 521; |
| 574 | return PSA_ECC_FAMILY_SECP_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 575 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 576 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 577 | case MBEDTLS_ECP_DP_BP256R1: |
| 578 | *bits = 256; |
| 579 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 580 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 581 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 582 | case MBEDTLS_ECP_DP_BP384R1: |
| 583 | *bits = 384; |
| 584 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 585 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 586 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 587 | case MBEDTLS_ECP_DP_BP512R1: |
| 588 | *bits = 512; |
| 589 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 590 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 591 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 592 | case MBEDTLS_ECP_DP_CURVE25519: |
| 593 | *bits = 255; |
| 594 | return PSA_ECC_FAMILY_MONTGOMERY; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 595 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 596 | #if defined(MBEDTLS_ECP_HAVE_SECP192K1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 597 | case MBEDTLS_ECP_DP_SECP192K1: |
| 598 | *bits = 192; |
| 599 | return PSA_ECC_FAMILY_SECP_K1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 600 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 601 | #if defined(MBEDTLS_ECP_HAVE_SECP224K1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 602 | case MBEDTLS_ECP_DP_SECP224K1: |
| 603 | *bits = 224; |
| 604 | return PSA_ECC_FAMILY_SECP_K1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 605 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 606 | #if defined(MBEDTLS_ECP_HAVE_SECP256K1) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 607 | case MBEDTLS_ECP_DP_SECP256K1: |
| 608 | *bits = 256; |
| 609 | return PSA_ECC_FAMILY_SECP_K1; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 610 | #endif |
Valerio Setti | c437fae | 2023-09-08 14:58:03 +0200 | [diff] [blame] | 611 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 612 | case MBEDTLS_ECP_DP_CURVE448: |
| 613 | *bits = 448; |
| 614 | return PSA_ECC_FAMILY_MONTGOMERY; |
Dave Rodgman | 6f68203 | 2023-08-16 18:44:32 +0100 | [diff] [blame] | 615 | #endif |
Valerio Setti | bc2b1d3 | 2023-06-19 12:15:13 +0200 | [diff] [blame] | 616 | default: |
| 617 | *bits = 0; |
| 618 | return 0; |
| 619 | } |
| 620 | } |
| 621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 623 | size_t bits, |
| 624 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 625 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 627 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 629 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 630 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 632 | #endif |
| 633 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 634 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 636 | #endif |
| 637 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 638 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 640 | #endif |
| 641 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 642 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 644 | #endif |
| 645 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 646 | case 521: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 648 | case 528: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | if (bits_is_sloppy) { |
| 650 | return MBEDTLS_ECP_DP_SECP521R1; |
| 651 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 652 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 653 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 654 | } |
| 655 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 656 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 657 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 659 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 660 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 662 | #endif |
| 663 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 664 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 666 | #endif |
| 667 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 668 | case 512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 670 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 671 | } |
| 672 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 673 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 674 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 676 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 677 | case 255: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 679 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 680 | if (bits_is_sloppy) { |
| 681 | return MBEDTLS_ECP_DP_CURVE25519; |
| 682 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 683 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 684 | #endif |
| 685 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 686 | case 448: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 688 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 689 | } |
| 690 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 691 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 692 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 694 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 695 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 697 | #endif |
| 698 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 699 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 701 | #endif |
| 702 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 703 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 705 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 706 | } |
| 707 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 708 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 709 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 710 | (void) bits_is_sloppy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 712 | } |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 713 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 716 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 717 | { |
| 718 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 720 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 721 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 722 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 723 | case PSA_KEY_TYPE_PASSWORD: |
| 724 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 725 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 726 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 727 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | if (bits != 128 && bits != 192 && bits != 256) { |
| 729 | return PSA_ERROR_INVALID_ARGUMENT; |
| 730 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 731 | break; |
| 732 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 733 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 734 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | if (bits != 128 && bits != 192 && bits != 256) { |
| 736 | return PSA_ERROR_INVALID_ARGUMENT; |
| 737 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 738 | break; |
| 739 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 740 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 741 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | if (bits != 128 && bits != 192 && bits != 256) { |
| 743 | return PSA_ERROR_INVALID_ARGUMENT; |
| 744 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 745 | break; |
| 746 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 747 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 748 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if (bits != 64 && bits != 128 && bits != 192) { |
| 750 | return PSA_ERROR_INVALID_ARGUMENT; |
| 751 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 752 | break; |
| 753 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 754 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 755 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | if (bits != 256) { |
| 757 | return PSA_ERROR_INVALID_ARGUMENT; |
| 758 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 759 | break; |
| 760 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 761 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 763 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | if (bits % 8 != 0) { |
| 765 | return PSA_ERROR_INVALID_ARGUMENT; |
| 766 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 771 | /** 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] | 772 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 773 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 774 | * 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] | 775 | * |
| 776 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 777 | * \param[in] key_type The key type of the key to be used with the |
| 778 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 779 | * |
| 780 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 781 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 782 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 783 | * 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] | 784 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 785 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 786 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 788 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 789 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 790 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 791 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 792 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 793 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 796 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 797 | * key. */ |
| 798 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 799 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 800 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 801 | * the block length (larger than 1) for block ciphers. */ |
| 802 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 803 | return PSA_SUCCESS; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 812 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 813 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | if (slot->key.data != NULL) { |
| 815 | return PSA_ERROR_ALREADY_EXISTS; |
| 816 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 818 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 819 | if (slot->key.data == NULL) { |
| 820 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 821 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 822 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 823 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 828 | const uint8_t *data, |
| 829 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 830 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 832 | data_length); |
| 833 | if (status != PSA_SUCCESS) { |
| 834 | return status; |
| 835 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | memcpy(slot->key.data, data, data_length); |
| 838 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 841 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 842 | const psa_key_attributes_t *attributes, |
| 843 | const uint8_t *data, size_t data_length, |
| 844 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 846 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 847 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 848 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 849 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 850 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | if (data_length == 0) { |
| 852 | return PSA_ERROR_NOT_SUPPORTED; |
| 853 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (key_type_is_raw_bytes(type)) { |
| 856 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 859 | *bits); |
| 860 | if (status != PSA_SUCCESS) { |
| 861 | return status; |
| 862 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 863 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 864 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 866 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | return PSA_SUCCESS; |
| 870 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 871 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 872 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 873 | if (PSA_KEY_TYPE_IS_DH(type)) { |
Przemek Stekiel | d1cf1ba | 2023-04-27 12:04:21 +0200 | [diff] [blame] | 874 | 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] | 875 | return PSA_ERROR_INVALID_ARGUMENT; |
| 876 | } |
Przemek Stekiel | 33c91eb | 2023-05-30 15:16:35 +0200 | [diff] [blame] | 877 | return mbedtls_psa_ffdh_import_key(attributes, |
| 878 | data, data_length, |
| 879 | key_buffer, key_buffer_size, |
| 880 | key_buffer_length, |
| 881 | bits); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 882 | } |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 883 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 884 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 885 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 887 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 888 | return mbedtls_psa_ecp_import_key(attributes, |
| 889 | data, data_length, |
| 890 | key_buffer, key_buffer_size, |
| 891 | key_buffer_length, |
| 892 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 893 | } |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 894 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 895 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 896 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 897 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 899 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 900 | return mbedtls_psa_rsa_import_key(attributes, |
| 901 | data, data_length, |
| 902 | key_buffer, key_buffer_size, |
| 903 | key_buffer_length, |
| 904 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 905 | } |
Valerio Setti | fe47890 | 2023-07-25 12:27:19 +0200 | [diff] [blame] | 906 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 907 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 908 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 909 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 914 | /** Calculate the intersection of two algorithm usage policies. |
| 915 | * |
| 916 | * Return 0 (which allows no operation) on incompatibility. |
| 917 | */ |
| 918 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 919 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 920 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 922 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 923 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 924 | if (alg1 == alg2) { |
| 925 | return alg1; |
| 926 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 927 | /* If the policies are from the same hash-and-sign family, check |
| 928 | * 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] | 929 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 930 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 931 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 932 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 933 | return alg2; |
| 934 | } |
| 935 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 936 | return alg1; |
| 937 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 938 | } |
| 939 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 940 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 941 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 943 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 944 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 945 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 946 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 947 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 948 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 949 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 951 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 952 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 953 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 954 | } |
| 955 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 957 | (alg1_len <= alg2_len)) { |
| 958 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 959 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 960 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 961 | (alg2_len <= alg1_len)) { |
| 962 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | /* If the policies are from the same MAC family, check whether one |
| 966 | * of them is a minimum-MAC-length policy. Calculate the most |
| 967 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 969 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 970 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 971 | /* Validate the combination of key type and algorithm. Since the base |
| 972 | * 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] | 973 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 974 | return 0; |
| 975 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 976 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 977 | /* Get the (exact or at-least) output lengths for both sides of the |
| 978 | * requested intersection. None of the currently supported algorithms |
| 979 | * have an output length dependent on the actual key size, so setting it |
| 980 | * to a bogus value of 0 is currently OK. |
| 981 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 982 | * Note that for at-least-this-length wildcard algorithms, the output |
| 983 | * length is set to the shortest allowed length, which allows us to |
| 984 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 986 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 987 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 988 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 989 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 991 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 992 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 993 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 994 | |
| 995 | /* If only one is an at-least-this-length policy, the intersection would |
| 996 | * be the other (fixed-length) policy as long as said fixed length is |
| 997 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 999 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 1000 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 1002 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1003 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 1004 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 1005 | /* If none of them are wildcards, check whether they define the same tag |
| 1006 | * length. This is still possible here when one is default-length and |
| 1007 | * the other specific-length. Ensure to always return the |
| 1008 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1009 | if (alg1_len == alg2_len) { |
| 1010 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 1011 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1012 | } |
| 1013 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 1018 | psa_algorithm_t policy_alg, |
| 1019 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1020 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1021 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1022 | if (requested_alg == policy_alg) { |
| 1023 | return 1; |
| 1024 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 1025 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 1026 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1027 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 1029 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 1030 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 1031 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 1032 | } |
| 1033 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 1034 | * the requested algorithm, check the requested tag length to be |
| 1035 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1036 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 1037 | PSA_ALG_IS_AEAD(requested_alg) && |
| 1038 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 1039 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 1040 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 1041 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 1042 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 1043 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 1044 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 1045 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 1047 | PSA_ALG_IS_MAC(requested_alg) && |
| 1048 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 1049 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 1050 | /* Validate the combination of key type and algorithm. Since the policy |
| 1051 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 1053 | return 0; |
| 1054 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 1055 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 1056 | /* Get both the requested output length for the algorithm which is to be |
| 1057 | * verified, and the default output length for the base algorithm. |
| 1058 | * Note that none of the currently supported algorithms have an output |
| 1059 | * length dependent on actual key size, so setting it to a bogus value |
| 1060 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 1061 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 1063 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | key_type, 0, |
| 1065 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1066 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1067 | /* If the policy is default-length, only allow an algorithm with |
| 1068 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 1070 | return requested_output_length == default_output_length; |
| 1071 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1072 | |
| 1073 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 1074 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 1076 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 1077 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 1080 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 1081 | * check for the requested MAC length to be equal to or longer than the |
| 1082 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 1084 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 1085 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 1086 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1087 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1088 | /* 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] | 1089 | * a key derivation with that key agreement should also be allowed. This |
| 1090 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 1092 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 1093 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 1094 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1095 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1096 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1100 | /** Test whether a policy permits an algorithm. |
| 1101 | * |
| 1102 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1103 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 1104 | * \note This function requires providing the key type for which the policy is |
| 1105 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 1106 | * have different properties depending on what kind of cipher it is |
| 1107 | * combined with. |
| 1108 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1109 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 1110 | * allowed by the \p policy. |
| 1111 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 1112 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 1113 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1114 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1115 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 1116 | psa_key_type_t key_type, |
| 1117 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1118 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1119 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1120 | if (alg == 0) { |
| 1121 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1122 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1123 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1124 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 1126 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1127 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1129 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 1130 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 1131 | return PSA_SUCCESS; |
| 1132 | } else { |
| 1133 | return PSA_ERROR_NOT_PERMITTED; |
| 1134 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1137 | /** Restrict a key policy based on a constraint. |
| 1138 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 1139 | * \note This function requires providing the key type for which the policy is |
| 1140 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 1141 | * have different properties depending on what kind of cipher it is |
| 1142 | * combined with. |
| 1143 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1144 | * \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] | 1145 | * \param[in,out] policy The policy to restrict. |
| 1146 | * \param[in] constraint The policy constraint to apply. |
| 1147 | * |
| 1148 | * \retval #PSA_SUCCESS |
| 1149 | * \c *policy contains the intersection of the original value of |
| 1150 | * \c *policy and \c *constraint. |
| 1151 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1152 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1153 | * \c *policy is unchanged. |
| 1154 | */ |
| 1155 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1156 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1157 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1159 | { |
| 1160 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 1162 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1163 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 1165 | constraint->alg2); |
| 1166 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1167 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1168 | } |
| 1169 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1170 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1171 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1172 | policy->usage &= constraint->usage; |
| 1173 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1174 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
Przemek Stekiel | 061f694 | 2022-11-30 10:51:35 +0100 | [diff] [blame] | 1178 | /** Get the description of a key given its identifier and policy constraints |
| 1179 | * and lock it. |
| 1180 | * |
| 1181 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1182 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1183 | * zero, the algorithm is not checked. |
| 1184 | * |
| 1185 | * In case of a persistent key, the function loads the description of the key |
| 1186 | * into a key slot if not already done. |
| 1187 | * |
| 1188 | * On success, the returned key slot is locked. It is the responsibility of |
| 1189 | * the caller to unlock the key slot when it does not access it anymore. |
| 1190 | */ |
| 1191 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1192 | mbedtls_svc_key_id_t key, |
| 1193 | psa_key_slot_t **p_slot, |
| 1194 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1195 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1196 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1197 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 1198 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1201 | if (status != PSA_SUCCESS) { |
| 1202 | return status; |
| 1203 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1204 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1205 | |
| 1206 | /* Enforce that usage policy for the key slot contains all the flags |
| 1207 | * required by the usage parameter. There is one exception: public |
| 1208 | * keys can always be exported, so we treat public key objects as |
| 1209 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1211 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1215 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1216 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1217 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1218 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1219 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | if (alg != 0) { |
| 1221 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1222 | slot->attr.type, |
| 1223 | alg); |
| 1224 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1225 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1226 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1227 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1230 | |
| 1231 | error: |
| 1232 | *p_slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1236 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1237 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1238 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1239 | * |
| 1240 | * 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] | 1241 | * available, as opposed to a key in a secure element and/or to be used |
| 1242 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1243 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1244 | * This is a temporary function that may be used instead of |
| 1245 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1246 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1247 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1248 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1249 | * caller to unlock the key slot when it does not access it anymore. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1250 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1251 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1252 | mbedtls_svc_key_id_t key, |
| 1253 | psa_key_slot_t **p_slot, |
| 1254 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1255 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1256 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1257 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1258 | usage, alg); |
| 1259 | if (status != PSA_SUCCESS) { |
| 1260 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1261 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1264 | psa_unlock_key_slot(*p_slot); |
| 1265 | *p_slot = NULL; |
| 1266 | return PSA_ERROR_NOT_SUPPORTED; |
| 1267 | } |
| 1268 | |
| 1269 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1270 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | 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] | 1273 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | if (slot->key.data != NULL) { |
Tom Cosgrove | 52f7e18 | 2023-08-01 09:08:48 +0100 | [diff] [blame] | 1275 | mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1277 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1278 | slot->key.data = NULL; |
| 1279 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1284 | /** Completely wipe a slot in memory, including its policy. |
| 1285 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1287 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | /* |
| 1291 | * As the return error code may not be handled in case of multiple errors, |
| 1292 | * do our best to report an unexpected lock counter. Assert with |
| 1293 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 1294 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1295 | * function is called as part of the execution of a test suite, the |
| 1296 | * execution of the test suite is stopped in error if the assertion fails. |
| 1297 | */ |
| 1298 | if (slot->lock_count != 1) { |
| 1299 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1300 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1301 | } |
| 1302 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1303 | /* Multipart operations may still be using the key. This is safe |
| 1304 | * because all multipart operation objects are independent from |
| 1305 | * the key slot: if they need to access the key after the setup |
| 1306 | * phase, they have a copy of the key. Note that this means that |
| 1307 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1308 | /* At this point, key material and other type-specific content has |
| 1309 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1310 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | memset(slot, 0, sizeof(*slot)); |
| 1312 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1316 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1317 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1318 | psa_status_t status; /* status of the last operation */ |
| 1319 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1320 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1321 | psa_se_drv_table_entry_t *driver; |
| 1322 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1325 | return PSA_SUCCESS; |
| 1326 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1327 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1328 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1329 | * Get the description of the key in a key slot. In case of a persistent |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1330 | * key, this will load the key description from persistent memory if not |
| 1331 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1332 | * the key is operated by an SE or not and this information is needed by |
| 1333 | * the current implementation. |
| 1334 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1336 | if (status != PSA_SUCCESS) { |
| 1337 | return status; |
| 1338 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1339 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1340 | /* |
| 1341 | * If the key slot containing the key description is under access by the |
| 1342 | * library (apart from the present access), the key cannot be destroyed |
| 1343 | * yet. For the time being, just return in error. Eventually (to be |
| 1344 | * implemented), the key should be destroyed when all accesses have |
| 1345 | * stopped. |
| 1346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1347 | if (slot->lock_count > 1) { |
| 1348 | psa_unlock_key_slot(slot); |
| 1349 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1353 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1354 | * if we attempt it, depending on whether the key is merely read-only |
| 1355 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1356 | * Just do the best we can, which is to wipe the copy in memory |
| 1357 | * (done in this function's cleanup code). */ |
| 1358 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1359 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1362 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1364 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1365 | /* For a key in a secure element, we need to do three things: |
| 1366 | * remove the key file in internal storage, destroy the |
| 1367 | * key inside the secure element, and update the driver's |
| 1368 | * persistent data. Start a transaction that will encompass these |
| 1369 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1371 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1373 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | status = psa_crypto_save_transaction(); |
| 1375 | if (status != PSA_SUCCESS) { |
| 1376 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1377 | /* We should still try to destroy the key in the secure |
| 1378 | * element and the key metadata in storage. This is especially |
| 1379 | * important if the error is that the storage is full. |
| 1380 | * But how to do it exactly without risking an inconsistent |
| 1381 | * state after a reset? |
| 1382 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1383 | */ |
| 1384 | overall_status = status; |
| 1385 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1388 | status = psa_destroy_se_key(driver, |
| 1389 | psa_key_slot_get_slot_number(slot)); |
| 1390 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1391 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1393 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1394 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1395 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1396 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1397 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1398 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1399 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1400 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1402 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1403 | /* TODO: other slots may have a copy of the same key. We should |
| 1404 | * invalidate them. |
| 1405 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1406 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1407 | } |
| 1408 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1409 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1410 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | if (driver != NULL) { |
| 1412 | status = psa_save_se_persistent_data(driver); |
| 1413 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1414 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1415 | } |
| 1416 | status = psa_crypto_stop_transaction(); |
| 1417 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1418 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1420 | } |
| 1421 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1422 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1423 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1425 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1427 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | } |
| 1429 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1430 | } |
| 1431 | |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1432 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1433 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1434 | static psa_status_t psa_get_rsa_public_exponent( |
| 1435 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1437 | { |
| 1438 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1439 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1440 | uint8_t *buffer = NULL; |
| 1441 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1442 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1445 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1446 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1447 | } |
| 1448 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1449 | /* It's the default value, which is reported as an empty string, |
| 1450 | * so there's nothing to do. */ |
| 1451 | goto exit; |
| 1452 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | buflen = mbedtls_mpi_size(&mpi); |
| 1455 | buffer = mbedtls_calloc(1, buflen); |
| 1456 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1457 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1458 | goto exit; |
| 1459 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1461 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1462 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1464 | attributes->domain_parameters = buffer; |
| 1465 | attributes->domain_parameters_size = buflen; |
| 1466 | |
| 1467 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | mbedtls_mpi_free(&mpi); |
| 1469 | if (ret != 0) { |
| 1470 | mbedtls_free(buffer); |
| 1471 | } |
| 1472 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1473 | } |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1474 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1475 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1476 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1477 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1478 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1480 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1481 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1482 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1483 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1484 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1488 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1489 | if (status != PSA_SUCCESS) { |
| 1490 | return status; |
| 1491 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1492 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1493 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1495 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1496 | |
| 1497 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1498 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1499 | psa_set_key_slot_number(attributes, |
| 1500 | psa_key_slot_get_slot_number(slot)); |
| 1501 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1502 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | switch (slot->attr.type) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1505 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1506 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1507 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1508 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1509 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1510 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1511 | * is not yet implemented. |
| 1512 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1513 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1514 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1515 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1516 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1517 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | slot->attr.type, |
| 1519 | slot->key.data, |
| 1520 | slot->key.bytes, |
| 1521 | &rsa); |
| 1522 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1523 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1526 | status = psa_get_rsa_public_exponent(rsa, |
| 1527 | attributes); |
| 1528 | mbedtls_rsa_free(rsa); |
| 1529 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1530 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1531 | break; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1532 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 1533 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1534 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1535 | default: |
| 1536 | /* Nothing else to do. */ |
| 1537 | break; |
| 1538 | } |
| 1539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | if (status != PSA_SUCCESS) { |
| 1541 | psa_reset_key_attributes(attributes); |
| 1542 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1549 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1550 | psa_status_t psa_get_key_slot_number( |
| 1551 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1553 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1555 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | return PSA_SUCCESS; |
| 1557 | } else { |
| 1558 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1559 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1560 | } |
| 1561 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1564 | size_t key_buffer_size, |
| 1565 | uint8_t *data, |
| 1566 | size_t data_size, |
| 1567 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1568 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | if (key_buffer_size > data_size) { |
| 1570 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1571 | } |
| 1572 | memcpy(data, key_buffer, key_buffer_size); |
| 1573 | memset(data + key_buffer_size, 0, |
| 1574 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1575 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1579 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1580 | const psa_key_attributes_t *attributes, |
| 1581 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1582 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1583 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1584 | psa_key_type_t type = attributes->core.type; |
| 1585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1586 | if (key_type_is_raw_bytes(type) || |
| 1587 | PSA_KEY_TYPE_IS_RSA(type) || |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1588 | PSA_KEY_TYPE_IS_ECC(type) || |
| 1589 | PSA_KEY_TYPE_IS_DH(type)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | return psa_export_key_buffer_internal( |
| 1591 | key_buffer, key_buffer_size, |
| 1592 | data, data_size, data_length); |
| 1593 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1594 | /* This shouldn't happen in the reference implementation, but |
| 1595 | it is valid for a special-purpose implementation to omit |
| 1596 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1602 | uint8_t *data, |
| 1603 | size_t data_size, |
| 1604 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1605 | { |
| 1606 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1607 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1608 | psa_key_slot_t *slot; |
| 1609 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1610 | /* Reject a zero-length output buffer now, since this can never be a |
| 1611 | * valid key representation. This way we know that data must be a valid |
| 1612 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1613 | if (data_size == 0) { |
| 1614 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1615 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1616 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1617 | /* Set the key to empty now, so that even when there are errors, we always |
| 1618 | * set data_length to a value between 0 and data_size. On error, setting |
| 1619 | * the key to empty is a good choice because an empty key representation is |
| 1620 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1621 | *data_length = 0; |
| 1622 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1623 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1624 | * which don't require any flag, but |
| 1625 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1626 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1628 | PSA_KEY_USAGE_EXPORT, 0); |
| 1629 | if (status != PSA_SUCCESS) { |
| 1630 | return status; |
| 1631 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1632 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1633 | psa_key_attributes_t attributes = { |
| 1634 | .core = slot->attr |
| 1635 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1636 | status = psa_driver_wrapper_export_key(&attributes, |
| 1637 | slot->key.data, slot->key.bytes, |
| 1638 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1645 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1646 | const psa_key_attributes_t *attributes, |
| 1647 | const uint8_t *key_buffer, |
| 1648 | size_t key_buffer_size, |
| 1649 | uint8_t *data, |
| 1650 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1651 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1652 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1653 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1654 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1655 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1656 | (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) || |
| 1657 | PSA_KEY_TYPE_IS_DH(type))) { |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1658 | /* Exporting public -> public */ |
| 1659 | return psa_export_key_buffer_internal( |
| 1660 | key_buffer, key_buffer_size, |
| 1661 | data, data_size, data_length); |
| 1662 | } else if (PSA_KEY_TYPE_IS_RSA(type)) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1663 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1664 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1665 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1666 | key_buffer, |
| 1667 | key_buffer_size, |
| 1668 | data, |
| 1669 | data_size, |
| 1670 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1671 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1672 | /* We don't know how to convert a private RSA key to public. */ |
| 1673 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 1674 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1675 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1676 | } else if (PSA_KEY_TYPE_IS_ECC(type)) { |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1677 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1678 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1679 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1680 | key_buffer, |
| 1681 | key_buffer_size, |
| 1682 | data, |
| 1683 | data_size, |
| 1684 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1685 | #else |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1686 | /* We don't know how to convert a private ECC key to public */ |
| 1687 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 1688 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1689 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 1690 | } else if (PSA_KEY_TYPE_IS_DH(type)) { |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1691 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \ |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1692 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 1693 | return mbedtls_psa_ffdh_export_public_key(attributes, |
Przemek Stekiel | 134cc2e | 2023-05-05 10:13:37 +0200 | [diff] [blame] | 1694 | key_buffer, |
| 1695 | key_buffer_size, |
| 1696 | data, data_size, |
| 1697 | data_length); |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1698 | #else |
Przemek Stekiel | 472b3f3 | 2022-12-01 14:38:49 +0100 | [diff] [blame] | 1699 | return PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 1700 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 1701 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | } else { |
Przemek Stekiel | 0b11ee0 | 2023-05-16 13:26:06 +0200 | [diff] [blame] | 1703 | (void) key_buffer; |
| 1704 | (void) key_buffer_size; |
| 1705 | (void) data; |
| 1706 | (void) data_size; |
| 1707 | (void) data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1708 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1709 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1710 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1713 | uint8_t *data, |
| 1714 | size_t data_size, |
| 1715 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1716 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1717 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1718 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1719 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1720 | psa_key_attributes_t attributes; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1721 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1722 | /* Reject a zero-length output buffer now, since this can never be a |
| 1723 | * valid key representation. This way we know that data must be a valid |
| 1724 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | if (data_size == 0) { |
| 1726 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1727 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1728 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1729 | /* Set the key to empty now, so that even when there are errors, we always |
| 1730 | * set data_length to a value between 0 and data_size. On error, setting |
| 1731 | * the key to empty is a good choice because an empty key representation is |
| 1732 | * unlikely to be accepted anywhere. */ |
| 1733 | *data_length = 0; |
| 1734 | |
| 1735 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1737 | if (status != PSA_SUCCESS) { |
| 1738 | return status; |
| 1739 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1742 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1743 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1744 | } |
| 1745 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 1746 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1747 | .core = slot->attr |
| 1748 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1749 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1750 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1752 | |
| 1753 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1757 | } |
| 1758 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1759 | MBEDTLS_STATIC_ASSERT( |
| 1760 | (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1761 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1762 | MBEDTLS_STATIC_ASSERT( |
| 1763 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1764 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1765 | MBEDTLS_STATIC_ASSERT( |
| 1766 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1767 | "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] | 1768 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1769 | /** Validate that a key policy is internally well-formed. |
| 1770 | * |
| 1771 | * This function only rejects invalid policies. It does not validate the |
| 1772 | * consistency of the policy with respect to other attributes of the key |
| 1773 | * such as the key type. |
| 1774 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | 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] | 1776 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1778 | PSA_KEY_USAGE_COPY | |
| 1779 | PSA_KEY_USAGE_ENCRYPT | |
| 1780 | PSA_KEY_USAGE_DECRYPT | |
| 1781 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1782 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1783 | PSA_KEY_USAGE_SIGN_HASH | |
| 1784 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1785 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1786 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1787 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1788 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1793 | /** Validate the internal consistency of key attributes. |
| 1794 | * |
| 1795 | * This function only rejects invalid attribute values. If does not |
| 1796 | * validate the consistency of the attributes with any key data that may |
| 1797 | * be involved in the creation of the key. |
| 1798 | * |
| 1799 | * Call this function early in the key creation process. |
| 1800 | * |
| 1801 | * \param[in] attributes Key attributes for the new key. |
| 1802 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1803 | * NULL for a transparent key. |
| 1804 | * |
| 1805 | */ |
| 1806 | static psa_status_t psa_validate_key_attributes( |
| 1807 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1809 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1810 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1812 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | status = psa_validate_key_location(lifetime, p_drv); |
| 1815 | if (status != PSA_SUCCESS) { |
| 1816 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | status = psa_validate_key_persistence(lifetime); |
| 1820 | if (status != PSA_SUCCESS) { |
| 1821 | return status; |
| 1822 | } |
| 1823 | |
| 1824 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1825 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1826 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1827 | } |
| 1828 | } else { |
| 1829 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1830 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1835 | if (status != PSA_SUCCESS) { |
| 1836 | return status; |
| 1837 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1838 | |
| 1839 | /* Refuse to create overly large keys. |
| 1840 | * Note that this doesn't trigger on import if the attributes don't |
| 1841 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1842 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1844 | return PSA_ERROR_NOT_SUPPORTED; |
| 1845 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1847 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1849 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1850 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1851 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1856 | /** Prepare a key slot to receive key material. |
| 1857 | * |
| 1858 | * This function allocates a key slot and sets its metadata. |
| 1859 | * |
| 1860 | * If this function fails, call psa_fail_key_creation(). |
| 1861 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1862 | * This function is intended to be used as follows: |
| 1863 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1864 | * it with the specified attributes, and in case of a volatile key assign it |
| 1865 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1866 | * -# Populate the slot with the key material. |
| 1867 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1868 | * In case of failure at any step, stop the sequence and call |
| 1869 | * psa_fail_key_creation(). |
| 1870 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1871 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1872 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1873 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1874 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1875 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1876 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1877 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1878 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1879 | * |
| 1880 | * \retval #PSA_SUCCESS |
| 1881 | * The key slot is ready to receive key material. |
| 1882 | * \return If this function fails, the key slot is an invalid state. |
| 1883 | * 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] | 1884 | */ |
| 1885 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1886 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1887 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1888 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1890 | { |
| 1891 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1892 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1893 | psa_key_slot_t *slot; |
| 1894 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1895 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1896 | *p_drv = NULL; |
| 1897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1899 | if (status != PSA_SUCCESS) { |
| 1900 | return status; |
| 1901 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1904 | if (status != PSA_SUCCESS) { |
| 1905 | return status; |
| 1906 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1907 | slot = *p_slot; |
| 1908 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1909 | /* We're storing the declared bit-size of the key. It's up to each |
| 1910 | * creation mechanism to verify that this information is correct. |
| 1911 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1912 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1913 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1914 | * volatile key identifier associated to the slot returned to contain its |
| 1915 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1916 | |
| 1917 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1919 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1920 | slot->attr.id = volatile_key_id; |
| 1921 | #else |
| 1922 | slot->attr.id.key_id = volatile_key_id; |
| 1923 | #endif |
| 1924 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1925 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1926 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1927 | * external-only flags, query `attributes`. Thanks to the check |
| 1928 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1929 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1930 | * may have set. */ |
| 1931 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1933 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1934 | /* 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] | 1935 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1936 | * create the key file in internal storage, create the |
| 1937 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1938 | * persistent data. This is done by starting a transaction that will |
| 1939 | * encompass these three actions. |
| 1940 | * For registering a volatile key, we just need to find an appropriate |
| 1941 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1942 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1943 | /* The first thing to do is to find a slot number for the new key. |
| 1944 | * We save the slot number in persistent storage as part of the |
| 1945 | * transaction data. It will be needed to recover if the power |
| 1946 | * fails during the key creation process, to clean up on the secure |
| 1947 | * element side after restarting. Obtaining a slot number from the |
| 1948 | * secure element driver updates its persistent state, but we do not yet |
| 1949 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1950 | * 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] | 1951 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1952 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1954 | &slot_number); |
| 1955 | if (status != PSA_SUCCESS) { |
| 1956 | return status; |
| 1957 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1960 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1961 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1962 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1963 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1964 | status = psa_crypto_save_transaction(); |
| 1965 | if (status != PSA_SUCCESS) { |
| 1966 | (void) psa_crypto_stop_transaction(); |
| 1967 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1968 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1969 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1970 | |
| 1971 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1972 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1973 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1976 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1978 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1979 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1980 | |
Ryan Everett | 975d411 | 2023-11-16 13:37:51 +0000 | [diff] [blame] | 1981 | slot->status = PSA_SLOT_OCCUPIED; |
| 1982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1983 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1984 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1985 | |
| 1986 | /** Finalize the creation of a key once its key material has been set. |
| 1987 | * |
| 1988 | * This entails writing the key to persistent storage. |
| 1989 | * |
| 1990 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1991 | * See the documentation of psa_start_key_creation() for the intended use |
| 1992 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1993 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1994 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1995 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1996 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1997 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1998 | * \param[in,out] slot Pointer to the slot with key material. |
| 1999 | * \param[in] driver The secure element driver for the key, |
| 2000 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2001 | * \param[out] key On success, identifier of the key. Note that the |
| 2002 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2003 | * |
| 2004 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2005 | * The key was successfully created. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 2006 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 2007 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 2008 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 2009 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 2010 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 2011 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 2012 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2013 | * \return If this function fails, the key slot is an invalid state. |
| 2014 | * 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] | 2015 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2016 | static psa_status_t psa_finish_key_creation( |
| 2017 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2018 | psa_se_drv_table_entry_t *driver, |
| 2019 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2020 | { |
| 2021 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2022 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2023 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2024 | |
| 2025 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2026 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2027 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2029 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2030 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 2032 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 2033 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 2034 | sizeof(data.slot_number), |
| 2035 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 2036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 2038 | status = psa_save_persistent_key(&slot->attr, |
| 2039 | (uint8_t *) &data, |
| 2040 | sizeof(data)); |
| 2041 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2042 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2043 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2044 | /* Key material is saved in export representation in the slot, so |
| 2045 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2046 | status = psa_save_persistent_key(&slot->attr, |
| 2047 | slot->key.data, |
| 2048 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2049 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2050 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2051 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2052 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2053 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2054 | /* Finish the transaction for a key creation. This does not |
| 2055 | * happen when registering an existing key. Detect this case |
| 2056 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2057 | * creation of a persistent key in a secure element requires a transaction, |
| 2058 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | if (driver != NULL && |
| 2060 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 2061 | status = psa_save_se_persistent_data(driver); |
| 2062 | if (status != PSA_SUCCESS) { |
| 2063 | psa_destroy_persistent_key(slot->attr.id); |
| 2064 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2065 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2067 | } |
| 2068 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2071 | *key = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | status = psa_unlock_key_slot(slot); |
| 2073 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2074 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2075 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2076 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | /** Abort the creation of a key. |
| 2082 | * |
| 2083 | * You may call this function after calling psa_start_key_creation(), |
| 2084 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2085 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2086 | * See the documentation of psa_start_key_creation() for the intended use |
| 2087 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2088 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2089 | * \param[in,out] slot Pointer to the slot with key material. |
| 2090 | * \param[in] driver The secure element driver for the key, |
| 2091 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2092 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 2094 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2095 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2096 | (void) driver; |
| 2097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2099 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2101 | |
| 2102 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2103 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2104 | * element, and the failure happened later (when saving metadata |
| 2105 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2106 | * element. |
| 2107 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2108 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2109 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2110 | /* Abort the ongoing transaction if any (there may not be one if |
| 2111 | * the creation process failed before starting one, or if the |
| 2112 | * key creation is a registration of a key in a secure element). |
| 2113 | * Earlier functions must already have done what it takes to undo any |
| 2114 | * partial creation. All that's left is to update the transaction data |
| 2115 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2117 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2120 | } |
| 2121 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2122 | /** Validate optional attributes during key creation. |
| 2123 | * |
| 2124 | * Some key attributes are optional during key creation. If they are |
| 2125 | * specified in the attributes structure, check that they are consistent |
| 2126 | * with the data in the slot. |
| 2127 | * |
| 2128 | * This function should be called near the end of key creation, after |
| 2129 | * the slot in memory is fully populated but before saving persistent data. |
| 2130 | */ |
| 2131 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2132 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2134 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | if (attributes->core.type != 0) { |
| 2136 | if (attributes->core.type != slot->attr.type) { |
| 2137 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2138 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | if (attributes->domain_parameters_size != 0) { |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2142 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 2143 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2145 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2146 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2147 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2148 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2149 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2150 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | slot->attr.type, |
| 2152 | slot->key.data, |
| 2153 | slot->key.bytes, |
| 2154 | &rsa); |
| 2155 | if (status != PSA_SUCCESS) { |
| 2156 | return status; |
| 2157 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | mbedtls_mpi_init(&actual); |
| 2160 | mbedtls_mpi_init(&required); |
| 2161 | ret = mbedtls_rsa_export(rsa, |
| 2162 | NULL, NULL, NULL, NULL, &actual); |
| 2163 | mbedtls_rsa_free(rsa); |
| 2164 | mbedtls_free(rsa); |
| 2165 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2166 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | } |
| 2168 | ret = mbedtls_mpi_read_binary(&required, |
| 2169 | attributes->domain_parameters, |
| 2170 | attributes->domain_parameters_size); |
| 2171 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2172 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | } |
| 2174 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2175 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | } |
| 2177 | rsa_exit: |
| 2178 | mbedtls_mpi_free(&actual); |
| 2179 | mbedtls_mpi_free(&required); |
| 2180 | if (ret != 0) { |
| 2181 | return mbedtls_to_psa_error(ret); |
| 2182 | } |
| 2183 | } else |
Valerio Setti | b2bcedb | 2023-07-10 11:24:00 +0200 | [diff] [blame] | 2184 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 2185 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2186 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2187 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | if (attributes->core.bits != 0) { |
| 2193 | if (attributes->core.bits != slot->attr.bits) { |
| 2194 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2195 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2196 | } |
| 2197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2199 | } |
| 2200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 2202 | const uint8_t *data, |
| 2203 | size_t data_length, |
| 2204 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2205 | { |
| 2206 | psa_status_t status; |
| 2207 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2208 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2209 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2210 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2211 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2212 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2213 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2214 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2215 | * This also rejects any key which might be encoded as an empty string, |
| 2216 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | if (data_length == 0) { |
| 2218 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2219 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2220 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2221 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | if (data_length > SIZE_MAX / 8) { |
| 2223 | return PSA_ERROR_NOT_SUPPORTED; |
| 2224 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2227 | &slot, &driver); |
| 2228 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2229 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2231 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2232 | /* 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] | 2233 | * storage ( thus not in the case of importing a key in a secure element |
| 2234 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 2235 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2236 | if (slot->key.data == NULL) { |
| 2237 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2238 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | attributes, data, data_length, &storage_size); |
| 2240 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2241 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 2243 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2244 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 2245 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2246 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2248 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2249 | |
| 2250 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2251 | status = psa_driver_wrapper_import_key(attributes, |
| 2252 | data, data_length, |
| 2253 | slot->key.data, |
| 2254 | slot->key.bytes, |
| 2255 | &slot->key.bytes, &bits); |
| 2256 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2257 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2258 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2261 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2263 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2264 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2265 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2266 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2267 | /* Enforce a size limit, and in particular ensure that the bit |
| 2268 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 2270 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2271 | goto exit; |
| 2272 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2273 | status = psa_validate_optional_attributes(slot, attributes); |
| 2274 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2275 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2279 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | if (status != PSA_SUCCESS) { |
| 2281 | psa_fail_key_creation(slot, driver); |
| 2282 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2285 | } |
| 2286 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2287 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2288 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2290 | { |
| 2291 | psa_status_t status; |
| 2292 | psa_key_slot_t *slot = NULL; |
| 2293 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2294 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2295 | |
| 2296 | /* Leaving attributes unspecified is not currently supported. |
| 2297 | * It could make sense to query the key type and size from the |
| 2298 | * secure element, but not all secure elements support this |
| 2299 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2301 | return PSA_ERROR_NOT_SUPPORTED; |
| 2302 | } |
| 2303 | if (psa_get_key_bits(attributes) == 0) { |
| 2304 | return PSA_ERROR_NOT_SUPPORTED; |
| 2305 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2308 | &slot, &driver); |
| 2309 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2310 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2314 | |
| 2315 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | if (status != PSA_SUCCESS) { |
| 2317 | psa_fail_key_creation(slot, driver); |
| 2318 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2319 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2320 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | psa_close_key(key); |
| 2322 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2323 | } |
| 2324 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2327 | const psa_key_attributes_t *specified_attributes, |
| 2328 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2329 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2330 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2331 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2332 | psa_key_slot_t *source_slot = NULL; |
| 2333 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2334 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2335 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2336 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2337 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2338 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2339 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2340 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2342 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2343 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | status = psa_validate_optional_attributes(source_slot, |
| 2347 | specified_attributes); |
| 2348 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2349 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2351 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2352 | /* The target key type and number of bits have been validated by |
| 2353 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2354 | * equal to the ones of the source key. So it is safe to inherit |
| 2355 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2356 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2357 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2358 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2359 | |
| 2360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2362 | &actual_attributes.core.policy, |
| 2363 | &source_slot->attr.policy); |
| 2364 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2365 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2369 | &target_slot, &driver); |
| 2370 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2371 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2372 | } |
| 2373 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2374 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2375 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2376 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2377 | * the source key would need to be exported as plaintext and re-imported |
| 2378 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2379 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2380 | * appropriate API invocations from the application, if needed. |
| 2381 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2382 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2383 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2384 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2385 | /* |
| 2386 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2387 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2388 | * - For opaque keys this translates to an invocation of the drivers' |
| 2389 | * copy_key entry point through the dispatch layer. |
| 2390 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2392 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2393 | &storage_size); |
| 2394 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2395 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2399 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2400 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2404 | source_slot->key.data, |
| 2405 | source_slot->key.bytes, |
| 2406 | target_slot->key.data, |
| 2407 | target_slot->key.bytes, |
| 2408 | &target_slot->key.bytes); |
| 2409 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2410 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | } |
| 2412 | } else { |
| 2413 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2414 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | source_slot->key.bytes); |
| 2416 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2417 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2419 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2421 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | if (status != PSA_SUCCESS) { |
| 2423 | psa_fail_key_creation(target_slot, driver); |
| 2424 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2431 | |
| 2432 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2433 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2434 | /* Message digests */ |
| 2435 | /****************************************************************/ |
| 2436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2438 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2439 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | if (operation->id == 0) { |
| 2441 | return PSA_SUCCESS; |
| 2442 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2445 | operation->id = 0; |
| 2446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2447 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2451 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2452 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2453 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2454 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2455 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2456 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2457 | status = PSA_ERROR_BAD_STATE; |
| 2458 | goto exit; |
| 2459 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2462 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2463 | goto exit; |
| 2464 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2465 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2466 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2467 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2471 | |
| 2472 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (status != PSA_SUCCESS) { |
| 2474 | psa_hash_abort(operation); |
| 2475 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2476 | |
| 2477 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2481 | const uint8_t *input, |
| 2482 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2483 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2484 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2487 | status = PSA_ERROR_BAD_STATE; |
| 2488 | goto exit; |
| 2489 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2490 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2491 | /* Don't require hash implementations to behave correctly on a |
| 2492 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | if (input_length == 0) { |
| 2494 | return PSA_SUCCESS; |
| 2495 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2498 | |
| 2499 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2500 | if (status != PSA_SUCCESS) { |
| 2501 | psa_hash_abort(operation); |
| 2502 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2508 | uint8_t *hash, |
| 2509 | size_t hash_size, |
| 2510 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2511 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2512 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | if (operation->id == 0) { |
| 2514 | return PSA_ERROR_BAD_STATE; |
| 2515 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2516 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2517 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | operation, hash, hash_size, hash_length); |
| 2519 | psa_hash_abort(operation); |
| 2520 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2524 | const uint8_t *hash, |
| 2525 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2526 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2527 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2528 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2529 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | operation, |
| 2531 | actual_hash, sizeof(actual_hash), |
| 2532 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2535 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2538 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2539 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2540 | goto exit; |
| 2541 | } |
| 2542 | |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2543 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2544 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2546 | |
| 2547 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2549 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2550 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2557 | const uint8_t *input, size_t input_length, |
| 2558 | uint8_t *hash, size_t hash_size, |
| 2559 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2560 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2561 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2563 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2564 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2566 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2567 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2570 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2571 | const uint8_t *input, size_t input_length, |
| 2572 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2573 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2574 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2575 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2578 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2579 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2580 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2581 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | alg, input, input_length, |
| 2583 | actual_hash, sizeof(actual_hash), |
| 2584 | &actual_hash_length); |
| 2585 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2586 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2587 | } |
| 2588 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2589 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2590 | goto exit; |
| 2591 | } |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2592 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2593 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2595 | |
| 2596 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2597 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2598 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2602 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2603 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | if (source_operation->id == 0 || |
| 2605 | target_operation->id != 0) { |
| 2606 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2607 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2610 | target_operation); |
| 2611 | if (status != PSA_SUCCESS) { |
| 2612 | psa_hash_abort(target_operation); |
| 2613 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2616 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2617 | |
| 2618 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2619 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2620 | /* MAC */ |
| 2621 | /****************************************************************/ |
| 2622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2625 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | if (operation->id == 0) { |
| 2627 | return PSA_SUCCESS; |
| 2628 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2631 | operation->mac_size = 0; |
| 2632 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2633 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2638 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2639 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2640 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2642 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2643 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2645 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2648 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2649 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2650 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2651 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2652 | status = psa_mac_key_can_do(alg, key_type); |
| 2653 | if (status != PSA_SUCCESS) { |
| 2654 | return status; |
| 2655 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2656 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2657 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2660 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2661 | /* A very short MAC is too short for security since it can be |
| 2662 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2663 | * so we make this our minimum, even though 32 bits is still |
| 2664 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2665 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2666 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2669 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2670 | /* It's impossible to "truncate" to a larger length than the full length |
| 2671 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2675 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2676 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2677 | * that is disabled in the compile-time configuration. The result can |
| 2678 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2679 | * configuration into account. In this case, force a return of |
| 2680 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2681 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2682 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2683 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2684 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2686 | } |
| 2687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2689 | } |
| 2690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2692 | mbedtls_svc_key_id_t key, |
| 2693 | psa_algorithm_t alg, |
| 2694 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2695 | { |
| 2696 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2697 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2698 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2699 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2700 | |
| 2701 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2703 | status = PSA_ERROR_BAD_STATE; |
| 2704 | goto exit; |
| 2705 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2706 | |
| 2707 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | key, |
| 2709 | &slot, |
| 2710 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2711 | alg); |
| 2712 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2713 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2714 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2715 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2716 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2717 | .core = slot->attr |
| 2718 | }; |
| 2719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2721 | &operation->mac_size); |
| 2722 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2723 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2725 | |
| 2726 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2727 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | if (is_sign) { |
| 2729 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2730 | &attributes, |
| 2731 | slot->key.data, |
| 2732 | slot->key.bytes, |
| 2733 | alg); |
| 2734 | } else { |
| 2735 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2736 | &attributes, |
| 2737 | slot->key.data, |
| 2738 | slot->key.bytes, |
| 2739 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2741 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2742 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | if (status != PSA_SUCCESS) { |
| 2744 | psa_mac_abort(operation); |
| 2745 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2747 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2750 | } |
| 2751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2753 | mbedtls_svc_key_id_t key, |
| 2754 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2755 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2757 | } |
| 2758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2760 | mbedtls_svc_key_id_t key, |
| 2761 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2762 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2764 | } |
| 2765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2767 | const uint8_t *input, |
| 2768 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2769 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | if (operation->id == 0) { |
| 2771 | return PSA_ERROR_BAD_STATE; |
| 2772 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2773 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2774 | /* Don't require hash implementations to behave correctly on a |
| 2775 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2776 | if (input_length == 0) { |
| 2777 | return PSA_SUCCESS; |
| 2778 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2781 | input, input_length); |
| 2782 | if (status != PSA_SUCCESS) { |
| 2783 | psa_mac_abort(operation); |
| 2784 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2786 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2787 | } |
| 2788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2790 | uint8_t *mac, |
| 2791 | size_t mac_size, |
| 2792 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2793 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2794 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2795 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2798 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2799 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2800 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2803 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2804 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2805 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2806 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2807 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2808 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2810 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2811 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2812 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2815 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2816 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2817 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2820 | mac, operation->mac_size, |
| 2821 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2822 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2823 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2824 | /* In case of success, set the potential excess room in the output buffer |
| 2825 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2826 | * In case of error, set the output length and content to a safe default, |
| 2827 | * such that in case the caller misses an error check, the output would be |
| 2828 | * an unachievable MAC. |
| 2829 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2831 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2832 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2833 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2834 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2835 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2839 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2843 | const uint8_t *mac, |
| 2844 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2845 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2846 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2847 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2850 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2851 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2852 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2855 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2856 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2857 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2860 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2861 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2864 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2865 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2866 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2867 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2874 | psa_algorithm_t alg, |
| 2875 | const uint8_t *input, |
| 2876 | size_t input_length, |
| 2877 | uint8_t *mac, |
| 2878 | size_t mac_size, |
| 2879 | size_t *mac_length, |
| 2880 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2881 | { |
| 2882 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2883 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2884 | psa_key_slot_t *slot; |
| 2885 | uint8_t operation_mac_size = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2886 | psa_key_attributes_t attributes; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2887 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2888 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | key, |
| 2890 | &slot, |
| 2891 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2892 | alg); |
| 2893 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2894 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2896 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 2897 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2898 | .core = slot->attr |
| 2899 | }; |
| 2900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2902 | &operation_mac_size); |
| 2903 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2904 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2905 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2908 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2909 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | &attributes, |
| 2914 | slot->key.data, slot->key.bytes, |
| 2915 | alg, |
| 2916 | input, input_length, |
| 2917 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2918 | |
| 2919 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2920 | /* In case of success, set the potential excess room in the output buffer |
| 2921 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2922 | * In case of error, set the output length and content to a safe default, |
| 2923 | * such that in case the caller misses an error check, the output would be |
| 2924 | * an unachievable MAC. |
| 2925 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2927 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2928 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2929 | } |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2930 | |
| 2931 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | 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] | 2939 | psa_algorithm_t alg, |
| 2940 | const uint8_t *input, |
| 2941 | size_t input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | uint8_t *mac, |
| 2943 | size_t mac_size, |
| 2944 | size_t *mac_length) |
| 2945 | { |
| 2946 | return psa_mac_compute_internal(key, alg, |
| 2947 | input, input_length, |
| 2948 | mac, mac_size, mac_length, 1); |
| 2949 | } |
| 2950 | |
| 2951 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2952 | psa_algorithm_t alg, |
| 2953 | const uint8_t *input, |
| 2954 | size_t input_length, |
| 2955 | const uint8_t *mac, |
| 2956 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2957 | { |
| 2958 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2959 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2960 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | status = psa_mac_compute_internal(key, alg, |
| 2963 | input, input_length, |
| 2964 | actual_mac, sizeof(actual_mac), |
| 2965 | &actual_mac_length, 0); |
| 2966 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2967 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2968 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2971 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2972 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2973 | } |
Dave Rodgman | 7870115 | 2023-08-29 14:20:18 +0100 | [diff] [blame] | 2974 | if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2975 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2976 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2977 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2978 | |
| 2979 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2983 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2984 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2985 | /****************************************************************/ |
| 2986 | /* Asymmetric cryptography */ |
| 2987 | /****************************************************************/ |
| 2988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2990 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2991 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | if (input_is_message) { |
| 2993 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2994 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2995 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2998 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2999 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3000 | } |
| 3001 | } |
| 3002 | } else { |
| 3003 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 3004 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3005 | } |
| 3006 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3009 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 3012 | int input_is_message, |
| 3013 | psa_algorithm_t alg, |
| 3014 | const uint8_t *input, |
| 3015 | size_t input_length, |
| 3016 | uint8_t *signature, |
| 3017 | size_t signature_size, |
| 3018 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3019 | { |
| 3020 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3021 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3022 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3023 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3024 | |
| 3025 | *signature_length = 0; |
| 3026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3028 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3029 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3031 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3032 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 3033 | * 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] | 3034 | * buffer can in principle be empty since it doesn't actually have |
| 3035 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | if (signature_size == 0) { |
| 3037 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 3038 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3039 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3040 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | key, &slot, |
| 3042 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 3043 | PSA_KEY_USAGE_SIGN_HASH, |
| 3044 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3047 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3048 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3051 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3052 | goto exit; |
| 3053 | } |
| 3054 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3055 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3057 | }; |
| 3058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3060 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3061 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3062 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | signature, signature_size, signature_length); |
| 3064 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3065 | |
| 3066 | status = psa_driver_wrapper_sign_hash( |
| 3067 | &attributes, slot->key.data, slot->key.bytes, |
| 3068 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3070 | } |
| 3071 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3072 | |
| 3073 | exit: |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3074 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3075 | *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3080 | } |
| 3081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3082 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 3083 | int input_is_message, |
| 3084 | psa_algorithm_t alg, |
| 3085 | const uint8_t *input, |
| 3086 | size_t input_length, |
| 3087 | const uint8_t *signature, |
| 3088 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3089 | { |
| 3090 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3091 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3092 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3095 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3096 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3098 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3099 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | key, &slot, |
| 3101 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 3102 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3103 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | if (status != PSA_SUCCESS) { |
| 3106 | return status; |
| 3107 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3108 | |
| 3109 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3110 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3111 | }; |
| 3112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3114 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3115 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3116 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3117 | signature, signature_length); |
| 3118 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3119 | status = psa_driver_wrapper_verify_hash( |
| 3120 | &attributes, slot->key.data, slot->key.bytes, |
| 3121 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3123 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3128 | |
| 3129 | } |
| 3130 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3131 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3132 | const psa_key_attributes_t *attributes, |
| 3133 | const uint8_t *key_buffer, |
| 3134 | size_t key_buffer_size, |
| 3135 | psa_algorithm_t alg, |
| 3136 | const uint8_t *input, |
| 3137 | size_t input_length, |
| 3138 | uint8_t *signature, |
| 3139 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3141 | { |
| 3142 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3145 | size_t hash_length; |
| 3146 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3147 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3148 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3150 | input, input_length, |
| 3151 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3154 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3155 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3156 | |
| 3157 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | attributes, key_buffer, key_buffer_size, |
| 3159 | alg, hash, hash_length, |
| 3160 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3161 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3166 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3167 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3168 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3170 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | size_t signature_size, |
| 3172 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3173 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3174 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3175 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3176 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3177 | |
| 3178 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3179 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3180 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3181 | signature_size, signature_length); |
| 3182 | |
| 3183 | exit: |
| 3184 | LOCAL_INPUT_FREE(input_external, input); |
| 3185 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3186 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3189 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3190 | const psa_key_attributes_t *attributes, |
| 3191 | const uint8_t *key_buffer, |
| 3192 | size_t key_buffer_size, |
| 3193 | psa_algorithm_t alg, |
| 3194 | const uint8_t *input, |
| 3195 | size_t input_length, |
| 3196 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3197 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3198 | { |
| 3199 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3202 | size_t hash_length; |
| 3203 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3204 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3205 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3206 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3207 | input, input_length, |
| 3208 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3211 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3213 | |
| 3214 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | attributes, key_buffer, key_buffer_size, |
| 3216 | alg, hash, hash_length, |
| 3217 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3218 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3224 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3225 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | size_t input_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3227 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3228 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3229 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3230 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3231 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3232 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3233 | |
| 3234 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3235 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3236 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3237 | signature_length); |
| 3238 | |
| 3239 | exit: |
| 3240 | LOCAL_INPUT_FREE(input_external, input); |
| 3241 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3242 | |
| 3243 | return status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3244 | } |
| 3245 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3246 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3247 | const psa_key_attributes_t *attributes, |
| 3248 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3249 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3251 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3253 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3254 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3255 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3256 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3257 | return mbedtls_psa_rsa_sign_hash( |
| 3258 | attributes, |
| 3259 | key_buffer, key_buffer_size, |
| 3260 | alg, hash, hash_length, |
| 3261 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3262 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3263 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | } else { |
| 3265 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3266 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3268 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3269 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3271 | return mbedtls_psa_ecdsa_sign_hash( |
| 3272 | attributes, |
| 3273 | key_buffer, key_buffer_size, |
| 3274 | alg, hash, hash_length, |
| 3275 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3276 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3277 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | } else { |
| 3279 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3280 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3281 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3283 | (void) key_buffer; |
| 3284 | (void) key_buffer_size; |
| 3285 | (void) hash; |
| 3286 | (void) hash_length; |
| 3287 | (void) signature; |
| 3288 | (void) signature_size; |
| 3289 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3292 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3295 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3296 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3298 | uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | size_t signature_size, |
| 3300 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3301 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3302 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3303 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3304 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3305 | |
| 3306 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3307 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3308 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3309 | signature_size, signature_length); |
| 3310 | |
| 3311 | exit: |
| 3312 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3313 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3314 | |
| 3315 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3316 | } |
| 3317 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3318 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3319 | const psa_key_attributes_t *attributes, |
| 3320 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3321 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3323 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3325 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3326 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3327 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3329 | return mbedtls_psa_rsa_verify_hash( |
| 3330 | attributes, |
| 3331 | key_buffer, key_buffer_size, |
| 3332 | alg, hash, hash_length, |
| 3333 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3334 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3335 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | } else { |
| 3337 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3338 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3340 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3341 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3343 | return mbedtls_psa_ecdsa_verify_hash( |
| 3344 | attributes, |
| 3345 | key_buffer, key_buffer_size, |
| 3346 | alg, hash, hash_length, |
| 3347 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3348 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3349 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | } else { |
| 3351 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3352 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3353 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | (void) key_buffer; |
| 3356 | (void) key_buffer_size; |
| 3357 | (void) hash; |
| 3358 | (void) hash_length; |
| 3359 | (void) signature; |
| 3360 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3366 | psa_algorithm_t alg, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3367 | const uint8_t *hash_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | size_t hash_length, |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3369 | const uint8_t *signature_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3371 | { |
Thomas Daubney | 4f8847b | 2024-01-18 17:23:02 +0000 | [diff] [blame^] | 3372 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3373 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3374 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3375 | |
| 3376 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3377 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3378 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3379 | signature_length); |
| 3380 | |
| 3381 | exit: |
| 3382 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3383 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3384 | |
| 3385 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3389 | psa_algorithm_t alg, |
| 3390 | const uint8_t *input, |
| 3391 | size_t input_length, |
| 3392 | const uint8_t *salt, |
| 3393 | size_t salt_length, |
| 3394 | uint8_t *output, |
| 3395 | size_t output_size, |
| 3396 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3397 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3398 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3399 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3400 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3401 | psa_key_attributes_t attributes; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3402 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3403 | (void) input; |
| 3404 | (void) input_length; |
| 3405 | (void) salt; |
| 3406 | (void) output; |
| 3407 | (void) output_size; |
| 3408 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3409 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3412 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3413 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3414 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3415 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3417 | if (status != PSA_SUCCESS) { |
| 3418 | return status; |
| 3419 | } |
| 3420 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3421 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3422 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3423 | goto exit; |
| 3424 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3425 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3426 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3428 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3429 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3430 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3431 | &attributes, slot->key.data, slot->key.bytes, |
| 3432 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3433 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3434 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3435 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3438 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3440 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3441 | psa_algorithm_t alg, |
| 3442 | const uint8_t *input, |
| 3443 | size_t input_length, |
| 3444 | const uint8_t *salt, |
| 3445 | size_t salt_length, |
| 3446 | uint8_t *output, |
| 3447 | size_t output_size, |
| 3448 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3449 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3450 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3451 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3452 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3453 | psa_key_attributes_t attributes; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3454 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3455 | (void) input; |
| 3456 | (void) input_length; |
| 3457 | (void) salt; |
| 3458 | (void) output; |
| 3459 | (void) output_size; |
| 3460 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3461 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3463 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3464 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3465 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3466 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3467 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3468 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3469 | if (status != PSA_SUCCESS) { |
| 3470 | return status; |
| 3471 | } |
| 3472 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3473 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3474 | goto exit; |
| 3475 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3476 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3477 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3479 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3480 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3481 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3482 | &attributes, slot->key.data, slot->key.bytes, |
| 3483 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3484 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3485 | |
| 3486 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3487 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3490 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3491 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3492 | /****************************************************************/ |
| 3493 | /* Asymmetric interruptible cryptography */ |
| 3494 | /****************************************************************/ |
| 3495 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3496 | static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3497 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3498 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3499 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3500 | psa_interruptible_max_ops = max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | uint32_t psa_interruptible_get_max_ops(void) |
| 3504 | { |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3505 | return psa_interruptible_max_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3508 | uint32_t psa_sign_hash_get_num_ops( |
| 3509 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3510 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3511 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | uint32_t psa_verify_hash_get_num_ops( |
| 3515 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3516 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3517 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3520 | static psa_status_t psa_sign_hash_abort_internal( |
| 3521 | psa_sign_hash_interruptible_operation_t *operation) |
| 3522 | { |
| 3523 | if (operation->id == 0) { |
| 3524 | /* The object has (apparently) been initialized but it is not (yet) |
| 3525 | * in use. It's ok to call abort on such an object, and there's |
| 3526 | * nothing to do. */ |
| 3527 | return PSA_SUCCESS; |
| 3528 | } |
| 3529 | |
| 3530 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3531 | |
| 3532 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3533 | |
| 3534 | operation->id = 0; |
| 3535 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3536 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3537 | * only want to be cleared by the application calling abort, not by abort |
| 3538 | * being called at completion of an operation. */ |
| 3539 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3540 | return status; |
| 3541 | } |
| 3542 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3543 | psa_status_t psa_sign_hash_start( |
| 3544 | psa_sign_hash_interruptible_operation_t *operation, |
| 3545 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3546 | const uint8_t *hash, size_t hash_length) |
| 3547 | { |
| 3548 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3549 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3550 | psa_key_slot_t *slot; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3551 | psa_key_attributes_t attributes; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3552 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3553 | /* Check that start has not been previously called, or operation has not |
| 3554 | * previously errored. */ |
| 3555 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3556 | return PSA_ERROR_BAD_STATE; |
| 3557 | } |
| 3558 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3559 | status = psa_sign_verify_check_alg(0, alg); |
| 3560 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3561 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3562 | return status; |
| 3563 | } |
| 3564 | |
| 3565 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3566 | PSA_KEY_USAGE_SIGN_HASH, |
| 3567 | alg); |
| 3568 | |
| 3569 | if (status != PSA_SUCCESS) { |
| 3570 | goto exit; |
| 3571 | } |
| 3572 | |
| 3573 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3574 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3575 | goto exit; |
| 3576 | } |
| 3577 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 3578 | attributes = (psa_key_attributes_t) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3579 | .core = slot->attr |
| 3580 | }; |
| 3581 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3582 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3583 | operation->num_ops = 0; |
| 3584 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3585 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3586 | slot->key.data, |
| 3587 | slot->key.bytes, alg, |
| 3588 | hash, hash_length); |
| 3589 | exit: |
| 3590 | |
| 3591 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3592 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3593 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | unlock_status = psa_unlock_key_slot(slot); |
| 3597 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3598 | if (unlock_status != PSA_SUCCESS) { |
| 3599 | operation->error_occurred = 1; |
| 3600 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3601 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3602 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
| 3606 | psa_status_t psa_sign_hash_complete( |
| 3607 | psa_sign_hash_interruptible_operation_t *operation, |
| 3608 | uint8_t *signature, size_t signature_size, |
| 3609 | size_t *signature_length) |
| 3610 | { |
| 3611 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3612 | |
| 3613 | *signature_length = 0; |
| 3614 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3615 | /* Check that start has been called first, and that operation has not |
| 3616 | * previously errored. */ |
| 3617 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3618 | status = PSA_ERROR_BAD_STATE; |
| 3619 | goto exit; |
| 3620 | } |
| 3621 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3622 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3623 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3624 | if (signature_size == 0) { |
| 3625 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3626 | goto exit; |
| 3627 | } |
| 3628 | |
| 3629 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3630 | signature_size, |
| 3631 | signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3632 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3633 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3634 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3635 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3636 | exit: |
| 3637 | |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3638 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3639 | *signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3640 | |
Paul Elliott | 53bb312 | 2023-02-10 14:22:22 +0000 | [diff] [blame] | 3641 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3642 | if (status != PSA_SUCCESS) { |
| 3643 | operation->error_occurred = 1; |
| 3644 | } |
| 3645 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3646 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | return status; |
| 3650 | } |
| 3651 | |
| 3652 | psa_status_t psa_sign_hash_abort( |
| 3653 | psa_sign_hash_interruptible_operation_t *operation) |
| 3654 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3655 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3656 | |
| 3657 | status = psa_sign_hash_abort_internal(operation); |
| 3658 | |
| 3659 | /* We clear the number of ops done here, so that it is not cleared when |
| 3660 | * the operation fails or succeeds, only on manual abort. */ |
| 3661 | operation->num_ops = 0; |
| 3662 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3663 | /* Likewise, failure state. */ |
| 3664 | operation->error_occurred = 0; |
| 3665 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3666 | return status; |
| 3667 | } |
| 3668 | |
| 3669 | static psa_status_t psa_verify_hash_abort_internal( |
| 3670 | psa_verify_hash_interruptible_operation_t *operation) |
| 3671 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3672 | if (operation->id == 0) { |
| 3673 | /* The object has (apparently) been initialized but it is not (yet) |
| 3674 | * in use. It's ok to call abort on such an object, and there's |
| 3675 | * nothing to do. */ |
| 3676 | return PSA_SUCCESS; |
| 3677 | } |
| 3678 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3679 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3680 | |
| 3681 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3682 | |
| 3683 | operation->id = 0; |
| 3684 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3685 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3686 | * only want to be cleared by the application calling abort, not by abort |
| 3687 | * being called at completion of an operation. */ |
| 3688 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3689 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | psa_status_t psa_verify_hash_start( |
| 3693 | psa_verify_hash_interruptible_operation_t *operation, |
| 3694 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3695 | const uint8_t *hash, size_t hash_length, |
| 3696 | const uint8_t *signature, size_t signature_length) |
| 3697 | { |
| 3698 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3699 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3700 | psa_key_slot_t *slot; |
| 3701 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3702 | /* Check that start has not been previously called, or operation has not |
| 3703 | * previously errored. */ |
| 3704 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3705 | return PSA_ERROR_BAD_STATE; |
| 3706 | } |
| 3707 | |
| 3708 | status = psa_sign_verify_check_alg(0, alg); |
| 3709 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3710 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3711 | return status; |
| 3712 | } |
| 3713 | |
| 3714 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3715 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3716 | alg); |
| 3717 | |
| 3718 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3719 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3720 | return status; |
| 3721 | } |
| 3722 | |
| 3723 | psa_key_attributes_t attributes = { |
| 3724 | .core = slot->attr |
| 3725 | }; |
| 3726 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3727 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3728 | operation->num_ops = 0; |
| 3729 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3730 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3731 | slot->key.data, |
| 3732 | slot->key.bytes, |
| 3733 | alg, hash, hash_length, |
| 3734 | signature, signature_length); |
| 3735 | |
| 3736 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3737 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3738 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | unlock_status = psa_unlock_key_slot(slot); |
| 3742 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3743 | if (unlock_status != PSA_SUCCESS) { |
| 3744 | operation->error_occurred = 1; |
| 3745 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3746 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3747 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | psa_status_t psa_verify_hash_complete( |
| 3751 | psa_verify_hash_interruptible_operation_t *operation) |
| 3752 | { |
| 3753 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3754 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3755 | /* Check that start has been called first, and that operation has not |
| 3756 | * previously errored. */ |
| 3757 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3758 | status = PSA_ERROR_BAD_STATE; |
| 3759 | goto exit; |
| 3760 | } |
| 3761 | |
| 3762 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3763 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3764 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3765 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3766 | operation); |
| 3767 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3768 | exit: |
| 3769 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3770 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3771 | if (status != PSA_SUCCESS) { |
| 3772 | operation->error_occurred = 1; |
| 3773 | } |
| 3774 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3775 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | return status; |
| 3779 | } |
| 3780 | |
| 3781 | psa_status_t psa_verify_hash_abort( |
| 3782 | psa_verify_hash_interruptible_operation_t *operation) |
| 3783 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3784 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3785 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3786 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3787 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3788 | /* We clear the number of ops done here, so that it is not cleared when |
| 3789 | * the operation fails or succeeds, only on manual abort. */ |
| 3790 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3791 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3792 | /* Likewise, failure state. */ |
| 3793 | operation->error_occurred = 0; |
| 3794 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3795 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3798 | /****************************************************************/ |
| 3799 | /* Asymmetric interruptible cryptography internal */ |
| 3800 | /* implementations */ |
| 3801 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3802 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3803 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3804 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3805 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3806 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3807 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3808 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3809 | |
| 3810 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3811 | * therefore avoid this value, and set to minimum possible. */ |
| 3812 | if (max_ops == 0) { |
| 3813 | max_ops = 1; |
| 3814 | } |
| 3815 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3816 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3817 | #else |
| 3818 | (void) max_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3819 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3820 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3821 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3822 | } |
| 3823 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3824 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3825 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3826 | { |
| 3827 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3828 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3829 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3830 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3831 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3832 | #else |
| 3833 | (void) operation; |
| 3834 | return 0; |
| 3835 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3836 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3837 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3838 | } |
| 3839 | |
| 3840 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3841 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3842 | { |
| 3843 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3844 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3845 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3846 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3847 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3848 | #else |
| 3849 | (void) operation; |
| 3850 | return 0; |
| 3851 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3852 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3853 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3854 | } |
| 3855 | |
| 3856 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3857 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3858 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3859 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3860 | const uint8_t *hash, size_t hash_length) |
| 3861 | { |
| 3862 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3863 | size_t required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3864 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3865 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3866 | return PSA_ERROR_NOT_SUPPORTED; |
| 3867 | } |
| 3868 | |
| 3869 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3870 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3871 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3872 | |
| 3873 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3874 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3875 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3876 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3877 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3878 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3879 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3880 | operation->num_ops = 0; |
| 3881 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3882 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3883 | attributes->core.bits, |
| 3884 | key_buffer, |
| 3885 | key_buffer_size, |
| 3886 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3887 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3888 | if (status != PSA_SUCCESS) { |
| 3889 | return status; |
| 3890 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3891 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3892 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3893 | operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3894 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3895 | 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] | 3896 | operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3897 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3898 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3899 | /* We only need to store the same length of hash as the private key size |
| 3900 | * here, it would be truncated by the internal implementation anyway. */ |
| 3901 | required_hash_length = (hash_length < operation->coordinate_bytes ? |
| 3902 | hash_length : operation->coordinate_bytes); |
| 3903 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3904 | if (required_hash_length > sizeof(operation->hash)) { |
| 3905 | /* Shouldn't happen, but better safe than sorry. */ |
| 3906 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3907 | } |
| 3908 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3909 | memcpy(operation->hash, hash, required_hash_length); |
| 3910 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3911 | |
| 3912 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3913 | |
| 3914 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3915 | (void) operation; |
| 3916 | (void) key_buffer; |
| 3917 | (void) key_buffer_size; |
| 3918 | (void) alg; |
| 3919 | (void) hash; |
| 3920 | (void) hash_length; |
| 3921 | (void) status; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3922 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3923 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3924 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3925 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3926 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3927 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3931 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3932 | uint8_t *signature, size_t signature_size, |
| 3933 | size_t *signature_length) |
| 3934 | { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3935 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3936 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3937 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3938 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3939 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3940 | mbedtls_mpi r; |
| 3941 | mbedtls_mpi s; |
| 3942 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3943 | mbedtls_mpi_init(&r); |
| 3944 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3945 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 3946 | /* Ensure max_ops is set to the current value (or default). */ |
| 3947 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 3948 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3949 | if (signature_size < 2 * operation->coordinate_bytes) { |
| 3950 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3951 | goto exit; |
| 3952 | } |
| 3953 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3954 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3955 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3957 | status = mbedtls_to_psa_error( |
| 3958 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3959 | &r, |
| 3960 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3961 | &operation->ctx->d, |
| 3962 | operation->hash, |
| 3963 | operation->hash_length, |
| 3964 | operation->md_alg, |
| 3965 | mbedtls_psa_get_random, |
| 3966 | MBEDTLS_PSA_RANDOM_STATE, |
| 3967 | &operation->restart_ctx)); |
| 3968 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3969 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3970 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3971 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3972 | } else { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3973 | status = mbedtls_to_psa_error( |
| 3974 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3975 | &r, |
| 3976 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3977 | &operation->ctx->d, |
| 3978 | operation->hash, |
| 3979 | operation->hash_length, |
| 3980 | mbedtls_psa_get_random, |
| 3981 | MBEDTLS_PSA_RANDOM_STATE, |
| 3982 | mbedtls_psa_get_random, |
| 3983 | MBEDTLS_PSA_RANDOM_STATE, |
| 3984 | &operation->restart_ctx)); |
| 3985 | } |
| 3986 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3987 | /* Hide the fact that the restart context only holds a delta of number of |
| 3988 | * ops done during the last operation, not an absolute value. */ |
| 3989 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3990 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3991 | if (status == PSA_SUCCESS) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3992 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3993 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3994 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3995 | operation->coordinate_bytes) |
| 3996 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3997 | |
| 3998 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3999 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 4003 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4004 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4005 | operation->coordinate_bytes, |
| 4006 | operation->coordinate_bytes) |
| 4007 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4008 | |
| 4009 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4010 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4013 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4014 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4015 | status = PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4016 | } |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 4017 | |
| 4018 | exit: |
| 4019 | |
| 4020 | mbedtls_mpi_free(&r); |
| 4021 | mbedtls_mpi_free(&s); |
| 4022 | return status; |
| 4023 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4024 | #else |
| 4025 | |
| 4026 | (void) operation; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4027 | (void) signature; |
| 4028 | (void) signature_size; |
| 4029 | (void) signature_length; |
| 4030 | |
| 4031 | return PSA_ERROR_NOT_SUPPORTED; |
| 4032 | |
| 4033 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4034 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4035 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4036 | } |
| 4037 | |
| 4038 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 4039 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 4040 | { |
| 4041 | |
| 4042 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4043 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4044 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4045 | |
| 4046 | if (operation->ctx) { |
| 4047 | mbedtls_ecdsa_free(operation->ctx); |
| 4048 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 4049 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 4053 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4054 | operation->num_ops = 0; |
| 4055 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4056 | return PSA_SUCCESS; |
| 4057 | |
| 4058 | #else |
| 4059 | |
| 4060 | (void) operation; |
| 4061 | |
| 4062 | return PSA_ERROR_NOT_SUPPORTED; |
| 4063 | |
| 4064 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4065 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4066 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4067 | } |
| 4068 | |
| 4069 | psa_status_t mbedtls_psa_verify_hash_start( |
| 4070 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 4071 | const psa_key_attributes_t *attributes, |
| 4072 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 4073 | psa_algorithm_t alg, |
| 4074 | const uint8_t *hash, size_t hash_length, |
| 4075 | const uint8_t *signature, size_t signature_length) |
| 4076 | { |
| 4077 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4078 | size_t coordinate_bytes = 0; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4079 | size_t required_hash_length = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4080 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4081 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 4082 | return PSA_ERROR_NOT_SUPPORTED; |
| 4083 | } |
| 4084 | |
| 4085 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 4086 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4087 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4088 | |
| 4089 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4090 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4091 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4092 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 4093 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 4094 | mbedtls_mpi_init(&operation->r); |
| 4095 | mbedtls_mpi_init(&operation->s); |
| 4096 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4097 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 4098 | operation->num_ops = 0; |
| 4099 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4100 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 4101 | attributes->core.bits, |
| 4102 | key_buffer, |
| 4103 | key_buffer_size, |
| 4104 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4105 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4106 | if (status != PSA_SUCCESS) { |
| 4107 | return status; |
| 4108 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4109 | |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 4110 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4111 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4112 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4113 | return PSA_ERROR_INVALID_SIGNATURE; |
| 4114 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4115 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4116 | status = mbedtls_to_psa_error( |
| 4117 | mbedtls_mpi_read_binary(&operation->r, |
| 4118 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4119 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4120 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4121 | if (status != PSA_SUCCESS) { |
| 4122 | return status; |
| 4123 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4124 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4125 | status = mbedtls_to_psa_error( |
| 4126 | mbedtls_mpi_read_binary(&operation->s, |
| 4127 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 4128 | coordinate_bytes, |
| 4129 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4130 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4131 | if (status != PSA_SUCCESS) { |
| 4132 | return status; |
| 4133 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4134 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4135 | status = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4136 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 4137 | if (status != PSA_SUCCESS) { |
| 4138 | return status; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4139 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4140 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4141 | /* We only need to store the same length of hash as the private key size |
| 4142 | * here, it would be truncated by the internal implementation anyway. */ |
| 4143 | required_hash_length = (hash_length < coordinate_bytes ? hash_length : |
| 4144 | coordinate_bytes); |
| 4145 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 4146 | if (required_hash_length > sizeof(operation->hash)) { |
| 4147 | /* Shouldn't happen, but better safe than sorry. */ |
| 4148 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4149 | } |
| 4150 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4151 | memcpy(operation->hash, hash, required_hash_length); |
| 4152 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4153 | |
| 4154 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4155 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4156 | (void) operation; |
| 4157 | (void) key_buffer; |
| 4158 | (void) key_buffer_size; |
| 4159 | (void) alg; |
| 4160 | (void) hash; |
| 4161 | (void) hash_length; |
| 4162 | (void) signature; |
| 4163 | (void) signature_length; |
| 4164 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 4165 | (void) coordinate_bytes; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 4166 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4167 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 4168 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4169 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4170 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4171 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 4175 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4176 | { |
| 4177 | |
| 4178 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4179 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4180 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4181 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4182 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4183 | |
Paul Elliott | a16ce9f | 2023-02-21 14:19:23 +0000 | [diff] [blame] | 4184 | /* Ensure max_ops is set to the current value (or default). */ |
| 4185 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 4186 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4187 | status = mbedtls_to_psa_error( |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4188 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 4189 | operation->hash, |
| 4190 | operation->hash_length, |
| 4191 | &operation->ctx->Q, |
| 4192 | &operation->r, |
| 4193 | &operation->s, |
| 4194 | &operation->restart_ctx)); |
| 4195 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 4196 | /* Hide the fact that the restart context only holds a delta of number of |
| 4197 | * ops done during the last operation, not an absolute value. */ |
| 4198 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 4199 | |
| 4200 | return status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4201 | #else |
| 4202 | (void) operation; |
| 4203 | |
| 4204 | return PSA_ERROR_NOT_SUPPORTED; |
| 4205 | |
| 4206 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4207 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4208 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4209 | } |
| 4210 | |
| 4211 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 4212 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 4213 | { |
| 4214 | |
| 4215 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4216 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 4217 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 4218 | |
| 4219 | if (operation->ctx) { |
| 4220 | mbedtls_ecdsa_free(operation->ctx); |
| 4221 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 4222 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 4226 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 4227 | operation->num_ops = 0; |
| 4228 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 4229 | mbedtls_mpi_free(&operation->r); |
| 4230 | mbedtls_mpi_free(&operation->s); |
| 4231 | |
| 4232 | return PSA_SUCCESS; |
| 4233 | |
| 4234 | #else |
| 4235 | (void) operation; |
| 4236 | |
| 4237 | return PSA_ERROR_NOT_SUPPORTED; |
| 4238 | |
| 4239 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4240 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 4241 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 4242 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4243 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4244 | /****************************************************************/ |
| 4245 | /* Symmetric cryptography */ |
| 4246 | /****************************************************************/ |
| 4247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4248 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 4249 | mbedtls_svc_key_id_t key, |
| 4250 | psa_algorithm_t alg, |
| 4251 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4252 | { |
| 4253 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4254 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4255 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 4257 | PSA_KEY_USAGE_ENCRYPT : |
| 4258 | PSA_KEY_USAGE_DECRYPT); |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4259 | psa_key_attributes_t attributes; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4260 | |
| 4261 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4263 | status = PSA_ERROR_BAD_STATE; |
| 4264 | goto exit; |
| 4265 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 4268 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4269 | goto exit; |
| 4270 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 4273 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4274 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4276 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4277 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4278 | * 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] | 4279 | * so we only set it (in the driver wrapper) after resources have been |
| 4280 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4281 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4283 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4285 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4286 | } |
| 4287 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4288 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4289 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 4291 | }; |
| 4292 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4293 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4294 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 4295 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 4296 | &attributes, |
| 4297 | slot->key.data, |
| 4298 | slot->key.bytes, |
| 4299 | alg); |
| 4300 | } else { |
| 4301 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 4302 | &attributes, |
| 4303 | slot->key.data, |
| 4304 | slot->key.bytes, |
| 4305 | alg); |
| 4306 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 4307 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4308 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | if (status != PSA_SUCCESS) { |
| 4310 | psa_cipher_abort(operation); |
| 4311 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 4319 | mbedtls_svc_key_id_t key, |
| 4320 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4321 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4323 | } |
| 4324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 4326 | mbedtls_svc_key_id_t key, |
| 4327 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4328 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 4333 | uint8_t *iv, |
| 4334 | size_t iv_size, |
| 4335 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4336 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4337 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4338 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 4339 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4341 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4342 | status = PSA_ERROR_BAD_STATE; |
| 4343 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4344 | } |
| 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4347 | status = PSA_ERROR_BAD_STATE; |
| 4348 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4349 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4350 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4351 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4353 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4354 | goto exit; |
| 4355 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4357 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4358 | status = PSA_ERROR_GENERIC_ERROR; |
| 4359 | goto exit; |
| 4360 | } |
| 4361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | status = psa_generate_random(local_iv, default_iv_length); |
| 4363 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4364 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4368 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4369 | |
| 4370 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | if (status == PSA_SUCCESS) { |
| 4372 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4373 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4374 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4376 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4377 | psa_cipher_abort(operation); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4378 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 4384 | const uint8_t *iv, |
| 4385 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4386 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4387 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4390 | status = PSA_ERROR_BAD_STATE; |
| 4391 | goto exit; |
| 4392 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4394 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4395 | status = PSA_ERROR_BAD_STATE; |
| 4396 | goto exit; |
| 4397 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4400 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4401 | goto exit; |
| 4402 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4404 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4405 | iv, |
| 4406 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4407 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4408 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4410 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | } else { |
| 4412 | psa_cipher_abort(operation); |
| 4413 | } |
| 4414 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 4418 | const uint8_t *input, |
| 4419 | size_t input_length, |
| 4420 | uint8_t *output, |
| 4421 | size_t output_size, |
| 4422 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4423 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4424 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4426 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4427 | status = PSA_ERROR_BAD_STATE; |
| 4428 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4429 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4432 | status = PSA_ERROR_BAD_STATE; |
| 4433 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4434 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | status = psa_driver_wrapper_cipher_update(operation, |
| 4437 | input, |
| 4438 | input_length, |
| 4439 | output, |
| 4440 | output_size, |
| 4441 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4442 | |
| 4443 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | if (status != PSA_SUCCESS) { |
| 4445 | psa_cipher_abort(operation); |
| 4446 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4449 | } |
| 4450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 4452 | uint8_t *output, |
| 4453 | size_t output_size, |
| 4454 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4455 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4456 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4459 | status = PSA_ERROR_BAD_STATE; |
| 4460 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4461 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4464 | status = PSA_ERROR_BAD_STATE; |
| 4465 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4466 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4469 | output, |
| 4470 | output_size, |
| 4471 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4472 | |
| 4473 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | if (status == PSA_SUCCESS) { |
| 4475 | return psa_cipher_abort(operation); |
| 4476 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4477 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4478 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4481 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4482 | } |
| 4483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4485 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4486 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4487 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4488 | * in use. It's ok to call abort on such an object, and there's |
| 4489 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4491 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4494 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4495 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4496 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4497 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4500 | } |
| 4501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4503 | psa_algorithm_t alg, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4504 | const uint8_t *input_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | size_t input_length, |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4506 | uint8_t *output_external, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | size_t output_size, |
| 4508 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4509 | { |
| 4510 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4511 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4512 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4513 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4514 | size_t default_iv_length = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4515 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4516 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4517 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4518 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4519 | |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4520 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
David Horstmann | 36df4b2 | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 4521 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
David Horstmann | c6977b4 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 4522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4524 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4525 | goto exit; |
| 4526 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4529 | PSA_KEY_USAGE_ENCRYPT, |
| 4530 | alg); |
| 4531 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4532 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4534 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4535 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4537 | }; |
| 4538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4540 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4541 | status = PSA_ERROR_GENERIC_ERROR; |
| 4542 | goto exit; |
| 4543 | } |
| 4544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | if (default_iv_length > 0) { |
| 4546 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4547 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4548 | goto exit; |
| 4549 | } |
| 4550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | status = psa_generate_random(local_iv, default_iv_length); |
| 4552 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4553 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4554 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4555 | } |
| 4556 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4557 | status = psa_driver_wrapper_cipher_encrypt( |
| 4558 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4559 | alg, local_iv, default_iv_length, input, input_length, |
Ronald Cron | afbc7ed | 2023-01-24 16:02:04 +0100 | [diff] [blame] | 4560 | psa_crypto_buffer_offset(output, default_iv_length), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4562 | |
| 4563 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | unlock_status = psa_unlock_key_slot(slot); |
| 4565 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4566 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4567 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | if (status == PSA_SUCCESS) { |
| 4570 | if (default_iv_length > 0) { |
| 4571 | memcpy(output, local_iv, default_iv_length); |
| 4572 | } |
| 4573 | *output_length += default_iv_length; |
| 4574 | } else { |
| 4575 | *output_length = 0; |
| 4576 | } |
| 4577 | |
David Horstmann | 62a56d9 | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 4578 | LOCAL_INPUT_FREE(input_external, input); |
| 4579 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | c6977b4 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 4580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4582 | } |
| 4583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4585 | psa_algorithm_t alg, |
| 4586 | const uint8_t *input, |
| 4587 | size_t input_length, |
| 4588 | uint8_t *output, |
| 4589 | size_t output_size, |
| 4590 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4591 | { |
| 4592 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4593 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4594 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4595 | psa_key_attributes_t attributes; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4598 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4599 | goto exit; |
| 4600 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4603 | PSA_KEY_USAGE_DECRYPT, |
| 4604 | alg); |
| 4605 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4606 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4608 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4609 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4611 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4614 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4615 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4616 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | } 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] | 4618 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4619 | goto exit; |
| 4620 | } |
| 4621 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4622 | status = psa_driver_wrapper_cipher_decrypt( |
| 4623 | &attributes, slot->key.data, slot->key.bytes, |
| 4624 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4626 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4627 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4628 | unlock_status = psa_unlock_key_slot(slot); |
| 4629 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4630 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4634 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4637 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4641 | /****************************************************************/ |
| 4642 | /* AEAD */ |
| 4643 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4644 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4645 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | 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] | 4647 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4652 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4653 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4654 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4658 | #if defined(PSA_WANT_ALG_GCM) |
| 4659 | case PSA_ALG_GCM: |
| 4660 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | * arbitrarily large nonces. Please note that we do not generally |
| 4662 | * recommend the usage of nonces of greater length than |
| 4663 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4664 | * size, which can then lead to collisions if you encrypt a very |
| 4665 | * large number of messages.*/ |
| 4666 | if (nonce_length != 0) { |
| 4667 | return PSA_SUCCESS; |
| 4668 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4669 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4670 | #endif /* PSA_WANT_ALG_GCM */ |
| 4671 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4672 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4674 | return PSA_SUCCESS; |
| 4675 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4676 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4677 | #endif /* PSA_WANT_ALG_CCM */ |
| 4678 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4679 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4680 | if (nonce_length == 12) { |
| 4681 | return PSA_SUCCESS; |
| 4682 | } else if (nonce_length == 8) { |
| 4683 | return PSA_ERROR_NOT_SUPPORTED; |
| 4684 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4685 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4686 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4687 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4688 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4690 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4693 | } |
| 4694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4695 | 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] | 4696 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4697 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4698 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4699 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4702 | } |
| 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4705 | psa_algorithm_t alg, |
| 4706 | const uint8_t *nonce, |
| 4707 | size_t nonce_length, |
| 4708 | const uint8_t *additional_data, |
| 4709 | size_t additional_data_length, |
| 4710 | const uint8_t *plaintext, |
| 4711 | size_t plaintext_length, |
| 4712 | uint8_t *ciphertext, |
| 4713 | size_t ciphertext_size, |
| 4714 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4715 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4716 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4717 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4718 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4719 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | status = psa_aead_check_algorithm(alg); |
| 4722 | if (status != PSA_SUCCESS) { |
| 4723 | return status; |
| 4724 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4725 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4726 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4728 | if (status != PSA_SUCCESS) { |
| 4729 | return status; |
| 4730 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4731 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4732 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4734 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4737 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4738 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4740 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4741 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4742 | &attributes, slot->key.data, slot->key.bytes, |
| 4743 | alg, |
| 4744 | nonce, nonce_length, |
| 4745 | additional_data, additional_data_length, |
| 4746 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4750 | memset(ciphertext, 0, ciphertext_size); |
| 4751 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4752 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4753 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4757 | } |
| 4758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4760 | psa_algorithm_t alg, |
| 4761 | const uint8_t *nonce, |
| 4762 | size_t nonce_length, |
| 4763 | const uint8_t *additional_data, |
| 4764 | size_t additional_data_length, |
| 4765 | const uint8_t *ciphertext, |
| 4766 | size_t ciphertext_length, |
| 4767 | uint8_t *plaintext, |
| 4768 | size_t plaintext_size, |
| 4769 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4770 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4771 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4772 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4774 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | status = psa_aead_check_algorithm(alg); |
| 4777 | if (status != PSA_SUCCESS) { |
| 4778 | return status; |
| 4779 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4780 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4781 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4782 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4783 | if (status != PSA_SUCCESS) { |
| 4784 | return status; |
| 4785 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4786 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4787 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4788 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4789 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4791 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4792 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4793 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4795 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4796 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4797 | &attributes, slot->key.data, slot->key.bytes, |
| 4798 | alg, |
| 4799 | nonce, nonce_length, |
| 4800 | additional_data, additional_data_length, |
| 4801 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4805 | memset(plaintext, 0, plaintext_size); |
| 4806 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4807 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4808 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4812 | } |
| 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4815 | { |
| 4816 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4819 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4820 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4821 | /* 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] | 4822 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4823 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4824 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4825 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4826 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4827 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4828 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4830 | /* 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] | 4831 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4832 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4833 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4834 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4835 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4836 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4837 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4839 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | if (tag_len != 16) { |
| 4841 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4842 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4843 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4844 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4845 | |
| 4846 | default: |
| 4847 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4849 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4853 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4855 | int is_encrypt, |
| 4856 | mbedtls_svc_key_id_t key, |
| 4857 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4858 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4859 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4860 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4861 | psa_key_slot_t *slot = NULL; |
| 4862 | psa_key_usage_t key_usage = 0; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4863 | psa_key_attributes_t attributes; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | status = psa_aead_check_algorithm(alg); |
| 4866 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4867 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4871 | status = PSA_ERROR_BAD_STATE; |
| 4872 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | if (operation->nonce_set || operation->lengths_set || |
| 4876 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4877 | status = PSA_ERROR_BAD_STATE; |
| 4878 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4882 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4884 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4887 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 4888 | alg); |
| 4889 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4890 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4892 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 4893 | attributes = (psa_key_attributes_t) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4894 | .core = slot->attr |
| 4895 | }; |
| 4896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4898 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | if (is_encrypt) { |
| 4902 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 4903 | &attributes, |
| 4904 | slot->key.data, |
| 4905 | slot->key.bytes, |
| 4906 | alg); |
| 4907 | } else { |
| 4908 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 4909 | &attributes, |
| 4910 | slot->key.data, |
| 4911 | slot->key.bytes, |
| 4912 | alg); |
| 4913 | } |
| 4914 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4915 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4919 | |
| 4920 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | unlock_status = psa_unlock_key_slot(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4924 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 4926 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | } else { |
| 4928 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4929 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4932 | } |
| 4933 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4934 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 4936 | mbedtls_svc_key_id_t key, |
| 4937 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4938 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 4944 | mbedtls_svc_key_id_t key, |
| 4945 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4946 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4951 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
| 4952 | uint8_t *nonce, |
| 4953 | size_t nonce_size, |
| 4954 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4955 | { |
| 4956 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4957 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 4958 | size_t required_nonce_size = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4959 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4960 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4962 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4963 | status = PSA_ERROR_BAD_STATE; |
| 4964 | goto exit; |
| 4965 | } |
| 4966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4968 | status = PSA_ERROR_BAD_STATE; |
| 4969 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4970 | } |
| 4971 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4972 | /* For CCM, this size may not be correct according to the PSA |
| 4973 | * specification. The PSA Crypto 1.0.1 specification states: |
| 4974 | * |
| 4975 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 4976 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 4977 | * |
| 4978 | * However this restriction that L has to be the smallest integer is not |
| 4979 | * applied in practice, and it is not implementable here since the |
| 4980 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 4982 | operation->alg); |
| 4983 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4984 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4985 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4986 | } |
| 4987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | status = psa_generate_random(local_nonce, required_nonce_size); |
| 4989 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4990 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4991 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4994 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4995 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | if (status == PSA_SUCCESS) { |
| 4997 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4998 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | } else { |
| 5000 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 5001 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5003 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 5007 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 5009 | const uint8_t *nonce, |
| 5010 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5011 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5012 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5014 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5015 | status = PSA_ERROR_BAD_STATE; |
| 5016 | goto exit; |
| 5017 | } |
| 5018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | if (operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5020 | status = PSA_ERROR_BAD_STATE; |
| 5021 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5022 | } |
| 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 5025 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 5026 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5027 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 5028 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 5029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 5031 | nonce_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5032 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5033 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | if (status == PSA_SUCCESS) { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5035 | operation->nonce_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | } else { |
| 5037 | psa_aead_abort(operation); |
| 5038 | } |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 5045 | size_t ad_length, |
| 5046 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5047 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5048 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5051 | status = PSA_ERROR_BAD_STATE; |
| 5052 | goto exit; |
| 5053 | } |
| 5054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | if (operation->lengths_set || operation->ad_started || |
| 5056 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5057 | status = PSA_ERROR_BAD_STATE; |
| 5058 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5059 | } |
| 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5062 | #if defined(PSA_WANT_ALG_GCM) |
| 5063 | case PSA_ALG_GCM: |
| 5064 | /* 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] | 5065 | * bits. Without the guard this code will generate warnings on 32bit |
| 5066 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5067 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 5069 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5070 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5071 | goto exit; |
| 5072 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5073 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5074 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5075 | #endif /* PSA_WANT_ALG_GCM */ |
| 5076 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5077 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5079 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5080 | goto exit; |
| 5081 | } |
| 5082 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5083 | #endif /* PSA_WANT_ALG_CCM */ |
| 5084 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5085 | case PSA_ALG_CHACHA20_POLY1305: |
| 5086 | /* No length restrictions for ChaChaPoly. */ |
| 5087 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 5088 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 5089 | default: |
| 5090 | break; |
| 5091 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 5092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 5094 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5095 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5096 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5098 | operation->ad_remaining = ad_length; |
| 5099 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5100 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | } else { |
| 5102 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5103 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5106 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 5107 | |
| 5108 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 5110 | const uint8_t *input, |
| 5111 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5112 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5113 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5116 | status = PSA_ERROR_BAD_STATE; |
| 5117 | goto exit; |
| 5118 | } |
| 5119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5121 | status = PSA_ERROR_BAD_STATE; |
| 5122 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5123 | } |
| 5124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | if (operation->lengths_set) { |
| 5126 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5127 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5128 | goto exit; |
| 5129 | } |
| 5130 | |
| 5131 | operation->ad_remaining -= input_length; |
| 5132 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5133 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5135 | status = PSA_ERROR_BAD_STATE; |
| 5136 | goto exit; |
| 5137 | } |
| 5138 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 5141 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5142 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5143 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5145 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | } else { |
| 5147 | psa_aead_abort(operation); |
| 5148 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 5154 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 5156 | const uint8_t *input, |
| 5157 | size_t input_length, |
| 5158 | uint8_t *output, |
| 5159 | size_t output_size, |
| 5160 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5161 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5162 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5163 | |
| 5164 | *output_length = 0; |
| 5165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5167 | status = PSA_ERROR_BAD_STATE; |
| 5168 | goto exit; |
| 5169 | } |
| 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5172 | status = PSA_ERROR_BAD_STATE; |
| 5173 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5174 | } |
| 5175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5177 | /* Additional data length was supplied, but not all the additional |
| 5178 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5180 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5181 | goto exit; |
| 5182 | } |
| 5183 | |
| 5184 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5186 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5187 | goto exit; |
| 5188 | } |
| 5189 | |
| 5190 | operation->body_remaining -= input_length; |
| 5191 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5192 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 5194 | status = PSA_ERROR_BAD_STATE; |
| 5195 | goto exit; |
| 5196 | } |
| 5197 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 5198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5199 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 5200 | output, output_size, |
| 5201 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5202 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5203 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5205 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | } else { |
| 5207 | psa_aead_abort(operation); |
| 5208 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5210 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5211 | } |
| 5212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | 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] | 5214 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | if (operation->id == 0 || !operation->nonce_set) { |
| 5216 | return PSA_ERROR_BAD_STATE; |
| 5217 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 5220 | operation->body_remaining != 0)) { |
| 5221 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5222 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5224 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5225 | } |
| 5226 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5227 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 5229 | uint8_t *ciphertext, |
| 5230 | size_t ciphertext_size, |
| 5231 | size_t *ciphertext_length, |
| 5232 | uint8_t *tag, |
| 5233 | size_t tag_size, |
| 5234 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5235 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5236 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5237 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5238 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 5239 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5241 | status = psa_aead_final_checks(operation); |
| 5242 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5243 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5247 | status = PSA_ERROR_BAD_STATE; |
| 5248 | goto exit; |
| 5249 | } |
| 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 5252 | ciphertext_size, |
| 5253 | ciphertext_length, |
| 5254 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 5255 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5256 | exit: |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5257 | |
| 5258 | |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5259 | /* 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] | 5260 | * the zero tag size, make sure the tag is set to something implausible. |
| 5261 | * Even if the operation succeeds, make sure we clear the rest of the |
| 5262 | * buffer to prevent potential leakage of anything previously placed in |
| 5263 | * the same buffer.*/ |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 5264 | psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length); |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 5272 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 5274 | uint8_t *plaintext, |
| 5275 | size_t plaintext_size, |
| 5276 | size_t *plaintext_length, |
| 5277 | const uint8_t *tag, |
| 5278 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5279 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5280 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5281 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5282 | *plaintext_length = 0; |
| 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | status = psa_aead_final_checks(operation); |
| 5285 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5286 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 5288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 5290 | status = PSA_ERROR_BAD_STATE; |
| 5291 | goto exit; |
| 5292 | } |
| 5293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 5295 | plaintext_size, |
| 5296 | plaintext_length, |
| 5297 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5298 | |
| 5299 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 5301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5307 | { |
| 5308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5311 | /* The object has (apparently) been initialized but it is not (yet) |
| 5312 | * in use. It's ok to call abort on such an object, and there's |
| 5313 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5317 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5319 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 5322 | } |
| 5323 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5324 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5325 | /* Generators */ |
| 5326 | /****************************************************************/ |
| 5327 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5328 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5329 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5330 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
Kusumit Ghoderao | af0b534 | 2023-05-03 11:58:46 +0530 | [diff] [blame] | 5331 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5332 | defined(PSA_HAVE_SOFT_PBKDF2) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5333 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5334 | #endif /* At least one builtin KDF */ |
| 5335 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5336 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5337 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5338 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5339 | static psa_status_t psa_key_derivation_start_hmac( |
| 5340 | psa_mac_operation_t *operation, |
| 5341 | psa_algorithm_t hash_alg, |
| 5342 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5344 | { |
| 5345 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5346 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 5348 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 5349 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5350 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5351 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 5355 | &attributes, |
| 5356 | hmac_key, hmac_key_length, |
| 5357 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | psa_reset_key_attributes(&attributes); |
| 5360 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5361 | } |
| 5362 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5363 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5364 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5365 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5366 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5367 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5368 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5369 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5371 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5373 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5374 | } else { |
| 5375 | return operation->alg; |
| 5376 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5377 | } |
| 5378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5379 | 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] | 5380 | { |
| 5381 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5383 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5384 | /* The object has (apparently) been initialized but it is not |
| 5385 | * in use. It's ok to call abort on such an object, and there's |
| 5386 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5388 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5390 | mbedtls_free(operation->ctx.hkdf.info); |
| 5391 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5392 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5393 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5394 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5395 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5397 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5398 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5399 | if (operation->ctx.tls12_prf.secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5400 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | operation->ctx.tls12_prf.secret_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5402 | } |
| 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | if (operation->ctx.tls12_prf.seed != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5405 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | operation->ctx.tls12_prf.seed_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5407 | } |
| 5408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | if (operation->ctx.tls12_prf.label != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5410 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | operation->ctx.tls12_prf.label_length); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5412 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5413 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5415 | mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | operation->ctx.tls12_prf.other_secret_length); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5417 | } |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 5418 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5419 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5420 | |
| 5421 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5422 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5424 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5425 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5426 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5428 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5429 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5430 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5431 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5432 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 5433 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5434 | if (operation->ctx.pbkdf2.salt != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5435 | mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt, |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5436 | operation->ctx.pbkdf2.salt_length); |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5437 | } |
Kusumit Ghoderao | f5fedf1 | 2023-05-03 12:29:48 +0530 | [diff] [blame] | 5438 | |
| 5439 | status = PSA_SUCCESS; |
| 5440 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5441 | #endif /* defined(PSA_HAVE_SOFT_PBKDF2) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5442 | { |
| 5443 | status = PSA_ERROR_BAD_STATE; |
| 5444 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5446 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5447 | } |
| 5448 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5449 | 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] | 5450 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5451 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5453 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5457 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5459 | } |
| 5460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5462 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5463 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | if (operation->alg == 0) { |
| 5465 | return PSA_ERROR_BAD_STATE; |
| 5466 | } |
| 5467 | if (capacity > operation->capacity) { |
| 5468 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5469 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5470 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5471 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5472 | } |
| 5473 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5474 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5475 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5477 | psa_algorithm_t kdf_alg, |
| 5478 | uint8_t *output, |
| 5479 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5480 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5482 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5483 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5484 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5485 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | 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] | 5487 | #else |
| 5488 | const uint8_t last_block = 0xff; |
| 5489 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5492 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5493 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5495 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | )) { |
| 5497 | return PSA_ERROR_BAD_STATE; |
| 5498 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5499 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5502 | /* Copy what remains of the current block */ |
| 5503 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5505 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | } |
| 5507 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5508 | output += n; |
| 5509 | output_length -= n; |
| 5510 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5511 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5512 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5513 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5514 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5515 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5516 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5517 | * object was corrupted or if this function is called directly |
| 5518 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | if (hkdf->block_number == last_block) { |
| 5520 | return PSA_ERROR_BAD_STATE; |
| 5521 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5522 | |
| 5523 | /* We need a new block */ |
| 5524 | ++hkdf->block_number; |
| 5525 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5528 | hash_alg, |
| 5529 | hkdf->prk, |
| 5530 | hash_length); |
| 5531 | if (status != PSA_SUCCESS) { |
| 5532 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5533 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | |
| 5535 | if (hkdf->block_number != 1) { |
| 5536 | status = psa_mac_update(&hkdf->hmac, |
| 5537 | hkdf->output_block, |
| 5538 | hash_length); |
| 5539 | if (status != PSA_SUCCESS) { |
| 5540 | return status; |
| 5541 | } |
| 5542 | } |
| 5543 | status = psa_mac_update(&hkdf->hmac, |
| 5544 | hkdf->info, |
| 5545 | hkdf->info_length); |
| 5546 | if (status != PSA_SUCCESS) { |
| 5547 | return status; |
| 5548 | } |
| 5549 | status = psa_mac_update(&hkdf->hmac, |
| 5550 | &hkdf->block_number, 1); |
| 5551 | if (status != PSA_SUCCESS) { |
| 5552 | return status; |
| 5553 | } |
| 5554 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5555 | hkdf->output_block, |
| 5556 | sizeof(hkdf->output_block), |
| 5557 | &hmac_output_length); |
| 5558 | if (status != PSA_SUCCESS) { |
| 5559 | return status; |
| 5560 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5561 | } |
| 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5564 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5565 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5566 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5567 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5568 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5569 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5570 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5572 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5574 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5575 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5576 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5577 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5578 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5579 | /* We can't be wanting more output after block 0xff, otherwise |
| 5580 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5581 | * prevented this call. It could happen only if the operation |
| 5582 | * object was corrupted or if this function is called directly |
| 5583 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | if (tls12_prf->block_number == 0xff) { |
| 5585 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5586 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5587 | |
| 5588 | /* We need a new block */ |
| 5589 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5590 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5591 | |
| 5592 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5593 | * |
| 5594 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5595 | * |
| 5596 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5597 | * HMAC_hash(secret, A(2) + seed) + |
| 5598 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5599 | * |
| 5600 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5601 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5602 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5603 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5604 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5605 | * is currently extracted as `output_block` and where i is |
| 5606 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5607 | */ |
| 5608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | status = psa_key_derivation_start_hmac(&hmac, |
| 5610 | hash_alg, |
| 5611 | tls12_prf->secret, |
| 5612 | tls12_prf->secret_length); |
| 5613 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5614 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5616 | |
| 5617 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5619 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5620 | * the variable seed and in this instance means it in the context of the |
| 5621 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | status = psa_mac_update(&hmac, |
| 5623 | tls12_prf->label, |
| 5624 | tls12_prf->label_length); |
| 5625 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5626 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | } |
| 5628 | status = psa_mac_update(&hmac, |
| 5629 | tls12_prf->seed, |
| 5630 | tls12_prf->seed_length); |
| 5631 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5632 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5633 | } |
| 5634 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5635 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5637 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5638 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5640 | } |
| 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | status = psa_mac_sign_finish(&hmac, |
| 5643 | tls12_prf->Ai, hash_length, |
| 5644 | &hmac_output_length); |
| 5645 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5646 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | } |
| 5648 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5649 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5651 | |
| 5652 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | status = psa_key_derivation_start_hmac(&hmac, |
| 5654 | hash_alg, |
| 5655 | tls12_prf->secret, |
| 5656 | tls12_prf->secret_length); |
| 5657 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5658 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | } |
| 5660 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5661 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5662 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | } |
| 5664 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5665 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5666 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | } |
| 5668 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5669 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5670 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | } |
| 5672 | status = psa_mac_sign_finish(&hmac, |
| 5673 | tls12_prf->output_block, hash_length, |
| 5674 | &hmac_output_length); |
| 5675 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5676 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5678 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5679 | |
| 5680 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | cleanup_status = psa_mac_abort(&hmac); |
| 5682 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5683 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5687 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5688 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5689 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5690 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5691 | psa_algorithm_t alg, |
| 5692 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5694 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5696 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5697 | psa_status_t status; |
| 5698 | uint8_t offset, length; |
| 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5701 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5702 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5703 | break; |
| 5704 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5705 | break; |
| 5706 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5708 | } |
| 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5711 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | if (tls12_prf->left_in_block == 0) { |
| 5713 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5714 | alg); |
| 5715 | if (status != PSA_SUCCESS) { |
| 5716 | return status; |
| 5717 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5718 | |
| 5719 | continue; |
| 5720 | } |
| 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5723 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5725 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5727 | |
| 5728 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5730 | output += length; |
| 5731 | output_length -= length; |
| 5732 | tls12_prf->left_in_block -= length; |
| 5733 | } |
| 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5736 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5737 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5738 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5739 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5740 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5741 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5742 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5743 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5745 | { |
| 5746 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5747 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | if (output_length != 32) { |
| 5750 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5751 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5754 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5755 | &output_size); |
| 5756 | if (status != PSA_SUCCESS) { |
| 5757 | return status; |
| 5758 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | if (output_size != output_length) { |
| 5761 | return PSA_ERROR_GENERIC_ERROR; |
| 5762 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5765 | } |
| 5766 | #endif |
| 5767 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5768 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5769 | static psa_status_t psa_key_derivation_pbkdf2_generate_block( |
| 5770 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 5771 | psa_algorithm_t prf_alg, |
| 5772 | uint8_t prf_output_length, |
| 5773 | psa_key_attributes_t *attributes) |
| 5774 | { |
| 5775 | psa_status_t status; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5776 | psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5777 | size_t mac_output_length; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5778 | uint8_t U_i[PSA_MAC_MAX_SIZE]; |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5779 | uint8_t *U_accumulator = pbkdf2->output_block; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5780 | uint64_t i; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5781 | uint8_t block_counter[4]; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5782 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5783 | mac_operation.is_sign = 1; |
| 5784 | mac_operation.mac_size = prf_output_length; |
| 5785 | MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5786 | |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5787 | status = psa_driver_wrapper_mac_sign_setup(&mac_operation, |
| 5788 | attributes, |
| 5789 | pbkdf2->password, |
| 5790 | pbkdf2->password_length, |
| 5791 | prf_alg); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5792 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5793 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5794 | } |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5795 | status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length); |
| 5796 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5797 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5798 | } |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5799 | status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter)); |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5800 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5801 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5802 | } |
| 5803 | status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i), |
| 5804 | &mac_output_length); |
| 5805 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5806 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | if (mac_output_length != prf_output_length) { |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5810 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5811 | goto cleanup; |
Kusumit Ghoderao | b821a5f | 2023-06-08 16:35:55 +0530 | [diff] [blame] | 5812 | } |
| 5813 | |
Kusumit Ghoderao | 257ea00 | 2023-06-14 15:55:11 +0530 | [diff] [blame] | 5814 | memcpy(U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5815 | |
| 5816 | for (i = 1; i < pbkdf2->input_cost; i++) { |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 5817 | /* We are passing prf_output_length as mac_size because the driver |
| 5818 | * function directly sets mac_output_length as mac_size upon success. |
Kusumit Ghoderao | 5f3345a | 2023-07-27 21:21:38 +0530 | [diff] [blame] | 5819 | * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5820 | status = psa_driver_wrapper_mac_compute(attributes, |
| 5821 | pbkdf2->password, |
| 5822 | pbkdf2->password_length, |
| 5823 | prf_alg, U_i, prf_output_length, |
Kusumit Ghoderao | 4536bb6 | 2023-06-22 15:42:59 +0530 | [diff] [blame] | 5824 | U_i, prf_output_length, |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5825 | &mac_output_length); |
| 5826 | if (status != PSA_SUCCESS) { |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5827 | goto cleanup; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5828 | } |
| 5829 | |
Kusumit Ghoderao | 109ee3d | 2023-06-08 16:36:45 +0530 | [diff] [blame] | 5830 | mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length); |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5831 | } |
Kusumit Ghoderao | 246e51f | 2023-06-15 22:15:43 +0530 | [diff] [blame] | 5832 | |
| 5833 | cleanup: |
| 5834 | /* Zeroise buffers to clear sensitive data from memory. */ |
| 5835 | mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE); |
| 5836 | return status; |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5837 | } |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5838 | |
| 5839 | static psa_status_t psa_key_derivation_pbkdf2_read( |
| 5840 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 5841 | psa_algorithm_t kdf_alg, |
| 5842 | uint8_t *output, |
| 5843 | size_t output_length) |
| 5844 | { |
| 5845 | psa_status_t status; |
| 5846 | psa_algorithm_t prf_alg; |
| 5847 | uint8_t prf_output_length; |
| 5848 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5849 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length)); |
| 5850 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 5851 | |
| 5852 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 5853 | prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg)); |
| 5854 | prf_output_length = PSA_HASH_LENGTH(prf_alg); |
| 5855 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Kusumit Ghoderao | a2520a5 | 2023-06-22 15:42:19 +0530 | [diff] [blame] | 5856 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 5857 | prf_alg = PSA_ALG_CMAC; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 5858 | 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] | 5859 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
Kusumit Ghoderao | d9ec1af | 2023-06-08 20:19:51 +0530 | [diff] [blame] | 5860 | } else { |
| 5861 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | switch (pbkdf2->state) { |
| 5865 | case PSA_PBKDF2_STATE_PASSWORD_SET: |
| 5866 | /* Initially we need a new block so bytes_used is equal to block size*/ |
| 5867 | pbkdf2->bytes_used = prf_output_length; |
| 5868 | pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT; |
| 5869 | break; |
| 5870 | case PSA_PBKDF2_STATE_OUTPUT: |
| 5871 | break; |
| 5872 | default: |
| 5873 | return PSA_ERROR_BAD_STATE; |
| 5874 | } |
| 5875 | |
| 5876 | while (output_length != 0) { |
| 5877 | uint8_t n = prf_output_length - pbkdf2->bytes_used; |
| 5878 | if (n > output_length) { |
| 5879 | n = (uint8_t) output_length; |
| 5880 | } |
| 5881 | memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n); |
| 5882 | output += n; |
| 5883 | output_length -= n; |
| 5884 | pbkdf2->bytes_used += n; |
| 5885 | |
| 5886 | if (output_length == 0) { |
| 5887 | break; |
| 5888 | } |
| 5889 | |
| 5890 | /* We need a new block */ |
| 5891 | pbkdf2->bytes_used = 0; |
| 5892 | pbkdf2->block_number++; |
| 5893 | |
| 5894 | status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg, |
| 5895 | prf_output_length, |
| 5896 | &attributes); |
| 5897 | if (status != PSA_SUCCESS) { |
| 5898 | return status; |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | return PSA_SUCCESS; |
| 5903 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5904 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | a4346cd | 2023-06-05 14:50:20 +0530 | [diff] [blame] | 5905 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5906 | psa_status_t psa_key_derivation_output_bytes( |
| 5907 | psa_key_derivation_operation_t *operation, |
| 5908 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5910 | { |
| 5911 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
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 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5915 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5920 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5921 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5922 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5923 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5924 | goto exit; |
| 5925 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5927 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5928 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5929 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5930 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5931 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5932 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5934 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5935 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5936 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5937 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5939 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 5940 | output, output_length); |
| 5941 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5942 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5943 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5944 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5946 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5947 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 5948 | kdf_alg, output, |
| 5949 | output_length); |
| 5950 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5951 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5952 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5953 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5955 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 5957 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5958 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5959 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 5960 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | f6a0d57 | 2023-06-05 14:55:56 +0530 | [diff] [blame] | 5961 | status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg, |
| 5962 | output, output_length); |
Kusumit Ghoderao | 056f0c5 | 2023-05-03 15:58:34 +0530 | [diff] [blame] | 5963 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 5964 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5965 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5966 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5967 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5973 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5974 | * This allows us to differentiate between exhausted operations and |
| 5975 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5976 | * operations. */ |
| 5977 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5979 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5981 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5983 | } |
| 5984 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5985 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | 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] | 5987 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | if (data_size >= 8) { |
| 5989 | mbedtls_des_key_set_parity(data); |
| 5990 | } |
| 5991 | if (data_size >= 16) { |
| 5992 | mbedtls_des_key_set_parity(data + 8); |
| 5993 | } |
| 5994 | if (data_size >= 24) { |
| 5995 | mbedtls_des_key_set_parity(data + 16); |
| 5996 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5997 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5998 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5999 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6000 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 6002 | * of a private key which is an integer |
| 6003 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 6004 | * N is the prime p for Diffie-Hellman, or the order of the |
| 6005 | * curve’s base point for ECC. |
| 6006 | * |
| 6007 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 6008 | * This function generates the private key using the following process: |
| 6009 | * |
| 6010 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 6011 | * 2. If m is not a multiple of 8, set the most significant |
| 6012 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 6013 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 6014 | * 4. If k > N - 2, discard the result and return to step 1. |
| 6015 | * 5. Output k + 1 as the private key. |
| 6016 | * |
| 6017 | * This method allows compliance to NIST standards, specifically the methods titled |
| 6018 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 6019 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 6020 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 6021 | * Diffie-Hellman keys. |
| 6022 | * |
| 6023 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 6024 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 6025 | * |
| 6026 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 6027 | * always NULL. |
| 6028 | */ |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 6029 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
| 6030 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6031 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6032 | psa_key_slot_t *slot, |
| 6033 | size_t bits, |
| 6034 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6035 | uint8_t **data |
| 6036 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6037 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6038 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6039 | mbedtls_mpi k; |
| 6040 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6041 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6042 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6043 | size_t m; |
| 6044 | size_t m_bytes; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | mbedtls_mpi_init(&k); |
| 6047 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 6048 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6049 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6050 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6051 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | mbedtls_ecc_group_of_psa(curve, bits, 0); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6055 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 6056 | goto cleanup; |
| 6057 | } |
| 6058 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6059 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6063 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6064 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6065 | /* Let m be the bit size of N. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6066 | m = ecp_group.nbits; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6067 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6068 | m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6069 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6070 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | 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] | 6072 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6073 | /* Note: This function is always called with *data == NULL and it |
| 6074 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6075 | *data = mbedtls_calloc(1, m_bytes); |
| 6076 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6077 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6078 | goto cleanup; |
| 6079 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6082 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6084 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6086 | |
| 6087 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6089 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6090 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 6091 | * the string to zero. |
| 6092 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6094 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | * big-endian byte string. |
| 6099 | */ |
| 6100 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6101 | |
| 6102 | /* 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] | 6103 | * Result of comparison is returned. When it indicates error |
| 6104 | * then this function is called again. |
| 6105 | */ |
| 6106 | 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] | 6107 | } |
| 6108 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6109 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 6111 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6112 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | if (ret != 0) { |
| 6114 | status = mbedtls_to_psa_error(ret); |
| 6115 | } |
| 6116 | if (status != PSA_SUCCESS) { |
| 6117 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6118 | *data = NULL; |
| 6119 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | mbedtls_mpi_free(&k); |
| 6121 | mbedtls_mpi_free(&diff_N_2); |
| 6122 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6123 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6124 | |
| 6125 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 6126 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 6127 | * |
| 6128 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 6129 | * draw a 32-byte string and process it as specified in |
| 6130 | * Elliptic Curves for Security [RFC7748] §5. |
| 6131 | * |
| 6132 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 6133 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 6134 | * |
| 6135 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 6136 | * always NULL. |
| 6137 | */ |
| 6138 | |
| 6139 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6140 | size_t bits, |
| 6141 | psa_key_derivation_operation_t *operation, |
| 6142 | uint8_t **data |
| 6143 | ) |
| 6144 | { |
| 6145 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6146 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6149 | case 255: |
| 6150 | output_length = 32; |
| 6151 | break; |
| 6152 | case 448: |
| 6153 | output_length = 56; |
| 6154 | break; |
| 6155 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6157 | break; |
| 6158 | } |
| 6159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6160 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | if (*data == NULL) { |
| 6163 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6164 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6169 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6173 | case 255: |
| 6174 | (*data)[0] &= 248; |
| 6175 | (*data)[31] &= 127; |
| 6176 | (*data)[31] |= 64; |
| 6177 | break; |
| 6178 | case 448: |
| 6179 | (*data)[0] &= 252; |
| 6180 | (*data)[55] |= 128; |
| 6181 | break; |
| 6182 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6183 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 6184 | break; |
| 6185 | } |
| 6186 | |
| 6187 | return status; |
| 6188 | } |
Valerio Setti | 86587ab | 2023-06-27 13:09:30 +0200 | [diff] [blame] | 6189 | #else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6190 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
| 6191 | psa_key_slot_t *slot, size_t bits, |
| 6192 | psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6193 | { |
| 6194 | (void) slot; |
| 6195 | (void) bits; |
| 6196 | (void) operation; |
| 6197 | (void) data; |
| 6198 | return PSA_ERROR_NOT_SUPPORTED; |
| 6199 | } |
| 6200 | |
| 6201 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 6202 | size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data) |
| 6203 | { |
| 6204 | (void) bits; |
| 6205 | (void) operation; |
| 6206 | (void) data; |
| 6207 | return PSA_ERROR_NOT_SUPPORTED; |
| 6208 | } |
| 6209 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
| 6210 | #endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 6211 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 6212 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6213 | psa_key_slot_t *slot, |
| 6214 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6215 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6216 | { |
| 6217 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6219 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 6220 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6221 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 6224 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6225 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6226 | |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6227 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \ |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6228 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6229 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 6230 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 6231 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6232 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6233 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 6234 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 6235 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6236 | } |
| 6237 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6238 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 6240 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6241 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 6243 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 6244 | } else |
Valerio Setti | 8ffdb5d | 2023-06-20 13:14:08 +0200 | [diff] [blame] | 6245 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || |
Valerio Setti | dd24f29 | 2023-06-26 12:21:17 +0200 | [diff] [blame] | 6246 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 6248 | if (bits % 8 != 0) { |
| 6249 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6250 | } |
| 6251 | data = mbedtls_calloc(1, bytes); |
| 6252 | if (data == NULL) { |
| 6253 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6254 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 6257 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6258 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6260 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 6262 | psa_des_set_key_parity(data, bytes); |
| 6263 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 6264 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | } else { |
| 6266 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 6267 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 6268 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6269 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 6270 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 6272 | }; |
| 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 6275 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 6276 | &storage_size); |
| 6277 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6278 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6280 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 6282 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6283 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | status = psa_driver_wrapper_import_key(&attributes, |
| 6287 | data, bytes, |
| 6288 | slot->key.data, |
| 6289 | slot->key.bytes, |
| 6290 | &slot->key.bytes, &bits); |
| 6291 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 6292 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6294 | |
| 6295 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | mbedtls_free(data); |
| 6297 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6298 | } |
| 6299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 6301 | psa_key_derivation_operation_t *operation, |
| 6302 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6303 | { |
| 6304 | psa_status_t status; |
| 6305 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6306 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6307 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6308 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6309 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6310 | /* Reject any attempt to create a zero-length key so that we don't |
| 6311 | * 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] | 6312 | if (psa_get_key_bits(attributes) == 0) { |
| 6313 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6314 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | if (operation->alg == PSA_ALG_NONE) { |
| 6317 | return PSA_ERROR_BAD_STATE; |
| 6318 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | if (!operation->can_output_key) { |
| 6321 | return PSA_ERROR_NOT_PERMITTED; |
| 6322 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 6325 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6326 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6328 | /* Deriving a key in a secure element is not implemented yet. */ |
| 6329 | status = PSA_ERROR_NOT_SUPPORTED; |
| 6330 | } |
| 6331 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | if (status == PSA_SUCCESS) { |
| 6333 | status = psa_generate_derived_key_internal(slot, |
| 6334 | attributes->core.bits, |
| 6335 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6336 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | if (status == PSA_SUCCESS) { |
| 6338 | status = psa_finish_key_creation(slot, driver, key); |
| 6339 | } |
| 6340 | if (status != PSA_SUCCESS) { |
| 6341 | psa_fail_key_creation(slot, driver); |
| 6342 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6345 | } |
| 6346 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 6347 | |
| 6348 | |
| 6349 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6350 | /* Key derivation */ |
| 6351 | /****************************************************************/ |
| 6352 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 6353 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6355 | { |
| 6356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 6358 | return 1; |
| 6359 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6360 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6361 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6363 | return 1; |
| 6364 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 6365 | #endif |
| 6366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6368 | return 1; |
| 6369 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6370 | #endif |
| 6371 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6373 | return 1; |
| 6374 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6375 | #endif |
| 6376 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6378 | return 1; |
| 6379 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6380 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6381 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6383 | return 1; |
| 6384 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6385 | #endif |
Kusumit Ghoderao | d132cac | 2023-05-03 12:00:27 +0530 | [diff] [blame] | 6386 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
| 6387 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 6388 | return 1; |
| 6389 | } |
| 6390 | #endif |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6391 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 6392 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
| 6393 | return 1; |
| 6394 | } |
| 6395 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 6397 | } |
| 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6400 | { |
| 6401 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 6403 | psa_hash_abort(&operation); |
| 6404 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6405 | } |
| 6406 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6407 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6408 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6410 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6411 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 6412 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6415 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 6417 | return PSA_ERROR_NOT_SUPPORTED; |
| 6418 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6419 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6420 | /* All currently supported key derivation algorithms (apart from |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6421 | * 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] | 6422 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 6423 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
Kusumit Ghoderao | 2cd6496 | 2023-06-22 15:38:57 +0530 | [diff] [blame] | 6424 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6425 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
| 6426 | } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6427 | 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] | 6428 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | if (hash_size == 0) { |
| 6430 | return PSA_ERROR_NOT_SUPPORTED; |
| 6431 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6432 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6433 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 6434 | * we might fail later, which is somewhat unfriendly and potentially |
| 6435 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 6437 | if (status != PSA_SUCCESS) { |
| 6438 | return status; |
| 6439 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6440 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 6441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 6443 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 6444 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 6445 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6446 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 6447 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6448 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 6450 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6451 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 6453 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 6454 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | operation->capacity = 255 * hash_size; |
| 6456 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6457 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6459 | 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] | 6460 | { |
| 6461 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | if (alg == PSA_ALG_ECDH) { |
| 6463 | return PSA_SUCCESS; |
| 6464 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6465 | #endif |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 6466 | #if defined(PSA_WANT_ALG_FFDH) |
| 6467 | if (alg == PSA_ALG_FFDH) { |
| 6468 | return PSA_SUCCESS; |
| 6469 | } |
| 6470 | #endif |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6471 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 6473 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6474 | |
| 6475 | static int psa_key_derivation_allows_free_form_secret_input( |
| 6476 | psa_algorithm_t kdf_alg) |
| 6477 | { |
| 6478 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6479 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 6480 | return 0; |
| 6481 | } |
| 6482 | #endif |
| 6483 | (void) kdf_alg; |
| 6484 | return 1; |
| 6485 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6486 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 6489 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6490 | { |
| 6491 | psa_status_t status; |
| 6492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6493 | if (operation->alg != 0) { |
| 6494 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6495 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6498 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6499 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 6500 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6501 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 6502 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 6503 | status = psa_key_agreement_try_support(ka_alg); |
| 6504 | if (status != PSA_SUCCESS) { |
| 6505 | return status; |
| 6506 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 6507 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 6508 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6509 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 6511 | #else |
| 6512 | return PSA_ERROR_NOT_SUPPORTED; |
| 6513 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6514 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 6515 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 6516 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 6517 | #else |
| 6518 | return PSA_ERROR_NOT_SUPPORTED; |
| 6519 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6520 | } else { |
| 6521 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6522 | } |
| 6523 | |
| 6524 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6525 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6526 | } |
| 6527 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6528 | } |
| 6529 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6530 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6531 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 6532 | psa_algorithm_t kdf_alg, |
| 6533 | psa_key_derivation_step_t step, |
| 6534 | const uint8_t *data, |
| 6535 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6536 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6537 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6538 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6539 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6540 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6541 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6543 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6544 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6545 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6546 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6547 | return PSA_ERROR_BAD_STATE; |
| 6548 | } else { |
| 6549 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6550 | hash_alg, |
| 6551 | data, data_length); |
| 6552 | if (status != PSA_SUCCESS) { |
| 6553 | return status; |
| 6554 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6555 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6556 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6557 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6558 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6559 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6560 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6561 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6562 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6563 | * the state. It could happen only if the hkdf |
| 6564 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6565 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6566 | return PSA_ERROR_BAD_STATE; |
| 6567 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6568 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6569 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6570 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6571 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6572 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | memcpy(hkdf->prk, data, data_length); |
| 6575 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6576 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6577 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6578 | /* HKDF: If no salt was provided, use an empty salt. |
| 6579 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6580 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6581 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6583 | return PSA_ERROR_BAD_STATE; |
| 6584 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6585 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6587 | hash_alg, |
| 6588 | NULL, 0); |
| 6589 | if (status != PSA_SUCCESS) { |
| 6590 | return status; |
| 6591 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6592 | hkdf->state = HKDF_STATE_STARTED; |
| 6593 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6594 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6595 | return PSA_ERROR_BAD_STATE; |
| 6596 | } |
| 6597 | status = psa_mac_update(&hkdf->hmac, |
| 6598 | data, data_length); |
| 6599 | if (status != PSA_SUCCESS) { |
| 6600 | return status; |
| 6601 | } |
| 6602 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6603 | hkdf->prk, |
| 6604 | sizeof(hkdf->prk), |
| 6605 | &data_length); |
| 6606 | if (status != PSA_SUCCESS) { |
| 6607 | return status; |
| 6608 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6609 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6610 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6611 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6612 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6613 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6614 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6615 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6616 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6617 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6618 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6619 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6620 | { |
| 6621 | /* Block 0 is empty, and the next block will be |
| 6622 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6624 | } |
| 6625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6627 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6628 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6629 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6630 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6631 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6632 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6633 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6634 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6635 | hkdf->state == HKDF_STATE_INIT) { |
| 6636 | return PSA_ERROR_BAD_STATE; |
| 6637 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6638 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6640 | return PSA_ERROR_BAD_STATE; |
| 6641 | } |
| 6642 | if (hkdf->info_set) { |
| 6643 | return PSA_ERROR_BAD_STATE; |
| 6644 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6645 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | if (data_length != 0) { |
| 6647 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6648 | if (hkdf->info == NULL) { |
| 6649 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6650 | } |
| 6651 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6652 | } |
| 6653 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6655 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6657 | } |
| 6658 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6659 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6660 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6661 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6662 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6664 | const uint8_t *data, |
| 6665 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6666 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6667 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6668 | return PSA_ERROR_BAD_STATE; |
| 6669 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | if (data_length != 0) { |
| 6672 | prf->seed = mbedtls_calloc(1, data_length); |
| 6673 | if (prf->seed == NULL) { |
| 6674 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6675 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6678 | prf->seed_length = data_length; |
| 6679 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6680 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6681 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6684 | } |
| 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6687 | const uint8_t *data, |
| 6688 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6689 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6691 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6692 | return PSA_ERROR_BAD_STATE; |
| 6693 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6695 | if (data_length != 0) { |
| 6696 | prf->secret = mbedtls_calloc(1, data_length); |
| 6697 | if (prf->secret == NULL) { |
| 6698 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6699 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6701 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6702 | prf->secret_length = data_length; |
| 6703 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6704 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6705 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6707 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6708 | } |
| 6709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6710 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6711 | const uint8_t *data, |
| 6712 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6713 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6715 | return PSA_ERROR_BAD_STATE; |
| 6716 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6718 | if (data_length != 0) { |
| 6719 | prf->label = mbedtls_calloc(1, data_length); |
| 6720 | if (prf->label == NULL) { |
| 6721 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6722 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6724 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6725 | prf->label_length = data_length; |
| 6726 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6727 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6728 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6730 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6731 | } |
| 6732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6734 | psa_key_derivation_step_t step, |
| 6735 | const uint8_t *data, |
| 6736 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6737 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6738 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6739 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6741 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6742 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6743 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6744 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6745 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6746 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6747 | } |
| 6748 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6749 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6750 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6751 | |
| 6752 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6753 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6754 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6755 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6757 | { |
| 6758 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6759 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6760 | 4 + data_length + prf->other_secret_length : |
| 6761 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6763 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6764 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6765 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6767 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6768 | if (pms == NULL) { |
| 6769 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6770 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6771 | uint8_t *cur = pms; |
| 6772 | |
| 6773 | /* pure-PSK: |
| 6774 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6775 | * |
| 6776 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6777 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6778 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6779 | * |
| 6780 | * mixed-PSK: |
| 6781 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6782 | * follows: concatenate a uint16 with the length of the other secret, |
| 6783 | * the other secret itself, uint16 with the length of PSK, and the |
| 6784 | * PSK itself. |
| 6785 | * For details please check: |
| 6786 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6787 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6788 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6789 | */ |
| 6790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6791 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6792 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 6793 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 6794 | if (prf->other_secret_length != 0) { |
| 6795 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 6796 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 6797 | cur += prf->other_secret_length; |
| 6798 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6799 | } else { |
| 6800 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6801 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6802 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6803 | cur += data_length; |
| 6804 | } |
| 6805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6806 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6807 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6808 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6809 | cur += data_length; |
| 6810 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 6811 | status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms)); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6812 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6813 | mbedtls_zeroize_and_free(pms, pms_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6814 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6815 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6816 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6817 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 6818 | psa_tls12_prf_key_derivation_t *prf, |
| 6819 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6820 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6821 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6822 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 6823 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6824 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6825 | |
| 6826 | if (data_length != 0) { |
| 6827 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 6828 | if (prf->other_secret == NULL) { |
| 6829 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6830 | } |
| 6831 | |
| 6832 | memcpy(prf->other_secret, data, data_length); |
| 6833 | prf->other_secret_length = data_length; |
| 6834 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6835 | prf->other_secret_length = 0; |
| 6836 | } |
| 6837 | |
| 6838 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 6839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6843 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6844 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6845 | psa_key_derivation_step_t step, |
| 6846 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6847 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6848 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6850 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 6852 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6853 | break; |
| 6854 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6855 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 6856 | data, |
| 6857 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6858 | break; |
| 6859 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6861 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6862 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6863 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6864 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6865 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6866 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6867 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6868 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 6869 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 6870 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6871 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6873 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 6875 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 6876 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 6877 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6878 | |
| 6879 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | if (data[0] != 0x04) { |
| 6881 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6882 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6883 | |
| 6884 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6887 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6888 | } |
| 6889 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6890 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 6891 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6892 | static psa_status_t psa_pbkdf2_set_input_cost( |
| 6893 | psa_pbkdf2_key_derivation_t *pbkdf2, |
| 6894 | psa_key_derivation_step_t step, |
| 6895 | uint64_t data) |
| 6896 | { |
| 6897 | if (step != PSA_KEY_DERIVATION_INPUT_COST) { |
| 6898 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6899 | } |
| 6900 | |
| 6901 | if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) { |
| 6902 | return PSA_ERROR_BAD_STATE; |
| 6903 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6904 | |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 6905 | if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) { |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6906 | return PSA_ERROR_NOT_SUPPORTED; |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6907 | } |
Kusumit Ghoderao | 3fc4ca7 | 2023-05-08 15:57:41 +0530 | [diff] [blame] | 6908 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6909 | if (data == 0) { |
| 6910 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6911 | } |
Kusumit Ghoderao | e66a8ad | 2023-05-24 12:30:43 +0530 | [diff] [blame] | 6912 | |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 6913 | pbkdf2->input_cost = data; |
| 6914 | pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET; |
| 6915 | |
| 6916 | return PSA_SUCCESS; |
| 6917 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6918 | |
| 6919 | static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2, |
| 6920 | const uint8_t *data, |
| 6921 | size_t data_length) |
| 6922 | { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6923 | if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) { |
| 6924 | pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET; |
| 6925 | } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) { |
| 6926 | /* Appending to existing salt. No state change. */ |
| 6927 | } else { |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6928 | return PSA_ERROR_BAD_STATE; |
| 6929 | } |
| 6930 | |
Gilles Peskine | ca57d78 | 2023-07-20 19:08:06 +0200 | [diff] [blame] | 6931 | if (data_length == 0) { |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6932 | /* Appending an empty string, nothing to do. */ |
| 6933 | } else { |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6934 | uint8_t *next_salt; |
Kusumit Ghoderao | b538bb7 | 2023-05-24 12:32:14 +0530 | [diff] [blame] | 6935 | |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6936 | next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length); |
| 6937 | if (next_salt == NULL) { |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 6938 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6939 | } |
| 6940 | |
Gilles Peskine | ead1766 | 2023-08-20 22:02:51 +0200 | [diff] [blame] | 6941 | if (pbkdf2->salt_length != 0) { |
| 6942 | memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length); |
| 6943 | } |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6944 | memcpy(next_salt + pbkdf2->salt_length, data, data_length); |
Kusumit Ghoderao | d0422f3 | 2023-05-08 15:56:19 +0530 | [diff] [blame] | 6945 | pbkdf2->salt_length += data_length; |
Kusumit Ghoderao | d7a3f80 | 2023-05-24 22:19:47 +0530 | [diff] [blame] | 6946 | mbedtls_free(pbkdf2->salt); |
| 6947 | pbkdf2->salt = next_salt; |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6948 | } |
Kusumit Ghoderao | 547a6c6 | 2023-05-03 12:18:19 +0530 | [diff] [blame] | 6949 | return PSA_SUCCESS; |
| 6950 | } |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 6951 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6952 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6953 | 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] | 6954 | const uint8_t *input, |
| 6955 | size_t input_len, |
| 6956 | uint8_t *output, |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6957 | size_t *output_len) |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6958 | { |
| 6959 | psa_status_t status = PSA_SUCCESS; |
| 6960 | if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) { |
| 6961 | status = psa_hash_compute(hash_alg, input, input_len, output, |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6962 | PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6963 | } else { |
| 6964 | memcpy(output, input, input_len); |
Kusumit Ghoderao | 91f99f5 | 2023-05-24 22:21:48 +0530 | [diff] [blame] | 6965 | *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg); |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6966 | } |
| 6967 | return status; |
| 6968 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6969 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 6970 | |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 6971 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6972 | static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input, |
| 6973 | size_t input_len, |
| 6974 | uint8_t *output, |
| 6975 | size_t *output_len) |
| 6976 | { |
| 6977 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6978 | 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] | 6979 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Kusumit Ghoderao | 3fde8fe | 2023-06-27 10:41:43 +0530 | [diff] [blame] | 6980 | uint8_t zeros[16] = { 0 }; |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6981 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 6982 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros))); |
| 6983 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6984 | /* 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] | 6985 | * mac_size as the driver function sets mac_output_length = mac_size |
| 6986 | * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */ |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6987 | status = psa_driver_wrapper_mac_compute(&attributes, |
| 6988 | zeros, sizeof(zeros), |
| 6989 | PSA_ALG_CMAC, input, input_len, |
| 6990 | output, |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6991 | PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, |
| 6992 | 128U, |
| 6993 | PSA_ALG_CMAC), |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 6994 | output_len); |
| 6995 | } else { |
| 6996 | memcpy(output, input, input_len); |
Kusumit Ghoderao | a12e2d5 | 2023-07-27 21:18:30 +0530 | [diff] [blame] | 6997 | *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] | 6998 | } |
| 6999 | return status; |
| 7000 | } |
Kusumit Ghoderao | 0bca4c5 | 2023-07-27 21:20:14 +0530 | [diff] [blame] | 7001 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7002 | |
| 7003 | 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] | 7004 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7005 | const uint8_t *data, |
| 7006 | size_t data_length) |
| 7007 | { |
Kusumit Ghoderao | aac9a58 | 2023-05-24 14:19:17 +0530 | [diff] [blame] | 7008 | psa_status_t status = PSA_SUCCESS; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7009 | if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) { |
| 7010 | return PSA_ERROR_BAD_STATE; |
| 7011 | } |
| 7012 | |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7013 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7014 | if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) { |
| 7015 | psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg); |
| 7016 | status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length, |
| 7017 | pbkdf2->password, |
| 7018 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7019 | } else |
| 7020 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */ |
| 7021 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) |
| 7022 | if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) { |
Kusumit Ghoderao | 3d5edb8 | 2023-06-22 15:41:25 +0530 | [diff] [blame] | 7023 | status = psa_pbkdf2_cmac_set_password(data, data_length, |
| 7024 | pbkdf2->password, |
| 7025 | &pbkdf2->password_length); |
Kusumit Ghoderao | f3e696d | 2023-07-28 13:30:50 +0530 | [diff] [blame] | 7026 | } else |
| 7027 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */ |
| 7028 | { |
| 7029 | return PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7030 | } |
| 7031 | |
| 7032 | pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET; |
| 7033 | |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7034 | return status; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7035 | } |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7036 | |
| 7037 | 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] | 7038 | psa_algorithm_t kdf_alg, |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7039 | psa_key_derivation_step_t step, |
| 7040 | const uint8_t *data, |
| 7041 | size_t data_length) |
| 7042 | { |
| 7043 | switch (step) { |
| 7044 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 7045 | return psa_pbkdf2_set_salt(pbkdf2, data, data_length); |
| 7046 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7047 | return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7048 | default: |
| 7049 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7050 | } |
| 7051 | } |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7052 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7053 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 7054 | /** Check whether the given key type is acceptable for the given |
| 7055 | * input step of a key derivation. |
| 7056 | * |
| 7057 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 7058 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 7059 | * Both secret and non-secret inputs can alternatively have the type |
| 7060 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 7061 | * that the input was passed as a buffer rather than via a key object. |
| 7062 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7063 | static int psa_key_derivation_check_input_type( |
| 7064 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7065 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7066 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7067 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7068 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7069 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7070 | return PSA_SUCCESS; |
| 7071 | } |
| 7072 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7073 | return PSA_SUCCESS; |
| 7074 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7075 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7076 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7077 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7078 | return PSA_SUCCESS; |
| 7079 | } |
| 7080 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7081 | return PSA_SUCCESS; |
| 7082 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 7083 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7084 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 7085 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 7086 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 7087 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7088 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 7089 | return PSA_SUCCESS; |
| 7090 | } |
| 7091 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7092 | return PSA_SUCCESS; |
| 7093 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7094 | break; |
Kusumit Ghoderao | f4fe3ee | 2023-05-03 12:21:07 +0530 | [diff] [blame] | 7095 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
| 7096 | if (key_type == PSA_KEY_TYPE_PASSWORD) { |
| 7097 | return PSA_SUCCESS; |
| 7098 | } |
| 7099 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 7100 | return PSA_SUCCESS; |
| 7101 | } |
| 7102 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 7103 | return PSA_SUCCESS; |
| 7104 | } |
| 7105 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7106 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7108 | } |
| 7109 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 7110 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7111 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7112 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7113 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7114 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7115 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7116 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7117 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7118 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 7119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7120 | status = psa_key_derivation_check_input_type(step, key_type); |
| 7121 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7122 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7123 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7124 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7125 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7126 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 7127 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 7128 | step, data, data_length); |
| 7129 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 7130 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7131 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7132 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 7133 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 7134 | step, data, data_length); |
| 7135 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7136 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 7137 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7138 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 7139 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 7140 | step, data, data_length); |
| 7141 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 7142 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7143 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7144 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7145 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7146 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 7147 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 7148 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7149 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7150 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | bd6cefb | 2023-05-24 12:36:34 +0530 | [diff] [blame] | 7151 | status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg, |
| 7152 | step, data, data_length); |
Kusumit Ghoderao | 24b3895 | 2023-05-03 12:25:26 +0530 | [diff] [blame] | 7153 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7154 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7155 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7156 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 7157 | (void) data; |
| 7158 | (void) data_length; |
| 7159 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7160 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7161 | } |
| 7162 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7163 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7164 | if (status != PSA_SUCCESS) { |
| 7165 | psa_key_derivation_abort(operation); |
| 7166 | } |
| 7167 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7168 | } |
| 7169 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7170 | static psa_status_t psa_key_derivation_input_integer_internal( |
| 7171 | psa_key_derivation_operation_t *operation, |
| 7172 | psa_key_derivation_step_t step, |
| 7173 | uint64_t value) |
| 7174 | { |
| 7175 | psa_status_t status; |
| 7176 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 7177 | |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7178 | #if defined(PSA_HAVE_SOFT_PBKDF2) |
Kusumit Ghoderao | 9ab03c3 | 2023-07-27 21:14:05 +0530 | [diff] [blame] | 7179 | if (PSA_ALG_IS_PBKDF2(kdf_alg)) { |
Kusumit Ghoderao | 944bba1 | 2023-05-03 12:14:03 +0530 | [diff] [blame] | 7180 | status = psa_pbkdf2_set_input_cost( |
| 7181 | &operation->ctx.pbkdf2, step, value); |
| 7182 | } else |
Kusumit Ghoderao | 2addf35 | 2023-07-27 21:09:26 +0530 | [diff] [blame] | 7183 | #endif /* PSA_HAVE_SOFT_PBKDF2 */ |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7184 | { |
Kusumit Ghoderao | 3a18dee | 2023-04-07 16:16:27 +0530 | [diff] [blame] | 7185 | (void) step; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7186 | (void) value; |
| 7187 | (void) kdf_alg; |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 7188 | status = PSA_ERROR_INVALID_ARGUMENT; |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | if (status != PSA_SUCCESS) { |
| 7192 | psa_key_derivation_abort(operation); |
| 7193 | } |
| 7194 | return status; |
| 7195 | } |
| 7196 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7197 | psa_status_t psa_key_derivation_input_bytes( |
| 7198 | psa_key_derivation_operation_t *operation, |
| 7199 | psa_key_derivation_step_t step, |
| 7200 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7201 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7202 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7203 | return psa_key_derivation_input_internal(operation, step, |
| 7204 | PSA_KEY_TYPE_NONE, |
| 7205 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 7206 | } |
| 7207 | |
Kusumit Ghoderao | 50e0e11 | 2023-04-06 17:47:25 +0530 | [diff] [blame] | 7208 | psa_status_t psa_key_derivation_input_integer( |
| 7209 | psa_key_derivation_operation_t *operation, |
| 7210 | psa_key_derivation_step_t step, |
| 7211 | uint64_t value) |
| 7212 | { |
| 7213 | return psa_key_derivation_input_integer_internal(operation, step, value); |
| 7214 | } |
| 7215 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 7216 | psa_status_t psa_key_derivation_input_key( |
| 7217 | psa_key_derivation_operation_t *operation, |
| 7218 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7219 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7220 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7221 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7222 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7223 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7224 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7225 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7226 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7227 | if (status != PSA_SUCCESS) { |
| 7228 | psa_key_derivation_abort(operation); |
| 7229 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 7230 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7231 | |
Kusumit Ghoderao | 3128c5d | 2023-05-03 12:27:57 +0530 | [diff] [blame] | 7232 | /* Passing a key object as a SECRET or PASSWORD input unlocks the |
| 7233 | * permission to output to a key object. */ |
| 7234 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET || |
| 7235 | step == PSA_KEY_DERIVATION_INPUT_PASSWORD) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7236 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7237 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 7238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7239 | status = psa_key_derivation_input_internal(operation, |
| 7240 | step, slot->attr.type, |
| 7241 | slot->key.data, |
| 7242 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7244 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7247 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7248 | |
| 7249 | |
| 7250 | |
| 7251 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7252 | /* Key agreement */ |
| 7253 | /****************************************************************/ |
| 7254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7255 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 7256 | const uint8_t *key_buffer, |
| 7257 | size_t key_buffer_size, |
| 7258 | psa_algorithm_t alg, |
| 7259 | const uint8_t *peer_key, |
| 7260 | size_t peer_key_length, |
| 7261 | uint8_t *shared_secret, |
| 7262 | size_t shared_secret_size, |
| 7263 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 7264 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7265 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 7266 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7267 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7268 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 7269 | key_buffer_size, alg, |
| 7270 | peer_key, peer_key_length, |
| 7271 | shared_secret, |
| 7272 | shared_secret_size, |
| 7273 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7274 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7275 | |
| 7276 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH) |
| 7277 | case PSA_ALG_FFDH: |
Przemek Stekiel | 152bb46 | 2023-06-01 11:52:39 +0200 | [diff] [blame] | 7278 | return mbedtls_psa_ffdh_key_agreement(attributes, |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 7279 | peer_key, |
| 7280 | peer_key_length, |
| 7281 | key_buffer, |
| 7282 | key_buffer_size, |
| 7283 | shared_secret, |
| 7284 | shared_secret_size, |
| 7285 | shared_secret_length); |
Przemek Stekiel | fb3dd54 | 2022-12-01 14:59:15 +0100 | [diff] [blame] | 7286 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */ |
| 7287 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7288 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7289 | (void) attributes; |
| 7290 | (void) key_buffer; |
| 7291 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 7292 | (void) peer_key; |
| 7293 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7294 | (void) shared_secret; |
| 7295 | (void) shared_secret_size; |
| 7296 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7297 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7298 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7299 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7300 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7301 | /** Internal function for raw key agreement |
| 7302 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 7303 | * to the driver's implementation if a driver is present. |
| 7304 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7305 | * (psa_key_agreement_raw_builtin). |
| 7306 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7307 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 7308 | psa_key_slot_t *private_key, |
| 7309 | const uint8_t *peer_key, |
| 7310 | size_t peer_key_length, |
| 7311 | uint8_t *shared_secret, |
| 7312 | size_t shared_secret_size, |
| 7313 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7314 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7315 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 7316 | return PSA_ERROR_NOT_SUPPORTED; |
| 7317 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7318 | |
| 7319 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7320 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 7321 | }; |
| 7322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7323 | return psa_driver_wrapper_key_agreement(&attributes, |
| 7324 | private_key->key.data, |
| 7325 | private_key->key.bytes, alg, |
| 7326 | peer_key, peer_key_length, |
| 7327 | shared_secret, |
| 7328 | shared_secret_size, |
| 7329 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7330 | } |
| 7331 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7332 | /* 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] | 7333 | * to potentially free embedded data structures and wipe confidential data. |
| 7334 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7335 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 7336 | psa_key_derivation_step_t step, |
| 7337 | psa_key_slot_t *private_key, |
| 7338 | const uint8_t *peer_key, |
| 7339 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7340 | { |
| 7341 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 7342 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7343 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7344 | 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] | 7345 | |
| 7346 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 7347 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7348 | status = psa_key_agreement_raw_internal(ka_alg, |
| 7349 | private_key, |
| 7350 | peer_key, peer_key_length, |
| 7351 | shared_secret, |
| 7352 | sizeof(shared_secret), |
| 7353 | &shared_secret_length); |
| 7354 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7355 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7356 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7357 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 7358 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 7359 | * the shared secret. A shared secret is permitted wherever a key |
| 7360 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7361 | status = psa_key_derivation_input_internal(operation, step, |
| 7362 | PSA_KEY_TYPE_DERIVE, |
| 7363 | shared_secret, |
| 7364 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7365 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7366 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 7367 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7368 | } |
| 7369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7370 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 7371 | psa_key_derivation_step_t step, |
| 7372 | mbedtls_svc_key_id_t private_key, |
| 7373 | const uint8_t *peer_key, |
| 7374 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7375 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7376 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7377 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 7378 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7380 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 7381 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7382 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7383 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7384 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 7385 | if (status != PSA_SUCCESS) { |
| 7386 | return status; |
| 7387 | } |
| 7388 | status = psa_key_agreement_internal(operation, step, |
| 7389 | slot, |
| 7390 | peer_key, peer_key_length); |
| 7391 | if (status != PSA_SUCCESS) { |
| 7392 | psa_key_derivation_abort(operation); |
| 7393 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7394 | /* If a private key has been added as SECRET, we allow the derived |
| 7395 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7397 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7398 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7399 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7401 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7403 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 7404 | } |
| 7405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7406 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 7407 | mbedtls_svc_key_id_t private_key, |
| 7408 | const uint8_t *peer_key, |
| 7409 | size_t peer_key_length, |
| 7410 | uint8_t *output, |
| 7411 | size_t output_size, |
| 7412 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7413 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7415 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7416 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7417 | size_t expected_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7419 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7420 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 7421 | goto exit; |
| 7422 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 7423 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7424 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 7425 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7426 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7427 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7428 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7429 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 7430 | * for the output size. The PSA specification only guarantees that this |
| 7431 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 7432 | * but it might be nice to allow smaller buffers if the output fits. |
| 7433 | * At the time of writing this comment, with only ECDH implemented, |
| 7434 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 7435 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 7436 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 7437 | expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7438 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 7439 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 7440 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 7441 | goto exit; |
| 7442 | } |
| 7443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7444 | status = psa_key_agreement_raw_internal(alg, slot, |
| 7445 | peer_key, peer_key_length, |
| 7446 | output, output_size, |
| 7447 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7448 | |
| 7449 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7450 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7451 | /* If an error happens and is not handled properly, the output |
| 7452 | * may be used as a key to protect sensitive data. Arrange for such |
| 7453 | * a key to be random, which is likely to result in decryption or |
| 7454 | * verification errors. This is better than filling the buffer with |
| 7455 | * some constant data such as zeros, which would result in the data |
| 7456 | * being protected with a reproducible, easily knowable key. |
| 7457 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7458 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7459 | *output_length = output_size; |
| 7460 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7462 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7464 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 7465 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 7466 | |
| 7467 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7468 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 7469 | /****************************************************************/ |
| 7470 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 7471 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7472 | |
Gilles Peskine | 672a771 | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 7473 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 7474 | #include "entropy_poll.h" |
| 7475 | #endif |
| 7476 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7477 | /** Initialize the PSA random generator. |
| 7478 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7479 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7480 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7481 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7482 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7483 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7484 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7485 | /* Set default configuration if |
| 7486 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7487 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7488 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7489 | } |
| 7490 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7491 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7492 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7494 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7495 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 7496 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 7497 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 7498 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7499 | mbedtls_entropy_add_source(&rng->entropy, |
| 7500 | mbedtls_nv_seed_poll, NULL, |
| 7501 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 7502 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7503 | #endif |
| 7504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7505 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7506 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7507 | } |
| 7508 | |
| 7509 | /** Deinitialize the PSA random generator. |
| 7510 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7511 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7512 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7513 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7514 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7515 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7516 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 7517 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7518 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | /** Seed the PSA random generator. |
| 7522 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7523 | 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] | 7524 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7525 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7526 | /* Do nothing: the external RNG seeds itself. */ |
| 7527 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7528 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7529 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7530 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7531 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 7532 | drbg_seed, sizeof(drbg_seed) - 1); |
| 7533 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7534 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7535 | } |
| 7536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7537 | psa_status_t psa_generate_random(uint8_t *output, |
| 7538 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7539 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7540 | GUARD_MODULE_INITIALIZED; |
| 7541 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7542 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7543 | |
| 7544 | size_t output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7545 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 7546 | output, output_size, |
| 7547 | &output_length); |
| 7548 | if (status != PSA_SUCCESS) { |
| 7549 | return status; |
| 7550 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7551 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 7552 | * for the external RNG interface. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7553 | if (output_length != output_size) { |
| 7554 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 7555 | } |
| 7556 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7557 | |
| 7558 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7560 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 7561 | size_t request_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7562 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 7563 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 7564 | output_size); |
| 7565 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 7566 | output, request_size); |
| 7567 | if (ret != 0) { |
| 7568 | return mbedtls_to_psa_error(ret); |
| 7569 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 7570 | output_size -= request_size; |
| 7571 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 7572 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7573 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7574 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7575 | } |
| 7576 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7577 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7578 | * |
| 7579 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 7580 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 7581 | * PSA API doesn't support passing an explicit state. |
| 7582 | * |
| 7583 | * In the non-external case, psa_generate_random() calls an |
| 7584 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 7585 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 7586 | * instead of doing this back-and-forth between the PSA API and the |
| 7587 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 7588 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7589 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 7591 | int mbedtls_psa_get_random(void *p_rng, |
| 7592 | unsigned char *output, |
| 7593 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7594 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 7595 | /* This function takes a pointer to the RNG state because that's what |
| 7596 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 7597 | * its own state internally and doesn't let the caller access that state. |
| 7598 | * So we just ignore the state parameter, and in practice we'll pass |
| 7599 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7600 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7601 | psa_status_t status = psa_generate_random(output, output_size); |
| 7602 | if (status == PSA_SUCCESS) { |
| 7603 | return 0; |
| 7604 | } else { |
| 7605 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 7606 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 7607 | } |
| 7608 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 7609 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7610 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7611 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 7612 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7613 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7614 | if (global_data.initialized) { |
| 7615 | return PSA_ERROR_NOT_PERMITTED; |
| 7616 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 7619 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 7620 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 7621 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7622 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 7623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7624 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7625 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 7626 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 7627 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7628 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7629 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7630 | * \param type The key type |
| 7631 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7632 | * |
| 7633 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7634 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7635 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 7636 | * The size in bits of the key is not valid. |
| 7637 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 7638 | * The type and/or the size in bits of the key or the combination of |
| 7639 | * the two is not supported. |
| 7640 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7641 | 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] | 7642 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7643 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 7644 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7646 | if (key_type_is_raw_bytes(type)) { |
| 7647 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 7648 | if (status != PSA_SUCCESS) { |
| 7649 | return status; |
| 7650 | } |
| 7651 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7652 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7653 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7654 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 7655 | return PSA_ERROR_NOT_SUPPORTED; |
| 7656 | } |
Waleed Elmelegy | d7bdbbe | 2023-07-20 16:26:58 +0000 | [diff] [blame] | 7657 | if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) { |
Waleed Elmelegy | ab57071 | 2023-07-05 16:40:58 +0000 | [diff] [blame] | 7658 | return PSA_ERROR_NOT_SUPPORTED; |
| 7659 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7660 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7661 | /* Accept only byte-aligned keys, for the same reasons as |
| 7662 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7663 | if (bits % 8 != 0) { |
| 7664 | return PSA_ERROR_NOT_SUPPORTED; |
| 7665 | } |
| 7666 | } else |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 7667 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7668 | |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7669 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7670 | 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] | 7671 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7672 | return PSA_SUCCESS; |
| 7673 | } else |
Valerio Setti | 6a9d0ee | 2023-06-21 10:07:21 +0200 | [diff] [blame] | 7674 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7675 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7676 | #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7677 | 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] | 7678 | if (psa_is_dh_key_size_valid(bits) == 0) { |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7679 | return PSA_ERROR_NOT_SUPPORTED; |
| 7680 | } |
| 7681 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7682 | #endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7683 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7684 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7685 | } |
| 7686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7687 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7688 | } |
| 7689 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 7690 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7691 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7692 | 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] | 7693 | { |
| 7694 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 7695 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7697 | if ((attributes->domain_parameters == NULL) && |
| 7698 | (attributes->domain_parameters_size != 0)) { |
| 7699 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7700 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 7701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7702 | if (key_type_is_raw_bytes(type)) { |
| 7703 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 7704 | if (status != PSA_SUCCESS) { |
| 7705 | return status; |
| 7706 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 7707 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 7708 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7709 | if (type == PSA_KEY_TYPE_DES) { |
| 7710 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 7711 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 7712 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7713 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7714 | |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7715 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7716 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 7717 | return mbedtls_psa_rsa_generate_key(attributes, |
| 7718 | key_buffer, |
| 7719 | key_buffer_size, |
| 7720 | key_buffer_length); |
| 7721 | } else |
Valerio Setti | 76df8c1 | 2023-07-11 14:11:28 +0200 | [diff] [blame] | 7722 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7723 | |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7724 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7725 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7726 | return mbedtls_psa_ecp_generate_key(attributes, |
| 7727 | key_buffer, |
| 7728 | key_buffer_size, |
| 7729 | key_buffer_length); |
| 7730 | } else |
Valerio Setti | bfeaf5b | 2023-06-20 13:27:46 +0200 | [diff] [blame] | 7731 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */ |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7732 | |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7733 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
Przemek Stekiel | fedd134 | 2022-12-01 15:00:02 +0100 | [diff] [blame] | 7734 | if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 7735 | return mbedtls_psa_ffdh_generate_key(attributes, |
| 7736 | key_buffer, |
| 7737 | key_buffer_size, |
| 7738 | key_buffer_length); |
| 7739 | } else |
Valerio Setti | a55f042 | 2023-07-10 15:34:41 +0200 | [diff] [blame] | 7740 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7741 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7742 | (void) key_buffer_length; |
| 7743 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 7744 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7746 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7747 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7749 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 7750 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7751 | { |
| 7752 | psa_status_t status; |
| 7753 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 7754 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7755 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7756 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 7757 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7758 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7759 | /* Reject any attempt to create a zero-length key so that we don't |
| 7760 | * 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] | 7761 | if (psa_get_key_bits(attributes) == 0) { |
| 7762 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7763 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 7764 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7765 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7766 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 7767 | return PSA_ERROR_INVALID_ARGUMENT; |
| 7768 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 7769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7770 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 7771 | &slot, &driver); |
| 7772 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7773 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7775 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7776 | /* 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] | 7777 | * storage ( thus not in the case of generating a key in a secure element |
| 7778 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 7779 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7780 | if (slot->key.data == NULL) { |
| 7781 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 7782 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7783 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7784 | attributes->core.type, attributes->core.bits); |
| 7785 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7786 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7787 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7788 | |
| 7789 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7790 | attributes->core.type, |
| 7791 | attributes->core.bits); |
| 7792 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7793 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7794 | attributes, &key_buffer_size); |
| 7795 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7796 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7797 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7798 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7800 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 7801 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7802 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7803 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7804 | } |
| 7805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7806 | status = psa_driver_wrapper_generate_key(attributes, |
| 7807 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7809 | if (status != PSA_SUCCESS) { |
| 7810 | psa_remove_key_data_from_memory(slot); |
| 7811 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7812 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7813 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7814 | if (status == PSA_SUCCESS) { |
| 7815 | status = psa_finish_key_creation(slot, driver, key); |
| 7816 | } |
| 7817 | if (status != PSA_SUCCESS) { |
| 7818 | psa_fail_key_creation(slot, driver); |
| 7819 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7821 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7822 | } |
| 7823 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7824 | /****************************************************************/ |
| 7825 | /* Module setup */ |
| 7826 | /****************************************************************/ |
| 7827 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7828 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7829 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7830 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 7831 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7832 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7833 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7834 | return PSA_ERROR_BAD_STATE; |
| 7835 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7836 | global_data.rng.entropy_init = entropy_init; |
| 7837 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7839 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7840 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7842 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7843 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7844 | psa_wipe_all_key_slots(); |
| 7845 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7846 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7847 | } |
| 7848 | /* Wipe all remaining data, including configuration. |
| 7849 | * In particular, this sets all state indicator to the value |
| 7850 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7851 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7852 | |
| 7853 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7854 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7855 | } |
| 7856 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7857 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 7858 | /** Recover a transaction that was interrupted by a power failure. |
| 7859 | * |
| 7860 | * This function is called during initialization, before psa_crypto_init() |
| 7861 | * returns. If this function returns a failure status, the initialization |
| 7862 | * fails. |
| 7863 | */ |
| 7864 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7866 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7867 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7868 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 7869 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7870 | /* TODO - fall through to the failure case until this |
| 7871 | * is implemented. |
| 7872 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 7873 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7874 | default: |
| 7875 | /* We found an unsupported transaction in the storage. |
| 7876 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7877 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7878 | } |
| 7879 | } |
| 7880 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 7881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7882 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7883 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7884 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7885 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7886 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7887 | if (global_data.initialized != 0) { |
| 7888 | return PSA_SUCCESS; |
| 7889 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7890 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 7891 | /* Init drivers */ |
| 7892 | status = psa_driver_wrapper_init(); |
| 7893 | if (status != PSA_SUCCESS) { |
| 7894 | goto exit; |
| 7895 | } |
| 7896 | global_data.drivers_initialized = 1; |
| 7897 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7898 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7899 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7900 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 7902 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7903 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7904 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7905 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | status = psa_initialize_key_slots(); |
| 7908 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7909 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7911 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7912 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7913 | status = psa_crypto_load_transaction(); |
| 7914 | if (status == PSA_SUCCESS) { |
| 7915 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 7916 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7917 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | } |
| 7919 | status = psa_crypto_stop_transaction(); |
| 7920 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7921 | /* There's no transaction to complete. It's all good. */ |
| 7922 | status = PSA_SUCCESS; |
| 7923 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7924 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7925 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7926 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7927 | global_data.initialized = 1; |
| 7928 | |
| 7929 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7930 | if (status != PSA_SUCCESS) { |
| 7931 | mbedtls_psa_crypto_free(); |
| 7932 | } |
| 7933 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7934 | } |
| 7935 | |
Yanray Wang | ffc3c48 | 2023-07-11 12:01:04 +0800 | [diff] [blame] | 7936 | #if defined(PSA_WANT_ALG_SOME_PAKE) |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 7937 | psa_status_t psa_crypto_driver_pake_get_password_len( |
| 7938 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7939 | size_t *password_len) |
| 7940 | { |
| 7941 | if (inputs->password_len == 0) { |
| 7942 | return PSA_ERROR_BAD_STATE; |
| 7943 | } |
| 7944 | |
| 7945 | *password_len = inputs->password_len; |
| 7946 | |
| 7947 | return PSA_SUCCESS; |
| 7948 | } |
| 7949 | |
| 7950 | psa_status_t psa_crypto_driver_pake_get_password( |
| 7951 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7952 | uint8_t *buffer, size_t buffer_size, size_t *buffer_length) |
| 7953 | { |
| 7954 | if (inputs->password_len == 0) { |
| 7955 | return PSA_ERROR_BAD_STATE; |
| 7956 | } |
| 7957 | |
| 7958 | if (buffer_size < inputs->password_len) { |
| 7959 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 7960 | } |
| 7961 | |
| 7962 | memcpy(buffer, inputs->password, inputs->password_len); |
| 7963 | *buffer_length = inputs->password_len; |
| 7964 | |
| 7965 | return PSA_SUCCESS; |
| 7966 | } |
| 7967 | |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7968 | psa_status_t psa_crypto_driver_pake_get_user_len( |
| 7969 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 7970 | size_t *user_len) |
| 7971 | { |
| 7972 | if (inputs->user_len == 0) { |
| 7973 | return PSA_ERROR_BAD_STATE; |
| 7974 | } |
| 7975 | |
| 7976 | *user_len = inputs->user_len; |
| 7977 | |
| 7978 | return PSA_SUCCESS; |
| 7979 | } |
| 7980 | |
| 7981 | psa_status_t psa_crypto_driver_pake_get_user( |
| 7982 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7983 | 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] | 7984 | { |
| 7985 | if (inputs->user_len == 0) { |
| 7986 | return PSA_ERROR_BAD_STATE; |
| 7987 | } |
| 7988 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7989 | if (user_id_size < inputs->user_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7990 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 7991 | } |
| 7992 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 7993 | memcpy(user_id, inputs->user, inputs->user_len); |
| 7994 | *user_id_len = inputs->user_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 7995 | |
| 7996 | return PSA_SUCCESS; |
| 7997 | } |
| 7998 | |
| 7999 | psa_status_t psa_crypto_driver_pake_get_peer_len( |
| 8000 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8001 | size_t *peer_len) |
| 8002 | { |
| 8003 | if (inputs->peer_len == 0) { |
| 8004 | return PSA_ERROR_BAD_STATE; |
| 8005 | } |
| 8006 | |
| 8007 | *peer_len = inputs->peer_len; |
| 8008 | |
| 8009 | return PSA_SUCCESS; |
| 8010 | } |
| 8011 | |
| 8012 | psa_status_t psa_crypto_driver_pake_get_peer( |
| 8013 | const psa_crypto_driver_pake_inputs_t *inputs, |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8014 | 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] | 8015 | { |
| 8016 | if (inputs->peer_len == 0) { |
| 8017 | return PSA_ERROR_BAD_STATE; |
| 8018 | } |
| 8019 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8020 | if (peer_id_size < inputs->peer_len) { |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8021 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 8022 | } |
| 8023 | |
Przemek Stekiel | c0e6250 | 2023-03-14 11:49:36 +0100 | [diff] [blame] | 8024 | memcpy(peer_id, inputs->peer, inputs->peer_len); |
| 8025 | *peer_id_length = inputs->peer_len; |
Przemek Stekiel | 1e7a927 | 2023-02-28 14:38:58 +0100 | [diff] [blame] | 8026 | |
| 8027 | return PSA_SUCCESS; |
| 8028 | } |
| 8029 | |
Przemek Stekiel | ca8d2b2 | 2023-01-17 16:21:33 +0100 | [diff] [blame] | 8030 | psa_status_t psa_crypto_driver_pake_get_cipher_suite( |
| 8031 | const psa_crypto_driver_pake_inputs_t *inputs, |
| 8032 | psa_pake_cipher_suite_t *cipher_suite) |
| 8033 | { |
| 8034 | if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) { |
| 8035 | return PSA_ERROR_BAD_STATE; |
| 8036 | } |
| 8037 | |
| 8038 | *cipher_suite = inputs->cipher_suite; |
| 8039 | |
| 8040 | return PSA_SUCCESS; |
| 8041 | } |
| 8042 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8043 | psa_status_t psa_pake_setup( |
| 8044 | psa_pake_operation_t *operation, |
| 8045 | const psa_pake_cipher_suite_t *cipher_suite) |
| 8046 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8047 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8048 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 8049 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8050 | status = PSA_ERROR_BAD_STATE; |
| 8051 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8052 | } |
| 8053 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8054 | if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 || |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8055 | PSA_ALG_IS_HASH(cipher_suite->hash) == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8056 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8057 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8058 | } |
| 8059 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8060 | memset(&operation->data.inputs, 0, sizeof(operation->data.inputs)); |
| 8061 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8062 | operation->alg = cipher_suite->algorithm; |
Przemek Stekiel | 656b259 | 2023-03-22 13:15:33 +0100 | [diff] [blame] | 8063 | operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type, |
| 8064 | cipher_suite->family, cipher_suite->bits); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8065 | operation->data.inputs.cipher_suite = *cipher_suite; |
| 8066 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8067 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8068 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8069 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | dde6a91 | 2023-01-26 08:46:37 +0100 | [diff] [blame] | 8070 | &operation->computation_stage.jpake; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8071 | |
David Horstmann | 16f0151 | 2023-06-14 17:21:07 +0100 | [diff] [blame] | 8072 | memset(computation_stage, 0, sizeof(*computation_stage)); |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8073 | computation_stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8074 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8075 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8076 | { |
| 8077 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8078 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8079 | } |
| 8080 | |
Przemek Stekiel | 1c3cfb4 | 2023-01-26 10:35:02 +0100 | [diff] [blame] | 8081 | operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS; |
| 8082 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8083 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8084 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8085 | psa_pake_abort(operation); |
| 8086 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8087 | } |
| 8088 | |
| 8089 | psa_status_t psa_pake_set_password_key( |
| 8090 | psa_pake_operation_t *operation, |
| 8091 | mbedtls_svc_key_id_t password) |
| 8092 | { |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8093 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8094 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8095 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8096 | psa_key_attributes_t attributes; |
| 8097 | psa_key_type_t type; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8098 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8099 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8100 | status = PSA_ERROR_BAD_STATE; |
| 8101 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8102 | } |
| 8103 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8104 | status = psa_get_and_lock_key_slot_with_policy(password, &slot, |
| 8105 | PSA_KEY_USAGE_DERIVE, |
Przemek Stekiel | d5d28a2 | 2023-01-26 10:46:05 +0100 | [diff] [blame] | 8106 | operation->alg); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8107 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8108 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8109 | } |
| 8110 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8111 | attributes = (psa_key_attributes_t) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 8112 | .core = slot->attr |
| 8113 | }; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8114 | |
Agathiyan Bragadeesh | 4d47cea | 2023-07-17 16:57:24 +0100 | [diff] [blame] | 8115 | type = psa_get_key_type(&attributes); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8116 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8117 | if (type != PSA_KEY_TYPE_PASSWORD && |
| 8118 | type != PSA_KEY_TYPE_PASSWORD_HASH) { |
| 8119 | status = PSA_ERROR_INVALID_ARGUMENT; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8120 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8121 | } |
| 8122 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8123 | operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes); |
| 8124 | if (operation->data.inputs.password == NULL) { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8125 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8126 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8127 | } |
| 8128 | |
| 8129 | memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes); |
| 8130 | operation->data.inputs.password_len = slot->key.bytes; |
Przemek Stekiel | 9dd2440 | 2023-01-26 15:06:09 +0100 | [diff] [blame] | 8131 | operation->data.inputs.attributes = attributes; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8132 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8133 | if (status != PSA_SUCCESS) { |
| 8134 | psa_pake_abort(operation); |
| 8135 | } |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8136 | unlock_status = psa_unlock_key_slot(slot); |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8137 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8138 | } |
| 8139 | |
| 8140 | psa_status_t psa_pake_set_user( |
| 8141 | psa_pake_operation_t *operation, |
| 8142 | const uint8_t *user_id, |
| 8143 | size_t user_id_len) |
| 8144 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8145 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8146 | |
| 8147 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8148 | status = PSA_ERROR_BAD_STATE; |
| 8149 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8150 | } |
| 8151 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8152 | if (user_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8153 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8154 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8155 | } |
| 8156 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8157 | if (operation->data.inputs.user_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8158 | status = PSA_ERROR_BAD_STATE; |
| 8159 | goto exit; |
| 8160 | } |
| 8161 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8162 | operation->data.inputs.user = mbedtls_calloc(1, user_id_len); |
| 8163 | if (operation->data.inputs.user == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8164 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8165 | goto exit; |
| 8166 | } |
| 8167 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8168 | memcpy(operation->data.inputs.user, user_id, user_id_len); |
| 8169 | operation->data.inputs.user_len = user_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8170 | |
| 8171 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8172 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8173 | psa_pake_abort(operation); |
| 8174 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8175 | } |
| 8176 | |
| 8177 | psa_status_t psa_pake_set_peer( |
| 8178 | psa_pake_operation_t *operation, |
| 8179 | const uint8_t *peer_id, |
| 8180 | size_t peer_id_len) |
| 8181 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8182 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8183 | |
| 8184 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8185 | status = PSA_ERROR_BAD_STATE; |
| 8186 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8187 | } |
| 8188 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8189 | if (peer_id_len == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8190 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8191 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8192 | } |
| 8193 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8194 | if (operation->data.inputs.peer_len != 0) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8195 | status = PSA_ERROR_BAD_STATE; |
| 8196 | goto exit; |
| 8197 | } |
| 8198 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8199 | operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len); |
| 8200 | if (operation->data.inputs.peer == NULL) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8201 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8202 | goto exit; |
| 8203 | } |
| 8204 | |
Przemek Stekiel | f309d6b | 2023-03-10 09:54:45 +0100 | [diff] [blame] | 8205 | memcpy(operation->data.inputs.peer, peer_id, peer_id_len); |
| 8206 | operation->data.inputs.peer_len = peer_id_len; |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8207 | |
| 8208 | return PSA_SUCCESS; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8209 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8210 | psa_pake_abort(operation); |
| 8211 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8212 | } |
| 8213 | |
| 8214 | psa_status_t psa_pake_set_role( |
| 8215 | psa_pake_operation_t *operation, |
| 8216 | psa_pake_role_t role) |
| 8217 | { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8218 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8219 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8220 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8221 | status = PSA_ERROR_BAD_STATE; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8222 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8223 | } |
| 8224 | |
Przemek Stekiel | 09104b8 | 2023-03-06 14:11:51 +0100 | [diff] [blame] | 8225 | switch (operation->alg) { |
| 8226 | #if defined(PSA_WANT_ALG_JPAKE) |
| 8227 | case PSA_ALG_JPAKE: |
| 8228 | if (role == PSA_PAKE_ROLE_NONE) { |
| 8229 | return PSA_SUCCESS; |
| 8230 | } |
| 8231 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8232 | break; |
| 8233 | #endif |
| 8234 | default: |
| 8235 | (void) role; |
| 8236 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8237 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8238 | } |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8239 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8240 | psa_pake_abort(operation); |
| 8241 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8242 | } |
| 8243 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8244 | /* Auxiliary function to convert core computation stage to single driver step. */ |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8245 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8246 | 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] | 8247 | psa_jpake_computation_stage_t *stage) |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8248 | { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8249 | psa_crypto_driver_pake_step_t key_share_step; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8250 | if (stage->round == PSA_JPAKE_FIRST) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8251 | int is_x1; |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8252 | |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8253 | if (stage->io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8254 | is_x1 = (stage->outputs < 1); |
| 8255 | } else { |
| 8256 | is_x1 = (stage->inputs < 1); |
| 8257 | } |
| 8258 | |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8259 | key_share_step = is_x1 ? |
| 8260 | PSA_JPAKE_X1_STEP_KEY_SHARE : |
| 8261 | PSA_JPAKE_X2_STEP_KEY_SHARE; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8262 | } else if (stage->round == PSA_JPAKE_SECOND) { |
David Horstmann | 74a3d8c | 2023-06-14 18:28:19 +0100 | [diff] [blame] | 8263 | key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ? |
| 8264 | PSA_JPAKE_X2S_STEP_KEY_SHARE : |
| 8265 | PSA_JPAKE_X4S_STEP_KEY_SHARE; |
| 8266 | } else { |
| 8267 | return PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8268 | } |
Agathiyan Bragadeesh | 387bfa5 | 2023-07-17 17:01:33 +0100 | [diff] [blame] | 8269 | 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] | 8270 | } |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8271 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 8272 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8273 | static psa_status_t psa_pake_complete_inputs( |
| 8274 | psa_pake_operation_t *operation) |
| 8275 | { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8276 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8277 | /* Create copy of the inputs on stack as inputs share memory |
| 8278 | with the driver context which will be setup by the driver. */ |
| 8279 | psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8280 | |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8281 | if (inputs.password_len == 0) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8282 | return PSA_ERROR_BAD_STATE; |
| 8283 | } |
| 8284 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8285 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 8286 | if (inputs.user_len == 0 || inputs.peer_len == 0) { |
| 8287 | return PSA_ERROR_BAD_STATE; |
| 8288 | } |
Przemek Stekiel | dff21d3 | 2023-02-14 20:09:10 +0100 | [diff] [blame] | 8289 | } |
| 8290 | |
Przemek Stekiel | 18620a3 | 2023-01-17 16:34:52 +0100 | [diff] [blame] | 8291 | /* Clear driver context */ |
| 8292 | mbedtls_platform_zeroize(&operation->data, sizeof(operation->data)); |
| 8293 | |
| 8294 | status = psa_driver_wrapper_pake_setup(operation, &inputs); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8295 | |
| 8296 | /* Driver is responsible for creating its own copy of the password. */ |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8297 | mbedtls_zeroize_and_free(inputs.password, inputs.password_len); |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8298 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8299 | /* User and peer are translated to role. */ |
| 8300 | mbedtls_free(inputs.user); |
| 8301 | mbedtls_free(inputs.peer); |
| 8302 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8303 | if (status == PSA_SUCCESS) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8304 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8305 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | d93de32 | 2023-02-24 08:39:04 +0100 | [diff] [blame] | 8306 | operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION; |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8307 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8308 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8309 | { |
| 8310 | status = PSA_ERROR_NOT_SUPPORTED; |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8311 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8312 | } |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8313 | return status; |
| 8314 | } |
| 8315 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8316 | #if defined(PSA_WANT_ALG_JPAKE) |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8317 | static psa_status_t psa_jpake_prologue( |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8318 | psa_pake_operation_t *operation, |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8319 | psa_pake_step_t step, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8320 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8321 | { |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8322 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
| 8323 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 8324 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 8325 | return PSA_ERROR_INVALID_ARGUMENT; |
| 8326 | } |
| 8327 | |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8328 | psa_jpake_computation_stage_t *computation_stage = |
| 8329 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8330 | |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8331 | if (computation_stage->round != PSA_JPAKE_FIRST && |
| 8332 | computation_stage->round != PSA_JPAKE_SECOND) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8333 | return PSA_ERROR_BAD_STATE; |
| 8334 | } |
| 8335 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8336 | /* Check that the step we are given is the one we were expecting */ |
| 8337 | if (step != computation_stage->step) { |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8338 | return PSA_ERROR_BAD_STATE; |
| 8339 | } |
| 8340 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8341 | if (step == PSA_PAKE_STEP_KEY_SHARE && |
| 8342 | computation_stage->inputs == 0 && |
| 8343 | computation_stage->outputs == 0) { |
| 8344 | /* Start of the round, so function decides whether we are inputting |
| 8345 | * or outputting */ |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8346 | computation_stage->io_mode = io_mode; |
| 8347 | } else if (computation_stage->io_mode != io_mode) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8348 | /* Middle of the round so the mode we are in must match the function |
| 8349 | * called by the user */ |
| 8350 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8351 | } |
| 8352 | |
| 8353 | return PSA_SUCCESS; |
| 8354 | } |
| 8355 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8356 | static psa_status_t psa_jpake_epilogue( |
| 8357 | psa_pake_operation_t *operation, |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8358 | psa_jpake_io_mode_t io_mode) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8359 | { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8360 | psa_jpake_computation_stage_t *stage = |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8361 | &operation->computation_stage.jpake; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8362 | |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8363 | if (stage->step == PSA_PAKE_STEP_ZK_PROOF) { |
| 8364 | /* End of an input/output */ |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8365 | if (io_mode == PSA_JPAKE_INPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8366 | stage->inputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8367 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8368 | stage->io_mode = PSA_JPAKE_OUTPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8369 | } |
| 8370 | } |
David Horstmann | 00ad6bf | 2023-06-14 15:44:24 +0100 | [diff] [blame] | 8371 | if (io_mode == PSA_JPAKE_OUTPUT) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8372 | stage->outputs++; |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8373 | if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | 024e5c5 | 2023-06-14 15:48:21 +0100 | [diff] [blame] | 8374 | stage->io_mode = PSA_JPAKE_INPUT; |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8375 | } |
| 8376 | } |
David Horstmann | 246ec5a | 2023-06-27 10:33:06 +0100 | [diff] [blame] | 8377 | if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) && |
| 8378 | stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8379 | /* End of a round, move to the next round */ |
| 8380 | stage->inputs = 0; |
| 8381 | stage->outputs = 0; |
| 8382 | stage->round++; |
| 8383 | } |
| 8384 | stage->step = PSA_PAKE_STEP_KEY_SHARE; |
Przemek Stekiel | 5eff103 | 2023-02-21 19:10:36 +0100 | [diff] [blame] | 8385 | } else { |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8386 | stage->step++; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8387 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8388 | return PSA_SUCCESS; |
| 8389 | } |
David Horstmann | e7f21e6 | 2023-05-12 18:17:21 +0100 | [diff] [blame] | 8390 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8391 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8392 | |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8393 | psa_status_t psa_pake_output( |
| 8394 | psa_pake_operation_t *operation, |
| 8395 | psa_pake_step_t step, |
| 8396 | uint8_t *output, |
| 8397 | size_t output_size, |
| 8398 | size_t *output_length) |
| 8399 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8400 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8401 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8402 | *output_length = 0; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8403 | |
| 8404 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8405 | status = psa_pake_complete_inputs(operation); |
| 8406 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8407 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8408 | } |
| 8409 | } |
| 8410 | |
| 8411 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8412 | status = PSA_ERROR_BAD_STATE; |
| 8413 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8414 | } |
| 8415 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8416 | if (output_size == 0) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8417 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8418 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8419 | } |
| 8420 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8421 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8422 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8423 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8424 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8425 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8426 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8427 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8428 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8429 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8430 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8431 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8432 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8433 | (void) step; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8434 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8435 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8436 | } |
| 8437 | |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8438 | status = psa_driver_wrapper_pake_output(operation, driver_step, |
| 8439 | output, output_size, output_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8440 | |
| 8441 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8442 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8443 | } |
| 8444 | |
| 8445 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8446 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8447 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8448 | status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8449 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8450 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8451 | } |
| 8452 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8453 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8454 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8455 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8456 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8457 | } |
| 8458 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8459 | return PSA_SUCCESS; |
| 8460 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8461 | psa_pake_abort(operation); |
| 8462 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8463 | } |
| 8464 | |
| 8465 | psa_status_t psa_pake_input( |
| 8466 | psa_pake_operation_t *operation, |
| 8467 | psa_pake_step_t step, |
| 8468 | const uint8_t *input, |
| 8469 | size_t input_length) |
| 8470 | { |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8471 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8472 | psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID; |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8473 | const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg, |
| 8474 | operation->primitive, |
| 8475 | step); |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8476 | |
| 8477 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 8478 | status = psa_pake_complete_inputs(operation); |
| 8479 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8480 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8481 | } |
| 8482 | } |
| 8483 | |
| 8484 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8485 | status = PSA_ERROR_BAD_STATE; |
| 8486 | goto exit; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 8487 | } |
| 8488 | |
Przemek Stekiel | 256c75d | 2023-03-23 14:09:34 +0100 | [diff] [blame] | 8489 | if (input_length == 0 || input_length > max_input_length) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8490 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 8491 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8492 | } |
| 8493 | |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8494 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8495 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8496 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8497 | status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8498 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8499 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8500 | } |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8501 | driver_step = convert_jpake_computation_stage_to_driver_step( |
| 8502 | &operation->computation_stage.jpake); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8503 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8504 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8505 | default: |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8506 | (void) step; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8507 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8508 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8509 | } |
| 8510 | |
Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 8511 | status = psa_driver_wrapper_pake_input(operation, driver_step, |
| 8512 | input, input_length); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8513 | |
| 8514 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8515 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8516 | } |
| 8517 | |
| 8518 | switch (operation->alg) { |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8519 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8520 | case PSA_ALG_JPAKE: |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8521 | status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT); |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8522 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8523 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8524 | } |
| 8525 | break; |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8526 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8527 | default: |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8528 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8529 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8530 | } |
| 8531 | |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8532 | return PSA_SUCCESS; |
| 8533 | exit: |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8534 | psa_pake_abort(operation); |
| 8535 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8536 | } |
| 8537 | |
| 8538 | psa_status_t psa_pake_get_implicit_key( |
| 8539 | psa_pake_operation_t *operation, |
| 8540 | psa_key_derivation_operation_t *output) |
| 8541 | { |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8542 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8543 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 8544 | uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8545 | size_t shared_key_len = 0; |
| 8546 | |
Przemek Stekiel | f62b3bb | 2023-01-31 19:51:24 +0100 | [diff] [blame] | 8547 | if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8548 | status = PSA_ERROR_BAD_STATE; |
| 8549 | goto exit; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8550 | } |
| 8551 | |
Przemek Stekiel | 4aa9940 | 2023-02-27 13:00:57 +0100 | [diff] [blame] | 8552 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8553 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8554 | psa_jpake_computation_stage_t *computation_stage = |
Przemek Stekiel | 083745e | 2023-02-23 17:28:23 +0100 | [diff] [blame] | 8555 | &operation->computation_stage.jpake; |
David Horstmann | 5da9560 | 2023-06-08 15:37:12 +0100 | [diff] [blame] | 8556 | if (computation_stage->round != PSA_JPAKE_FINISHED) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8557 | status = PSA_ERROR_BAD_STATE; |
| 8558 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8559 | } |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8560 | } else |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8561 | #endif /* PSA_WANT_ALG_JPAKE */ |
Przemek Stekiel | 80a8849 | 2023-02-20 13:32:22 +0100 | [diff] [blame] | 8562 | { |
| 8563 | status = PSA_ERROR_NOT_SUPPORTED; |
| 8564 | goto exit; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8565 | } |
| 8566 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8567 | status = psa_driver_wrapper_pake_get_implicit_key(operation, |
| 8568 | shared_key, |
Przemek Stekiel | 6b64862 | 2023-02-19 22:55:33 +0100 | [diff] [blame] | 8569 | sizeof(shared_key), |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8570 | &shared_key_len); |
| 8571 | |
| 8572 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8573 | goto exit; |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 8574 | } |
| 8575 | |
| 8576 | status = psa_key_derivation_input_bytes(output, |
| 8577 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8578 | shared_key, |
| 8579 | shared_key_len); |
| 8580 | |
Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 8581 | mbedtls_platform_zeroize(shared_key, sizeof(shared_key)); |
Przemek Stekiel | 3e784d8 | 2023-02-08 09:12:42 +0100 | [diff] [blame] | 8582 | exit: |
| 8583 | abort_status = psa_pake_abort(operation); |
| 8584 | return status == PSA_SUCCESS ? abort_status : status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8585 | } |
| 8586 | |
| 8587 | psa_status_t psa_pake_abort( |
| 8588 | psa_pake_operation_t *operation) |
| 8589 | { |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8590 | psa_status_t status = PSA_SUCCESS; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 8591 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8592 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) { |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8593 | status = psa_driver_wrapper_pake_abort(operation); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 8594 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8595 | |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8596 | if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) { |
| 8597 | if (operation->data.inputs.password != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 8598 | mbedtls_zeroize_and_free(operation->data.inputs.password, |
Przemek Stekiel | aa18342 | 2023-03-06 14:21:44 +0100 | [diff] [blame] | 8599 | operation->data.inputs.password_len); |
Przemek Stekiel | 26c909d | 2023-02-28 12:34:03 +0100 | [diff] [blame] | 8600 | } |
| 8601 | if (operation->data.inputs.user != NULL) { |
| 8602 | mbedtls_free(operation->data.inputs.user); |
| 8603 | } |
| 8604 | if (operation->data.inputs.peer != NULL) { |
| 8605 | mbedtls_free(operation->data.inputs.peer); |
| 8606 | } |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8607 | } |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8608 | memset(operation, 0, sizeof(psa_pake_operation_t)); |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 8609 | |
Przemek Stekiel | d69dca9 | 2023-01-31 19:59:20 +0100 | [diff] [blame] | 8610 | return status; |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8611 | } |
Tom Cosgrove | 6d62fac | 2023-05-10 14:40:05 +0100 | [diff] [blame] | 8612 | #endif /* PSA_WANT_ALG_SOME_PAKE */ |
Neil Armstrong | a7d08c3 | 2022-06-01 18:21:20 +0200 | [diff] [blame] | 8613 | |
David Horstmann | 00d7a04 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 8614 | /* Memory copying test hooks. These are called before input copy, after input |
| 8615 | * copy, before output copy and after output copy, respectively. |
| 8616 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 8617 | * while they are copied. */ |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8618 | #if defined(MBEDTLS_TEST_HOOKS) |
| 8619 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8620 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 8621 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8622 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 8623 | #endif |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8624 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8625 | /** Copy from an input buffer to a local copy. |
| 8626 | * |
| 8627 | * \param[in] input Pointer to input buffer. |
| 8628 | * \param[in] input_len Length of the input buffer. |
| 8629 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 8630 | * \param[out] input_copy_len Length of the local copy buffer. |
| 8631 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8632 | * copied. |
| 8633 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 8634 | * copy is too small to hold contents of the |
| 8635 | * input buffer. |
| 8636 | */ |
| 8637 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8638 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 8639 | uint8_t *input_copy, size_t input_copy_len) |
| 8640 | { |
| 8641 | if (input_len > input_copy_len) { |
David Horstmann | 49a7276 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 8642 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8643 | } |
| 8644 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8645 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8646 | if (psa_input_pre_copy_hook != NULL) { |
| 8647 | psa_input_pre_copy_hook(input, input_len); |
| 8648 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8649 | #endif |
| 8650 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8651 | if (input_len > 0) { |
| 8652 | memcpy(input_copy, input, input_len); |
| 8653 | } |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8654 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8655 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8656 | if (psa_input_post_copy_hook != NULL) { |
| 8657 | psa_input_post_copy_hook(input, input_len); |
| 8658 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8659 | #endif |
| 8660 | |
David Horstmann | fde9739 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 8661 | return PSA_SUCCESS; |
| 8662 | } |
| 8663 | |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8664 | /** Copy from a local output buffer into a user-supplied one. |
| 8665 | * |
| 8666 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 8667 | * \param[in] output_copy_len Length of the local buffer. |
| 8668 | * \param[out] output Pointer to user-supplied output buffer. |
| 8669 | * \param[out] output_len Length of the user-supplied output buffer. |
| 8670 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 8671 | * copied. |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8672 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | 1b7279a | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 8673 | * user-supplied output buffer is too small to |
| 8674 | * hold the contents of the local buffer. |
| 8675 | */ |
| 8676 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8677 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 8678 | uint8_t *output, size_t output_len) |
| 8679 | { |
| 8680 | if (output_len < output_copy_len) { |
David Horstmann | 671f5f5 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 8681 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8682 | } |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8683 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8684 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8685 | if (psa_output_pre_copy_hook != NULL) { |
| 8686 | psa_output_pre_copy_hook(output, output_len); |
| 8687 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8688 | #endif |
| 8689 | |
David Horstmann | 777e741 | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 8690 | if (output_copy_len > 0) { |
| 8691 | memcpy(output, output_copy, output_copy_len); |
| 8692 | } |
| 8693 | |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8694 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | e138dce | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 8695 | if (psa_output_post_copy_hook != NULL) { |
| 8696 | psa_output_post_copy_hook(output, output_len); |
| 8697 | } |
David Horstmann | 372b8bb | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | |
David Horstmann | 8978f5c | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 8700 | return PSA_SUCCESS; |
| 8701 | } |
| 8702 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8703 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 8704 | psa_crypto_local_input_t *local_input) |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8705 | { |
| 8706 | psa_status_t status; |
| 8707 | |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8708 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8709 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8710 | if (input_len == 0) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8711 | return PSA_SUCCESS; |
| 8712 | } |
| 8713 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8714 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 8715 | if (local_input->buffer == NULL) { |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8716 | /* Since we dealt with the zero-length case above, we know that |
| 8717 | * a NULL return value means a failure of allocation. */ |
| 8718 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8719 | } |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8720 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8721 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8722 | local_input->length = input_len; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8723 | |
| 8724 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8725 | local_input->buffer, local_input->length); |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8726 | if (status != PSA_SUCCESS) { |
| 8727 | goto error; |
| 8728 | } |
| 8729 | |
| 8730 | return PSA_SUCCESS; |
| 8731 | |
| 8732 | error: |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8733 | mbedtls_free(local_input->buffer); |
| 8734 | local_input->buffer = NULL; |
| 8735 | local_input->length = 0; |
David Horstmann | 4ac7885 | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 8736 | return status; |
| 8737 | } |
| 8738 | |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8739 | 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] | 8740 | { |
David Horstmann | f173405 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 8741 | mbedtls_free(local_input->buffer); |
| 8742 | local_input->buffer = NULL; |
| 8743 | local_input->length = 0; |
David Horstmann | e6042ff | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8746 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 8747 | psa_crypto_local_output_t *local_output) |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8748 | { |
David Horstmann | 9db1448 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 8749 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8750 | |
David Horstmann | c5cc1c3 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 8751 | if (output_len == 0) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8752 | return PSA_SUCCESS; |
| 8753 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8754 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 8755 | if (local_output->buffer == NULL) { |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8756 | /* Since we dealt with the zero-length case above, we know that |
| 8757 | * a NULL return value means a failure of allocation. */ |
| 8758 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8759 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8760 | local_output->length = output_len; |
| 8761 | local_output->original = output; |
David Horstmann | ba3c7d6 | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 8762 | |
| 8763 | return PSA_SUCCESS; |
| 8764 | } |
| 8765 | |
Gabor Mezei | 1882c51 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 8766 | psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len, |
| 8767 | psa_crypto_local_output_t *local_output) |
| 8768 | { |
| 8769 | psa_status_t status; |
| 8770 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
| 8771 | |
| 8772 | if (output_len == 0) { |
| 8773 | return PSA_SUCCESS; |
| 8774 | } |
| 8775 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 8776 | if (local_output->buffer == NULL) { |
| 8777 | /* Since we dealt with the zero-length case above, we know that |
| 8778 | * a NULL return value means a failure of allocation. */ |
| 8779 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 8780 | } |
| 8781 | local_output->length = output_len; |
| 8782 | local_output->original = output; |
| 8783 | |
| 8784 | status = psa_crypto_copy_input(output, output_len, |
| 8785 | local_output->buffer, local_output->length); |
| 8786 | if (status != PSA_SUCCESS) { |
| 8787 | goto error; |
| 8788 | } |
| 8789 | |
| 8790 | return PSA_SUCCESS; |
| 8791 | |
| 8792 | error: |
| 8793 | mbedtls_free(local_output->buffer); |
| 8794 | local_output->buffer = NULL; |
| 8795 | local_output->length = 0; |
| 8796 | return status; |
| 8797 | } |
| 8798 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8799 | 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] | 8800 | { |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 8801 | psa_status_t status; |
| 8802 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8803 | if (local_output->buffer == NULL) { |
| 8804 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8805 | return PSA_SUCCESS; |
| 8806 | } |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8807 | if (local_output->original == NULL) { |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8808 | /* We have an internal copy but nothing to copy back to. */ |
| 8809 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 8810 | } |
| 8811 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8812 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 8813 | local_output->original, local_output->length); |
David Horstmann | c335a4e | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 8814 | if (status != PSA_SUCCESS) { |
| 8815 | return status; |
| 8816 | } |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8817 | |
David Horstmann | 89875a4 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 8818 | mbedtls_free(local_output->buffer); |
| 8819 | local_output->buffer = NULL; |
| 8820 | local_output->length = 0; |
David Horstmann | 9467ea3 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 8821 | |
| 8822 | return PSA_SUCCESS; |
| 8823 | } |
| 8824 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 8825 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |