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 | 7ff7965 | 2023-11-03 12:04:52 +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 | b66bc0a | 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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 5b673a8 | 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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 137 | |
| 138 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Gilles Peskine | b534759 | 2022-04-21 11:14:30 +0200 | [diff] [blame] | 149 | size_t iv_length; |
Gilles Peskine | 8f3aad2 | 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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 8f3aad2 | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 161 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 8f3aad2 | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 168 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 8f3aad2 | 2022-03-18 18:40:47 +0100 | [diff] [blame] | 190 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 200 | * if the input is some arbitrary data rather than an actual |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 211 | |
| 212 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | unsigned char nonce[PSA_AEAD_NONCE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 743972c | 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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 264 | |
| 265 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | static int can_sign_or_verify_message(psa_key_usage_t usage, |
| 271 | psa_algorithm_t alg) |
Gilles Peskine | 275ecde | 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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 275ecde | 2022-03-19 11:15:41 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | if (usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH)) { |
| 287 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 288 | size_t payload_length = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
| 290 | size_t signature_length = sizeof(signature); |
| 291 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 292 | |
| 293 | /* If the policy allows signing with any hash, just pick one. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | if (PSA_ALG_IS_SIGN_HASH(alg) && hash_alg == PSA_ALG_ANY_HASH) { |
Ronald Cron | ef14af0 | 2021-08-31 19:08:55 +0200 | [diff] [blame] | 295 | #if defined(KNOWN_MBEDTLS_SUPPORTED_HASH_ALG) |
| 296 | hash_alg = KNOWN_MBEDTLS_SUPPORTED_HASH_ALG; |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 297 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
| 298 | #else |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 299 | TEST_FAIL("No hash algorithm for hash-and-sign testing"); |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 300 | #endif |
| 301 | } |
| 302 | |
Janos Follath | 02becd9 | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 303 | /* Some algorithms require the payload to have the size of |
| 304 | * the hash encoded in the algorithm. Use this input size |
| 305 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | if (hash_alg != 0) { |
| 307 | payload_length = PSA_HASH_LENGTH(hash_alg); |
| 308 | } |
Janos Follath | 02becd9 | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | if (usage & PSA_KEY_USAGE_SIGN_HASH) { |
| 311 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 312 | payload, payload_length, |
| 313 | signature, sizeof(signature), |
| 314 | &signature_length)); |
| 315 | } |
| 316 | |
| 317 | if (usage & PSA_KEY_USAGE_VERIFY_HASH) { |
| 318 | psa_status_t verify_status = |
| 319 | (usage & PSA_KEY_USAGE_SIGN_HASH ? |
| 320 | PSA_SUCCESS : |
| 321 | PSA_ERROR_INVALID_SIGNATURE); |
| 322 | TEST_EQUAL(psa_verify_hash(key, alg, |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 323 | payload, payload_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 324 | signature, signature_length), |
| 325 | verify_status); |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 326 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | if (can_sign_or_verify_message(usage, alg)) { |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 330 | unsigned char message[256] = "Hello, world..."; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 331 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 332 | size_t message_length = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | size_t signature_length = sizeof(signature); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | if (usage & PSA_KEY_USAGE_SIGN_MESSAGE) { |
| 336 | PSA_ASSERT(psa_sign_message(key, alg, |
| 337 | message, message_length, |
| 338 | signature, sizeof(signature), |
| 339 | &signature_length)); |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | if (usage & PSA_KEY_USAGE_VERIFY_MESSAGE) { |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 343 | psa_status_t verify_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | (usage & PSA_KEY_USAGE_SIGN_MESSAGE ? |
| 345 | PSA_SUCCESS : |
| 346 | PSA_ERROR_INVALID_SIGNATURE); |
| 347 | TEST_EQUAL(psa_verify_message(key, alg, |
| 348 | message, message_length, |
| 349 | signature, signature_length), |
| 350 | verify_status); |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 351 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 355 | |
| 356 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | static int exercise_asymmetric_encryption_key(mbedtls_svc_key_id_t key, |
| 361 | psa_key_usage_t usage, |
| 362 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 363 | { |
| 364 | unsigned char plaintext[256] = "Hello, world..."; |
| 365 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 366 | size_t ciphertext_length = sizeof(ciphertext); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 367 | size_t plaintext_length = 16; |
| 368 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
| 370 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 371 | plaintext, plaintext_length, |
| 372 | NULL, 0, |
| 373 | ciphertext, sizeof(ciphertext), |
| 374 | &ciphertext_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 378 | psa_status_t status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | psa_asymmetric_decrypt(key, alg, |
| 380 | ciphertext, ciphertext_length, |
| 381 | NULL, 0, |
| 382 | plaintext, sizeof(plaintext), |
| 383 | &plaintext_length); |
| 384 | TEST_ASSERT(status == PSA_SUCCESS || |
| 385 | ((usage & PSA_KEY_USAGE_ENCRYPT) == 0 && |
| 386 | (status == PSA_ERROR_INVALID_ARGUMENT || |
| 387 | status == PSA_ERROR_INVALID_PADDING))); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 391 | |
| 392 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | int mbedtls_test_psa_setup_key_derivation_wrap( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 398 | mbedtls_svc_key_id_t key, |
| 399 | psa_algorithm_t alg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 400 | const unsigned char *input1, size_t input1_length, |
| 401 | const unsigned char *input2, size_t input2_length, |
| 402 | size_t capacity) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 403 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | PSA_ASSERT(psa_key_derivation_setup(operation, alg)); |
| 405 | if (PSA_ALG_IS_HKDF(alg)) { |
| 406 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 407 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 408 | input1, input1_length)); |
| 409 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 410 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 411 | key)); |
| 412 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 413 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 414 | input2, |
| 415 | input2_length)); |
| 416 | } else if (PSA_ALG_IS_TLS12_PRF(alg) || |
| 417 | PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 418 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 419 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 420 | input1, input1_length)); |
| 421 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 422 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 423 | key)); |
| 424 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 425 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 426 | input2, input2_length)); |
| 427 | } else { |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 428 | TEST_FAIL("Key derivation algorithm not supported"); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 431 | if (capacity != SIZE_MAX) { |
| 432 | PSA_ASSERT(psa_key_derivation_set_capacity(operation, capacity)); |
| 433 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 436 | |
| 437 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | static int exercise_key_derivation_key(mbedtls_svc_key_id_t key, |
| 443 | psa_key_usage_t usage, |
| 444 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 445 | { |
| 446 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 447 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 448 | size_t input1_length = sizeof(input1); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 449 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | size_t input2_length = sizeof(input2); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 451 | unsigned char output[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | size_t capacity = sizeof(output); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | if (usage & PSA_KEY_USAGE_DERIVE) { |
| 455 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 456 | input1, input1_length, |
| 457 | input2, input2_length, |
| 458 | capacity)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 459 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 462 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 463 | output, |
| 464 | capacity)); |
| 465 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 468 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 469 | |
| 470 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* We need two keys to exercise key agreement. Exercise the |
| 475 | * private key against its own public key. */ |
| 476 | psa_status_t mbedtls_test_psa_key_agreement_with_self( |
| 477 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 479 | { |
| 480 | psa_key_type_t private_key_type; |
| 481 | psa_key_type_t public_key_type; |
| 482 | size_t key_bits; |
| 483 | uint8_t *public_key = NULL; |
| 484 | size_t public_key_length; |
| 485 | /* Return GENERIC_ERROR if something other than the final call to |
| 486 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 487 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 488 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 489 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 490 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 491 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 492 | private_key_type = psa_get_key_type(&attributes); |
| 493 | key_bits = psa_get_key_bits(&attributes); |
| 494 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 495 | public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 496 | TEST_CALLOC(public_key, public_key_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 497 | PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length, |
| 498 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 499 | |
| 500 | status = psa_key_derivation_key_agreement( |
| 501 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | public_key, public_key_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 503 | exit: |
| 504 | /* |
| 505 | * Key attributes may have been returned by psa_get_key_attributes() |
| 506 | * thus reset them as required. |
| 507 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | mbedtls_free(public_key); |
| 511 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* We need two keys to exercise key agreement. Exercise the |
| 515 | * private key against its own public key. */ |
| 516 | psa_status_t mbedtls_test_psa_raw_key_agreement_with_self( |
| 517 | psa_algorithm_t alg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 518 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 519 | { |
| 520 | psa_key_type_t private_key_type; |
| 521 | psa_key_type_t public_key_type; |
| 522 | size_t key_bits; |
| 523 | uint8_t *public_key = NULL; |
| 524 | size_t public_key_length; |
| 525 | uint8_t output[1024]; |
| 526 | size_t output_length; |
| 527 | /* Return GENERIC_ERROR if something other than the final call to |
| 528 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 529 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 530 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 531 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 532 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 533 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 534 | private_key_type = psa_get_key_type(&attributes); |
| 535 | key_bits = psa_get_key_bits(&attributes); |
| 536 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 537 | public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 538 | TEST_CALLOC(public_key, public_key_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 539 | PSA_ASSERT(psa_export_public_key(key, |
| 540 | public_key, public_key_length, |
| 541 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 542 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 543 | status = psa_raw_key_agreement(alg, key, |
| 544 | public_key, public_key_length, |
| 545 | output, sizeof(output), &output_length); |
| 546 | if (status == PSA_SUCCESS) { |
| 547 | TEST_ASSERT(output_length <= |
| 548 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(private_key_type, |
| 549 | key_bits)); |
| 550 | TEST_ASSERT(output_length <= |
| 551 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 554 | exit: |
| 555 | /* |
| 556 | * Key attributes may have been returned by psa_get_key_attributes() |
| 557 | * thus reset them as required. |
| 558 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 560 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 561 | mbedtls_free(public_key); |
| 562 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 565 | static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key, |
| 566 | psa_key_usage_t usage, |
| 567 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 568 | { |
| 569 | int ok = 0; |
| 570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 572 | /* We need two keys to exercise key agreement. Exercise the |
| 573 | * private key against its own public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 574 | PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 575 | } |
| 576 | ok = 1; |
| 577 | |
| 578 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 579 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | static int exercise_key_agreement_key(mbedtls_svc_key_id_t key, |
| 583 | psa_key_usage_t usage, |
| 584 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 585 | { |
| 586 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 9d3706f | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 587 | unsigned char input[1]; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 588 | unsigned char output[1]; |
| 589 | int ok = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 590 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 593 | /* We need two keys to exercise key agreement. Exercise the |
| 594 | * private key against its own public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 596 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 597 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 598 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 599 | &operation, PSA_KEY_DERIVATION_INPUT_SEED, |
| 600 | input, sizeof(input))); |
Gilles Peskine | 9d3706f | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | PSA_ASSERT(mbedtls_test_psa_key_agreement_with_self(&operation, key)); |
Gilles Peskine | 9d3706f | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 604 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 605 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 606 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 607 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 608 | &operation, PSA_KEY_DERIVATION_INPUT_LABEL, |
| 609 | input, sizeof(input))); |
| 610 | } else if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 611 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 612 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 613 | input, sizeof(input))); |
Gilles Peskine | 9d3706f | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 614 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 615 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 616 | output, |
| 617 | sizeof(output))); |
| 618 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 619 | } |
| 620 | ok = 1; |
| 621 | |
| 622 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | int mbedtls_test_psa_exported_key_sanity_check( |
| 627 | psa_key_type_t type, size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | const uint8_t *exported, size_t exported_length) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 629 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 630 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 631 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 632 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) { |
| 633 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
| 634 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 635 | |
Ronald Cron | fefa458 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 636 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 638 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 639 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 640 | size_t len; |
| 641 | /* RSAPrivateKey ::= SEQUENCE { |
| 642 | * version INTEGER, -- must be 0 |
| 643 | * modulus INTEGER, -- n |
| 644 | * publicExponent INTEGER, -- e |
| 645 | * privateExponent INTEGER, -- d |
| 646 | * prime1 INTEGER, -- p |
| 647 | * prime2 INTEGER, -- q |
| 648 | * exponent1 INTEGER, -- d mod (p-1) |
| 649 | * exponent2 INTEGER, -- d mod (q-1) |
| 650 | * coefficient INTEGER, -- (inverse of q) mod p |
| 651 | * } |
| 652 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 653 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 654 | MBEDTLS_ASN1_SEQUENCE | |
| 655 | MBEDTLS_ASN1_CONSTRUCTED), 0); |
| 656 | TEST_EQUAL(len, end - p); |
| 657 | if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 658 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 659 | } |
| 660 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 661 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 662 | } |
| 663 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 664 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 666 | /* Require d to be at least half the size of n. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 667 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 668 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 669 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 670 | /* Require p and q to be at most half the size of n, rounded up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 671 | 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] | 672 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 673 | } |
| 674 | 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] | 675 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 676 | } |
| 677 | 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] | 678 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 679 | } |
| 680 | 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] | 681 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | } |
| 683 | 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] | 684 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 685 | } |
| 686 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 688 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 689 | } else |
Ronald Cron | fefa458 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 690 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 691 | |
| 692 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 693 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 694 | /* Just the secret value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 695 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 697 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 698 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 699 | #endif /* MBEDTLS_ECP_C */ |
| 700 | |
Ronald Cron | fefa458 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 701 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 702 | if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 703 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 704 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 705 | size_t len; |
| 706 | /* RSAPublicKey ::= SEQUENCE { |
| 707 | * modulus INTEGER, -- n |
| 708 | * publicExponent INTEGER } -- e |
| 709 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 710 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 711 | MBEDTLS_ASN1_SEQUENCE | |
| 712 | MBEDTLS_ASN1_CONSTRUCTED), |
| 713 | 0); |
| 714 | TEST_EQUAL(len, end - p); |
| 715 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 716 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | } |
| 718 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 719 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 720 | } |
| 721 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 722 | |
| 723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 724 | TEST_ASSERT(exported_length <= |
| 725 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 726 | TEST_ASSERT(exported_length <= |
| 727 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 728 | } else |
Ronald Cron | fefa458 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 729 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 730 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 731 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 732 | if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) { |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 733 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 734 | TEST_ASSERT(exported_length <= |
| 735 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 736 | TEST_ASSERT(exported_length <= |
| 737 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 738 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 739 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 740 | /* The representation of an ECC Montgomery public key is |
| 741 | * the raw compressed point */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 742 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length); |
| 743 | } else { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 744 | /* The representation of an ECC Weierstrass public key is: |
| 745 | * - The byte 0x04; |
| 746 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 747 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 748 | * - where m is the bit size associated with the curve. |
| 749 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 750 | TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length); |
| 751 | TEST_EQUAL(exported[0], 4); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 752 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 753 | } else |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 754 | #endif /* MBEDTLS_ECP_C */ |
| 755 | |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 756 | { |
Andrzej Kurek | 53ad763 | 2022-01-17 15:26:24 +0100 | [diff] [blame] | 757 | (void) exported; |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 758 | TEST_FAIL("Sanity check not implemented for this key type"); |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 759 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 760 | |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 761 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 762 | if (type == PSA_KEY_TYPE_DES) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 763 | /* Check the parity bits. */ |
| 764 | unsigned i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 765 | for (i = 0; i < bits / 8; i++) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 766 | unsigned bit_count = 0; |
| 767 | unsigned m; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | for (m = 1; m <= 0x100; m <<= 1) { |
| 769 | if (exported[i] & m) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 770 | ++bit_count; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 771 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 772 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 773 | TEST_ASSERT(bit_count % 2 != 0); |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 774 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 775 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 776 | #endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 778 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 779 | |
| 780 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 781 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 784 | static int exercise_export_key(mbedtls_svc_key_id_t key, |
| 785 | psa_key_usage_t usage) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 786 | { |
| 787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 788 | uint8_t *exported = NULL; |
| 789 | size_t exported_size = 0; |
| 790 | size_t exported_length = 0; |
| 791 | int ok = 0; |
| 792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 793 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 794 | |
| 795 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 796 | psa_get_key_type(&attributes), |
| 797 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 798 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 800 | if ((usage & PSA_KEY_USAGE_EXPORT) == 0 && |
| 801 | !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) { |
| 802 | TEST_EQUAL(psa_export_key(key, exported, |
| 803 | exported_size, &exported_length), |
| 804 | PSA_ERROR_NOT_PERMITTED); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 805 | ok = 1; |
| 806 | goto exit; |
| 807 | } |
| 808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 809 | PSA_ASSERT(psa_export_key(key, |
| 810 | exported, exported_size, |
| 811 | &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 812 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 813 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes), |
| 814 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 815 | |
| 816 | exit: |
| 817 | /* |
| 818 | * Key attributes may have been returned by psa_get_key_attributes() |
| 819 | * thus reset them as required. |
| 820 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 821 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 822 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 823 | mbedtls_free(exported); |
| 824 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 825 | } |
| 826 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 827 | static int exercise_export_public_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 828 | { |
| 829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 830 | psa_key_type_t public_type; |
| 831 | uint8_t *exported = NULL; |
| 832 | size_t exported_size = 0; |
| 833 | size_t exported_length = 0; |
| 834 | int ok = 0; |
| 835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 836 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 837 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 838 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 839 | psa_get_key_type(&attributes), |
| 840 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 841 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 843 | TEST_EQUAL(psa_export_public_key(key, exported, |
| 844 | exported_size, &exported_length), |
| 845 | PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 846 | ok = 1; |
| 847 | goto exit; |
| 848 | } |
| 849 | |
| 850 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 851 | psa_get_key_type(&attributes)); |
| 852 | exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, |
| 853 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 854 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 856 | PSA_ASSERT(psa_export_public_key(key, |
| 857 | exported, exported_size, |
| 858 | &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 859 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 860 | public_type, psa_get_key_bits(&attributes), |
| 861 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 862 | |
| 863 | exit: |
| 864 | /* |
| 865 | * Key attributes may have been returned by psa_get_key_attributes() |
| 866 | * thus reset them as required. |
| 867 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 868 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 869 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 870 | mbedtls_free(exported); |
| 871 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 874 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 875 | psa_key_usage_t usage, |
| 876 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 877 | { |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 878 | int ok = 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 880 | if (!check_key_attributes_sanity(key)) { |
| 881 | return 0; |
| 882 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 884 | if (alg == 0) { |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 885 | ok = 1; /* If no algorithm, do nothing (used for raw data "keys"). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 886 | } else if (PSA_ALG_IS_MAC(alg)) { |
| 887 | ok = exercise_mac_key(key, usage, alg); |
| 888 | } else if (PSA_ALG_IS_CIPHER(alg)) { |
| 889 | ok = exercise_cipher_key(key, usage, alg); |
| 890 | } else if (PSA_ALG_IS_AEAD(alg)) { |
| 891 | ok = exercise_aead_key(key, usage, alg); |
| 892 | } else if (PSA_ALG_IS_SIGN(alg)) { |
| 893 | ok = exercise_signature_key(key, usage, alg); |
| 894 | } else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 895 | ok = exercise_asymmetric_encryption_key(key, usage, alg); |
| 896 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 897 | ok = exercise_key_derivation_key(key, usage, alg); |
| 898 | } else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 899 | ok = exercise_raw_key_agreement_key(key, usage, alg); |
| 900 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 901 | ok = exercise_key_agreement_key(key, usage, alg); |
| 902 | } else { |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 903 | TEST_FAIL("No code to exercise this category of algorithm"); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 904 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 905 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 906 | ok = ok && exercise_export_key(key, usage); |
| 907 | ok = ok && exercise_export_public_key(key); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 908 | |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 909 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 910 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 913 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 914 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 915 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 916 | if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) { |
| 917 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 918 | if (PSA_ALG_SIGN_GET_HASH(alg)) { |
| 919 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 920 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 921 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | |
| 922 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 923 | } |
| 924 | } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 925 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 926 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 927 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
gabor-mezei-arm | 7261697 | 2021-05-11 13:29:24 +0200 | [diff] [blame] | 928 | } |
gabor-mezei-arm | 7a74c13 | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 929 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 930 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 931 | PSA_KEY_USAGE_VERIFY_HASH : |
| 932 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; |
| 933 | } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) || |
| 934 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 935 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 936 | PSA_KEY_USAGE_ENCRYPT : |
| 937 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 938 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg) || |
| 939 | PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 940 | return PSA_KEY_USAGE_DERIVE; |
| 941 | } else { |
| 942 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | } |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 946 | |
| 947 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |