Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 1 | /** Code to exercise a PSA key object, i.e. validate that it seems well-formed |
| 2 | * and can do what it is supposed to do. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <test/helpers.h> |
| 11 | #include <test/macros.h> |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 12 | #include <test/psa_exercise_key.h> |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 13 | |
David Horstmann | a7d2afe | 2024-10-23 16:55:20 +0100 | [diff] [blame] | 14 | #if (MBEDTLS_VERSION_MAJOR < 4 && defined(MBEDTLS_PSA_CRYPTO_C)) || \ |
| 15 | (MBEDTLS_VERSION_MAJOR >= 4 && defined(MBEDTLS_PSA_CRYPTO_CLIENT)) |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 16 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include <mbedtls/asn1.h> |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 18 | #include <psa/crypto.h> |
| 19 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 20 | #include <test/asn1_helpers.h> |
Przemyslaw Stekiel | 53de262 | 2021-11-03 09:35:35 +0100 | [diff] [blame] | 21 | #include <psa_crypto_slot_management.h> |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 22 | #include <test/psa_crypto_helpers.h> |
| 23 | |
Gilles Peskine | 6fe8a06 | 2024-02-15 17:21:17 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_PK_C) |
| 25 | #include <pk_internal.h> |
| 26 | #endif |
| 27 | #if defined(MBEDTLS_ECP_C) |
| 28 | #include <mbedtls/ecp.h> |
| 29 | #endif |
| 30 | #if defined(MBEDTLS_RSA_C) |
| 31 | #include <rsa_internal.h> |
| 32 | #endif |
| 33 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | static int lifetime_is_dynamic_secure_element(psa_key_lifetime_t lifetime) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 36 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | return PSA_KEY_LIFETIME_GET_LOCATION(lifetime) != |
| 38 | PSA_KEY_LOCATION_LOCAL_STORAGE; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
Ryan Everett | f08a93f | 2024-03-12 16:00:08 +0000 | [diff] [blame] | 42 | static int check_key_attributes_sanity(mbedtls_svc_key_id_t key, |
| 43 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 44 | { |
| 45 | int ok = 0; |
| 46 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 47 | psa_key_lifetime_t lifetime; |
| 48 | mbedtls_svc_key_id_t id; |
| 49 | psa_key_type_t type; |
Gilles Peskine | 6b362e6 | 2021-02-15 12:03:16 +0100 | [diff] [blame] | 50 | size_t bits; |
Ryan Everett | f08a93f | 2024-03-12 16:00:08 +0000 | [diff] [blame] | 51 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 52 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 53 | /* The key has been destroyed. */ |
| 54 | psa_reset_key_attributes(&attributes); |
| 55 | return 1; |
| 56 | } |
| 57 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | lifetime = psa_get_key_lifetime(&attributes); |
| 59 | id = psa_get_key_id(&attributes); |
| 60 | type = psa_get_key_type(&attributes); |
| 61 | bits = psa_get_key_bits(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 62 | |
| 63 | /* Persistence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 65 | TEST_ASSERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | (PSA_KEY_ID_VOLATILE_MIN <= |
| 67 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) && |
| 68 | (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) <= |
| 69 | PSA_KEY_ID_VOLATILE_MAX)); |
| 70 | } else { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 71 | TEST_ASSERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | (PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) && |
| 73 | (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) <= PSA_KEY_ID_USER_MAX)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 74 | } |
| 75 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ryan Everett | f08a93f | 2024-03-12 16:00:08 +0000 | [diff] [blame] | 76 | /* MBEDTLS_PSA_CRYPTO_SE_C does not support thread safety. */ |
| 77 | if (key_destroyable == 0) { |
| 78 | /* randomly-generated 64-bit constant, should never appear in test data */ |
| 79 | psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21; |
| 80 | status = psa_get_key_slot_number(&attributes, &slot_number); |
| 81 | if (lifetime_is_dynamic_secure_element(lifetime)) { |
| 82 | /* Mbed TLS currently always exposes the slot number to |
| 83 | * applications. This is not mandated by the PSA specification |
| 84 | * and may change in future versions. */ |
| 85 | TEST_EQUAL(status, 0); |
| 86 | TEST_ASSERT(slot_number != 0xec94d4a5058a1a21); |
| 87 | } else { |
| 88 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
| 89 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 90 | } |
| 91 | #endif |
| 92 | |
| 93 | /* Type and size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | TEST_ASSERT(type != 0); |
| 95 | TEST_ASSERT(bits != 0); |
| 96 | TEST_ASSERT(bits <= PSA_MAX_KEY_BITS); |
| 97 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) { |
| 98 | TEST_ASSERT(bits % 8 == 0); |
| 99 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 100 | |
| 101 | /* MAX macros concerning specific key types */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 103 | TEST_ASSERT(bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS); |
| 104 | } else if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 105 | TEST_ASSERT(bits <= PSA_VENDOR_RSA_MAX_KEY_BITS); |
| 106 | } |
| 107 | TEST_ASSERT(PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 108 | |
| 109 | ok = 1; |
| 110 | |
| 111 | exit: |
| 112 | /* |
| 113 | * Key attributes may have been returned by psa_get_key_attributes() |
| 114 | * thus reset them as required. |
| 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | static int exercise_mac_key(mbedtls_svc_key_id_t key, |
| 122 | psa_key_usage_t usage, |
Ryan Everett | 7763550 | 2024-03-12 16:02:23 +0000 | [diff] [blame] | 123 | psa_algorithm_t alg, |
| 124 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 125 | { |
| 126 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 127 | const unsigned char input[] = "foo"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | unsigned char mac[PSA_MAC_MAX_SIZE] = { 0 }; |
| 129 | size_t mac_length = sizeof(mac); |
Ryan Everett | 7763550 | 2024-03-12 16:02:23 +0000 | [diff] [blame] | 130 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 131 | /* Convert wildcard algorithm to exercisable algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | if (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) { |
| 133 | alg = PSA_ALG_TRUNCATED_MAC(alg, PSA_MAC_TRUNCATED_LENGTH(alg)); |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | if (usage & PSA_KEY_USAGE_SIGN_HASH) { |
Ryan Everett | 7763550 | 2024-03-12 16:02:23 +0000 | [diff] [blame] | 137 | status = psa_mac_sign_setup(&operation, key, alg); |
| 138 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 139 | /* The key has been destroyed. */ |
| 140 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 141 | return 1; |
| 142 | } |
| 143 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | PSA_ASSERT(psa_mac_update(&operation, |
| 145 | input, sizeof(input))); |
| 146 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 147 | mac, sizeof(mac), |
| 148 | &mac_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | if (usage & PSA_KEY_USAGE_VERIFY_HASH) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 152 | psa_status_t verify_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | (usage & PSA_KEY_USAGE_SIGN_HASH ? |
| 154 | PSA_SUCCESS : |
| 155 | PSA_ERROR_INVALID_SIGNATURE); |
Ryan Everett | 7763550 | 2024-03-12 16:02:23 +0000 | [diff] [blame] | 156 | status = psa_mac_verify_setup(&operation, key, alg); |
| 157 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 158 | /* The key has been destroyed. */ |
| 159 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 160 | return 1; |
| 161 | } |
| 162 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | PSA_ASSERT(psa_mac_update(&operation, |
| 164 | input, sizeof(input))); |
| 165 | TEST_EQUAL(psa_mac_verify_finish(&operation, mac, mac_length), |
| 166 | verify_status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 170 | |
| 171 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | psa_mac_abort(&operation); |
| 173 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | static int exercise_cipher_key(mbedtls_svc_key_id_t key, |
| 177 | psa_key_usage_t usage, |
Ryan Everett | 70691f3 | 2024-03-12 16:04:45 +0000 | [diff] [blame] | 178 | psa_algorithm_t alg, |
| 179 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 180 | { |
| 181 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Gilles Peskine | 5eef11a | 2022-04-21 11:14:30 +0200 | [diff] [blame] | 183 | size_t iv_length; |
Gilles Peskine | bbf452c | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 185 | psa_key_type_t key_type; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 186 | const unsigned char plaintext[16] = "Hello, world..."; |
| 187 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | size_t ciphertext_length = sizeof(ciphertext); |
| 189 | unsigned char decrypted[sizeof(ciphertext)]; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 190 | size_t part_length; |
Ryan Everett | 70691f3 | 2024-03-12 16:04:45 +0000 | [diff] [blame] | 191 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 194 | key_type = psa_get_key_type(&attributes); |
| 195 | iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Gilles Peskine | bbf452c | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
Ryan Everett | 70691f3 | 2024-03-12 16:04:45 +0000 | [diff] [blame] | 198 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 199 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 200 | /* The key has been destroyed. */ |
| 201 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 202 | return 1; |
| 203 | } |
| 204 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | if (iv_length != 0) { |
| 206 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 207 | iv, sizeof(iv), |
| 208 | &iv_length)); |
Gilles Peskine | bbf452c | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 209 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | PSA_ASSERT(psa_cipher_update(&operation, |
| 211 | plaintext, sizeof(plaintext), |
| 212 | ciphertext, sizeof(ciphertext), |
| 213 | &ciphertext_length)); |
| 214 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 215 | ciphertext + ciphertext_length, |
| 216 | sizeof(ciphertext) - ciphertext_length, |
| 217 | &part_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 218 | ciphertext_length += part_length; |
| 219 | } |
| 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 222 | int maybe_invalid_padding = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | if (!(usage & PSA_KEY_USAGE_ENCRYPT)) { |
| 224 | maybe_invalid_padding = !PSA_ALG_IS_STREAM_CIPHER(alg); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 225 | } |
Ryan Everett | 70691f3 | 2024-03-12 16:04:45 +0000 | [diff] [blame] | 226 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 227 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 228 | /* The key has been destroyed. */ |
| 229 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 230 | return 1; |
| 231 | } |
| 232 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | if (iv_length != 0) { |
| 234 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 235 | iv, iv_length)); |
Gilles Peskine | bbf452c | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 236 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | PSA_ASSERT(psa_cipher_update(&operation, |
| 238 | ciphertext, ciphertext_length, |
| 239 | decrypted, sizeof(decrypted), |
| 240 | &part_length)); |
| 241 | status = psa_cipher_finish(&operation, |
| 242 | decrypted + part_length, |
| 243 | sizeof(decrypted) - part_length, |
| 244 | &part_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 245 | /* For a stream cipher, all inputs are valid. For a block cipher, |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 246 | * if the input is some arbitrary data rather than an actual |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | ciphertext, a padding error is likely. */ |
| 248 | if (maybe_invalid_padding) { |
| 249 | TEST_ASSERT(status == PSA_SUCCESS || |
| 250 | status == PSA_ERROR_INVALID_PADDING); |
| 251 | } else { |
| 252 | PSA_ASSERT(status); |
| 253 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 257 | |
| 258 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | psa_cipher_abort(&operation); |
| 260 | psa_reset_key_attributes(&attributes); |
| 261 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | static int exercise_aead_key(mbedtls_svc_key_id_t key, |
| 265 | psa_key_usage_t usage, |
Ryan Everett | fbe703d | 2024-03-12 16:09:25 +0000 | [diff] [blame] | 266 | psa_algorithm_t alg, |
| 267 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 268 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | unsigned char nonce[PSA_AEAD_NONCE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 7acb198 | 2022-03-19 11:03:32 +0100 | [diff] [blame] | 270 | size_t nonce_length; |
| 271 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 272 | psa_key_type_t key_type; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 273 | unsigned char plaintext[16] = "Hello, world..."; |
| 274 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | size_t ciphertext_length = sizeof(ciphertext); |
| 276 | size_t plaintext_length = sizeof(ciphertext); |
Ryan Everett | fbe703d | 2024-03-12 16:09:25 +0000 | [diff] [blame] | 277 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 278 | |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 279 | /* Convert wildcard algorithm to exercisable algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | if (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) { |
| 281 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, PSA_ALG_AEAD_GET_TAG_LENGTH(alg)); |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 285 | key_type = psa_get_key_type(&attributes); |
| 286 | nonce_length = PSA_AEAD_NONCE_LENGTH(key_type, alg); |
Steven Cooreman | aaec341 | 2021-02-18 13:30:34 +0100 | [diff] [blame] | 287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
Ryan Everett | fbe703d | 2024-03-12 16:09:25 +0000 | [diff] [blame] | 289 | status = psa_aead_encrypt(key, alg, |
| 290 | nonce, nonce_length, |
| 291 | NULL, 0, |
| 292 | plaintext, sizeof(plaintext), |
| 293 | ciphertext, sizeof(ciphertext), |
| 294 | &ciphertext_length); |
| 295 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 296 | /* The key has been destroyed. */ |
| 297 | return 1; |
| 298 | } |
| 299 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 303 | psa_status_t verify_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | (usage & PSA_KEY_USAGE_ENCRYPT ? |
| 305 | PSA_SUCCESS : |
| 306 | PSA_ERROR_INVALID_SIGNATURE); |
Ryan Everett | fbe703d | 2024-03-12 16:09:25 +0000 | [diff] [blame] | 307 | status = psa_aead_decrypt(key, alg, |
| 308 | nonce, nonce_length, |
| 309 | NULL, 0, |
| 310 | ciphertext, ciphertext_length, |
| 311 | plaintext, sizeof(plaintext), |
| 312 | &plaintext_length); |
| 313 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 314 | /* The key has been destroyed. */ |
| 315 | return 1; |
| 316 | } |
| 317 | TEST_ASSERT(status == verify_status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 321 | |
| 322 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | psa_reset_key_attributes(&attributes); |
| 324 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | static int can_sign_or_verify_message(psa_key_usage_t usage, |
| 328 | psa_algorithm_t alg) |
Gilles Peskine | d586b82 | 2022-03-19 11:15:41 +0100 | [diff] [blame] | 329 | { |
| 330 | /* Sign-the-unspecified-hash algorithms can only be used with |
| 331 | * {sign,verify}_hash, not with {sign,verify}_message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | if (alg == PSA_ALG_ECDSA_ANY || alg == PSA_ALG_RSA_PKCS1V15_SIGN_RAW) { |
| 333 | return 0; |
| 334 | } |
| 335 | return usage & (PSA_KEY_USAGE_SIGN_MESSAGE | |
| 336 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
Gilles Peskine | d586b82 | 2022-03-19 11:15:41 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | static int exercise_signature_key(mbedtls_svc_key_id_t key, |
| 340 | psa_key_usage_t usage, |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 341 | psa_algorithm_t alg, |
| 342 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 343 | { |
Gilles Peskine | 4781bd9 | 2024-02-09 17:32:45 +0100 | [diff] [blame] | 344 | /* If the policy allows signing with any hash, just pick one. */ |
| 345 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 346 | if (PSA_ALG_IS_SIGN_HASH(alg) && hash_alg == PSA_ALG_ANY_HASH && |
| 347 | usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | |
| 348 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE)) { |
| 349 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 350 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 351 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
| 352 | #else |
| 353 | TEST_FAIL("No hash algorithm for hash-and-sign testing"); |
| 354 | #endif |
| 355 | } |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 356 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 4781bd9 | 2024-02-09 17:32:45 +0100 | [diff] [blame] | 357 | |
oberon-sk | f7a824b | 2023-02-15 19:43:30 +0100 | [diff] [blame] | 358 | if (usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH) && |
| 359 | PSA_ALG_IS_SIGN_HASH(alg)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 361 | size_t payload_length = 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
| 363 | size_t signature_length = sizeof(signature); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 364 | |
Janos Follath | 4c0b60e | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 365 | /* Some algorithms require the payload to have the size of |
| 366 | * the hash encoded in the algorithm. Use this input size |
| 367 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (hash_alg != 0) { |
| 369 | payload_length = PSA_HASH_LENGTH(hash_alg); |
| 370 | } |
Janos Follath | 4c0b60e | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (usage & PSA_KEY_USAGE_SIGN_HASH) { |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 373 | status = psa_sign_hash(key, alg, |
| 374 | payload, payload_length, |
| 375 | signature, sizeof(signature), |
| 376 | &signature_length); |
| 377 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 378 | /* The key has been destroyed. */ |
| 379 | return 1; |
| 380 | } |
| 381 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (usage & PSA_KEY_USAGE_VERIFY_HASH) { |
| 385 | psa_status_t verify_status = |
| 386 | (usage & PSA_KEY_USAGE_SIGN_HASH ? |
| 387 | PSA_SUCCESS : |
| 388 | PSA_ERROR_INVALID_SIGNATURE); |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 389 | status = psa_verify_hash(key, alg, |
| 390 | payload, payload_length, |
| 391 | signature, signature_length); |
| 392 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 393 | /* The key has been destroyed. */ |
| 394 | return 1; |
| 395 | } |
| 396 | TEST_ASSERT(status == verify_status); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 397 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | if (can_sign_or_verify_message(usage, alg)) { |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 401 | unsigned char message[256] = "Hello, world..."; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 403 | size_t message_length = 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | size_t signature_length = sizeof(signature); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | if (usage & PSA_KEY_USAGE_SIGN_MESSAGE) { |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 407 | status = psa_sign_message(key, alg, |
| 408 | message, message_length, |
| 409 | signature, sizeof(signature), |
| 410 | &signature_length); |
| 411 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 412 | /* The key has been destroyed. */ |
| 413 | return 1; |
| 414 | } |
| 415 | PSA_ASSERT(status); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | if (usage & PSA_KEY_USAGE_VERIFY_MESSAGE) { |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 419 | psa_status_t verify_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | (usage & PSA_KEY_USAGE_SIGN_MESSAGE ? |
| 421 | PSA_SUCCESS : |
| 422 | PSA_ERROR_INVALID_SIGNATURE); |
Ryan Everett | 6edd408 | 2024-03-12 16:11:01 +0000 | [diff] [blame] | 423 | status = psa_verify_message(key, alg, |
| 424 | message, message_length, |
| 425 | signature, signature_length); |
| 426 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 427 | /* The key has been destroyed. */ |
| 428 | return 1; |
| 429 | } |
| 430 | TEST_ASSERT(status == verify_status); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 431 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 435 | |
| 436 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | static int exercise_asymmetric_encryption_key(mbedtls_svc_key_id_t key, |
| 441 | psa_key_usage_t usage, |
Ryan Everett | d48fc10 | 2024-03-12 16:12:41 +0000 | [diff] [blame] | 442 | psa_algorithm_t alg, |
| 443 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 444 | { |
Gilles Peskine | f50cd59 | 2024-02-15 13:13:26 +0100 | [diff] [blame] | 445 | unsigned char plaintext[PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE] = |
Gilles Peskine | fdb809e | 2024-02-09 19:22:30 +0100 | [diff] [blame] | 446 | "Hello, world..."; |
Gilles Peskine | f50cd59 | 2024-02-15 13:13:26 +0100 | [diff] [blame] | 447 | unsigned char ciphertext[PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE] = |
Gilles Peskine | fdb809e | 2024-02-09 19:22:30 +0100 | [diff] [blame] | 448 | "(wabblewebblewibblewobblewubble)"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | size_t ciphertext_length = sizeof(ciphertext); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 450 | size_t plaintext_length = 16; |
Ryan Everett | d48fc10 | 2024-03-12 16:12:41 +0000 | [diff] [blame] | 451 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
Ryan Everett | d48fc10 | 2024-03-12 16:12:41 +0000 | [diff] [blame] | 453 | status = psa_asymmetric_encrypt(key, alg, |
| 454 | plaintext, plaintext_length, |
| 455 | NULL, 0, |
| 456 | ciphertext, sizeof(ciphertext), |
| 457 | &ciphertext_length); |
| 458 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 459 | /* The key has been destroyed. */ |
| 460 | return 1; |
| 461 | } |
| 462 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Ryan Everett | d48fc10 | 2024-03-12 16:12:41 +0000 | [diff] [blame] | 466 | status = psa_asymmetric_decrypt(key, alg, |
| 467 | ciphertext, ciphertext_length, |
| 468 | NULL, 0, |
| 469 | plaintext, sizeof(plaintext), |
| 470 | &plaintext_length); |
| 471 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 472 | /* The key has been destroyed. */ |
| 473 | return 1; |
| 474 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | TEST_ASSERT(status == PSA_SUCCESS || |
| 476 | ((usage & PSA_KEY_USAGE_ENCRYPT) == 0 && |
| 477 | (status == PSA_ERROR_INVALID_ARGUMENT || |
| 478 | status == PSA_ERROR_INVALID_PADDING))); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 482 | |
| 483 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | int mbedtls_test_psa_setup_key_derivation_wrap( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 489 | mbedtls_svc_key_id_t key, |
| 490 | psa_algorithm_t alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | const unsigned char *input1, size_t input1_length, |
| 492 | const unsigned char *input2, size_t input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 493 | size_t capacity, int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 494 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | PSA_ASSERT(psa_key_derivation_setup(operation, alg)); |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 496 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | if (PSA_ALG_IS_HKDF(alg)) { |
| 498 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 499 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 500 | input1, input1_length)); |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 501 | status = psa_key_derivation_input_key(operation, |
| 502 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 503 | key); |
| 504 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 505 | /* The key has been destroyed. */ |
| 506 | return 1; |
| 507 | } |
| 508 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 510 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 511 | input2, |
| 512 | input2_length)); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 513 | } else if (PSA_ALG_IS_HKDF_EXTRACT(alg)) { |
| 514 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 515 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 516 | input1, input1_length)); |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 517 | status = psa_key_derivation_input_key(operation, |
| 518 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 519 | key); |
| 520 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 521 | /* The key has been destroyed. */ |
| 522 | return 1; |
| 523 | } |
| 524 | PSA_ASSERT(status); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 525 | } else if (PSA_ALG_IS_HKDF_EXPAND(alg)) { |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 526 | status = psa_key_derivation_input_key(operation, |
| 527 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 528 | key); |
| 529 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 530 | /* The key has been destroyed. */ |
| 531 | return 1; |
| 532 | } |
| 533 | PSA_ASSERT(status); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 534 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 535 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 536 | input2, |
| 537 | input2_length)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | } else if (PSA_ALG_IS_TLS12_PRF(alg) || |
| 539 | PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 540 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 541 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 542 | input1, input1_length)); |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 543 | status = psa_key_derivation_input_key(operation, |
| 544 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 545 | key); |
| 546 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 547 | /* The key has been destroyed. */ |
| 548 | return 1; |
| 549 | } |
| 550 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 552 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 553 | input2, input2_length)); |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 554 | } else if (PSA_ALG_IS_PBKDF2(alg)) { |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 555 | PSA_ASSERT(psa_key_derivation_input_integer(operation, |
| 556 | PSA_KEY_DERIVATION_INPUT_COST, |
Kusumit Ghoderao | 94d3190 | 2023-09-05 19:30:22 +0530 | [diff] [blame] | 557 | 1U)); |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 558 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 559 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 560 | input2, |
| 561 | input2_length)); |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 562 | status = psa_key_derivation_input_key(operation, |
| 563 | PSA_KEY_DERIVATION_INPUT_PASSWORD, |
| 564 | key); |
| 565 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 566 | /* The key has been destroyed. */ |
| 567 | return 1; |
| 568 | } |
| 569 | PSA_ASSERT(status); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 570 | } else if (alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 571 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 572 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 573 | input1, input1_length)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | } else { |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 575 | TEST_FAIL("Key derivation algorithm not supported"); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | if (capacity != SIZE_MAX) { |
| 579 | PSA_ASSERT(psa_key_derivation_set_capacity(operation, capacity)); |
| 580 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 583 | |
| 584 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | static int exercise_key_derivation_key(mbedtls_svc_key_id_t key, |
| 590 | psa_key_usage_t usage, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 591 | psa_algorithm_t alg, |
| 592 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 593 | { |
| 594 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 595 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | size_t input1_length = sizeof(input1); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 597 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | size_t input2_length = sizeof(input2); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 599 | unsigned char output[1]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 600 | size_t capacity = sizeof(output); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | if (usage & PSA_KEY_USAGE_DERIVE) { |
| 603 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 604 | input1, input1_length, |
| 605 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 606 | capacity, key_destroyable)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 607 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 609 | |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 610 | psa_status_t status = psa_key_derivation_output_bytes(&operation, |
| 611 | output, |
| 612 | capacity); |
| 613 | if (key_destroyable && status == PSA_ERROR_BAD_STATE) { |
| 614 | /* The key has been destroyed. */ |
| 615 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 616 | } else { |
| 617 | PSA_ASSERT(status); |
| 618 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 619 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 623 | |
| 624 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* We need two keys to exercise key agreement. Exercise the |
| 629 | * private key against its own public key. */ |
| 630 | psa_status_t mbedtls_test_psa_key_agreement_with_self( |
| 631 | psa_key_derivation_operation_t *operation, |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 632 | mbedtls_svc_key_id_t key, int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 633 | { |
| 634 | psa_key_type_t private_key_type; |
| 635 | psa_key_type_t public_key_type; |
| 636 | size_t key_bits; |
| 637 | uint8_t *public_key = NULL; |
| 638 | size_t public_key_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 639 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 640 | |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 641 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 642 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 643 | /* The key has been destroyed. */ |
| 644 | psa_reset_key_attributes(&attributes); |
| 645 | return PSA_SUCCESS; |
| 646 | } |
| 647 | PSA_ASSERT(status); |
| 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | private_key_type = psa_get_key_type(&attributes); |
| 650 | key_bits = psa_get_key_bits(&attributes); |
| 651 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 652 | public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 653 | TEST_CALLOC(public_key, public_key_length); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 654 | status = psa_export_public_key(key, public_key, public_key_length, |
| 655 | &public_key_length); |
| 656 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 657 | /* The key has been destroyed. */ |
| 658 | status = PSA_SUCCESS; |
| 659 | goto exit; |
| 660 | } |
| 661 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 662 | |
| 663 | status = psa_key_derivation_key_agreement( |
| 664 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | public_key, public_key_length); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 666 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 667 | /* The key has been destroyed. */ |
| 668 | status = PSA_SUCCESS; |
| 669 | goto exit; |
| 670 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 671 | exit: |
| 672 | /* |
| 673 | * Key attributes may have been returned by psa_get_key_attributes() |
| 674 | * thus reset them as required. |
| 675 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | mbedtls_free(public_key); |
| 679 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* We need two keys to exercise key agreement. Exercise the |
| 683 | * private key against its own public key. */ |
| 684 | psa_status_t mbedtls_test_psa_raw_key_agreement_with_self( |
| 685 | psa_algorithm_t alg, |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 686 | mbedtls_svc_key_id_t key, |
| 687 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 688 | { |
| 689 | psa_key_type_t private_key_type; |
| 690 | psa_key_type_t public_key_type; |
| 691 | size_t key_bits; |
| 692 | uint8_t *public_key = NULL; |
| 693 | size_t public_key_length; |
| 694 | uint8_t output[1024]; |
| 695 | size_t output_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 696 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 697 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 698 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 699 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 700 | /* The key has been destroyed. */ |
| 701 | psa_reset_key_attributes(&attributes); |
| 702 | return PSA_SUCCESS; |
| 703 | } |
| 704 | PSA_ASSERT(status); |
| 705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | private_key_type = psa_get_key_type(&attributes); |
| 707 | key_bits = psa_get_key_bits(&attributes); |
| 708 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 709 | public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 710 | TEST_CALLOC(public_key, public_key_length); |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 711 | status = psa_export_public_key(key, |
| 712 | public_key, public_key_length, |
| 713 | &public_key_length); |
| 714 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 715 | /* The key has been destroyed. */ |
| 716 | status = PSA_SUCCESS; |
| 717 | goto exit; |
| 718 | } |
Ryan Everett | 6de38ac | 2024-03-14 17:50:39 +0000 | [diff] [blame] | 719 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | status = psa_raw_key_agreement(alg, key, |
| 722 | public_key, public_key_length, |
| 723 | output, sizeof(output), &output_length); |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 724 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 725 | /* The key has been destroyed. */ |
| 726 | status = PSA_SUCCESS; |
| 727 | goto exit; |
| 728 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 729 | if (status == PSA_SUCCESS) { |
| 730 | TEST_ASSERT(output_length <= |
| 731 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(private_key_type, |
| 732 | key_bits)); |
| 733 | TEST_ASSERT(output_length <= |
| 734 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 737 | exit: |
| 738 | /* |
| 739 | * Key attributes may have been returned by psa_get_key_attributes() |
| 740 | * thus reset them as required. |
| 741 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | mbedtls_free(public_key); |
| 745 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key, |
| 749 | psa_key_usage_t usage, |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 750 | psa_algorithm_t alg, |
| 751 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 752 | { |
| 753 | int ok = 0; |
| 754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 756 | /* We need two keys to exercise key agreement. Exercise the |
| 757 | * private key against its own public key. */ |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 758 | PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key, |
| 759 | key_destroyable)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 760 | } |
| 761 | ok = 1; |
| 762 | |
| 763 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | static int exercise_key_agreement_key(mbedtls_svc_key_id_t key, |
| 768 | psa_key_usage_t usage, |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 769 | psa_algorithm_t alg, |
| 770 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 771 | { |
| 772 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gabor Mezei | dc3f3bb | 2022-07-01 15:06:34 +0200 | [diff] [blame] | 773 | unsigned char input[1] = { 0 }; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 774 | unsigned char output[1]; |
| 775 | int ok = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 777 | psa_status_t expected_key_agreement_status = PSA_SUCCESS; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 780 | /* We need two keys to exercise key agreement. Exercise the |
| 781 | * private key against its own public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 783 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 784 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 785 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 786 | &operation, PSA_KEY_DERIVATION_INPUT_SEED, |
| 787 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 788 | } |
| 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 791 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 792 | &operation, PSA_KEY_DERIVATION_INPUT_SALT, |
| 793 | input, sizeof(input))); |
Przemek Stekiel | d898745 | 2022-06-14 11:41:52 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 796 | /* For HKDF_EXPAND input secret may fail as secret size may not match |
| 797 | to expected PRK size. In practice it means that key bits must match |
| 798 | hash length. Otherwise test should fail with INVALID_ARGUMENT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 801 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 802 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 803 | /* The key has been destroyed. */ |
| 804 | ok = 1; |
| 805 | } |
| 806 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | size_t key_bits = psa_get_key_bits(&attributes); |
| 808 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | if (PSA_BITS_TO_BYTES(key_bits) != PSA_HASH_LENGTH(hash_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 811 | expected_key_agreement_status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | } |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 815 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(&operation, key, |
| 816 | key_destroyable), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | expected_key_agreement_status); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | if (expected_key_agreement_status != PSA_SUCCESS) { |
| 820 | return 1; |
| 821 | } |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 824 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 825 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 826 | &operation, PSA_KEY_DERIVATION_INPUT_LABEL, |
| 827 | input, sizeof(input))); |
| 828 | } else if (PSA_ALG_IS_HKDF(kdf_alg) || PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 829 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 830 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 831 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 832 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 834 | output, |
| 835 | sizeof(output))); |
| 836 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 837 | } |
| 838 | ok = 1; |
| 839 | |
| 840 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | int mbedtls_test_psa_exported_key_sanity_check( |
| 845 | psa_key_type_t type, size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | const uint8_t *exported, size_t exported_length) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 847 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) { |
| 851 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
| 852 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 853 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 854 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 856 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 857 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 858 | size_t len; |
| 859 | /* RSAPrivateKey ::= SEQUENCE { |
| 860 | * version INTEGER, -- must be 0 |
| 861 | * modulus INTEGER, -- n |
| 862 | * publicExponent INTEGER, -- e |
| 863 | * privateExponent INTEGER, -- d |
| 864 | * prime1 INTEGER, -- p |
| 865 | * prime2 INTEGER, -- q |
| 866 | * exponent1 INTEGER, -- d mod (p-1) |
| 867 | * exponent2 INTEGER, -- d mod (q-1) |
| 868 | * coefficient INTEGER, -- (inverse of q) mod p |
| 869 | * } |
| 870 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 872 | MBEDTLS_ASN1_SEQUENCE | |
| 873 | MBEDTLS_ASN1_CONSTRUCTED), 0); |
| 874 | TEST_EQUAL(len, end - p); |
| 875 | if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 876 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 877 | } |
| 878 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 879 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | } |
| 881 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 882 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 884 | /* Require d to be at least half the size of n. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 886 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 888 | /* Require p and q to be at most half the size of n, rounded up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits / 2 + 1, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 890 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | } |
| 892 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits / 2 + 1, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 893 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | } |
| 895 | if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 896 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | } |
| 898 | if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 899 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | } |
| 901 | if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | } |
| 904 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 907 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 908 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 911 | /* Just the secret value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 914 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 915 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 916 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 917 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 919 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 920 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 921 | size_t len; |
| 922 | /* RSAPublicKey ::= SEQUENCE { |
| 923 | * modulus INTEGER, -- n |
| 924 | * publicExponent INTEGER } -- e |
| 925 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 927 | MBEDTLS_ASN1_SEQUENCE | |
| 928 | MBEDTLS_ASN1_CONSTRUCTED), |
| 929 | 0); |
| 930 | TEST_EQUAL(len, end - p); |
| 931 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 932 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | } |
| 934 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 935 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | } |
| 937 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 938 | |
| 939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | TEST_ASSERT(exported_length <= |
| 941 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 942 | TEST_ASSERT(exported_length <= |
| 943 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 944 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 945 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) { |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | TEST_ASSERT(exported_length <= |
| 950 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 951 | TEST_ASSERT(exported_length <= |
| 952 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 955 | /* The representation of an ECC Montgomery public key is |
| 956 | * the raw compressed point */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length); |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 958 | } else if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_TWISTED_EDWARDS) { |
oberon-sk | 6d50173 | 2023-02-13 12:13:20 +0100 | [diff] [blame] | 959 | /* The representation of an ECC Edwards public key is |
| 960 | * the raw compressed point */ |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 961 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits + 1), exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | } else { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 963 | /* The representation of an ECC Weierstrass public key is: |
| 964 | * - The byte 0x04; |
| 965 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 966 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 967 | * - where m is the bit size associated with the curve. |
| 968 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length); |
| 970 | TEST_EQUAL(exported[0], 4); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 971 | } |
Przemek Stekiel | 7cf26df | 2022-12-01 15:09:40 +0100 | [diff] [blame] | 972 | } else |
| 973 | if (PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) || PSA_KEY_TYPE_IS_DH_KEY_PAIR(type)) { |
Przemek Stekiel | 4c0da51 | 2023-04-27 13:04:20 +0200 | [diff] [blame] | 974 | TEST_ASSERT(exported_length == |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 975 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 976 | TEST_ASSERT(exported_length <= |
| 977 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Valerio Setti | 2dbc306 | 2023-04-13 12:19:57 +0200 | [diff] [blame] | 978 | } else { |
Andrzej Kurek | 57d2f13 | 2022-01-17 15:26:24 +0100 | [diff] [blame] | 979 | (void) exported; |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 980 | TEST_FAIL("Sanity check not implemented for this key type"); |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 981 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 982 | |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 983 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | if (type == PSA_KEY_TYPE_DES) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 985 | /* Check the parity bits. */ |
| 986 | unsigned i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | for (i = 0; i < bits / 8; i++) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 988 | unsigned bit_count = 0; |
| 989 | unsigned m; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | for (m = 1; m <= 0x100; m <<= 1) { |
| 991 | if (exported[i] & m) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 992 | ++bit_count; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 994 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | TEST_ASSERT(bit_count % 2 != 0); |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 996 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 997 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 998 | #endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1001 | |
| 1002 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | static int exercise_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1007 | psa_key_usage_t usage, |
| 1008 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1009 | { |
| 1010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1011 | uint8_t *exported = NULL; |
| 1012 | size_t exported_size = 0; |
| 1013 | size_t exported_length = 0; |
| 1014 | int ok = 0; |
| 1015 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1016 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 1017 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1018 | /* The key has been destroyed. */ |
| 1019 | psa_reset_key_attributes(&attributes); |
| 1020 | return 1; |
| 1021 | } |
| 1022 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1023 | |
| 1024 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | psa_get_key_type(&attributes), |
| 1026 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1027 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1028 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1029 | status = psa_export_key(key, exported, exported_size, &exported_length); |
| 1030 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1031 | /* The key has been destroyed. */ |
| 1032 | ok = 1; |
| 1033 | goto exit; |
| 1034 | } else if ((usage & PSA_KEY_USAGE_EXPORT) == 0 && |
| 1035 | !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) { |
| 1036 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1037 | ok = 1; |
| 1038 | goto exit; |
| 1039 | } |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1040 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1041 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes), |
| 1043 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1044 | |
| 1045 | exit: |
| 1046 | /* |
| 1047 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1048 | * thus reset them as required. |
| 1049 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | mbedtls_free(exported); |
| 1053 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1056 | static int exercise_export_public_key(mbedtls_svc_key_id_t key, |
| 1057 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1058 | { |
| 1059 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1060 | psa_key_type_t public_type; |
| 1061 | uint8_t *exported = NULL; |
| 1062 | size_t exported_size = 0; |
| 1063 | size_t exported_length = 0; |
| 1064 | int ok = 0; |
| 1065 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1066 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 1067 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1068 | /* The key has been destroyed. */ |
| 1069 | psa_reset_key_attributes(&attributes); |
| 1070 | return 1; |
| 1071 | } |
| 1072 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1073 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1074 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | psa_get_key_type(&attributes), |
| 1076 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1077 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1078 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1079 | status = psa_export_public_key(key, exported, |
| 1080 | exported_size, &exported_length); |
| 1081 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1082 | /* The key has been destroyed. */ |
| 1083 | ok = 1; |
| 1084 | goto exit; |
| 1085 | } |
| 1086 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1087 | ok = 1; |
| 1088 | goto exit; |
| 1089 | } |
| 1090 | |
| 1091 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | psa_get_key_type(&attributes)); |
| 1093 | exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, |
| 1094 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1095 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1096 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1097 | status = psa_export_public_key(key, exported, |
| 1098 | exported_size, &exported_length); |
| 1099 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1100 | /* The key has been destroyed. */ |
| 1101 | ok = 1; |
| 1102 | goto exit; |
| 1103 | } |
| 1104 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1105 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | public_type, psa_get_key_bits(&attributes), |
| 1107 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1108 | |
| 1109 | exit: |
| 1110 | /* |
| 1111 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1112 | * thus reset them as required. |
| 1113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | mbedtls_free(exported); |
| 1117 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1120 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 1121 | psa_key_usage_t usage, |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1122 | psa_algorithm_t alg, |
| 1123 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1124 | { |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 1125 | int ok = 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1126 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1127 | if (!check_key_attributes_sanity(key, key_destroyable)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | if (alg == 0) { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1132 | ok = 1; /* If no algorithm, do nothing (used for raw data "keys"). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | } else if (PSA_ALG_IS_MAC(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1134 | ok = exercise_mac_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | } else if (PSA_ALG_IS_CIPHER(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1136 | ok = exercise_cipher_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | } else if (PSA_ALG_IS_AEAD(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1138 | ok = exercise_aead_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | } else if (PSA_ALG_IS_SIGN(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1140 | ok = exercise_signature_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | } else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1142 | ok = exercise_asymmetric_encryption_key(key, usage, alg, |
| 1143 | key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1144 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1145 | ok = exercise_key_derivation_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | } else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1147 | ok = exercise_raw_key_agreement_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1149 | ok = exercise_key_agreement_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | } else { |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 1151 | TEST_FAIL("No code to exercise this category of algorithm"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1153 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1154 | ok = ok && exercise_export_key(key, |
| 1155 | usage, |
| 1156 | key_destroyable); |
| 1157 | ok = ok && exercise_export_public_key(key, |
| 1158 | key_destroyable); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1159 | |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 1160 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 1165 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1166 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) { |
| 1168 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 1169 | if (PSA_ALG_SIGN_GET_HASH(alg)) { |
| 1170 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1171 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 1172 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | |
| 1173 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 1174 | } |
| 1175 | } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 1176 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1177 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 1178 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
gabor-mezei-arm | 041887b | 2021-05-11 13:29:24 +0200 | [diff] [blame] | 1179 | } |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1182 | PSA_KEY_USAGE_VERIFY_HASH : |
| 1183 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; |
| 1184 | } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) || |
| 1185 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 1186 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1187 | PSA_KEY_USAGE_ENCRYPT : |
| 1188 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 1189 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg) || |
| 1190 | PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 1191 | return PSA_KEY_USAGE_DERIVE; |
| 1192 | } else { |
| 1193 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | } |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 1197 | |
Gilles Peskine | 3495567 | 2024-02-12 14:19:24 +0100 | [diff] [blame] | 1198 | int mbedtls_test_can_exercise_psa_algorithm(psa_algorithm_t alg) |
| 1199 | { |
| 1200 | /* Reject algorithms that we know are not supported. Default to |
| 1201 | * attempting exercise, so that if an algorithm is missing from this |
| 1202 | * function, the result will be a test failure and not silently |
| 1203 | * omitting exercise. */ |
| 1204 | #if !defined(PSA_WANT_ALG_RSA_PKCS1V15_CRYPT) |
| 1205 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
| 1206 | return 0; |
| 1207 | } |
| 1208 | #endif |
| 1209 | #if !defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) |
| 1210 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) { |
| 1211 | return 0; |
| 1212 | } |
| 1213 | #endif |
| 1214 | #if !defined(PSA_WANT_ALG_RSA_PSS) |
| 1215 | if (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg)) { |
| 1216 | return 0; |
| 1217 | } |
| 1218 | #endif |
| 1219 | #if !defined(PSA_WANT_ALG_RSA_PSS_ANY_SALT) |
| 1220 | if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) { |
| 1221 | return 0; |
| 1222 | } |
| 1223 | #endif |
| 1224 | #if !defined(PSA_WANT_ALG_ECDSA) |
| 1225 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 1226 | return 0; |
| 1227 | } |
| 1228 | #endif |
| 1229 | #if !defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA) |
| 1230 | if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) { |
| 1231 | return 0; |
| 1232 | } |
| 1233 | #endif |
| 1234 | #if !defined(PSA_WANT_ALG_ECDH) |
| 1235 | if (PSA_ALG_IS_ECDH(alg)) { |
| 1236 | return 0; |
| 1237 | } |
| 1238 | #endif |
| 1239 | (void) alg; |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
Gilles Peskine | 6fe8a06 | 2024-02-15 17:21:17 +0100 | [diff] [blame] | 1243 | #if defined(MBEDTLS_PK_C) |
| 1244 | int mbedtls_test_key_consistency_psa_pk(mbedtls_svc_key_id_t psa_key, |
| 1245 | const mbedtls_pk_context *pk) |
| 1246 | { |
| 1247 | psa_key_attributes_t psa_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1248 | psa_key_attributes_t pk_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1249 | int ok = 0; |
| 1250 | |
| 1251 | PSA_ASSERT(psa_get_key_attributes(psa_key, &psa_attributes)); |
| 1252 | psa_key_type_t psa_type = psa_get_key_type(&psa_attributes); |
| 1253 | mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk); |
| 1254 | |
| 1255 | TEST_ASSERT(PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_type) || |
| 1256 | PSA_KEY_TYPE_IS_KEY_PAIR(psa_type)); |
| 1257 | TEST_EQUAL(psa_get_key_bits(&psa_attributes), mbedtls_pk_get_bitlen(pk)); |
| 1258 | |
| 1259 | uint8_t pk_public_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]; |
| 1260 | const uint8_t *pk_public = NULL; |
| 1261 | size_t pk_public_length = 0; |
| 1262 | |
| 1263 | switch (pk_type) { |
| 1264 | #if defined(MBEDTLS_RSA_C) |
| 1265 | case MBEDTLS_PK_RSA: |
| 1266 | TEST_ASSERT(PSA_KEY_TYPE_IS_RSA(psa_type)); |
| 1267 | const mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk); |
| 1268 | uint8_t *const end = pk_public_buffer + sizeof(pk_public_buffer); |
| 1269 | uint8_t *cursor = end; |
| 1270 | TEST_LE_U(1, mbedtls_rsa_write_pubkey(rsa, |
| 1271 | pk_public_buffer, &cursor)); |
| 1272 | pk_public = cursor; |
| 1273 | pk_public_length = end - pk_public; |
| 1274 | break; |
| 1275 | #endif |
| 1276 | |
| 1277 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1278 | case MBEDTLS_PK_ECKEY: |
| 1279 | case MBEDTLS_PK_ECKEY_DH: |
| 1280 | case MBEDTLS_PK_ECDSA: |
| 1281 | TEST_ASSERT(PSA_KEY_TYPE_IS_ECC(psa_type)); |
| 1282 | TEST_EQUAL(PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type), pk->ec_family); |
| 1283 | pk_public = pk->pub_raw; |
| 1284 | pk_public_length = pk->pub_raw_len; |
| 1285 | break; |
| 1286 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 1287 | |
David Horstmann | 8660e4b | 2024-09-06 14:55:05 +0100 | [diff] [blame] | 1288 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Gilles Peskine | 6fe8a06 | 2024-02-15 17:21:17 +0100 | [diff] [blame] | 1289 | case MBEDTLS_PK_ECKEY: |
| 1290 | case MBEDTLS_PK_ECKEY_DH: |
| 1291 | case MBEDTLS_PK_ECDSA: |
| 1292 | TEST_ASSERT(PSA_KEY_TYPE_IS_ECC(psa_get_key_type(&psa_attributes))); |
| 1293 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk); |
| 1294 | TEST_EQUAL(mbedtls_ecp_write_public_key( |
| 1295 | ec, MBEDTLS_ECP_PF_UNCOMPRESSED, &pk_public_length, |
| 1296 | pk_public_buffer, sizeof(pk_public_buffer)), 0); |
| 1297 | pk_public = pk_public_buffer; |
| 1298 | break; |
David Horstmann | 8660e4b | 2024-09-06 14:55:05 +0100 | [diff] [blame] | 1299 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY && !MBEDTLS_PK_USE_PSA_EC_DATA */ |
Gilles Peskine | 6fe8a06 | 2024-02-15 17:21:17 +0100 | [diff] [blame] | 1300 | |
| 1301 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1302 | case MBEDTLS_PK_OPAQUE: |
| 1303 | PSA_ASSERT(psa_get_key_attributes(pk->priv_id, &pk_attributes)); |
| 1304 | psa_key_type_t pk_psa_type = psa_get_key_type(&pk_attributes); |
| 1305 | TEST_EQUAL(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_type), |
| 1306 | PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(pk_psa_type)); |
| 1307 | PSA_ASSERT(psa_export_public_key(psa_key, |
| 1308 | pk_public_buffer, |
| 1309 | sizeof(pk_public_buffer), |
| 1310 | &pk_public_length)); |
| 1311 | pk_public = pk_public_buffer; |
| 1312 | break; |
| 1313 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1314 | |
| 1315 | default: |
| 1316 | TEST_FAIL("pk type not supported"); |
| 1317 | } |
| 1318 | |
| 1319 | uint8_t psa_public[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]; |
| 1320 | size_t psa_public_length = 0; |
| 1321 | PSA_ASSERT(psa_export_public_key(psa_key, |
| 1322 | psa_public, sizeof(psa_public), |
| 1323 | &psa_public_length)); |
| 1324 | TEST_MEMORY_COMPARE(pk_public, pk_public_length, |
| 1325 | psa_public, psa_public_length); |
| 1326 | |
| 1327 | ok = 1; |
| 1328 | |
| 1329 | exit: |
| 1330 | psa_reset_key_attributes(&psa_attributes); |
| 1331 | psa_reset_key_attributes(&pk_attributes); |
| 1332 | return ok; |
| 1333 | } |
| 1334 | #endif /* MBEDTLS_PK_C */ |
| 1335 | |
David Horstmann | 8660e4b | 2024-09-06 14:55:05 +0100 | [diff] [blame] | 1336 | #endif /* MBEDTLS_PSA_CRYPTO_C || MBEDTLS_PSA_CRYPTO_CLIENT */ |