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