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 | { |
| 367 | unsigned char plaintext[256] = "Hello, world..."; |
| 368 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | size_t ciphertext_length = sizeof(ciphertext); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 370 | size_t plaintext_length = 16; |
| 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
| 373 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 374 | plaintext, plaintext_length, |
| 375 | NULL, 0, |
| 376 | ciphertext, sizeof(ciphertext), |
| 377 | &ciphertext_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 381 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | psa_asymmetric_decrypt(key, alg, |
| 383 | ciphertext, ciphertext_length, |
| 384 | NULL, 0, |
| 385 | plaintext, sizeof(plaintext), |
| 386 | &plaintext_length); |
| 387 | TEST_ASSERT(status == PSA_SUCCESS || |
| 388 | ((usage & PSA_KEY_USAGE_ENCRYPT) == 0 && |
| 389 | (status == PSA_ERROR_INVALID_ARGUMENT || |
| 390 | status == PSA_ERROR_INVALID_PADDING))); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 394 | |
| 395 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | int mbedtls_test_psa_setup_key_derivation_wrap( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 401 | mbedtls_svc_key_id_t key, |
| 402 | psa_algorithm_t alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | const unsigned char *input1, size_t input1_length, |
| 404 | const unsigned char *input2, size_t input2_length, |
| 405 | size_t capacity) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 406 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | PSA_ASSERT(psa_key_derivation_setup(operation, alg)); |
| 408 | if (PSA_ALG_IS_HKDF(alg)) { |
| 409 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 410 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 411 | input1, input1_length)); |
| 412 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 413 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 414 | key)); |
| 415 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 416 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 417 | input2, |
| 418 | input2_length)); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 419 | } else if (PSA_ALG_IS_HKDF_EXTRACT(alg)) { |
| 420 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 421 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 422 | input1, input1_length)); |
| 423 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 424 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 425 | key)); |
| 426 | } else if (PSA_ALG_IS_HKDF_EXPAND(alg)) { |
| 427 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 428 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 429 | key)); |
| 430 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 431 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 432 | input2, |
| 433 | input2_length)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | } else if (PSA_ALG_IS_TLS12_PRF(alg) || |
| 435 | PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 436 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 437 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 438 | input1, input1_length)); |
| 439 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 440 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 441 | key)); |
| 442 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 443 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 444 | input2, input2_length)); |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 445 | } else if (PSA_ALG_IS_PBKDF2(alg)) { |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 446 | PSA_ASSERT(psa_key_derivation_input_integer(operation, |
| 447 | PSA_KEY_DERIVATION_INPUT_COST, |
Kusumit Ghoderao | 94d3190 | 2023-09-05 19:30:22 +0530 | [diff] [blame] | 448 | 1U)); |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 449 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 450 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 451 | input2, |
| 452 | input2_length)); |
| 453 | PSA_ASSERT(psa_key_derivation_input_key(operation, |
| 454 | PSA_KEY_DERIVATION_INPUT_PASSWORD, |
| 455 | key)); |
Kusumit Ghoderao | 2c4264b | 2023-12-01 16:41:26 +0530 | [diff] [blame] | 456 | } else if (alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 457 | PSA_ASSERT(psa_key_derivation_input_bytes(operation, |
| 458 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 459 | input1, input1_length)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | } else { |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 461 | TEST_FAIL("Key derivation algorithm not supported"); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | if (capacity != SIZE_MAX) { |
| 465 | PSA_ASSERT(psa_key_derivation_set_capacity(operation, capacity)); |
| 466 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 469 | |
| 470 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | static int exercise_key_derivation_key(mbedtls_svc_key_id_t key, |
| 476 | psa_key_usage_t usage, |
| 477 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 478 | { |
| 479 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 480 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | size_t input1_length = sizeof(input1); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 482 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | size_t input2_length = sizeof(input2); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 484 | unsigned char output[1]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | size_t capacity = sizeof(output); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | if (usage & PSA_KEY_USAGE_DERIVE) { |
| 488 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 489 | input1, input1_length, |
| 490 | input2, input2_length, |
| 491 | capacity)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 492 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 496 | output, |
| 497 | capacity)); |
| 498 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 502 | |
| 503 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* We need two keys to exercise key agreement. Exercise the |
| 508 | * private key against its own public key. */ |
| 509 | psa_status_t mbedtls_test_psa_key_agreement_with_self( |
| 510 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 512 | { |
| 513 | psa_key_type_t private_key_type; |
| 514 | psa_key_type_t public_key_type; |
| 515 | size_t key_bits; |
| 516 | uint8_t *public_key = NULL; |
| 517 | size_t public_key_length; |
| 518 | /* Return GENERIC_ERROR if something other than the final call to |
| 519 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 520 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 521 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 522 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 525 | private_key_type = psa_get_key_type(&attributes); |
| 526 | key_bits = psa_get_key_bits(&attributes); |
| 527 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 528 | 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] | 529 | TEST_CALLOC(public_key, public_key_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length, |
| 531 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 532 | |
| 533 | status = psa_key_derivation_key_agreement( |
| 534 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | public_key, public_key_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 536 | exit: |
| 537 | /* |
| 538 | * Key attributes may have been returned by psa_get_key_attributes() |
| 539 | * thus reset them as required. |
| 540 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | mbedtls_free(public_key); |
| 544 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* We need two keys to exercise key agreement. Exercise the |
| 548 | * private key against its own public key. */ |
| 549 | psa_status_t mbedtls_test_psa_raw_key_agreement_with_self( |
| 550 | psa_algorithm_t alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 552 | { |
| 553 | psa_key_type_t private_key_type; |
| 554 | psa_key_type_t public_key_type; |
| 555 | size_t key_bits; |
| 556 | uint8_t *public_key = NULL; |
| 557 | size_t public_key_length; |
| 558 | uint8_t output[1024]; |
| 559 | size_t output_length; |
| 560 | /* Return GENERIC_ERROR if something other than the final call to |
| 561 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 562 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 563 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 564 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 567 | private_key_type = psa_get_key_type(&attributes); |
| 568 | key_bits = psa_get_key_bits(&attributes); |
| 569 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 570 | 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] | 571 | TEST_CALLOC(public_key, public_key_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | PSA_ASSERT(psa_export_public_key(key, |
| 573 | public_key, public_key_length, |
| 574 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | status = psa_raw_key_agreement(alg, key, |
| 577 | public_key, public_key_length, |
| 578 | output, sizeof(output), &output_length); |
| 579 | if (status == PSA_SUCCESS) { |
| 580 | TEST_ASSERT(output_length <= |
| 581 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(private_key_type, |
| 582 | key_bits)); |
| 583 | TEST_ASSERT(output_length <= |
| 584 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 587 | exit: |
| 588 | /* |
| 589 | * Key attributes may have been returned by psa_get_key_attributes() |
| 590 | * thus reset them as required. |
| 591 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | mbedtls_free(public_key); |
| 595 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key, |
| 599 | psa_key_usage_t usage, |
| 600 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 601 | { |
| 602 | int ok = 0; |
| 603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 605 | /* We need two keys to exercise key agreement. Exercise the |
| 606 | * private key against its own public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 608 | } |
| 609 | ok = 1; |
| 610 | |
| 611 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | static int exercise_key_agreement_key(mbedtls_svc_key_id_t key, |
| 616 | psa_key_usage_t usage, |
| 617 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 618 | { |
| 619 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gabor Mezei | dc3f3bb | 2022-07-01 15:06:34 +0200 | [diff] [blame] | 620 | unsigned char input[1] = { 0 }; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 621 | unsigned char output[1]; |
| 622 | int ok = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 624 | psa_status_t expected_key_agreement_status = PSA_SUCCESS; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 627 | /* We need two keys to exercise key agreement. Exercise the |
| 628 | * private key against its own public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 630 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 631 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 632 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 633 | &operation, PSA_KEY_DERIVATION_INPUT_SEED, |
| 634 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 638 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 639 | &operation, PSA_KEY_DERIVATION_INPUT_SALT, |
| 640 | input, sizeof(input))); |
Przemek Stekiel | d898745 | 2022-06-14 11:41:52 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 643 | /* For HKDF_EXPAND input secret may fail as secret size may not match |
| 644 | to expected PRK size. In practice it means that key bits must match |
| 645 | hash length. Otherwise test should fail with INVALID_ARGUMENT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 647 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 649 | size_t key_bits = psa_get_key_bits(&attributes); |
| 650 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | if (PSA_BITS_TO_BYTES(key_bits) != PSA_HASH_LENGTH(hash_alg)) { |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 653 | expected_key_agreement_status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | } |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(&operation, key), |
| 658 | expected_key_agreement_status); |
Przemek Stekiel | 6c9fd61 | 2022-06-14 14:41:42 +0200 | [diff] [blame] | 659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if (expected_key_agreement_status != PSA_SUCCESS) { |
| 661 | return 1; |
| 662 | } |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 665 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 666 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 667 | &operation, PSA_KEY_DERIVATION_INPUT_LABEL, |
| 668 | input, sizeof(input))); |
| 669 | } else if (PSA_ALG_IS_HKDF(kdf_alg) || PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 670 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 671 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 672 | input, sizeof(input))); |
Gilles Peskine | aa3449d | 2022-03-19 16:04:30 +0100 | [diff] [blame] | 673 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 675 | output, |
| 676 | sizeof(output))); |
| 677 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 678 | } |
| 679 | ok = 1; |
| 680 | |
| 681 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | int mbedtls_test_psa_exported_key_sanity_check( |
| 686 | psa_key_type_t type, size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | const uint8_t *exported, size_t exported_length) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 688 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits)); |
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 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) { |
| 692 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
| 693 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 694 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 695 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 697 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 698 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 699 | size_t len; |
| 700 | /* RSAPrivateKey ::= SEQUENCE { |
| 701 | * version INTEGER, -- must be 0 |
| 702 | * modulus INTEGER, -- n |
| 703 | * publicExponent INTEGER, -- e |
| 704 | * privateExponent INTEGER, -- d |
| 705 | * prime1 INTEGER, -- p |
| 706 | * prime2 INTEGER, -- q |
| 707 | * exponent1 INTEGER, -- d mod (p-1) |
| 708 | * exponent2 INTEGER, -- d mod (q-1) |
| 709 | * coefficient INTEGER, -- (inverse of q) mod p |
| 710 | * } |
| 711 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 713 | MBEDTLS_ASN1_SEQUENCE | |
| 714 | MBEDTLS_ASN1_CONSTRUCTED), 0); |
| 715 | TEST_EQUAL(len, end - p); |
| 716 | if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 717 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | } |
| 719 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 720 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | } |
| 722 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 723 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 725 | /* Require d to be at least half the size of n. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 726 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 727 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 729 | /* 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] | 730 | 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] | 731 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | } |
| 733 | 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] | 734 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | } |
| 736 | 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] | 737 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | } |
| 739 | 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] | 740 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | } |
| 742 | 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] | 743 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | } |
| 745 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 748 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 749 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 752 | /* Just the secret value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 756 | } else |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 757 | |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 758 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 760 | uint8_t *p = (uint8_t *) exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 761 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 762 | size_t len; |
| 763 | /* RSAPublicKey ::= SEQUENCE { |
| 764 | * modulus INTEGER, -- n |
| 765 | * publicExponent INTEGER } -- e |
| 766 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 768 | MBEDTLS_ASN1_SEQUENCE | |
| 769 | MBEDTLS_ASN1_CONSTRUCTED), |
| 770 | 0); |
| 771 | TEST_EQUAL(len, end - p); |
| 772 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 773 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | } |
| 775 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 776 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | } |
| 778 | TEST_EQUAL(p - end, 0); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 779 | |
| 780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | TEST_ASSERT(exported_length <= |
| 782 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 783 | TEST_ASSERT(exported_length <= |
| 784 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 785 | } else |
Ronald Cron | 64df738 | 2021-07-06 09:23:06 +0200 | [diff] [blame] | 786 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) { |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | TEST_ASSERT(exported_length <= |
| 791 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 792 | TEST_ASSERT(exported_length <= |
| 793 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 796 | /* The representation of an ECC Montgomery public key is |
| 797 | * the raw compressed point */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 798 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length); |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 799 | } 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] | 800 | /* The representation of an ECC Edwards public key is |
| 801 | * the raw compressed point */ |
Stephan Koch | 6eb7311 | 2023-03-03 17:48:40 +0100 | [diff] [blame] | 802 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits + 1), exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | } else { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 804 | /* The representation of an ECC Weierstrass public key is: |
| 805 | * - The byte 0x04; |
| 806 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 807 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 808 | * - where m is the bit size associated with the curve. |
| 809 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length); |
| 811 | TEST_EQUAL(exported[0], 4); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 812 | } |
Przemek Stekiel | 7cf26df | 2022-12-01 15:09:40 +0100 | [diff] [blame] | 813 | } else |
| 814 | 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] | 815 | TEST_ASSERT(exported_length == |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 816 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 817 | TEST_ASSERT(exported_length <= |
| 818 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Valerio Setti | 2dbc306 | 2023-04-13 12:19:57 +0200 | [diff] [blame] | 819 | } else { |
Andrzej Kurek | 57d2f13 | 2022-01-17 15:26:24 +0100 | [diff] [blame] | 820 | (void) exported; |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 821 | TEST_FAIL("Sanity check not implemented for this key type"); |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 822 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 823 | |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 824 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | if (type == PSA_KEY_TYPE_DES) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 826 | /* Check the parity bits. */ |
| 827 | unsigned i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | for (i = 0; i < bits / 8; i++) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 829 | unsigned bit_count = 0; |
| 830 | unsigned m; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | for (m = 1; m <= 0x100; m <<= 1) { |
| 832 | if (exported[i] & m) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 833 | ++bit_count; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 834 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 835 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | TEST_ASSERT(bit_count % 2 != 0); |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 837 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 838 | } |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 839 | #endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 842 | |
| 843 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | static int exercise_export_key(mbedtls_svc_key_id_t key, |
| 848 | psa_key_usage_t usage) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 849 | { |
| 850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 851 | uint8_t *exported = NULL; |
| 852 | size_t exported_size = 0; |
| 853 | size_t exported_length = 0; |
| 854 | int ok = 0; |
| 855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 857 | |
| 858 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | psa_get_key_type(&attributes), |
| 860 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 861 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | if ((usage & PSA_KEY_USAGE_EXPORT) == 0 && |
| 864 | !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) { |
| 865 | TEST_EQUAL(psa_export_key(key, exported, |
| 866 | exported_size, &exported_length), |
| 867 | PSA_ERROR_NOT_PERMITTED); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 868 | ok = 1; |
| 869 | goto exit; |
| 870 | } |
| 871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 872 | PSA_ASSERT(psa_export_key(key, |
| 873 | exported, exported_size, |
| 874 | &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 875 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes), |
| 877 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 878 | |
| 879 | exit: |
| 880 | /* |
| 881 | * Key attributes may have been returned by psa_get_key_attributes() |
| 882 | * thus reset them as required. |
| 883 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | mbedtls_free(exported); |
| 887 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | static int exercise_export_public_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 891 | { |
| 892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 893 | psa_key_type_t public_type; |
| 894 | uint8_t *exported = NULL; |
| 895 | size_t exported_size = 0; |
| 896 | size_t exported_length = 0; |
| 897 | int ok = 0; |
| 898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 900 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 901 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | psa_get_key_type(&attributes), |
| 903 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 904 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | TEST_EQUAL(psa_export_public_key(key, exported, |
| 907 | exported_size, &exported_length), |
| 908 | PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 909 | ok = 1; |
| 910 | goto exit; |
| 911 | } |
| 912 | |
| 913 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 914 | psa_get_key_type(&attributes)); |
| 915 | exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, |
| 916 | psa_get_key_bits(&attributes)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 917 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | PSA_ASSERT(psa_export_public_key(key, |
| 920 | exported, exported_size, |
| 921 | &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 922 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | public_type, psa_get_key_bits(&attributes), |
| 924 | exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 925 | |
| 926 | exit: |
| 927 | /* |
| 928 | * Key attributes may have been returned by psa_get_key_attributes() |
| 929 | * thus reset them as required. |
| 930 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | mbedtls_free(exported); |
| 934 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 937 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 938 | psa_key_usage_t usage, |
| 939 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 940 | { |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 941 | int ok = 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | if (!check_key_attributes_sanity(key)) { |
| 944 | return 0; |
| 945 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | if (alg == 0) { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 948 | ok = 1; /* If no algorithm, do nothing (used for raw data "keys"). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | } else if (PSA_ALG_IS_MAC(alg)) { |
| 950 | ok = exercise_mac_key(key, usage, alg); |
| 951 | } else if (PSA_ALG_IS_CIPHER(alg)) { |
| 952 | ok = exercise_cipher_key(key, usage, alg); |
| 953 | } else if (PSA_ALG_IS_AEAD(alg)) { |
| 954 | ok = exercise_aead_key(key, usage, alg); |
| 955 | } else if (PSA_ALG_IS_SIGN(alg)) { |
| 956 | ok = exercise_signature_key(key, usage, alg); |
| 957 | } else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 958 | ok = exercise_asymmetric_encryption_key(key, usage, alg); |
| 959 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 960 | ok = exercise_key_derivation_key(key, usage, alg); |
| 961 | } else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 962 | ok = exercise_raw_key_agreement_key(key, usage, alg); |
| 963 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 964 | ok = exercise_key_agreement_key(key, usage, alg); |
| 965 | } else { |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 966 | TEST_FAIL("No code to exercise this category of algorithm"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | ok = ok && exercise_export_key(key, usage); |
| 970 | ok = ok && exercise_export_public_key(key); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 971 | |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 972 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 974 | } |
| 975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 977 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 978 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) { |
| 980 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 981 | if (PSA_ALG_SIGN_GET_HASH(alg)) { |
| 982 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 983 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 984 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | |
| 985 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 986 | } |
| 987 | } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 988 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 989 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 990 | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE; |
gabor-mezei-arm | 041887b | 2021-05-11 13:29:24 +0200 | [diff] [blame] | 991 | } |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 994 | PSA_KEY_USAGE_VERIFY_HASH : |
| 995 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; |
| 996 | } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) || |
| 997 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 998 | return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 999 | PSA_KEY_USAGE_ENCRYPT : |
| 1000 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 1001 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg) || |
| 1002 | PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 1003 | return PSA_KEY_USAGE_DERIVE; |
| 1004 | } else { |
| 1005 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | } |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 1009 | |
| 1010 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |