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