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 | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 109 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_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 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 185 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 186 | * after first copying back its contents to the original buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 187 | * |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 188 | * Assumptions: |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 189 | * - psa_status_t status exists |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 190 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 191 | * - output is the name of the original buffer that was copied |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 192 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 193 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 194 | output_copy = NULL; \ |
David Horstmann | 2b70a66 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 195 | do { \ |
| 196 | psa_status_t local_output_status; \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 197 | 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] | 198 | if (local_output_status != PSA_SUCCESS) { \ |
| 199 | /* Since this error case is an internal error, it's more serious than \ |
| 200 | * any existing error code and so it's fine to overwrite the existing \ |
| 201 | * status. */ \ |
| 202 | status = local_output_status; \ |
| 203 | } \ |
| 204 | } while (0) |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 205 | #else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 206 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 207 | const uint8_t *input_copy_name = NULL; |
| 208 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 209 | input_copy = input; |
| 210 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 211 | input_copy = NULL; |
| 212 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 213 | uint8_t *output_copy_name = NULL; |
| 214 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 215 | output_copy = output; |
| 216 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 217 | output_copy = NULL; |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 218 | #endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 221 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 222 | /* Mbed TLS error codes can combine a high-level error code and a |
| 223 | * low-level error code. The low-level error usually reflects the |
| 224 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | int low_level_ret = -(-ret & 0x007f); |
| 226 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 227 | case 0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | |
| 230 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 231 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 232 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 237 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 240 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 241 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 242 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 243 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 244 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 246 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 248 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 250 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 251 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 252 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 253 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 254 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 255 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 256 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 259 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 260 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 261 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 262 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 263 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 264 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 265 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 266 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 267 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 270 | |
| 271 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 273 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 277 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 278 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 280 | |
| 281 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 283 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 285 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 288 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 290 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 292 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 294 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 298 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 300 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 302 | |
| 303 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 304 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 307 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 308 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 309 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 310 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 312 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 313 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 317 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 318 | |
| 319 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 321 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 322 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 323 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 324 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 325 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 326 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 328 | |
| 329 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 331 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 332 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 333 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 334 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 335 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 336 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 338 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 339 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 340 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 342 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 343 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 345 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 347 | #endif |
| 348 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 349 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 350 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 351 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 352 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 353 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 354 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 355 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 356 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 358 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 360 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | return PSA_ERROR_STORAGE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 362 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 364 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 365 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 366 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 367 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 369 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 370 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 371 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 373 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 375 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 379 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 381 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 382 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 384 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 385 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 387 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 389 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 390 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 392 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 394 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 395 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 396 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 397 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 399 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 400 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 401 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 403 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 405 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 407 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 408 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 410 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 412 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 413 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 415 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 416 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 418 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 420 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 421 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 422 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 424 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 425 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 427 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 429 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 431 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 433 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 435 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 437 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 438 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 439 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 440 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 441 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 442 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 443 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 445 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 447 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 448 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 449 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 451 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 453 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 455 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 456 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 458 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 459 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 460 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 462 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 463 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 464 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 465 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 467 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 468 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 473 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 474 | |
| 475 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 476 | /****************************************************************/ |
| 477 | /* Key management */ |
| 478 | /****************************************************************/ |
| 479 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 480 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 481 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 482 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 483 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 484 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 485 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 486 | size_t bits, |
| 487 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 488 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 489 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 490 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 491 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 492 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 493 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 494 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 495 | #endif |
| 496 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 497 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 498 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 499 | #endif |
| 500 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 501 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 503 | #endif |
| 504 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 505 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 506 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 507 | #endif |
| 508 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 509 | case 521: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 511 | case 528: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 512 | if (bits_is_sloppy) { |
| 513 | return MBEDTLS_ECP_DP_SECP521R1; |
| 514 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 515 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 516 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 517 | } |
| 518 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 519 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 520 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 522 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 523 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 524 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 525 | #endif |
| 526 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 527 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 528 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 529 | #endif |
| 530 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 531 | case 512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 532 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 533 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 534 | } |
| 535 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 536 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 537 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 538 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 539 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 540 | case 255: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 541 | return MBEDTLS_ECP_DP_CURVE25519; |
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 | if (bits_is_sloppy) { |
| 544 | return MBEDTLS_ECP_DP_CURVE25519; |
| 545 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 546 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 547 | #endif |
| 548 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 549 | case 448: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 550 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 551 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 552 | } |
| 553 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 554 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 555 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 556 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 557 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 558 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 560 | #endif |
| 561 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 562 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 563 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 564 | #endif |
| 565 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 566 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 568 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 569 | } |
| 570 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 571 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 572 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 573 | (void) bits_is_sloppy; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 574 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 575 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 576 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 577 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 578 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 579 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 580 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 581 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type, |
| 583 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 584 | { |
| 585 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 586 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 587 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 588 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 589 | case PSA_KEY_TYPE_DERIVE: |
| 590 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 591 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 592 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 593 | if (bits != 128 && bits != 192 && bits != 256) { |
| 594 | return PSA_ERROR_INVALID_ARGUMENT; |
| 595 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 596 | break; |
| 597 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 598 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 599 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 600 | if (bits != 128 && bits != 192 && bits != 256) { |
| 601 | return PSA_ERROR_INVALID_ARGUMENT; |
| 602 | } |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 603 | break; |
| 604 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 605 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 606 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 607 | if (bits != 128 && bits != 192 && bits != 256) { |
| 608 | return PSA_ERROR_INVALID_ARGUMENT; |
| 609 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 610 | break; |
| 611 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 612 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 613 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 614 | if (bits != 64 && bits != 128 && bits != 192) { |
| 615 | return PSA_ERROR_INVALID_ARGUMENT; |
| 616 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 617 | break; |
| 618 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 619 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 620 | case PSA_KEY_TYPE_ARC4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | if (bits < 8 || bits > 2048) { |
| 622 | return PSA_ERROR_INVALID_ARGUMENT; |
| 623 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 624 | break; |
| 625 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 626 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 627 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | if (bits != 256) { |
| 629 | return PSA_ERROR_INVALID_ARGUMENT; |
| 630 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 631 | break; |
| 632 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 633 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 634 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 635 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 636 | if (bits % 8 != 0) { |
| 637 | return PSA_ERROR_INVALID_ARGUMENT; |
| 638 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 639 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 640 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 643 | /** 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] | 644 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 645 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 646 | * 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] | 647 | * |
| 648 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 649 | * \param[in] key_type The key type of the key to be used with the |
| 650 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 651 | * |
| 652 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 653 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 654 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 655 | * 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] | 656 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 657 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 658 | psa_algorithm_t algorithm, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 659 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 660 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 661 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 662 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 663 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 664 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 665 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 667 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 668 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 669 | * key. */ |
| 670 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 671 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 672 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 673 | * the block length (larger than 1) for block ciphers. */ |
| 674 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 675 | return PSA_SUCCESS; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 683 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 684 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 685 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 686 | if (slot->key.data != NULL) { |
| 687 | return PSA_ERROR_ALREADY_EXISTS; |
| 688 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 689 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 690 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 691 | if (slot->key.data == NULL) { |
| 692 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 693 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 694 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 695 | slot->key.bytes = buffer_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 696 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 699 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 700 | const uint8_t *data, |
| 701 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 702 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 703 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 704 | data_length); |
| 705 | if (status != PSA_SUCCESS) { |
| 706 | return status; |
| 707 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 708 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | memcpy(slot->key.data, data, data_length); |
| 710 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 711 | } |
| 712 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 713 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 714 | const psa_key_attributes_t *attributes, |
| 715 | const uint8_t *data, size_t data_length, |
| 716 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 718 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 719 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 720 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 721 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 722 | /* zero-length keys are never supported. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 723 | if (data_length == 0) { |
| 724 | return PSA_ERROR_NOT_SUPPORTED; |
| 725 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 727 | if (key_type_is_raw_bytes(type)) { |
| 728 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 729 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 730 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 731 | if (data_length > SIZE_MAX / 8) { |
| 732 | return PSA_ERROR_NOT_SUPPORTED; |
| 733 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 734 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 735 | /* Enforce a size limit, and in particular ensure that the bit |
| 736 | * size fits in its representation type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 737 | if ((*bits) > PSA_MAX_KEY_BITS) { |
| 738 | return PSA_ERROR_NOT_SUPPORTED; |
| 739 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 740 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 741 | status = validate_unstructured_key_bit_size(type, *bits); |
| 742 | if (status != PSA_SUCCESS) { |
| 743 | return status; |
| 744 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 745 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 746 | /* Copy the key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 747 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 748 | *key_buffer_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 749 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 751 | return PSA_SUCCESS; |
| 752 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 753 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 754 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 755 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 756 | return mbedtls_psa_ecp_import_key(attributes, |
| 757 | data, data_length, |
| 758 | key_buffer, key_buffer_size, |
| 759 | key_buffer_length, |
| 760 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 761 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 762 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 763 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 764 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 765 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 766 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 767 | return mbedtls_psa_rsa_import_key(attributes, |
| 768 | data, data_length, |
| 769 | key_buffer, key_buffer_size, |
| 770 | key_buffer_length, |
| 771 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 772 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 773 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 774 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 775 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 776 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 777 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 780 | /** Calculate the intersection of two algorithm usage policies. |
| 781 | * |
| 782 | * Return 0 (which allows no operation) on incompatibility. |
| 783 | */ |
| 784 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 785 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 786 | psa_algorithm_t alg1, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 787 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 788 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 789 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 790 | if (alg1 == alg2) { |
| 791 | return alg1; |
| 792 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 793 | /* If the policies are from the same hash-and-sign family, check |
| 794 | * 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] | 795 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 796 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 797 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 798 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 799 | return alg2; |
| 800 | } |
| 801 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 802 | return alg1; |
| 803 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 804 | } |
| 805 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 806 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 807 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 808 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 809 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 810 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 811 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 812 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 813 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 814 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 815 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 816 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 817 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 818 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 819 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 820 | } |
| 821 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 822 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 823 | (alg1_len <= alg2_len)) { |
| 824 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 825 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 826 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 827 | (alg2_len <= alg1_len)) { |
| 828 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | /* If the policies are from the same MAC family, check whether one |
| 832 | * of them is a minimum-MAC-length policy. Calculate the most |
| 833 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 834 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 835 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 836 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 837 | /* Validate the combination of key type and algorithm. Since the base |
| 838 | * 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] | 839 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 840 | return 0; |
| 841 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 842 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 843 | /* Get the (exact or at-least) output lengths for both sides of the |
| 844 | * requested intersection. None of the currently supported algorithms |
| 845 | * have an output length dependent on the actual key size, so setting it |
| 846 | * to a bogus value of 0 is currently OK. |
| 847 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 848 | * Note that for at-least-this-length wildcard algorithms, the output |
| 849 | * length is set to the shortest allowed length, which allows us to |
| 850 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 851 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 852 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 853 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 854 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 855 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 856 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 857 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 858 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 859 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 860 | |
| 861 | /* If only one is an at-least-this-length policy, the intersection would |
| 862 | * be the other (fixed-length) policy as long as said fixed length is |
| 863 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 864 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 865 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 866 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 867 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 868 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 869 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 870 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 871 | /* If none of them are wildcards, check whether they define the same tag |
| 872 | * length. This is still possible here when one is default-length and |
| 873 | * the other specific-length. Ensure to always return the |
| 874 | * specific-length version for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 875 | if (alg1_len == alg2_len) { |
| 876 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 877 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 878 | } |
| 879 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 880 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 883 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 884 | psa_algorithm_t policy_alg, |
| 885 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 886 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 887 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 888 | if (requested_alg == policy_alg) { |
| 889 | return 1; |
| 890 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 891 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 892 | * and requested_alg is the same hash-and-sign family with any hash, |
| 893 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 894 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 895 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 896 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 897 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 898 | } |
| 899 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 900 | * the requested algorithm, check the requested tag length to be |
| 901 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 902 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 903 | PSA_ALG_IS_AEAD(requested_alg) && |
| 904 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 905 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 906 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 907 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 908 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 909 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 910 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 911 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 912 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 913 | PSA_ALG_IS_MAC(requested_alg) && |
| 914 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 915 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 916 | /* Validate the combination of key type and algorithm. Since the policy |
| 917 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 918 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 919 | return 0; |
| 920 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 921 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 922 | /* Get both the requested output length for the algorithm which is to be |
| 923 | * verified, and the default output length for the base algorithm. |
| 924 | * Note that none of the currently supported algorithms have an output |
| 925 | * length dependent on actual key size, so setting it to a bogus value |
| 926 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 927 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 928 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 929 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 930 | key_type, 0, |
| 931 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 932 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 933 | /* If the policy is default-length, only allow an algorithm with |
| 934 | * a declared exact-length matching the default. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 935 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 936 | return requested_output_length == default_output_length; |
| 937 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 938 | |
| 939 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 940 | * length exactly matches the default length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 941 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 942 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 943 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 944 | } |
| 945 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 946 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 947 | * check for the requested MAC length to be equal to or longer than the |
| 948 | * minimum allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 949 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 950 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 951 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 952 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 953 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 954 | /* 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] | 955 | * a key derivation with that key agreement should also be allowed. This |
| 956 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 957 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 958 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 959 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 960 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 961 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 962 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 963 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 966 | /** Test whether a policy permits an algorithm. |
| 967 | * |
| 968 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 969 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 970 | * \note This function requires providing the key type for which the policy is |
| 971 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 972 | * have different properties depending on what kind of cipher it is |
| 973 | * combined with. |
| 974 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 975 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 976 | * allowed by the \p policy. |
| 977 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 978 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 979 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 980 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 981 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 982 | psa_key_type_t key_type, |
| 983 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 984 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 985 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 986 | if (alg == 0) { |
| 987 | return PSA_ERROR_INVALID_ARGUMENT; |
| 988 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 989 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 990 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 991 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 992 | return PSA_ERROR_INVALID_ARGUMENT; |
| 993 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 995 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 996 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 997 | return PSA_SUCCESS; |
| 998 | } else { |
| 999 | return PSA_ERROR_NOT_PERMITTED; |
| 1000 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1003 | /** Restrict a key policy based on a constraint. |
| 1004 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 1005 | * \note This function requires providing the key type for which the policy is |
| 1006 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 1007 | * have different properties depending on what kind of cipher it is |
| 1008 | * combined with. |
| 1009 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1010 | * \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] | 1011 | * \param[in,out] policy The policy to restrict. |
| 1012 | * \param[in] constraint The policy constraint to apply. |
| 1013 | * |
| 1014 | * \retval #PSA_SUCCESS |
| 1015 | * \c *policy contains the intersection of the original value of |
| 1016 | * \c *policy and \c *constraint. |
| 1017 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1018 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1019 | * \c *policy is unchanged. |
| 1020 | */ |
| 1021 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1022 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1023 | psa_key_policy_t *policy, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1024 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1025 | { |
| 1026 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1027 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 1028 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1029 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1030 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 1031 | constraint->alg2); |
| 1032 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1033 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1034 | } |
| 1035 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1036 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1037 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1038 | policy->usage &= constraint->usage; |
| 1039 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1040 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1041 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1044 | /** Get the description of a key given its identifier and policy constraints |
| 1045 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1046 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1047 | * 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] | 1048 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1049 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1050 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1051 | * In case of a persistent key, the function loads the description of the key |
| 1052 | * into a key slot if not already done. |
| 1053 | * |
| 1054 | * On success, the returned key slot is locked. It is the responsibility of |
| 1055 | * 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] | 1056 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1057 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1058 | mbedtls_svc_key_id_t key, |
| 1059 | psa_key_slot_t **p_slot, |
| 1060 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1061 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1062 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1063 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1064 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1066 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1067 | if (status != PSA_SUCCESS) { |
| 1068 | return status; |
| 1069 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1070 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1071 | |
| 1072 | /* Enforce that usage policy for the key slot contains all the flags |
| 1073 | * required by the usage parameter. There is one exception: public |
| 1074 | * keys can always be exported, so we treat public key objects as |
| 1075 | * if they had the export flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1076 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1077 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1078 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1079 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1080 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1081 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1082 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1083 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1084 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1085 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1086 | if (alg != 0) { |
| 1087 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1088 | slot->attr.type, |
| 1089 | alg); |
| 1090 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1091 | goto error; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1092 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1093 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1096 | |
| 1097 | error: |
| 1098 | *p_slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1099 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1101 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1102 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1103 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1104 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1105 | * |
| 1106 | * 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] | 1107 | * available, as opposed to a key in a secure element and/or to be used |
| 1108 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1109 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1110 | * This is a temporary function that may be used instead of |
| 1111 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1112 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1113 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1114 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1115 | * 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] | 1116 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1117 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1118 | mbedtls_svc_key_id_t key, |
| 1119 | psa_key_slot_t **p_slot, |
| 1120 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1121 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1122 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1123 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1124 | usage, alg); |
| 1125 | if (status != PSA_SUCCESS) { |
| 1126 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1127 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1128 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1129 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1130 | psa_unlock_key_slot(*p_slot); |
| 1131 | *p_slot = NULL; |
| 1132 | return PSA_ERROR_NOT_SUPPORTED; |
| 1133 | } |
| 1134 | |
| 1135 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1136 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1138 | 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] | 1139 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1140 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1141 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1142 | if (slot->key.data != NULL) { |
| 1143 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1144 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1146 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1147 | slot->key.data = NULL; |
| 1148 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1149 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1150 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1153 | /** Completely wipe a slot in memory, including its policy. |
| 1154 | * Persistent storage is not affected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1156 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1157 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* |
| 1160 | * 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] | 1161 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1162 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1163 | * 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] | 1164 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1165 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1166 | if (slot->lock_count != 1) { |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1167 | #ifdef MBEDTLS_CHECK_PARAMS |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | MBEDTLS_PARAM_FAILED(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1169 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1170 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1171 | } |
| 1172 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1173 | /* Multipart operations may still be using the key. This is safe |
| 1174 | * because all multipart operation objects are independent from |
| 1175 | * the key slot: if they need to access the key after the setup |
| 1176 | * phase, they have a copy of the key. Note that this means that |
| 1177 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1178 | /* At this point, key material and other type-specific content has |
| 1179 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1180 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1181 | memset(slot, 0, sizeof(*slot)); |
| 1182 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1185 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1186 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1187 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1188 | psa_status_t status; /* status of the last operation */ |
| 1189 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1190 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1191 | psa_se_drv_table_entry_t *driver; |
| 1192 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1193 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1194 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1195 | return PSA_SUCCESS; |
| 1196 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1197 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1198 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1199 | * 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] | 1200 | * key, this will load the key description from persistent memory if not |
| 1201 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1202 | * the key is operated by an SE or not and this information is needed by |
| 1203 | * the current implementation. |
| 1204 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1205 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1206 | if (status != PSA_SUCCESS) { |
| 1207 | return status; |
| 1208 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1209 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1210 | /* |
| 1211 | * If the key slot containing the key description is under access by the |
| 1212 | * library (apart from the present access), the key cannot be destroyed |
| 1213 | * yet. For the time being, just return in error. Eventually (to be |
| 1214 | * implemented), the key should be destroyed when all accesses have |
| 1215 | * stopped. |
| 1216 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1217 | if (slot->lock_count > 1) { |
| 1218 | psa_unlock_key_slot(slot); |
| 1219 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1222 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1223 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1224 | * if we attempt it, depending on whether the key is merely read-only |
| 1225 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1226 | * Just do the best we can, which is to wipe the copy in memory |
| 1227 | * (done in this function's cleanup code). */ |
| 1228 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1229 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1232 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1233 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1234 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1235 | /* For a key in a secure element, we need to do three things: |
| 1236 | * remove the key file in internal storage, destroy the |
| 1237 | * key inside the secure element, and update the driver's |
| 1238 | * persistent data. Start a transaction that will encompass these |
| 1239 | * three actions. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1240 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1241 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1243 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1244 | status = psa_crypto_save_transaction(); |
| 1245 | if (status != PSA_SUCCESS) { |
| 1246 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1247 | /* We should still try to destroy the key in the secure |
| 1248 | * element and the key metadata in storage. This is especially |
| 1249 | * important if the error is that the storage is full. |
| 1250 | * But how to do it exactly without risking an inconsistent |
| 1251 | * state after a reset? |
| 1252 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1253 | */ |
| 1254 | overall_status = status; |
| 1255 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1258 | status = psa_destroy_se_key(driver, |
| 1259 | psa_key_slot_get_slot_number(slot)); |
| 1260 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1261 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1262 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1263 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1264 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1265 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1266 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1267 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1268 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1269 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1270 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1271 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1272 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1273 | /* TODO: other slots may have a copy of the same key. We should |
| 1274 | * invalidate them. |
| 1275 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1276 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1277 | } |
| 1278 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1279 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1280 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1281 | if (driver != NULL) { |
| 1282 | status = psa_save_se_persistent_data(driver); |
| 1283 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1284 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1285 | } |
| 1286 | status = psa_crypto_stop_transaction(); |
| 1287 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1288 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1289 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1290 | } |
| 1291 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1292 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1293 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1294 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1295 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1296 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1297 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1298 | } |
| 1299 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1302 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1303 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1304 | static psa_status_t psa_get_rsa_public_exponent( |
| 1305 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1306 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1307 | { |
| 1308 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1309 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1310 | uint8_t *buffer = NULL; |
| 1311 | size_t buflen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1312 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1314 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1315 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1316 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1317 | } |
| 1318 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1319 | /* It's the default value, which is reported as an empty string, |
| 1320 | * so there's nothing to do. */ |
| 1321 | goto exit; |
| 1322 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1324 | buflen = mbedtls_mpi_size(&mpi); |
| 1325 | buffer = mbedtls_calloc(1, buflen); |
| 1326 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1327 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1328 | goto exit; |
| 1329 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1330 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1331 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1332 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1333 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1334 | attributes->domain_parameters = buffer; |
| 1335 | attributes->domain_parameters_size = buflen; |
| 1336 | |
| 1337 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1338 | mbedtls_mpi_free(&mpi); |
| 1339 | if (ret != 0) { |
| 1340 | mbedtls_free(buffer); |
| 1341 | } |
| 1342 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1343 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1344 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1345 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1346 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1347 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1348 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1349 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1350 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1351 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1352 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1353 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1354 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1356 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1358 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1359 | if (status != PSA_SUCCESS) { |
| 1360 | return status; |
| 1361 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1362 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1363 | attributes->core = slot->attr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1364 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1365 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1366 | |
| 1367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1368 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1369 | psa_set_key_slot_number(attributes, |
| 1370 | psa_key_slot_get_slot_number(slot)); |
| 1371 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1373 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1374 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1375 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1376 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1377 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1378 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1379 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1380 | * is not yet implemented. |
| 1381 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1382 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1383 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1384 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1385 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1386 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1387 | slot->attr.type, |
| 1388 | slot->key.data, |
| 1389 | slot->key.bytes, |
| 1390 | &rsa); |
| 1391 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1392 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1393 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1394 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1395 | status = psa_get_rsa_public_exponent(rsa, |
| 1396 | attributes); |
| 1397 | mbedtls_rsa_free(rsa); |
| 1398 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1399 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1400 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1401 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1402 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1403 | default: |
| 1404 | /* Nothing else to do. */ |
| 1405 | break; |
| 1406 | } |
| 1407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1408 | if (status != PSA_SUCCESS) { |
| 1409 | psa_reset_key_attributes(attributes); |
| 1410 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1412 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1414 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1417 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1418 | psa_status_t psa_get_key_slot_number( |
| 1419 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1420 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1421 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1422 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1423 | *slot_number = attributes->slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1424 | return PSA_SUCCESS; |
| 1425 | } else { |
| 1426 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1427 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1428 | } |
| 1429 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1432 | size_t key_buffer_size, |
| 1433 | uint8_t *data, |
| 1434 | size_t data_size, |
| 1435 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1436 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1437 | if (key_buffer_size > data_size) { |
| 1438 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1439 | } |
| 1440 | memcpy(data, key_buffer, key_buffer_size); |
| 1441 | memset(data + key_buffer_size, 0, |
| 1442 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1443 | *data_length = key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1444 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1447 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1448 | const psa_key_attributes_t *attributes, |
| 1449 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1450 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1451 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1452 | psa_key_type_t type = attributes->core.type; |
| 1453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1454 | if (key_type_is_raw_bytes(type) || |
| 1455 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1456 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1457 | return psa_export_key_buffer_internal( |
| 1458 | key_buffer, key_buffer_size, |
| 1459 | data, data_size, data_length); |
| 1460 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1461 | /* This shouldn't happen in the reference implementation, but |
| 1462 | it is valid for a special-purpose implementation to omit |
| 1463 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1464 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1468 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1469 | uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1470 | size_t data_size, |
| 1471 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1472 | { |
| 1473 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1474 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1475 | psa_key_slot_t *slot; |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1476 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1477 | /* Reject a zero-length output buffer now, since this can never be a |
| 1478 | * valid key representation. This way we know that data must be a valid |
| 1479 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1480 | if (data_size == 0) { |
| 1481 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1482 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1483 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1484 | /* Set the key to empty now, so that even when there are errors, we always |
| 1485 | * set data_length to a value between 0 and data_size. On error, setting |
| 1486 | * the key to empty is a good choice because an empty key representation is |
| 1487 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1488 | *data_length = 0; |
| 1489 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1490 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1491 | * which don't require any flag, but |
| 1492 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1493 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1494 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1495 | PSA_KEY_USAGE_EXPORT, 0); |
| 1496 | if (status != PSA_SUCCESS) { |
| 1497 | return status; |
| 1498 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1499 | |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1500 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1501 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1502 | psa_key_attributes_t attributes = { |
| 1503 | .core = slot->attr |
| 1504 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1505 | status = psa_driver_wrapper_export_key(&attributes, |
| 1506 | slot->key.data, slot->key.bytes, |
| 1507 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1508 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 1509 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1510 | exit: |
Ryan Everett | 3a4153a | 2024-01-25 12:04:55 +0000 | [diff] [blame] | 1511 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1512 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1513 | |
Ryan Everett | e3e760c | 2024-01-19 16:03:50 +0000 | [diff] [blame] | 1514 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1515 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1518 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1519 | const psa_key_attributes_t *attributes, |
| 1520 | const uint8_t *key_buffer, |
| 1521 | size_t key_buffer_size, |
| 1522 | uint8_t *data, |
| 1523 | size_t data_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1524 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1525 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1526 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1527 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1528 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1529 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1530 | /* Exporting public -> public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | return psa_export_key_buffer_internal( |
| 1532 | key_buffer, key_buffer_size, |
| 1533 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1534 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1536 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1537 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1538 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1539 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1540 | key_buffer, |
| 1541 | key_buffer_size, |
| 1542 | data, |
| 1543 | data_size, |
| 1544 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1545 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1546 | /* 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] | 1547 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1548 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1549 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1550 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1551 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1552 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1553 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1554 | key_buffer, |
| 1555 | key_buffer_size, |
| 1556 | data, |
| 1557 | data_size, |
| 1558 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1559 | #else |
| 1560 | /* 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] | 1561 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1562 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1563 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1564 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1565 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1566 | /* This shouldn't happen in the reference implementation, but |
| 1567 | it is valid for a special-purpose implementation to omit |
| 1568 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1569 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1570 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1571 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1572 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1573 | 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] | 1574 | uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1575 | size_t data_size, |
| 1576 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1577 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1578 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1579 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1580 | psa_key_attributes_t attributes; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1581 | psa_key_slot_t *slot; |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1582 | LOCAL_OUTPUT_DECLARE(data_external, data); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1583 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1584 | /* Reject a zero-length output buffer now, since this can never be a |
| 1585 | * valid key representation. This way we know that data must be a valid |
| 1586 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1587 | if (data_size == 0) { |
| 1588 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1589 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1590 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1591 | /* Set the key to empty now, so that even when there are errors, we always |
| 1592 | * set data_length to a value between 0 and data_size. On error, setting |
| 1593 | * the key to empty is a good choice because an empty key representation is |
| 1594 | * unlikely to be accepted anywhere. */ |
| 1595 | *data_length = 0; |
| 1596 | |
| 1597 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1598 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1599 | if (status != PSA_SUCCESS) { |
| 1600 | return status; |
| 1601 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1602 | |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1603 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1604 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1605 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1606 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1607 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1608 | } |
| 1609 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1610 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1611 | .core = slot->attr |
| 1612 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1613 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1614 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1615 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1616 | |
| 1617 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1618 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1619 | |
Ryan Everett | 3082791 | 2024-01-19 16:05:00 +0000 | [diff] [blame] | 1620 | LOCAL_OUTPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1621 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1622 | } |
| 1623 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1624 | MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1625 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1626 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1627 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1628 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1629 | "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] | 1630 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1631 | /** Validate that a key policy is internally well-formed. |
| 1632 | * |
| 1633 | * This function only rejects invalid policies. It does not validate the |
| 1634 | * consistency of the policy with respect to other attributes of the key |
| 1635 | * such as the key type. |
| 1636 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1637 | 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] | 1638 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1639 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1640 | PSA_KEY_USAGE_COPY | |
| 1641 | PSA_KEY_USAGE_ENCRYPT | |
| 1642 | PSA_KEY_USAGE_DECRYPT | |
| 1643 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1644 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1645 | PSA_KEY_USAGE_SIGN_HASH | |
| 1646 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1647 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1648 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1649 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1651 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1654 | /** Validate the internal consistency of key attributes. |
| 1655 | * |
| 1656 | * This function only rejects invalid attribute values. If does not |
| 1657 | * validate the consistency of the attributes with any key data that may |
| 1658 | * be involved in the creation of the key. |
| 1659 | * |
| 1660 | * Call this function early in the key creation process. |
| 1661 | * |
| 1662 | * \param[in] attributes Key attributes for the new key. |
| 1663 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1664 | * NULL for a transparent key. |
| 1665 | * |
| 1666 | */ |
| 1667 | static psa_status_t psa_validate_key_attributes( |
| 1668 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1669 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1670 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1671 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1672 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1673 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1674 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1675 | status = psa_validate_key_location(lifetime, p_drv); |
| 1676 | if (status != PSA_SUCCESS) { |
| 1677 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1680 | status = psa_validate_key_persistence(lifetime); |
| 1681 | if (status != PSA_SUCCESS) { |
| 1682 | return status; |
| 1683 | } |
| 1684 | |
| 1685 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1686 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1687 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1688 | } |
| 1689 | } else { |
| 1690 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1691 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1696 | if (status != PSA_SUCCESS) { |
| 1697 | return status; |
| 1698 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* Refuse to create overly large keys. |
| 1701 | * Note that this doesn't trigger on import if the attributes don't |
| 1702 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1703 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1704 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1705 | return PSA_ERROR_NOT_SUPPORTED; |
| 1706 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1707 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1708 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1709 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1710 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1711 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1712 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1713 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1714 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1717 | /** Prepare a key slot to receive key material. |
| 1718 | * |
| 1719 | * This function allocates a key slot and sets its metadata. |
| 1720 | * |
| 1721 | * If this function fails, call psa_fail_key_creation(). |
| 1722 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1723 | * This function is intended to be used as follows: |
| 1724 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1725 | * it with the specified attributes, and in case of a volatile key assign it |
| 1726 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1727 | * -# Populate the slot with the key material. |
| 1728 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1729 | * In case of failure at any step, stop the sequence and call |
| 1730 | * psa_fail_key_creation(). |
| 1731 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1732 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1733 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1734 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1735 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1736 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1737 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1738 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1739 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1740 | * |
| 1741 | * \retval #PSA_SUCCESS |
| 1742 | * The key slot is ready to receive key material. |
| 1743 | * \return If this function fails, the key slot is an invalid state. |
| 1744 | * 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] | 1745 | */ |
| 1746 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1747 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1748 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1749 | psa_key_slot_t **p_slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1750 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1751 | { |
| 1752 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1753 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1754 | psa_key_slot_t *slot; |
| 1755 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1756 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1757 | *p_drv = NULL; |
| 1758 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1759 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1760 | if (status != PSA_SUCCESS) { |
| 1761 | return status; |
| 1762 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1764 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1765 | if (status != PSA_SUCCESS) { |
| 1766 | return status; |
| 1767 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1768 | slot = *p_slot; |
| 1769 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1770 | /* We're storing the declared bit-size of the key. It's up to each |
| 1771 | * creation mechanism to verify that this information is correct. |
| 1772 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1773 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1774 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1775 | * volatile key identifier associated to the slot returned to contain its |
| 1776 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1777 | |
| 1778 | slot->attr = attributes->core; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1779 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1780 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1781 | slot->attr.id = volatile_key_id; |
| 1782 | #else |
| 1783 | slot->attr.id.key_id = volatile_key_id; |
| 1784 | #endif |
| 1785 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1786 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1787 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1788 | * external-only flags, query `attributes`. Thanks to the check |
| 1789 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1790 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1791 | * may have set. */ |
| 1792 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1793 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1794 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1795 | /* 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] | 1796 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1797 | * create the key file in internal storage, create the |
| 1798 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1799 | * persistent data. This is done by starting a transaction that will |
| 1800 | * encompass these three actions. |
| 1801 | * For registering a volatile key, we just need to find an appropriate |
| 1802 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1803 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1804 | /* The first thing to do is to find a slot number for the new key. |
| 1805 | * We save the slot number in persistent storage as part of the |
| 1806 | * transaction data. It will be needed to recover if the power |
| 1807 | * fails during the key creation process, to clean up on the secure |
| 1808 | * element side after restarting. Obtaining a slot number from the |
| 1809 | * secure element driver updates its persistent state, but we do not yet |
| 1810 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1811 | * 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] | 1812 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1813 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1814 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1815 | &slot_number); |
| 1816 | if (status != PSA_SUCCESS) { |
| 1817 | return status; |
| 1818 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1820 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1821 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1822 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1823 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1824 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1825 | status = psa_crypto_save_transaction(); |
| 1826 | if (status != PSA_SUCCESS) { |
| 1827 | (void) psa_crypto_stop_transaction(); |
| 1828 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1829 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1830 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1831 | |
| 1832 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1833 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1834 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1836 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1837 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1838 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1839 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1840 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1842 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1843 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1844 | |
| 1845 | /** Finalize the creation of a key once its key material has been set. |
| 1846 | * |
| 1847 | * This entails writing the key to persistent storage. |
| 1848 | * |
| 1849 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1850 | * See the documentation of psa_start_key_creation() for the intended use |
| 1851 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1852 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1853 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1854 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1855 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1856 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1857 | * \param[in,out] slot Pointer to the slot with key material. |
| 1858 | * \param[in] driver The secure element driver for the key, |
| 1859 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1860 | * \param[out] key On success, identifier of the key. Note that the |
| 1861 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1862 | * |
| 1863 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1864 | * The key was successfully created. |
Gilles Peskine | ec1eff3 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1865 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1866 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1867 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1868 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1869 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1870 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1871 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1872 | * \return If this function fails, the key slot is an invalid state. |
| 1873 | * 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] | 1874 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1875 | static psa_status_t psa_finish_key_creation( |
| 1876 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1877 | psa_se_drv_table_entry_t *driver, |
| 1878 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | { |
| 1880 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1881 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1882 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1883 | |
| 1884 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1885 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1886 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1887 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1888 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1889 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1890 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1891 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1892 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1893 | sizeof(data.slot_number), |
| 1894 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1896 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1897 | status = psa_save_persistent_key(&slot->attr, |
| 1898 | (uint8_t *) &data, |
| 1899 | sizeof(data)); |
| 1900 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1901 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1902 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1903 | /* Key material is saved in export representation in the slot, so |
| 1904 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1905 | status = psa_save_persistent_key(&slot->attr, |
| 1906 | slot->key.data, |
| 1907 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1908 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1909 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1910 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1911 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1912 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1913 | /* Finish the transaction for a key creation. This does not |
| 1914 | * happen when registering an existing key. Detect this case |
| 1915 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1916 | * creation of a persistent key in a secure element requires a transaction, |
| 1917 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1918 | if (driver != NULL && |
| 1919 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1920 | status = psa_save_se_persistent_data(driver); |
| 1921 | if (status != PSA_SUCCESS) { |
| 1922 | psa_destroy_persistent_key(slot->attr.id); |
| 1923 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1924 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1925 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1926 | } |
| 1927 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1928 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1929 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1930 | *key = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1931 | status = psa_unlock_key_slot(slot); |
| 1932 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1933 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1934 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1935 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1936 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1937 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /** Abort the creation of a key. |
| 1941 | * |
| 1942 | * You may call this function after calling psa_start_key_creation(), |
| 1943 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1944 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1945 | * See the documentation of psa_start_key_creation() for the intended use |
| 1946 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1947 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1948 | * \param[in,out] slot Pointer to the slot with key material. |
| 1949 | * \param[in] driver The secure element driver for the key, |
| 1950 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1951 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1952 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1953 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1955 | (void) driver; |
| 1956 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1957 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1958 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1959 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1960 | |
| 1961 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1962 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1963 | * element, and the failure happened later (when saving metadata |
| 1964 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1965 | * element. |
| 1966 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1967 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1968 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1969 | /* Abort the ongoing transaction if any (there may not be one if |
| 1970 | * the creation process failed before starting one, or if the |
| 1971 | * key creation is a registration of a key in a secure element). |
| 1972 | * Earlier functions must already have done what it takes to undo any |
| 1973 | * partial creation. All that's left is to update the transaction data |
| 1974 | * itself. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1975 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1976 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1977 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1978 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1979 | } |
| 1980 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1981 | /** Validate optional attributes during key creation. |
| 1982 | * |
| 1983 | * Some key attributes are optional during key creation. If they are |
| 1984 | * specified in the attributes structure, check that they are consistent |
| 1985 | * with the data in the slot. |
| 1986 | * |
| 1987 | * This function should be called near the end of key creation, after |
| 1988 | * the slot in memory is fully populated but before saving persistent data. |
| 1989 | */ |
| 1990 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1991 | const psa_key_slot_t *slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1992 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1993 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1994 | if (attributes->core.type != 0) { |
| 1995 | if (attributes->core.type != slot->attr.type) { |
| 1996 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1997 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2000 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2001 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2002 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2003 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2004 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2005 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2006 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2007 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2008 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2009 | slot->attr.type, |
| 2010 | slot->key.data, |
| 2011 | slot->key.bytes, |
| 2012 | &rsa); |
| 2013 | if (status != PSA_SUCCESS) { |
| 2014 | return status; |
| 2015 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2017 | mbedtls_mpi_init(&actual); |
| 2018 | mbedtls_mpi_init(&required); |
| 2019 | ret = mbedtls_rsa_export(rsa, |
| 2020 | NULL, NULL, NULL, NULL, &actual); |
| 2021 | mbedtls_rsa_free(rsa); |
| 2022 | mbedtls_free(rsa); |
| 2023 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2024 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2025 | } |
| 2026 | ret = mbedtls_mpi_read_binary(&required, |
| 2027 | attributes->domain_parameters, |
| 2028 | attributes->domain_parameters_size); |
| 2029 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2030 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2031 | } |
| 2032 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2033 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2034 | } |
| 2035 | rsa_exit: |
| 2036 | mbedtls_mpi_free(&actual); |
| 2037 | mbedtls_mpi_free(&required); |
| 2038 | if (ret != 0) { |
| 2039 | return mbedtls_to_psa_error(ret); |
| 2040 | } |
| 2041 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2042 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2043 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2044 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2045 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2049 | if (attributes->core.bits != 0) { |
| 2050 | if (attributes->core.bits != slot->attr.bits) { |
| 2051 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2052 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2055 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2058 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2059 | const uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2060 | size_t data_length, |
| 2061 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2062 | { |
| 2063 | psa_status_t status; |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2064 | LOCAL_INPUT_DECLARE(data_external, data); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2065 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2066 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2067 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2068 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2069 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2070 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2071 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2072 | * This also rejects any key which might be encoded as an empty string, |
| 2073 | * which is never valid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2074 | if (data_length == 0) { |
| 2075 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2076 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2077 | |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2078 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2079 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2080 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2081 | &slot, &driver); |
| 2082 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2083 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2084 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2085 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2086 | /* In the case of a transparent key or an opaque key stored in local |
| 2087 | * storage (thus not in the case of generating a key in a secure element |
| 2088 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 2089 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2090 | if (slot->key.data == NULL) { |
| 2091 | status = psa_allocate_buffer_to_slot(slot, data_length); |
| 2092 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2093 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2094 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2095 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2096 | |
| 2097 | bits = slot->attr.bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2098 | status = psa_driver_wrapper_import_key(attributes, |
| 2099 | data, data_length, |
| 2100 | slot->key.data, |
| 2101 | slot->key.bytes, |
| 2102 | &slot->key.bytes, &bits); |
| 2103 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2104 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2105 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2107 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2108 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2109 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2110 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2111 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2112 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2114 | status = psa_validate_optional_attributes(slot, attributes); |
| 2115 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2116 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2117 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2119 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2120 | exit: |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame] | 2121 | LOCAL_INPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2122 | if (status != PSA_SUCCESS) { |
| 2123 | psa_fail_key_creation(slot, driver); |
| 2124 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2127 | } |
| 2128 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2129 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2130 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2131 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2132 | { |
| 2133 | psa_status_t status; |
| 2134 | psa_key_slot_t *slot = NULL; |
| 2135 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2136 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2137 | |
| 2138 | /* Leaving attributes unspecified is not currently supported. |
| 2139 | * It could make sense to query the key type and size from the |
| 2140 | * secure element, but not all secure elements support this |
| 2141 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2142 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2143 | return PSA_ERROR_NOT_SUPPORTED; |
| 2144 | } |
| 2145 | if (psa_get_key_bits(attributes) == 0) { |
| 2146 | return PSA_ERROR_NOT_SUPPORTED; |
| 2147 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2149 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2150 | &slot, &driver); |
| 2151 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2152 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2153 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2155 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2156 | |
| 2157 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2158 | if (status != PSA_SUCCESS) { |
| 2159 | psa_fail_key_creation(slot, driver); |
| 2160 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2161 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2162 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2163 | psa_close_key(key); |
| 2164 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2165 | } |
| 2166 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2168 | static psa_status_t psa_copy_key_material(const psa_key_slot_t *source, |
| 2169 | psa_key_slot_t *target) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2170 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2171 | psa_status_t status = psa_copy_key_material_into_slot(target, |
| 2172 | source->key.data, |
| 2173 | source->key.bytes); |
| 2174 | if (status != PSA_SUCCESS) { |
| 2175 | return status; |
| 2176 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2177 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2178 | target->attr.type = source->attr.type; |
| 2179 | target->attr.bits = source->attr.bits; |
| 2180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2181 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2184 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2185 | const psa_key_attributes_t *specified_attributes, |
| 2186 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2187 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2188 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2189 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2190 | psa_key_slot_t *source_slot = NULL; |
| 2191 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2192 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2193 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2194 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2195 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2196 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2197 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2198 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2199 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2200 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2201 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2203 | status = psa_validate_optional_attributes(source_slot, |
| 2204 | specified_attributes); |
| 2205 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2206 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2207 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2209 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2210 | &actual_attributes.core.policy, |
| 2211 | &source_slot->attr.policy); |
| 2212 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2213 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2214 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2216 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2217 | &target_slot, &driver); |
| 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 | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2222 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2223 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2224 | /* Copying to a secure element is not implemented yet. */ |
| 2225 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2226 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2227 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2228 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2230 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * Copying through an opaque driver is not implemented yet, consider |
| 2233 | * a lifetime with an external location as an invalid parameter for |
| 2234 | * now. |
| 2235 | */ |
| 2236 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2237 | goto exit; |
| 2238 | } |
| 2239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2240 | status = psa_copy_key_material(source_slot, target_slot); |
| 2241 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2242 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2243 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2245 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2246 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2247 | if (status != PSA_SUCCESS) { |
| 2248 | psa_fail_key_creation(target_slot, driver); |
| 2249 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2251 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2253 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2254 | } |
| 2255 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2256 | |
| 2257 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2258 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2259 | /* Message digests */ |
| 2260 | /****************************************************************/ |
| 2261 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2262 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2263 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2264 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2265 | if (operation->id == 0) { |
| 2266 | return PSA_SUCCESS; |
| 2267 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2269 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2270 | operation->id = 0; |
| 2271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2272 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2275 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2276 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2277 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2278 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2279 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2280 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2281 | if (operation->id != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2282 | status = PSA_ERROR_BAD_STATE; |
| 2283 | goto exit; |
| 2284 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2286 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2287 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2288 | goto exit; |
| 2289 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2290 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2291 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2292 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2293 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2295 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2296 | |
| 2297 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2298 | if (status != PSA_SUCCESS) { |
| 2299 | psa_hash_abort(operation); |
| 2300 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2301 | |
| 2302 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2306 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2308 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2309 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2310 | LOCAL_INPUT_DECLARE(input_external, input); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2312 | if (operation->id == 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2313 | status = PSA_ERROR_BAD_STATE; |
| 2314 | goto exit; |
| 2315 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2316 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2317 | /* Don't require hash implementations to behave correctly on a |
| 2318 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2319 | if (input_length == 0) { |
| 2320 | return PSA_SUCCESS; |
| 2321 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2322 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2323 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2324 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2325 | |
| 2326 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2327 | if (status != PSA_SUCCESS) { |
| 2328 | psa_hash_abort(operation); |
| 2329 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2330 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2331 | LOCAL_INPUT_FREE(input_external, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2332 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2335 | 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] | 2336 | uint8_t *hash, |
| 2337 | size_t hash_size, |
| 2338 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2340 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2341 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2342 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2343 | if (operation->id == 0) { |
| 2344 | return PSA_ERROR_BAD_STATE; |
| 2345 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2346 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2347 | status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2348 | operation, hash, hash_size, hash_length); |
| 2349 | psa_hash_abort(operation); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2350 | |
| 2351 | return status; |
| 2352 | } |
| 2353 | |
| 2354 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2355 | uint8_t *hash_external, |
| 2356 | size_t hash_size, |
| 2357 | size_t *hash_length) |
| 2358 | { |
| 2359 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2360 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2361 | |
| 2362 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2363 | status = psa_hash_finish_internal(operation, hash, hash_size, hash_length); |
| 2364 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 2365 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2366 | exit: |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2367 | #endif |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2368 | LOCAL_OUTPUT_FREE(hash_external, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2369 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2370 | } |
| 2371 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2372 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2373 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2374 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2375 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2376 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2377 | size_t actual_hash_length; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2378 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2379 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 2380 | |
| 2381 | status = psa_hash_finish_internal( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2382 | operation, |
| 2383 | actual_hash, sizeof(actual_hash), |
| 2384 | &actual_hash_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2386 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2387 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2388 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2390 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2391 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2392 | goto exit; |
| 2393 | } |
| 2394 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2395 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2396 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2397 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2398 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2399 | |
| 2400 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2401 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2402 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2403 | psa_hash_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2404 | } |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2405 | LOCAL_INPUT_FREE(hash_external, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2406 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2410 | const uint8_t *input_external, size_t input_length, |
| 2411 | uint8_t *hash_external, size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2412 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2413 | { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2415 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2416 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2417 | |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2418 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2419 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2420 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2421 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2422 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2423 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2424 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2425 | status = psa_driver_wrapper_hash_compute(alg, input, input_length, |
Thomas Daubney | cbf0921 | 2024-01-25 17:14:29 +0000 | [diff] [blame] | 2426 | hash, hash_size, hash_length); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2427 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 2428 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2429 | exit: |
Thomas Daubney | 5e6b84a | 2024-01-25 17:10:42 +0000 | [diff] [blame] | 2430 | #endif |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2431 | LOCAL_INPUT_FREE(input_external, input); |
| 2432 | LOCAL_OUTPUT_FREE(hash_external, hash); |
| 2433 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2436 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2437 | const uint8_t *input_external, size_t input_length, |
| 2438 | const uint8_t *hash_external, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2439 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2440 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2441 | size_t actual_hash_length; |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2442 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2443 | |
| 2444 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2445 | LOCAL_INPUT_DECLARE(hash_external, hash); |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2446 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2447 | if (!PSA_ALG_IS_HASH(alg)) { |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2448 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2449 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2450 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2451 | |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2452 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2453 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2454 | alg, input, input_length, |
| 2455 | actual_hash, sizeof(actual_hash), |
| 2456 | &actual_hash_length); |
| 2457 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2458 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2459 | } |
| 2460 | if (actual_hash_length != hash_length) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2461 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2462 | goto exit; |
| 2463 | } |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2464 | |
| 2465 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2466 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2467 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2468 | } |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2469 | |
| 2470 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2471 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
Thomas Daubney | 62cb36a | 2024-01-12 16:50:26 +0000 | [diff] [blame] | 2472 | |
| 2473 | LOCAL_INPUT_FREE(input_external, input); |
| 2474 | LOCAL_INPUT_FREE(hash_external, hash); |
| 2475 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2476 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2477 | } |
| 2478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2479 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2480 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2481 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2482 | if (source_operation->id == 0 || |
| 2483 | target_operation->id != 0) { |
| 2484 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2485 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2487 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2488 | target_operation); |
| 2489 | if (status != PSA_SUCCESS) { |
| 2490 | psa_hash_abort(target_operation); |
| 2491 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2492 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2493 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2494 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2495 | |
| 2496 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2497 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2498 | /* MAC */ |
| 2499 | /****************************************************************/ |
| 2500 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2501 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2502 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2503 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2504 | if (operation->id == 0) { |
| 2505 | return PSA_SUCCESS; |
| 2506 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2507 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2508 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2509 | operation->mac_size = 0; |
| 2510 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2511 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2512 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2513 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2514 | } |
| 2515 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2516 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2517 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2518 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2519 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2520 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2521 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2522 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2523 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2525 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2526 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2527 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2528 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2529 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2530 | status = psa_mac_key_can_do(alg, key_type); |
| 2531 | if (status != PSA_SUCCESS) { |
| 2532 | return status; |
| 2533 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2534 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2535 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2536 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2538 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2539 | /* A very short MAC is too short for security since it can be |
| 2540 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2541 | * so we make this our minimum, even though 32 bits is still |
| 2542 | * too small for security. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2543 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2544 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2545 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2546 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2547 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2548 | /* It's impossible to "truncate" to a larger length than the full length |
| 2549 | * of the algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2550 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2553 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2554 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2555 | * that is disabled in the compile-time configuration. The result can |
| 2556 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2557 | * configuration into account. In this case, force a return of |
| 2558 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2559 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2560 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2561 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2562 | * systematically generated tests. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2563 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2564 | } |
| 2565 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2566 | return PSA_SUCCESS; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2567 | } |
| 2568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2569 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2570 | mbedtls_svc_key_id_t key, |
| 2571 | psa_algorithm_t alg, |
| 2572 | int is_sign) |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2573 | { |
| 2574 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2575 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2576 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2577 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2578 | |
| 2579 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2580 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2581 | status = PSA_ERROR_BAD_STATE; |
| 2582 | goto exit; |
| 2583 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2584 | |
| 2585 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2586 | key, |
| 2587 | &slot, |
| 2588 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2589 | alg); |
| 2590 | if (status != PSA_SUCCESS) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2591 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2592 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2593 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2594 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2595 | .core = slot->attr |
| 2596 | }; |
| 2597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2598 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2599 | &operation->mac_size); |
| 2600 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2601 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2602 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2603 | |
| 2604 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2605 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2606 | if (is_sign) { |
| 2607 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2608 | &attributes, |
| 2609 | slot->key.data, |
| 2610 | slot->key.bytes, |
| 2611 | alg); |
| 2612 | } else { |
| 2613 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2614 | &attributes, |
| 2615 | slot->key.data, |
| 2616 | slot->key.bytes, |
| 2617 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2618 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2619 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2620 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2621 | if (status != PSA_SUCCESS) { |
| 2622 | psa_mac_abort(operation); |
| 2623 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2624 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2625 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2626 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2627 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2628 | } |
| 2629 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2630 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2631 | mbedtls_svc_key_id_t key, |
| 2632 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2633 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2634 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2637 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2638 | mbedtls_svc_key_id_t key, |
| 2639 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2640 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2641 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2644 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2645 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2647 | { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2648 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2649 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2651 | if (operation->id == 0) { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2652 | status = PSA_ERROR_BAD_STATE; |
| 2653 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2654 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2655 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2656 | /* Don't require hash implementations to behave correctly on a |
| 2657 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2658 | if (input_length == 0) { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2659 | status = PSA_SUCCESS; |
| 2660 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2661 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2662 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2663 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2664 | status = psa_driver_wrapper_mac_update(operation, input, input_length); |
| 2665 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2666 | if (status != PSA_SUCCESS) { |
| 2667 | psa_mac_abort(operation); |
| 2668 | } |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2669 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 2670 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2671 | exit: |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2672 | #endif |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2673 | LOCAL_INPUT_FREE(input_external, input); |
| 2674 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2675 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2676 | } |
| 2677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2678 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2679 | uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2680 | size_t mac_size, |
| 2681 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2682 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2683 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2684 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2685 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
Thomas Daubney | 301491d | 2024-02-01 14:32:59 +0000 | [diff] [blame] | 2686 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2688 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2689 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2690 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2691 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2693 | if (!operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2694 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2695 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2696 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2697 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2698 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2699 | * once all the error checks are done. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2700 | if (operation->mac_size == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2701 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2702 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2703 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2705 | if (mac_size < operation->mac_size) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2706 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2707 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2708 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2710 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2711 | mac, operation->mac_size, |
| 2712 | mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2713 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2714 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2715 | /* In case of success, set the potential excess room in the output buffer |
| 2716 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2717 | * In case of error, set the output length and content to a safe default, |
| 2718 | * such that in case the caller misses an error check, the output would be |
| 2719 | * an unachievable MAC. |
| 2720 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2721 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2722 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2723 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2724 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2725 | |
Thomas Daubney | 480347d | 2024-02-01 19:00:26 +0000 | [diff] [blame] | 2726 | if ((mac != NULL) && (mac_size > operation->mac_size)) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2727 | memset(&mac[operation->mac_size], '!', |
| 2728 | mac_size - operation->mac_size); |
| 2729 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2731 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2732 | LOCAL_OUTPUT_FREE(mac_external, mac); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2733 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2734 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2737 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2738 | const uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2739 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2741 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2742 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2743 | LOCAL_INPUT_DECLARE(mac_external, mac); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2744 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2745 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2746 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2747 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2748 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2749 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2750 | if (operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2751 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2752 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2753 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2754 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2755 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2756 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2757 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2760 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2761 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2762 | mac, mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2763 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2764 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2765 | abort_status = psa_mac_abort(operation); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2766 | LOCAL_INPUT_FREE(mac_external, mac); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2768 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2769 | } |
| 2770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2771 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2772 | psa_algorithm_t alg, |
| 2773 | const uint8_t *input, |
| 2774 | size_t input_length, |
| 2775 | uint8_t *mac, |
| 2776 | size_t mac_size, |
| 2777 | size_t *mac_length, |
| 2778 | int is_sign) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2779 | { |
| 2780 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2781 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2782 | psa_key_attributes_t attributes; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2783 | psa_key_slot_t *slot; |
| 2784 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2785 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2786 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2787 | key, |
| 2788 | &slot, |
| 2789 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2790 | alg); |
| 2791 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2792 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2793 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2794 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2795 | attributes = (psa_key_attributes_t) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2796 | .core = slot->attr |
| 2797 | }; |
| 2798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2799 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2800 | &operation_mac_size); |
| 2801 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2802 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2803 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2805 | if (mac_size < operation_mac_size) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2806 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2807 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2811 | &attributes, |
| 2812 | slot->key.data, slot->key.bytes, |
| 2813 | alg, |
| 2814 | input, input_length, |
| 2815 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2816 | |
| 2817 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2818 | /* In case of success, set the potential excess room in the output buffer |
| 2819 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2820 | * In case of error, set the output length and content to a safe default, |
| 2821 | * such that in case the caller misses an error check, the output would be |
| 2822 | * an unachievable MAC. |
| 2823 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | if (status != PSA_SUCCESS) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2825 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2826 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2827 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2828 | if (mac_size > operation_mac_size) { |
| 2829 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
| 2830 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2832 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2833 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2834 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2835 | } |
| 2836 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2837 | 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] | 2838 | psa_algorithm_t alg, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2839 | const uint8_t *input_external, |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2840 | size_t input_length, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2841 | uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2842 | size_t mac_size, |
| 2843 | size_t *mac_length) |
| 2844 | { |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2845 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2846 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2847 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
| 2848 | |
| 2849 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2850 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
| 2851 | status = psa_mac_compute_internal(key, alg, |
Thomas Daubney | f298f65 | 2024-01-30 12:15:36 +0000 | [diff] [blame] | 2852 | input, input_length, |
| 2853 | mac, mac_size, mac_length, 1); |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2854 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 2855 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2856 | exit: |
Thomas Daubney | 2bb3a1f | 2024-01-30 12:12:56 +0000 | [diff] [blame] | 2857 | #endif |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2858 | LOCAL_INPUT_FREE(input_external, input); |
| 2859 | LOCAL_OUTPUT_FREE(mac_external, mac); |
| 2860 | |
| 2861 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2865 | psa_algorithm_t alg, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2866 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2867 | size_t input_length, |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2868 | const uint8_t *mac_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2869 | size_t mac_length) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2870 | { |
| 2871 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2872 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2873 | size_t actual_mac_length; |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2874 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2875 | LOCAL_INPUT_DECLARE(mac_external, mac); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2876 | |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2877 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2878 | status = psa_mac_compute_internal(key, alg, |
| 2879 | input, input_length, |
| 2880 | actual_mac, sizeof(actual_mac), |
| 2881 | &actual_mac_length, 0); |
| 2882 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2883 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2884 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | if (mac_length != actual_mac_length) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2887 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2888 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2889 | } |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2890 | |
| 2891 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2892 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2893 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2894 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2895 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2896 | |
| 2897 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
Thomas Daubney | 324f7de | 2024-01-18 13:18:58 +0000 | [diff] [blame] | 2899 | LOCAL_INPUT_FREE(input_external, input); |
| 2900 | LOCAL_INPUT_FREE(mac_external, mac); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2901 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2902 | return status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2903 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2904 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2905 | /****************************************************************/ |
| 2906 | /* Asymmetric cryptography */ |
| 2907 | /****************************************************************/ |
| 2908 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2909 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2910 | psa_algorithm_t alg) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2911 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2912 | if (input_is_message) { |
| 2913 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2914 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2915 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2917 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2918 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2919 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2920 | } |
| 2921 | } |
| 2922 | } else { |
| 2923 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2924 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2925 | } |
| 2926 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2928 | return PSA_SUCCESS; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2929 | } |
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 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2932 | int input_is_message, |
| 2933 | psa_algorithm_t alg, |
| 2934 | const uint8_t *input, |
| 2935 | size_t input_length, |
| 2936 | uint8_t *signature, |
| 2937 | size_t signature_size, |
| 2938 | size_t *signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2939 | { |
| 2940 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2941 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2942 | psa_key_attributes_t attributes; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2943 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2944 | |
| 2945 | *signature_length = 0; |
| 2946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2947 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2948 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2949 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2950 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2951 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2952 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2953 | * 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] | 2954 | * buffer can in principle be empty since it doesn't actually have |
| 2955 | * to be a hash.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2956 | if (signature_size == 0) { |
| 2957 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2958 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2959 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2960 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2961 | key, &slot, |
| 2962 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2963 | PSA_KEY_USAGE_SIGN_HASH, |
| 2964 | alg); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2965 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2966 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2967 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2968 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2969 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2970 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2971 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2972 | goto exit; |
| 2973 | } |
| 2974 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2975 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2976 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2977 | }; |
| 2978 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2979 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2980 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2981 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2982 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2983 | signature, signature_size, signature_length); |
| 2984 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2985 | |
| 2986 | status = psa_driver_wrapper_sign_hash( |
| 2987 | &attributes, slot->key.data, slot->key.bytes, |
| 2988 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2989 | signature, signature_size, signature_length); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2992 | |
| 2993 | exit: |
| 2994 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2995 | * the trailing part on success) with something that isn't a valid signature |
| 2996 | * (barring an attack on the signature and deliberately-crafted input), |
| 2997 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2998 | if (status == PSA_SUCCESS) { |
| 2999 | memset(signature + *signature_length, '!', |
| 3000 | signature_size - *signature_length); |
| 3001 | } else { |
| 3002 | memset(signature, '!', signature_size); |
| 3003 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3004 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3005 | * memset because signature may be NULL in this case. */ |
| 3006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3007 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3009 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3010 | } |
| 3011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3012 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 3013 | int input_is_message, |
| 3014 | psa_algorithm_t alg, |
| 3015 | const uint8_t *input, |
| 3016 | size_t input_length, |
| 3017 | const uint8_t *signature, |
| 3018 | size_t signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3019 | { |
| 3020 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3021 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3022 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3023 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3024 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3025 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3026 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3027 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3028 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3029 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3030 | key, &slot, |
| 3031 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 3032 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3033 | alg); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3034 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3035 | if (status != PSA_SUCCESS) { |
| 3036 | return status; |
| 3037 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3038 | |
| 3039 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3040 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3041 | }; |
| 3042 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3043 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3044 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3045 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3046 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3047 | signature, signature_length); |
| 3048 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3049 | status = psa_driver_wrapper_verify_hash( |
| 3050 | &attributes, slot->key.data, slot->key.bytes, |
| 3051 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | signature, signature_length); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3053 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3054 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3055 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3056 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3057 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3058 | |
| 3059 | } |
| 3060 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3061 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3062 | const psa_key_attributes_t *attributes, |
| 3063 | const uint8_t *key_buffer, |
| 3064 | size_t key_buffer_size, |
| 3065 | psa_algorithm_t alg, |
| 3066 | const uint8_t *input, |
| 3067 | size_t input_length, |
| 3068 | uint8_t *signature, |
| 3069 | size_t signature_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3070 | size_t *signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3071 | { |
| 3072 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3075 | size_t hash_length; |
| 3076 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3077 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3078 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3079 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3080 | input, input_length, |
| 3081 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3083 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3084 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3085 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3086 | |
| 3087 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3088 | attributes, key_buffer, key_buffer_size, |
| 3089 | alg, hash, hash_length, |
| 3090 | signature, signature_size, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3091 | } |
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 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3094 | } |
| 3095 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3096 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3097 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3098 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3099 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3100 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3101 | size_t signature_size, |
| 3102 | size_t *signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3103 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3104 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3105 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3106 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3107 | |
| 3108 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3109 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3110 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3111 | signature_size, signature_length); |
| 3112 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 3113 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3114 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3115 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3116 | LOCAL_INPUT_FREE(input_external, input); |
| 3117 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3118 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3119 | } |
| 3120 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3121 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3122 | const psa_key_attributes_t *attributes, |
| 3123 | const uint8_t *key_buffer, |
| 3124 | size_t key_buffer_size, |
| 3125 | psa_algorithm_t alg, |
| 3126 | const uint8_t *input, |
| 3127 | size_t input_length, |
| 3128 | const uint8_t *signature, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3129 | size_t signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3130 | { |
| 3131 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3133 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3134 | size_t hash_length; |
| 3135 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3136 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3137 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3138 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3139 | input, input_length, |
| 3140 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3142 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3143 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3144 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3145 | |
| 3146 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3147 | attributes, key_buffer, key_buffer_size, |
| 3148 | alg, hash, hash_length, |
| 3149 | signature, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3150 | } |
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 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3153 | } |
| 3154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3155 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3156 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3157 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3158 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3159 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3160 | size_t signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3161 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3162 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3163 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3164 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3165 | |
| 3166 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3167 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3168 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3169 | signature_length); |
| 3170 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 3171 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3172 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3173 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3174 | LOCAL_INPUT_FREE(input_external, input); |
| 3175 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3176 | |
| 3177 | return status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3178 | } |
| 3179 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3180 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3181 | const psa_key_attributes_t *attributes, |
| 3182 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3183 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3184 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3185 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3186 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3187 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3188 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3189 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3190 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3191 | return mbedtls_psa_rsa_sign_hash( |
| 3192 | attributes, |
| 3193 | key_buffer, key_buffer_size, |
| 3194 | alg, hash, hash_length, |
| 3195 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3196 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3197 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3198 | } else { |
| 3199 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3200 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3201 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3202 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3203 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3204 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3205 | return mbedtls_psa_ecdsa_sign_hash( |
| 3206 | attributes, |
| 3207 | key_buffer, key_buffer_size, |
| 3208 | alg, hash, hash_length, |
| 3209 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3210 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3211 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3212 | } else { |
| 3213 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3214 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3215 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3217 | (void) key_buffer; |
| 3218 | (void) key_buffer_size; |
| 3219 | (void) hash; |
| 3220 | (void) hash_length; |
| 3221 | (void) signature; |
| 3222 | (void) signature_size; |
| 3223 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3225 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3226 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3228 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3229 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3230 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3231 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3232 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3233 | size_t signature_size, |
| 3234 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3235 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3236 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3237 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3238 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3239 | |
| 3240 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3241 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3242 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3243 | signature_size, signature_length); |
| 3244 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 3245 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3246 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3247 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3248 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3249 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3250 | |
| 3251 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3252 | } |
| 3253 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3254 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3255 | const psa_key_attributes_t *attributes, |
| 3256 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3257 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3258 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3259 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3260 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3261 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3262 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3263 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3264 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3265 | return mbedtls_psa_rsa_verify_hash( |
| 3266 | attributes, |
| 3267 | key_buffer, key_buffer_size, |
| 3268 | alg, hash, hash_length, |
| 3269 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3270 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3271 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3272 | } else { |
| 3273 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3274 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3275 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3276 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3277 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3278 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3279 | return mbedtls_psa_ecdsa_verify_hash( |
| 3280 | attributes, |
| 3281 | key_buffer, key_buffer_size, |
| 3282 | alg, hash, hash_length, |
| 3283 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3284 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3285 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3286 | } else { |
| 3287 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3288 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3289 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3291 | (void) key_buffer; |
| 3292 | (void) key_buffer_size; |
| 3293 | (void) hash; |
| 3294 | (void) hash_length; |
| 3295 | (void) signature; |
| 3296 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3298 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3299 | } |
| 3300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3301 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3302 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3303 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3304 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3305 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3306 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3307 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3309 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3310 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3311 | |
| 3312 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3313 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3314 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3315 | signature_length); |
| 3316 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 3317 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3318 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3319 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3320 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3321 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3322 | |
| 3323 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3324 | } |
| 3325 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3326 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3327 | static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg, |
| 3328 | mbedtls_rsa_context *rsa) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3329 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3330 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg); |
| 3331 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 3332 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
| 3333 | mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg); |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3334 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3335 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3337 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3338 | psa_algorithm_t alg, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3339 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3340 | size_t input_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3341 | const uint8_t *salt_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3342 | size_t salt_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3343 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3344 | size_t output_size, |
| 3345 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3346 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3347 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3348 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3349 | psa_key_slot_t *slot; |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3350 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3351 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3352 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3353 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3354 | (void) input; |
| 3355 | (void) input_length; |
| 3356 | (void) salt; |
| 3357 | (void) output; |
| 3358 | (void) output_size; |
| 3359 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3360 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3362 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3363 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3364 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3365 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3366 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3367 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3368 | if (status != PSA_SUCCESS) { |
| 3369 | return status; |
| 3370 | } |
| 3371 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3372 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3373 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3374 | goto exit; |
| 3375 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3377 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3378 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3379 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3380 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3381 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3382 | slot->key.data, |
| 3383 | slot->key.bytes, |
| 3384 | &rsa); |
| 3385 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3386 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3387 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3389 | if (output_size < mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3390 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3391 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3392 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3393 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3394 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3395 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3396 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3397 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3398 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3399 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3400 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3401 | mbedtls_rsa_pkcs1_encrypt(rsa, |
| 3402 | mbedtls_psa_get_random, |
| 3403 | MBEDTLS_PSA_RANDOM_STATE, |
| 3404 | MBEDTLS_RSA_PUBLIC, |
| 3405 | input_length, |
| 3406 | input, |
| 3407 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3408 | #else |
| 3409 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3410 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3411 | } else |
| 3412 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3413 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3414 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3415 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3416 | mbedtls_rsa_rsaes_oaep_encrypt(rsa, |
| 3417 | mbedtls_psa_get_random, |
| 3418 | MBEDTLS_PSA_RANDOM_STATE, |
| 3419 | MBEDTLS_RSA_PUBLIC, |
| 3420 | salt, salt_length, |
| 3421 | input_length, |
| 3422 | input, |
| 3423 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3424 | #else |
| 3425 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3426 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3427 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3428 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3429 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3430 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3431 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3432 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3433 | if (status == PSA_SUCCESS) { |
| 3434 | *output_length = mbedtls_rsa_get_len(rsa); |
| 3435 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3436 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3437 | mbedtls_rsa_free(rsa); |
| 3438 | mbedtls_free(rsa); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3439 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3440 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3441 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3442 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3443 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3444 | |
| 3445 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3446 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3447 | |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3448 | LOCAL_INPUT_FREE(input_external, input); |
| 3449 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3450 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3452 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3453 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3455 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3456 | psa_algorithm_t alg, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3457 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3458 | size_t input_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3459 | const uint8_t *salt_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3460 | size_t salt_length, |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3461 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3462 | size_t output_size, |
| 3463 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3464 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3465 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3466 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3467 | psa_key_slot_t *slot; |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3468 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3469 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3470 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3471 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3472 | (void) input; |
| 3473 | (void) input_length; |
| 3474 | (void) salt; |
| 3475 | (void) output; |
| 3476 | (void) output_size; |
| 3477 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3478 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3480 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3481 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3482 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3483 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3484 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3485 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3486 | if (status != PSA_SUCCESS) { |
| 3487 | return status; |
| 3488 | } |
| 3489 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3490 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3491 | goto exit; |
| 3492 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3493 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3494 | if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3495 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3496 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3497 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3498 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3499 | slot->key.data, |
| 3500 | slot->key.bytes, |
| 3501 | &rsa); |
| 3502 | if (status != PSA_SUCCESS) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3503 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3504 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3505 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3506 | if (input_length != mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3507 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3508 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3509 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3510 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3511 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3512 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3513 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3514 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3515 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3516 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3517 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3518 | mbedtls_rsa_pkcs1_decrypt(rsa, |
| 3519 | mbedtls_psa_get_random, |
| 3520 | MBEDTLS_PSA_RANDOM_STATE, |
| 3521 | MBEDTLS_RSA_PRIVATE, |
| 3522 | output_length, |
| 3523 | input, |
| 3524 | output, |
| 3525 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3526 | #else |
| 3527 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3528 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3529 | } else |
| 3530 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3531 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3532 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3533 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3534 | mbedtls_rsa_rsaes_oaep_decrypt(rsa, |
| 3535 | mbedtls_psa_get_random, |
| 3536 | MBEDTLS_PSA_RANDOM_STATE, |
| 3537 | MBEDTLS_RSA_PRIVATE, |
| 3538 | salt, salt_length, |
| 3539 | output_length, |
| 3540 | input, |
| 3541 | output, |
| 3542 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3543 | #else |
| 3544 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3545 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3546 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3547 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3548 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3549 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3550 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3551 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3552 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3553 | mbedtls_rsa_free(rsa); |
| 3554 | mbedtls_free(rsa); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3555 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3556 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3557 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3558 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3559 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3560 | |
| 3561 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3562 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3563 | |
Thomas Daubney | 1a6137b | 2024-01-19 10:26:26 +0000 | [diff] [blame] | 3564 | LOCAL_INPUT_FREE(input_external, input); |
| 3565 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3566 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3567 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3568 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3569 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3570 | |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 3571 | static psa_status_t psa_generate_random_internal(uint8_t *output, |
| 3572 | size_t output_size) |
| 3573 | { |
| 3574 | GUARD_MODULE_INITIALIZED; |
| 3575 | |
| 3576 | psa_status_t status; |
| 3577 | |
| 3578 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 3579 | |
| 3580 | size_t output_length = 0; |
| 3581 | status = mbedtls_psa_external_get_random(&global_data.rng, |
| 3582 | output, output_size, |
| 3583 | &output_length); |
| 3584 | if (status != PSA_SUCCESS) { |
| 3585 | goto exit; |
| 3586 | } |
| 3587 | /* Breaking up a request into smaller chunks is currently not supported |
| 3588 | * for the external RNG interface. */ |
| 3589 | if (output_length != output_size) { |
| 3590 | status = PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 3591 | goto exit; |
| 3592 | } |
| 3593 | status = PSA_SUCCESS; |
| 3594 | |
| 3595 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 3596 | |
| 3597 | while (output_size > 0) { |
| 3598 | size_t request_size = |
| 3599 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 3600 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 3601 | output_size); |
| 3602 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 3603 | output, request_size); |
| 3604 | if (ret != 0) { |
| 3605 | status = mbedtls_to_psa_error(ret); |
| 3606 | goto exit; |
| 3607 | } |
| 3608 | output_size -= request_size; |
| 3609 | output += request_size; |
| 3610 | } |
| 3611 | status = PSA_SUCCESS; |
| 3612 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 3613 | |
| 3614 | exit: |
| 3615 | return status; |
| 3616 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3617 | |
| 3618 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3619 | /****************************************************************/ |
| 3620 | /* Symmetric cryptography */ |
| 3621 | /****************************************************************/ |
| 3622 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3623 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3624 | mbedtls_svc_key_id_t key, |
| 3625 | psa_algorithm_t alg, |
| 3626 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3627 | { |
| 3628 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3629 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3630 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3631 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3632 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3633 | PSA_KEY_USAGE_ENCRYPT : |
| 3634 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3635 | |
| 3636 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3637 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3638 | status = PSA_ERROR_BAD_STATE; |
| 3639 | goto exit; |
| 3640 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3641 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3642 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3643 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3644 | goto exit; |
| 3645 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3647 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3648 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3649 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3650 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3651 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3652 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3653 | * 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] | 3654 | * so we only set it (in the driver wrapper) after resources have been |
| 3655 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3656 | operation->iv_set = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3657 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3658 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3659 | } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) { |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3660 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3661 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3662 | operation->iv_required = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3663 | } |
| 3664 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3665 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3666 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3667 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3668 | }; |
| 3669 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3670 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3671 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3672 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3673 | &attributes, |
| 3674 | slot->key.data, |
| 3675 | slot->key.bytes, |
| 3676 | alg); |
| 3677 | } else { |
| 3678 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3679 | &attributes, |
| 3680 | slot->key.data, |
| 3681 | slot->key.bytes, |
| 3682 | alg); |
| 3683 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3684 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3685 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3686 | if (status != PSA_SUCCESS) { |
| 3687 | psa_cipher_abort(operation); |
| 3688 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3689 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3690 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3691 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3692 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3695 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3696 | mbedtls_svc_key_id_t key, |
| 3697 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3698 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3699 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3702 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3703 | mbedtls_svc_key_id_t key, |
| 3704 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3705 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3706 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3707 | } |
| 3708 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3709 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3710 | uint8_t *iv_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3711 | size_t iv_size, |
| 3712 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3713 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3714 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Sergey | 04eb7c0 | 2023-03-06 15:37:23 -0700 | [diff] [blame] | 3715 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3716 | |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3717 | LOCAL_OUTPUT_DECLARE(iv_external, iv); |
| 3718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3719 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3720 | status = PSA_ERROR_BAD_STATE; |
| 3721 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3724 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3725 | status = PSA_ERROR_BAD_STATE; |
| 3726 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3727 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3728 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3729 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3730 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3731 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3732 | goto exit; |
| 3733 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3735 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3736 | status = PSA_ERROR_GENERIC_ERROR; |
| 3737 | goto exit; |
| 3738 | } |
| 3739 | |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3740 | LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv); |
| 3741 | |
Gabor Mezei | f3c3504 | 2024-03-04 17:15:08 +0100 | [diff] [blame] | 3742 | status = psa_generate_random_internal(iv, default_iv_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3743 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3744 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3745 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3746 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3747 | status = psa_driver_wrapper_cipher_set_iv(operation, |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3748 | iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3749 | |
| 3750 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3751 | if (status == PSA_SUCCESS) { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3752 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3753 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3754 | } else { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3755 | *iv_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3756 | psa_cipher_abort(operation); |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3757 | if (iv != NULL) { |
| 3758 | mbedtls_platform_zeroize(iv, default_iv_length); |
| 3759 | } |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3760 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3761 | |
Gabor Mezei | 8677edd | 2024-02-07 18:10:13 +0100 | [diff] [blame] | 3762 | LOCAL_OUTPUT_FREE(iv_external, iv); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3763 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3764 | } |
| 3765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3766 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 3767 | const uint8_t *iv_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3768 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3769 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3770 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3771 | |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 3772 | LOCAL_INPUT_DECLARE(iv_external, iv); |
| 3773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3774 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3775 | status = PSA_ERROR_BAD_STATE; |
| 3776 | goto exit; |
| 3777 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3778 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3779 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3780 | status = PSA_ERROR_BAD_STATE; |
| 3781 | goto exit; |
| 3782 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3783 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3784 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3785 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3786 | goto exit; |
| 3787 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3788 | |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 3789 | LOCAL_INPUT_ALLOC(iv_external, iv_length, iv); |
| 3790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3791 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3792 | iv, |
| 3793 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3794 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3795 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3796 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3797 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3798 | } else { |
| 3799 | psa_cipher_abort(operation); |
| 3800 | } |
Gabor Mezei | 282bb53 | 2024-02-01 10:39:26 +0100 | [diff] [blame] | 3801 | |
| 3802 | LOCAL_INPUT_FREE(iv_external, iv); |
| 3803 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3804 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3805 | } |
| 3806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3807 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3808 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3809 | size_t input_length, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3810 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3811 | size_t output_size, |
| 3812 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3813 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3814 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3815 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3816 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3817 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3819 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3820 | status = PSA_ERROR_BAD_STATE; |
| 3821 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3822 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3824 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3825 | status = PSA_ERROR_BAD_STATE; |
| 3826 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3827 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3828 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3829 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
Gabor Mezei | ff783e0 | 2024-02-29 10:08:16 +0000 | [diff] [blame] | 3830 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3832 | status = psa_driver_wrapper_cipher_update(operation, |
| 3833 | input, |
| 3834 | input_length, |
| 3835 | output, |
| 3836 | output_size, |
| 3837 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3838 | |
| 3839 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3840 | if (status != PSA_SUCCESS) { |
| 3841 | psa_cipher_abort(operation); |
| 3842 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3843 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3844 | LOCAL_INPUT_FREE(input_external, input); |
| 3845 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3847 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3850 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3851 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3852 | size_t output_size, |
| 3853 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3854 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3855 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3856 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3857 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3858 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3859 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3860 | status = PSA_ERROR_BAD_STATE; |
| 3861 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3862 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3863 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3864 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3865 | status = PSA_ERROR_BAD_STATE; |
| 3866 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3867 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3868 | |
Gabor Mezei | ff783e0 | 2024-02-29 10:08:16 +0000 | [diff] [blame] | 3869 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3871 | status = psa_driver_wrapper_cipher_finish(operation, |
| 3872 | output, |
| 3873 | output_size, |
| 3874 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3875 | |
| 3876 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3877 | if (status == PSA_SUCCESS) { |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3878 | status = psa_cipher_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3879 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3880 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3881 | (void) psa_cipher_abort(operation); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3882 | } |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3883 | |
| 3884 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3885 | |
| 3886 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3889 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3890 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3891 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3892 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3893 | * in use. It's ok to call abort on such an object, and there's |
| 3894 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3895 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3896 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3897 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3898 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3899 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3900 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3901 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3902 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3903 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3904 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3907 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 3908 | psa_algorithm_t alg, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3909 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3910 | size_t input_length, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3911 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3912 | size_t output_size, |
| 3913 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3914 | { |
| 3915 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3916 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3917 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3918 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3919 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3920 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3921 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3922 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3923 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3924 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3925 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3926 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3927 | goto exit; |
| 3928 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3929 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3930 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3931 | PSA_KEY_USAGE_ENCRYPT, |
| 3932 | alg); |
| 3933 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3934 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3935 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3936 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3937 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3938 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3939 | }; |
| 3940 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3941 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 3942 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3943 | status = PSA_ERROR_GENERIC_ERROR; |
| 3944 | goto exit; |
| 3945 | } |
| 3946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3947 | if (default_iv_length > 0) { |
| 3948 | if (output_size < default_iv_length) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3949 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3950 | goto exit; |
| 3951 | } |
| 3952 | |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 3953 | status = psa_generate_random_internal(local_iv, default_iv_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3954 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3955 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3956 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3957 | } |
| 3958 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 3959 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3960 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 3961 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3962 | status = psa_driver_wrapper_cipher_encrypt( |
| 3963 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3964 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3965 | mbedtls_buffer_offset(output, default_iv_length), |
| 3966 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3967 | |
| 3968 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3969 | unlock_status = psa_unlock_key_slot(slot); |
| 3970 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3971 | status = unlock_status; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3972 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3973 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3974 | if (status == PSA_SUCCESS) { |
| 3975 | if (default_iv_length > 0) { |
| 3976 | memcpy(output, local_iv, default_iv_length); |
| 3977 | } |
| 3978 | *output_length += default_iv_length; |
| 3979 | } else { |
| 3980 | *output_length = 0; |
| 3981 | } |
| 3982 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3983 | LOCAL_INPUT_FREE(input_external, input); |
| 3984 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | d2ad886 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 3985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3986 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3987 | } |
| 3988 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3989 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 3990 | psa_algorithm_t alg, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3991 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3992 | size_t input_length, |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 3993 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3994 | size_t output_size, |
| 3995 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3996 | { |
| 3997 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3998 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3999 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4000 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4001 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4002 | LOCAL_INPUT_DECLARE(input_external, input); |
| 4003 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4005 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4006 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4007 | goto exit; |
| 4008 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4009 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4010 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4011 | PSA_KEY_USAGE_DECRYPT, |
| 4012 | alg); |
| 4013 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4014 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4015 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4016 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4017 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4018 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4019 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4021 | if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4022 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4023 | goto exit; |
| 4024 | } |
| 4025 | |
Gabor Mezei | ed96d68 | 2024-01-31 17:45:29 +0100 | [diff] [blame] | 4026 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 4027 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 4028 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4029 | status = psa_driver_wrapper_cipher_decrypt( |
| 4030 | &attributes, slot->key.data, slot->key.bytes, |
| 4031 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4032 | output, output_size, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4033 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4034 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4035 | unlock_status = psa_unlock_key_slot(slot); |
| 4036 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4037 | status = unlock_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4038 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4040 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4041 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4042 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4043 | |
Gabor Mezei | 13a15c2 | 2024-01-24 16:56:00 +0100 | [diff] [blame] | 4044 | LOCAL_INPUT_FREE(input_external, input); |
| 4045 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4046 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4047 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4051 | /****************************************************************/ |
| 4052 | /* AEAD */ |
| 4053 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4054 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4055 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4056 | psa_algorithm_t alg, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4057 | const uint8_t *nonce_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4058 | size_t nonce_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4059 | const uint8_t *additional_data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4060 | size_t additional_data_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4061 | const uint8_t *plaintext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4062 | size_t plaintext_length, |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4063 | uint8_t *ciphertext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4064 | size_t ciphertext_size, |
| 4065 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4066 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4067 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4068 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4069 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4070 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4071 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4072 | LOCAL_INPUT_DECLARE(plaintext_external, plaintext); |
| 4073 | LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext); |
| 4074 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4075 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4076 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4077 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4078 | return PSA_ERROR_NOT_SUPPORTED; |
| 4079 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4080 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4081 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4082 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4083 | if (status != PSA_SUCCESS) { |
| 4084 | return status; |
| 4085 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4086 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4087 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4088 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4089 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4090 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4091 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4092 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data); |
| 4093 | LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext); |
| 4094 | LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext); |
| 4095 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4096 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4097 | &attributes, slot->key.data, slot->key.bytes, |
| 4098 | alg, |
| 4099 | nonce, nonce_length, |
| 4100 | additional_data, additional_data_length, |
| 4101 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4102 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4104 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4105 | memset(ciphertext, 0, ciphertext_size); |
| 4106 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4107 | |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4108 | /* Exit label is only used for buffer copying, prevent unused warnings. */ |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 4109 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
David Horstmann | 21c1a94 | 2023-11-28 19:25:00 +0000 | [diff] [blame] | 4110 | exit: |
| 4111 | #endif |
| 4112 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4113 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4114 | LOCAL_INPUT_FREE(plaintext_external, plaintext); |
| 4115 | LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext); |
| 4116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4117 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4118 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4119 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4120 | } |
| 4121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4122 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4123 | psa_algorithm_t alg, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4124 | const uint8_t *nonce_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4125 | size_t nonce_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4126 | const uint8_t *additional_data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4127 | size_t additional_data_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4128 | const uint8_t *ciphertext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4129 | size_t ciphertext_length, |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4130 | uint8_t *plaintext_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4131 | size_t plaintext_size, |
| 4132 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4133 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4134 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4135 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4136 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4137 | LOCAL_INPUT_DECLARE(nonce_external, nonce); |
| 4138 | LOCAL_INPUT_DECLARE(additional_data_external, additional_data); |
| 4139 | LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext); |
| 4140 | LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext); |
| 4141 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4142 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4144 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4145 | return PSA_ERROR_NOT_SUPPORTED; |
| 4146 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4147 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4148 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4149 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4150 | if (status != PSA_SUCCESS) { |
| 4151 | return status; |
| 4152 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4153 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4154 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4155 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4156 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4157 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4158 | LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce); |
| 4159 | LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, |
| 4160 | additional_data); |
| 4161 | LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext); |
| 4162 | LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext); |
| 4163 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4164 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4165 | &attributes, slot->key.data, slot->key.bytes, |
| 4166 | alg, |
| 4167 | nonce, nonce_length, |
| 4168 | additional_data, additional_data_length, |
| 4169 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4170 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4172 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4173 | memset(plaintext, 0, plaintext_size); |
| 4174 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4175 | |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4176 | /* Exit label is only used for buffer copying, prevent unused warnings. */ |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 4177 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
David Horstmann | 6baf6e9 | 2023-11-28 19:43:53 +0000 | [diff] [blame] | 4178 | exit: |
| 4179 | #endif |
| 4180 | LOCAL_INPUT_FREE(nonce_external, nonce); |
| 4181 | LOCAL_INPUT_FREE(additional_data_external, additional_data); |
| 4182 | LOCAL_INPUT_FREE(ciphertext_external, ciphertext); |
| 4183 | LOCAL_OUTPUT_FREE(plaintext_external, plaintext); |
| 4184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4185 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4187 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4188 | } |
| 4189 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4190 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4191 | /* Generators */ |
| 4192 | /****************************************************************/ |
| 4193 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4195 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4196 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4197 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4198 | #endif /* At least one builtin KDF */ |
| 4199 | |
| 4200 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 4201 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4202 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4203 | static psa_status_t psa_key_derivation_start_hmac( |
| 4204 | psa_mac_operation_t *operation, |
| 4205 | psa_algorithm_t hash_alg, |
| 4206 | const uint8_t *hmac_key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4207 | size_t hmac_key_length) |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4208 | { |
| 4209 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4211 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 4212 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 4213 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4214 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4215 | operation->is_sign = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4216 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4218 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 4219 | &attributes, |
| 4220 | hmac_key, hmac_key_length, |
| 4221 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4223 | psa_reset_key_attributes(&attributes); |
| 4224 | return status; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4225 | } |
| 4226 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4227 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4228 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4229 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4230 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4231 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4232 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4233 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4234 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4235 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4236 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 4237 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 4238 | } else { |
| 4239 | return operation->alg; |
| 4240 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4241 | } |
| 4242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4243 | 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] | 4244 | { |
| 4245 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4246 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 4247 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4248 | /* The object has (apparently) been initialized but it is not |
| 4249 | * in use. It's ok to call abort on such an object, and there's |
| 4250 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4251 | } else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4252 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4253 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4254 | mbedtls_free(operation->ctx.hkdf.info); |
| 4255 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 4256 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4257 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4258 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4259 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4260 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4261 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 4262 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4263 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 4264 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 4265 | operation->ctx.tls12_prf.secret_length); |
| 4266 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4267 | } |
| 4268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4269 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 4270 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 4271 | operation->ctx.tls12_prf.seed_length); |
| 4272 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4273 | } |
| 4274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4275 | if (operation->ctx.tls12_prf.label != NULL) { |
| 4276 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 4277 | operation->ctx.tls12_prf.label_length); |
| 4278 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4281 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4282 | |
| 4283 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4284 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4285 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4286 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4287 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4288 | { |
| 4289 | status = PSA_ERROR_BAD_STATE; |
| 4290 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4291 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 4292 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4293 | } |
| 4294 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4295 | 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] | 4296 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4297 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4298 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4299 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4300 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4303 | *capacity = operation->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4304 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4307 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 4308 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4309 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4310 | if (operation->alg == 0) { |
| 4311 | return PSA_ERROR_BAD_STATE; |
| 4312 | } |
| 4313 | if (capacity > operation->capacity) { |
| 4314 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4315 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4316 | operation->capacity = capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4317 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4318 | } |
| 4319 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4321 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4322 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4323 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 4324 | psa_algorithm_t hash_alg, |
| 4325 | uint8_t *output, |
| 4326 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4327 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4328 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4329 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4330 | psa_status_t status; |
| 4331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4332 | if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) { |
| 4333 | return PSA_ERROR_BAD_STATE; |
| 4334 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4335 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4337 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4338 | /* Copy what remains of the current block */ |
| 4339 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4340 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4341 | n = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4342 | } |
| 4343 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4344 | output += n; |
| 4345 | output_length -= n; |
| 4346 | hkdf->offset_in_block += n; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4347 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4348 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4349 | } |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4350 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4351 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4352 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4353 | * object was corrupted or if this function is called directly |
| 4354 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4355 | if (hkdf->block_number == 0xff) { |
| 4356 | return PSA_ERROR_BAD_STATE; |
| 4357 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4358 | |
| 4359 | /* We need a new block */ |
| 4360 | ++hkdf->block_number; |
| 4361 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4363 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4364 | hash_alg, |
| 4365 | hkdf->prk, |
| 4366 | hash_length); |
| 4367 | if (status != PSA_SUCCESS) { |
| 4368 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4369 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4370 | |
| 4371 | if (hkdf->block_number != 1) { |
| 4372 | status = psa_mac_update(&hkdf->hmac, |
| 4373 | hkdf->output_block, |
| 4374 | hash_length); |
| 4375 | if (status != PSA_SUCCESS) { |
| 4376 | return status; |
| 4377 | } |
| 4378 | } |
| 4379 | status = psa_mac_update(&hkdf->hmac, |
| 4380 | hkdf->info, |
| 4381 | hkdf->info_length); |
| 4382 | if (status != PSA_SUCCESS) { |
| 4383 | return status; |
| 4384 | } |
| 4385 | status = psa_mac_update(&hkdf->hmac, |
| 4386 | &hkdf->block_number, 1); |
| 4387 | if (status != PSA_SUCCESS) { |
| 4388 | return status; |
| 4389 | } |
| 4390 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4391 | hkdf->output_block, |
| 4392 | sizeof(hkdf->output_block), |
| 4393 | &hmac_output_length); |
| 4394 | if (status != PSA_SUCCESS) { |
| 4395 | return status; |
| 4396 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4397 | } |
| 4398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4399 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4400 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4401 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4402 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4403 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4404 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4405 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4406 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4407 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4408 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4409 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 4410 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4411 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4412 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4413 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4414 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4415 | /* We can't be wanting more output after block 0xff, otherwise |
| 4416 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4417 | * prevented this call. It could happen only if the operation |
| 4418 | * object was corrupted or if this function is called directly |
| 4419 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4420 | if (tls12_prf->block_number == 0xff) { |
| 4421 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4422 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4423 | |
| 4424 | /* We need a new block */ |
| 4425 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4426 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4427 | |
| 4428 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4429 | * |
| 4430 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4431 | * |
| 4432 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4433 | * HMAC_hash(secret, A(2) + seed) + |
| 4434 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4435 | * |
| 4436 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4437 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4438 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4439 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4440 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4441 | * is currently extracted as `output_block` and where i is |
| 4442 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4443 | */ |
| 4444 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4445 | status = psa_key_derivation_start_hmac(&hmac, |
| 4446 | hash_alg, |
| 4447 | tls12_prf->secret, |
| 4448 | tls12_prf->secret_length); |
| 4449 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4450 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4451 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4452 | |
| 4453 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4454 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4455 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4456 | * the variable seed and in this instance means it in the context of the |
| 4457 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4458 | status = psa_mac_update(&hmac, |
| 4459 | tls12_prf->label, |
| 4460 | tls12_prf->label_length); |
| 4461 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4462 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4463 | } |
| 4464 | status = psa_mac_update(&hmac, |
| 4465 | tls12_prf->seed, |
| 4466 | tls12_prf->seed_length); |
| 4467 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4468 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4469 | } |
| 4470 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4471 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4472 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4473 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4474 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4475 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4478 | status = psa_mac_sign_finish(&hmac, |
| 4479 | tls12_prf->Ai, hash_length, |
| 4480 | &hmac_output_length); |
| 4481 | if (hmac_output_length != hash_length) { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4482 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4483 | } |
| 4484 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4485 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4486 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4489 | status = psa_key_derivation_start_hmac(&hmac, |
| 4490 | hash_alg, |
| 4491 | tls12_prf->secret, |
| 4492 | tls12_prf->secret_length); |
| 4493 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4494 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4495 | } |
| 4496 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4497 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4498 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4499 | } |
| 4500 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 4501 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4502 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4503 | } |
| 4504 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 4505 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4506 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4507 | } |
| 4508 | status = psa_mac_sign_finish(&hmac, |
| 4509 | tls12_prf->output_block, hash_length, |
| 4510 | &hmac_output_length); |
| 4511 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4512 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4513 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4514 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4515 | |
| 4516 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4517 | cleanup_status = psa_mac_abort(&hmac); |
| 4518 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4519 | status = cleanup_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4520 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4521 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4522 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4523 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4524 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4525 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4526 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4527 | psa_algorithm_t alg, |
| 4528 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4529 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4530 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4531 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 4532 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4533 | psa_status_t status; |
| 4534 | uint8_t offset, length; |
| 4535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4536 | switch (tls12_prf->state) { |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4537 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4538 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4539 | break; |
| 4540 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4541 | break; |
| 4542 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4543 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4544 | } |
| 4545 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4546 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4547 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4548 | if (tls12_prf->left_in_block == 0) { |
| 4549 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 4550 | alg); |
| 4551 | if (status != PSA_SUCCESS) { |
| 4552 | return status; |
| 4553 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4554 | |
| 4555 | continue; |
| 4556 | } |
| 4557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4558 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4559 | length = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4560 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4561 | length = tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4562 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4563 | |
| 4564 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4565 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4566 | output += length; |
| 4567 | output_length -= length; |
| 4568 | tls12_prf->left_in_block -= length; |
| 4569 | } |
| 4570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4571 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4572 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4573 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4574 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4575 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4576 | psa_status_t psa_key_derivation_output_bytes( |
| 4577 | psa_key_derivation_operation_t *operation, |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4578 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4579 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4580 | { |
| 4581 | psa_status_t status; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4582 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 4583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4584 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4585 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4586 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4587 | /* This is a blank operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4588 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4591 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4592 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4593 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4594 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4595 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4596 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4597 | * output_length > 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4598 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4599 | } |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4600 | |
| 4601 | LOCAL_OUTPUT_ALLOC(output_external, output_length, output); |
| 4602 | if (output_length > operation->capacity) { |
| 4603 | operation->capacity = 0; |
| 4604 | /* Go through the error path to wipe all confidential data now |
| 4605 | * that the operation object is useless. */ |
| 4606 | status = PSA_ERROR_INSUFFICIENT_DATA; |
| 4607 | goto exit; |
| 4608 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4609 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4610 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4611 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4612 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4613 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4614 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg, |
| 4615 | output, output_length); |
| 4616 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4617 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4618 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4619 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4620 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4621 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4622 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 4623 | kdf_alg, output, |
| 4624 | output_length); |
| 4625 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4626 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4627 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4628 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4629 | (void) kdf_alg; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4630 | status = PSA_ERROR_BAD_STATE; |
| 4631 | LOCAL_OUTPUT_FREE(output_external, output); |
| 4632 | |
| 4633 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4637 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4638 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4639 | * This allows us to differentiate between exhausted operations and |
| 4640 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4641 | * operations. */ |
| 4642 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4643 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4644 | operation->alg = alg; |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4645 | if (output != NULL) { |
| 4646 | memset(output, '!', output_length); |
| 4647 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4648 | } |
Ryan Everett | 08bd246 | 2024-02-09 16:15:32 +0000 | [diff] [blame] | 4649 | |
| 4650 | LOCAL_OUTPUT_FREE(output_external, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4651 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4654 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4655 | 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] | 4656 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4657 | if (data_size >= 8) { |
| 4658 | mbedtls_des_key_set_parity(data); |
| 4659 | } |
| 4660 | if (data_size >= 16) { |
| 4661 | mbedtls_des_key_set_parity(data + 8); |
| 4662 | } |
| 4663 | if (data_size >= 24) { |
| 4664 | mbedtls_des_key_set_parity(data + 16); |
| 4665 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4666 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4667 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4668 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4669 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4670 | psa_key_slot_t *slot, |
| 4671 | size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4672 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4673 | { |
| 4674 | uint8_t *data = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4675 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4676 | psa_status_t status; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4677 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4679 | if (!key_type_is_raw_bytes(slot->attr.type)) { |
| 4680 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4681 | } |
| 4682 | if (bits % 8 != 0) { |
| 4683 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4684 | } |
| 4685 | data = mbedtls_calloc(1, bytes); |
| 4686 | if (data == NULL) { |
| 4687 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4688 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4689 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4690 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 4691 | if (status != PSA_SUCCESS) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4692 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4693 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4694 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4695 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 4696 | psa_des_set_key_parity(data, bytes); |
| 4697 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4698 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4699 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4700 | status = psa_allocate_buffer_to_slot(slot, bytes); |
| 4701 | if (status != PSA_SUCCESS) { |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4702 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4703 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4704 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4705 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4706 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4707 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4708 | }; |
| 4709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4710 | status = psa_driver_wrapper_import_key(&attributes, |
| 4711 | data, bytes, |
| 4712 | slot->key.data, |
| 4713 | slot->key.bytes, |
| 4714 | &slot->key.bytes, &bits); |
| 4715 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4716 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4717 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4718 | |
| 4719 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4720 | mbedtls_free(data); |
| 4721 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4722 | } |
| 4723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4724 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 4725 | psa_key_derivation_operation_t *operation, |
| 4726 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4727 | { |
| 4728 | psa_status_t status; |
| 4729 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4730 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4731 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4732 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4733 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4734 | /* Reject any attempt to create a zero-length key so that we don't |
| 4735 | * 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] | 4736 | if (psa_get_key_bits(attributes) == 0) { |
| 4737 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4738 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4740 | if (operation->alg == PSA_ALG_NONE) { |
| 4741 | return PSA_ERROR_BAD_STATE; |
| 4742 | } |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4743 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4744 | if (!operation->can_output_key) { |
| 4745 | return PSA_ERROR_NOT_PERMITTED; |
| 4746 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4747 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4748 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 4749 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4750 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4751 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4752 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4753 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4754 | } |
| 4755 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4756 | if (status == PSA_SUCCESS) { |
| 4757 | status = psa_generate_derived_key_internal(slot, |
| 4758 | attributes->core.bits, |
| 4759 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4760 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4761 | if (status == PSA_SUCCESS) { |
| 4762 | status = psa_finish_key_creation(slot, driver, key); |
| 4763 | } |
| 4764 | if (status != PSA_SUCCESS) { |
| 4765 | psa_fail_key_creation(slot, driver); |
| 4766 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4769 | } |
| 4770 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4771 | |
| 4772 | |
| 4773 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4774 | /* Key derivation */ |
| 4775 | /****************************************************************/ |
| 4776 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4777 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4778 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4779 | { |
| 4780 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4781 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4782 | return 1; |
| 4783 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4784 | #endif |
| 4785 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4786 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4787 | return 1; |
| 4788 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4789 | #endif |
| 4790 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4791 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4792 | return 1; |
| 4793 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4794 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4795 | return 0; |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4796 | } |
| 4797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4798 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4799 | { |
| 4800 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4801 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 4802 | psa_hash_abort(&operation); |
| 4803 | return status; |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame] | 4806 | static psa_status_t psa_key_derivation_set_maximum_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4807 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4808 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4809 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4810 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4811 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 4812 | if (hash_size == 0) { |
| 4813 | return PSA_ERROR_NOT_SUPPORTED; |
| 4814 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4815 | |
| 4816 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4817 | * we might fail later, which is somewhat unfriendly and potentially |
| 4818 | * risk-prone. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4819 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 4820 | if (status != PSA_SUCCESS) { |
| 4821 | return status; |
| 4822 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4823 | |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame] | 4824 | #if defined(PSA_WANT_ALG_HKDF) |
| 4825 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4826 | operation->capacity = 255 * hash_size; |
| 4827 | } else |
| 4828 | #endif |
| 4829 | #if defined(PSA_WANT_ALG_TLS12_PRF) |
| 4830 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) && |
| 4831 | (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 4832 | operation->capacity = SIZE_MAX; |
| 4833 | } else |
| 4834 | #endif |
| 4835 | #if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS) |
| 4836 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) && |
| 4837 | (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 4838 | /* Master Secret is always 48 bytes |
| 4839 | * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */ |
| 4840 | operation->capacity = 48U; |
| 4841 | } else |
| 4842 | #endif |
| 4843 | { |
| 4844 | (void) hash_size; |
| 4845 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4846 | } |
| 4847 | return status; |
| 4848 | } |
| 4849 | |
| 4850 | |
| 4851 | static psa_status_t psa_key_derivation_setup_kdf( |
| 4852 | psa_key_derivation_operation_t *operation, |
| 4853 | psa_algorithm_t kdf_alg) |
| 4854 | { |
| 4855 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4856 | * initialisers for this union leave some bytes unspecified.) */ |
| 4857 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
| 4858 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
| 4859 | if (!is_kdf_alg_supported(kdf_alg)) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4860 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4861 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4862 | |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame] | 4863 | psa_status_t status = psa_key_derivation_set_maximum_capacity(operation, |
| 4864 | kdf_alg); |
| 4865 | return status; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4866 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4868 | 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] | 4869 | { |
| 4870 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4871 | if (alg == PSA_ALG_ECDH) { |
| 4872 | return PSA_SUCCESS; |
| 4873 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4874 | #endif |
| 4875 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4876 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4877 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4878 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4880 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 4881 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4882 | { |
| 4883 | psa_status_t status; |
| 4884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4885 | if (operation->alg != 0) { |
| 4886 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4887 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4889 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 4890 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4891 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 4892 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4893 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 4894 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 4895 | status = psa_key_agreement_try_support(ka_alg); |
| 4896 | if (status != PSA_SUCCESS) { |
| 4897 | return status; |
| 4898 | } |
| 4899 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 4900 | #else |
| 4901 | return PSA_ERROR_NOT_SUPPORTED; |
| 4902 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4903 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 4904 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4905 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 4906 | #else |
| 4907 | return PSA_ERROR_NOT_SUPPORTED; |
| 4908 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4909 | } else { |
| 4910 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4911 | } |
| 4912 | |
| 4913 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4914 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4915 | } |
| 4916 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4919 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4920 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 4921 | psa_algorithm_t hash_alg, |
| 4922 | psa_key_derivation_step_t step, |
| 4923 | const uint8_t *data, |
| 4924 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4925 | { |
| 4926 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4927 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4928 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4929 | if (hkdf->state != HKDF_STATE_INIT) { |
| 4930 | return PSA_ERROR_BAD_STATE; |
| 4931 | } else { |
| 4932 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4933 | hash_alg, |
| 4934 | data, data_length); |
| 4935 | if (status != PSA_SUCCESS) { |
| 4936 | return status; |
| 4937 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4938 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4939 | return PSA_SUCCESS; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4940 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4941 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4942 | /* If no salt was provided, use an empty salt. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4943 | if (hkdf->state == HKDF_STATE_INIT) { |
| 4944 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4945 | hash_alg, |
| 4946 | NULL, 0); |
| 4947 | if (status != PSA_SUCCESS) { |
| 4948 | return status; |
| 4949 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4950 | hkdf->state = HKDF_STATE_STARTED; |
| 4951 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4952 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 4953 | return PSA_ERROR_BAD_STATE; |
| 4954 | } |
| 4955 | status = psa_mac_update(&hkdf->hmac, |
| 4956 | data, data_length); |
| 4957 | if (status != PSA_SUCCESS) { |
| 4958 | return status; |
| 4959 | } |
| 4960 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4961 | hkdf->prk, |
| 4962 | sizeof(hkdf->prk), |
| 4963 | &data_length); |
| 4964 | if (status != PSA_SUCCESS) { |
| 4965 | return status; |
| 4966 | } |
| 4967 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4968 | hkdf->block_number = 0; |
| 4969 | hkdf->state = HKDF_STATE_KEYED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4970 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4971 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4972 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 4973 | return PSA_ERROR_BAD_STATE; |
| 4974 | } |
| 4975 | if (hkdf->info_set) { |
| 4976 | return PSA_ERROR_BAD_STATE; |
| 4977 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4978 | hkdf->info_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4979 | if (data_length != 0) { |
| 4980 | hkdf->info = mbedtls_calloc(1, data_length); |
| 4981 | if (hkdf->info == NULL) { |
| 4982 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4983 | } |
| 4984 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4985 | } |
| 4986 | hkdf->info_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4987 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4988 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4989 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4990 | } |
| 4991 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4992 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4993 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4994 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4995 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4996 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 4997 | const uint8_t *data, |
| 4998 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4999 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5000 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 5001 | return PSA_ERROR_BAD_STATE; |
| 5002 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5004 | if (data_length != 0) { |
| 5005 | prf->seed = mbedtls_calloc(1, data_length); |
| 5006 | if (prf->seed == NULL) { |
| 5007 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5008 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5009 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5010 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5011 | prf->seed_length = data_length; |
| 5012 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5013 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5014 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5015 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5016 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5019 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 5020 | const uint8_t *data, |
| 5021 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5022 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5023 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 5024 | return PSA_ERROR_BAD_STATE; |
| 5025 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5026 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5027 | if (data_length != 0) { |
| 5028 | prf->secret = mbedtls_calloc(1, data_length); |
| 5029 | if (prf->secret == NULL) { |
| 5030 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5031 | } |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5032 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5033 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5034 | prf->secret_length = data_length; |
| 5035 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5036 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5037 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5038 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5039 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5040 | } |
| 5041 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5042 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 5043 | const uint8_t *data, |
| 5044 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5045 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5046 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 5047 | return PSA_ERROR_BAD_STATE; |
| 5048 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5049 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5050 | if (data_length != 0) { |
| 5051 | prf->label = mbedtls_calloc(1, data_length); |
| 5052 | if (prf->label == NULL) { |
| 5053 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5054 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5056 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5057 | prf->label_length = data_length; |
| 5058 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5059 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5060 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5062 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5063 | } |
| 5064 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5065 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 5066 | psa_key_derivation_step_t step, |
| 5067 | const uint8_t *data, |
| 5068 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5069 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5070 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5071 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5072 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5073 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5074 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5075 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5076 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5077 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5078 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5079 | } |
| 5080 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5081 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5082 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5083 | |
| 5084 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5085 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5086 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5087 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5088 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5089 | { |
| 5090 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5091 | 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] | 5092 | uint8_t *cur = pms; |
| 5093 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5094 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 5095 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5096 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5097 | |
| 5098 | /* Quoting RFC 4279, Section 2: |
| 5099 | * |
| 5100 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5101 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5102 | * uint16 with the value N, and the PSK itself. |
| 5103 | */ |
| 5104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5105 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 5106 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 5107 | memset(cur, 0, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5108 | cur += data_length; |
| 5109 | *cur++ = pms[0]; |
| 5110 | *cur++ = pms[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5111 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5112 | cur += data_length; |
| 5113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5114 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5116 | mbedtls_platform_zeroize(pms, sizeof(pms)); |
| 5117 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5118 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5119 | |
| 5120 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5121 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5122 | psa_key_derivation_step_t step, |
| 5123 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5124 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5125 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5126 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 5127 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 5128 | data, data_length); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5129 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5131 | return psa_tls12_prf_input(prf, step, data, data_length); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5132 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5133 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5134 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5135 | /** Check whether the given key type is acceptable for the given |
| 5136 | * input step of a key derivation. |
| 5137 | * |
| 5138 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5139 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5140 | * Both secret and non-secret inputs can alternatively have the type |
| 5141 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5142 | * that the input was passed as a buffer rather than via a key object. |
| 5143 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5144 | static int psa_key_derivation_check_input_type( |
| 5145 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5146 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5147 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5148 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5149 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5150 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 5151 | return PSA_SUCCESS; |
| 5152 | } |
| 5153 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 5154 | return PSA_SUCCESS; |
| 5155 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5156 | break; |
| 5157 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5158 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5159 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5160 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5161 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 5162 | return PSA_SUCCESS; |
| 5163 | } |
| 5164 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 5165 | return PSA_SUCCESS; |
| 5166 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5167 | break; |
| 5168 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5169 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5172 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5173 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5174 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5175 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5176 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5177 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5178 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5179 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5180 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5182 | status = psa_key_derivation_check_input_type(step, key_type); |
| 5183 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5184 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5185 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5186 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5187 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5188 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 5189 | status = psa_hkdf_input(&operation->ctx.hkdf, |
| 5190 | PSA_ALG_HKDF_GET_HASH(kdf_alg), |
| 5191 | step, data, data_length); |
| 5192 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5193 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5195 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 5196 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 5197 | step, data, data_length); |
| 5198 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5199 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5200 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5201 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5202 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 5203 | step, data, data_length); |
| 5204 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5205 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5206 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5207 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5208 | (void) data; |
| 5209 | (void) data_length; |
| 5210 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5211 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5212 | } |
| 5213 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5214 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5215 | if (status != PSA_SUCCESS) { |
| 5216 | psa_key_derivation_abort(operation); |
| 5217 | } |
| 5218 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5219 | } |
| 5220 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5221 | psa_status_t psa_key_derivation_input_bytes( |
| 5222 | psa_key_derivation_operation_t *operation, |
| 5223 | psa_key_derivation_step_t step, |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5224 | const uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5225 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5226 | { |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5227 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5228 | LOCAL_INPUT_DECLARE(data_external, data); |
| 5229 | |
| 5230 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 5231 | |
| 5232 | status = psa_key_derivation_input_internal(operation, step, |
| 5233 | PSA_KEY_TYPE_NONE, |
| 5234 | data, data_length); |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 5235 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5236 | exit: |
Ryan Everett | d0d12fb | 2024-02-12 09:19:29 +0000 | [diff] [blame] | 5237 | #endif |
Ryan Everett | 6f68206 | 2024-02-09 16:18:39 +0000 | [diff] [blame] | 5238 | LOCAL_INPUT_FREE(data_external, data); |
| 5239 | return status; |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5242 | psa_status_t psa_key_derivation_input_key( |
| 5243 | psa_key_derivation_operation_t *operation, |
| 5244 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5245 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5246 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5247 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5248 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5249 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5250 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5251 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5252 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 5253 | if (status != PSA_SUCCESS) { |
| 5254 | psa_key_derivation_abort(operation); |
| 5255 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5256 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5257 | |
| 5258 | /* Passing a key object as a SECRET input unlocks the permission |
| 5259 | * to output to a key object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5260 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5261 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5262 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5264 | status = psa_key_derivation_input_internal(operation, |
| 5265 | step, slot->attr.type, |
| 5266 | slot->key.data, |
| 5267 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5269 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5271 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5272 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5273 | |
| 5274 | |
| 5275 | |
| 5276 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5277 | /* Key agreement */ |
| 5278 | /****************************************************************/ |
| 5279 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5281 | static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key, |
| 5282 | size_t peer_key_length, |
| 5283 | const mbedtls_ecp_keypair *our_key, |
| 5284 | uint8_t *shared_secret, |
| 5285 | size_t shared_secret_size, |
| 5286 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5287 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5288 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5289 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5290 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5291 | size_t bits = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5292 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 5293 | mbedtls_ecdh_init(&ecdh); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5294 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5295 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5296 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5297 | bits, |
| 5298 | peer_key, |
| 5299 | peer_key_length, |
| 5300 | &their_key); |
| 5301 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5302 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5303 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5304 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5305 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5306 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 5307 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5308 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5309 | } |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5310 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5311 | mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS)); |
| 5312 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5313 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5314 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5315 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5316 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5317 | mbedtls_ecdh_calc_secret(&ecdh, |
| 5318 | shared_secret_length, |
| 5319 | shared_secret, shared_secret_size, |
| 5320 | mbedtls_psa_get_random, |
| 5321 | MBEDTLS_PSA_RANDOM_STATE)); |
| 5322 | if (status != PSA_SUCCESS) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5323 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5324 | } |
| 5325 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5326 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5327 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5328 | |
| 5329 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5330 | if (status != PSA_SUCCESS) { |
| 5331 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 5332 | } |
| 5333 | mbedtls_ecdh_free(&ecdh); |
| 5334 | mbedtls_ecp_keypair_free(their_key); |
| 5335 | mbedtls_free(their_key); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5337 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5338 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5339 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5340 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5341 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5342 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5343 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 5344 | psa_key_slot_t *private_key, |
| 5345 | const uint8_t *peer_key, |
| 5346 | size_t peer_key_length, |
| 5347 | uint8_t *shared_secret, |
| 5348 | size_t shared_secret_size, |
| 5349 | size_t *shared_secret_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5350 | { |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5351 | mbedtls_ecp_keypair *ecp = NULL; |
| 5352 | psa_status_t status; |
Agathiyan Bragadeesh | e7eb805 | 2023-07-31 16:16:38 +0100 | [diff] [blame] | 5353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5354 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5355 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5356 | case PSA_ALG_ECDH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5357 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) { |
| 5358 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5359 | } |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5360 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5361 | private_key->attr.type, |
| 5362 | private_key->attr.bits, |
| 5363 | private_key->key.data, |
| 5364 | private_key->key.bytes, |
| 5365 | &ecp); |
| 5366 | if (status != PSA_SUCCESS) { |
| 5367 | return status; |
| 5368 | } |
| 5369 | status = psa_key_agreement_ecdh(peer_key, peer_key_length, |
| 5370 | ecp, |
| 5371 | shared_secret, shared_secret_size, |
| 5372 | shared_secret_length); |
| 5373 | mbedtls_ecp_keypair_free(ecp); |
| 5374 | mbedtls_free(ecp); |
| 5375 | return status; |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5376 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5377 | default: |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5378 | (void) ecp; |
| 5379 | (void) status; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5380 | (void) private_key; |
| 5381 | (void) peer_key; |
| 5382 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5383 | (void) shared_secret; |
| 5384 | (void) shared_secret_size; |
| 5385 | (void) shared_secret_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5386 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5387 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5390 | /* 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] | 5391 | * to potentially free embedded data structures and wipe confidential data. |
| 5392 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5393 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 5394 | psa_key_derivation_step_t step, |
| 5395 | psa_key_slot_t *private_key, |
| 5396 | const uint8_t *peer_key, |
| 5397 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5398 | { |
| 5399 | psa_status_t status; |
| 5400 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5401 | size_t shared_secret_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5402 | 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] | 5403 | |
| 5404 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5405 | * secret. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5406 | status = psa_key_agreement_raw_internal(ka_alg, |
| 5407 | private_key, |
| 5408 | peer_key, peer_key_length, |
| 5409 | shared_secret, |
| 5410 | sizeof(shared_secret), |
| 5411 | &shared_secret_length); |
| 5412 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5413 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5414 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5415 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5416 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5417 | * the shared secret. A shared secret is permitted wherever a key |
| 5418 | * of type DERIVE is permitted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5419 | status = psa_key_derivation_input_internal(operation, step, |
| 5420 | PSA_KEY_TYPE_DERIVE, |
| 5421 | shared_secret, |
| 5422 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5423 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5424 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 5425 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5428 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 5429 | psa_key_derivation_step_t step, |
| 5430 | mbedtls_svc_key_id_t private_key, |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5431 | const uint8_t *peer_key_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5432 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5433 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5434 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5435 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5436 | psa_key_slot_t *slot; |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5437 | LOCAL_INPUT_DECLARE(peer_key_external, peer_key); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5438 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5439 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5440 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5441 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5442 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5443 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 5444 | if (status != PSA_SUCCESS) { |
| 5445 | return status; |
| 5446 | } |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5447 | |
Thomas Daubney | ca92831 | 2024-03-12 17:53:30 +0000 | [diff] [blame] | 5448 | LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5449 | status = psa_key_agreement_internal(operation, step, |
| 5450 | slot, |
| 5451 | peer_key, peer_key_length); |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5452 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 5453 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5454 | exit: |
Thomas Daubney | 3c0c6b1 | 2024-02-15 14:25:08 +0000 | [diff] [blame] | 5455 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5456 | if (status != PSA_SUCCESS) { |
| 5457 | psa_key_derivation_abort(operation); |
| 5458 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5459 | /* If a private key has been added as SECRET, we allow the derived |
| 5460 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5461 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5462 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5463 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5464 | } |
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 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5467 | |
Thomas Daubney | 9da359f | 2024-02-15 13:15:47 +0000 | [diff] [blame] | 5468 | LOCAL_INPUT_FREE(peer_key_external, peer_key); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5469 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5470 | } |
| 5471 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5472 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 5473 | mbedtls_svc_key_id_t private_key, |
Thomas Daubney | 4304276 | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 5474 | const uint8_t *peer_key_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5475 | size_t peer_key_length, |
Thomas Daubney | 4304276 | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 5476 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5477 | size_t output_size, |
| 5478 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5479 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5480 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5481 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5482 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5483 | size_t expected_length; |
Thomas Daubney | 4304276 | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 5484 | LOCAL_INPUT_DECLARE(peer_key_external, peer_key); |
| 5485 | LOCAL_OUTPUT_DECLARE(output_external, output); |
Thomas Daubney | 0736df3 | 2024-02-21 12:28:20 +0000 | [diff] [blame] | 5486 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5488 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5489 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5490 | goto exit; |
| 5491 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5492 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5493 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 5494 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5495 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5496 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5497 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5498 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5499 | * for the output size. The PSA specification only guarantees that this |
| 5500 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5501 | * but it might be nice to allow smaller buffers if the output fits. |
| 5502 | * At the time of writing this comment, with only ECDH implemented, |
| 5503 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5504 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5505 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5506 | expected_length = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5507 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 5508 | if (output_size < expected_length) { |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5509 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5510 | goto exit; |
| 5511 | } |
| 5512 | |
Thomas Daubney | 4304276 | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 5513 | LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5514 | status = psa_key_agreement_raw_internal(alg, slot, |
| 5515 | peer_key, peer_key_length, |
| 5516 | output, output_size, |
| 5517 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5518 | |
| 5519 | exit: |
Thomas Daubney | 09cf4f2 | 2024-02-22 11:08:22 +0000 | [diff] [blame] | 5520 | /* Check for successful allocation of output, |
| 5521 | * with an unsuccessful status. */ |
Thomas Daubney | 2ea8d8f | 2024-02-21 15:16:01 +0000 | [diff] [blame] | 5522 | if (output != NULL && status != PSA_SUCCESS) { |
| 5523 | /* If an error happens and is not handled properly, the output |
Thomas Daubney | 09cf4f2 | 2024-02-22 11:08:22 +0000 | [diff] [blame] | 5524 | * may be used as a key to protect sensitive data. Arrange for such |
| 5525 | * a key to be random, which is likely to result in decryption or |
| 5526 | * verification errors. This is better than filling the buffer with |
| 5527 | * some constant data such as zeros, which would result in the data |
| 5528 | * being protected with a reproducible, easily knowable key. |
| 5529 | */ |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5530 | psa_generate_random_internal(output, output_size); |
Thomas Daubney | 2ea8d8f | 2024-02-21 15:16:01 +0000 | [diff] [blame] | 5531 | *output_length = output_size; |
Thomas Daubney | 09cf4f2 | 2024-02-22 11:08:22 +0000 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | if (output == NULL) { |
Thomas Daubney | 0736df3 | 2024-02-21 12:28:20 +0000 | [diff] [blame] | 5535 | /* output allocation failed. */ |
| 5536 | *output_length = 0; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5537 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5538 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5539 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5540 | |
Thomas Daubney | 4304276 | 2024-02-15 12:57:26 +0000 | [diff] [blame] | 5541 | LOCAL_INPUT_FREE(peer_key_external, peer_key); |
| 5542 | LOCAL_OUTPUT_FREE(output_external, output); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5543 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5544 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5545 | |
| 5546 | |
| 5547 | /****************************************************************/ |
| 5548 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5549 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5550 | |
Gilles Peskine | 9a9d5ee | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 5551 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5552 | #include "mbedtls/entropy_poll.h" |
| 5553 | #endif |
| 5554 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5555 | /** Initialize the PSA random generator. |
| 5556 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5557 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5558 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5559 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5560 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5561 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5562 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5563 | /* Set default configuration if |
| 5564 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5565 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5566 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5567 | } |
| 5568 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5569 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5570 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5572 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5573 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5574 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5575 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5576 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5577 | mbedtls_entropy_add_source(&rng->entropy, |
| 5578 | mbedtls_nv_seed_poll, NULL, |
| 5579 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5580 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5581 | #endif |
| 5582 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5583 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5584 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | /** Deinitialize the PSA random generator. |
| 5588 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5589 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5590 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5591 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5592 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5593 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5594 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 5595 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5596 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | /** Seed the PSA random generator. |
| 5600 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5601 | 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] | 5602 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5603 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5604 | /* Do nothing: the external RNG seeds itself. */ |
| 5605 | (void) rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5606 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5607 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5608 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5609 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 5610 | drbg_seed, sizeof(drbg_seed) - 1); |
| 5611 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5612 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5613 | } |
| 5614 | |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5615 | psa_status_t psa_generate_random(uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5616 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5617 | { |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5618 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5619 | |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5620 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 5621 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5622 | |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5623 | status = psa_generate_random_internal(output, output_size); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5624 | |
David Horstmann | 4201533 | 2024-03-13 15:42:31 +0000 | [diff] [blame^] | 5625 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5626 | exit: |
David Horstmann | 10e44f3 | 2024-02-28 14:17:10 +0000 | [diff] [blame] | 5627 | #endif |
David Horstmann | 65bf12c | 2024-02-06 15:27:49 +0000 | [diff] [blame] | 5628 | LOCAL_OUTPUT_FREE(output_external, output); |
| 5629 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5630 | } |
| 5631 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5632 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5633 | * |
| 5634 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5635 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5636 | * PSA API doesn't support passing an explicit state. |
| 5637 | * |
| 5638 | * In the non-external case, psa_generate_random() calls an |
| 5639 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5640 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5641 | * instead of doing this back-and-forth between the PSA API and the |
| 5642 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5643 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5644 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5645 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5646 | int mbedtls_psa_get_random(void *p_rng, |
| 5647 | unsigned char *output, |
| 5648 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5649 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5650 | /* This function takes a pointer to the RNG state because that's what |
| 5651 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5652 | * its own state internally and doesn't let the caller access that state. |
| 5653 | * So we just ignore the state parameter, and in practice we'll pass |
| 5654 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5655 | (void) p_rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5656 | psa_status_t status = psa_generate_random(output, output_size); |
| 5657 | if (status == PSA_SUCCESS) { |
| 5658 | return 0; |
| 5659 | } else { |
| 5660 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 5661 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5662 | } |
| 5663 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5664 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5665 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5666 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 5667 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5668 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5669 | if (global_data.initialized) { |
| 5670 | return PSA_ERROR_NOT_PERMITTED; |
| 5671 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5672 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5673 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 5674 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 5675 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 5676 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5677 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5679 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5680 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5681 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5682 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5683 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5684 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5685 | * \param type The key type |
| 5686 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5687 | * |
| 5688 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5689 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5690 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5691 | * The size in bits of the key is not valid. |
| 5692 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5693 | * The type and/or the size in bits of the key or the combination of |
| 5694 | * the two is not supported. |
| 5695 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5696 | 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] | 5697 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5698 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5699 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5700 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5701 | if (key_type_is_raw_bytes(type)) { |
| 5702 | status = validate_unstructured_key_bit_size(type, bits); |
| 5703 | if (status != PSA_SUCCESS) { |
| 5704 | return status; |
| 5705 | } |
| 5706 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5707 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5708 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5709 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 5710 | return PSA_ERROR_NOT_SUPPORTED; |
| 5711 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5712 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5713 | /* Accept only byte-aligned keys, for the same reasons as |
| 5714 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5715 | if (bits % 8 != 0) { |
| 5716 | return PSA_ERROR_NOT_SUPPORTED; |
| 5717 | } |
| 5718 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5719 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5720 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5721 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5722 | 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] | 5723 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5724 | return PSA_SUCCESS; |
| 5725 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5726 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5727 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5728 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5729 | } |
| 5730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5731 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5732 | } |
| 5733 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5734 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5735 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5736 | 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] | 5737 | { |
| 5738 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5739 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5740 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5741 | if ((attributes->domain_parameters == NULL) && |
| 5742 | (attributes->domain_parameters_size != 0)) { |
| 5743 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5744 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5745 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5746 | if (key_type_is_raw_bytes(type)) { |
| 5747 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 5748 | if (status != PSA_SUCCESS) { |
| 5749 | return status; |
| 5750 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5751 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5752 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5753 | if (type == PSA_KEY_TYPE_DES) { |
| 5754 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 5755 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5756 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5757 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5758 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5759 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5760 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5761 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 5762 | return mbedtls_psa_rsa_generate_key(attributes, |
| 5763 | key_buffer, |
| 5764 | key_buffer_size, |
| 5765 | key_buffer_length); |
| 5766 | } else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5767 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5768 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5769 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5770 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5771 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5772 | return mbedtls_psa_ecp_generate_key(attributes, |
| 5773 | key_buffer, |
| 5774 | key_buffer_size, |
| 5775 | key_buffer_length); |
| 5776 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5777 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5778 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5779 | (void) key_buffer_length; |
| 5780 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5781 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5783 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5784 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5786 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 5787 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5788 | { |
| 5789 | psa_status_t status; |
| 5790 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5791 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5792 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5793 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5794 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5795 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5796 | /* Reject any attempt to create a zero-length key so that we don't |
| 5797 | * 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] | 5798 | if (psa_get_key_bits(attributes) == 0) { |
| 5799 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5800 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5801 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5802 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5803 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 5804 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5805 | } |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5807 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 5808 | &slot, &driver); |
| 5809 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5810 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5811 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5812 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5813 | /* In the case of a transparent key or an opaque key stored in local |
| 5814 | * storage (thus not in the case of generating a key in a secure element |
| 5815 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5816 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5817 | if (slot->key.data == NULL) { |
| 5818 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 5819 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5820 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5821 | attributes->core.type, attributes->core.bits); |
| 5822 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5823 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5824 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5825 | |
| 5826 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5827 | attributes->core.type, |
| 5828 | attributes->core.bits); |
| 5829 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5830 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5831 | attributes, &key_buffer_size); |
| 5832 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5833 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5834 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5835 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5836 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5837 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 5838 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5839 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5840 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5841 | } |
| 5842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5843 | status = psa_driver_wrapper_generate_key(attributes, |
| 5844 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5846 | if (status != PSA_SUCCESS) { |
| 5847 | psa_remove_key_data_from_memory(slot); |
| 5848 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5849 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5850 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5851 | if (status == PSA_SUCCESS) { |
| 5852 | status = psa_finish_key_creation(slot, driver, key); |
| 5853 | } |
| 5854 | if (status != PSA_SUCCESS) { |
| 5855 | psa_fail_key_creation(slot, driver); |
| 5856 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5857 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5858 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5859 | } |
| 5860 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5861 | /****************************************************************/ |
| 5862 | /* Module setup */ |
| 5863 | /****************************************************************/ |
| 5864 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5865 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5866 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5867 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 5868 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5869 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5870 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5871 | return PSA_ERROR_BAD_STATE; |
| 5872 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5873 | global_data.rng.entropy_init = entropy_init; |
| 5874 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5875 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5876 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5877 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5879 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5880 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5881 | psa_wipe_all_key_slots(); |
| 5882 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5883 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5884 | } |
| 5885 | /* Wipe all remaining data, including configuration. |
| 5886 | * In particular, this sets all state indicator to the value |
| 5887 | * indicating "uninitialized". */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5888 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5889 | |
| 5890 | /* Terminate drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5891 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5892 | } |
| 5893 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5894 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5895 | /** Recover a transaction that was interrupted by a power failure. |
| 5896 | * |
| 5897 | * This function is called during initialization, before psa_crypto_init() |
| 5898 | * returns. If this function returns a failure status, the initialization |
| 5899 | * fails. |
| 5900 | */ |
| 5901 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5902 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5903 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5904 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5905 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5906 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5907 | /* TODO - fall through to the failure case until this |
| 5908 | * is implemented. |
| 5909 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5910 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5911 | default: |
| 5912 | /* We found an unsupported transaction in the storage. |
| 5913 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5914 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5919 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5920 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5921 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5922 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5923 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5924 | if (global_data.initialized != 0) { |
| 5925 | return PSA_SUCCESS; |
| 5926 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5927 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5928 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5929 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5930 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5931 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 5932 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5933 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5934 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5935 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5936 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5937 | status = psa_initialize_key_slots(); |
| 5938 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5939 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5940 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5941 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5942 | /* Init drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5943 | status = psa_driver_wrapper_init(); |
| 5944 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5945 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5946 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5947 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5948 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5949 | status = psa_crypto_load_transaction(); |
| 5950 | if (status == PSA_SUCCESS) { |
| 5951 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 5952 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5953 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5954 | } |
| 5955 | status = psa_crypto_stop_transaction(); |
| 5956 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5957 | /* There's no transaction to complete. It's all good. */ |
| 5958 | status = PSA_SUCCESS; |
| 5959 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5960 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5961 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5962 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5963 | global_data.initialized = 1; |
| 5964 | |
| 5965 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5966 | if (status != PSA_SUCCESS) { |
| 5967 | mbedtls_psa_crypto_free(); |
| 5968 | } |
| 5969 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
David Horstmann | 114d824 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 5972 | /* Memory copying test hooks. These are called before input copy, after input |
| 5973 | * copy, before output copy and after output copy, respectively. |
| 5974 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 5975 | * while they are copied. */ |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5976 | #if defined(MBEDTLS_TEST_HOOKS) |
| 5977 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5978 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5979 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5980 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5981 | #endif |
| 5982 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5983 | /** Copy from an input buffer to a local copy. |
| 5984 | * |
| 5985 | * \param[in] input Pointer to input buffer. |
| 5986 | * \param[in] input_len Length of the input buffer. |
| 5987 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 5988 | * \param[out] input_copy_len Length of the local copy buffer. |
| 5989 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 5990 | * copied. |
| 5991 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 5992 | * copy is too small to hold contents of the |
| 5993 | * input buffer. |
| 5994 | */ |
| 5995 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5996 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 5997 | uint8_t *input_copy, size_t input_copy_len) |
| 5998 | { |
| 5999 | if (input_len > input_copy_len) { |
David Horstmann | 8684ad5 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 6000 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6003 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 6004 | if (psa_input_pre_copy_hook != NULL) { |
| 6005 | psa_input_pre_copy_hook(input, input_len); |
| 6006 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6007 | #endif |
| 6008 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 6009 | if (input_len > 0) { |
| 6010 | memcpy(input_copy, input, input_len); |
| 6011 | } |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 6012 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6013 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 6014 | if (psa_input_post_copy_hook != NULL) { |
| 6015 | psa_input_post_copy_hook(input, input_len); |
| 6016 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6017 | #endif |
| 6018 | |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 6019 | return PSA_SUCCESS; |
| 6020 | } |
| 6021 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 6022 | /** Copy from a local output buffer into a user-supplied one. |
| 6023 | * |
| 6024 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 6025 | * \param[in] output_copy_len Length of the local buffer. |
| 6026 | * \param[out] output Pointer to user-supplied output buffer. |
| 6027 | * \param[out] output_len Length of the user-supplied output buffer. |
| 6028 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 6029 | * copied. |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 6030 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 6031 | * user-supplied output buffer is too small to |
| 6032 | * hold the contents of the local buffer. |
| 6033 | */ |
| 6034 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 6035 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 6036 | uint8_t *output, size_t output_len) |
| 6037 | { |
| 6038 | if (output_len < output_copy_len) { |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 6039 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 6040 | } |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 6041 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6042 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 6043 | if (psa_output_pre_copy_hook != NULL) { |
| 6044 | psa_output_pre_copy_hook(output, output_len); |
| 6045 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6046 | #endif |
| 6047 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 6048 | if (output_copy_len > 0) { |
| 6049 | memcpy(output, output_copy, output_copy_len); |
| 6050 | } |
| 6051 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6052 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 6053 | if (psa_output_post_copy_hook != NULL) { |
| 6054 | psa_output_post_copy_hook(output, output_len); |
| 6055 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 6056 | #endif |
| 6057 | |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 6058 | return PSA_SUCCESS; |
| 6059 | } |
| 6060 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6061 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 6062 | psa_crypto_local_input_t *local_input) |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6063 | { |
| 6064 | psa_status_t status; |
| 6065 | |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 6066 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6067 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 6068 | if (input_len == 0) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6069 | return PSA_SUCCESS; |
| 6070 | } |
| 6071 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6072 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 6073 | if (local_input->buffer == NULL) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6074 | /* Since we dealt with the zero-length case above, we know that |
| 6075 | * a NULL return value means a failure of allocation. */ |
| 6076 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6077 | } |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6078 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6079 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6080 | local_input->length = input_len; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6081 | |
| 6082 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6083 | local_input->buffer, local_input->length); |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6084 | if (status != PSA_SUCCESS) { |
| 6085 | goto error; |
| 6086 | } |
| 6087 | |
| 6088 | return PSA_SUCCESS; |
| 6089 | |
| 6090 | error: |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6091 | mbedtls_free(local_input->buffer); |
| 6092 | local_input->buffer = NULL; |
| 6093 | local_input->length = 0; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 6094 | return status; |
| 6095 | } |
| 6096 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6097 | 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] | 6098 | { |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 6099 | mbedtls_free(local_input->buffer); |
| 6100 | local_input->buffer = NULL; |
| 6101 | local_input->length = 0; |
David Horstmann | fa2d75d | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6104 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 6105 | psa_crypto_local_output_t *local_output) |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6106 | { |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 6107 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6108 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 6109 | if (output_len == 0) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6110 | return PSA_SUCCESS; |
| 6111 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6112 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 6113 | if (local_output->buffer == NULL) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6114 | /* Since we dealt with the zero-length case above, we know that |
| 6115 | * a NULL return value means a failure of allocation. */ |
| 6116 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6117 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6118 | local_output->length = output_len; |
| 6119 | local_output->original = output; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 6120 | |
| 6121 | return PSA_SUCCESS; |
| 6122 | } |
| 6123 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6124 | 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] | 6125 | { |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 6126 | psa_status_t status; |
| 6127 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6128 | if (local_output->buffer == NULL) { |
| 6129 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6130 | return PSA_SUCCESS; |
| 6131 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6132 | if (local_output->original == NULL) { |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6133 | /* We have an internal copy but nothing to copy back to. */ |
| 6134 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 6135 | } |
| 6136 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6137 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 6138 | local_output->original, local_output->length); |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 6139 | if (status != PSA_SUCCESS) { |
| 6140 | return status; |
| 6141 | } |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6142 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 6143 | mbedtls_free(local_output->buffer); |
| 6144 | local_output->buffer = NULL; |
| 6145 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 6146 | |
| 6147 | return PSA_SUCCESS; |
| 6148 | } |
| 6149 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6150 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |