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 | 08ccf6a | 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 | |
Paul Elliott | 2c9d398 | 2024-09-19 15:49:32 +0100 | [diff] [blame] | 698 | mbedtls_svc_key_id_t shared_secret_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 699 | psa_key_attributes_t shared_secret_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 700 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 701 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 702 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 703 | /* The key has been destroyed. */ |
| 704 | psa_reset_key_attributes(&attributes); |
| 705 | return PSA_SUCCESS; |
| 706 | } |
| 707 | PSA_ASSERT(status); |
| 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | private_key_type = psa_get_key_type(&attributes); |
| 710 | key_bits = psa_get_key_bits(&attributes); |
| 711 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 712 | 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] | 713 | TEST_CALLOC(public_key, public_key_length); |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 714 | status = psa_export_public_key(key, |
| 715 | public_key, public_key_length, |
| 716 | &public_key_length); |
| 717 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 718 | /* The key has been destroyed. */ |
| 719 | status = PSA_SUCCESS; |
| 720 | goto exit; |
| 721 | } |
Ryan Everett | 6de38ac | 2024-03-14 17:50:39 +0000 | [diff] [blame] | 722 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | status = psa_raw_key_agreement(alg, key, |
| 725 | public_key, public_key_length, |
| 726 | output, sizeof(output), &output_length); |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 727 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 728 | /* The key has been destroyed. */ |
| 729 | status = PSA_SUCCESS; |
| 730 | goto exit; |
| 731 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | if (status == PSA_SUCCESS) { |
| 733 | TEST_ASSERT(output_length <= |
| 734 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(private_key_type, |
| 735 | key_bits)); |
| 736 | TEST_ASSERT(output_length <= |
| 737 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Paul Elliott | 2c9d398 | 2024-09-19 15:49:32 +0100 | [diff] [blame] | 740 | psa_set_key_type(&shared_secret_attributes, PSA_KEY_TYPE_DERIVE); |
| 741 | psa_set_key_usage_flags(&shared_secret_attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
| 742 | |
| 743 | status = psa_key_agreement(key, public_key, public_key_length, alg, |
| 744 | &shared_secret_attributes, &shared_secret_id); |
| 745 | |
| 746 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 747 | /* The key has been destroyed. */ |
| 748 | status = PSA_SUCCESS; |
| 749 | } |
| 750 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 751 | exit: |
| 752 | /* |
| 753 | * Key attributes may have been returned by psa_get_key_attributes() |
| 754 | * thus reset them as required. |
| 755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 757 | |
Paul Elliott | 2c9d398 | 2024-09-19 15:49:32 +0100 | [diff] [blame] | 758 | /* Make sure to reset and free derived key attributes and slot. */ |
| 759 | psa_reset_key_attributes(&shared_secret_attributes); |
| 760 | psa_destroy_key(shared_secret_id); |
| 761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | mbedtls_free(public_key); |
| 763 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key, |
| 767 | psa_key_usage_t usage, |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 768 | psa_algorithm_t alg, |
| 769 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 770 | { |
| 771 | int ok = 0; |
| 772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 773 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 774 | /* We need two keys to exercise key agreement. Exercise the |
| 775 | * private key against its own public key. */ |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 776 | PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key, |
| 777 | key_destroyable)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 778 | } |
| 779 | ok = 1; |
| 780 | |
| 781 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | static int exercise_key_agreement_key(mbedtls_svc_key_id_t key, |
| 786 | psa_key_usage_t usage, |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 787 | psa_algorithm_t alg, |
| 788 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 789 | { |
| 790 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gabor Mezei | dc3f3bb | 2022-07-01 15:06:34 +0200 | [diff] [blame] | 791 | unsigned char input[1] = { 0 }; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 792 | unsigned char output[1]; |
| 793 | int ok = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 795 | psa_status_t expected_key_agreement_status = PSA_SUCCESS; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 798 | /* We need two keys to exercise key agreement. Exercise the |
| 799 | * private key against its own public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 801 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 802 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 803 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 804 | &operation, PSA_KEY_DERIVATION_INPUT_SEED, |
| 805 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 808 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 809 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 810 | &operation, PSA_KEY_DERIVATION_INPUT_SALT, |
| 811 | input, sizeof(input))); |
Przemek Stekiel | d898745 | 2022-06-14 11:41:52 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 814 | /* For HKDF_EXPAND input secret may fail as secret size may not match |
| 815 | to expected PRK size. In practice it means that key bits must match |
| 816 | hash length. Otherwise test should fail with INVALID_ARGUMENT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 818 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 819 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 820 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 821 | /* The key has been destroyed. */ |
| 822 | ok = 1; |
| 823 | } |
| 824 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | size_t key_bits = psa_get_key_bits(&attributes); |
| 826 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | if (PSA_BITS_TO_BYTES(key_bits) != PSA_HASH_LENGTH(hash_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 829 | expected_key_agreement_status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | } |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 833 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(&operation, key, |
| 834 | key_destroyable), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | expected_key_agreement_status); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | if (expected_key_agreement_status != PSA_SUCCESS) { |
| 838 | return 1; |
| 839 | } |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 842 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 843 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 844 | &operation, PSA_KEY_DERIVATION_INPUT_LABEL, |
| 845 | input, sizeof(input))); |
| 846 | } else if (PSA_ALG_IS_HKDF(kdf_alg) || PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 847 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 848 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 849 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 850 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 852 | output, |
| 853 | sizeof(output))); |
| 854 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 855 | } |
| 856 | ok = 1; |
| 857 | |
| 858 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | int mbedtls_test_psa_exported_key_sanity_check( |
| 863 | psa_key_type_t type, size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | const uint8_t *exported, size_t exported_length) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 865 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) { |
| 869 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
| 870 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 871 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 872 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 874 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 875 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 876 | size_t len; |
| 877 | /* RSAPrivateKey ::= SEQUENCE { |
| 878 | * version INTEGER, -- must be 0 |
| 879 | * modulus INTEGER, -- n |
| 880 | * publicExponent INTEGER, -- e |
| 881 | * privateExponent INTEGER, -- d |
| 882 | * prime1 INTEGER, -- p |
| 883 | * prime2 INTEGER, -- q |
| 884 | * exponent1 INTEGER, -- d mod (p-1) |
| 885 | * exponent2 INTEGER, -- d mod (q-1) |
| 886 | * coefficient INTEGER, -- (inverse of q) mod p |
| 887 | * } |
| 888 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 890 | MBEDTLS_ASN1_SEQUENCE | |
| 891 | MBEDTLS_ASN1_CONSTRUCTED), 0); |
| 892 | TEST_EQUAL(len, end - p); |
| 893 | if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) { |
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, bits, bits, 1)) { |
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, 2, bits, 1)) { |
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 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 902 | /* Require d to be at least half the size of n. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 904 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 906 | /* 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] | 907 | 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] | 908 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | } |
| 910 | 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] | 911 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | } |
| 913 | 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] | 914 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | } |
| 916 | 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] | 917 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | } |
| 919 | 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] | 920 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | } |
| 922 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 924 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 925 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 926 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 929 | /* Just the secret value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 933 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 934 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 935 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 937 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 938 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 939 | size_t len; |
| 940 | /* RSAPublicKey ::= SEQUENCE { |
| 941 | * modulus INTEGER, -- n |
| 942 | * publicExponent INTEGER } -- e |
| 943 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 944 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 945 | MBEDTLS_ASN1_SEQUENCE | |
| 946 | MBEDTLS_ASN1_CONSTRUCTED), |
| 947 | 0); |
| 948 | TEST_EQUAL(len, end - p); |
| 949 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 950 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | } |
| 952 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 953 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | } |
| 955 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 956 | |
| 957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | TEST_ASSERT(exported_length <= |
| 959 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 960 | TEST_ASSERT(exported_length <= |
| 961 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 962 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 963 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 965 | if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) { |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | TEST_ASSERT(exported_length <= |
| 968 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 969 | TEST_ASSERT(exported_length <= |
| 970 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 973 | /* The representation of an ECC Montgomery public key is |
| 974 | * the raw compressed point */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length); |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 976 | } 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] | 977 | /* The representation of an ECC Edwards public key is |
| 978 | * the raw compressed point */ |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 979 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits + 1), exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | } else { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 981 | /* The representation of an ECC Weierstrass public key is: |
| 982 | * - The byte 0x04; |
| 983 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 984 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 985 | * - where m is the bit size associated with the curve. |
| 986 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length); |
| 988 | TEST_EQUAL(exported[0], 4); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 989 | } |
Przemek Stekiel | 7cf26df | 2022-12-01 15:09:40 +0100 | [diff] [blame] | 990 | } else |
| 991 | 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] | 992 | TEST_ASSERT(exported_length == |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 993 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 994 | TEST_ASSERT(exported_length <= |
| 995 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Valerio Setti | 2dbc306 | 2023-04-13 12:19:57 +0200 | [diff] [blame] | 996 | } else { |
Andrzej Kurek | 57d2f13 | 2022-01-17 15:26:24 +0100 | [diff] [blame] | 997 | (void) exported; |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 998 | TEST_FAIL("Sanity check not implemented for this key type"); |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 999 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1000 | |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1001 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | if (type == PSA_KEY_TYPE_DES) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1003 | /* Check the parity bits. */ |
| 1004 | unsigned i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | for (i = 0; i < bits / 8; i++) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1006 | unsigned bit_count = 0; |
| 1007 | unsigned m; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | for (m = 1; m <= 0x100; m <<= 1) { |
| 1009 | if (exported[i] & m) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1010 | ++bit_count; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1012 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1013 | TEST_ASSERT(bit_count % 2 != 0); |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1014 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1015 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 1016 | #endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1018 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1019 | |
| 1020 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | static int exercise_export_key(mbedtls_svc_key_id_t key, |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1025 | psa_key_usage_t usage, |
| 1026 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1027 | { |
| 1028 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1029 | uint8_t *exported = NULL; |
| 1030 | size_t exported_size = 0; |
| 1031 | size_t exported_length = 0; |
| 1032 | int ok = 0; |
| 1033 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1034 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 1035 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1036 | /* The key has been destroyed. */ |
| 1037 | psa_reset_key_attributes(&attributes); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1041 | |
| 1042 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | psa_get_key_type(&attributes), |
| 1044 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1045 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1046 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1047 | status = psa_export_key(key, exported, exported_size, &exported_length); |
| 1048 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1049 | /* The key has been destroyed. */ |
| 1050 | ok = 1; |
| 1051 | goto exit; |
| 1052 | } else if ((usage & PSA_KEY_USAGE_EXPORT) == 0 && |
| 1053 | !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) { |
| 1054 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1055 | ok = 1; |
| 1056 | goto exit; |
| 1057 | } |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1058 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1059 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes), |
| 1061 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1062 | |
| 1063 | exit: |
| 1064 | /* |
| 1065 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1066 | * thus reset them as required. |
| 1067 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | mbedtls_free(exported); |
| 1071 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1074 | static int exercise_export_public_key(mbedtls_svc_key_id_t key, |
| 1075 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1076 | { |
| 1077 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1078 | psa_key_type_t public_type; |
| 1079 | uint8_t *exported = NULL; |
| 1080 | size_t exported_size = 0; |
| 1081 | size_t exported_length = 0; |
| 1082 | int ok = 0; |
| 1083 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1084 | psa_status_t status = psa_get_key_attributes(key, &attributes); |
| 1085 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1086 | /* The key has been destroyed. */ |
| 1087 | psa_reset_key_attributes(&attributes); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | PSA_ASSERT(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1092 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | psa_get_key_type(&attributes), |
| 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 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1105 | ok = 1; |
| 1106 | goto exit; |
| 1107 | } |
| 1108 | |
| 1109 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | psa_get_key_type(&attributes)); |
| 1111 | exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, |
| 1112 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1113 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1114 | |
Ryan Everett | fbf815d | 2024-03-12 16:32:29 +0000 | [diff] [blame] | 1115 | status = psa_export_public_key(key, exported, |
| 1116 | exported_size, &exported_length); |
| 1117 | if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) { |
| 1118 | /* The key has been destroyed. */ |
| 1119 | ok = 1; |
| 1120 | goto exit; |
| 1121 | } |
| 1122 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1123 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | public_type, psa_get_key_bits(&attributes), |
| 1125 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1126 | |
| 1127 | exit: |
| 1128 | /* |
| 1129 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1130 | * thus reset them as required. |
| 1131 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1134 | mbedtls_free(exported); |
| 1135 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1136 | } |
| 1137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 1139 | psa_key_usage_t usage, |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1140 | psa_algorithm_t alg, |
| 1141 | int key_destroyable) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1142 | { |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 1143 | int ok = 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1144 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1145 | if (!check_key_attributes_sanity(key, key_destroyable)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | if (alg == 0) { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1150 | ok = 1; /* If no algorithm, do nothing (used for raw data "keys"). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | } else if (PSA_ALG_IS_MAC(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1152 | ok = exercise_mac_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | } else if (PSA_ALG_IS_CIPHER(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1154 | ok = exercise_cipher_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | } else if (PSA_ALG_IS_AEAD(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1156 | ok = exercise_aead_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | } else if (PSA_ALG_IS_SIGN(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1158 | ok = exercise_signature_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | } else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1160 | ok = exercise_asymmetric_encryption_key(key, usage, alg, |
| 1161 | key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1163 | ok = exercise_key_derivation_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | } else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1165 | ok = exercise_raw_key_agreement_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1166 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1167 | ok = exercise_key_agreement_key(key, usage, alg, key_destroyable); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | } else { |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 1169 | TEST_FAIL("No code to exercise this category of algorithm"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1171 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1172 | ok = ok && exercise_export_key(key, |
| 1173 | usage, |
| 1174 | key_destroyable); |
| 1175 | ok = ok && exercise_export_public_key(key, |
| 1176 | key_destroyable); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1177 | |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 1178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1182 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 1183 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1184 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) { |
| 1186 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 1187 | if (PSA_ALG_SIGN_GET_HASH(alg)) { |
| 1188 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1189 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 1190 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | |
| 1191 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 1192 | } |
| 1193 | } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 1194 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1195 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 1196 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
gabor-mezei-arm | 041887b | 2021-05-11 13:29:24 +0200 | [diff] [blame] | 1197 | } |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 1198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1199 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1200 | PSA_KEY_USAGE_VERIFY_HASH : |
| 1201 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; |
| 1202 | } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) || |
| 1203 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 1204 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 1205 | PSA_KEY_USAGE_ENCRYPT : |
| 1206 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 1207 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg) || |
| 1208 | PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 1209 | return PSA_KEY_USAGE_DERIVE; |
| 1210 | } else { |
| 1211 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | } |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 1215 | |
Gilles Peskine | 3495567 | 2024-02-12 14:19:24 +0100 | [diff] [blame] | 1216 | int mbedtls_test_can_exercise_psa_algorithm(psa_algorithm_t alg) |
| 1217 | { |
| 1218 | /* Reject algorithms that we know are not supported. Default to |
| 1219 | * attempting exercise, so that if an algorithm is missing from this |
| 1220 | * function, the result will be a test failure and not silently |
| 1221 | * omitting exercise. */ |
| 1222 | #if !defined(PSA_WANT_ALG_RSA_PKCS1V15_CRYPT) |
| 1223 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
| 1224 | return 0; |
| 1225 | } |
| 1226 | #endif |
| 1227 | #if !defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) |
| 1228 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) { |
| 1229 | return 0; |
| 1230 | } |
| 1231 | #endif |
| 1232 | #if !defined(PSA_WANT_ALG_RSA_PSS) |
| 1233 | if (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg)) { |
| 1234 | return 0; |
| 1235 | } |
| 1236 | #endif |
| 1237 | #if !defined(PSA_WANT_ALG_RSA_PSS_ANY_SALT) |
| 1238 | if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) { |
| 1239 | return 0; |
| 1240 | } |
| 1241 | #endif |
| 1242 | #if !defined(PSA_WANT_ALG_ECDSA) |
| 1243 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 1244 | return 0; |
| 1245 | } |
| 1246 | #endif |
| 1247 | #if !defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA) |
| 1248 | if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) { |
| 1249 | return 0; |
| 1250 | } |
| 1251 | #endif |
| 1252 | #if !defined(PSA_WANT_ALG_ECDH) |
| 1253 | if (PSA_ALG_IS_ECDH(alg)) { |
| 1254 | return 0; |
| 1255 | } |
| 1256 | #endif |
| 1257 | (void) alg; |
| 1258 | return 1; |
| 1259 | } |
| 1260 | |
Gilles Peskine | 6fe8a06 | 2024-02-15 17:21:17 +0100 | [diff] [blame] | 1261 | #if defined(MBEDTLS_PK_C) |
| 1262 | int mbedtls_test_key_consistency_psa_pk(mbedtls_svc_key_id_t psa_key, |
| 1263 | const mbedtls_pk_context *pk) |
| 1264 | { |
| 1265 | psa_key_attributes_t psa_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1266 | psa_key_attributes_t pk_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1267 | int ok = 0; |
| 1268 | |
| 1269 | PSA_ASSERT(psa_get_key_attributes(psa_key, &psa_attributes)); |
| 1270 | psa_key_type_t psa_type = psa_get_key_type(&psa_attributes); |
| 1271 | mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk); |
| 1272 | |
| 1273 | TEST_ASSERT(PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_type) || |
| 1274 | PSA_KEY_TYPE_IS_KEY_PAIR(psa_type)); |
| 1275 | TEST_EQUAL(psa_get_key_bits(&psa_attributes), mbedtls_pk_get_bitlen(pk)); |
| 1276 | |
| 1277 | uint8_t pk_public_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]; |
| 1278 | const uint8_t *pk_public = NULL; |
| 1279 | size_t pk_public_length = 0; |
| 1280 | |
| 1281 | switch (pk_type) { |
| 1282 | #if defined(MBEDTLS_RSA_C) |
| 1283 | case MBEDTLS_PK_RSA: |
| 1284 | TEST_ASSERT(PSA_KEY_TYPE_IS_RSA(psa_type)); |
| 1285 | const mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk); |
| 1286 | uint8_t *const end = pk_public_buffer + sizeof(pk_public_buffer); |
| 1287 | uint8_t *cursor = end; |
| 1288 | TEST_LE_U(1, mbedtls_rsa_write_pubkey(rsa, |
| 1289 | pk_public_buffer, &cursor)); |
| 1290 | pk_public = cursor; |
| 1291 | pk_public_length = end - pk_public; |
| 1292 | break; |
| 1293 | #endif |
| 1294 | |
| 1295 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1296 | case MBEDTLS_PK_ECKEY: |
| 1297 | case MBEDTLS_PK_ECKEY_DH: |
| 1298 | case MBEDTLS_PK_ECDSA: |
| 1299 | TEST_ASSERT(PSA_KEY_TYPE_IS_ECC(psa_type)); |
| 1300 | TEST_EQUAL(PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type), pk->ec_family); |
| 1301 | pk_public = pk->pub_raw; |
| 1302 | pk_public_length = pk->pub_raw_len; |
| 1303 | break; |
| 1304 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 1305 | |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 1306 | #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] | 1307 | case MBEDTLS_PK_ECKEY: |
| 1308 | case MBEDTLS_PK_ECKEY_DH: |
| 1309 | case MBEDTLS_PK_ECDSA: |
| 1310 | TEST_ASSERT(PSA_KEY_TYPE_IS_ECC(psa_get_key_type(&psa_attributes))); |
| 1311 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk); |
| 1312 | TEST_EQUAL(mbedtls_ecp_write_public_key( |
| 1313 | ec, MBEDTLS_ECP_PF_UNCOMPRESSED, &pk_public_length, |
| 1314 | pk_public_buffer, sizeof(pk_public_buffer)), 0); |
| 1315 | pk_public = pk_public_buffer; |
| 1316 | break; |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 1317 | #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] | 1318 | |
| 1319 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1320 | case MBEDTLS_PK_OPAQUE: |
| 1321 | PSA_ASSERT(psa_get_key_attributes(pk->priv_id, &pk_attributes)); |
| 1322 | psa_key_type_t pk_psa_type = psa_get_key_type(&pk_attributes); |
| 1323 | TEST_EQUAL(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_type), |
| 1324 | PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(pk_psa_type)); |
| 1325 | PSA_ASSERT(psa_export_public_key(psa_key, |
| 1326 | pk_public_buffer, |
| 1327 | sizeof(pk_public_buffer), |
| 1328 | &pk_public_length)); |
| 1329 | pk_public = pk_public_buffer; |
| 1330 | break; |
| 1331 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1332 | |
| 1333 | default: |
| 1334 | TEST_FAIL("pk type not supported"); |
| 1335 | } |
| 1336 | |
| 1337 | uint8_t psa_public[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]; |
| 1338 | size_t psa_public_length = 0; |
| 1339 | PSA_ASSERT(psa_export_public_key(psa_key, |
| 1340 | psa_public, sizeof(psa_public), |
| 1341 | &psa_public_length)); |
| 1342 | TEST_MEMORY_COMPARE(pk_public, pk_public_length, |
| 1343 | psa_public, psa_public_length); |
| 1344 | |
| 1345 | ok = 1; |
| 1346 | |
| 1347 | exit: |
| 1348 | psa_reset_key_attributes(&psa_attributes); |
| 1349 | psa_reset_key_attributes(&pk_attributes); |
| 1350 | return ok; |
| 1351 | } |
| 1352 | #endif /* MBEDTLS_PK_C */ |
| 1353 | |
David Horstmann | 0d8287c | 2024-07-26 18:01:04 +0100 | [diff] [blame] | 1354 | #endif /* MBEDTLS_PSA_CRYPTO_C || MBEDTLS_PSA_CRYPTO_CLIENT */ |