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