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