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 | 7ff7965 | 2023-11-03 12:04:52 +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" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 12 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 13 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 14 | #include "check_crypto_config.h" |
| 15 | #endif |
| 16 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 17 | #include "psa/crypto.h" |
| 18 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 19 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 20 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 21 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 22 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 23 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 24 | #include "psa_crypto_hash.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 25 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 26 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 27 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 29 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 30 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 31 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 32 | /* Include internal declarations that are useful for implementing persistently |
| 33 | * stored keys. */ |
| 34 | #include "psa_crypto_storage.h" |
| 35 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 36 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 37 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | #include "mbedtls/platform.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 41 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 42 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 43 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 44 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 45 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 46 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 47 | #include "mbedtls/blowfish.h" |
| 48 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 49 | #include "mbedtls/chacha20.h" |
| 50 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/cipher.h" |
| 52 | #include "mbedtls/ccm.h" |
| 53 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 54 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 55 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 56 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 57 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 58 | #include "mbedtls/error.h" |
| 59 | #include "mbedtls/gcm.h" |
| 60 | #include "mbedtls/md2.h" |
| 61 | #include "mbedtls/md4.h" |
| 62 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 63 | #include "mbedtls/md.h" |
| 64 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 65 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 67 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 69 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 70 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 71 | #include "mbedtls/sha1.h" |
| 72 | #include "mbedtls/sha256.h" |
| 73 | #include "mbedtls/sha512.h" |
| 74 | #include "mbedtls/xtea.h" |
| 75 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 77 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 78 | /****************************************************************/ |
| 79 | /* Global data, support functions and library management */ |
| 80 | /****************************************************************/ |
| 81 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | static int key_type_is_raw_bytes(psa_key_type_t type) |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 83 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 87 | /* Values for psa_global_data_t::rng_state */ |
| 88 | #define RNG_NOT_INITIALIZED 0 |
| 89 | #define RNG_INITIALIZED 1 |
| 90 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 91 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | typedef struct { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 93 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 94 | unsigned rng_state : 2; |
Gilles Peskine | b8006a6 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 95 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 96 | } psa_global_data_t; |
| 97 | |
| 98 | static psa_global_data_t global_data; |
| 99 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 100 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 101 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 102 | &global_data.rng.drbg; |
| 103 | #endif |
| 104 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 105 | #define GUARD_MODULE_INITIALIZED \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | if (global_data.initialized == 0) \ |
| 107 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 108 | |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 109 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 110 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 111 | /* Declare a local copy of an input buffer and a variable that will be used |
| 112 | * to store a pointer to the start of the buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 113 | * |
| 114 | * Note: This macro must be called before any operations which may jump to |
| 115 | * the exit label, so that the local input copy object is safe to be freed. |
| 116 | * |
| 117 | * Assumptions: |
| 118 | * - input is the name of a pointer to the buffer to be copied |
| 119 | * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 120 | * - input_copy_name is a name that is unused in the current scope |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 121 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 122 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 123 | psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \ |
| 124 | const uint8_t *input_copy_name = NULL; |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 125 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 126 | /* Allocate a copy of the buffer input and set the pointer input_copy to |
| 127 | * point to the start of the copy. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 128 | * |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 129 | * Assumptions: |
| 130 | * - psa_status_t status exists |
| 131 | * - An exit label is declared |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 132 | * - input is the name of a pointer to the buffer to be copied |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 133 | * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 134 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 135 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 136 | status = psa_crypto_local_input_alloc(input, length, \ |
| 137 | &LOCAL_INPUT_COPY_OF_##input); \ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 138 | if (status != PSA_SUCCESS) { \ |
| 139 | goto exit; \ |
| 140 | } \ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 141 | input_copy = LOCAL_INPUT_COPY_OF_##input.buffer; |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 142 | |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 143 | /* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC() |
| 144 | * |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 145 | * Assumptions: |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 146 | * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC() |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 147 | * - input is the name of the original buffer that was copied |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 148 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 149 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 150 | input_copy = NULL; \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 151 | psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input); |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 152 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 153 | /* Declare a local copy of an output buffer and a variable that will be used |
| 154 | * to store a pointer to the start of the buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 155 | * |
| 156 | * Note: This macro must be called before any operations which may jump to |
| 157 | * the exit label, so that the local output copy object is safe to be freed. |
| 158 | * |
| 159 | * Assumptions: |
| 160 | * - output is the name of a pointer to the buffer to be copied |
| 161 | * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 162 | * - output_copy_name is a name that is unused in the current scope |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 163 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 164 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 165 | psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \ |
| 166 | uint8_t *output_copy_name = NULL; |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 167 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 168 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 169 | * point to the start of the copy. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 170 | * |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 171 | * Assumptions: |
| 172 | * - psa_status_t status exists |
| 173 | * - An exit label is declared |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 174 | * - output is the name of a pointer to the buffer to be copied |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 175 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 176 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 177 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 178 | status = psa_crypto_local_output_alloc(output, length, \ |
| 179 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 180 | if (status != PSA_SUCCESS) { \ |
| 181 | goto exit; \ |
| 182 | } \ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 183 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 184 | |
Gabor Mezei | 3b0c371 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 185 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 186 | * point to the start of the copy. |
| 187 | * |
| 188 | * Assumptions: |
| 189 | * - psa_status_t status exists |
| 190 | * - An exit label is declared |
| 191 | * - output is the name of a pointer to the buffer to be copied |
| 192 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
| 193 | */ |
| 194 | #define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \ |
| 195 | status = psa_crypto_local_output_alloc_with_copy(output, length, \ |
| 196 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
| 197 | if (status != PSA_SUCCESS) { \ |
| 198 | goto exit; \ |
| 199 | } \ |
| 200 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
| 201 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 202 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 203 | * after first copying back its contents to the original buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 204 | * |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 205 | * Assumptions: |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 206 | * - psa_status_t status exists |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 207 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 208 | * - output is the name of the original buffer that was copied |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 209 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 210 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 211 | output_copy = NULL; \ |
David Horstmann | 2b70a66 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 212 | do { \ |
| 213 | psa_status_t local_output_status; \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 214 | local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | 2b70a66 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 215 | if (local_output_status != PSA_SUCCESS) { \ |
| 216 | /* Since this error case is an internal error, it's more serious than \ |
| 217 | * any existing error code and so it's fine to overwrite the existing \ |
| 218 | * status. */ \ |
| 219 | status = local_output_status; \ |
| 220 | } \ |
| 221 | } while (0) |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 222 | #else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 223 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 224 | const uint8_t *input_copy_name = NULL; |
| 225 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 226 | input_copy = input; |
| 227 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 228 | input_copy = NULL; |
| 229 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 230 | uint8_t *output_copy_name = NULL; |
| 231 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 232 | output_copy = output; |
Gabor Mezei | 69f680a | 2024-01-29 17:27:44 +0100 | [diff] [blame] | 233 | #define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \ |
| 234 | output_copy = output; |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 235 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 236 | output_copy = NULL; |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 237 | #endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 239 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 240 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 241 | /* Mbed TLS error codes can combine a high-level error code and a |
| 242 | * low-level error code. The low-level error usually reflects the |
| 243 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | int low_level_ret = -(-ret & 0x007f); |
| 245 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 246 | case 0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 248 | |
| 249 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 250 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 251 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 253 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 255 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 256 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 258 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 259 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 260 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 261 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 262 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 263 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 265 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 267 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 269 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 270 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 271 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 272 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 273 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 274 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 277 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 279 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 280 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 281 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 282 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 283 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 284 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 287 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 288 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 289 | |
| 290 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 292 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 294 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 297 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 299 | |
| 300 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 302 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 304 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 307 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 309 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 311 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 313 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 321 | |
| 322 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 326 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 327 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 328 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 329 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 331 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 332 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 334 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 336 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 337 | |
| 338 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 340 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 342 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 343 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 344 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 345 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 347 | |
| 348 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 350 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 352 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 354 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 355 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 357 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 358 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 359 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 361 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 362 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 364 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 366 | #endif |
| 367 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 368 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 369 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 370 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 371 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 372 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 373 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 375 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 379 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | return PSA_ERROR_STORAGE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 381 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 383 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 384 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 386 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 388 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 390 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 392 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 394 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 396 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 398 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 400 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 401 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 403 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 404 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 406 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 408 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 409 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 411 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 413 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 414 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 415 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 416 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 418 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 419 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 420 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 421 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 422 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 424 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 425 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 426 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 427 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 429 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 431 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 432 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 434 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 435 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 437 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 439 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 440 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 441 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 443 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 444 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 445 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 446 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 447 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 448 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 450 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 452 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 454 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 457 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 458 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 459 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 461 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 462 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 463 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 464 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 466 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 467 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 468 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 470 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 472 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 474 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 475 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 476 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 477 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 479 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 481 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 482 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 483 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 484 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 485 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 486 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 487 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 488 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 492 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 493 | |
| 494 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 495 | /****************************************************************/ |
| 496 | /* Key management */ |
| 497 | /****************************************************************/ |
| 498 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 499 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 500 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 501 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 502 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 503 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 505 | size_t bits, |
| 506 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 507 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 509 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 511 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 512 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 513 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 514 | #endif |
| 515 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 516 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 517 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 518 | #endif |
| 519 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 520 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 522 | #endif |
| 523 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 524 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 525 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 526 | #endif |
| 527 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 528 | case 521: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 530 | case 528: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 531 | if (bits_is_sloppy) { |
| 532 | return MBEDTLS_ECP_DP_SECP521R1; |
| 533 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 534 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 535 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 536 | } |
| 537 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 538 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 539 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 540 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 541 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 542 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 543 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 544 | #endif |
| 545 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 546 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 547 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 548 | #endif |
| 549 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 550 | case 512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 551 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 552 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 553 | } |
| 554 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 555 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 556 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 557 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 558 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 559 | case 255: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 560 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 561 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 562 | if (bits_is_sloppy) { |
| 563 | return MBEDTLS_ECP_DP_CURVE25519; |
| 564 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 565 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 566 | #endif |
| 567 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 568 | case 448: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 569 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 570 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 571 | } |
| 572 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 573 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 574 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 575 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 576 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 577 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 578 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 579 | #endif |
| 580 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 581 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 583 | #endif |
| 584 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 585 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 586 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 587 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 588 | } |
| 589 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 590 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 591 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 592 | (void) bits_is_sloppy; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 593 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 594 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 595 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 596 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 597 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 598 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 599 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 601 | static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type, |
| 602 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 603 | { |
| 604 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 605 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 606 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 607 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 608 | case PSA_KEY_TYPE_DERIVE: |
| 609 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 610 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 611 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 612 | if (bits != 128 && bits != 192 && bits != 256) { |
| 613 | return PSA_ERROR_INVALID_ARGUMENT; |
| 614 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 615 | break; |
| 616 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 617 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 618 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 619 | if (bits != 128 && bits != 192 && bits != 256) { |
| 620 | return PSA_ERROR_INVALID_ARGUMENT; |
| 621 | } |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 622 | break; |
| 623 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 624 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 625 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 626 | if (bits != 128 && bits != 192 && bits != 256) { |
| 627 | return PSA_ERROR_INVALID_ARGUMENT; |
| 628 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 629 | break; |
| 630 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 631 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 632 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 633 | if (bits != 64 && bits != 128 && bits != 192) { |
| 634 | return PSA_ERROR_INVALID_ARGUMENT; |
| 635 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 636 | break; |
| 637 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 638 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 639 | case PSA_KEY_TYPE_ARC4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 640 | if (bits < 8 || bits > 2048) { |
| 641 | return PSA_ERROR_INVALID_ARGUMENT; |
| 642 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 643 | break; |
| 644 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 645 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 646 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 647 | if (bits != 256) { |
| 648 | return PSA_ERROR_INVALID_ARGUMENT; |
| 649 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 650 | break; |
| 651 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 652 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 653 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 654 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 655 | if (bits % 8 != 0) { |
| 656 | return PSA_ERROR_INVALID_ARGUMENT; |
| 657 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 658 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 659 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 662 | /** 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] | 663 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 664 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 665 | * 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] | 666 | * |
| 667 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 668 | * \param[in] key_type The key type of the key to be used with the |
| 669 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 670 | * |
| 671 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 672 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 673 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 674 | * 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] | 675 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 676 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 677 | psa_algorithm_t algorithm, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 678 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 679 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 680 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 681 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 682 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 683 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 684 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 685 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 686 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 687 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 688 | * key. */ |
| 689 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 690 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 691 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 692 | * the block length (larger than 1) for block ciphers. */ |
| 693 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 694 | return PSA_SUCCESS; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 702 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 703 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 704 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 705 | if (slot->key.data != NULL) { |
| 706 | return PSA_ERROR_ALREADY_EXISTS; |
| 707 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 708 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 710 | if (slot->key.data == NULL) { |
| 711 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 712 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 713 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 714 | slot->key.bytes = buffer_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 718 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 719 | const uint8_t *data, |
| 720 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 721 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 722 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 723 | data_length); |
| 724 | if (status != PSA_SUCCESS) { |
| 725 | return status; |
| 726 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 727 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 728 | memcpy(slot->key.data, data, data_length); |
| 729 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 732 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 733 | const psa_key_attributes_t *attributes, |
| 734 | const uint8_t *data, size_t data_length, |
| 735 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 736 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 737 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 738 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 739 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 740 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 741 | /* zero-length keys are never supported. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 742 | if (data_length == 0) { |
| 743 | return PSA_ERROR_NOT_SUPPORTED; |
| 744 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 745 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 746 | if (key_type_is_raw_bytes(type)) { |
| 747 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 748 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 749 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 750 | if (data_length > SIZE_MAX / 8) { |
| 751 | return PSA_ERROR_NOT_SUPPORTED; |
| 752 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 753 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 754 | /* Enforce a size limit, and in particular ensure that the bit |
| 755 | * size fits in its representation type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | if ((*bits) > PSA_MAX_KEY_BITS) { |
| 757 | return PSA_ERROR_NOT_SUPPORTED; |
| 758 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 759 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 760 | status = validate_unstructured_key_bit_size(type, *bits); |
| 761 | if (status != PSA_SUCCESS) { |
| 762 | return status; |
| 763 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 764 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 765 | /* Copy the key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 766 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 767 | *key_buffer_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 769 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 770 | return PSA_SUCCESS; |
| 771 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 772 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 773 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 774 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 775 | return mbedtls_psa_ecp_import_key(attributes, |
| 776 | data, data_length, |
| 777 | key_buffer, key_buffer_size, |
| 778 | key_buffer_length, |
| 779 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 780 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 781 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 782 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 783 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 784 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 785 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 786 | return mbedtls_psa_rsa_import_key(attributes, |
| 787 | data, data_length, |
| 788 | key_buffer, key_buffer_size, |
| 789 | key_buffer_length, |
| 790 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 791 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 792 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 793 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 794 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 795 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 796 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 799 | /** Calculate the intersection of two algorithm usage policies. |
| 800 | * |
| 801 | * Return 0 (which allows no operation) on incompatibility. |
| 802 | */ |
| 803 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 804 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 805 | psa_algorithm_t alg1, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 806 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 807 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 808 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 809 | if (alg1 == alg2) { |
| 810 | return alg1; |
| 811 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 812 | /* If the policies are from the same hash-and-sign family, check |
| 813 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 814 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 815 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 816 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 817 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 818 | return alg2; |
| 819 | } |
| 820 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 821 | return alg1; |
| 822 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 823 | } |
| 824 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 825 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 826 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 827 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 828 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 829 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 830 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 831 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 832 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 833 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 834 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 835 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 836 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 837 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 838 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 839 | } |
| 840 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 841 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 842 | (alg1_len <= alg2_len)) { |
| 843 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 844 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 845 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 846 | (alg2_len <= alg1_len)) { |
| 847 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | /* If the policies are from the same MAC family, check whether one |
| 851 | * of them is a minimum-MAC-length policy. Calculate the most |
| 852 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 853 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 854 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 855 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 856 | /* Validate the combination of key type and algorithm. Since the base |
| 857 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 858 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 859 | return 0; |
| 860 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 861 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 862 | /* Get the (exact or at-least) output lengths for both sides of the |
| 863 | * requested intersection. None of the currently supported algorithms |
| 864 | * have an output length dependent on the actual key size, so setting it |
| 865 | * to a bogus value of 0 is currently OK. |
| 866 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 867 | * Note that for at-least-this-length wildcard algorithms, the output |
| 868 | * length is set to the shortest allowed length, which allows us to |
| 869 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 870 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 871 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 872 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 873 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 874 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 875 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 876 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 877 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 878 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 879 | |
| 880 | /* If only one is an at-least-this-length policy, the intersection would |
| 881 | * be the other (fixed-length) policy as long as said fixed length is |
| 882 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 883 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 884 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 885 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 886 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 887 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 888 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 889 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 890 | /* If none of them are wildcards, check whether they define the same tag |
| 891 | * length. This is still possible here when one is default-length and |
| 892 | * the other specific-length. Ensure to always return the |
| 893 | * specific-length version for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 894 | if (alg1_len == alg2_len) { |
| 895 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 896 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 897 | } |
| 898 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 899 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 902 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 903 | psa_algorithm_t policy_alg, |
| 904 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 905 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 906 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 907 | if (requested_alg == policy_alg) { |
| 908 | return 1; |
| 909 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 910 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 911 | * and requested_alg is the same hash-and-sign family with any hash, |
| 912 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 913 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 914 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 915 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 916 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 917 | } |
| 918 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 919 | * the requested algorithm, check the requested tag length to be |
| 920 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 921 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 922 | PSA_ALG_IS_AEAD(requested_alg) && |
| 923 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 924 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 925 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 926 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 927 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 928 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 929 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 930 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 931 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 932 | PSA_ALG_IS_MAC(requested_alg) && |
| 933 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 934 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 935 | /* Validate the combination of key type and algorithm. Since the policy |
| 936 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 937 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 938 | return 0; |
| 939 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 940 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 941 | /* Get both the requested output length for the algorithm which is to be |
| 942 | * verified, and the default output length for the base algorithm. |
| 943 | * Note that none of the currently supported algorithms have an output |
| 944 | * length dependent on actual key size, so setting it to a bogus value |
| 945 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 946 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 947 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 948 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 949 | key_type, 0, |
| 950 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 951 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 952 | /* If the policy is default-length, only allow an algorithm with |
| 953 | * a declared exact-length matching the default. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 954 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 955 | return requested_output_length == default_output_length; |
| 956 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 957 | |
| 958 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 959 | * length exactly matches the default length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 960 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 961 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 962 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 965 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 966 | * check for the requested MAC length to be equal to or longer than the |
| 967 | * minimum allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 968 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 969 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 970 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 971 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 972 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 973 | /* 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] | 974 | * a key derivation with that key agreement should also be allowed. This |
| 975 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 976 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 977 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 978 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 979 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 980 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 981 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 982 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 985 | /** Test whether a policy permits an algorithm. |
| 986 | * |
| 987 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 988 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 989 | * \note This function requires providing the key type for which the policy is |
| 990 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 991 | * have different properties depending on what kind of cipher it is |
| 992 | * combined with. |
| 993 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 994 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 995 | * allowed by the \p policy. |
| 996 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 997 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 998 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 999 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1000 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 1001 | psa_key_type_t key_type, |
| 1002 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1003 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1004 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1005 | if (alg == 0) { |
| 1006 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1007 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1008 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1009 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1010 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 1011 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1012 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1013 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1014 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 1015 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 1016 | return PSA_SUCCESS; |
| 1017 | } else { |
| 1018 | return PSA_ERROR_NOT_PERMITTED; |
| 1019 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1022 | /** Restrict a key policy based on a constraint. |
| 1023 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 1024 | * \note This function requires providing the key type for which the policy is |
| 1025 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 1026 | * have different properties depending on what kind of cipher it is |
| 1027 | * combined with. |
| 1028 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1029 | * \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] | 1030 | * \param[in,out] policy The policy to restrict. |
| 1031 | * \param[in] constraint The policy constraint to apply. |
| 1032 | * |
| 1033 | * \retval #PSA_SUCCESS |
| 1034 | * \c *policy contains the intersection of the original value of |
| 1035 | * \c *policy and \c *constraint. |
| 1036 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1037 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1038 | * \c *policy is unchanged. |
| 1039 | */ |
| 1040 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1041 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1042 | psa_key_policy_t *policy, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1043 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1044 | { |
| 1045 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1046 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 1047 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1048 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1049 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 1050 | constraint->alg2); |
| 1051 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1052 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1053 | } |
| 1054 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1055 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1056 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1057 | policy->usage &= constraint->usage; |
| 1058 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1059 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1060 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1063 | /** Get the description of a key given its identifier and policy constraints |
| 1064 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1065 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1066 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1067 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1068 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1069 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1070 | * In case of a persistent key, the function loads the description of the key |
| 1071 | * into a key slot if not already done. |
| 1072 | * |
| 1073 | * On success, the returned key slot is locked. It is the responsibility of |
| 1074 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1075 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1076 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1077 | mbedtls_svc_key_id_t key, |
| 1078 | psa_key_slot_t **p_slot, |
| 1079 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1080 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1081 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1082 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1083 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1084 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1085 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1086 | if (status != PSA_SUCCESS) { |
| 1087 | return status; |
| 1088 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1089 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1090 | |
| 1091 | /* Enforce that usage policy for the key slot contains all the flags |
| 1092 | * required by the usage parameter. There is one exception: public |
| 1093 | * keys can always be exported, so we treat public key objects as |
| 1094 | * if they had the export flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1096 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1097 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1098 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1099 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1100 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1101 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1102 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1103 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1104 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1105 | if (alg != 0) { |
| 1106 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1107 | slot->attr.type, |
| 1108 | alg); |
| 1109 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1110 | goto error; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1111 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1112 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1114 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1115 | |
| 1116 | error: |
| 1117 | *p_slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1118 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1120 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1121 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1122 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1123 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1124 | * |
| 1125 | * A transparent key is a key for which the key material is directly |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1126 | * available, as opposed to a key in a secure element and/or to be used |
| 1127 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1128 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1129 | * This is a temporary function that may be used instead of |
| 1130 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1131 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1132 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1133 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1134 | * 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] | 1135 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1136 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1137 | mbedtls_svc_key_id_t key, |
| 1138 | psa_key_slot_t **p_slot, |
| 1139 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1140 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1141 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1142 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1143 | usage, alg); |
| 1144 | if (status != PSA_SUCCESS) { |
| 1145 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1146 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1147 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1148 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1149 | psa_unlock_key_slot(*p_slot); |
| 1150 | *p_slot = NULL; |
| 1151 | return PSA_ERROR_NOT_SUPPORTED; |
| 1152 | } |
| 1153 | |
| 1154 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1155 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1157 | 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] | 1158 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1159 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1160 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1161 | if (slot->key.data != NULL) { |
| 1162 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1163 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1164 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1165 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1166 | slot->key.data = NULL; |
| 1167 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1169 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1172 | /** Completely wipe a slot in memory, including its policy. |
| 1173 | * Persistent storage is not affected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1174 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1175 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1176 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1177 | |
| 1178 | /* |
| 1179 | * As the return error code may not be handled in case of multiple errors, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1180 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1181 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1182 | * part of the execution of a test suite this will stop the test suite |
Ronald Cron | 4640c15 | 2020-11-13 10:11:01 +0100 | [diff] [blame] | 1183 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1184 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1185 | if (slot->lock_count != 1) { |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1186 | #ifdef MBEDTLS_CHECK_PARAMS |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1187 | MBEDTLS_PARAM_FAILED(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1188 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1189 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1190 | } |
| 1191 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1192 | /* Multipart operations may still be using the key. This is safe |
| 1193 | * because all multipart operation objects are independent from |
| 1194 | * the key slot: if they need to access the key after the setup |
| 1195 | * phase, they have a copy of the key. Note that this means that |
| 1196 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1197 | /* At this point, key material and other type-specific content has |
| 1198 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1199 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1200 | memset(slot, 0, sizeof(*slot)); |
| 1201 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1204 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1205 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1206 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1207 | psa_status_t status; /* status of the last operation */ |
| 1208 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1209 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1210 | psa_se_drv_table_entry_t *driver; |
| 1211 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1213 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1214 | return PSA_SUCCESS; |
| 1215 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1216 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1217 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1218 | * 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] | 1219 | * key, this will load the key description from persistent memory if not |
| 1220 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1221 | * the key is operated by an SE or not and this information is needed by |
| 1222 | * the current implementation. |
| 1223 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1224 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1225 | if (status != PSA_SUCCESS) { |
| 1226 | return status; |
| 1227 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1228 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1229 | /* |
| 1230 | * If the key slot containing the key description is under access by the |
| 1231 | * library (apart from the present access), the key cannot be destroyed |
| 1232 | * yet. For the time being, just return in error. Eventually (to be |
| 1233 | * implemented), the key should be destroyed when all accesses have |
| 1234 | * stopped. |
| 1235 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1236 | if (slot->lock_count > 1) { |
| 1237 | psa_unlock_key_slot(slot); |
| 1238 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1241 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1242 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1243 | * if we attempt it, depending on whether the key is merely read-only |
| 1244 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1245 | * Just do the best we can, which is to wipe the copy in memory |
| 1246 | * (done in this function's cleanup code). */ |
| 1247 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1248 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1251 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1252 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1253 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1254 | /* For a key in a secure element, we need to do three things: |
| 1255 | * remove the key file in internal storage, destroy the |
| 1256 | * key inside the secure element, and update the driver's |
| 1257 | * persistent data. Start a transaction that will encompass these |
| 1258 | * three actions. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1259 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1260 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1261 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1262 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1263 | status = psa_crypto_save_transaction(); |
| 1264 | if (status != PSA_SUCCESS) { |
| 1265 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1266 | /* We should still try to destroy the key in the secure |
| 1267 | * element and the key metadata in storage. This is especially |
| 1268 | * important if the error is that the storage is full. |
| 1269 | * But how to do it exactly without risking an inconsistent |
| 1270 | * state after a reset? |
| 1271 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1272 | */ |
| 1273 | overall_status = status; |
| 1274 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1277 | status = psa_destroy_se_key(driver, |
| 1278 | psa_key_slot_get_slot_number(slot)); |
| 1279 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1280 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1281 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1282 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1283 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1284 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1285 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1286 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1287 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1288 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1289 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1290 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1291 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1292 | /* TODO: other slots may have a copy of the same key. We should |
| 1293 | * invalidate them. |
| 1294 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1295 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1296 | } |
| 1297 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1298 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1300 | if (driver != NULL) { |
| 1301 | status = psa_save_se_persistent_data(driver); |
| 1302 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1303 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1304 | } |
| 1305 | status = psa_crypto_stop_transaction(); |
| 1306 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1307 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1308 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1309 | } |
| 1310 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1311 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1312 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1313 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1314 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1315 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1316 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1317 | } |
| 1318 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1321 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1322 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1323 | static psa_status_t psa_get_rsa_public_exponent( |
| 1324 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1325 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1326 | { |
| 1327 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1328 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1329 | uint8_t *buffer = NULL; |
| 1330 | size_t buflen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1331 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1332 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1333 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1334 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1335 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1336 | } |
| 1337 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1338 | /* It's the default value, which is reported as an empty string, |
| 1339 | * so there's nothing to do. */ |
| 1340 | goto exit; |
| 1341 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1342 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1343 | buflen = mbedtls_mpi_size(&mpi); |
| 1344 | buffer = mbedtls_calloc(1, buflen); |
| 1345 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1346 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1347 | goto exit; |
| 1348 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1349 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1350 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1351 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1352 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1353 | attributes->domain_parameters = buffer; |
| 1354 | attributes->domain_parameters_size = buflen; |
| 1355 | |
| 1356 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1357 | mbedtls_mpi_free(&mpi); |
| 1358 | if (ret != 0) { |
| 1359 | mbedtls_free(buffer); |
| 1360 | } |
| 1361 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1362 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1363 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1364 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1365 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1366 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1367 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1368 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1369 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1370 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1371 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1372 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1373 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1375 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1377 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1378 | if (status != PSA_SUCCESS) { |
| 1379 | return status; |
| 1380 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1381 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1382 | attributes->core = slot->attr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1383 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1384 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1385 | |
| 1386 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1387 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1388 | psa_set_key_slot_number(attributes, |
| 1389 | psa_key_slot_get_slot_number(slot)); |
| 1390 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1391 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1393 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1394 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1395 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1396 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1397 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1398 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1399 | * is not yet implemented. |
| 1400 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1401 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1402 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1403 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1404 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1405 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1406 | slot->attr.type, |
| 1407 | slot->key.data, |
| 1408 | slot->key.bytes, |
| 1409 | &rsa); |
| 1410 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1411 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1412 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1414 | status = psa_get_rsa_public_exponent(rsa, |
| 1415 | attributes); |
| 1416 | mbedtls_rsa_free(rsa); |
| 1417 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1418 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1419 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1420 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1421 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1422 | default: |
| 1423 | /* Nothing else to do. */ |
| 1424 | break; |
| 1425 | } |
| 1426 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1427 | if (status != PSA_SUCCESS) { |
| 1428 | psa_reset_key_attributes(attributes); |
| 1429 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1433 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1436 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1437 | psa_status_t psa_get_key_slot_number( |
| 1438 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1439 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1440 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1441 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1442 | *slot_number = attributes->slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1443 | return PSA_SUCCESS; |
| 1444 | } else { |
| 1445 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1446 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1447 | } |
| 1448 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1450 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1451 | size_t key_buffer_size, |
| 1452 | uint8_t *data, |
| 1453 | size_t data_size, |
| 1454 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1455 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1456 | if (key_buffer_size > data_size) { |
| 1457 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1458 | } |
| 1459 | memcpy(data, key_buffer, key_buffer_size); |
| 1460 | memset(data + key_buffer_size, 0, |
| 1461 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1462 | *data_length = key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1466 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1467 | const psa_key_attributes_t *attributes, |
| 1468 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1469 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1470 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1471 | psa_key_type_t type = attributes->core.type; |
| 1472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1473 | if (key_type_is_raw_bytes(type) || |
| 1474 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1475 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1476 | return psa_export_key_buffer_internal( |
| 1477 | key_buffer, key_buffer_size, |
| 1478 | data, data_size, data_length); |
| 1479 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1480 | /* This shouldn't happen in the reference implementation, but |
| 1481 | it is valid for a special-purpose implementation to omit |
| 1482 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1483 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1487 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1488 | uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1489 | size_t data_size, |
| 1490 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1491 | { |
| 1492 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1493 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1494 | psa_key_slot_t *slot; |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1495 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1496 | /* Reject a zero-length output buffer now, since this can never be a |
| 1497 | * valid key representation. This way we know that data must be a valid |
| 1498 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1499 | if (data_size == 0) { |
| 1500 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1501 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1502 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1503 | /* Set the key to empty now, so that even when there are errors, we always |
| 1504 | * set data_length to a value between 0 and data_size. On error, setting |
| 1505 | * the key to empty is a good choice because an empty key representation is |
| 1506 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1507 | *data_length = 0; |
| 1508 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1509 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1510 | * which don't require any flag, but |
| 1511 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1512 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1513 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1514 | PSA_KEY_USAGE_EXPORT, 0); |
| 1515 | if (status != PSA_SUCCESS) { |
| 1516 | return status; |
| 1517 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1518 | |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1519 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1520 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1521 | psa_key_attributes_t attributes = { |
| 1522 | .core = slot->attr |
| 1523 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1524 | status = psa_driver_wrapper_export_key(&attributes, |
| 1525 | slot->key.data, slot->key.bytes, |
| 1526 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1527 | |
Ryan Everett | 3a4153a | 2024-01-25 12:04:55 +0000 | [diff] [blame] | 1528 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1529 | exit: |
Ryan Everett | 3a4153a | 2024-01-25 12:04:55 +0000 | [diff] [blame] | 1530 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1532 | |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1533 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1534 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1535 | } |
| 1536 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1537 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1538 | const psa_key_attributes_t *attributes, |
| 1539 | const uint8_t *key_buffer, |
| 1540 | size_t key_buffer_size, |
| 1541 | uint8_t *data, |
| 1542 | size_t data_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1543 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1544 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1545 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1547 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1548 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1549 | /* Exporting public -> public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1550 | return psa_export_key_buffer_internal( |
| 1551 | key_buffer, key_buffer_size, |
| 1552 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1553 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1555 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1556 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1557 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1558 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1559 | key_buffer, |
| 1560 | key_buffer_size, |
| 1561 | data, |
| 1562 | data_size, |
| 1563 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1564 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1565 | /* We don't know how to convert a private RSA key to public. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1566 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1567 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1568 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1569 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1570 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1571 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1572 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1573 | key_buffer, |
| 1574 | key_buffer_size, |
| 1575 | data, |
| 1576 | data_size, |
| 1577 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1578 | #else |
| 1579 | /* We don't know how to convert a private ECC key to public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1580 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1581 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1582 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1583 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1584 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1585 | /* This shouldn't happen in the reference implementation, but |
| 1586 | it is valid for a special-purpose implementation to omit |
| 1587 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1588 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1589 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1592 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1593 | uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1594 | size_t data_size, |
| 1595 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1596 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1597 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1598 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1599 | psa_key_attributes_t attributes; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1600 | psa_key_slot_t *slot; |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1601 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1602 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1603 | /* Reject a zero-length output buffer now, since this can never be a |
| 1604 | * valid key representation. This way we know that data must be a valid |
| 1605 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1606 | if (data_size == 0) { |
| 1607 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1608 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1609 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1610 | /* Set the key to empty now, so that even when there are errors, we always |
| 1611 | * set data_length to a value between 0 and data_size. On error, setting |
| 1612 | * the key to empty is a good choice because an empty key representation is |
| 1613 | * unlikely to be accepted anywhere. */ |
| 1614 | *data_length = 0; |
| 1615 | |
| 1616 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1617 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1618 | if (status != PSA_SUCCESS) { |
| 1619 | return status; |
| 1620 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1621 | |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1622 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1623 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1624 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1625 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1626 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1629 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1630 | .core = slot->attr |
| 1631 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1632 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1633 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1634 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1635 | |
| 1636 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1637 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1638 | |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1639 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1640 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1641 | } |
| 1642 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1643 | MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1644 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1645 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1646 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1647 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1648 | "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] | 1649 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1650 | /** Validate that a key policy is internally well-formed. |
| 1651 | * |
| 1652 | * This function only rejects invalid policies. It does not validate the |
| 1653 | * consistency of the policy with respect to other attributes of the key |
| 1654 | * such as the key type. |
| 1655 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1656 | 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] | 1657 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1658 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1659 | PSA_KEY_USAGE_COPY | |
| 1660 | PSA_KEY_USAGE_ENCRYPT | |
| 1661 | PSA_KEY_USAGE_DECRYPT | |
| 1662 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1663 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1664 | PSA_KEY_USAGE_SIGN_HASH | |
| 1665 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1666 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1667 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1668 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1669 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1670 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1673 | /** Validate the internal consistency of key attributes. |
| 1674 | * |
| 1675 | * This function only rejects invalid attribute values. If does not |
| 1676 | * validate the consistency of the attributes with any key data that may |
| 1677 | * be involved in the creation of the key. |
| 1678 | * |
| 1679 | * Call this function early in the key creation process. |
| 1680 | * |
| 1681 | * \param[in] attributes Key attributes for the new key. |
| 1682 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1683 | * NULL for a transparent key. |
| 1684 | * |
| 1685 | */ |
| 1686 | static psa_status_t psa_validate_key_attributes( |
| 1687 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1688 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1689 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1690 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1691 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1692 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1693 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1694 | status = psa_validate_key_location(lifetime, p_drv); |
| 1695 | if (status != PSA_SUCCESS) { |
| 1696 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1699 | status = psa_validate_key_persistence(lifetime); |
| 1700 | if (status != PSA_SUCCESS) { |
| 1701 | return status; |
| 1702 | } |
| 1703 | |
| 1704 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1705 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1706 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1707 | } |
| 1708 | } else { |
| 1709 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1710 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1715 | if (status != PSA_SUCCESS) { |
| 1716 | return status; |
| 1717 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1718 | |
| 1719 | /* Refuse to create overly large keys. |
| 1720 | * Note that this doesn't trigger on import if the attributes don't |
| 1721 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1722 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1723 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1724 | return PSA_ERROR_NOT_SUPPORTED; |
| 1725 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1726 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1727 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1728 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1729 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1730 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1731 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1732 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1733 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1736 | /** Prepare a key slot to receive key material. |
| 1737 | * |
| 1738 | * This function allocates a key slot and sets its metadata. |
| 1739 | * |
| 1740 | * If this function fails, call psa_fail_key_creation(). |
| 1741 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1742 | * This function is intended to be used as follows: |
| 1743 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1744 | * it with the specified attributes, and in case of a volatile key assign it |
| 1745 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1746 | * -# Populate the slot with the key material. |
| 1747 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1748 | * In case of failure at any step, stop the sequence and call |
| 1749 | * psa_fail_key_creation(). |
| 1750 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1751 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1752 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1753 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1754 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1755 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1756 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1757 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1758 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1759 | * |
| 1760 | * \retval #PSA_SUCCESS |
| 1761 | * The key slot is ready to receive key material. |
| 1762 | * \return If this function fails, the key slot is an invalid state. |
| 1763 | * 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] | 1764 | */ |
| 1765 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1766 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1767 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1768 | psa_key_slot_t **p_slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1769 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | { |
| 1771 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1772 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1773 | psa_key_slot_t *slot; |
| 1774 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1775 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1776 | *p_drv = NULL; |
| 1777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1778 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1779 | if (status != PSA_SUCCESS) { |
| 1780 | return status; |
| 1781 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1783 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1784 | if (status != PSA_SUCCESS) { |
| 1785 | return status; |
| 1786 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1787 | slot = *p_slot; |
| 1788 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1789 | /* We're storing the declared bit-size of the key. It's up to each |
| 1790 | * creation mechanism to verify that this information is correct. |
| 1791 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1792 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1793 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1794 | * volatile key identifier associated to the slot returned to contain its |
| 1795 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1796 | |
| 1797 | slot->attr = attributes->core; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1798 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1799 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1800 | slot->attr.id = volatile_key_id; |
| 1801 | #else |
| 1802 | slot->attr.id.key_id = volatile_key_id; |
| 1803 | #endif |
| 1804 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1805 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1806 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1807 | * external-only flags, query `attributes`. Thanks to the check |
| 1808 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1809 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1810 | * may have set. */ |
| 1811 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1812 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1813 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1814 | /* 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] | 1815 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1816 | * create the key file in internal storage, create the |
| 1817 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1818 | * persistent data. This is done by starting a transaction that will |
| 1819 | * encompass these three actions. |
| 1820 | * For registering a volatile key, we just need to find an appropriate |
| 1821 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1822 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1823 | /* The first thing to do is to find a slot number for the new key. |
| 1824 | * We save the slot number in persistent storage as part of the |
| 1825 | * transaction data. It will be needed to recover if the power |
| 1826 | * fails during the key creation process, to clean up on the secure |
| 1827 | * element side after restarting. Obtaining a slot number from the |
| 1828 | * secure element driver updates its persistent state, but we do not yet |
| 1829 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1830 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1831 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1832 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1833 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1834 | &slot_number); |
| 1835 | if (status != PSA_SUCCESS) { |
| 1836 | return status; |
| 1837 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1838 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1839 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1840 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1841 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1842 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1843 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1844 | status = psa_crypto_save_transaction(); |
| 1845 | if (status != PSA_SUCCESS) { |
| 1846 | (void) psa_crypto_stop_transaction(); |
| 1847 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1848 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1849 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1850 | |
| 1851 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1852 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1853 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1854 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1855 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1856 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1857 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1858 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1859 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1860 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1861 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1862 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1863 | |
| 1864 | /** Finalize the creation of a key once its key material has been set. |
| 1865 | * |
| 1866 | * This entails writing the key to persistent storage. |
| 1867 | * |
| 1868 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1869 | * See the documentation of psa_start_key_creation() for the intended use |
| 1870 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1871 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1872 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1873 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1874 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1875 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1876 | * \param[in,out] slot Pointer to the slot with key material. |
| 1877 | * \param[in] driver The secure element driver for the key, |
| 1878 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1879 | * \param[out] key On success, identifier of the key. Note that the |
| 1880 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1881 | * |
| 1882 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1883 | * The key was successfully created. |
Gilles Peskine | ec1eff3 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1884 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1885 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1886 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1887 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1888 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1889 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1890 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1891 | * \return If this function fails, the key slot is an invalid state. |
| 1892 | * 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] | 1893 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1894 | static psa_status_t psa_finish_key_creation( |
| 1895 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1896 | psa_se_drv_table_entry_t *driver, |
| 1897 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1898 | { |
| 1899 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1900 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1901 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1902 | |
| 1903 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1904 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1905 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1906 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1907 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1908 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1909 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1910 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1911 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1912 | sizeof(data.slot_number), |
| 1913 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1914 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1915 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1916 | status = psa_save_persistent_key(&slot->attr, |
| 1917 | (uint8_t *) &data, |
| 1918 | sizeof(data)); |
| 1919 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1920 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1921 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1922 | /* Key material is saved in export representation in the slot, so |
| 1923 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1924 | status = psa_save_persistent_key(&slot->attr, |
| 1925 | slot->key.data, |
| 1926 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1927 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1928 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1929 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1930 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1931 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1932 | /* Finish the transaction for a key creation. This does not |
| 1933 | * happen when registering an existing key. Detect this case |
| 1934 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1935 | * creation of a persistent key in a secure element requires a transaction, |
| 1936 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1937 | if (driver != NULL && |
| 1938 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1939 | status = psa_save_se_persistent_data(driver); |
| 1940 | if (status != PSA_SUCCESS) { |
| 1941 | psa_destroy_persistent_key(slot->attr.id); |
| 1942 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1943 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1944 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1945 | } |
| 1946 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1948 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1949 | *key = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1950 | status = psa_unlock_key_slot(slot); |
| 1951 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1952 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1953 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1954 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1955 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1956 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | /** Abort the creation of a key. |
| 1960 | * |
| 1961 | * You may call this function after calling psa_start_key_creation(), |
| 1962 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1963 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1964 | * See the documentation of psa_start_key_creation() for the intended use |
| 1965 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1966 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1967 | * \param[in,out] slot Pointer to the slot with key material. |
| 1968 | * \param[in] driver The secure element driver for the key, |
| 1969 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1970 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1971 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1972 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1973 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1974 | (void) driver; |
| 1975 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1976 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1977 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1978 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1979 | |
| 1980 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1981 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1982 | * element, and the failure happened later (when saving metadata |
| 1983 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1984 | * element. |
| 1985 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1986 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1987 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1988 | /* Abort the ongoing transaction if any (there may not be one if |
| 1989 | * the creation process failed before starting one, or if the |
| 1990 | * key creation is a registration of a key in a secure element). |
| 1991 | * Earlier functions must already have done what it takes to undo any |
| 1992 | * partial creation. All that's left is to update the transaction data |
| 1993 | * itself. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1994 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1995 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1997 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2000 | /** Validate optional attributes during key creation. |
| 2001 | * |
| 2002 | * Some key attributes are optional during key creation. If they are |
| 2003 | * specified in the attributes structure, check that they are consistent |
| 2004 | * with the data in the slot. |
| 2005 | * |
| 2006 | * This function should be called near the end of key creation, after |
| 2007 | * the slot in memory is fully populated but before saving persistent data. |
| 2008 | */ |
| 2009 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2010 | const psa_key_slot_t *slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2011 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2012 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2013 | if (attributes->core.type != 0) { |
| 2014 | if (attributes->core.type != slot->attr.type) { |
| 2015 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2016 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2019 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2020 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2021 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2022 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2023 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2024 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2025 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2026 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2027 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | slot->attr.type, |
| 2029 | slot->key.data, |
| 2030 | slot->key.bytes, |
| 2031 | &rsa); |
| 2032 | if (status != PSA_SUCCESS) { |
| 2033 | return status; |
| 2034 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2035 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2036 | mbedtls_mpi_init(&actual); |
| 2037 | mbedtls_mpi_init(&required); |
| 2038 | ret = mbedtls_rsa_export(rsa, |
| 2039 | NULL, NULL, NULL, NULL, &actual); |
| 2040 | mbedtls_rsa_free(rsa); |
| 2041 | mbedtls_free(rsa); |
| 2042 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2043 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2044 | } |
| 2045 | ret = mbedtls_mpi_read_binary(&required, |
| 2046 | attributes->domain_parameters, |
| 2047 | attributes->domain_parameters_size); |
| 2048 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2049 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2050 | } |
| 2051 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2052 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2053 | } |
| 2054 | rsa_exit: |
| 2055 | mbedtls_mpi_free(&actual); |
| 2056 | mbedtls_mpi_free(&required); |
| 2057 | if (ret != 0) { |
| 2058 | return mbedtls_to_psa_error(ret); |
| 2059 | } |
| 2060 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2061 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2062 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2063 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2064 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2068 | if (attributes->core.bits != 0) { |
| 2069 | if (attributes->core.bits != slot->attr.bits) { |
| 2070 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2071 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2074 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2077 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2078 | const uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2079 | size_t data_length, |
| 2080 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2081 | { |
| 2082 | psa_status_t status; |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2083 | LOCAL_INPUT_DECLARE(data_external, data); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2084 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2085 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2086 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2087 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2088 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2089 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2090 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2091 | * This also rejects any key which might be encoded as an empty string, |
| 2092 | * which is never valid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2093 | if (data_length == 0) { |
| 2094 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2095 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2096 | |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2097 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2098 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2099 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2100 | &slot, &driver); |
| 2101 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2102 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2103 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2104 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2105 | /* In the case of a transparent key or an opaque key stored in local |
| 2106 | * storage (thus not in the case of generating a key in a secure element |
| 2107 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 2108 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2109 | if (slot->key.data == NULL) { |
| 2110 | status = psa_allocate_buffer_to_slot(slot, data_length); |
| 2111 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2112 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2113 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2114 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2115 | |
| 2116 | bits = slot->attr.bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2117 | status = psa_driver_wrapper_import_key(attributes, |
| 2118 | data, data_length, |
| 2119 | slot->key.data, |
| 2120 | slot->key.bytes, |
| 2121 | &slot->key.bytes, &bits); |
| 2122 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2123 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2124 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2127 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2128 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2129 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2130 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2131 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2133 | status = psa_validate_optional_attributes(slot, attributes); |
| 2134 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2135 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2136 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2138 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2139 | exit: |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2140 | LOCAL_INPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2141 | if (status != PSA_SUCCESS) { |
| 2142 | psa_fail_key_creation(slot, driver); |
| 2143 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2145 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2146 | } |
| 2147 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2148 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2149 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2150 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2151 | { |
| 2152 | psa_status_t status; |
| 2153 | psa_key_slot_t *slot = NULL; |
| 2154 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2156 | |
| 2157 | /* Leaving attributes unspecified is not currently supported. |
| 2158 | * It could make sense to query the key type and size from the |
| 2159 | * secure element, but not all secure elements support this |
| 2160 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2161 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2162 | return PSA_ERROR_NOT_SUPPORTED; |
| 2163 | } |
| 2164 | if (psa_get_key_bits(attributes) == 0) { |
| 2165 | return PSA_ERROR_NOT_SUPPORTED; |
| 2166 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2168 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2169 | &slot, &driver); |
| 2170 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2171 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2172 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2175 | |
| 2176 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2177 | if (status != PSA_SUCCESS) { |
| 2178 | psa_fail_key_creation(slot, driver); |
| 2179 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2181 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2182 | psa_close_key(key); |
| 2183 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2184 | } |
| 2185 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2187 | static psa_status_t psa_copy_key_material(const psa_key_slot_t *source, |
| 2188 | psa_key_slot_t *target) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2189 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2190 | psa_status_t status = psa_copy_key_material_into_slot(target, |
| 2191 | source->key.data, |
| 2192 | source->key.bytes); |
| 2193 | if (status != PSA_SUCCESS) { |
| 2194 | return status; |
| 2195 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2196 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2197 | target->attr.type = source->attr.type; |
| 2198 | target->attr.bits = source->attr.bits; |
| 2199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2200 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2203 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2204 | const psa_key_attributes_t *specified_attributes, |
| 2205 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2206 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2207 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2208 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2209 | psa_key_slot_t *source_slot = NULL; |
| 2210 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2211 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2212 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2213 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2214 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2215 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2216 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2217 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2218 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2219 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2220 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2222 | status = psa_validate_optional_attributes(source_slot, |
| 2223 | specified_attributes); |
| 2224 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2225 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2226 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2228 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2229 | &actual_attributes.core.policy, |
| 2230 | &source_slot->attr.policy); |
| 2231 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2232 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2233 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2235 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2236 | &target_slot, &driver); |
| 2237 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2238 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2239 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2241 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2242 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2243 | /* Copying to a secure element is not implemented yet. */ |
| 2244 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2245 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2246 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2247 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2249 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2250 | /* |
| 2251 | * Copying through an opaque driver is not implemented yet, consider |
| 2252 | * a lifetime with an external location as an invalid parameter for |
| 2253 | * now. |
| 2254 | */ |
| 2255 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2256 | goto exit; |
| 2257 | } |
| 2258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2259 | status = psa_copy_key_material(source_slot, target_slot); |
| 2260 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2261 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2262 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2264 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2265 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2266 | if (status != PSA_SUCCESS) { |
| 2267 | psa_fail_key_creation(target_slot, driver); |
| 2268 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2270 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2272 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2275 | |
| 2276 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2277 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2278 | /* Message digests */ |
| 2279 | /****************************************************************/ |
| 2280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2281 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2282 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2283 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2284 | if (operation->id == 0) { |
| 2285 | return PSA_SUCCESS; |
| 2286 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2288 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2289 | operation->id = 0; |
| 2290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2291 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2292 | } |
| 2293 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2294 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2295 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2296 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2297 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2298 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2299 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2300 | if (operation->id != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2301 | status = PSA_ERROR_BAD_STATE; |
| 2302 | goto exit; |
| 2303 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2304 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2306 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2307 | goto exit; |
| 2308 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2309 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2310 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2311 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2312 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2314 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2315 | |
| 2316 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2317 | if (status != PSA_SUCCESS) { |
| 2318 | psa_hash_abort(operation); |
| 2319 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2320 | |
| 2321 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2324 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2325 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2326 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2327 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2328 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2329 | LOCAL_INPUT_DECLARE(input_external, input); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2331 | if (operation->id == 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2332 | status = PSA_ERROR_BAD_STATE; |
| 2333 | goto exit; |
| 2334 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2335 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2336 | /* Don't require hash implementations to behave correctly on a |
| 2337 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2338 | if (input_length == 0) { |
| 2339 | return PSA_SUCCESS; |
| 2340 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2341 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2342 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2343 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2344 | |
| 2345 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2346 | if (status != PSA_SUCCESS) { |
| 2347 | psa_hash_abort(operation); |
| 2348 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2349 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2350 | LOCAL_INPUT_FREE(input_external, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2351 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2354 | static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation, |
Thomas Daubney | cbf0921 | 2024-01-25 17:14:29 +0000 | [diff] [blame] | 2355 | uint8_t *hash, |
| 2356 | size_t hash_size, |
| 2357 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2358 | { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2359 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2360 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2361 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2362 | if (operation->id == 0) { |
| 2363 | return PSA_ERROR_BAD_STATE; |
| 2364 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2365 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2366 | status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2367 | operation, hash, hash_size, hash_length); |
| 2368 | psa_hash_abort(operation); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2369 | |
| 2370 | return status; |
| 2371 | } |
| 2372 | |
| 2373 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2374 | uint8_t *hash_external, |
| 2375 | size_t hash_size, |
| 2376 | size_t *hash_length) |
| 2377 | { |
| 2378 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2379 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2380 | |
| 2381 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2382 | status = psa_hash_finish_internal(operation, hash, hash_size, hash_length); |
| 2383 | |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2384 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2385 | exit: |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2386 | #endif |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2387 | LOCAL_OUTPUT_FREE(hash_external, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2388 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2391 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2392 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2393 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2394 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2395 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2396 | size_t actual_hash_length; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2397 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2398 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 2399 | |
| 2400 | status = psa_hash_finish_internal( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2401 | operation, |
| 2402 | actual_hash, sizeof(actual_hash), |
| 2403 | &actual_hash_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2405 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2406 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2407 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2410 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2411 | goto exit; |
| 2412 | } |
| 2413 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2414 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2415 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2416 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2417 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2418 | |
| 2419 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2420 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2421 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2422 | psa_hash_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2423 | } |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2424 | LOCAL_INPUT_FREE(hash_external, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2425 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2428 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2429 | const uint8_t *input_external, size_t input_length, |
| 2430 | uint8_t *hash_external, size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2431 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2432 | { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2433 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2434 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2435 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2436 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2437 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2438 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2439 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2440 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2441 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2442 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2443 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2444 | status = psa_driver_wrapper_hash_compute(alg, input, input_length, |
Thomas Daubney | cbf0921 | 2024-01-25 17:14:29 +0000 | [diff] [blame] | 2445 | hash, hash_size, hash_length); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2446 | |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2447 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2448 | exit: |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2449 | #endif |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2450 | LOCAL_INPUT_FREE(input_external, input); |
| 2451 | LOCAL_OUTPUT_FREE(hash_external, hash); |
| 2452 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2453 | } |
| 2454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2455 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2456 | const uint8_t *input_external, size_t input_length, |
| 2457 | const uint8_t *hash_external, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2458 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2459 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2460 | size_t actual_hash_length; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2461 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2462 | |
| 2463 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2464 | LOCAL_INPUT_DECLARE(hash_external, hash); |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2466 | if (!PSA_ALG_IS_HASH(alg)) { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2467 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2468 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2469 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2470 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2471 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2472 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2473 | alg, input, input_length, |
| 2474 | actual_hash, sizeof(actual_hash), |
| 2475 | &actual_hash_length); |
| 2476 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2477 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2478 | } |
| 2479 | if (actual_hash_length != hash_length) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2480 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2481 | goto exit; |
| 2482 | } |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2483 | |
| 2484 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2485 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2486 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2487 | } |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2488 | |
| 2489 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2490 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2491 | |
| 2492 | LOCAL_INPUT_FREE(input_external, input); |
| 2493 | LOCAL_INPUT_FREE(hash_external, hash); |
| 2494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2495 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2498 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2499 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2500 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2501 | if (source_operation->id == 0 || |
| 2502 | target_operation->id != 0) { |
| 2503 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2504 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2505 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2506 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2507 | target_operation); |
| 2508 | if (status != PSA_SUCCESS) { |
| 2509 | psa_hash_abort(target_operation); |
| 2510 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2512 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2513 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2514 | |
| 2515 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2516 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2517 | /* MAC */ |
| 2518 | /****************************************************************/ |
| 2519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2520 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2521 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2522 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2523 | if (operation->id == 0) { |
| 2524 | return PSA_SUCCESS; |
| 2525 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2526 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2527 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2528 | operation->mac_size = 0; |
| 2529 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2530 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2532 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2535 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2536 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2537 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2538 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2539 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2540 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2541 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2542 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2543 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2544 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2545 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2546 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2547 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2548 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2549 | status = psa_mac_key_can_do(alg, key_type); |
| 2550 | if (status != PSA_SUCCESS) { |
| 2551 | return status; |
| 2552 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2553 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2554 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2555 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2557 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2558 | /* A very short MAC is too short for security since it can be |
| 2559 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2560 | * so we make this our minimum, even though 32 bits is still |
| 2561 | * too small for security. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2562 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2563 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2564 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2565 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2566 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2567 | /* It's impossible to "truncate" to a larger length than the full length |
| 2568 | * of the algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2569 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2572 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2573 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2574 | * that is disabled in the compile-time configuration. The result can |
| 2575 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2576 | * configuration into account. In this case, force a return of |
| 2577 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2578 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2579 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2580 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2581 | * systematically generated tests. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2582 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2585 | return PSA_SUCCESS; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2588 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2589 | mbedtls_svc_key_id_t key, |
| 2590 | psa_algorithm_t alg, |
| 2591 | int is_sign) |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2592 | { |
| 2593 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2594 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2595 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2596 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2597 | |
| 2598 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2599 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2600 | status = PSA_ERROR_BAD_STATE; |
| 2601 | goto exit; |
| 2602 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2603 | |
| 2604 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2605 | key, |
| 2606 | &slot, |
| 2607 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2608 | alg); |
| 2609 | if (status != PSA_SUCCESS) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2610 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2611 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2612 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2613 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2614 | .core = slot->attr |
| 2615 | }; |
| 2616 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2617 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2618 | &operation->mac_size); |
| 2619 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2620 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2621 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2622 | |
| 2623 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2624 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2625 | if (is_sign) { |
| 2626 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2627 | &attributes, |
| 2628 | slot->key.data, |
| 2629 | slot->key.bytes, |
| 2630 | alg); |
| 2631 | } else { |
| 2632 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2633 | &attributes, |
| 2634 | slot->key.data, |
| 2635 | slot->key.bytes, |
| 2636 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2637 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2638 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2639 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2640 | if (status != PSA_SUCCESS) { |
| 2641 | psa_mac_abort(operation); |
| 2642 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2644 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2649 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2650 | mbedtls_svc_key_id_t key, |
| 2651 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2652 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2653 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2654 | } |
| 2655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2656 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2657 | mbedtls_svc_key_id_t key, |
| 2658 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2659 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2660 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2661 | } |
| 2662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2663 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2664 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2665 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2666 | { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2667 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2668 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2669 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2670 | if (operation->id == 0) { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2671 | status = PSA_ERROR_BAD_STATE; |
| 2672 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2673 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2674 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2675 | /* Don't require hash implementations to behave correctly on a |
| 2676 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2677 | if (input_length == 0) { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2678 | status = PSA_SUCCESS; |
| 2679 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2680 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2681 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2682 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2683 | status = psa_driver_wrapper_mac_update(operation, input, input_length); |
| 2684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2685 | if (status != PSA_SUCCESS) { |
| 2686 | psa_mac_abort(operation); |
| 2687 | } |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2688 | |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2689 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2690 | exit: |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2691 | #endif |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2692 | LOCAL_INPUT_FREE(input_external, input); |
| 2693 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2694 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2697 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2698 | uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2699 | size_t mac_size, |
| 2700 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2701 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2702 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2703 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2704 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
Thomas Daubney | 301491d | 2024-02-01 14:32:59 +0000 | [diff] [blame] | 2705 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2706 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2707 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2708 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2709 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2710 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2712 | if (!operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2713 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2714 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2715 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2716 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2717 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2718 | * once all the error checks are done. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2719 | if (operation->mac_size == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2720 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2721 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2722 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2724 | if (mac_size < operation->mac_size) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2725 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2726 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2727 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2728 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2729 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2730 | mac, operation->mac_size, |
| 2731 | mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2732 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2733 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2734 | /* In case of success, set the potential excess room in the output buffer |
| 2735 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2736 | * In case of error, set the output length and content to a safe default, |
| 2737 | * such that in case the caller misses an error check, the output would be |
| 2738 | * an unachievable MAC. |
| 2739 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2741 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2742 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2743 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2744 | |
Thomas Daubney | 480347d | 2024-02-01 19:00:26 +0000 | [diff] [blame] | 2745 | if ((mac != NULL) && (mac_size > operation->mac_size)) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2746 | memset(&mac[operation->mac_size], '!', |
| 2747 | mac_size - operation->mac_size); |
| 2748 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2749 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2750 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2751 | LOCAL_OUTPUT_FREE(mac_external, mac); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2752 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2753 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2756 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2757 | const uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2758 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2759 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2760 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2761 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2762 | LOCAL_INPUT_DECLARE(mac_external, mac); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2764 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2765 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2766 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2767 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2769 | if (operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2770 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2771 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2772 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2774 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2775 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2776 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2779 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2780 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2781 | mac, mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2782 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2783 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2784 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2785 | LOCAL_INPUT_FREE(mac_external, mac); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2786 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2787 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2790 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2791 | psa_algorithm_t alg, |
| 2792 | const uint8_t *input, |
| 2793 | size_t input_length, |
| 2794 | uint8_t *mac, |
| 2795 | size_t mac_size, |
| 2796 | size_t *mac_length, |
| 2797 | int is_sign) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2798 | { |
| 2799 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2800 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2801 | psa_key_attributes_t attributes; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2802 | psa_key_slot_t *slot; |
| 2803 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2804 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2805 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2806 | key, |
| 2807 | &slot, |
| 2808 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2809 | alg); |
| 2810 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2811 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2812 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2813 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2814 | attributes = (psa_key_attributes_t) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2815 | .core = slot->attr |
| 2816 | }; |
| 2817 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2818 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2819 | &operation_mac_size); |
| 2820 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2821 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2822 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | if (mac_size < operation_mac_size) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2825 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2826 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2830 | &attributes, |
| 2831 | slot->key.data, slot->key.bytes, |
| 2832 | alg, |
| 2833 | input, input_length, |
| 2834 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2835 | |
| 2836 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2837 | /* In case of success, set the potential excess room in the output buffer |
| 2838 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2839 | * In case of error, set the output length and content to a safe default, |
| 2840 | * such that in case the caller misses an error check, the output would be |
| 2841 | * an unachievable MAC. |
| 2842 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2843 | if (status != PSA_SUCCESS) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2844 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2845 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2846 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2847 | if (mac_size > operation_mac_size) { |
| 2848 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
| 2849 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2850 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2851 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2852 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2853 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2854 | } |
| 2855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2856 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2857 | psa_algorithm_t alg, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2858 | const uint8_t *input_external, |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2859 | size_t input_length, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2860 | uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2861 | size_t mac_size, |
| 2862 | size_t *mac_length) |
| 2863 | { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2864 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2865 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2866 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
| 2867 | |
| 2868 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2869 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
| 2870 | status = psa_mac_compute_internal(key, alg, |
Thomas Daubney | f298f65 | 2024-01-30 12:15:36 +0000 | [diff] [blame] | 2871 | input, input_length, |
| 2872 | mac, mac_size, mac_length, 1); |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2873 | |
| 2874 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2875 | exit: |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2876 | #endif |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2877 | LOCAL_INPUT_FREE(input_external, input); |
| 2878 | LOCAL_OUTPUT_FREE(mac_external, mac); |
| 2879 | |
| 2880 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2884 | psa_algorithm_t alg, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2885 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | size_t input_length, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2887 | const uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2888 | size_t mac_length) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2889 | { |
| 2890 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2891 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2892 | size_t actual_mac_length; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2893 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2894 | LOCAL_INPUT_DECLARE(mac_external, mac); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2895 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2896 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2897 | status = psa_mac_compute_internal(key, alg, |
| 2898 | input, input_length, |
| 2899 | actual_mac, sizeof(actual_mac), |
| 2900 | &actual_mac_length, 0); |
| 2901 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2902 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2903 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2904 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2905 | if (mac_length != actual_mac_length) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2906 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2907 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2908 | } |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2909 | |
| 2910 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2911 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2912 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2913 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2914 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2915 | |
| 2916 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2917 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2918 | LOCAL_INPUT_FREE(input_external, input); |
| 2919 | LOCAL_INPUT_FREE(mac_external, mac); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2921 | return status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2922 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2923 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2924 | /****************************************************************/ |
| 2925 | /* Asymmetric cryptography */ |
| 2926 | /****************************************************************/ |
| 2927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2928 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2929 | psa_algorithm_t alg) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2930 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2931 | if (input_is_message) { |
| 2932 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2933 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2934 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2935 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2936 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2937 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2938 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2939 | } |
| 2940 | } |
| 2941 | } else { |
| 2942 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2943 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2944 | } |
| 2945 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2947 | return PSA_SUCCESS; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2948 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2949 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2950 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2951 | int input_is_message, |
| 2952 | psa_algorithm_t alg, |
| 2953 | const uint8_t *input, |
| 2954 | size_t input_length, |
| 2955 | uint8_t *signature, |
| 2956 | size_t signature_size, |
| 2957 | size_t *signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2958 | { |
| 2959 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2960 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2961 | psa_key_attributes_t attributes; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2962 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2963 | |
| 2964 | *signature_length = 0; |
| 2965 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2966 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2967 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2968 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2969 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2970 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2971 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2972 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2973 | * buffer can in principle be empty since it doesn't actually have |
| 2974 | * to be a hash.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2975 | if (signature_size == 0) { |
| 2976 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2977 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2978 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2979 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2980 | key, &slot, |
| 2981 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2982 | PSA_KEY_USAGE_SIGN_HASH, |
| 2983 | alg); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2984 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2985 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2986 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2987 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2988 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2989 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2990 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2991 | goto exit; |
| 2992 | } |
| 2993 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2994 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2995 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2996 | }; |
| 2997 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2998 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2999 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3000 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3001 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3002 | signature, signature_size, signature_length); |
| 3003 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3004 | |
| 3005 | status = psa_driver_wrapper_sign_hash( |
| 3006 | &attributes, slot->key.data, slot->key.bytes, |
| 3007 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3008 | signature, signature_size, signature_length); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3009 | } |
| 3010 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3011 | |
| 3012 | exit: |
| 3013 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3014 | * the trailing part on success) with something that isn't a valid signature |
| 3015 | * (barring an attack on the signature and deliberately-crafted input), |
| 3016 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3017 | if (status == PSA_SUCCESS) { |
| 3018 | memset(signature + *signature_length, '!', |
| 3019 | signature_size - *signature_length); |
| 3020 | } else { |
| 3021 | memset(signature, '!', signature_size); |
| 3022 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3023 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3024 | * memset because signature may be NULL in this case. */ |
| 3025 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3026 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3027 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3028 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3029 | } |
| 3030 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3031 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 3032 | int input_is_message, |
| 3033 | psa_algorithm_t alg, |
| 3034 | const uint8_t *input, |
| 3035 | size_t input_length, |
| 3036 | const uint8_t *signature, |
| 3037 | size_t signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3038 | { |
| 3039 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3040 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3041 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3042 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3043 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3044 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3045 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3046 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3047 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3048 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3049 | key, &slot, |
| 3050 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 3051 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3052 | alg); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3053 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3054 | if (status != PSA_SUCCESS) { |
| 3055 | return status; |
| 3056 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3057 | |
| 3058 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3059 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3060 | }; |
| 3061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3062 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3063 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3064 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3065 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3066 | signature, signature_length); |
| 3067 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3068 | status = psa_driver_wrapper_verify_hash( |
| 3069 | &attributes, slot->key.data, slot->key.bytes, |
| 3070 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3071 | signature, signature_length); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3072 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3076 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3077 | |
| 3078 | } |
| 3079 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3080 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3081 | const psa_key_attributes_t *attributes, |
| 3082 | const uint8_t *key_buffer, |
| 3083 | size_t key_buffer_size, |
| 3084 | psa_algorithm_t alg, |
| 3085 | const uint8_t *input, |
| 3086 | size_t input_length, |
| 3087 | uint8_t *signature, |
| 3088 | size_t signature_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3089 | size_t *signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3090 | { |
| 3091 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3092 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3093 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3094 | size_t hash_length; |
| 3095 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3096 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3097 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3098 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3099 | input, input_length, |
| 3100 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3102 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3103 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3104 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3105 | |
| 3106 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3107 | attributes, key_buffer, key_buffer_size, |
| 3108 | alg, hash, hash_length, |
| 3109 | signature, signature_size, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3110 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3112 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3113 | } |
| 3114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3115 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3116 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3117 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3118 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3119 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3120 | size_t signature_size, |
| 3121 | size_t *signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3122 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3123 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3124 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3125 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3126 | |
| 3127 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3128 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3129 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3130 | signature_size, signature_length); |
| 3131 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3132 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3133 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3134 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3135 | LOCAL_INPUT_FREE(input_external, input); |
| 3136 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3137 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3138 | } |
| 3139 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3140 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3141 | const psa_key_attributes_t *attributes, |
| 3142 | const uint8_t *key_buffer, |
| 3143 | size_t key_buffer_size, |
| 3144 | psa_algorithm_t alg, |
| 3145 | const uint8_t *input, |
| 3146 | size_t input_length, |
| 3147 | const uint8_t *signature, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3148 | size_t signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3149 | { |
| 3150 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3152 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3153 | size_t hash_length; |
| 3154 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3155 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3156 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3157 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3158 | input, input_length, |
| 3159 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3161 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3162 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3163 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3164 | |
| 3165 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3166 | attributes, key_buffer, key_buffer_size, |
| 3167 | alg, hash, hash_length, |
| 3168 | signature, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3169 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3171 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3174 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3175 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3176 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3177 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3178 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3179 | size_t signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3180 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3181 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3182 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3183 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3184 | |
| 3185 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3186 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3187 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3188 | signature_length); |
| 3189 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3190 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3191 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3192 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3193 | LOCAL_INPUT_FREE(input_external, input); |
| 3194 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3195 | |
| 3196 | return status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3199 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3200 | const psa_key_attributes_t *attributes, |
| 3201 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3202 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3203 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3204 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3205 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3206 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3207 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3208 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3209 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3210 | return mbedtls_psa_rsa_sign_hash( |
| 3211 | attributes, |
| 3212 | key_buffer, key_buffer_size, |
| 3213 | alg, hash, hash_length, |
| 3214 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3215 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3216 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3217 | } else { |
| 3218 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3219 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3220 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3221 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3222 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3223 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3224 | return mbedtls_psa_ecdsa_sign_hash( |
| 3225 | attributes, |
| 3226 | key_buffer, key_buffer_size, |
| 3227 | alg, hash, hash_length, |
| 3228 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3229 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3230 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3231 | } else { |
| 3232 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3233 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3234 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3236 | (void) key_buffer; |
| 3237 | (void) key_buffer_size; |
| 3238 | (void) hash; |
| 3239 | (void) hash_length; |
| 3240 | (void) signature; |
| 3241 | (void) signature_size; |
| 3242 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3244 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3245 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3247 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3248 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3249 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3250 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3251 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3252 | size_t signature_size, |
| 3253 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3254 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3255 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3256 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3257 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3258 | |
| 3259 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3260 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3261 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3262 | signature_size, signature_length); |
| 3263 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3264 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3265 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3266 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3267 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3268 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3269 | |
| 3270 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3271 | } |
| 3272 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3273 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3274 | const psa_key_attributes_t *attributes, |
| 3275 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3276 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3277 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3278 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3279 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3280 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3281 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3282 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3283 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3284 | return mbedtls_psa_rsa_verify_hash( |
| 3285 | attributes, |
| 3286 | key_buffer, key_buffer_size, |
| 3287 | alg, hash, hash_length, |
| 3288 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3289 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3290 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3291 | } else { |
| 3292 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3293 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3294 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3295 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3296 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3297 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3298 | return mbedtls_psa_ecdsa_verify_hash( |
| 3299 | attributes, |
| 3300 | key_buffer, key_buffer_size, |
| 3301 | alg, hash, hash_length, |
| 3302 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3303 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3304 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3305 | } else { |
| 3306 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3307 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3308 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3310 | (void) key_buffer; |
| 3311 | (void) key_buffer_size; |
| 3312 | (void) hash; |
| 3313 | (void) hash_length; |
| 3314 | (void) signature; |
| 3315 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3317 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3318 | } |
| 3319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3320 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3321 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3322 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3323 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3324 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3325 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3326 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3327 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3328 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3329 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3330 | |
| 3331 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3332 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3333 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3334 | signature_length); |
| 3335 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3336 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3337 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3338 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3339 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3340 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3341 | |
| 3342 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3343 | } |
| 3344 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3345 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3346 | static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg, |
| 3347 | mbedtls_rsa_context *rsa) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3348 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3349 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg); |
| 3350 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 3351 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
| 3352 | mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg); |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3353 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3354 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3356 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3357 | psa_algorithm_t alg, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3358 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3359 | size_t input_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3360 | const uint8_t *salt_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3361 | size_t salt_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3362 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3363 | size_t output_size, |
| 3364 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3365 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3366 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3367 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3368 | psa_key_slot_t *slot; |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3369 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3370 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3371 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3372 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3373 | (void) input; |
| 3374 | (void) input_length; |
| 3375 | (void) salt; |
| 3376 | (void) output; |
| 3377 | (void) output_size; |
| 3378 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3379 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3380 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3381 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3382 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3383 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3384 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3385 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3386 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3387 | if (status != PSA_SUCCESS) { |
| 3388 | return status; |
| 3389 | } |
| 3390 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3391 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3392 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3393 | goto exit; |
| 3394 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3395 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3396 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3397 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3398 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3399 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3400 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3401 | slot->key.data, |
| 3402 | slot->key.bytes, |
| 3403 | &rsa); |
| 3404 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3405 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3406 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3408 | if (output_size < mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3409 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3410 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3411 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3412 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3413 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3414 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3415 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3416 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3417 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3418 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3419 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3420 | mbedtls_rsa_pkcs1_encrypt(rsa, |
| 3421 | mbedtls_psa_get_random, |
| 3422 | MBEDTLS_PSA_RANDOM_STATE, |
| 3423 | MBEDTLS_RSA_PUBLIC, |
| 3424 | input_length, |
| 3425 | input, |
| 3426 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3427 | #else |
| 3428 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3429 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3430 | } else |
| 3431 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3432 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3433 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3434 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3435 | mbedtls_rsa_rsaes_oaep_encrypt(rsa, |
| 3436 | mbedtls_psa_get_random, |
| 3437 | MBEDTLS_PSA_RANDOM_STATE, |
| 3438 | MBEDTLS_RSA_PUBLIC, |
| 3439 | salt, salt_length, |
| 3440 | input_length, |
| 3441 | input, |
| 3442 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3443 | #else |
| 3444 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3445 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3446 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3447 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3448 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3449 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3450 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3451 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3452 | if (status == PSA_SUCCESS) { |
| 3453 | *output_length = mbedtls_rsa_get_len(rsa); |
| 3454 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3456 | mbedtls_rsa_free(rsa); |
| 3457 | mbedtls_free(rsa); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3458 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3459 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3460 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3461 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3462 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3463 | |
| 3464 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3465 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3466 | |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3467 | LOCAL_INPUT_FREE(input_external, input); |
| 3468 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3469 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3470 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3471 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3472 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3473 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3474 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3475 | psa_algorithm_t alg, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3476 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3477 | size_t input_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3478 | const uint8_t *salt_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3479 | size_t salt_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3480 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3481 | size_t output_size, |
| 3482 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3483 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3484 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3485 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3486 | psa_key_slot_t *slot; |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3487 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3488 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3489 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3490 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3491 | (void) input; |
| 3492 | (void) input_length; |
| 3493 | (void) salt; |
| 3494 | (void) output; |
| 3495 | (void) output_size; |
| 3496 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3497 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3498 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3499 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3500 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3501 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3502 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3503 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3504 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3505 | if (status != PSA_SUCCESS) { |
| 3506 | return status; |
| 3507 | } |
| 3508 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3509 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3510 | goto exit; |
| 3511 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3512 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3513 | if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3514 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3515 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3516 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3517 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3518 | slot->key.data, |
| 3519 | slot->key.bytes, |
| 3520 | &rsa); |
| 3521 | if (status != PSA_SUCCESS) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3522 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3523 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3525 | if (input_length != mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3526 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3527 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3528 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3529 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3530 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3531 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3532 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3533 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3534 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3535 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3536 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3537 | mbedtls_rsa_pkcs1_decrypt(rsa, |
| 3538 | mbedtls_psa_get_random, |
| 3539 | MBEDTLS_PSA_RANDOM_STATE, |
| 3540 | MBEDTLS_RSA_PRIVATE, |
| 3541 | output_length, |
| 3542 | input, |
| 3543 | output, |
| 3544 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3545 | #else |
| 3546 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3547 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3548 | } else |
| 3549 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3550 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3551 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3552 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3553 | mbedtls_rsa_rsaes_oaep_decrypt(rsa, |
| 3554 | mbedtls_psa_get_random, |
| 3555 | MBEDTLS_PSA_RANDOM_STATE, |
| 3556 | MBEDTLS_RSA_PRIVATE, |
| 3557 | salt, salt_length, |
| 3558 | output_length, |
| 3559 | input, |
| 3560 | output, |
| 3561 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3562 | #else |
| 3563 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3564 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3565 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3566 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3567 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3568 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3569 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3570 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3571 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3572 | mbedtls_rsa_free(rsa); |
| 3573 | mbedtls_free(rsa); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3574 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3575 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3576 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3577 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3578 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3579 | |
| 3580 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3581 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3582 | |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3583 | LOCAL_INPUT_FREE(input_external, input); |
| 3584 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3585 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3586 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3587 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3588 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3589 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3590 | |
| 3591 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3592 | /****************************************************************/ |
| 3593 | /* Symmetric cryptography */ |
| 3594 | /****************************************************************/ |
| 3595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3596 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3597 | mbedtls_svc_key_id_t key, |
| 3598 | psa_algorithm_t alg, |
| 3599 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3600 | { |
| 3601 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3602 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3603 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3604 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3605 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3606 | PSA_KEY_USAGE_ENCRYPT : |
| 3607 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3608 | |
| 3609 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3610 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3611 | status = PSA_ERROR_BAD_STATE; |
| 3612 | goto exit; |
| 3613 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3614 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3615 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3616 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3617 | goto exit; |
| 3618 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3620 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3621 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3622 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3623 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3624 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3625 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3626 | * 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] | 3627 | * so we only set it (in the driver wrapper) after resources have been |
| 3628 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3629 | operation->iv_set = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3630 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3631 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3632 | } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) { |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3633 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3634 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3635 | operation->iv_required = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3636 | } |
| 3637 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3638 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3639 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3640 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3641 | }; |
| 3642 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3643 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3644 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3645 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3646 | &attributes, |
| 3647 | slot->key.data, |
| 3648 | slot->key.bytes, |
| 3649 | alg); |
| 3650 | } else { |
| 3651 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3652 | &attributes, |
| 3653 | slot->key.data, |
| 3654 | slot->key.bytes, |
| 3655 | alg); |
| 3656 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3657 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3658 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3659 | if (status != PSA_SUCCESS) { |
| 3660 | psa_cipher_abort(operation); |
| 3661 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3663 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3664 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3665 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3668 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3669 | mbedtls_svc_key_id_t key, |
| 3670 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3671 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3675 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3676 | mbedtls_svc_key_id_t key, |
| 3677 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3678 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3679 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3682 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 3683 | uint8_t *iv, |
| 3684 | size_t iv_size, |
| 3685 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3686 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3687 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3688 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
Sergey | 04eb7c0 | 2023-03-06 15:37:23 -0700 | [diff] [blame] | 3689 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3691 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3692 | status = PSA_ERROR_BAD_STATE; |
| 3693 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3696 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3697 | status = PSA_ERROR_BAD_STATE; |
| 3698 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3699 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3700 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3701 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3702 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3703 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3704 | goto exit; |
| 3705 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3706 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3707 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3708 | status = PSA_ERROR_GENERIC_ERROR; |
| 3709 | goto exit; |
| 3710 | } |
| 3711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3712 | status = psa_generate_random(local_iv, default_iv_length); |
| 3713 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3714 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3715 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3717 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3718 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3719 | |
| 3720 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | if (status == PSA_SUCCESS) { |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame^] | 3722 | psa_crypto_copy_output(local_iv, default_iv_length, iv, iv_size); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3723 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3724 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3725 | } else { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3726 | *iv_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3727 | psa_cipher_abort(operation); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3728 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3729 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3730 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3733 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame^] | 3734 | const uint8_t *iv_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3735 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3736 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3737 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3738 | |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame^] | 3739 | LOCAL_INPUT_DECLARE(iv_external, iv); |
| 3740 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3741 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3742 | status = PSA_ERROR_BAD_STATE; |
| 3743 | goto exit; |
| 3744 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3745 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3746 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3747 | status = PSA_ERROR_BAD_STATE; |
| 3748 | goto exit; |
| 3749 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3751 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3752 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3753 | goto exit; |
| 3754 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3755 | |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame^] | 3756 | LOCAL_INPUT_ALLOC(iv_external, iv_length, iv); |
| 3757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3758 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3759 | iv, |
| 3760 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3761 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3762 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3763 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3764 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3765 | } else { |
| 3766 | psa_cipher_abort(operation); |
| 3767 | } |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame^] | 3768 | |
| 3769 | LOCAL_INPUT_FREE(iv_external, iv); |
| 3770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3771 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3772 | } |
| 3773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3774 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3775 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3776 | size_t input_length, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3777 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3778 | size_t output_size, |
| 3779 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3780 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3781 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3782 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3783 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3784 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3786 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3787 | status = PSA_ERROR_BAD_STATE; |
| 3788 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3789 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3791 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3792 | status = PSA_ERROR_BAD_STATE; |
| 3793 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3794 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3795 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3796 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3797 | LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output); |
| 3798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3799 | status = psa_driver_wrapper_cipher_update(operation, |
| 3800 | input, |
| 3801 | input_length, |
| 3802 | output, |
| 3803 | output_size, |
| 3804 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3805 | |
| 3806 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3807 | if (status != PSA_SUCCESS) { |
| 3808 | psa_cipher_abort(operation); |
| 3809 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3810 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3811 | LOCAL_INPUT_FREE(input_external, input); |
| 3812 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3814 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3815 | } |
| 3816 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3817 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3818 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3819 | size_t output_size, |
| 3820 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3821 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3822 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3823 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3824 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3826 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3827 | status = PSA_ERROR_BAD_STATE; |
| 3828 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3829 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3831 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3832 | status = PSA_ERROR_BAD_STATE; |
| 3833 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3834 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3835 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3836 | LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output); |
| 3837 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3838 | status = psa_driver_wrapper_cipher_finish(operation, |
| 3839 | output, |
| 3840 | output_size, |
| 3841 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3842 | |
| 3843 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3844 | if (status == PSA_SUCCESS) { |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3845 | status = psa_cipher_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3846 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3847 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3848 | (void) psa_cipher_abort(operation); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3849 | } |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3850 | |
| 3851 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3852 | |
| 3853 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3854 | } |
| 3855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3856 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3857 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3858 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3859 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3860 | * in use. It's ok to call abort on such an object, and there's |
| 3861 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3862 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3863 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3864 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3865 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3866 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3867 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3868 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3869 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3871 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3872 | } |
| 3873 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3874 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 3875 | psa_algorithm_t alg, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3876 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3877 | size_t input_length, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3878 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3879 | size_t output_size, |
| 3880 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3881 | { |
| 3882 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3883 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3884 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3885 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3886 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3887 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3888 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3889 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3890 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3891 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3892 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3893 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3894 | goto exit; |
| 3895 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3897 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3898 | PSA_KEY_USAGE_ENCRYPT, |
| 3899 | alg); |
| 3900 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3901 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3902 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3903 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3904 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3905 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3906 | }; |
| 3907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3908 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 3909 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3910 | status = PSA_ERROR_GENERIC_ERROR; |
| 3911 | goto exit; |
| 3912 | } |
| 3913 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3914 | if (default_iv_length > 0) { |
| 3915 | if (output_size < default_iv_length) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3916 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3917 | goto exit; |
| 3918 | } |
| 3919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3920 | status = psa_generate_random(local_iv, default_iv_length); |
| 3921 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3922 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3923 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3924 | } |
| 3925 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3926 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3927 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 3928 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3929 | status = psa_driver_wrapper_cipher_encrypt( |
| 3930 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3931 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3932 | mbedtls_buffer_offset(output, default_iv_length), |
| 3933 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3934 | |
| 3935 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3936 | unlock_status = psa_unlock_key_slot(slot); |
| 3937 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3938 | status = unlock_status; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3939 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3940 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3941 | if (status == PSA_SUCCESS) { |
| 3942 | if (default_iv_length > 0) { |
| 3943 | memcpy(output, local_iv, default_iv_length); |
| 3944 | } |
| 3945 | *output_length += default_iv_length; |
| 3946 | } else { |
| 3947 | *output_length = 0; |
| 3948 | } |
| 3949 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3950 | LOCAL_INPUT_FREE(input_external, input); |
| 3951 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | d2ad886 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 3952 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3953 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3956 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 3957 | psa_algorithm_t alg, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3958 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3959 | size_t input_length, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3960 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3961 | size_t output_size, |
| 3962 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3963 | { |
| 3964 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3965 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3966 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3967 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3968 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3969 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3970 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3971 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3972 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3973 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3974 | goto exit; |
| 3975 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3976 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3977 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3978 | PSA_KEY_USAGE_DECRYPT, |
| 3979 | alg); |
| 3980 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3981 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3982 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3983 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3984 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3985 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3986 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3988 | if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3989 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3990 | goto exit; |
| 3991 | } |
| 3992 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3993 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3994 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 3995 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3996 | status = psa_driver_wrapper_cipher_decrypt( |
| 3997 | &attributes, slot->key.data, slot->key.bytes, |
| 3998 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3999 | output, output_size, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4000 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4001 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4002 | unlock_status = psa_unlock_key_slot(slot); |
| 4003 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4004 | status = unlock_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4005 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4007 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4008 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4009 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4010 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4011 | LOCAL_INPUT_FREE(input_external, input); |
| 4012 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4013 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4014 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4018 | /****************************************************************/ |
| 4019 | /* AEAD */ |
| 4020 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4021 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4022 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4023 | psa_algorithm_t alg, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4024 | const uint8_t *nonce_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4025 | size_t nonce_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4026 | const uint8_t *additional_data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4027 | size_t additional_data_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4028 | const uint8_t *plaintext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4029 | size_t plaintext_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4030 | uint8_t *ciphertext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4031 | size_t ciphertext_size, |
| 4032 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4033 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4034 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4035 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4036 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4037 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4038 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4039 | LOCAL_INPUT_DECLARE(plaintext_external, plaintext); |
| 4040 | LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext); |
| 4041 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4042 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4043 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4044 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4045 | return PSA_ERROR_NOT_SUPPORTED; |
| 4046 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4047 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4048 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4049 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4050 | if (status != PSA_SUCCESS) { |
| 4051 | return status; |
| 4052 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4053 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4054 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4055 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4056 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4057 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4058 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4059 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data); |
| 4060 | LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext); |
| 4061 | LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext); |
| 4062 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4063 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4064 | &attributes, slot->key.data, slot->key.bytes, |
| 4065 | alg, |
| 4066 | nonce, nonce_length, |
| 4067 | additional_data, additional_data_length, |
| 4068 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4069 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4071 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4072 | memset(ciphertext, 0, ciphertext_size); |
| 4073 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4074 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4075 | /* Exit label is only used for buffer copying, prevent unused warnings. */ |
| 4076 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
| 4077 | exit: |
| 4078 | #endif |
| 4079 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4080 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4081 | LOCAL_INPUT_FREE(plaintext_external, plaintext); |
| 4082 | LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext); |
| 4083 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4084 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4085 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4086 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4089 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4090 | psa_algorithm_t alg, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4091 | const uint8_t *nonce_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4092 | size_t nonce_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4093 | const uint8_t *additional_data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4094 | size_t additional_data_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4095 | const uint8_t *ciphertext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4096 | size_t ciphertext_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4097 | uint8_t *plaintext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4098 | size_t plaintext_size, |
| 4099 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4100 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4101 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4102 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4103 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4104 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4105 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4106 | LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext); |
| 4107 | LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext); |
| 4108 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4109 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4111 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4112 | return PSA_ERROR_NOT_SUPPORTED; |
| 4113 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4114 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4115 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4116 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4117 | if (status != PSA_SUCCESS) { |
| 4118 | return status; |
| 4119 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4120 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4121 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4122 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4123 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4124 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4125 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4126 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, |
| 4127 | additional_data); |
| 4128 | LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext); |
| 4129 | LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext); |
| 4130 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4131 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4132 | &attributes, slot->key.data, slot->key.bytes, |
| 4133 | alg, |
| 4134 | nonce, nonce_length, |
| 4135 | additional_data, additional_data_length, |
| 4136 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4137 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4139 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4140 | memset(plaintext, 0, plaintext_size); |
| 4141 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4142 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4143 | /* Exit label is only used for buffer copying, prevent unused warnings. */ |
| 4144 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
| 4145 | exit: |
| 4146 | #endif |
| 4147 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4148 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4149 | LOCAL_INPUT_FREE(ciphertext_external, ciphertext); |
| 4150 | LOCAL_OUTPUT_FREE(plaintext_external, plaintext); |
| 4151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4152 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4153 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4154 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4155 | } |
| 4156 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4157 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4158 | /* Generators */ |
| 4159 | /****************************************************************/ |
| 4160 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4161 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4162 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4163 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4164 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4165 | #endif /* At least one builtin KDF */ |
| 4166 | |
| 4167 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4168 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4169 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4170 | static psa_status_t psa_key_derivation_start_hmac( |
| 4171 | psa_mac_operation_t *operation, |
| 4172 | psa_algorithm_t hash_alg, |
| 4173 | const uint8_t *hmac_key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4174 | size_t hmac_key_length) |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4175 | { |
| 4176 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4178 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 4179 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 4180 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4181 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4182 | operation->is_sign = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4183 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4185 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 4186 | &attributes, |
| 4187 | hmac_key, hmac_key_length, |
| 4188 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4190 | psa_reset_key_attributes(&attributes); |
| 4191 | return status; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4192 | } |
| 4193 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4194 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4195 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4196 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4197 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4198 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4199 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4200 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4201 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4202 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4203 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 4204 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 4205 | } else { |
| 4206 | return operation->alg; |
| 4207 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4208 | } |
| 4209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4210 | 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] | 4211 | { |
| 4212 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4213 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 4214 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4215 | /* The object has (apparently) been initialized but it is not |
| 4216 | * in use. It's ok to call abort on such an object, and there's |
| 4217 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4218 | } else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4219 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4220 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4221 | mbedtls_free(operation->ctx.hkdf.info); |
| 4222 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 4223 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4224 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4225 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4226 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4227 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4228 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 4229 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4230 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 4231 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 4232 | operation->ctx.tls12_prf.secret_length); |
| 4233 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4234 | } |
| 4235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4236 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 4237 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 4238 | operation->ctx.tls12_prf.seed_length); |
| 4239 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4242 | if (operation->ctx.tls12_prf.label != NULL) { |
| 4243 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 4244 | operation->ctx.tls12_prf.label_length); |
| 4245 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4246 | } |
| 4247 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4248 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4249 | |
| 4250 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4251 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4252 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4253 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4254 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4255 | { |
| 4256 | status = PSA_ERROR_BAD_STATE; |
| 4257 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4258 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 4259 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4260 | } |
| 4261 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4262 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4263 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4264 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4265 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4266 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4267 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4270 | *capacity = operation->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4271 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4272 | } |
| 4273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4274 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 4275 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4276 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4277 | if (operation->alg == 0) { |
| 4278 | return PSA_ERROR_BAD_STATE; |
| 4279 | } |
| 4280 | if (capacity > operation->capacity) { |
| 4281 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4282 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4283 | operation->capacity = capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4284 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4287 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4288 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4289 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4290 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 4291 | psa_algorithm_t hash_alg, |
| 4292 | uint8_t *output, |
| 4293 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4294 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4295 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4296 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4297 | psa_status_t status; |
| 4298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4299 | if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) { |
| 4300 | return PSA_ERROR_BAD_STATE; |
| 4301 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4302 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4303 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4304 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4305 | /* Copy what remains of the current block */ |
| 4306 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4307 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4308 | n = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4309 | } |
| 4310 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4311 | output += n; |
| 4312 | output_length -= n; |
| 4313 | hkdf->offset_in_block += n; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4314 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4315 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4316 | } |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4317 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4318 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4319 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4320 | * object was corrupted or if this function is called directly |
| 4321 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4322 | if (hkdf->block_number == 0xff) { |
| 4323 | return PSA_ERROR_BAD_STATE; |
| 4324 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4325 | |
| 4326 | /* We need a new block */ |
| 4327 | ++hkdf->block_number; |
| 4328 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4329 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4330 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4331 | hash_alg, |
| 4332 | hkdf->prk, |
| 4333 | hash_length); |
| 4334 | if (status != PSA_SUCCESS) { |
| 4335 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4336 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4337 | |
| 4338 | if (hkdf->block_number != 1) { |
| 4339 | status = psa_mac_update(&hkdf->hmac, |
| 4340 | hkdf->output_block, |
| 4341 | hash_length); |
| 4342 | if (status != PSA_SUCCESS) { |
| 4343 | return status; |
| 4344 | } |
| 4345 | } |
| 4346 | status = psa_mac_update(&hkdf->hmac, |
| 4347 | hkdf->info, |
| 4348 | hkdf->info_length); |
| 4349 | if (status != PSA_SUCCESS) { |
| 4350 | return status; |
| 4351 | } |
| 4352 | status = psa_mac_update(&hkdf->hmac, |
| 4353 | &hkdf->block_number, 1); |
| 4354 | if (status != PSA_SUCCESS) { |
| 4355 | return status; |
| 4356 | } |
| 4357 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4358 | hkdf->output_block, |
| 4359 | sizeof(hkdf->output_block), |
| 4360 | &hmac_output_length); |
| 4361 | if (status != PSA_SUCCESS) { |
| 4362 | return status; |
| 4363 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4364 | } |
| 4365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4366 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4367 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4368 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4369 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4370 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4371 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4372 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4373 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4374 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4375 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4376 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 4377 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4378 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4379 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4380 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4381 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4382 | /* We can't be wanting more output after block 0xff, otherwise |
| 4383 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4384 | * prevented this call. It could happen only if the operation |
| 4385 | * object was corrupted or if this function is called directly |
| 4386 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4387 | if (tls12_prf->block_number == 0xff) { |
| 4388 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4389 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4390 | |
| 4391 | /* We need a new block */ |
| 4392 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4393 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4394 | |
| 4395 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4396 | * |
| 4397 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4398 | * |
| 4399 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4400 | * HMAC_hash(secret, A(2) + seed) + |
| 4401 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4402 | * |
| 4403 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4404 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4405 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4406 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4407 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4408 | * is currently extracted as `output_block` and where i is |
| 4409 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4410 | */ |
| 4411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4412 | status = psa_key_derivation_start_hmac(&hmac, |
| 4413 | hash_alg, |
| 4414 | tls12_prf->secret, |
| 4415 | tls12_prf->secret_length); |
| 4416 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4417 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4418 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4419 | |
| 4420 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4421 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4422 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4423 | * the variable seed and in this instance means it in the context of the |
| 4424 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4425 | status = psa_mac_update(&hmac, |
| 4426 | tls12_prf->label, |
| 4427 | tls12_prf->label_length); |
| 4428 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4429 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4430 | } |
| 4431 | status = psa_mac_update(&hmac, |
| 4432 | tls12_prf->seed, |
| 4433 | tls12_prf->seed_length); |
| 4434 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4435 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4436 | } |
| 4437 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4438 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4439 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4440 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4441 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4442 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4443 | } |
| 4444 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4445 | status = psa_mac_sign_finish(&hmac, |
| 4446 | tls12_prf->Ai, hash_length, |
| 4447 | &hmac_output_length); |
| 4448 | if (hmac_output_length != hash_length) { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4449 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4450 | } |
| 4451 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4452 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4453 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4456 | status = psa_key_derivation_start_hmac(&hmac, |
| 4457 | hash_alg, |
| 4458 | tls12_prf->secret, |
| 4459 | tls12_prf->secret_length); |
| 4460 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4461 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4462 | } |
| 4463 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4464 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4465 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4466 | } |
| 4467 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 4468 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4469 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4470 | } |
| 4471 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 4472 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4473 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4474 | } |
| 4475 | status = psa_mac_sign_finish(&hmac, |
| 4476 | tls12_prf->output_block, hash_length, |
| 4477 | &hmac_output_length); |
| 4478 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4479 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4480 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4481 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4482 | |
| 4483 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4484 | cleanup_status = psa_mac_abort(&hmac); |
| 4485 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4486 | status = cleanup_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4487 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4488 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4489 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4490 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4491 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4492 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4493 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4494 | psa_algorithm_t alg, |
| 4495 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4496 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4497 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4498 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 4499 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4500 | psa_status_t status; |
| 4501 | uint8_t offset, length; |
| 4502 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4503 | switch (tls12_prf->state) { |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4504 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4505 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4506 | break; |
| 4507 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4508 | break; |
| 4509 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4510 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4511 | } |
| 4512 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4513 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4514 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4515 | if (tls12_prf->left_in_block == 0) { |
| 4516 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 4517 | alg); |
| 4518 | if (status != PSA_SUCCESS) { |
| 4519 | return status; |
| 4520 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4521 | |
| 4522 | continue; |
| 4523 | } |
| 4524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4525 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4526 | length = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4527 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4528 | length = tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4529 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4530 | |
| 4531 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4533 | output += length; |
| 4534 | output_length -= length; |
| 4535 | tls12_prf->left_in_block -= length; |
| 4536 | } |
| 4537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4538 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4539 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4540 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4541 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4542 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4543 | psa_status_t psa_key_derivation_output_bytes( |
| 4544 | psa_key_derivation_operation_t *operation, |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4545 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4546 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4547 | { |
| 4548 | psa_status_t status; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4549 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4551 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4552 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4553 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4554 | /* This is a blank operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4555 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4558 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4559 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4560 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4561 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4562 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4563 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4564 | * output_length > 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4565 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4566 | } |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4567 | |
| 4568 | LOCAL_OUTPUT_ALLOC(output_external, output_length, output); |
| 4569 | if (output_length > operation->capacity) { |
| 4570 | operation->capacity = 0; |
| 4571 | /* Go through the error path to wipe all confidential data now |
| 4572 | * that the operation object is useless. */ |
| 4573 | status = PSA_ERROR_INSUFFICIENT_DATA; |
| 4574 | goto exit; |
| 4575 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4576 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4577 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4578 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4579 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4580 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4581 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg, |
| 4582 | output, output_length); |
| 4583 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4584 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4585 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4586 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4587 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4588 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4589 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 4590 | kdf_alg, output, |
| 4591 | output_length); |
| 4592 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4593 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4594 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4595 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4596 | (void) kdf_alg; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4597 | status = PSA_ERROR_BAD_STATE; |
| 4598 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4599 | |
| 4600 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4604 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4605 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4606 | * This allows us to differentiate between exhausted operations and |
| 4607 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4608 | * operations. */ |
| 4609 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4610 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4611 | operation->alg = alg; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4612 | if (output != NULL) { |
| 4613 | memset(output, '!', output_length); |
| 4614 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4615 | } |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4616 | |
| 4617 | LOCAL_OUTPUT_FREE(output_external, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4618 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4619 | } |
| 4620 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4621 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4622 | 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] | 4623 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4624 | if (data_size >= 8) { |
| 4625 | mbedtls_des_key_set_parity(data); |
| 4626 | } |
| 4627 | if (data_size >= 16) { |
| 4628 | mbedtls_des_key_set_parity(data + 8); |
| 4629 | } |
| 4630 | if (data_size >= 24) { |
| 4631 | mbedtls_des_key_set_parity(data + 16); |
| 4632 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4633 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4634 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4635 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4636 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4637 | psa_key_slot_t *slot, |
| 4638 | size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4639 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4640 | { |
| 4641 | uint8_t *data = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4642 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4643 | psa_status_t status; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4644 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4646 | if (!key_type_is_raw_bytes(slot->attr.type)) { |
| 4647 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4648 | } |
| 4649 | if (bits % 8 != 0) { |
| 4650 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4651 | } |
| 4652 | data = mbedtls_calloc(1, bytes); |
| 4653 | if (data == NULL) { |
| 4654 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4655 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4656 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4657 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 4658 | if (status != PSA_SUCCESS) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4659 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4660 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4661 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4662 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 4663 | psa_des_set_key_parity(data, bytes); |
| 4664 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4665 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4667 | status = psa_allocate_buffer_to_slot(slot, bytes); |
| 4668 | if (status != PSA_SUCCESS) { |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4669 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4670 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4671 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4672 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4673 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4674 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4675 | }; |
| 4676 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4677 | status = psa_driver_wrapper_import_key(&attributes, |
| 4678 | data, bytes, |
| 4679 | slot->key.data, |
| 4680 | slot->key.bytes, |
| 4681 | &slot->key.bytes, &bits); |
| 4682 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4683 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4684 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | |
| 4686 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4687 | mbedtls_free(data); |
| 4688 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4689 | } |
| 4690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4691 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 4692 | psa_key_derivation_operation_t *operation, |
| 4693 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4694 | { |
| 4695 | psa_status_t status; |
| 4696 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4697 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4698 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4699 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4700 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4701 | /* Reject any attempt to create a zero-length key so that we don't |
| 4702 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4703 | if (psa_get_key_bits(attributes) == 0) { |
| 4704 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4705 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4707 | if (operation->alg == PSA_ALG_NONE) { |
| 4708 | return PSA_ERROR_BAD_STATE; |
| 4709 | } |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4710 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4711 | if (!operation->can_output_key) { |
| 4712 | return PSA_ERROR_NOT_PERMITTED; |
| 4713 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4715 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 4716 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4717 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4718 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4719 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4720 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4721 | } |
| 4722 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4723 | if (status == PSA_SUCCESS) { |
| 4724 | status = psa_generate_derived_key_internal(slot, |
| 4725 | attributes->core.bits, |
| 4726 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4727 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4728 | if (status == PSA_SUCCESS) { |
| 4729 | status = psa_finish_key_creation(slot, driver, key); |
| 4730 | } |
| 4731 | if (status != PSA_SUCCESS) { |
| 4732 | psa_fail_key_creation(slot, driver); |
| 4733 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4735 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4736 | } |
| 4737 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4738 | |
| 4739 | |
| 4740 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4741 | /* Key derivation */ |
| 4742 | /****************************************************************/ |
| 4743 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4744 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4745 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4746 | { |
| 4747 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4748 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4749 | return 1; |
| 4750 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4751 | #endif |
| 4752 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4753 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4754 | return 1; |
| 4755 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4756 | #endif |
| 4757 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4758 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4759 | return 1; |
| 4760 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4761 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4762 | return 0; |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4765 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4766 | { |
| 4767 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 4769 | psa_hash_abort(&operation); |
| 4770 | return status; |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4771 | } |
| 4772 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4773 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4774 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4775 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4776 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4777 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4778 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4779 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4780 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4781 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4782 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 4783 | return PSA_ERROR_NOT_SUPPORTED; |
| 4784 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4785 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4786 | /* All currently supported key derivation algorithms are based on a |
| 4787 | * hash algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4788 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4789 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 4790 | if (hash_size == 0) { |
| 4791 | return PSA_ERROR_NOT_SUPPORTED; |
| 4792 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4793 | |
| 4794 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4795 | * we might fail later, which is somewhat unfriendly and potentially |
| 4796 | * risk-prone. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4797 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 4798 | if (status != PSA_SUCCESS) { |
| 4799 | return status; |
| 4800 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4802 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4803 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 4804 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 4805 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4806 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4807 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4808 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4809 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4810 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4812 | static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg) |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4813 | { |
| 4814 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4815 | if (alg == PSA_ALG_ECDH) { |
| 4816 | return PSA_SUCCESS; |
| 4817 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4818 | #endif |
| 4819 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4820 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4821 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4822 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4824 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 4825 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4826 | { |
| 4827 | psa_status_t status; |
| 4828 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4829 | if (operation->alg != 0) { |
| 4830 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4831 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4833 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 4834 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4835 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 4836 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4837 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 4838 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 4839 | status = psa_key_agreement_try_support(ka_alg); |
| 4840 | if (status != PSA_SUCCESS) { |
| 4841 | return status; |
| 4842 | } |
| 4843 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 4844 | #else |
| 4845 | return PSA_ERROR_NOT_SUPPORTED; |
| 4846 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4847 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 4848 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4849 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 4850 | #else |
| 4851 | return PSA_ERROR_NOT_SUPPORTED; |
| 4852 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4853 | } else { |
| 4854 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4855 | } |
| 4856 | |
| 4857 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4858 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4859 | } |
| 4860 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4861 | } |
| 4862 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4863 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4864 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 4865 | psa_algorithm_t hash_alg, |
| 4866 | psa_key_derivation_step_t step, |
| 4867 | const uint8_t *data, |
| 4868 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4869 | { |
| 4870 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4871 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4872 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4873 | if (hkdf->state != HKDF_STATE_INIT) { |
| 4874 | return PSA_ERROR_BAD_STATE; |
| 4875 | } else { |
| 4876 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4877 | hash_alg, |
| 4878 | data, data_length); |
| 4879 | if (status != PSA_SUCCESS) { |
| 4880 | return status; |
| 4881 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4882 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4883 | return PSA_SUCCESS; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4884 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4885 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4886 | /* If no salt was provided, use an empty salt. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4887 | if (hkdf->state == HKDF_STATE_INIT) { |
| 4888 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4889 | hash_alg, |
| 4890 | NULL, 0); |
| 4891 | if (status != PSA_SUCCESS) { |
| 4892 | return status; |
| 4893 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4894 | hkdf->state = HKDF_STATE_STARTED; |
| 4895 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4896 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 4897 | return PSA_ERROR_BAD_STATE; |
| 4898 | } |
| 4899 | status = psa_mac_update(&hkdf->hmac, |
| 4900 | data, data_length); |
| 4901 | if (status != PSA_SUCCESS) { |
| 4902 | return status; |
| 4903 | } |
| 4904 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4905 | hkdf->prk, |
| 4906 | sizeof(hkdf->prk), |
| 4907 | &data_length); |
| 4908 | if (status != PSA_SUCCESS) { |
| 4909 | return status; |
| 4910 | } |
| 4911 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4912 | hkdf->block_number = 0; |
| 4913 | hkdf->state = HKDF_STATE_KEYED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4914 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4915 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4916 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 4917 | return PSA_ERROR_BAD_STATE; |
| 4918 | } |
| 4919 | if (hkdf->info_set) { |
| 4920 | return PSA_ERROR_BAD_STATE; |
| 4921 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4922 | hkdf->info_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4923 | if (data_length != 0) { |
| 4924 | hkdf->info = mbedtls_calloc(1, data_length); |
| 4925 | if (hkdf->info == NULL) { |
| 4926 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4927 | } |
| 4928 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4929 | } |
| 4930 | hkdf->info_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4931 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4932 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4933 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4934 | } |
| 4935 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4936 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4937 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4938 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4939 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4940 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 4941 | const uint8_t *data, |
| 4942 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4943 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4944 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 4945 | return PSA_ERROR_BAD_STATE; |
| 4946 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4948 | if (data_length != 0) { |
| 4949 | prf->seed = mbedtls_calloc(1, data_length); |
| 4950 | if (prf->seed == NULL) { |
| 4951 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4952 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4954 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4955 | prf->seed_length = data_length; |
| 4956 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4957 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4958 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4959 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4960 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4963 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 4964 | const uint8_t *data, |
| 4965 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4966 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4967 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 4968 | return PSA_ERROR_BAD_STATE; |
| 4969 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4970 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4971 | if (data_length != 0) { |
| 4972 | prf->secret = mbedtls_calloc(1, data_length); |
| 4973 | if (prf->secret == NULL) { |
| 4974 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4975 | } |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4976 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4977 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4978 | prf->secret_length = data_length; |
| 4979 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4980 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4981 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4982 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4983 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4986 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 4987 | const uint8_t *data, |
| 4988 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4989 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4990 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 4991 | return PSA_ERROR_BAD_STATE; |
| 4992 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4994 | if (data_length != 0) { |
| 4995 | prf->label = mbedtls_calloc(1, data_length); |
| 4996 | if (prf->label == NULL) { |
| 4997 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4998 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5000 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5001 | prf->label_length = data_length; |
| 5002 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5003 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5004 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5005 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5006 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5007 | } |
| 5008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5009 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 5010 | psa_key_derivation_step_t step, |
| 5011 | const uint8_t *data, |
| 5012 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5013 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5014 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5015 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5016 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5017 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5018 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5019 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5020 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5021 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5022 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5023 | } |
| 5024 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5025 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5026 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5027 | |
| 5028 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5029 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5030 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5031 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5032 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5033 | { |
| 5034 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5035 | uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE]; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5036 | uint8_t *cur = pms; |
| 5037 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5038 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 5039 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5040 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5041 | |
| 5042 | /* Quoting RFC 4279, Section 2: |
| 5043 | * |
| 5044 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5045 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5046 | * uint16 with the value N, and the PSK itself. |
| 5047 | */ |
| 5048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5049 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 5050 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 5051 | memset(cur, 0, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5052 | cur += data_length; |
| 5053 | *cur++ = pms[0]; |
| 5054 | *cur++ = pms[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5055 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5056 | cur += data_length; |
| 5057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5058 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5060 | mbedtls_platform_zeroize(pms, sizeof(pms)); |
| 5061 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5062 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5063 | |
| 5064 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5065 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5066 | psa_key_derivation_step_t step, |
| 5067 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5068 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5069 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5070 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 5071 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 5072 | data, data_length); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5073 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5074 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5075 | return psa_tls12_prf_input(prf, step, data, data_length); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5076 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5077 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5078 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5079 | /** Check whether the given key type is acceptable for the given |
| 5080 | * input step of a key derivation. |
| 5081 | * |
| 5082 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5083 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5084 | * Both secret and non-secret inputs can alternatively have the type |
| 5085 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5086 | * that the input was passed as a buffer rather than via a key object. |
| 5087 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5088 | static int psa_key_derivation_check_input_type( |
| 5089 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5090 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5091 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5092 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5093 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5094 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 5095 | return PSA_SUCCESS; |
| 5096 | } |
| 5097 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 5098 | return PSA_SUCCESS; |
| 5099 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5100 | break; |
| 5101 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5102 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5103 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5104 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5105 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 5106 | return PSA_SUCCESS; |
| 5107 | } |
| 5108 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 5109 | return PSA_SUCCESS; |
| 5110 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5111 | break; |
| 5112 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5113 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5114 | } |
| 5115 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5116 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5117 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5118 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5119 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5120 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5121 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5122 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5123 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5124 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5126 | status = psa_key_derivation_check_input_type(step, key_type); |
| 5127 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5128 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5129 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5130 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5131 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5132 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 5133 | status = psa_hkdf_input(&operation->ctx.hkdf, |
| 5134 | PSA_ALG_HKDF_GET_HASH(kdf_alg), |
| 5135 | step, data, data_length); |
| 5136 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5137 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5138 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5139 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 5140 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 5141 | step, data, data_length); |
| 5142 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5143 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5144 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5145 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5146 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 5147 | step, data, data_length); |
| 5148 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5149 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5150 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5151 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5152 | (void) data; |
| 5153 | (void) data_length; |
| 5154 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5155 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5158 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5159 | if (status != PSA_SUCCESS) { |
| 5160 | psa_key_derivation_abort(operation); |
| 5161 | } |
| 5162 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5165 | psa_status_t psa_key_derivation_input_bytes( |
| 5166 | psa_key_derivation_operation_t *operation, |
| 5167 | psa_key_derivation_step_t step, |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5168 | const uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5169 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5170 | { |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5171 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5172 | LOCAL_INPUT_DECLARE(data_external, data); |
| 5173 | |
| 5174 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 5175 | |
| 5176 | status = psa_key_derivation_input_internal(operation, step, |
| 5177 | PSA_KEY_TYPE_NONE, |
| 5178 | data, data_length); |
Ryan Everett | d0d12fb | 2024-02-12 09:19:29 +0000 | [diff] [blame] | 5179 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5180 | exit: |
Ryan Everett | d0d12fb | 2024-02-12 09:19:29 +0000 | [diff] [blame] | 5181 | #endif |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5182 | LOCAL_INPUT_FREE(data_external, data); |
| 5183 | return status; |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5184 | } |
| 5185 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5186 | psa_status_t psa_key_derivation_input_key( |
| 5187 | psa_key_derivation_operation_t *operation, |
| 5188 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5189 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5190 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5191 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5192 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5193 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5194 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5195 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5196 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 5197 | if (status != PSA_SUCCESS) { |
| 5198 | psa_key_derivation_abort(operation); |
| 5199 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5200 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5201 | |
| 5202 | /* Passing a key object as a SECRET input unlocks the permission |
| 5203 | * to output to a key object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5204 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5205 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5206 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5208 | status = psa_key_derivation_input_internal(operation, |
| 5209 | step, slot->attr.type, |
| 5210 | slot->key.data, |
| 5211 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5213 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5215 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5216 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5217 | |
| 5218 | |
| 5219 | |
| 5220 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5221 | /* Key agreement */ |
| 5222 | /****************************************************************/ |
| 5223 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5224 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5225 | static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key, |
| 5226 | size_t peer_key_length, |
| 5227 | const mbedtls_ecp_keypair *our_key, |
| 5228 | uint8_t *shared_secret, |
| 5229 | size_t shared_secret_size, |
| 5230 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5231 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5232 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5233 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5234 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5235 | size_t bits = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5236 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 5237 | mbedtls_ecdh_init(&ecdh); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5238 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5239 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5240 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5241 | bits, |
| 5242 | peer_key, |
| 5243 | peer_key_length, |
| 5244 | &their_key); |
| 5245 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5246 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5247 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5248 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5249 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5250 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 5251 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5252 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5253 | } |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5254 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5255 | mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS)); |
| 5256 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5257 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5258 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5259 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5260 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5261 | mbedtls_ecdh_calc_secret(&ecdh, |
| 5262 | shared_secret_length, |
| 5263 | shared_secret, shared_secret_size, |
| 5264 | mbedtls_psa_get_random, |
| 5265 | MBEDTLS_PSA_RANDOM_STATE)); |
| 5266 | if (status != PSA_SUCCESS) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5267 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5268 | } |
| 5269 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5270 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5271 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5272 | |
| 5273 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5274 | if (status != PSA_SUCCESS) { |
| 5275 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 5276 | } |
| 5277 | mbedtls_ecdh_free(&ecdh); |
| 5278 | mbedtls_ecp_keypair_free(their_key); |
| 5279 | mbedtls_free(their_key); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5281 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5282 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5283 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5284 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5285 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5287 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 5288 | psa_key_slot_t *private_key, |
| 5289 | const uint8_t *peer_key, |
| 5290 | size_t peer_key_length, |
| 5291 | uint8_t *shared_secret, |
| 5292 | size_t shared_secret_size, |
| 5293 | size_t *shared_secret_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5294 | { |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5295 | mbedtls_ecp_keypair *ecp = NULL; |
| 5296 | psa_status_t status; |
Agathiyan Bragadeesh | e7eb805 | 2023-07-31 16:16:38 +0100 | [diff] [blame] | 5297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5298 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5299 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5300 | case PSA_ALG_ECDH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5301 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) { |
| 5302 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5303 | } |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5304 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5305 | private_key->attr.type, |
| 5306 | private_key->attr.bits, |
| 5307 | private_key->key.data, |
| 5308 | private_key->key.bytes, |
| 5309 | &ecp); |
| 5310 | if (status != PSA_SUCCESS) { |
| 5311 | return status; |
| 5312 | } |
| 5313 | status = psa_key_agreement_ecdh(peer_key, peer_key_length, |
| 5314 | ecp, |
| 5315 | shared_secret, shared_secret_size, |
| 5316 | shared_secret_length); |
| 5317 | mbedtls_ecp_keypair_free(ecp); |
| 5318 | mbedtls_free(ecp); |
| 5319 | return status; |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5320 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5321 | default: |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5322 | (void) ecp; |
| 5323 | (void) status; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5324 | (void) private_key; |
| 5325 | (void) peer_key; |
| 5326 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5327 | (void) shared_secret; |
| 5328 | (void) shared_secret_size; |
| 5329 | (void) shared_secret_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5330 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5331 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5332 | } |
| 5333 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5334 | /* 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] | 5335 | * to potentially free embedded data structures and wipe confidential data. |
| 5336 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5337 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 5338 | psa_key_derivation_step_t step, |
| 5339 | psa_key_slot_t *private_key, |
| 5340 | const uint8_t *peer_key, |
| 5341 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5342 | { |
| 5343 | psa_status_t status; |
| 5344 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5345 | size_t shared_secret_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5346 | 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] | 5347 | |
| 5348 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5349 | * secret. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5350 | status = psa_key_agreement_raw_internal(ka_alg, |
| 5351 | private_key, |
| 5352 | peer_key, peer_key_length, |
| 5353 | shared_secret, |
| 5354 | sizeof(shared_secret), |
| 5355 | &shared_secret_length); |
| 5356 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5357 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5358 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5359 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5360 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5361 | * the shared secret. A shared secret is permitted wherever a key |
| 5362 | * of type DERIVE is permitted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5363 | status = psa_key_derivation_input_internal(operation, step, |
| 5364 | PSA_KEY_TYPE_DERIVE, |
| 5365 | shared_secret, |
| 5366 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5367 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5368 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 5369 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5370 | } |
| 5371 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5372 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 5373 | psa_key_derivation_step_t step, |
| 5374 | mbedtls_svc_key_id_t private_key, |
| 5375 | const uint8_t *peer_key, |
| 5376 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5377 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5378 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5379 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5380 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5382 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5383 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5384 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5385 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5386 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 5387 | if (status != PSA_SUCCESS) { |
| 5388 | return status; |
| 5389 | } |
| 5390 | status = psa_key_agreement_internal(operation, step, |
| 5391 | slot, |
| 5392 | peer_key, peer_key_length); |
| 5393 | if (status != PSA_SUCCESS) { |
| 5394 | psa_key_derivation_abort(operation); |
| 5395 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5396 | /* If a private key has been added as SECRET, we allow the derived |
| 5397 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5398 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5399 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5400 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5401 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5402 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5403 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5405 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5406 | } |
| 5407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5408 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 5409 | mbedtls_svc_key_id_t private_key, |
| 5410 | const uint8_t *peer_key, |
| 5411 | size_t peer_key_length, |
| 5412 | uint8_t *output, |
| 5413 | size_t output_size, |
| 5414 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5415 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5416 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5417 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5418 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5419 | size_t expected_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5421 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5422 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5423 | goto exit; |
| 5424 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5425 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5426 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 5427 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5428 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5429 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5430 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5431 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5432 | * for the output size. The PSA specification only guarantees that this |
| 5433 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5434 | * but it might be nice to allow smaller buffers if the output fits. |
| 5435 | * At the time of writing this comment, with only ECDH implemented, |
| 5436 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5437 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5438 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5439 | expected_length = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5440 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 5441 | if (output_size < expected_length) { |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5442 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5443 | goto exit; |
| 5444 | } |
| 5445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5446 | status = psa_key_agreement_raw_internal(alg, slot, |
| 5447 | peer_key, peer_key_length, |
| 5448 | output, output_size, |
| 5449 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5450 | |
| 5451 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5452 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5453 | /* If an error happens and is not handled properly, the output |
| 5454 | * may be used as a key to protect sensitive data. Arrange for such |
| 5455 | * a key to be random, which is likely to result in decryption or |
| 5456 | * verification errors. This is better than filling the buffer with |
| 5457 | * some constant data such as zeros, which would result in the data |
| 5458 | * being protected with a reproducible, easily knowable key. |
| 5459 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5460 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5461 | *output_length = output_size; |
| 5462 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5463 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5464 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5466 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5467 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5468 | |
| 5469 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5470 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5471 | /****************************************************************/ |
| 5472 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5473 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5474 | |
Gilles Peskine | 9a9d5ee | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 5475 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5476 | #include "mbedtls/entropy_poll.h" |
| 5477 | #endif |
| 5478 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5479 | /** Initialize the PSA random generator. |
| 5480 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5481 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5482 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5483 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5484 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5485 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5486 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5487 | /* Set default configuration if |
| 5488 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5489 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5490 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5491 | } |
| 5492 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5493 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5494 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5496 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5497 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5498 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5499 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5500 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5501 | mbedtls_entropy_add_source(&rng->entropy, |
| 5502 | mbedtls_nv_seed_poll, NULL, |
| 5503 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5504 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5505 | #endif |
| 5506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5507 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5508 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | /** Deinitialize the PSA random generator. |
| 5512 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5513 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5514 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5515 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5516 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5517 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5518 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 5519 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5520 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | /** Seed the PSA random generator. |
| 5524 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5525 | 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] | 5526 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5527 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5528 | /* Do nothing: the external RNG seeds itself. */ |
| 5529 | (void) rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5530 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5531 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5532 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5533 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 5534 | drbg_seed, sizeof(drbg_seed) - 1); |
| 5535 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5536 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5537 | } |
| 5538 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5539 | psa_status_t psa_generate_random(uint8_t *output, |
| 5540 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5541 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5542 | GUARD_MODULE_INITIALIZED; |
| 5543 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5544 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5545 | |
| 5546 | size_t output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5547 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 5548 | output, output_size, |
| 5549 | &output_length); |
| 5550 | if (status != PSA_SUCCESS) { |
| 5551 | return status; |
| 5552 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5553 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 5554 | * for the external RNG interface. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5555 | if (output_length != output_size) { |
| 5556 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 5557 | } |
| 5558 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5559 | |
| 5560 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5561 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5562 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5563 | size_t request_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5564 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5565 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5566 | output_size); |
| 5567 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 5568 | output, request_size); |
| 5569 | if (ret != 0) { |
| 5570 | return mbedtls_to_psa_error(ret); |
| 5571 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5572 | output_size -= request_size; |
| 5573 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5574 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5575 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5576 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5579 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5580 | * |
| 5581 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5582 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5583 | * PSA API doesn't support passing an explicit state. |
| 5584 | * |
| 5585 | * In the non-external case, psa_generate_random() calls an |
| 5586 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5587 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5588 | * instead of doing this back-and-forth between the PSA API and the |
| 5589 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5590 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5591 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5592 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5593 | int mbedtls_psa_get_random(void *p_rng, |
| 5594 | unsigned char *output, |
| 5595 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5596 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5597 | /* This function takes a pointer to the RNG state because that's what |
| 5598 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5599 | * its own state internally and doesn't let the caller access that state. |
| 5600 | * So we just ignore the state parameter, and in practice we'll pass |
| 5601 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5602 | (void) p_rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5603 | psa_status_t status = psa_generate_random(output, output_size); |
| 5604 | if (status == PSA_SUCCESS) { |
| 5605 | return 0; |
| 5606 | } else { |
| 5607 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 5608 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5609 | } |
| 5610 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5611 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5612 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5613 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 5614 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5615 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5616 | if (global_data.initialized) { |
| 5617 | return PSA_ERROR_NOT_PERMITTED; |
| 5618 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5620 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 5621 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 5622 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 5623 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5624 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5626 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5627 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5628 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5629 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5630 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5631 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5632 | * \param type The key type |
| 5633 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5634 | * |
| 5635 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5636 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5637 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5638 | * The size in bits of the key is not valid. |
| 5639 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5640 | * The type and/or the size in bits of the key or the combination of |
| 5641 | * the two is not supported. |
| 5642 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5643 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5644 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5645 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5646 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5648 | if (key_type_is_raw_bytes(type)) { |
| 5649 | status = validate_unstructured_key_bit_size(type, bits); |
| 5650 | if (status != PSA_SUCCESS) { |
| 5651 | return status; |
| 5652 | } |
| 5653 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5654 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5655 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5656 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 5657 | return PSA_ERROR_NOT_SUPPORTED; |
| 5658 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5659 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5660 | /* Accept only byte-aligned keys, for the same reasons as |
| 5661 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5662 | if (bits % 8 != 0) { |
| 5663 | return PSA_ERROR_NOT_SUPPORTED; |
| 5664 | } |
| 5665 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5666 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5667 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5668 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5669 | 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] | 5670 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5671 | return PSA_SUCCESS; |
| 5672 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5673 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5674 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5675 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5676 | } |
| 5677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5678 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5679 | } |
| 5680 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5681 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5682 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5683 | 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] | 5684 | { |
| 5685 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5686 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5688 | if ((attributes->domain_parameters == NULL) && |
| 5689 | (attributes->domain_parameters_size != 0)) { |
| 5690 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5691 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5693 | if (key_type_is_raw_bytes(type)) { |
| 5694 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 5695 | if (status != PSA_SUCCESS) { |
| 5696 | return status; |
| 5697 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5698 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5699 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5700 | if (type == PSA_KEY_TYPE_DES) { |
| 5701 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 5702 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5703 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5704 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5705 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5706 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5707 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5708 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 5709 | return mbedtls_psa_rsa_generate_key(attributes, |
| 5710 | key_buffer, |
| 5711 | key_buffer_size, |
| 5712 | key_buffer_length); |
| 5713 | } else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5714 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5715 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5716 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5717 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5718 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5719 | return mbedtls_psa_ecp_generate_key(attributes, |
| 5720 | key_buffer, |
| 5721 | key_buffer_size, |
| 5722 | key_buffer_length); |
| 5723 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5724 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5725 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5726 | (void) key_buffer_length; |
| 5727 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5728 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5729 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5730 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5731 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5732 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5733 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 5734 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5735 | { |
| 5736 | psa_status_t status; |
| 5737 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5738 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5739 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5740 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5741 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5742 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5743 | /* Reject any attempt to create a zero-length key so that we don't |
| 5744 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5745 | if (psa_get_key_bits(attributes) == 0) { |
| 5746 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5747 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5748 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5749 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5750 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 5751 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5752 | } |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5753 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5754 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 5755 | &slot, &driver); |
| 5756 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5757 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5758 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5759 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5760 | /* In the case of a transparent key or an opaque key stored in local |
| 5761 | * storage (thus not in the case of generating a key in a secure element |
| 5762 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5763 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5764 | if (slot->key.data == NULL) { |
| 5765 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 5766 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5767 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5768 | attributes->core.type, attributes->core.bits); |
| 5769 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5770 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5771 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5772 | |
| 5773 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5774 | attributes->core.type, |
| 5775 | attributes->core.bits); |
| 5776 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5777 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5778 | attributes, &key_buffer_size); |
| 5779 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5780 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5781 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5782 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5783 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5784 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 5785 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5786 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5787 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5788 | } |
| 5789 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5790 | status = psa_driver_wrapper_generate_key(attributes, |
| 5791 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5793 | if (status != PSA_SUCCESS) { |
| 5794 | psa_remove_key_data_from_memory(slot); |
| 5795 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5796 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5797 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5798 | if (status == PSA_SUCCESS) { |
| 5799 | status = psa_finish_key_creation(slot, driver, key); |
| 5800 | } |
| 5801 | if (status != PSA_SUCCESS) { |
| 5802 | psa_fail_key_creation(slot, driver); |
| 5803 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5805 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5806 | } |
| 5807 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5808 | /****************************************************************/ |
| 5809 | /* Module setup */ |
| 5810 | /****************************************************************/ |
| 5811 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5812 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5813 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5814 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 5815 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5816 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5817 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5818 | return PSA_ERROR_BAD_STATE; |
| 5819 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5820 | global_data.rng.entropy_init = entropy_init; |
| 5821 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5822 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5823 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5824 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5826 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5827 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5828 | psa_wipe_all_key_slots(); |
| 5829 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5830 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5831 | } |
| 5832 | /* Wipe all remaining data, including configuration. |
| 5833 | * In particular, this sets all state indicator to the value |
| 5834 | * indicating "uninitialized". */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5835 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5836 | |
| 5837 | /* Terminate drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5838 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5839 | } |
| 5840 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5841 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5842 | /** Recover a transaction that was interrupted by a power failure. |
| 5843 | * |
| 5844 | * This function is called during initialization, before psa_crypto_init() |
| 5845 | * returns. If this function returns a failure status, the initialization |
| 5846 | * fails. |
| 5847 | */ |
| 5848 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5849 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5850 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5851 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5852 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5853 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5854 | /* TODO - fall through to the failure case until this |
| 5855 | * is implemented. |
| 5856 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5857 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5858 | default: |
| 5859 | /* We found an unsupported transaction in the storage. |
| 5860 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5861 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5862 | } |
| 5863 | } |
| 5864 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5865 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5866 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5867 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5868 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5869 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5870 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5871 | if (global_data.initialized != 0) { |
| 5872 | return PSA_SUCCESS; |
| 5873 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5875 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5876 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5877 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5878 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 5879 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5880 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5881 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5882 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5884 | status = psa_initialize_key_slots(); |
| 5885 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5886 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5887 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5888 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5889 | /* Init drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5890 | status = psa_driver_wrapper_init(); |
| 5891 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5892 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5893 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5894 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5895 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5896 | status = psa_crypto_load_transaction(); |
| 5897 | if (status == PSA_SUCCESS) { |
| 5898 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 5899 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5900 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5901 | } |
| 5902 | status = psa_crypto_stop_transaction(); |
| 5903 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5904 | /* There's no transaction to complete. It's all good. */ |
| 5905 | status = PSA_SUCCESS; |
| 5906 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5907 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5908 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5909 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5910 | global_data.initialized = 1; |
| 5911 | |
| 5912 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5913 | if (status != PSA_SUCCESS) { |
| 5914 | mbedtls_psa_crypto_free(); |
| 5915 | } |
| 5916 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5917 | } |
| 5918 | |
David Horstmann | 114d824 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 5919 | /* Memory copying test hooks. These are called before input copy, after input |
| 5920 | * copy, before output copy and after output copy, respectively. |
| 5921 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 5922 | * while they are copied. */ |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5923 | #if defined(MBEDTLS_TEST_HOOKS) |
| 5924 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5925 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5926 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5927 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5928 | #endif |
| 5929 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5930 | /** Copy from an input buffer to a local copy. |
| 5931 | * |
| 5932 | * \param[in] input Pointer to input buffer. |
| 5933 | * \param[in] input_len Length of the input buffer. |
| 5934 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 5935 | * \param[out] input_copy_len Length of the local copy buffer. |
| 5936 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 5937 | * copied. |
| 5938 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 5939 | * copy is too small to hold contents of the |
| 5940 | * input buffer. |
| 5941 | */ |
| 5942 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5943 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 5944 | uint8_t *input_copy, size_t input_copy_len) |
| 5945 | { |
| 5946 | if (input_len > input_copy_len) { |
David Horstmann | 8684ad5 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 5947 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5950 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5951 | if (psa_input_pre_copy_hook != NULL) { |
| 5952 | psa_input_pre_copy_hook(input, input_len); |
| 5953 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5954 | #endif |
| 5955 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5956 | if (input_len > 0) { |
| 5957 | memcpy(input_copy, input, input_len); |
| 5958 | } |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5959 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5960 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5961 | if (psa_input_post_copy_hook != NULL) { |
| 5962 | psa_input_post_copy_hook(input, input_len); |
| 5963 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5964 | #endif |
| 5965 | |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5966 | return PSA_SUCCESS; |
| 5967 | } |
| 5968 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5969 | /** Copy from a local output buffer into a user-supplied one. |
| 5970 | * |
| 5971 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 5972 | * \param[in] output_copy_len Length of the local buffer. |
| 5973 | * \param[out] output Pointer to user-supplied output buffer. |
| 5974 | * \param[out] output_len Length of the user-supplied output buffer. |
| 5975 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 5976 | * copied. |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 5977 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5978 | * user-supplied output buffer is too small to |
| 5979 | * hold the contents of the local buffer. |
| 5980 | */ |
| 5981 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 5982 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 5983 | uint8_t *output, size_t output_len) |
| 5984 | { |
| 5985 | if (output_len < output_copy_len) { |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 5986 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 5987 | } |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5988 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5989 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5990 | if (psa_output_pre_copy_hook != NULL) { |
| 5991 | psa_output_pre_copy_hook(output, output_len); |
| 5992 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5993 | #endif |
| 5994 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5995 | if (output_copy_len > 0) { |
| 5996 | memcpy(output, output_copy, output_copy_len); |
| 5997 | } |
| 5998 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5999 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 6000 | if (psa_output_post_copy_hook != NULL) { |
| 6001 | psa_output_post_copy_hook(output, output_len); |
| 6002 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6003 | #endif |
| 6004 | |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 6005 | return PSA_SUCCESS; |
| 6006 | } |
| 6007 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6008 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 6009 | psa_crypto_local_input_t *local_input) |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6010 | { |
| 6011 | psa_status_t status; |
| 6012 | |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 6013 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6014 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 6015 | if (input_len == 0) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6016 | return PSA_SUCCESS; |
| 6017 | } |
| 6018 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6019 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 6020 | if (local_input->buffer == NULL) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6021 | /* Since we dealt with the zero-length case above, we know that |
| 6022 | * a NULL return value means a failure of allocation. */ |
| 6023 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6024 | } |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6025 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6026 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6027 | local_input->length = input_len; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6028 | |
| 6029 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6030 | local_input->buffer, local_input->length); |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6031 | if (status != PSA_SUCCESS) { |
| 6032 | goto error; |
| 6033 | } |
| 6034 | |
| 6035 | return PSA_SUCCESS; |
| 6036 | |
| 6037 | error: |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6038 | mbedtls_free(local_input->buffer); |
| 6039 | local_input->buffer = NULL; |
| 6040 | local_input->length = 0; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6041 | return status; |
| 6042 | } |
| 6043 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6044 | void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input) |
David Horstmann | fa2d75d | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 6045 | { |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6046 | mbedtls_free(local_input->buffer); |
| 6047 | local_input->buffer = NULL; |
| 6048 | local_input->length = 0; |
David Horstmann | fa2d75d | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 6049 | } |
| 6050 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6051 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 6052 | psa_crypto_local_output_t *local_output) |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6053 | { |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 6054 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6055 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 6056 | if (output_len == 0) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6057 | return PSA_SUCCESS; |
| 6058 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6059 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 6060 | if (local_output->buffer == NULL) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6061 | /* Since we dealt with the zero-length case above, we know that |
| 6062 | * a NULL return value means a failure of allocation. */ |
| 6063 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6064 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6065 | local_output->length = output_len; |
| 6066 | local_output->original = output; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6067 | |
| 6068 | return PSA_SUCCESS; |
| 6069 | } |
| 6070 | |
Gabor Mezei | 3b0c371 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 6071 | psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len, |
| 6072 | psa_crypto_local_output_t *local_output) |
| 6073 | { |
| 6074 | psa_status_t status; |
| 6075 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
| 6076 | |
| 6077 | if (output_len == 0) { |
| 6078 | return PSA_SUCCESS; |
| 6079 | } |
| 6080 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 6081 | if (local_output->buffer == NULL) { |
| 6082 | /* Since we dealt with the zero-length case above, we know that |
| 6083 | * a NULL return value means a failure of allocation. */ |
| 6084 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6085 | } |
| 6086 | local_output->length = output_len; |
| 6087 | local_output->original = output; |
| 6088 | |
| 6089 | status = psa_crypto_copy_input(output, output_len, |
| 6090 | local_output->buffer, local_output->length); |
| 6091 | if (status != PSA_SUCCESS) { |
| 6092 | goto error; |
| 6093 | } |
| 6094 | |
| 6095 | return PSA_SUCCESS; |
| 6096 | |
| 6097 | error: |
| 6098 | mbedtls_free(local_output->buffer); |
| 6099 | local_output->buffer = NULL; |
| 6100 | local_output->length = 0; |
| 6101 | return status; |
| 6102 | } |
| 6103 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6104 | psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output) |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6105 | { |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 6106 | psa_status_t status; |
| 6107 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6108 | if (local_output->buffer == NULL) { |
| 6109 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6110 | return PSA_SUCCESS; |
| 6111 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6112 | if (local_output->original == NULL) { |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6113 | /* We have an internal copy but nothing to copy back to. */ |
| 6114 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 6115 | } |
| 6116 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6117 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 6118 | local_output->original, local_output->length); |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 6119 | if (status != PSA_SUCCESS) { |
| 6120 | return status; |
| 6121 | } |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6122 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6123 | mbedtls_free(local_output->buffer); |
| 6124 | local_output->buffer = NULL; |
| 6125 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6126 | |
| 6127 | return PSA_SUCCESS; |
| 6128 | } |
| 6129 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6130 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |