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