Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 9 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 12 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 13 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 14 | #include "check_crypto_config.h" |
| 15 | #endif |
| 16 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 17 | #include "psa/crypto.h" |
| 18 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 19 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 20 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 21 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 22 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 23 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 24 | #include "psa_crypto_hash.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 25 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 26 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 27 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 29 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 30 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 31 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 32 | /* Include internal declarations that are useful for implementing persistently |
| 33 | * stored keys. */ |
| 34 | #include "psa_crypto_storage.h" |
| 35 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 36 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 37 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | #include "mbedtls/platform.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 41 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 42 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 43 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 44 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 45 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 46 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 47 | #include "mbedtls/blowfish.h" |
| 48 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 49 | #include "mbedtls/chacha20.h" |
| 50 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/cipher.h" |
| 52 | #include "mbedtls/ccm.h" |
| 53 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 54 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 55 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 56 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 57 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 58 | #include "mbedtls/error.h" |
| 59 | #include "mbedtls/gcm.h" |
| 60 | #include "mbedtls/md2.h" |
| 61 | #include "mbedtls/md4.h" |
| 62 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 63 | #include "mbedtls/md.h" |
| 64 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 65 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 67 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 69 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 70 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 71 | #include "mbedtls/sha1.h" |
| 72 | #include "mbedtls/sha256.h" |
| 73 | #include "mbedtls/sha512.h" |
| 74 | #include "mbedtls/xtea.h" |
| 75 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 77 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 78 | /****************************************************************/ |
| 79 | /* Global data, support functions and library management */ |
| 80 | /****************************************************************/ |
| 81 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | static int key_type_is_raw_bytes(psa_key_type_t type) |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 83 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 87 | /* Values for psa_global_data_t::rng_state */ |
| 88 | #define RNG_NOT_INITIALIZED 0 |
| 89 | #define RNG_INITIALIZED 1 |
| 90 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 91 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | typedef struct { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 93 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 94 | unsigned rng_state : 2; |
Gilles Peskine | b8006a6 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 95 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 96 | } psa_global_data_t; |
| 97 | |
| 98 | static psa_global_data_t global_data; |
| 99 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 100 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 101 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 102 | &global_data.rng.drbg; |
| 103 | #endif |
| 104 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 105 | #define GUARD_MODULE_INITIALIZED \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | if (global_data.initialized == 0) \ |
| 107 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 108 | |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 109 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 110 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 111 | /* Declare a local copy of an input buffer and a variable that will be used |
| 112 | * to store a pointer to the start of the buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 113 | * |
| 114 | * Note: This macro must be called before any operations which may jump to |
| 115 | * the exit label, so that the local input copy object is safe to be freed. |
| 116 | * |
| 117 | * Assumptions: |
| 118 | * - input is the name of a pointer to the buffer to be copied |
| 119 | * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 120 | * - input_copy_name is a name that is unused in the current scope |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 121 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 122 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 123 | psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \ |
| 124 | const uint8_t *input_copy_name = NULL; |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 125 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 126 | /* Allocate a copy of the buffer input and set the pointer input_copy to |
| 127 | * point to the start of the copy. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 128 | * |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 129 | * Assumptions: |
| 130 | * - psa_status_t status exists |
| 131 | * - An exit label is declared |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 132 | * - input is the name of a pointer to the buffer to be copied |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 133 | * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 134 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 135 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 136 | status = psa_crypto_local_input_alloc(input, length, \ |
| 137 | &LOCAL_INPUT_COPY_OF_##input); \ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 138 | if (status != PSA_SUCCESS) { \ |
| 139 | goto exit; \ |
| 140 | } \ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 141 | input_copy = LOCAL_INPUT_COPY_OF_##input.buffer; |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 142 | |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 143 | /* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC() |
| 144 | * |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 145 | * Assumptions: |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 146 | * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC() |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 147 | * - input is the name of the original buffer that was copied |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 148 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 149 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 150 | input_copy = NULL; \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 151 | psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input); |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 152 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 153 | /* Declare a local copy of an output buffer and a variable that will be used |
| 154 | * to store a pointer to the start of the buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 155 | * |
| 156 | * Note: This macro must be called before any operations which may jump to |
| 157 | * the exit label, so that the local output copy object is safe to be freed. |
| 158 | * |
| 159 | * Assumptions: |
| 160 | * - output is the name of a pointer to the buffer to be copied |
| 161 | * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 162 | * - output_copy_name is a name that is unused in the current scope |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 163 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 164 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 165 | psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \ |
| 166 | uint8_t *output_copy_name = NULL; |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 167 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 168 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 169 | * point to the start of the copy. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 170 | * |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 171 | * Assumptions: |
| 172 | * - psa_status_t status exists |
| 173 | * - An exit label is declared |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 174 | * - output is the name of a pointer to the buffer to be copied |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 175 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 176 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 177 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 178 | status = psa_crypto_local_output_alloc(output, length, \ |
| 179 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 180 | if (status != PSA_SUCCESS) { \ |
| 181 | goto exit; \ |
| 182 | } \ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 183 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 184 | |
Gabor Mezei | 3b0c371 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 185 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 186 | * point to the start of the copy. |
| 187 | * |
| 188 | * Assumptions: |
| 189 | * - psa_status_t status exists |
| 190 | * - An exit label is declared |
| 191 | * - output is the name of a pointer to the buffer to be copied |
| 192 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
| 193 | */ |
| 194 | #define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \ |
| 195 | status = psa_crypto_local_output_alloc_with_copy(output, length, \ |
| 196 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
| 197 | if (status != PSA_SUCCESS) { \ |
| 198 | goto exit; \ |
| 199 | } \ |
| 200 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
| 201 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 202 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 203 | * after first copying back its contents to the original buffer. |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 204 | * |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 205 | * Assumptions: |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 206 | * - psa_status_t status exists |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 207 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 208 | * - output is the name of the original buffer that was copied |
David Horstmann | 1f53213 | 2023-12-08 14:08:18 +0000 | [diff] [blame] | 209 | */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 210 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 211 | output_copy = NULL; \ |
David Horstmann | 2b70a66 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 212 | do { \ |
| 213 | psa_status_t local_output_status; \ |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 214 | local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \ |
David Horstmann | 2b70a66 | 2023-12-13 14:09:08 +0000 | [diff] [blame] | 215 | if (local_output_status != PSA_SUCCESS) { \ |
| 216 | /* Since this error case is an internal error, it's more serious than \ |
| 217 | * any existing error code and so it's fine to overwrite the existing \ |
| 218 | * status. */ \ |
| 219 | status = local_output_status; \ |
| 220 | } \ |
| 221 | } while (0) |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 222 | #else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 223 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 224 | const uint8_t *input_copy_name = NULL; |
| 225 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 226 | input_copy = input; |
| 227 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 228 | input_copy = NULL; |
| 229 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 230 | uint8_t *output_copy_name = NULL; |
| 231 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 232 | output_copy = output; |
| 233 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 234 | output_copy = NULL; |
David Horstmann | c0a2c30 | 2023-11-29 17:24:08 +0000 | [diff] [blame] | 235 | #endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */ |
David Horstmann | 34980bd | 2023-11-29 17:07:13 +0000 | [diff] [blame] | 236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 237 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 238 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 239 | /* Mbed TLS error codes can combine a high-level error code and a |
| 240 | * low-level error code. The low-level error usually reflects the |
| 241 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | int low_level_ret = -(-ret & 0x007f); |
| 243 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 244 | case 0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 246 | |
| 247 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 248 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 249 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 251 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 254 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 256 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 257 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 258 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 259 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 260 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 261 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 263 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 265 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 267 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 268 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 269 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 270 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 271 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 272 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 273 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_BLOWFISH_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 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 278 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 279 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 280 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 281 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 282 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 287 | |
| 288 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
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_CCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 292 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 294 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 295 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 296 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 297 | |
| 298 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 300 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 302 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 303 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 304 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 307 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 309 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 311 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 313 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | |
| 320 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 322 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 324 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 325 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 326 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 327 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 329 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 330 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 331 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 332 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 334 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 335 | |
| 336 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 338 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 340 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 341 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 342 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 343 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 345 | |
| 346 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 348 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 350 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 352 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 353 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 355 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 356 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 357 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 358 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 359 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 360 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 362 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 364 | #endif |
| 365 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 366 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 367 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 368 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 370 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 371 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 373 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 375 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 377 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | return PSA_ERROR_STORAGE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 379 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 381 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 382 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 384 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 386 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 388 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 390 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 392 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 394 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 396 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 398 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 399 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 400 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 401 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 402 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 404 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 406 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 407 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 408 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 409 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 411 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 412 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 414 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 415 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 416 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 417 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 418 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 420 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 421 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 422 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 424 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 425 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 427 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 429 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 430 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 431 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 432 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 433 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 435 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 437 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 439 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 440 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 441 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 442 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 444 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 445 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 446 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 447 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 448 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 450 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 452 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 455 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 456 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 457 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 458 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 459 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 460 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 462 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 463 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 464 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 465 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 466 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 467 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 468 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 470 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 472 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 473 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 474 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 475 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 476 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 477 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 479 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 481 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 482 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 483 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 484 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 485 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 486 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 490 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 491 | |
| 492 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 493 | /****************************************************************/ |
| 494 | /* Key management */ |
| 495 | /****************************************************************/ |
| 496 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 497 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 498 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 499 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 500 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 501 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 503 | size_t bits, |
| 504 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 505 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 506 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 507 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 509 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 510 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 511 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 512 | #endif |
| 513 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 514 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 515 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 516 | #endif |
| 517 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 518 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 519 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 520 | #endif |
| 521 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 522 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 523 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 524 | #endif |
| 525 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 526 | case 521: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 527 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 528 | case 528: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | if (bits_is_sloppy) { |
| 530 | return MBEDTLS_ECP_DP_SECP521R1; |
| 531 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 532 | break; |
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_BRAINPOOL_P_R1: |
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_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 540 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 541 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 542 | #endif |
| 543 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 544 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 545 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 546 | #endif |
| 547 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 548 | case 512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 549 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 550 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 551 | } |
| 552 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 553 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 554 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 556 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 557 | case 255: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 558 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 559 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 560 | if (bits_is_sloppy) { |
| 561 | return MBEDTLS_ECP_DP_CURVE25519; |
| 562 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 563 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 564 | #endif |
| 565 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 566 | case 448: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | return MBEDTLS_ECP_DP_CURVE448; |
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 | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 571 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 572 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 573 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 574 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 575 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 576 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 577 | #endif |
| 578 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 579 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 580 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 581 | #endif |
| 582 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 583 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 585 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 586 | } |
| 587 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 588 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 589 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 590 | (void) bits_is_sloppy; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 591 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 592 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 593 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 594 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 595 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 596 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 597 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 599 | static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type, |
| 600 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 601 | { |
| 602 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 604 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 605 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 606 | case PSA_KEY_TYPE_DERIVE: |
| 607 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 608 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 609 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 610 | if (bits != 128 && bits != 192 && bits != 256) { |
| 611 | return PSA_ERROR_INVALID_ARGUMENT; |
| 612 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 613 | break; |
| 614 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 615 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 616 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 617 | if (bits != 128 && bits != 192 && bits != 256) { |
| 618 | return PSA_ERROR_INVALID_ARGUMENT; |
| 619 | } |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 620 | break; |
| 621 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 622 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 623 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | if (bits != 128 && bits != 192 && bits != 256) { |
| 625 | return PSA_ERROR_INVALID_ARGUMENT; |
| 626 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 627 | break; |
| 628 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 629 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 630 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | if (bits != 64 && bits != 128 && bits != 192) { |
| 632 | return PSA_ERROR_INVALID_ARGUMENT; |
| 633 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 634 | break; |
| 635 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 636 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 637 | case PSA_KEY_TYPE_ARC4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 638 | if (bits < 8 || bits > 2048) { |
| 639 | return PSA_ERROR_INVALID_ARGUMENT; |
| 640 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 641 | break; |
| 642 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 643 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 644 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 645 | if (bits != 256) { |
| 646 | return PSA_ERROR_INVALID_ARGUMENT; |
| 647 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 648 | break; |
| 649 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 650 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 651 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 652 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 653 | if (bits % 8 != 0) { |
| 654 | return PSA_ERROR_INVALID_ARGUMENT; |
| 655 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 656 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 657 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 660 | /** 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] | 661 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 662 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 663 | * 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] | 664 | * |
| 665 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 666 | * \param[in] key_type The key type of the key to be used with the |
| 667 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 668 | * |
| 669 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 670 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 671 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 672 | * 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] | 673 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 674 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 675 | psa_algorithm_t algorithm, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 676 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 677 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 678 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 679 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 680 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 681 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 682 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 684 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 685 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 686 | * key. */ |
| 687 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 688 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 689 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 690 | * the block length (larger than 1) for block ciphers. */ |
| 691 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 692 | return PSA_SUCCESS; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 700 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 701 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 702 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 703 | if (slot->key.data != NULL) { |
| 704 | return PSA_ERROR_ALREADY_EXISTS; |
| 705 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 706 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 707 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 708 | if (slot->key.data == NULL) { |
| 709 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 710 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 711 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 712 | slot->key.bytes = buffer_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 713 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 716 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 717 | const uint8_t *data, |
| 718 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 719 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 720 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 721 | data_length); |
| 722 | if (status != PSA_SUCCESS) { |
| 723 | return status; |
| 724 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 726 | memcpy(slot->key.data, data, data_length); |
| 727 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 730 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 731 | const psa_key_attributes_t *attributes, |
| 732 | const uint8_t *data, size_t data_length, |
| 733 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 734 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 735 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 736 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 737 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 738 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 739 | /* zero-length keys are never supported. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 740 | if (data_length == 0) { |
| 741 | return PSA_ERROR_NOT_SUPPORTED; |
| 742 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 743 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 744 | if (key_type_is_raw_bytes(type)) { |
| 745 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 746 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 747 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 748 | if (data_length > SIZE_MAX / 8) { |
| 749 | return PSA_ERROR_NOT_SUPPORTED; |
| 750 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 751 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 752 | /* Enforce a size limit, and in particular ensure that the bit |
| 753 | * size fits in its representation type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 754 | if ((*bits) > PSA_MAX_KEY_BITS) { |
| 755 | return PSA_ERROR_NOT_SUPPORTED; |
| 756 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 758 | status = validate_unstructured_key_bit_size(type, *bits); |
| 759 | if (status != PSA_SUCCESS) { |
| 760 | return status; |
| 761 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 762 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 763 | /* Copy the key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 764 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 765 | *key_buffer_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 766 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | return PSA_SUCCESS; |
| 769 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 770 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 771 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 772 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 773 | return mbedtls_psa_ecp_import_key(attributes, |
| 774 | data, data_length, |
| 775 | key_buffer, key_buffer_size, |
| 776 | key_buffer_length, |
| 777 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 778 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 779 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 780 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 781 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 782 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 783 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 784 | return mbedtls_psa_rsa_import_key(attributes, |
| 785 | data, data_length, |
| 786 | key_buffer, key_buffer_size, |
| 787 | key_buffer_length, |
| 788 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 789 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 790 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 791 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 792 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 794 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 795 | } |
| 796 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 797 | /** Calculate the intersection of two algorithm usage policies. |
| 798 | * |
| 799 | * Return 0 (which allows no operation) on incompatibility. |
| 800 | */ |
| 801 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 802 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 803 | psa_algorithm_t alg1, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 804 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 805 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 806 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 807 | if (alg1 == alg2) { |
| 808 | return alg1; |
| 809 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 810 | /* If the policies are from the same hash-and-sign family, check |
| 811 | * 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] | 812 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 813 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 814 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 815 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 816 | return alg2; |
| 817 | } |
| 818 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 819 | return alg1; |
| 820 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 821 | } |
| 822 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 823 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 824 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 825 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 826 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 827 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 828 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 829 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 830 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 831 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 832 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 833 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 834 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 835 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 836 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 837 | } |
| 838 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 839 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 840 | (alg1_len <= alg2_len)) { |
| 841 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 842 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 843 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 844 | (alg2_len <= alg1_len)) { |
| 845 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | /* If the policies are from the same MAC family, check whether one |
| 849 | * of them is a minimum-MAC-length policy. Calculate the most |
| 850 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 851 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 852 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 853 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 854 | /* Validate the combination of key type and algorithm. Since the base |
| 855 | * 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] | 856 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 857 | return 0; |
| 858 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 859 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 860 | /* Get the (exact or at-least) output lengths for both sides of the |
| 861 | * requested intersection. None of the currently supported algorithms |
| 862 | * have an output length dependent on the actual key size, so setting it |
| 863 | * to a bogus value of 0 is currently OK. |
| 864 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 865 | * Note that for at-least-this-length wildcard algorithms, the output |
| 866 | * length is set to the shortest allowed length, which allows us to |
| 867 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 868 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 869 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 870 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 871 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 872 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 873 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 874 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 875 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 876 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 877 | |
| 878 | /* If only one is an at-least-this-length policy, the intersection would |
| 879 | * be the other (fixed-length) policy as long as said fixed length is |
| 880 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 881 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 882 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 883 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 884 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 885 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 886 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 887 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 888 | /* If none of them are wildcards, check whether they define the same tag |
| 889 | * length. This is still possible here when one is default-length and |
| 890 | * the other specific-length. Ensure to always return the |
| 891 | * specific-length version for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 892 | if (alg1_len == alg2_len) { |
| 893 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 894 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 895 | } |
| 896 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 897 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 900 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 901 | psa_algorithm_t policy_alg, |
| 902 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 903 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 904 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 905 | if (requested_alg == policy_alg) { |
| 906 | return 1; |
| 907 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 908 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 909 | * and requested_alg is the same hash-and-sign family with any hash, |
| 910 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 911 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 912 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 913 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 914 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 915 | } |
| 916 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 917 | * the requested algorithm, check the requested tag length to be |
| 918 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 919 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 920 | PSA_ALG_IS_AEAD(requested_alg) && |
| 921 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 922 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 923 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 924 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 925 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 926 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 927 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 928 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 929 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 930 | PSA_ALG_IS_MAC(requested_alg) && |
| 931 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 932 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 933 | /* Validate the combination of key type and algorithm. Since the policy |
| 934 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 935 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 936 | return 0; |
| 937 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 938 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 939 | /* Get both the requested output length for the algorithm which is to be |
| 940 | * verified, and the default output length for the base algorithm. |
| 941 | * Note that none of the currently supported algorithms have an output |
| 942 | * length dependent on actual key size, so setting it to a bogus value |
| 943 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 944 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 945 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 946 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 947 | key_type, 0, |
| 948 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 949 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 950 | /* If the policy is default-length, only allow an algorithm with |
| 951 | * a declared exact-length matching the default. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 952 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 953 | return requested_output_length == default_output_length; |
| 954 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 955 | |
| 956 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 957 | * length exactly matches the default length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 958 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 959 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 960 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 963 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 964 | * check for the requested MAC length to be equal to or longer than the |
| 965 | * minimum allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 966 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 967 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 968 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 969 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 970 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 971 | /* 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] | 972 | * a key derivation with that key agreement should also be allowed. This |
| 973 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 974 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 975 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 976 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 977 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 978 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 979 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 980 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 983 | /** Test whether a policy permits an algorithm. |
| 984 | * |
| 985 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 986 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 987 | * \note This function requires providing the key type for which the policy is |
| 988 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 989 | * have different properties depending on what kind of cipher it is |
| 990 | * combined with. |
| 991 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 992 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 993 | * allowed by the \p policy. |
| 994 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 995 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 996 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 997 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 998 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 999 | psa_key_type_t key_type, |
| 1000 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1001 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1002 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1003 | if (alg == 0) { |
| 1004 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1005 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 1006 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1007 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 1009 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1010 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1012 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 1013 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 1014 | return PSA_SUCCESS; |
| 1015 | } else { |
| 1016 | return PSA_ERROR_NOT_PERMITTED; |
| 1017 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1020 | /** Restrict a key policy based on a constraint. |
| 1021 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 1022 | * \note This function requires providing the key type for which the policy is |
| 1023 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 1024 | * have different properties depending on what kind of cipher it is |
| 1025 | * combined with. |
| 1026 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1027 | * \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] | 1028 | * \param[in,out] policy The policy to restrict. |
| 1029 | * \param[in] constraint The policy constraint to apply. |
| 1030 | * |
| 1031 | * \retval #PSA_SUCCESS |
| 1032 | * \c *policy contains the intersection of the original value of |
| 1033 | * \c *policy and \c *constraint. |
| 1034 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1035 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1036 | * \c *policy is unchanged. |
| 1037 | */ |
| 1038 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 1039 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1040 | psa_key_policy_t *policy, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1041 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1042 | { |
| 1043 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1044 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 1045 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1046 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1047 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 1048 | constraint->alg2); |
| 1049 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1050 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1051 | } |
| 1052 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1053 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1054 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1055 | policy->usage &= constraint->usage; |
| 1056 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1057 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1058 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1061 | /** Get the description of a key given its identifier and policy constraints |
| 1062 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1063 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1064 | * 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] | 1065 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1066 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1067 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1068 | * In case of a persistent key, the function loads the description of the key |
| 1069 | * into a key slot if not already done. |
| 1070 | * |
| 1071 | * On success, the returned key slot is locked. It is the responsibility of |
| 1072 | * 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] | 1073 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1074 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1075 | mbedtls_svc_key_id_t key, |
| 1076 | psa_key_slot_t **p_slot, |
| 1077 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1078 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1079 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1080 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1081 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1084 | if (status != PSA_SUCCESS) { |
| 1085 | return status; |
| 1086 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1087 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1088 | |
| 1089 | /* Enforce that usage policy for the key slot contains all the flags |
| 1090 | * required by the usage parameter. There is one exception: public |
| 1091 | * keys can always be exported, so we treat public key objects as |
| 1092 | * if they had the export flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1093 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1094 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1096 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1097 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1098 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1099 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 1100 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1101 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1102 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1103 | if (alg != 0) { |
| 1104 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1105 | slot->attr.type, |
| 1106 | alg); |
| 1107 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1108 | goto error; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1109 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 1110 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1112 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1113 | |
| 1114 | error: |
| 1115 | *p_slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1116 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1118 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1119 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1120 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1121 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1122 | * |
| 1123 | * 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] | 1124 | * available, as opposed to a key in a secure element and/or to be used |
| 1125 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1126 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1127 | * This is a temporary function that may be used instead of |
| 1128 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1129 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1130 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1131 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1132 | * 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] | 1133 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1134 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1135 | mbedtls_svc_key_id_t key, |
| 1136 | psa_key_slot_t **p_slot, |
| 1137 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1138 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1139 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1140 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1141 | usage, alg); |
| 1142 | if (status != PSA_SUCCESS) { |
| 1143 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1144 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1146 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1147 | psa_unlock_key_slot(*p_slot); |
| 1148 | *p_slot = NULL; |
| 1149 | return PSA_ERROR_NOT_SUPPORTED; |
| 1150 | } |
| 1151 | |
| 1152 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1153 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | 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] | 1156 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1157 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1158 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1159 | if (slot->key.data != NULL) { |
| 1160 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1161 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1163 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1164 | slot->key.data = NULL; |
| 1165 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1167 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1170 | /** Completely wipe a slot in memory, including its policy. |
| 1171 | * Persistent storage is not affected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1172 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1173 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1174 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* |
| 1177 | * 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] | 1178 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1179 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1180 | * 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] | 1181 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1182 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1183 | if (slot->lock_count != 1) { |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1184 | #ifdef MBEDTLS_CHECK_PARAMS |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1185 | MBEDTLS_PARAM_FAILED(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1186 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1187 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1188 | } |
| 1189 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1190 | /* Multipart operations may still be using the key. This is safe |
| 1191 | * because all multipart operation objects are independent from |
| 1192 | * the key slot: if they need to access the key after the setup |
| 1193 | * phase, they have a copy of the key. Note that this means that |
| 1194 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1195 | /* At this point, key material and other type-specific content has |
| 1196 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1197 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1198 | memset(slot, 0, sizeof(*slot)); |
| 1199 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1202 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1203 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1204 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1205 | psa_status_t status; /* status of the last operation */ |
| 1206 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1207 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1208 | psa_se_drv_table_entry_t *driver; |
| 1209 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1210 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1211 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1212 | return PSA_SUCCESS; |
| 1213 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1214 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1215 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1216 | * 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] | 1217 | * key, this will load the key description from persistent memory if not |
| 1218 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1219 | * the key is operated by an SE or not and this information is needed by |
| 1220 | * the current implementation. |
| 1221 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1222 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1223 | if (status != PSA_SUCCESS) { |
| 1224 | return status; |
| 1225 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1226 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1227 | /* |
| 1228 | * If the key slot containing the key description is under access by the |
| 1229 | * library (apart from the present access), the key cannot be destroyed |
| 1230 | * yet. For the time being, just return in error. Eventually (to be |
| 1231 | * implemented), the key should be destroyed when all accesses have |
| 1232 | * stopped. |
| 1233 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1234 | if (slot->lock_count > 1) { |
| 1235 | psa_unlock_key_slot(slot); |
| 1236 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1239 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1240 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1241 | * if we attempt it, depending on whether the key is merely read-only |
| 1242 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1243 | * Just do the best we can, which is to wipe the copy in memory |
| 1244 | * (done in this function's cleanup code). */ |
| 1245 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1246 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1249 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1250 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1251 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1252 | /* For a key in a secure element, we need to do three things: |
| 1253 | * remove the key file in internal storage, destroy the |
| 1254 | * key inside the secure element, and update the driver's |
| 1255 | * persistent data. Start a transaction that will encompass these |
| 1256 | * three actions. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1257 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1258 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1259 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1260 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1261 | status = psa_crypto_save_transaction(); |
| 1262 | if (status != PSA_SUCCESS) { |
| 1263 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1264 | /* We should still try to destroy the key in the secure |
| 1265 | * element and the key metadata in storage. This is especially |
| 1266 | * important if the error is that the storage is full. |
| 1267 | * But how to do it exactly without risking an inconsistent |
| 1268 | * state after a reset? |
| 1269 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1270 | */ |
| 1271 | overall_status = status; |
| 1272 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1275 | status = psa_destroy_se_key(driver, |
| 1276 | psa_key_slot_get_slot_number(slot)); |
| 1277 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1278 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1279 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1280 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1281 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1282 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1283 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1284 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1285 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1286 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1287 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1288 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1289 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1290 | /* TODO: other slots may have a copy of the same key. We should |
| 1291 | * invalidate them. |
| 1292 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1293 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1294 | } |
| 1295 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1296 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1297 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1298 | if (driver != NULL) { |
| 1299 | status = psa_save_se_persistent_data(driver); |
| 1300 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1301 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1302 | } |
| 1303 | status = psa_crypto_stop_transaction(); |
| 1304 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1305 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1306 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1307 | } |
| 1308 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1309 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1310 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1311 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1312 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1313 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1314 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1315 | } |
| 1316 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1319 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1320 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1321 | static psa_status_t psa_get_rsa_public_exponent( |
| 1322 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1323 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1324 | { |
| 1325 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1326 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1327 | uint8_t *buffer = NULL; |
| 1328 | size_t buflen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1329 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1331 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1332 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1333 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1334 | } |
| 1335 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1336 | /* It's the default value, which is reported as an empty string, |
| 1337 | * so there's nothing to do. */ |
| 1338 | goto exit; |
| 1339 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1341 | buflen = mbedtls_mpi_size(&mpi); |
| 1342 | buffer = mbedtls_calloc(1, buflen); |
| 1343 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1344 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1345 | goto exit; |
| 1346 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1348 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1349 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1350 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1351 | attributes->domain_parameters = buffer; |
| 1352 | attributes->domain_parameters_size = buflen; |
| 1353 | |
| 1354 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1355 | mbedtls_mpi_free(&mpi); |
| 1356 | if (ret != 0) { |
| 1357 | mbedtls_free(buffer); |
| 1358 | } |
| 1359 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1360 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1361 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1362 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1363 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1364 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1365 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1366 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1367 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1368 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1369 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1370 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1371 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1373 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1375 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1376 | if (status != PSA_SUCCESS) { |
| 1377 | return status; |
| 1378 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1379 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1380 | attributes->core = slot->attr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1381 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1382 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1383 | |
| 1384 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1385 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1386 | psa_set_key_slot_number(attributes, |
| 1387 | psa_key_slot_get_slot_number(slot)); |
| 1388 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1389 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1391 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1392 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1393 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1394 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1395 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1396 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1397 | * is not yet implemented. |
| 1398 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1399 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1400 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1401 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1402 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1403 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1404 | slot->attr.type, |
| 1405 | slot->key.data, |
| 1406 | slot->key.bytes, |
| 1407 | &rsa); |
| 1408 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1409 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1410 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1412 | status = psa_get_rsa_public_exponent(rsa, |
| 1413 | attributes); |
| 1414 | mbedtls_rsa_free(rsa); |
| 1415 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1416 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1417 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1418 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1419 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1420 | default: |
| 1421 | /* Nothing else to do. */ |
| 1422 | break; |
| 1423 | } |
| 1424 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1425 | if (status != PSA_SUCCESS) { |
| 1426 | psa_reset_key_attributes(attributes); |
| 1427 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1429 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1432 | } |
| 1433 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1434 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1435 | psa_status_t psa_get_key_slot_number( |
| 1436 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1437 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1438 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1439 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1440 | *slot_number = attributes->slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1441 | return PSA_SUCCESS; |
| 1442 | } else { |
| 1443 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1444 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1445 | } |
| 1446 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1447 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1448 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1449 | size_t key_buffer_size, |
| 1450 | uint8_t *data, |
| 1451 | size_t data_size, |
| 1452 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1453 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1454 | if (key_buffer_size > data_size) { |
| 1455 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1456 | } |
| 1457 | memcpy(data, key_buffer, key_buffer_size); |
| 1458 | memset(data + key_buffer_size, 0, |
| 1459 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1460 | *data_length = key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1461 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1464 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1465 | const psa_key_attributes_t *attributes, |
| 1466 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1467 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1468 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1469 | psa_key_type_t type = attributes->core.type; |
| 1470 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1471 | if (key_type_is_raw_bytes(type) || |
| 1472 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1473 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1474 | return psa_export_key_buffer_internal( |
| 1475 | key_buffer, key_buffer_size, |
| 1476 | data, data_size, data_length); |
| 1477 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1478 | /* This shouldn't happen in the reference implementation, but |
| 1479 | it is valid for a special-purpose implementation to omit |
| 1480 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1481 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1485 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1486 | uint8_t *data, |
| 1487 | size_t data_size, |
| 1488 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1489 | { |
| 1490 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1491 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1492 | psa_key_slot_t *slot; |
| 1493 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1494 | /* Reject a zero-length output buffer now, since this can never be a |
| 1495 | * valid key representation. This way we know that data must be a valid |
| 1496 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1497 | if (data_size == 0) { |
| 1498 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1499 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1500 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1501 | /* Set the key to empty now, so that even when there are errors, we always |
| 1502 | * set data_length to a value between 0 and data_size. On error, setting |
| 1503 | * the key to empty is a good choice because an empty key representation is |
| 1504 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1505 | *data_length = 0; |
| 1506 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1507 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1508 | * which don't require any flag, but |
| 1509 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1510 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1511 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1512 | PSA_KEY_USAGE_EXPORT, 0); |
| 1513 | if (status != PSA_SUCCESS) { |
| 1514 | return status; |
| 1515 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1516 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1517 | psa_key_attributes_t attributes = { |
| 1518 | .core = slot->attr |
| 1519 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1520 | status = psa_driver_wrapper_export_key(&attributes, |
| 1521 | slot->key.data, slot->key.bytes, |
| 1522 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1524 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1526 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1529 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1530 | const psa_key_attributes_t *attributes, |
| 1531 | const uint8_t *key_buffer, |
| 1532 | size_t key_buffer_size, |
| 1533 | uint8_t *data, |
| 1534 | size_t data_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1535 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1536 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1537 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1538 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1539 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1540 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1541 | /* Exporting public -> public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1542 | return psa_export_key_buffer_internal( |
| 1543 | key_buffer, key_buffer_size, |
| 1544 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1545 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1547 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1548 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1549 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1550 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1551 | key_buffer, |
| 1552 | key_buffer_size, |
| 1553 | data, |
| 1554 | data_size, |
| 1555 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1556 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1557 | /* 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] | 1558 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1559 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1560 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1561 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1562 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1563 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1564 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1565 | key_buffer, |
| 1566 | key_buffer_size, |
| 1567 | data, |
| 1568 | data_size, |
| 1569 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1570 | #else |
| 1571 | /* 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] | 1572 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1573 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1574 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1575 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1576 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1577 | /* This shouldn't happen in the reference implementation, but |
| 1578 | it is valid for a special-purpose implementation to omit |
| 1579 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1580 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1581 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1582 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1584 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1585 | uint8_t *data, |
| 1586 | size_t data_size, |
| 1587 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1588 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1589 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1590 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1591 | psa_key_attributes_t attributes; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1592 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1593 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1594 | /* Reject a zero-length output buffer now, since this can never be a |
| 1595 | * valid key representation. This way we know that data must be a valid |
| 1596 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1597 | if (data_size == 0) { |
| 1598 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1599 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1600 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1601 | /* Set the key to empty now, so that even when there are errors, we always |
| 1602 | * set data_length to a value between 0 and data_size. On error, setting |
| 1603 | * the key to empty is a good choice because an empty key representation is |
| 1604 | * unlikely to be accepted anywhere. */ |
| 1605 | *data_length = 0; |
| 1606 | |
| 1607 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1608 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1609 | if (status != PSA_SUCCESS) { |
| 1610 | return status; |
| 1611 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1613 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1614 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1615 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 1618 | attributes = (psa_key_attributes_t) { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1619 | .core = slot->attr |
| 1620 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1621 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1622 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1623 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1624 | |
| 1625 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1626 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1629 | } |
| 1630 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1631 | MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1632 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1633 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1634 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1635 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1636 | "One or more key attribute flag is listed as both internal-only and external-only") |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1638 | /** Validate that a key policy is internally well-formed. |
| 1639 | * |
| 1640 | * This function only rejects invalid policies. It does not validate the |
| 1641 | * consistency of the policy with respect to other attributes of the key |
| 1642 | * such as the key type. |
| 1643 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1644 | static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1646 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1647 | PSA_KEY_USAGE_COPY | |
| 1648 | PSA_KEY_USAGE_ENCRYPT | |
| 1649 | PSA_KEY_USAGE_DECRYPT | |
| 1650 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1651 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1652 | PSA_KEY_USAGE_SIGN_HASH | |
| 1653 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1654 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1655 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1656 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1658 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1661 | /** Validate the internal consistency of key attributes. |
| 1662 | * |
| 1663 | * This function only rejects invalid attribute values. If does not |
| 1664 | * validate the consistency of the attributes with any key data that may |
| 1665 | * be involved in the creation of the key. |
| 1666 | * |
| 1667 | * Call this function early in the key creation process. |
| 1668 | * |
| 1669 | * \param[in] attributes Key attributes for the new key. |
| 1670 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1671 | * NULL for a transparent key. |
| 1672 | * |
| 1673 | */ |
| 1674 | static psa_status_t psa_validate_key_attributes( |
| 1675 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1676 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1677 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1678 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1679 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1680 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1682 | status = psa_validate_key_location(lifetime, p_drv); |
| 1683 | if (status != PSA_SUCCESS) { |
| 1684 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1687 | status = psa_validate_key_persistence(lifetime); |
| 1688 | if (status != PSA_SUCCESS) { |
| 1689 | return status; |
| 1690 | } |
| 1691 | |
| 1692 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1693 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1694 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1695 | } |
| 1696 | } else { |
| 1697 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1698 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1703 | if (status != PSA_SUCCESS) { |
| 1704 | return status; |
| 1705 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1706 | |
| 1707 | /* Refuse to create overly large keys. |
| 1708 | * Note that this doesn't trigger on import if the attributes don't |
| 1709 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1710 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1711 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1712 | return PSA_ERROR_NOT_SUPPORTED; |
| 1713 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1714 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1715 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1716 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1717 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1718 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1719 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1721 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1724 | /** Prepare a key slot to receive key material. |
| 1725 | * |
| 1726 | * This function allocates a key slot and sets its metadata. |
| 1727 | * |
| 1728 | * If this function fails, call psa_fail_key_creation(). |
| 1729 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1730 | * This function is intended to be used as follows: |
| 1731 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1732 | * it with the specified attributes, and in case of a volatile key assign it |
| 1733 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1734 | * -# Populate the slot with the key material. |
| 1735 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1736 | * In case of failure at any step, stop the sequence and call |
| 1737 | * psa_fail_key_creation(). |
| 1738 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1739 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1740 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1741 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1742 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1743 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1744 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1745 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1746 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1747 | * |
| 1748 | * \retval #PSA_SUCCESS |
| 1749 | * The key slot is ready to receive key material. |
| 1750 | * \return If this function fails, the key slot is an invalid state. |
| 1751 | * 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] | 1752 | */ |
| 1753 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1754 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1755 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1756 | psa_key_slot_t **p_slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1757 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1758 | { |
| 1759 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1760 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1761 | psa_key_slot_t *slot; |
| 1762 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1763 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1764 | *p_drv = NULL; |
| 1765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1766 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1767 | if (status != PSA_SUCCESS) { |
| 1768 | return status; |
| 1769 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1771 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1772 | if (status != PSA_SUCCESS) { |
| 1773 | return status; |
| 1774 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1775 | slot = *p_slot; |
| 1776 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1777 | /* We're storing the declared bit-size of the key. It's up to each |
| 1778 | * creation mechanism to verify that this information is correct. |
| 1779 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1780 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1781 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1782 | * volatile key identifier associated to the slot returned to contain its |
| 1783 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1784 | |
| 1785 | slot->attr = attributes->core; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1786 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1787 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1788 | slot->attr.id = volatile_key_id; |
| 1789 | #else |
| 1790 | slot->attr.id.key_id = volatile_key_id; |
| 1791 | #endif |
| 1792 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1793 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1794 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1795 | * external-only flags, query `attributes`. Thanks to the check |
| 1796 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1797 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1798 | * may have set. */ |
| 1799 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1800 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1801 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1802 | /* 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] | 1803 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1804 | * create the key file in internal storage, create the |
| 1805 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1806 | * persistent data. This is done by starting a transaction that will |
| 1807 | * encompass these three actions. |
| 1808 | * For registering a volatile key, we just need to find an appropriate |
| 1809 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1810 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1811 | /* The first thing to do is to find a slot number for the new key. |
| 1812 | * We save the slot number in persistent storage as part of the |
| 1813 | * transaction data. It will be needed to recover if the power |
| 1814 | * fails during the key creation process, to clean up on the secure |
| 1815 | * element side after restarting. Obtaining a slot number from the |
| 1816 | * secure element driver updates its persistent state, but we do not yet |
| 1817 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1818 | * 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] | 1819 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1820 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1821 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1822 | &slot_number); |
| 1823 | if (status != PSA_SUCCESS) { |
| 1824 | return status; |
| 1825 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1826 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1827 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1828 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1829 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1830 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1831 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1832 | status = psa_crypto_save_transaction(); |
| 1833 | if (status != PSA_SUCCESS) { |
| 1834 | (void) psa_crypto_stop_transaction(); |
| 1835 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1836 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1837 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1838 | |
| 1839 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1840 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1841 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1843 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1844 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1845 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1846 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1847 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1848 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1849 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1850 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1851 | |
| 1852 | /** Finalize the creation of a key once its key material has been set. |
| 1853 | * |
| 1854 | * This entails writing the key to persistent storage. |
| 1855 | * |
| 1856 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1857 | * See the documentation of psa_start_key_creation() for the intended use |
| 1858 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1859 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1860 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1861 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1862 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1863 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1864 | * \param[in,out] slot Pointer to the slot with key material. |
| 1865 | * \param[in] driver The secure element driver for the key, |
| 1866 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1867 | * \param[out] key On success, identifier of the key. Note that the |
| 1868 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1869 | * |
| 1870 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1871 | * The key was successfully created. |
Gilles Peskine | ec1eff3 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1872 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1873 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1874 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1875 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1876 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1877 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1878 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1879 | * \return If this function fails, the key slot is an invalid state. |
| 1880 | * 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] | 1881 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1882 | static psa_status_t psa_finish_key_creation( |
| 1883 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1884 | psa_se_drv_table_entry_t *driver, |
| 1885 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1886 | { |
| 1887 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1888 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1889 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1890 | |
| 1891 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1892 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1893 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1894 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1895 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1896 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1897 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1898 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1899 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1900 | sizeof(data.slot_number), |
| 1901 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1902 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1903 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1904 | status = psa_save_persistent_key(&slot->attr, |
| 1905 | (uint8_t *) &data, |
| 1906 | sizeof(data)); |
| 1907 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1908 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1909 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1910 | /* Key material is saved in export representation in the slot, so |
| 1911 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1912 | status = psa_save_persistent_key(&slot->attr, |
| 1913 | slot->key.data, |
| 1914 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1915 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1916 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1917 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1918 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1919 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1920 | /* Finish the transaction for a key creation. This does not |
| 1921 | * happen when registering an existing key. Detect this case |
| 1922 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1923 | * creation of a persistent key in a secure element requires a transaction, |
| 1924 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1925 | if (driver != NULL && |
| 1926 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1927 | status = psa_save_se_persistent_data(driver); |
| 1928 | if (status != PSA_SUCCESS) { |
| 1929 | psa_destroy_persistent_key(slot->attr.id); |
| 1930 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1931 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1932 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1933 | } |
| 1934 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1935 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1936 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1937 | *key = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1938 | status = psa_unlock_key_slot(slot); |
| 1939 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1940 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1941 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1942 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1943 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1944 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /** Abort the creation of a key. |
| 1948 | * |
| 1949 | * You may call this function after calling psa_start_key_creation(), |
| 1950 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1951 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1952 | * See the documentation of psa_start_key_creation() for the intended use |
| 1953 | * of this function. |
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 | * \param[in,out] slot Pointer to the slot with key material. |
| 1956 | * \param[in] driver The secure element driver for the key, |
| 1957 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1958 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1959 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1960 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1961 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1962 | (void) driver; |
| 1963 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1964 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1965 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1966 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1967 | |
| 1968 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1969 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1970 | * element, and the failure happened later (when saving metadata |
| 1971 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1972 | * element. |
| 1973 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1974 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1975 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1976 | /* Abort the ongoing transaction if any (there may not be one if |
| 1977 | * the creation process failed before starting one, or if the |
| 1978 | * key creation is a registration of a key in a secure element). |
| 1979 | * Earlier functions must already have done what it takes to undo any |
| 1980 | * partial creation. All that's left is to update the transaction data |
| 1981 | * itself. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1982 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1983 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1984 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1985 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1988 | /** Validate optional attributes during key creation. |
| 1989 | * |
| 1990 | * Some key attributes are optional during key creation. If they are |
| 1991 | * specified in the attributes structure, check that they are consistent |
| 1992 | * with the data in the slot. |
| 1993 | * |
| 1994 | * This function should be called near the end of key creation, after |
| 1995 | * the slot in memory is fully populated but before saving persistent data. |
| 1996 | */ |
| 1997 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1998 | const psa_key_slot_t *slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1999 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2000 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2001 | if (attributes->core.type != 0) { |
| 2002 | if (attributes->core.type != slot->attr.type) { |
| 2003 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2004 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2007 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2008 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2009 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 2010 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2011 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2012 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2013 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2014 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 2015 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2016 | slot->attr.type, |
| 2017 | slot->key.data, |
| 2018 | slot->key.bytes, |
| 2019 | &rsa); |
| 2020 | if (status != PSA_SUCCESS) { |
| 2021 | return status; |
| 2022 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2023 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2024 | mbedtls_mpi_init(&actual); |
| 2025 | mbedtls_mpi_init(&required); |
| 2026 | ret = mbedtls_rsa_export(rsa, |
| 2027 | NULL, NULL, NULL, NULL, &actual); |
| 2028 | mbedtls_rsa_free(rsa); |
| 2029 | mbedtls_free(rsa); |
| 2030 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2031 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2032 | } |
| 2033 | ret = mbedtls_mpi_read_binary(&required, |
| 2034 | attributes->domain_parameters, |
| 2035 | attributes->domain_parameters_size); |
| 2036 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2037 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2038 | } |
| 2039 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2040 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | } |
| 2042 | rsa_exit: |
| 2043 | mbedtls_mpi_free(&actual); |
| 2044 | mbedtls_mpi_free(&required); |
| 2045 | if (ret != 0) { |
| 2046 | return mbedtls_to_psa_error(ret); |
| 2047 | } |
| 2048 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2049 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2050 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2051 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2052 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2056 | if (attributes->core.bits != 0) { |
| 2057 | if (attributes->core.bits != slot->attr.bits) { |
| 2058 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2059 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2062 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2063 | } |
| 2064 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2065 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame^] | 2066 | const uint8_t *data_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2067 | size_t data_length, |
| 2068 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2069 | { |
| 2070 | psa_status_t status; |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame^] | 2071 | LOCAL_INPUT_DECLARE(data_external, data); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2072 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2073 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2074 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2075 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2076 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2077 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2078 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2079 | * This also rejects any key which might be encoded as an empty string, |
| 2080 | * which is never valid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2081 | if (data_length == 0) { |
| 2082 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2083 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2084 | |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame^] | 2085 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2086 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2087 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2088 | &slot, &driver); |
| 2089 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2090 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2091 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2092 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2093 | /* In the case of a transparent key or an opaque key stored in local |
| 2094 | * storage (thus not in the case of generating a key in a secure element |
| 2095 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 2096 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2097 | if (slot->key.data == NULL) { |
| 2098 | status = psa_allocate_buffer_to_slot(slot, data_length); |
| 2099 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2100 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2101 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2102 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2103 | |
| 2104 | bits = slot->attr.bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2105 | status = psa_driver_wrapper_import_key(attributes, |
| 2106 | data, data_length, |
| 2107 | slot->key.data, |
| 2108 | slot->key.bytes, |
| 2109 | &slot->key.bytes, &bits); |
| 2110 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2111 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2112 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2114 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 2115 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2116 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 2117 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2118 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2119 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2121 | status = psa_validate_optional_attributes(slot, attributes); |
| 2122 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2123 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2124 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2127 | exit: |
Ryan Everett | 6b97025 | 2024-01-19 16:02:59 +0000 | [diff] [blame^] | 2128 | LOCAL_INPUT_FREE(data_external, data); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2129 | if (status != PSA_SUCCESS) { |
| 2130 | psa_fail_key_creation(slot, driver); |
| 2131 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2133 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2134 | } |
| 2135 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2136 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2137 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2138 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2139 | { |
| 2140 | psa_status_t status; |
| 2141 | psa_key_slot_t *slot = NULL; |
| 2142 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2143 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2144 | |
| 2145 | /* Leaving attributes unspecified is not currently supported. |
| 2146 | * It could make sense to query the key type and size from the |
| 2147 | * secure element, but not all secure elements support this |
| 2148 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2149 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2150 | return PSA_ERROR_NOT_SUPPORTED; |
| 2151 | } |
| 2152 | if (psa_get_key_bits(attributes) == 0) { |
| 2153 | return PSA_ERROR_NOT_SUPPORTED; |
| 2154 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2156 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2157 | &slot, &driver); |
| 2158 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2159 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2160 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2161 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2162 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2163 | |
| 2164 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2165 | if (status != PSA_SUCCESS) { |
| 2166 | psa_fail_key_creation(slot, driver); |
| 2167 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2168 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2169 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2170 | psa_close_key(key); |
| 2171 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2172 | } |
| 2173 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2174 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2175 | static psa_status_t psa_copy_key_material(const psa_key_slot_t *source, |
| 2176 | psa_key_slot_t *target) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2177 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2178 | psa_status_t status = psa_copy_key_material_into_slot(target, |
| 2179 | source->key.data, |
| 2180 | source->key.bytes); |
| 2181 | if (status != PSA_SUCCESS) { |
| 2182 | return status; |
| 2183 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2184 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2185 | target->attr.type = source->attr.type; |
| 2186 | target->attr.bits = source->attr.bits; |
| 2187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2188 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2191 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2192 | const psa_key_attributes_t *specified_attributes, |
| 2193 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2194 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2195 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2196 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2197 | psa_key_slot_t *source_slot = NULL; |
| 2198 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2199 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2200 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2201 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2202 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2203 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2204 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2205 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2206 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2207 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2208 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2210 | status = psa_validate_optional_attributes(source_slot, |
| 2211 | specified_attributes); |
| 2212 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2213 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2214 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2216 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2217 | &actual_attributes.core.policy, |
| 2218 | &source_slot->attr.policy); |
| 2219 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2220 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2221 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2223 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2224 | &target_slot, &driver); |
| 2225 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2226 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2227 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2228 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2229 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2230 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2231 | /* Copying to a secure element is not implemented yet. */ |
| 2232 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2233 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2234 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2235 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2237 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2238 | /* |
| 2239 | * Copying through an opaque driver is not implemented yet, consider |
| 2240 | * a lifetime with an external location as an invalid parameter for |
| 2241 | * now. |
| 2242 | */ |
| 2243 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2244 | goto exit; |
| 2245 | } |
| 2246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2247 | status = psa_copy_key_material(source_slot, target_slot); |
| 2248 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2249 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2250 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2252 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2253 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2254 | if (status != PSA_SUCCESS) { |
| 2255 | psa_fail_key_creation(target_slot, driver); |
| 2256 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2258 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2260 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2263 | |
| 2264 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2265 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2266 | /* Message digests */ |
| 2267 | /****************************************************************/ |
| 2268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2269 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2271 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2272 | if (operation->id == 0) { |
| 2273 | return PSA_SUCCESS; |
| 2274 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2275 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2276 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2277 | operation->id = 0; |
| 2278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2279 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2282 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2283 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2284 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2285 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2286 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2287 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2288 | if (operation->id != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2289 | status = PSA_ERROR_BAD_STATE; |
| 2290 | goto exit; |
| 2291 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2293 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2294 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2295 | goto exit; |
| 2296 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2297 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2298 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2299 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2300 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2302 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2303 | |
| 2304 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | if (status != PSA_SUCCESS) { |
| 2306 | psa_hash_abort(operation); |
| 2307 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2308 | |
| 2309 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2312 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2313 | const uint8_t *input, |
| 2314 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2315 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2316 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2318 | if (operation->id == 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2319 | status = PSA_ERROR_BAD_STATE; |
| 2320 | goto exit; |
| 2321 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2322 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2323 | /* Don't require hash implementations to behave correctly on a |
| 2324 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2325 | if (input_length == 0) { |
| 2326 | return PSA_SUCCESS; |
| 2327 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2329 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2330 | |
| 2331 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2332 | if (status != PSA_SUCCESS) { |
| 2333 | psa_hash_abort(operation); |
| 2334 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2335 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2336 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2339 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2340 | uint8_t *hash, |
| 2341 | size_t hash_size, |
| 2342 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2343 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2344 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2345 | if (operation->id == 0) { |
| 2346 | return PSA_ERROR_BAD_STATE; |
| 2347 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2349 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2350 | operation, hash, hash_size, hash_length); |
| 2351 | psa_hash_abort(operation); |
| 2352 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2353 | } |
| 2354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2355 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2356 | const uint8_t *hash, |
| 2357 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2358 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2359 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2360 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2361 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2362 | operation, |
| 2363 | actual_hash, sizeof(actual_hash), |
| 2364 | &actual_hash_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2366 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2367 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2368 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2370 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2371 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2372 | goto exit; |
| 2373 | } |
| 2374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2375 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2376 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2377 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2378 | |
| 2379 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2380 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2381 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2382 | psa_hash_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2383 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2385 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2388 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2389 | const uint8_t *input, size_t input_length, |
| 2390 | uint8_t *hash, size_t hash_size, |
| 2391 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2392 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2393 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2394 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2395 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2396 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2398 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2399 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2400 | } |
| 2401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2402 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2403 | const uint8_t *input, size_t input_length, |
| 2404 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2405 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2406 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2407 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2410 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2411 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2412 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2413 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2414 | alg, input, input_length, |
| 2415 | actual_hash, sizeof(actual_hash), |
| 2416 | &actual_hash_length); |
| 2417 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2418 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2419 | } |
| 2420 | if (actual_hash_length != hash_length) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2421 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2422 | goto exit; |
| 2423 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2424 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2425 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2426 | } |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2427 | |
| 2428 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2429 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2430 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2431 | } |
| 2432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2433 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2434 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2435 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2436 | if (source_operation->id == 0 || |
| 2437 | target_operation->id != 0) { |
| 2438 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2439 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2441 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2442 | target_operation); |
| 2443 | if (status != PSA_SUCCESS) { |
| 2444 | psa_hash_abort(target_operation); |
| 2445 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2446 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2447 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2448 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2449 | |
| 2450 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2451 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2452 | /* MAC */ |
| 2453 | /****************************************************************/ |
| 2454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2455 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2456 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2457 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2458 | if (operation->id == 0) { |
| 2459 | return PSA_SUCCESS; |
| 2460 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2462 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2463 | operation->mac_size = 0; |
| 2464 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2465 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2466 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2467 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2468 | } |
| 2469 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2470 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2471 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2472 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2473 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2474 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2475 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2476 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2477 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2479 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2480 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2481 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2482 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2483 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2484 | status = psa_mac_key_can_do(alg, key_type); |
| 2485 | if (status != PSA_SUCCESS) { |
| 2486 | return status; |
| 2487 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2488 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2489 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2490 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2491 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2492 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2493 | /* A very short MAC is too short for security since it can be |
| 2494 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2495 | * so we make this our minimum, even though 32 bits is still |
| 2496 | * too small for security. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2497 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2498 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2499 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2500 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2501 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2502 | /* It's impossible to "truncate" to a larger length than the full length |
| 2503 | * of the algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2504 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2507 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2508 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2509 | * that is disabled in the compile-time configuration. The result can |
| 2510 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2511 | * configuration into account. In this case, force a return of |
| 2512 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2513 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2514 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2515 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2516 | * systematically generated tests. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2517 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2520 | return PSA_SUCCESS; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2521 | } |
| 2522 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2523 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2524 | mbedtls_svc_key_id_t key, |
| 2525 | psa_algorithm_t alg, |
| 2526 | int is_sign) |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2527 | { |
| 2528 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2529 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2530 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2531 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2532 | |
| 2533 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2534 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2535 | status = PSA_ERROR_BAD_STATE; |
| 2536 | goto exit; |
| 2537 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2538 | |
| 2539 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2540 | key, |
| 2541 | &slot, |
| 2542 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2543 | alg); |
| 2544 | if (status != PSA_SUCCESS) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2545 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2546 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2547 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2548 | attributes = (psa_key_attributes_t) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2549 | .core = slot->attr |
| 2550 | }; |
| 2551 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2552 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2553 | &operation->mac_size); |
| 2554 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2555 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2556 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2557 | |
| 2558 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2559 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2560 | if (is_sign) { |
| 2561 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2562 | &attributes, |
| 2563 | slot->key.data, |
| 2564 | slot->key.bytes, |
| 2565 | alg); |
| 2566 | } else { |
| 2567 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2568 | &attributes, |
| 2569 | slot->key.data, |
| 2570 | slot->key.bytes, |
| 2571 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2572 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2573 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2574 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2575 | if (status != PSA_SUCCESS) { |
| 2576 | psa_mac_abort(operation); |
| 2577 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2578 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2579 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2580 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2581 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2584 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2585 | mbedtls_svc_key_id_t key, |
| 2586 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2587 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2588 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2591 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2592 | mbedtls_svc_key_id_t key, |
| 2593 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2594 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2595 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2596 | } |
| 2597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2598 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2599 | const uint8_t *input, |
| 2600 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2601 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2602 | if (operation->id == 0) { |
| 2603 | return PSA_ERROR_BAD_STATE; |
| 2604 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2606 | /* Don't require hash implementations to behave correctly on a |
| 2607 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2608 | if (input_length == 0) { |
| 2609 | return PSA_SUCCESS; |
| 2610 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2611 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2612 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2613 | input, input_length); |
| 2614 | if (status != PSA_SUCCESS) { |
| 2615 | psa_mac_abort(operation); |
| 2616 | } |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2617 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2618 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2621 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2622 | uint8_t *mac, |
| 2623 | size_t mac_size, |
| 2624 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2625 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2626 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2627 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2628 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2629 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2630 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2631 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2632 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2633 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2634 | if (!operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2635 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2636 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2637 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2638 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2639 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2640 | * once all the error checks are done. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2641 | if (operation->mac_size == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2642 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2643 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2644 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | if (mac_size < operation->mac_size) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2647 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2648 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2649 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2651 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2652 | mac, operation->mac_size, |
| 2653 | mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2654 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2655 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2656 | /* In case of success, set the potential excess room in the output buffer |
| 2657 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2658 | * In case of error, set the output length and content to a safe default, |
| 2659 | * such that in case the caller misses an error check, the output would be |
| 2660 | * an unachievable MAC. |
| 2661 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2662 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2663 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2664 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2665 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2667 | if (mac_size > operation->mac_size) { |
| 2668 | memset(&mac[operation->mac_size], '!', |
| 2669 | mac_size - operation->mac_size); |
| 2670 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2672 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2673 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2674 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2677 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2678 | const uint8_t *mac, |
| 2679 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2680 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2681 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2682 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2684 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2685 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2686 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2687 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2688 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2689 | if (operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2690 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2691 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2692 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2693 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2694 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2695 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2696 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2697 | } |
| 2698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2699 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2700 | mac, mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2701 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2702 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2703 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2705 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2706 | } |
| 2707 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2708 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2709 | psa_algorithm_t alg, |
| 2710 | const uint8_t *input, |
| 2711 | size_t input_length, |
| 2712 | uint8_t *mac, |
| 2713 | size_t mac_size, |
| 2714 | size_t *mac_length, |
| 2715 | int is_sign) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2716 | { |
| 2717 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2718 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2719 | psa_key_attributes_t attributes; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2720 | psa_key_slot_t *slot; |
| 2721 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2723 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2724 | key, |
| 2725 | &slot, |
| 2726 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2727 | alg); |
| 2728 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2729 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2730 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2731 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2732 | attributes = (psa_key_attributes_t) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2733 | .core = slot->attr |
| 2734 | }; |
| 2735 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2736 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2737 | &operation_mac_size); |
| 2738 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2739 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2741 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2742 | if (mac_size < operation_mac_size) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2743 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2744 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2748 | &attributes, |
| 2749 | slot->key.data, slot->key.bytes, |
| 2750 | alg, |
| 2751 | input, input_length, |
| 2752 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2753 | |
| 2754 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2755 | /* In case of success, set the potential excess room in the output buffer |
| 2756 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2757 | * In case of error, set the output length and content to a safe default, |
| 2758 | * such that in case the caller misses an error check, the output would be |
| 2759 | * an unachievable MAC. |
| 2760 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2761 | if (status != PSA_SUCCESS) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2762 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2763 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2764 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2765 | if (mac_size > operation_mac_size) { |
| 2766 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
| 2767 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2769 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2771 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2772 | } |
| 2773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2774 | 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] | 2775 | psa_algorithm_t alg, |
| 2776 | const uint8_t *input, |
| 2777 | size_t input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2778 | uint8_t *mac, |
| 2779 | size_t mac_size, |
| 2780 | size_t *mac_length) |
| 2781 | { |
| 2782 | return psa_mac_compute_internal(key, alg, |
| 2783 | input, input_length, |
| 2784 | mac, mac_size, mac_length, 1); |
| 2785 | } |
| 2786 | |
| 2787 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2788 | psa_algorithm_t alg, |
| 2789 | const uint8_t *input, |
| 2790 | size_t input_length, |
| 2791 | const uint8_t *mac, |
| 2792 | size_t mac_length) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2793 | { |
| 2794 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2795 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2796 | size_t actual_mac_length; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2798 | status = psa_mac_compute_internal(key, alg, |
| 2799 | input, input_length, |
| 2800 | actual_mac, sizeof(actual_mac), |
| 2801 | &actual_mac_length, 0); |
| 2802 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2803 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2804 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2806 | if (mac_length != actual_mac_length) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2807 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2808 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2809 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2810 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2811 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2812 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2813 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2814 | |
| 2815 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2816 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2817 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2818 | return status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2819 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2820 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2821 | /****************************************************************/ |
| 2822 | /* Asymmetric cryptography */ |
| 2823 | /****************************************************************/ |
| 2824 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2825 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2826 | psa_algorithm_t alg) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2827 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2828 | if (input_is_message) { |
| 2829 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2830 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2831 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2833 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2834 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2835 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2836 | } |
| 2837 | } |
| 2838 | } else { |
| 2839 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2840 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2841 | } |
| 2842 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2843 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2844 | return PSA_SUCCESS; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2845 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2847 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2848 | int input_is_message, |
| 2849 | psa_algorithm_t alg, |
| 2850 | const uint8_t *input, |
| 2851 | size_t input_length, |
| 2852 | uint8_t *signature, |
| 2853 | size_t signature_size, |
| 2854 | size_t *signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2855 | { |
| 2856 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2857 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2858 | psa_key_attributes_t attributes; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2859 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2860 | |
| 2861 | *signature_length = 0; |
| 2862 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2863 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2864 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2865 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2866 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2867 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2868 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2869 | * 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] | 2870 | * buffer can in principle be empty since it doesn't actually have |
| 2871 | * to be a hash.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2872 | if (signature_size == 0) { |
| 2873 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2874 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2875 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2876 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2877 | key, &slot, |
| 2878 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2879 | PSA_KEY_USAGE_SIGN_HASH, |
| 2880 | alg); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2882 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2883 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2884 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2887 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2888 | goto exit; |
| 2889 | } |
| 2890 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 2891 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2892 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2893 | }; |
| 2894 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2895 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2896 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2897 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2898 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2899 | signature, signature_size, signature_length); |
| 2900 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2901 | |
| 2902 | status = psa_driver_wrapper_sign_hash( |
| 2903 | &attributes, slot->key.data, slot->key.bytes, |
| 2904 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2905 | signature, signature_size, signature_length); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2906 | } |
| 2907 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2908 | |
| 2909 | exit: |
| 2910 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2911 | * the trailing part on success) with something that isn't a valid signature |
| 2912 | * (barring an attack on the signature and deliberately-crafted input), |
| 2913 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2914 | if (status == PSA_SUCCESS) { |
| 2915 | memset(signature + *signature_length, '!', |
| 2916 | signature_size - *signature_length); |
| 2917 | } else { |
| 2918 | memset(signature, '!', signature_size); |
| 2919 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2920 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2921 | * memset because signature may be NULL in this case. */ |
| 2922 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2923 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2924 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2925 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2926 | } |
| 2927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2928 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2929 | int input_is_message, |
| 2930 | psa_algorithm_t alg, |
| 2931 | const uint8_t *input, |
| 2932 | size_t input_length, |
| 2933 | const uint8_t *signature, |
| 2934 | size_t signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2935 | { |
| 2936 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2937 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2938 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2939 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2940 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2941 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2942 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2943 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2944 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2945 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2946 | key, &slot, |
| 2947 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2948 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2949 | alg); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2951 | if (status != PSA_SUCCESS) { |
| 2952 | return status; |
| 2953 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2954 | |
| 2955 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2956 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2957 | }; |
| 2958 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2959 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2960 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2961 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2962 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2963 | signature, signature_length); |
| 2964 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2965 | status = psa_driver_wrapper_verify_hash( |
| 2966 | &attributes, slot->key.data, slot->key.bytes, |
| 2967 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2968 | signature, signature_length); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2969 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2970 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2971 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2972 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2973 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2974 | |
| 2975 | } |
| 2976 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2977 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2978 | const psa_key_attributes_t *attributes, |
| 2979 | const uint8_t *key_buffer, |
| 2980 | size_t key_buffer_size, |
| 2981 | psa_algorithm_t alg, |
| 2982 | const uint8_t *input, |
| 2983 | size_t input_length, |
| 2984 | uint8_t *signature, |
| 2985 | size_t signature_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2986 | size_t *signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2987 | { |
| 2988 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2991 | size_t hash_length; |
| 2992 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2993 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2994 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2995 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2996 | input, input_length, |
| 2997 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2998 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2999 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3000 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3001 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3002 | |
| 3003 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3004 | attributes, key_buffer, key_buffer_size, |
| 3005 | alg, hash, hash_length, |
| 3006 | signature, signature_size, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3007 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3009 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3010 | } |
| 3011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3012 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3013 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3014 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3015 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3016 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3017 | size_t signature_size, |
| 3018 | size_t *signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3019 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3020 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3021 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3022 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3023 | |
| 3024 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3025 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3026 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3027 | signature_size, signature_length); |
| 3028 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3029 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3030 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3031 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3032 | LOCAL_INPUT_FREE(input_external, input); |
| 3033 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3034 | return status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3037 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3038 | const psa_key_attributes_t *attributes, |
| 3039 | const uint8_t *key_buffer, |
| 3040 | size_t key_buffer_size, |
| 3041 | psa_algorithm_t alg, |
| 3042 | const uint8_t *input, |
| 3043 | size_t input_length, |
| 3044 | const uint8_t *signature, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3045 | size_t signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3046 | { |
| 3047 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3049 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3050 | size_t hash_length; |
| 3051 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3052 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3053 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3054 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3055 | input, input_length, |
| 3056 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3058 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3059 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3060 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 3061 | |
| 3062 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3063 | attributes, key_buffer, key_buffer_size, |
| 3064 | alg, hash, hash_length, |
| 3065 | signature, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 3066 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3067 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3068 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3071 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3072 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3073 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | size_t input_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3075 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3076 | size_t signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3077 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3078 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3079 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3080 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3081 | |
| 3082 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3083 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3084 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3085 | signature_length); |
| 3086 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3087 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3088 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3089 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3090 | LOCAL_INPUT_FREE(input_external, input); |
| 3091 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3092 | |
| 3093 | return status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 3094 | } |
| 3095 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3096 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3097 | const psa_key_attributes_t *attributes, |
| 3098 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3099 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3100 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3101 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3102 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3103 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3104 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3105 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3106 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3107 | return mbedtls_psa_rsa_sign_hash( |
| 3108 | attributes, |
| 3109 | key_buffer, key_buffer_size, |
| 3110 | alg, hash, hash_length, |
| 3111 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3112 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3113 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3114 | } else { |
| 3115 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3116 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3117 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3118 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3119 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3120 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3121 | return mbedtls_psa_ecdsa_sign_hash( |
| 3122 | attributes, |
| 3123 | key_buffer, key_buffer_size, |
| 3124 | alg, hash, hash_length, |
| 3125 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3126 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3127 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3128 | } else { |
| 3129 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3130 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3131 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3133 | (void) key_buffer; |
| 3134 | (void) key_buffer_size; |
| 3135 | (void) hash; |
| 3136 | (void) hash_length; |
| 3137 | (void) signature; |
| 3138 | (void) signature_size; |
| 3139 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3140 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3141 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3142 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3144 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3145 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3146 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3147 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3148 | uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | size_t signature_size, |
| 3150 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3151 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3152 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3153 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3154 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3155 | |
| 3156 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3157 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3158 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3159 | signature_size, signature_length); |
| 3160 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3161 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3162 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3163 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3164 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3165 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3166 | |
| 3167 | return status; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3168 | } |
| 3169 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3170 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3171 | const psa_key_attributes_t *attributes, |
| 3172 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3173 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3174 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3175 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3176 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3177 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3178 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3179 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3180 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3181 | return mbedtls_psa_rsa_verify_hash( |
| 3182 | attributes, |
| 3183 | key_buffer, key_buffer_size, |
| 3184 | alg, hash, hash_length, |
| 3185 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3186 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3187 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3188 | } else { |
| 3189 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3190 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3191 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3192 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3193 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3194 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3195 | return mbedtls_psa_ecdsa_verify_hash( |
| 3196 | attributes, |
| 3197 | key_buffer, key_buffer_size, |
| 3198 | alg, hash, hash_length, |
| 3199 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3200 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3201 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3202 | } else { |
| 3203 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3204 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3205 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3207 | (void) key_buffer; |
| 3208 | (void) key_buffer_size; |
| 3209 | (void) hash; |
| 3210 | (void) hash_length; |
| 3211 | (void) signature; |
| 3212 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3213 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3214 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3215 | } |
| 3216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3217 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3218 | psa_algorithm_t alg, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3219 | const uint8_t *hash_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3220 | size_t hash_length, |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3221 | const uint8_t *signature_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3222 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3223 | { |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3224 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3225 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3226 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3227 | |
| 3228 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3229 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3230 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3231 | signature_length); |
| 3232 | |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3233 | #if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3234 | exit: |
Thomas Daubney | f446b89 | 2024-01-30 13:36:01 +0000 | [diff] [blame] | 3235 | #endif |
Thomas Daubney | 290aac4 | 2024-01-18 17:23:02 +0000 | [diff] [blame] | 3236 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3237 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3238 | |
| 3239 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3242 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3243 | static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg, |
| 3244 | mbedtls_rsa_context *rsa) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3245 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3246 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg); |
| 3247 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 3248 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
| 3249 | mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg); |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3250 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3251 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3253 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3254 | psa_algorithm_t alg, |
| 3255 | const uint8_t *input, |
| 3256 | size_t input_length, |
| 3257 | const uint8_t *salt, |
| 3258 | size_t salt_length, |
| 3259 | uint8_t *output, |
| 3260 | size_t output_size, |
| 3261 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3262 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3263 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3264 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3265 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3266 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3267 | (void) input; |
| 3268 | (void) input_length; |
| 3269 | (void) salt; |
| 3270 | (void) output; |
| 3271 | (void) output_size; |
| 3272 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3273 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3275 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3276 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3277 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3278 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3279 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3280 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3281 | if (status != PSA_SUCCESS) { |
| 3282 | return status; |
| 3283 | } |
| 3284 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3285 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3286 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3287 | goto exit; |
| 3288 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3290 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3291 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3292 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3293 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3294 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3295 | slot->key.data, |
| 3296 | slot->key.bytes, |
| 3297 | &rsa); |
| 3298 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3299 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3300 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3302 | if (output_size < mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3303 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3304 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3305 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3306 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3307 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3308 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3309 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3310 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3311 | mbedtls_rsa_pkcs1_encrypt(rsa, |
| 3312 | mbedtls_psa_get_random, |
| 3313 | MBEDTLS_PSA_RANDOM_STATE, |
| 3314 | MBEDTLS_RSA_PUBLIC, |
| 3315 | input_length, |
| 3316 | input, |
| 3317 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3318 | #else |
| 3319 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3320 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | } else |
| 3322 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3323 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3324 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3325 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3326 | mbedtls_rsa_rsaes_oaep_encrypt(rsa, |
| 3327 | mbedtls_psa_get_random, |
| 3328 | MBEDTLS_PSA_RANDOM_STATE, |
| 3329 | MBEDTLS_RSA_PUBLIC, |
| 3330 | salt, salt_length, |
| 3331 | input_length, |
| 3332 | input, |
| 3333 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3334 | #else |
| 3335 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3336 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3337 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3338 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3339 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3340 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3341 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3342 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3343 | if (status == PSA_SUCCESS) { |
| 3344 | *output_length = mbedtls_rsa_get_len(rsa); |
| 3345 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3347 | mbedtls_rsa_free(rsa); |
| 3348 | mbedtls_free(rsa); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3349 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3350 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3351 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3352 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3353 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3354 | |
| 3355 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3356 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3358 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3359 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3360 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3361 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3362 | psa_algorithm_t alg, |
| 3363 | const uint8_t *input, |
| 3364 | size_t input_length, |
| 3365 | const uint8_t *salt, |
| 3366 | size_t salt_length, |
| 3367 | uint8_t *output, |
| 3368 | size_t output_size, |
| 3369 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3370 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3371 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3372 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3373 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3374 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3375 | (void) input; |
| 3376 | (void) input_length; |
| 3377 | (void) salt; |
| 3378 | (void) output; |
| 3379 | (void) output_size; |
| 3380 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3381 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3382 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3383 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3384 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3385 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3386 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3387 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3388 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3389 | if (status != PSA_SUCCESS) { |
| 3390 | return status; |
| 3391 | } |
| 3392 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3393 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3394 | goto exit; |
| 3395 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3396 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3397 | if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3399 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3400 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3401 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3402 | slot->key.data, |
| 3403 | slot->key.bytes, |
| 3404 | &rsa); |
| 3405 | if (status != PSA_SUCCESS) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3406 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3407 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3409 | if (input_length != mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3410 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3411 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3412 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3413 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3414 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3415 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3416 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3417 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3418 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3419 | mbedtls_rsa_pkcs1_decrypt(rsa, |
| 3420 | mbedtls_psa_get_random, |
| 3421 | MBEDTLS_PSA_RANDOM_STATE, |
| 3422 | MBEDTLS_RSA_PRIVATE, |
| 3423 | output_length, |
| 3424 | input, |
| 3425 | output, |
| 3426 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3427 | #else |
| 3428 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3429 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3430 | } else |
| 3431 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3432 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3433 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3434 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3435 | mbedtls_rsa_rsaes_oaep_decrypt(rsa, |
| 3436 | mbedtls_psa_get_random, |
| 3437 | MBEDTLS_PSA_RANDOM_STATE, |
| 3438 | MBEDTLS_RSA_PRIVATE, |
| 3439 | salt, salt_length, |
| 3440 | output_length, |
| 3441 | input, |
| 3442 | output, |
| 3443 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3444 | #else |
| 3445 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3446 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3447 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3448 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3449 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3450 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3451 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3452 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3453 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3454 | mbedtls_rsa_free(rsa); |
| 3455 | mbedtls_free(rsa); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3456 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3457 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3458 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3459 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3460 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3461 | |
| 3462 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3463 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3464 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3465 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3466 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3467 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3468 | |
| 3469 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3470 | /****************************************************************/ |
| 3471 | /* Symmetric cryptography */ |
| 3472 | /****************************************************************/ |
| 3473 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3474 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3475 | mbedtls_svc_key_id_t key, |
| 3476 | psa_algorithm_t alg, |
| 3477 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3478 | { |
| 3479 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3480 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3481 | psa_key_attributes_t attributes; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3482 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3483 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3484 | PSA_KEY_USAGE_ENCRYPT : |
| 3485 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3486 | |
| 3487 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3488 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3489 | status = PSA_ERROR_BAD_STATE; |
| 3490 | goto exit; |
| 3491 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3492 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3493 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3494 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3495 | goto exit; |
| 3496 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3497 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3498 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3499 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3500 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3501 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3502 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3503 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3504 | * 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] | 3505 | * so we only set it (in the driver wrapper) after resources have been |
| 3506 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3507 | operation->iv_set = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3508 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3509 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3510 | } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) { |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3511 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3512 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3513 | operation->iv_required = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3514 | } |
| 3515 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3516 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3517 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3518 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3519 | }; |
| 3520 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3521 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3522 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3523 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3524 | &attributes, |
| 3525 | slot->key.data, |
| 3526 | slot->key.bytes, |
| 3527 | alg); |
| 3528 | } else { |
| 3529 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3530 | &attributes, |
| 3531 | slot->key.data, |
| 3532 | slot->key.bytes, |
| 3533 | alg); |
| 3534 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3535 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3536 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3537 | if (status != PSA_SUCCESS) { |
| 3538 | psa_cipher_abort(operation); |
| 3539 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3541 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3542 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3543 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3544 | } |
| 3545 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3546 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3547 | mbedtls_svc_key_id_t key, |
| 3548 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3549 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3550 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3553 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3554 | mbedtls_svc_key_id_t key, |
| 3555 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3556 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3557 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3558 | } |
| 3559 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3560 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 3561 | uint8_t *iv, |
| 3562 | size_t iv_size, |
| 3563 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3564 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3565 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3566 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
Sergey | 04eb7c0 | 2023-03-06 15:37:23 -0700 | [diff] [blame] | 3567 | size_t default_iv_length = 0; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3569 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3570 | status = PSA_ERROR_BAD_STATE; |
| 3571 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3572 | } |
| 3573 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3574 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3575 | status = PSA_ERROR_BAD_STATE; |
| 3576 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3577 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3578 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3579 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3580 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3581 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3582 | goto exit; |
| 3583 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3585 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3586 | status = PSA_ERROR_GENERIC_ERROR; |
| 3587 | goto exit; |
| 3588 | } |
| 3589 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3590 | status = psa_generate_random(local_iv, default_iv_length); |
| 3591 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3592 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3593 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3594 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3595 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3596 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3597 | |
| 3598 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3599 | if (status == PSA_SUCCESS) { |
| 3600 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3601 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3602 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3603 | } else { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3604 | *iv_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3605 | psa_cipher_abort(operation); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3606 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3607 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3608 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3611 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 3612 | const uint8_t *iv, |
| 3613 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3614 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3615 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3616 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3617 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3618 | status = PSA_ERROR_BAD_STATE; |
| 3619 | goto exit; |
| 3620 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3622 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3623 | status = PSA_ERROR_BAD_STATE; |
| 3624 | goto exit; |
| 3625 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3626 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3627 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3628 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3629 | goto exit; |
| 3630 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3631 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3632 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3633 | iv, |
| 3634 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3635 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3636 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3637 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3638 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3639 | } else { |
| 3640 | psa_cipher_abort(operation); |
| 3641 | } |
| 3642 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3643 | } |
| 3644 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3645 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 3646 | const uint8_t *input, |
| 3647 | size_t input_length, |
| 3648 | uint8_t *output, |
| 3649 | size_t output_size, |
| 3650 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3651 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3652 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3653 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3654 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3655 | status = PSA_ERROR_BAD_STATE; |
| 3656 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3657 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3658 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3659 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3660 | status = PSA_ERROR_BAD_STATE; |
| 3661 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3662 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3663 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3664 | status = psa_driver_wrapper_cipher_update(operation, |
| 3665 | input, |
| 3666 | input_length, |
| 3667 | output, |
| 3668 | output_size, |
| 3669 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3670 | |
| 3671 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | if (status != PSA_SUCCESS) { |
| 3673 | psa_cipher_abort(operation); |
| 3674 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3675 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3676 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3679 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 3680 | uint8_t *output, |
| 3681 | size_t output_size, |
| 3682 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3683 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3684 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3685 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3686 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3687 | status = PSA_ERROR_BAD_STATE; |
| 3688 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3689 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3691 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3692 | status = PSA_ERROR_BAD_STATE; |
| 3693 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3694 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3695 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3696 | status = psa_driver_wrapper_cipher_finish(operation, |
| 3697 | output, |
| 3698 | output_size, |
| 3699 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3700 | |
| 3701 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3702 | if (status == PSA_SUCCESS) { |
| 3703 | return psa_cipher_abort(operation); |
| 3704 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3705 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3706 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3707 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3708 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3709 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3712 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3713 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3714 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3715 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3716 | * in use. It's ok to call abort on such an object, and there's |
| 3717 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3718 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3719 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3722 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3723 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3724 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3725 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3727 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3728 | } |
| 3729 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3730 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 3731 | psa_algorithm_t alg, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3732 | const uint8_t *input_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3733 | size_t input_length, |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3734 | uint8_t *output_external, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3735 | size_t output_size, |
| 3736 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3737 | { |
| 3738 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3739 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3740 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3741 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3742 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3743 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3744 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3745 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3746 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3747 | |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3748 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
David Horstmann | 926193a | 2023-12-13 15:55:25 +0000 | [diff] [blame] | 3749 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
David Horstmann | d2ad886 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 3750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3751 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3752 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3753 | goto exit; |
| 3754 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3756 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3757 | PSA_KEY_USAGE_ENCRYPT, |
| 3758 | alg); |
| 3759 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3760 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3761 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3762 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3763 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3764 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3765 | }; |
| 3766 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 3768 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3769 | status = PSA_ERROR_GENERIC_ERROR; |
| 3770 | goto exit; |
| 3771 | } |
| 3772 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3773 | if (default_iv_length > 0) { |
| 3774 | if (output_size < default_iv_length) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3775 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3776 | goto exit; |
| 3777 | } |
| 3778 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3779 | status = psa_generate_random(local_iv, default_iv_length); |
| 3780 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3781 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3782 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3785 | status = psa_driver_wrapper_cipher_encrypt( |
| 3786 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3787 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3788 | mbedtls_buffer_offset(output, default_iv_length), |
| 3789 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3790 | |
| 3791 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3792 | unlock_status = psa_unlock_key_slot(slot); |
| 3793 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3794 | status = unlock_status; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3795 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3797 | if (status == PSA_SUCCESS) { |
| 3798 | if (default_iv_length > 0) { |
| 3799 | memcpy(output, local_iv, default_iv_length); |
| 3800 | } |
| 3801 | *output_length += default_iv_length; |
| 3802 | } else { |
| 3803 | *output_length = 0; |
| 3804 | } |
| 3805 | |
David Horstmann | b80e35a | 2023-12-14 18:12:47 +0000 | [diff] [blame] | 3806 | LOCAL_INPUT_FREE(input_external, input); |
| 3807 | LOCAL_OUTPUT_FREE(output_external, output); |
David Horstmann | d2ad886 | 2023-11-27 17:43:05 +0000 | [diff] [blame] | 3808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3809 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3812 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 3813 | psa_algorithm_t alg, |
| 3814 | const uint8_t *input, |
| 3815 | size_t input_length, |
| 3816 | uint8_t *output, |
| 3817 | size_t output_size, |
| 3818 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3819 | { |
| 3820 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3821 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3822 | psa_key_attributes_t attributes; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3823 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3824 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3825 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3826 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3827 | goto exit; |
| 3828 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3830 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3831 | PSA_KEY_USAGE_DECRYPT, |
| 3832 | alg); |
| 3833 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3834 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3835 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3836 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 3837 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3838 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3839 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3840 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3841 | if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3842 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3843 | goto exit; |
| 3844 | } |
| 3845 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3846 | status = psa_driver_wrapper_cipher_decrypt( |
| 3847 | &attributes, slot->key.data, slot->key.bytes, |
| 3848 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3849 | output, output_size, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3850 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3851 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3852 | unlock_status = psa_unlock_key_slot(slot); |
| 3853 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3854 | status = unlock_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3855 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3856 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3857 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3858 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3859 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3860 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3861 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3865 | /****************************************************************/ |
| 3866 | /* AEAD */ |
| 3867 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3868 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3869 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 3870 | psa_algorithm_t alg, |
| 3871 | const uint8_t *nonce, |
| 3872 | size_t nonce_length, |
| 3873 | const uint8_t *additional_data, |
| 3874 | size_t additional_data_length, |
| 3875 | const uint8_t *plaintext, |
| 3876 | size_t plaintext_length, |
| 3877 | uint8_t *ciphertext, |
| 3878 | size_t ciphertext_size, |
| 3879 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3880 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3881 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3882 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3883 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3884 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3886 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 3887 | return PSA_ERROR_NOT_SUPPORTED; |
| 3888 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3889 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3890 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3891 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3892 | if (status != PSA_SUCCESS) { |
| 3893 | return status; |
| 3894 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3895 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3896 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3897 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3898 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3899 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3900 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3901 | &attributes, slot->key.data, slot->key.bytes, |
| 3902 | alg, |
| 3903 | nonce, nonce_length, |
| 3904 | additional_data, additional_data_length, |
| 3905 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3906 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3908 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 3909 | memset(ciphertext, 0, ciphertext_size); |
| 3910 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3911 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3912 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3913 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3914 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3915 | } |
| 3916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3917 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 3918 | psa_algorithm_t alg, |
| 3919 | const uint8_t *nonce, |
| 3920 | size_t nonce_length, |
| 3921 | const uint8_t *additional_data, |
| 3922 | size_t additional_data_length, |
| 3923 | const uint8_t *ciphertext, |
| 3924 | size_t ciphertext_length, |
| 3925 | uint8_t *plaintext, |
| 3926 | size_t plaintext_size, |
| 3927 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3928 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3929 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3930 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3931 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3932 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3933 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3934 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 3935 | return PSA_ERROR_NOT_SUPPORTED; |
| 3936 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3937 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3938 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3939 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3940 | if (status != PSA_SUCCESS) { |
| 3941 | return status; |
| 3942 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3943 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3944 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3945 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3946 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3947 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3948 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3949 | &attributes, slot->key.data, slot->key.bytes, |
| 3950 | alg, |
| 3951 | nonce, nonce_length, |
| 3952 | additional_data, additional_data_length, |
| 3953 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3954 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3955 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3956 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 3957 | memset(plaintext, 0, plaintext_size); |
| 3958 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3959 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3960 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3961 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3962 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3963 | } |
| 3964 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3965 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3966 | /* Generators */ |
| 3967 | /****************************************************************/ |
| 3968 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3969 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3970 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3971 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3972 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3973 | #endif /* At least one builtin KDF */ |
| 3974 | |
| 3975 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3976 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3977 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3978 | static psa_status_t psa_key_derivation_start_hmac( |
| 3979 | psa_mac_operation_t *operation, |
| 3980 | psa_algorithm_t hash_alg, |
| 3981 | const uint8_t *hmac_key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3982 | size_t hmac_key_length) |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3983 | { |
| 3984 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3985 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3986 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 3987 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 3988 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3989 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3990 | operation->is_sign = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3991 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3992 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3993 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 3994 | &attributes, |
| 3995 | hmac_key, hmac_key_length, |
| 3996 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3997 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3998 | psa_reset_key_attributes(&attributes); |
| 3999 | return status; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4000 | } |
| 4001 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4002 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4003 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4004 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4005 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4006 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4007 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4008 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4009 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4010 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4011 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 4012 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 4013 | } else { |
| 4014 | return operation->alg; |
| 4015 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4016 | } |
| 4017 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4018 | 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] | 4019 | { |
| 4020 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4021 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 4022 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4023 | /* The object has (apparently) been initialized but it is not |
| 4024 | * in use. It's ok to call abort on such an object, and there's |
| 4025 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4026 | } else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4027 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4028 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4029 | mbedtls_free(operation->ctx.hkdf.info); |
| 4030 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 4031 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4032 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4033 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4034 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4035 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4036 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 4037 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4038 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 4039 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 4040 | operation->ctx.tls12_prf.secret_length); |
| 4041 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4044 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 4045 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 4046 | operation->ctx.tls12_prf.seed_length); |
| 4047 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4050 | if (operation->ctx.tls12_prf.label != NULL) { |
| 4051 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 4052 | operation->ctx.tls12_prf.label_length); |
| 4053 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4056 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4059 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4060 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4061 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4062 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4063 | { |
| 4064 | status = PSA_ERROR_BAD_STATE; |
| 4065 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4066 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 4067 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4070 | 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] | 4071 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4072 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4073 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4074 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4075 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4078 | *capacity = operation->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4079 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4080 | } |
| 4081 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4082 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 4083 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4084 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4085 | if (operation->alg == 0) { |
| 4086 | return PSA_ERROR_BAD_STATE; |
| 4087 | } |
| 4088 | if (capacity > operation->capacity) { |
| 4089 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4090 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4091 | operation->capacity = capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4092 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4093 | } |
| 4094 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4095 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4096 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4097 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4098 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 4099 | psa_algorithm_t hash_alg, |
| 4100 | uint8_t *output, |
| 4101 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4102 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4103 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4104 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4105 | psa_status_t status; |
| 4106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4107 | if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) { |
| 4108 | return PSA_ERROR_BAD_STATE; |
| 4109 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4110 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4112 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4113 | /* Copy what remains of the current block */ |
| 4114 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4115 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4116 | n = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4117 | } |
| 4118 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4119 | output += n; |
| 4120 | output_length -= n; |
| 4121 | hkdf->offset_in_block += n; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4122 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4123 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4124 | } |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4125 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4126 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4127 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4128 | * object was corrupted or if this function is called directly |
| 4129 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4130 | if (hkdf->block_number == 0xff) { |
| 4131 | return PSA_ERROR_BAD_STATE; |
| 4132 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4133 | |
| 4134 | /* We need a new block */ |
| 4135 | ++hkdf->block_number; |
| 4136 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4138 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4139 | hash_alg, |
| 4140 | hkdf->prk, |
| 4141 | hash_length); |
| 4142 | if (status != PSA_SUCCESS) { |
| 4143 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4144 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4145 | |
| 4146 | if (hkdf->block_number != 1) { |
| 4147 | status = psa_mac_update(&hkdf->hmac, |
| 4148 | hkdf->output_block, |
| 4149 | hash_length); |
| 4150 | if (status != PSA_SUCCESS) { |
| 4151 | return status; |
| 4152 | } |
| 4153 | } |
| 4154 | status = psa_mac_update(&hkdf->hmac, |
| 4155 | hkdf->info, |
| 4156 | hkdf->info_length); |
| 4157 | if (status != PSA_SUCCESS) { |
| 4158 | return status; |
| 4159 | } |
| 4160 | status = psa_mac_update(&hkdf->hmac, |
| 4161 | &hkdf->block_number, 1); |
| 4162 | if (status != PSA_SUCCESS) { |
| 4163 | return status; |
| 4164 | } |
| 4165 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4166 | hkdf->output_block, |
| 4167 | sizeof(hkdf->output_block), |
| 4168 | &hmac_output_length); |
| 4169 | if (status != PSA_SUCCESS) { |
| 4170 | return status; |
| 4171 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4172 | } |
| 4173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4174 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4175 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4176 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4177 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4178 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4179 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4180 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4181 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4182 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4183 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4184 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 4185 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4186 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4187 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4188 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4189 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4190 | /* We can't be wanting more output after block 0xff, otherwise |
| 4191 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4192 | * prevented this call. It could happen only if the operation |
| 4193 | * object was corrupted or if this function is called directly |
| 4194 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4195 | if (tls12_prf->block_number == 0xff) { |
| 4196 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4197 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4198 | |
| 4199 | /* We need a new block */ |
| 4200 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4201 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4202 | |
| 4203 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4204 | * |
| 4205 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4206 | * |
| 4207 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4208 | * HMAC_hash(secret, A(2) + seed) + |
| 4209 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4210 | * |
| 4211 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4212 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4213 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4214 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4215 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4216 | * is currently extracted as `output_block` and where i is |
| 4217 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4218 | */ |
| 4219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4220 | status = psa_key_derivation_start_hmac(&hmac, |
| 4221 | hash_alg, |
| 4222 | tls12_prf->secret, |
| 4223 | tls12_prf->secret_length); |
| 4224 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4225 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4226 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4227 | |
| 4228 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4229 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4230 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4231 | * the variable seed and in this instance means it in the context of the |
| 4232 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4233 | status = psa_mac_update(&hmac, |
| 4234 | tls12_prf->label, |
| 4235 | tls12_prf->label_length); |
| 4236 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4237 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4238 | } |
| 4239 | status = psa_mac_update(&hmac, |
| 4240 | tls12_prf->seed, |
| 4241 | tls12_prf->seed_length); |
| 4242 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4243 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4244 | } |
| 4245 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4246 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4247 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4248 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4249 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4250 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4251 | } |
| 4252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4253 | status = psa_mac_sign_finish(&hmac, |
| 4254 | tls12_prf->Ai, hash_length, |
| 4255 | &hmac_output_length); |
| 4256 | if (hmac_output_length != hash_length) { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4257 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4258 | } |
| 4259 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4260 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4261 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4262 | |
| 4263 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4264 | status = psa_key_derivation_start_hmac(&hmac, |
| 4265 | hash_alg, |
| 4266 | tls12_prf->secret, |
| 4267 | tls12_prf->secret_length); |
| 4268 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4269 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4270 | } |
| 4271 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4272 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4273 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4274 | } |
| 4275 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 4276 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4277 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4278 | } |
| 4279 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 4280 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4281 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4282 | } |
| 4283 | status = psa_mac_sign_finish(&hmac, |
| 4284 | tls12_prf->output_block, hash_length, |
| 4285 | &hmac_output_length); |
| 4286 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4287 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4288 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4289 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4290 | |
| 4291 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4292 | cleanup_status = psa_mac_abort(&hmac); |
| 4293 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4294 | status = cleanup_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4295 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4297 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4298 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4299 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4300 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4301 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4302 | psa_algorithm_t alg, |
| 4303 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4304 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4305 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4306 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 4307 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4308 | psa_status_t status; |
| 4309 | uint8_t offset, length; |
| 4310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4311 | switch (tls12_prf->state) { |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4312 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4313 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4314 | break; |
| 4315 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4316 | break; |
| 4317 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4318 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4319 | } |
| 4320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4321 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4322 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4323 | if (tls12_prf->left_in_block == 0) { |
| 4324 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 4325 | alg); |
| 4326 | if (status != PSA_SUCCESS) { |
| 4327 | return status; |
| 4328 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4329 | |
| 4330 | continue; |
| 4331 | } |
| 4332 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4333 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4334 | length = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4335 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4336 | length = tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4337 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4338 | |
| 4339 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4340 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4341 | output += length; |
| 4342 | output_length -= length; |
| 4343 | tls12_prf->left_in_block -= length; |
| 4344 | } |
| 4345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4346 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4347 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4348 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4349 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4350 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4351 | psa_status_t psa_key_derivation_output_bytes( |
| 4352 | psa_key_derivation_operation_t *operation, |
| 4353 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4354 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4355 | { |
| 4356 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4357 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4359 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4360 | /* This is a blank operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4361 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4364 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4365 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4366 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4367 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4368 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4369 | goto exit; |
| 4370 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4371 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4372 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4373 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4374 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4375 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4376 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4377 | * output_length > 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4378 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4379 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4380 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4381 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4382 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4383 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4384 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4385 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg, |
| 4386 | output, output_length); |
| 4387 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4388 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4389 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4390 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4391 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4392 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4393 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 4394 | kdf_alg, output, |
| 4395 | output_length); |
| 4396 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4398 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4399 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4400 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4401 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4405 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4406 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4407 | * This allows us to differentiate between exhausted operations and |
| 4408 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4409 | * operations. */ |
| 4410 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4411 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4412 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4413 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4414 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4415 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4416 | } |
| 4417 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4418 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4419 | 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] | 4420 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4421 | if (data_size >= 8) { |
| 4422 | mbedtls_des_key_set_parity(data); |
| 4423 | } |
| 4424 | if (data_size >= 16) { |
| 4425 | mbedtls_des_key_set_parity(data + 8); |
| 4426 | } |
| 4427 | if (data_size >= 24) { |
| 4428 | mbedtls_des_key_set_parity(data + 16); |
| 4429 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4430 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4431 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4432 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4433 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4434 | psa_key_slot_t *slot, |
| 4435 | size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4436 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4437 | { |
| 4438 | uint8_t *data = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4439 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4440 | psa_status_t status; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4441 | psa_key_attributes_t attributes; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4443 | if (!key_type_is_raw_bytes(slot->attr.type)) { |
| 4444 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4445 | } |
| 4446 | if (bits % 8 != 0) { |
| 4447 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4448 | } |
| 4449 | data = mbedtls_calloc(1, bytes); |
| 4450 | if (data == NULL) { |
| 4451 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4452 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4454 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 4455 | if (status != PSA_SUCCESS) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4456 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4457 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4458 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4459 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 4460 | psa_des_set_key_parity(data, bytes); |
| 4461 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4462 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4463 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4464 | status = psa_allocate_buffer_to_slot(slot, bytes); |
| 4465 | if (status != PSA_SUCCESS) { |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4466 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4467 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4468 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4469 | slot->attr.bits = (psa_key_bits_t) bits; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 4470 | attributes = (psa_key_attributes_t) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4471 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4472 | }; |
| 4473 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4474 | status = psa_driver_wrapper_import_key(&attributes, |
| 4475 | data, bytes, |
| 4476 | slot->key.data, |
| 4477 | slot->key.bytes, |
| 4478 | &slot->key.bytes, &bits); |
| 4479 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4480 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4481 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4482 | |
| 4483 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4484 | mbedtls_free(data); |
| 4485 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4486 | } |
| 4487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4488 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 4489 | psa_key_derivation_operation_t *operation, |
| 4490 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4491 | { |
| 4492 | psa_status_t status; |
| 4493 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4494 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4495 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4496 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4497 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4498 | /* Reject any attempt to create a zero-length key so that we don't |
| 4499 | * 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] | 4500 | if (psa_get_key_bits(attributes) == 0) { |
| 4501 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4502 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4504 | if (operation->alg == PSA_ALG_NONE) { |
| 4505 | return PSA_ERROR_BAD_STATE; |
| 4506 | } |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4507 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4508 | if (!operation->can_output_key) { |
| 4509 | return PSA_ERROR_NOT_PERMITTED; |
| 4510 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4512 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 4513 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4514 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4515 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4516 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4517 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4518 | } |
| 4519 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4520 | if (status == PSA_SUCCESS) { |
| 4521 | status = psa_generate_derived_key_internal(slot, |
| 4522 | attributes->core.bits, |
| 4523 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4524 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4525 | if (status == PSA_SUCCESS) { |
| 4526 | status = psa_finish_key_creation(slot, driver, key); |
| 4527 | } |
| 4528 | if (status != PSA_SUCCESS) { |
| 4529 | psa_fail_key_creation(slot, driver); |
| 4530 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4533 | } |
| 4534 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4535 | |
| 4536 | |
| 4537 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4538 | /* Key derivation */ |
| 4539 | /****************************************************************/ |
| 4540 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4541 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4542 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4543 | { |
| 4544 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4545 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4546 | return 1; |
| 4547 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4548 | #endif |
| 4549 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4550 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4551 | return 1; |
| 4552 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4553 | #endif |
| 4554 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4555 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4556 | return 1; |
| 4557 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4558 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4559 | return 0; |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4560 | } |
| 4561 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4562 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4563 | { |
| 4564 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4565 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 4566 | psa_hash_abort(&operation); |
| 4567 | return status; |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4570 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4571 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4572 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4573 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4574 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4575 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4576 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4577 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4578 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4579 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 4580 | return PSA_ERROR_NOT_SUPPORTED; |
| 4581 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4582 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4583 | /* All currently supported key derivation algorithms are based on a |
| 4584 | * hash algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4585 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4586 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 4587 | if (hash_size == 0) { |
| 4588 | return PSA_ERROR_NOT_SUPPORTED; |
| 4589 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4590 | |
| 4591 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4592 | * we might fail later, which is somewhat unfriendly and potentially |
| 4593 | * risk-prone. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4594 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 4595 | if (status != PSA_SUCCESS) { |
| 4596 | return status; |
| 4597 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4599 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4600 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 4601 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 4602 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4603 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4604 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4605 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4606 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4607 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4609 | 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] | 4610 | { |
| 4611 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4612 | if (alg == PSA_ALG_ECDH) { |
| 4613 | return PSA_SUCCESS; |
| 4614 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4615 | #endif |
| 4616 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4617 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4618 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4619 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4620 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4621 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 4622 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4623 | { |
| 4624 | psa_status_t status; |
| 4625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4626 | if (operation->alg != 0) { |
| 4627 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4628 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4629 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4630 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 4631 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4632 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 4633 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4634 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 4635 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 4636 | status = psa_key_agreement_try_support(ka_alg); |
| 4637 | if (status != PSA_SUCCESS) { |
| 4638 | return status; |
| 4639 | } |
| 4640 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 4641 | #else |
| 4642 | return PSA_ERROR_NOT_SUPPORTED; |
| 4643 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4644 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 4645 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4646 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 4647 | #else |
| 4648 | return PSA_ERROR_NOT_SUPPORTED; |
| 4649 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4650 | } else { |
| 4651 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4652 | } |
| 4653 | |
| 4654 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4656 | } |
| 4657 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4660 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4661 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 4662 | psa_algorithm_t hash_alg, |
| 4663 | psa_key_derivation_step_t step, |
| 4664 | const uint8_t *data, |
| 4665 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4666 | { |
| 4667 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4668 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4669 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4670 | if (hkdf->state != HKDF_STATE_INIT) { |
| 4671 | return PSA_ERROR_BAD_STATE; |
| 4672 | } else { |
| 4673 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4674 | hash_alg, |
| 4675 | data, data_length); |
| 4676 | if (status != PSA_SUCCESS) { |
| 4677 | return status; |
| 4678 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4679 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4680 | return PSA_SUCCESS; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4681 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4682 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4683 | /* If no salt was provided, use an empty salt. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4684 | if (hkdf->state == HKDF_STATE_INIT) { |
| 4685 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4686 | hash_alg, |
| 4687 | NULL, 0); |
| 4688 | if (status != PSA_SUCCESS) { |
| 4689 | return status; |
| 4690 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4691 | hkdf->state = HKDF_STATE_STARTED; |
| 4692 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4693 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 4694 | return PSA_ERROR_BAD_STATE; |
| 4695 | } |
| 4696 | status = psa_mac_update(&hkdf->hmac, |
| 4697 | data, data_length); |
| 4698 | if (status != PSA_SUCCESS) { |
| 4699 | return status; |
| 4700 | } |
| 4701 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4702 | hkdf->prk, |
| 4703 | sizeof(hkdf->prk), |
| 4704 | &data_length); |
| 4705 | if (status != PSA_SUCCESS) { |
| 4706 | return status; |
| 4707 | } |
| 4708 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4709 | hkdf->block_number = 0; |
| 4710 | hkdf->state = HKDF_STATE_KEYED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4711 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4712 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4713 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 4714 | return PSA_ERROR_BAD_STATE; |
| 4715 | } |
| 4716 | if (hkdf->info_set) { |
| 4717 | return PSA_ERROR_BAD_STATE; |
| 4718 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4719 | hkdf->info_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4720 | if (data_length != 0) { |
| 4721 | hkdf->info = mbedtls_calloc(1, data_length); |
| 4722 | if (hkdf->info == NULL) { |
| 4723 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4724 | } |
| 4725 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4726 | } |
| 4727 | hkdf->info_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4728 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4729 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4730 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4731 | } |
| 4732 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4733 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4734 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4735 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4736 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4737 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 4738 | const uint8_t *data, |
| 4739 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4740 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4741 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 4742 | return PSA_ERROR_BAD_STATE; |
| 4743 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4744 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4745 | if (data_length != 0) { |
| 4746 | prf->seed = mbedtls_calloc(1, data_length); |
| 4747 | if (prf->seed == NULL) { |
| 4748 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4749 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4751 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4752 | prf->seed_length = data_length; |
| 4753 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4754 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4755 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4756 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4757 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4758 | } |
| 4759 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4760 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 4761 | const uint8_t *data, |
| 4762 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4763 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4764 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 4765 | return PSA_ERROR_BAD_STATE; |
| 4766 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | if (data_length != 0) { |
| 4769 | prf->secret = mbedtls_calloc(1, data_length); |
| 4770 | if (prf->secret == NULL) { |
| 4771 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4772 | } |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4774 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4775 | prf->secret_length = data_length; |
| 4776 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4777 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4778 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4779 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4780 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4781 | } |
| 4782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4783 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 4784 | const uint8_t *data, |
| 4785 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4786 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4787 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 4788 | return PSA_ERROR_BAD_STATE; |
| 4789 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4791 | if (data_length != 0) { |
| 4792 | prf->label = mbedtls_calloc(1, data_length); |
| 4793 | if (prf->label == NULL) { |
| 4794 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4795 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4797 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4798 | prf->label_length = data_length; |
| 4799 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4800 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4801 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4802 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4803 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4806 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 4807 | psa_key_derivation_step_t step, |
| 4808 | const uint8_t *data, |
| 4809 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4810 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4811 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4812 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4813 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4814 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4815 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4816 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4817 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4818 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4819 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4820 | } |
| 4821 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4822 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4823 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4824 | |
| 4825 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4826 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4827 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4828 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4829 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4830 | { |
| 4831 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4832 | 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] | 4833 | uint8_t *cur = pms; |
| 4834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4835 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 4836 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4837 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4838 | |
| 4839 | /* Quoting RFC 4279, Section 2: |
| 4840 | * |
| 4841 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4842 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4843 | * uint16 with the value N, and the PSK itself. |
| 4844 | */ |
| 4845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4846 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 4847 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 4848 | memset(cur, 0, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4849 | cur += data_length; |
| 4850 | *cur++ = pms[0]; |
| 4851 | *cur++ = pms[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4852 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4853 | cur += data_length; |
| 4854 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4855 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4856 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4857 | mbedtls_platform_zeroize(pms, sizeof(pms)); |
| 4858 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4859 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4860 | |
| 4861 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4862 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4863 | psa_key_derivation_step_t step, |
| 4864 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4865 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4866 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4867 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 4868 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 4869 | data, data_length); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4870 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4872 | return psa_tls12_prf_input(prf, step, data, data_length); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4873 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4874 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4875 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4876 | /** Check whether the given key type is acceptable for the given |
| 4877 | * input step of a key derivation. |
| 4878 | * |
| 4879 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4880 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4881 | * Both secret and non-secret inputs can alternatively have the type |
| 4882 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4883 | * that the input was passed as a buffer rather than via a key object. |
| 4884 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4885 | static int psa_key_derivation_check_input_type( |
| 4886 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4887 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4888 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4889 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4890 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4891 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 4892 | return PSA_SUCCESS; |
| 4893 | } |
| 4894 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 4895 | return PSA_SUCCESS; |
| 4896 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4897 | break; |
| 4898 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4899 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4900 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4901 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4902 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 4903 | return PSA_SUCCESS; |
| 4904 | } |
| 4905 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 4906 | return PSA_SUCCESS; |
| 4907 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4908 | break; |
| 4909 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4910 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4913 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4914 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4915 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4916 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4917 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4918 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4919 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4920 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4921 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4922 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4923 | status = psa_key_derivation_check_input_type(step, key_type); |
| 4924 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4925 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4926 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4927 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4928 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4929 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4930 | status = psa_hkdf_input(&operation->ctx.hkdf, |
| 4931 | PSA_ALG_HKDF_GET_HASH(kdf_alg), |
| 4932 | step, data, data_length); |
| 4933 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4934 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4935 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4936 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4937 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 4938 | step, data, data_length); |
| 4939 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4940 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4941 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4942 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4943 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 4944 | step, data, data_length); |
| 4945 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4946 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4947 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4948 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4949 | (void) data; |
| 4950 | (void) data_length; |
| 4951 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4952 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4955 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4956 | if (status != PSA_SUCCESS) { |
| 4957 | psa_key_derivation_abort(operation); |
| 4958 | } |
| 4959 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4960 | } |
| 4961 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4962 | psa_status_t psa_key_derivation_input_bytes( |
| 4963 | psa_key_derivation_operation_t *operation, |
| 4964 | psa_key_derivation_step_t step, |
| 4965 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4966 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4967 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4968 | return psa_key_derivation_input_internal(operation, step, |
| 4969 | PSA_KEY_TYPE_NONE, |
| 4970 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4971 | } |
| 4972 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4973 | psa_status_t psa_key_derivation_input_key( |
| 4974 | psa_key_derivation_operation_t *operation, |
| 4975 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4976 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4977 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4978 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4979 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4980 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4981 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4982 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4983 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 4984 | if (status != PSA_SUCCESS) { |
| 4985 | psa_key_derivation_abort(operation); |
| 4986 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4987 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4988 | |
| 4989 | /* Passing a key object as a SECRET input unlocks the permission |
| 4990 | * to output to a key object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4991 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4992 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4993 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4995 | status = psa_key_derivation_input_internal(operation, |
| 4996 | step, slot->attr.type, |
| 4997 | slot->key.data, |
| 4998 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5000 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5002 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5003 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5004 | |
| 5005 | |
| 5006 | |
| 5007 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5008 | /* Key agreement */ |
| 5009 | /****************************************************************/ |
| 5010 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5011 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5012 | static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key, |
| 5013 | size_t peer_key_length, |
| 5014 | const mbedtls_ecp_keypair *our_key, |
| 5015 | uint8_t *shared_secret, |
| 5016 | size_t shared_secret_size, |
| 5017 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5018 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5019 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5020 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5021 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5022 | size_t bits = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5023 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 5024 | mbedtls_ecdh_init(&ecdh); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5025 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5026 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5027 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5028 | bits, |
| 5029 | peer_key, |
| 5030 | peer_key_length, |
| 5031 | &their_key); |
| 5032 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5033 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5034 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5035 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5036 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5037 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 5038 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5039 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5040 | } |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5041 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5042 | mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS)); |
| 5043 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5044 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5045 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5046 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5047 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5048 | mbedtls_ecdh_calc_secret(&ecdh, |
| 5049 | shared_secret_length, |
| 5050 | shared_secret, shared_secret_size, |
| 5051 | mbedtls_psa_get_random, |
| 5052 | MBEDTLS_PSA_RANDOM_STATE)); |
| 5053 | if (status != PSA_SUCCESS) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5054 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5055 | } |
| 5056 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5057 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5058 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5059 | |
| 5060 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5061 | if (status != PSA_SUCCESS) { |
| 5062 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 5063 | } |
| 5064 | mbedtls_ecdh_free(&ecdh); |
| 5065 | mbedtls_ecp_keypair_free(their_key); |
| 5066 | mbedtls_free(their_key); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5067 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5068 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5069 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5070 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5072 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5074 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 5075 | psa_key_slot_t *private_key, |
| 5076 | const uint8_t *peer_key, |
| 5077 | size_t peer_key_length, |
| 5078 | uint8_t *shared_secret, |
| 5079 | size_t shared_secret_size, |
| 5080 | size_t *shared_secret_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5081 | { |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5082 | mbedtls_ecp_keypair *ecp = NULL; |
| 5083 | psa_status_t status; |
Agathiyan Bragadeesh | e7eb805 | 2023-07-31 16:16:38 +0100 | [diff] [blame] | 5084 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5085 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5086 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5087 | case PSA_ALG_ECDH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5088 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) { |
| 5089 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5090 | } |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5091 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5092 | private_key->attr.type, |
| 5093 | private_key->attr.bits, |
| 5094 | private_key->key.data, |
| 5095 | private_key->key.bytes, |
| 5096 | &ecp); |
| 5097 | if (status != PSA_SUCCESS) { |
| 5098 | return status; |
| 5099 | } |
| 5100 | status = psa_key_agreement_ecdh(peer_key, peer_key_length, |
| 5101 | ecp, |
| 5102 | shared_secret, shared_secret_size, |
| 5103 | shared_secret_length); |
| 5104 | mbedtls_ecp_keypair_free(ecp); |
| 5105 | mbedtls_free(ecp); |
| 5106 | return status; |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5107 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5108 | default: |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5109 | (void) ecp; |
| 5110 | (void) status; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5111 | (void) private_key; |
| 5112 | (void) peer_key; |
| 5113 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5114 | (void) shared_secret; |
| 5115 | (void) shared_secret_size; |
| 5116 | (void) shared_secret_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5117 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5118 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5119 | } |
| 5120 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5121 | /* 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] | 5122 | * to potentially free embedded data structures and wipe confidential data. |
| 5123 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5124 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 5125 | psa_key_derivation_step_t step, |
| 5126 | psa_key_slot_t *private_key, |
| 5127 | const uint8_t *peer_key, |
| 5128 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5129 | { |
| 5130 | psa_status_t status; |
| 5131 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5132 | size_t shared_secret_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5133 | 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] | 5134 | |
| 5135 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5136 | * secret. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5137 | status = psa_key_agreement_raw_internal(ka_alg, |
| 5138 | private_key, |
| 5139 | peer_key, peer_key_length, |
| 5140 | shared_secret, |
| 5141 | sizeof(shared_secret), |
| 5142 | &shared_secret_length); |
| 5143 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5144 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5145 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5146 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5147 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5148 | * the shared secret. A shared secret is permitted wherever a key |
| 5149 | * of type DERIVE is permitted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5150 | status = psa_key_derivation_input_internal(operation, step, |
| 5151 | PSA_KEY_TYPE_DERIVE, |
| 5152 | shared_secret, |
| 5153 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5154 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5155 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 5156 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5157 | } |
| 5158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5159 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 5160 | psa_key_derivation_step_t step, |
| 5161 | mbedtls_svc_key_id_t private_key, |
| 5162 | const uint8_t *peer_key, |
| 5163 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5164 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5165 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5166 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5167 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5169 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5170 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5171 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5172 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5173 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 5174 | if (status != PSA_SUCCESS) { |
| 5175 | return status; |
| 5176 | } |
| 5177 | status = psa_key_agreement_internal(operation, step, |
| 5178 | slot, |
| 5179 | peer_key, peer_key_length); |
| 5180 | if (status != PSA_SUCCESS) { |
| 5181 | psa_key_derivation_abort(operation); |
| 5182 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5183 | /* If a private key has been added as SECRET, we allow the derived |
| 5184 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5185 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5186 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5187 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5188 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5190 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5192 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5195 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 5196 | mbedtls_svc_key_id_t private_key, |
| 5197 | const uint8_t *peer_key, |
| 5198 | size_t peer_key_length, |
| 5199 | uint8_t *output, |
| 5200 | size_t output_size, |
| 5201 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5202 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5203 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5204 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5205 | psa_key_slot_t *slot = NULL; |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5206 | size_t expected_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5208 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5209 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5210 | goto exit; |
| 5211 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5212 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5213 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 5214 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5215 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5216 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5217 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5218 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5219 | * for the output size. The PSA specification only guarantees that this |
| 5220 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5221 | * but it might be nice to allow smaller buffers if the output fits. |
| 5222 | * At the time of writing this comment, with only ECDH implemented, |
| 5223 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5224 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5225 | * be exact for it as well. */ |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 5226 | expected_length = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5227 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 5228 | if (output_size < expected_length) { |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5229 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5230 | goto exit; |
| 5231 | } |
| 5232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5233 | status = psa_key_agreement_raw_internal(alg, slot, |
| 5234 | peer_key, peer_key_length, |
| 5235 | output, output_size, |
| 5236 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5237 | |
| 5238 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5239 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5240 | /* If an error happens and is not handled properly, the output |
| 5241 | * may be used as a key to protect sensitive data. Arrange for such |
| 5242 | * a key to be random, which is likely to result in decryption or |
| 5243 | * verification errors. This is better than filling the buffer with |
| 5244 | * some constant data such as zeros, which would result in the data |
| 5245 | * being protected with a reproducible, easily knowable key. |
| 5246 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5247 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5248 | *output_length = output_size; |
| 5249 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5251 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5253 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5254 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5255 | |
| 5256 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5257 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5258 | /****************************************************************/ |
| 5259 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5260 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5261 | |
Gilles Peskine | 9a9d5ee | 2023-04-28 21:00:28 +0200 | [diff] [blame] | 5262 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5263 | #include "mbedtls/entropy_poll.h" |
| 5264 | #endif |
| 5265 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5266 | /** Initialize the PSA random generator. |
| 5267 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5268 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5269 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5270 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5271 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5272 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5273 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5274 | /* Set default configuration if |
| 5275 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5276 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5277 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5278 | } |
| 5279 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5280 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5281 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5283 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5284 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5285 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5286 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5287 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5288 | mbedtls_entropy_add_source(&rng->entropy, |
| 5289 | mbedtls_nv_seed_poll, NULL, |
| 5290 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5291 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5292 | #endif |
| 5293 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5294 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5295 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | /** Deinitialize the PSA random generator. |
| 5299 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5300 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5301 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5302 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5303 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5304 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5305 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 5306 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5307 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | /** Seed the PSA random generator. |
| 5311 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5312 | 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] | 5313 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5314 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5315 | /* Do nothing: the external RNG seeds itself. */ |
| 5316 | (void) rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5317 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5318 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5319 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5320 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 5321 | drbg_seed, sizeof(drbg_seed) - 1); |
| 5322 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5323 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5326 | psa_status_t psa_generate_random(uint8_t *output, |
| 5327 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5328 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5329 | GUARD_MODULE_INITIALIZED; |
| 5330 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5331 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5332 | |
| 5333 | size_t output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5334 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 5335 | output, output_size, |
| 5336 | &output_length); |
| 5337 | if (status != PSA_SUCCESS) { |
| 5338 | return status; |
| 5339 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5340 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 5341 | * for the external RNG interface. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5342 | if (output_length != output_size) { |
| 5343 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 5344 | } |
| 5345 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5346 | |
| 5347 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5348 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5349 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5350 | size_t request_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5351 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5352 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5353 | output_size); |
| 5354 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 5355 | output, request_size); |
| 5356 | if (ret != 0) { |
| 5357 | return mbedtls_to_psa_error(ret); |
| 5358 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5359 | output_size -= request_size; |
| 5360 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5361 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5362 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5363 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5364 | } |
| 5365 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5366 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5367 | * |
| 5368 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5369 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5370 | * PSA API doesn't support passing an explicit state. |
| 5371 | * |
| 5372 | * In the non-external case, psa_generate_random() calls an |
| 5373 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5374 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5375 | * instead of doing this back-and-forth between the PSA API and the |
| 5376 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5377 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5378 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5379 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5380 | int mbedtls_psa_get_random(void *p_rng, |
| 5381 | unsigned char *output, |
| 5382 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5383 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5384 | /* This function takes a pointer to the RNG state because that's what |
| 5385 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5386 | * its own state internally and doesn't let the caller access that state. |
| 5387 | * So we just ignore the state parameter, and in practice we'll pass |
| 5388 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5389 | (void) p_rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5390 | psa_status_t status = psa_generate_random(output, output_size); |
| 5391 | if (status == PSA_SUCCESS) { |
| 5392 | return 0; |
| 5393 | } else { |
| 5394 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 5395 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5396 | } |
| 5397 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5398 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5399 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5400 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 5401 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5402 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5403 | if (global_data.initialized) { |
| 5404 | return PSA_ERROR_NOT_PERMITTED; |
| 5405 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5407 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 5408 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 5409 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 5410 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5411 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5412 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5413 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5414 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5415 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5416 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5417 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5418 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5419 | * \param type The key type |
| 5420 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5421 | * |
| 5422 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5423 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5424 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5425 | * The size in bits of the key is not valid. |
| 5426 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5427 | * The type and/or the size in bits of the key or the combination of |
| 5428 | * the two is not supported. |
| 5429 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5430 | 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] | 5431 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5432 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5433 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5435 | if (key_type_is_raw_bytes(type)) { |
| 5436 | status = validate_unstructured_key_bit_size(type, bits); |
| 5437 | if (status != PSA_SUCCESS) { |
| 5438 | return status; |
| 5439 | } |
| 5440 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5441 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5442 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5443 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 5444 | return PSA_ERROR_NOT_SUPPORTED; |
| 5445 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5446 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5447 | /* Accept only byte-aligned keys, for the same reasons as |
| 5448 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5449 | if (bits % 8 != 0) { |
| 5450 | return PSA_ERROR_NOT_SUPPORTED; |
| 5451 | } |
| 5452 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5453 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5454 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5455 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5456 | 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] | 5457 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5458 | return PSA_SUCCESS; |
| 5459 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5460 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5461 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5462 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5465 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5466 | } |
| 5467 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5468 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5469 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5470 | 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] | 5471 | { |
| 5472 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5473 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5474 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5475 | if ((attributes->domain_parameters == NULL) && |
| 5476 | (attributes->domain_parameters_size != 0)) { |
| 5477 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5478 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5480 | if (key_type_is_raw_bytes(type)) { |
| 5481 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 5482 | if (status != PSA_SUCCESS) { |
| 5483 | return status; |
| 5484 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5485 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5486 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5487 | if (type == PSA_KEY_TYPE_DES) { |
| 5488 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 5489 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5490 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5491 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5492 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5493 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5494 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5495 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 5496 | return mbedtls_psa_rsa_generate_key(attributes, |
| 5497 | key_buffer, |
| 5498 | key_buffer_size, |
| 5499 | key_buffer_length); |
| 5500 | } else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5501 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5502 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5503 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5504 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5505 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5506 | return mbedtls_psa_ecp_generate_key(attributes, |
| 5507 | key_buffer, |
| 5508 | key_buffer_size, |
| 5509 | key_buffer_length); |
| 5510 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5511 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5512 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5513 | (void) key_buffer_length; |
| 5514 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5515 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5517 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5518 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5520 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 5521 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5522 | { |
| 5523 | psa_status_t status; |
| 5524 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5525 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5526 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5527 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5528 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5529 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5530 | /* Reject any attempt to create a zero-length key so that we don't |
| 5531 | * 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] | 5532 | if (psa_get_key_bits(attributes) == 0) { |
| 5533 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5534 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5535 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5536 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5537 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 5538 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5539 | } |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5541 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 5542 | &slot, &driver); |
| 5543 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5544 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5545 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5546 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5547 | /* In the case of a transparent key or an opaque key stored in local |
| 5548 | * storage (thus not in the case of generating a key in a secure element |
| 5549 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5550 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5551 | if (slot->key.data == NULL) { |
| 5552 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 5553 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5554 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5555 | attributes->core.type, attributes->core.bits); |
| 5556 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5557 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5558 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5559 | |
| 5560 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5561 | attributes->core.type, |
| 5562 | attributes->core.bits); |
| 5563 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5564 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5565 | attributes, &key_buffer_size); |
| 5566 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5567 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5568 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5569 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5571 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 5572 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5573 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5574 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5575 | } |
| 5576 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5577 | status = psa_driver_wrapper_generate_key(attributes, |
| 5578 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5579 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5580 | if (status != PSA_SUCCESS) { |
| 5581 | psa_remove_key_data_from_memory(slot); |
| 5582 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5583 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5584 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5585 | if (status == PSA_SUCCESS) { |
| 5586 | status = psa_finish_key_creation(slot, driver, key); |
| 5587 | } |
| 5588 | if (status != PSA_SUCCESS) { |
| 5589 | psa_fail_key_creation(slot, driver); |
| 5590 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5592 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5593 | } |
| 5594 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5595 | /****************************************************************/ |
| 5596 | /* Module setup */ |
| 5597 | /****************************************************************/ |
| 5598 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5599 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5600 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5601 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 5602 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5603 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5604 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5605 | return PSA_ERROR_BAD_STATE; |
| 5606 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5607 | global_data.rng.entropy_init = entropy_init; |
| 5608 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5609 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5610 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5611 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5613 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5614 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5615 | psa_wipe_all_key_slots(); |
| 5616 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5617 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5618 | } |
| 5619 | /* Wipe all remaining data, including configuration. |
| 5620 | * In particular, this sets all state indicator to the value |
| 5621 | * indicating "uninitialized". */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5622 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5623 | |
| 5624 | /* Terminate drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5625 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5628 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5629 | /** Recover a transaction that was interrupted by a power failure. |
| 5630 | * |
| 5631 | * This function is called during initialization, before psa_crypto_init() |
| 5632 | * returns. If this function returns a failure status, the initialization |
| 5633 | * fails. |
| 5634 | */ |
| 5635 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5636 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5637 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5638 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5639 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5640 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5641 | /* TODO - fall through to the failure case until this |
| 5642 | * is implemented. |
| 5643 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5644 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5645 | default: |
| 5646 | /* We found an unsupported transaction in the storage. |
| 5647 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5648 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5649 | } |
| 5650 | } |
| 5651 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5652 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5653 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5654 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5655 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5657 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5658 | if (global_data.initialized != 0) { |
| 5659 | return PSA_SUCCESS; |
| 5660 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5661 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5662 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5663 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5664 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5665 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 5666 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5667 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5668 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5669 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5671 | status = psa_initialize_key_slots(); |
| 5672 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5673 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5674 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5675 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5676 | /* Init drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5677 | status = psa_driver_wrapper_init(); |
| 5678 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5679 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5680 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5681 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5682 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5683 | status = psa_crypto_load_transaction(); |
| 5684 | if (status == PSA_SUCCESS) { |
| 5685 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 5686 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5687 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5688 | } |
| 5689 | status = psa_crypto_stop_transaction(); |
| 5690 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5691 | /* There's no transaction to complete. It's all good. */ |
| 5692 | status = PSA_SUCCESS; |
| 5693 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5694 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5695 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5696 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5697 | global_data.initialized = 1; |
| 5698 | |
| 5699 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5700 | if (status != PSA_SUCCESS) { |
| 5701 | mbedtls_psa_crypto_free(); |
| 5702 | } |
| 5703 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5704 | } |
| 5705 | |
David Horstmann | 114d824 | 2023-12-07 18:39:17 +0000 | [diff] [blame] | 5706 | /* Memory copying test hooks. These are called before input copy, after input |
| 5707 | * copy, before output copy and after output copy, respectively. |
| 5708 | * They are used by memory-poisoning tests to temporarily unpoison buffers |
| 5709 | * while they are copied. */ |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5710 | #if defined(MBEDTLS_TEST_HOOKS) |
| 5711 | void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5712 | void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL; |
| 5713 | void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5714 | void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL; |
| 5715 | #endif |
| 5716 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5717 | /** Copy from an input buffer to a local copy. |
| 5718 | * |
| 5719 | * \param[in] input Pointer to input buffer. |
| 5720 | * \param[in] input_len Length of the input buffer. |
| 5721 | * \param[out] input_copy Pointer to a local copy in which to store the input data. |
| 5722 | * \param[out] input_copy_len Length of the local copy buffer. |
| 5723 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 5724 | * copied. |
| 5725 | * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local |
| 5726 | * copy is too small to hold contents of the |
| 5727 | * input buffer. |
| 5728 | */ |
| 5729 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5730 | psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len, |
| 5731 | uint8_t *input_copy, size_t input_copy_len) |
| 5732 | { |
| 5733 | if (input_len > input_copy_len) { |
David Horstmann | 8684ad5 | 2023-11-03 19:51:40 +0000 | [diff] [blame] | 5734 | return PSA_ERROR_CORRUPTION_DETECTED; |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5737 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5738 | if (psa_input_pre_copy_hook != NULL) { |
| 5739 | psa_input_pre_copy_hook(input, input_len); |
| 5740 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5741 | #endif |
| 5742 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5743 | if (input_len > 0) { |
| 5744 | memcpy(input_copy, input, input_len); |
| 5745 | } |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5746 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5747 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5748 | if (psa_input_post_copy_hook != NULL) { |
| 5749 | psa_input_post_copy_hook(input, input_len); |
| 5750 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5751 | #endif |
| 5752 | |
David Horstmann | 957f980 | 2023-10-30 20:29:43 +0000 | [diff] [blame] | 5753 | return PSA_SUCCESS; |
| 5754 | } |
| 5755 | |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5756 | /** Copy from a local output buffer into a user-supplied one. |
| 5757 | * |
| 5758 | * \param[in] output_copy Pointer to a local buffer containing the output. |
| 5759 | * \param[in] output_copy_len Length of the local buffer. |
| 5760 | * \param[out] output Pointer to user-supplied output buffer. |
| 5761 | * \param[out] output_len Length of the user-supplied output buffer. |
| 5762 | * \return #PSA_SUCCESS, if the buffer was successfully |
| 5763 | * copied. |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 5764 | * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the |
David Horstmann | df49355 | 2023-11-15 15:18:30 +0000 | [diff] [blame] | 5765 | * user-supplied output buffer is too small to |
| 5766 | * hold the contents of the local buffer. |
| 5767 | */ |
| 5768 | MBEDTLS_STATIC_TESTABLE |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 5769 | psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len, |
| 5770 | uint8_t *output, size_t output_len) |
| 5771 | { |
| 5772 | if (output_len < output_copy_len) { |
David Horstmann | 232ab25 | 2023-11-20 12:39:38 +0000 | [diff] [blame] | 5773 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 5774 | } |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5775 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5776 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5777 | if (psa_output_pre_copy_hook != NULL) { |
| 5778 | psa_output_pre_copy_hook(output, output_len); |
| 5779 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5780 | #endif |
| 5781 | |
David Horstmann | 660027f | 2023-11-15 17:33:47 +0000 | [diff] [blame] | 5782 | if (output_copy_len > 0) { |
| 5783 | memcpy(output, output_copy, output_copy_len); |
| 5784 | } |
| 5785 | |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5786 | #if defined(MBEDTLS_TEST_HOOKS) |
David Horstmann | 63f82f7 | 2023-11-28 15:21:56 +0000 | [diff] [blame] | 5787 | if (psa_output_post_copy_hook != NULL) { |
| 5788 | psa_output_post_copy_hook(output, output_len); |
| 5789 | } |
David Horstmann | 0760b15 | 2023-11-24 16:21:04 +0000 | [diff] [blame] | 5790 | #endif |
| 5791 | |
David Horstmann | 2bd296e | 2023-10-30 20:37:12 +0000 | [diff] [blame] | 5792 | return PSA_SUCCESS; |
| 5793 | } |
| 5794 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5795 | psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, |
| 5796 | psa_crypto_local_input_t *local_input) |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5797 | { |
| 5798 | psa_status_t status; |
| 5799 | |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 5800 | *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5801 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 5802 | if (input_len == 0) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5803 | return PSA_SUCCESS; |
| 5804 | } |
| 5805 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5806 | local_input->buffer = mbedtls_calloc(input_len, 1); |
| 5807 | if (local_input->buffer == NULL) { |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5808 | /* Since we dealt with the zero-length case above, we know that |
| 5809 | * a NULL return value means a failure of allocation. */ |
| 5810 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5811 | } |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5812 | /* From now on, we must free local_input->buffer on error. */ |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5813 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5814 | local_input->length = input_len; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5815 | |
| 5816 | status = psa_crypto_copy_input(input, input_len, |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5817 | local_input->buffer, local_input->length); |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5818 | if (status != PSA_SUCCESS) { |
| 5819 | goto error; |
| 5820 | } |
| 5821 | |
| 5822 | return PSA_SUCCESS; |
| 5823 | |
| 5824 | error: |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5825 | mbedtls_free(local_input->buffer); |
| 5826 | local_input->buffer = NULL; |
| 5827 | local_input->length = 0; |
David Horstmann | 8d9d4fe | 2023-11-07 16:36:48 +0000 | [diff] [blame] | 5828 | return status; |
| 5829 | } |
| 5830 | |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5831 | 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] | 5832 | { |
David Horstmann | 81a0d57 | 2023-11-20 17:15:32 +0000 | [diff] [blame] | 5833 | mbedtls_free(local_input->buffer); |
| 5834 | local_input->buffer = NULL; |
| 5835 | local_input->length = 0; |
David Horstmann | fa2d75d | 2023-11-07 18:00:41 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5838 | psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, |
| 5839 | psa_crypto_local_output_t *local_output) |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 5840 | { |
David Horstmann | 0d52c71 | 2023-11-23 15:50:37 +0000 | [diff] [blame] | 5841 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 5842 | |
David Horstmann | 726bf05 | 2023-11-15 18:11:26 +0000 | [diff] [blame] | 5843 | if (output_len == 0) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 5844 | return PSA_SUCCESS; |
| 5845 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5846 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 5847 | if (local_output->buffer == NULL) { |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 5848 | /* Since we dealt with the zero-length case above, we know that |
| 5849 | * a NULL return value means a failure of allocation. */ |
| 5850 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5851 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5852 | local_output->length = output_len; |
| 5853 | local_output->original = output; |
David Horstmann | 953cd5b | 2023-11-08 15:19:43 +0000 | [diff] [blame] | 5854 | |
| 5855 | return PSA_SUCCESS; |
| 5856 | } |
| 5857 | |
Gabor Mezei | 3b0c371 | 2024-01-24 13:07:17 +0100 | [diff] [blame] | 5858 | psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len, |
| 5859 | psa_crypto_local_output_t *local_output) |
| 5860 | { |
| 5861 | psa_status_t status; |
| 5862 | *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; |
| 5863 | |
| 5864 | if (output_len == 0) { |
| 5865 | return PSA_SUCCESS; |
| 5866 | } |
| 5867 | local_output->buffer = mbedtls_calloc(output_len, 1); |
| 5868 | if (local_output->buffer == NULL) { |
| 5869 | /* Since we dealt with the zero-length case above, we know that |
| 5870 | * a NULL return value means a failure of allocation. */ |
| 5871 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5872 | } |
| 5873 | local_output->length = output_len; |
| 5874 | local_output->original = output; |
| 5875 | |
| 5876 | status = psa_crypto_copy_input(output, output_len, |
| 5877 | local_output->buffer, local_output->length); |
| 5878 | if (status != PSA_SUCCESS) { |
| 5879 | goto error; |
| 5880 | } |
| 5881 | |
| 5882 | return PSA_SUCCESS; |
| 5883 | |
| 5884 | error: |
| 5885 | mbedtls_free(local_output->buffer); |
| 5886 | local_output->buffer = NULL; |
| 5887 | local_output->length = 0; |
| 5888 | return status; |
| 5889 | } |
| 5890 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5891 | 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] | 5892 | { |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 5893 | psa_status_t status; |
| 5894 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5895 | if (local_output->buffer == NULL) { |
| 5896 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 5897 | return PSA_SUCCESS; |
| 5898 | } |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5899 | if (local_output->original == NULL) { |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 5900 | /* We have an internal copy but nothing to copy back to. */ |
| 5901 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5902 | } |
| 5903 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5904 | status = psa_crypto_copy_output(local_output->buffer, local_output->length, |
| 5905 | local_output->original, local_output->length); |
David Horstmann | 761761f | 2023-11-15 15:57:32 +0000 | [diff] [blame] | 5906 | if (status != PSA_SUCCESS) { |
| 5907 | return status; |
| 5908 | } |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 5909 | |
David Horstmann | 1a76ab1 | 2023-11-20 12:54:09 +0000 | [diff] [blame] | 5910 | mbedtls_free(local_output->buffer); |
| 5911 | local_output->buffer = NULL; |
| 5912 | local_output->length = 0; |
David Horstmann | aeeb274 | 2023-11-08 17:44:18 +0000 | [diff] [blame] | 5913 | |
| 5914 | return PSA_SUCCESS; |
| 5915 | } |
| 5916 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5917 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |