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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # include <mbedtls/asn1.h> |
| 29 | # include <psa/crypto.h> |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 30 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 31 | # include <test/asn1_helpers.h> |
| 32 | # include <test/psa_crypto_helpers.h> |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 33 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 34 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 35 | static int lifetime_is_dynamic_secure_element(psa_key_lifetime_t lifetime) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 36 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 37 | return (PSA_KEY_LIFETIME_GET_LOCATION(lifetime) != |
| 38 | PSA_KEY_LOCATION_LOCAL_STORAGE); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 39 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 40 | # endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 41 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 42 | static int check_key_attributes_sanity(mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 43 | { |
| 44 | int ok = 0; |
| 45 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 46 | psa_key_lifetime_t lifetime; |
| 47 | mbedtls_svc_key_id_t id; |
| 48 | psa_key_type_t type; |
Gilles Peskine | 6b362e6 | 2021-02-15 12:03:16 +0100 | [diff] [blame] | 49 | size_t bits; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 50 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 51 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 52 | lifetime = psa_get_key_lifetime(&attributes); |
| 53 | id = psa_get_key_id(&attributes); |
| 54 | type = psa_get_key_type(&attributes); |
| 55 | bits = psa_get_key_bits(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 56 | |
| 57 | /* Persistence */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 58 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 59 | TEST_ASSERT( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 60 | (PSA_KEY_ID_VOLATILE_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) && |
| 61 | (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) <= PSA_KEY_ID_VOLATILE_MAX)); |
| 62 | } else { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 63 | TEST_ASSERT( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | (PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) && |
| 65 | (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] | 66 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 67 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 68 | /* randomly-generated 64-bit constant, should never appear in test data */ |
| 69 | psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | psa_status_t status = psa_get_key_slot_number(&attributes, &slot_number); |
| 71 | if (lifetime_is_dynamic_secure_element(lifetime)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 72 | /* Mbed Crypto currently always exposes the slot number to |
| 73 | * applications. This is not mandated by the PSA specification |
| 74 | * and may change in future versions. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 75 | TEST_EQUAL(status, 0); |
| 76 | TEST_ASSERT(slot_number != 0xec94d4a5058a1a21); |
| 77 | } else { |
| 78 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 79 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 80 | # endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 81 | |
| 82 | /* Type and size */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 83 | TEST_ASSERT(type != 0); |
| 84 | TEST_ASSERT(bits != 0); |
| 85 | TEST_ASSERT(bits <= PSA_MAX_KEY_BITS); |
| 86 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) |
| 87 | TEST_ASSERT(bits % 8 == 0); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 88 | |
| 89 | /* MAX macros concerning specific key types */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 90 | if (PSA_KEY_TYPE_IS_ECC(type)) |
| 91 | TEST_ASSERT(bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS); |
| 92 | else if (PSA_KEY_TYPE_IS_RSA(type)) |
| 93 | TEST_ASSERT(bits <= PSA_VENDOR_RSA_MAX_KEY_BITS); |
| 94 | TEST_ASSERT(PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) <= |
| 95 | PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 96 | |
| 97 | ok = 1; |
| 98 | |
| 99 | exit: |
| 100 | /* |
| 101 | * Key attributes may have been returned by psa_get_key_attributes() |
| 102 | * thus reset them as required. |
| 103 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 106 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 109 | static int exercise_mac_key(mbedtls_svc_key_id_t key, |
| 110 | psa_key_usage_t usage, |
| 111 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 112 | { |
| 113 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 114 | const unsigned char input[] = "foo"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 115 | unsigned char mac[PSA_MAC_MAX_SIZE] = { 0 }; |
| 116 | size_t mac_length = sizeof(mac); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 117 | |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 118 | /* Convert wildcard algorithm to exercisable algorithm */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 119 | if (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) { |
| 120 | alg = PSA_ALG_TRUNCATED_MAC(alg, PSA_MAC_TRUNCATED_LENGTH(alg)); |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 123 | if (usage & PSA_KEY_USAGE_SIGN_HASH) { |
| 124 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 125 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 126 | PSA_ASSERT( |
| 127 | psa_mac_sign_finish(&operation, mac, sizeof(mac), &mac_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 130 | if (usage & PSA_KEY_USAGE_VERIFY_HASH) { |
| 131 | psa_status_t verify_status = (usage & PSA_KEY_USAGE_SIGN_HASH ? |
| 132 | PSA_SUCCESS : |
| 133 | PSA_ERROR_INVALID_SIGNATURE); |
| 134 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 135 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 136 | TEST_EQUAL(psa_mac_verify_finish(&operation, mac, mac_length), |
| 137 | verify_status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 140 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 141 | |
| 142 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 143 | psa_mac_abort(&operation); |
| 144 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 147 | static int exercise_cipher_key(mbedtls_svc_key_id_t key, |
| 148 | psa_key_usage_t usage, |
| 149 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 150 | { |
| 151 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 152 | unsigned char iv[16] = { 0 }; |
| 153 | size_t iv_length = sizeof(iv); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 154 | const unsigned char plaintext[16] = "Hello, world..."; |
| 155 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 156 | size_t ciphertext_length = sizeof(ciphertext); |
| 157 | unsigned char decrypted[sizeof(ciphertext)]; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 158 | size_t part_length; |
| 159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 160 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
| 161 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 162 | PSA_ASSERT( |
| 163 | psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_length)); |
| 164 | PSA_ASSERT(psa_cipher_update(&operation, plaintext, sizeof(plaintext), |
| 165 | ciphertext, sizeof(ciphertext), |
| 166 | &ciphertext_length)); |
| 167 | PSA_ASSERT(psa_cipher_finish(&operation, ciphertext + ciphertext_length, |
| 168 | sizeof(ciphertext) - ciphertext_length, |
| 169 | &part_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 170 | ciphertext_length += part_length; |
| 171 | } |
| 172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 173 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 174 | psa_status_t status; |
| 175 | int maybe_invalid_padding = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 176 | if (!(usage & PSA_KEY_USAGE_ENCRYPT)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 178 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 179 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 180 | * have this macro yet. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 181 | iv_length = |
| 182 | PSA_BLOCK_CIPHER_BLOCK_LENGTH(psa_get_key_type(&attributes)); |
| 183 | maybe_invalid_padding = !PSA_ALG_IS_STREAM_CIPHER(alg); |
| 184 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 185 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 186 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 187 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv, iv_length)); |
| 188 | PSA_ASSERT(psa_cipher_update(&operation, ciphertext, ciphertext_length, |
| 189 | decrypted, sizeof(decrypted), |
| 190 | &part_length)); |
| 191 | status = psa_cipher_finish(&operation, decrypted + part_length, |
| 192 | sizeof(decrypted) - part_length, |
| 193 | &part_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 194 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 195 | * if the input is some aribtrary data rather than an actual |
| 196 | ciphertext, a padding error is likely. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 197 | if (maybe_invalid_padding) |
| 198 | TEST_ASSERT(status == PSA_SUCCESS || |
| 199 | status == PSA_ERROR_INVALID_PADDING); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 200 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 201 | PSA_ASSERT(status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 204 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 205 | |
| 206 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 207 | psa_cipher_abort(&operation); |
| 208 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 211 | static int exercise_aead_key(mbedtls_svc_key_id_t key, |
| 212 | psa_key_usage_t usage, |
| 213 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 214 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 215 | unsigned char nonce[16] = { 0 }; |
| 216 | size_t nonce_length = sizeof(nonce); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 217 | unsigned char plaintext[16] = "Hello, world..."; |
| 218 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 219 | size_t ciphertext_length = sizeof(ciphertext); |
| 220 | size_t plaintext_length = sizeof(ciphertext); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 221 | |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 222 | /* Convert wildcard algorithm to exercisable algorithm */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 223 | if (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) { |
| 224 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, |
| 225 | PSA_ALG_AEAD_GET_TAG_LENGTH(alg)); |
Steven Cooreman | fb9cb92 | 2021-02-23 14:37:38 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 228 | /* Default IV length for AES-GCM is 12 bytes */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 229 | if (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0) == |
| 230 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 231 | nonce_length = 12; |
| 232 | } |
| 233 | |
Steven Cooreman | aaec341 | 2021-02-18 13:30:34 +0100 | [diff] [blame] | 234 | /* IV length for CCM needs to be between 7 and 13 bytes */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 235 | if (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0) == |
| 236 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0)) { |
Steven Cooreman | aaec341 | 2021-02-18 13:30:34 +0100 | [diff] [blame] | 237 | nonce_length = 12; |
| 238 | } |
| 239 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 240 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
| 241 | PSA_ASSERT(psa_aead_encrypt(key, alg, nonce, nonce_length, NULL, 0, |
| 242 | plaintext, sizeof(plaintext), ciphertext, |
| 243 | sizeof(ciphertext), &ciphertext_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 246 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
| 247 | psa_status_t verify_status = (usage & PSA_KEY_USAGE_ENCRYPT ? |
| 248 | PSA_SUCCESS : |
| 249 | PSA_ERROR_INVALID_SIGNATURE); |
| 250 | TEST_EQUAL(psa_aead_decrypt(key, alg, nonce, nonce_length, NULL, 0, |
| 251 | ciphertext, ciphertext_length, plaintext, |
| 252 | sizeof(plaintext), &plaintext_length), |
| 253 | verify_status); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 256 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 257 | |
| 258 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 259 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 262 | static int exercise_signature_key(mbedtls_svc_key_id_t key, |
| 263 | psa_key_usage_t usage, |
| 264 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 265 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 266 | if (usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH)) { |
| 267 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 268 | size_t payload_length = 16; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 269 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
| 270 | size_t signature_length = sizeof(signature); |
| 271 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 272 | |
| 273 | /* If the policy allows signing with any hash, just pick one. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 274 | if (PSA_ALG_IS_HASH_AND_SIGN(alg) && hash_alg == PSA_ALG_ANY_HASH) { |
| 275 | # if defined(KNOWN_SUPPORTED_HASH_ALG) |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 276 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 277 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 278 | # else |
| 279 | TEST_ASSERT(!"No hash algorithm for hash-and-sign testing"); |
| 280 | # endif |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Janos Follath | 4c0b60e | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 283 | /* Some algorithms require the payload to have the size of |
| 284 | * the hash encoded in the algorithm. Use this input size |
| 285 | * even for algorithms that allow other input sizes. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 286 | if (hash_alg != 0) |
| 287 | payload_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 4c0b60e | 2021-06-14 12:34:30 +0100 | [diff] [blame] | 288 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 289 | if (usage & PSA_KEY_USAGE_SIGN_HASH) { |
| 290 | PSA_ASSERT(psa_sign_hash(key, alg, payload, payload_length, |
| 291 | signature, sizeof(signature), |
| 292 | &signature_length)); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 295 | if (usage & PSA_KEY_USAGE_VERIFY_HASH) { |
| 296 | psa_status_t verify_status = (usage & PSA_KEY_USAGE_SIGN_HASH ? |
| 297 | PSA_SUCCESS : |
| 298 | PSA_ERROR_INVALID_SIGNATURE); |
| 299 | TEST_EQUAL(psa_verify_hash(key, alg, payload, payload_length, |
| 300 | signature, signature_length), |
| 301 | verify_status); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 302 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 305 | if (usage & (PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE)) { |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 306 | unsigned char message[256] = "Hello, world..."; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 307 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 308 | size_t message_length = 16; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 309 | size_t signature_length = sizeof(signature); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 310 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 311 | if (usage & PSA_KEY_USAGE_SIGN_MESSAGE) { |
| 312 | PSA_ASSERT(psa_sign_message(key, alg, message, message_length, |
| 313 | signature, sizeof(signature), |
| 314 | &signature_length)); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 317 | if (usage & PSA_KEY_USAGE_VERIFY_MESSAGE) { |
| 318 | psa_status_t verify_status = (usage & PSA_KEY_USAGE_SIGN_MESSAGE ? |
| 319 | PSA_SUCCESS : |
| 320 | PSA_ERROR_INVALID_SIGNATURE); |
| 321 | TEST_EQUAL(psa_verify_message(key, alg, message, message_length, |
| 322 | signature, signature_length), |
| 323 | verify_status); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 324 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 327 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 328 | |
| 329 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 330 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 333 | static int exercise_asymmetric_encryption_key(mbedtls_svc_key_id_t key, |
| 334 | psa_key_usage_t usage, |
| 335 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 336 | { |
| 337 | unsigned char plaintext[256] = "Hello, world..."; |
| 338 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 339 | size_t ciphertext_length = sizeof(ciphertext); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 340 | size_t plaintext_length = 16; |
| 341 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 342 | if (usage & PSA_KEY_USAGE_ENCRYPT) { |
| 343 | PSA_ASSERT(psa_asymmetric_encrypt( |
| 344 | key, alg, plaintext, plaintext_length, NULL, 0, ciphertext, |
| 345 | sizeof(ciphertext), &ciphertext_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 348 | if (usage & PSA_KEY_USAGE_DECRYPT) { |
| 349 | psa_status_t status = psa_asymmetric_decrypt( |
| 350 | key, alg, ciphertext, ciphertext_length, NULL, 0, plaintext, |
| 351 | sizeof(plaintext), &plaintext_length); |
| 352 | TEST_ASSERT(status == PSA_SUCCESS || |
| 353 | ((usage & PSA_KEY_USAGE_ENCRYPT) == 0 && |
| 354 | (status == PSA_ERROR_INVALID_ARGUMENT || |
| 355 | status == PSA_ERROR_INVALID_PADDING))); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 358 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 359 | |
| 360 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 361 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int mbedtls_test_psa_setup_key_derivation_wrap( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 365 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 366 | mbedtls_svc_key_id_t key, |
| 367 | psa_algorithm_t alg, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 368 | const unsigned char *input1, |
| 369 | size_t input1_length, |
| 370 | const unsigned char *input2, |
| 371 | size_t input2_length, |
| 372 | size_t capacity) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 373 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 374 | PSA_ASSERT(psa_key_derivation_setup(operation, alg)); |
| 375 | if (PSA_ALG_IS_HKDF(alg)) { |
| 376 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 377 | operation, PSA_KEY_DERIVATION_INPUT_SALT, input1, input1_length)); |
| 378 | PSA_ASSERT(psa_key_derivation_input_key( |
| 379 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key)); |
| 380 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 381 | operation, PSA_KEY_DERIVATION_INPUT_INFO, input2, input2_length)); |
| 382 | } else if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 383 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 384 | operation, PSA_KEY_DERIVATION_INPUT_SEED, input1, input1_length)); |
| 385 | PSA_ASSERT(psa_key_derivation_input_key( |
| 386 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key)); |
| 387 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 388 | operation, PSA_KEY_DERIVATION_INPUT_LABEL, input2, input2_length)); |
| 389 | } else { |
| 390 | TEST_ASSERT(!"Key derivation algorithm not supported"); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 393 | if (capacity != SIZE_MAX) |
| 394 | PSA_ASSERT(psa_key_derivation_set_capacity(operation, capacity)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 395 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 396 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 397 | |
| 398 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 399 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 402 | static int exercise_key_derivation_key(mbedtls_svc_key_id_t key, |
| 403 | psa_key_usage_t usage, |
| 404 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 405 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 406 | psa_key_derivation_operation_t operation = |
| 407 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 408 | unsigned char input1[] = "Input 1"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 409 | size_t input1_length = sizeof(input1); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 410 | unsigned char input2[] = "Input 2"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 411 | size_t input2_length = sizeof(input2); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 412 | unsigned char output[1]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 413 | size_t capacity = sizeof(output); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 414 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 415 | if (usage & PSA_KEY_USAGE_DERIVE) { |
| 416 | if (!mbedtls_test_psa_setup_key_derivation_wrap( |
| 417 | &operation, key, alg, input1, input1_length, input2, |
| 418 | input2_length, capacity)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 419 | goto exit; |
| 420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 421 | PSA_ASSERT( |
| 422 | psa_key_derivation_output_bytes(&operation, output, capacity)); |
| 423 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 426 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 427 | |
| 428 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 429 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* We need two keys to exercise key agreement. Exercise the |
| 433 | * private key against its own public key. */ |
| 434 | psa_status_t mbedtls_test_psa_key_agreement_with_self( |
| 435 | psa_key_derivation_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 436 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 437 | { |
| 438 | psa_key_type_t private_key_type; |
| 439 | psa_key_type_t public_key_type; |
| 440 | size_t key_bits; |
| 441 | uint8_t *public_key = NULL; |
| 442 | size_t public_key_length; |
| 443 | /* Return GENERIC_ERROR if something other than the final call to |
| 444 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 445 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 446 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 449 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 450 | private_key_type = psa_get_key_type(&attributes); |
| 451 | key_bits = psa_get_key_bits(&attributes); |
| 452 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 453 | public_key_length = |
| 454 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
| 455 | ASSERT_ALLOC(public_key, public_key_length); |
| 456 | PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length, |
| 457 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 458 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 459 | status = psa_key_derivation_key_agreement(operation, |
| 460 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 461 | key, public_key, |
| 462 | public_key_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 463 | exit: |
| 464 | /* |
| 465 | * Key attributes may have been returned by psa_get_key_attributes() |
| 466 | * thus reset them as required. |
| 467 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 468 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 470 | mbedtls_free(public_key); |
| 471 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* We need two keys to exercise key agreement. Exercise the |
| 475 | * private key against its own public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 476 | psa_status_t |
| 477 | mbedtls_test_psa_raw_key_agreement_with_self(psa_algorithm_t alg, |
| 478 | mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 479 | { |
| 480 | psa_key_type_t private_key_type; |
| 481 | psa_key_type_t public_key_type; |
| 482 | size_t key_bits; |
| 483 | uint8_t *public_key = NULL; |
| 484 | size_t public_key_length; |
| 485 | uint8_t output[1024]; |
| 486 | size_t output_length; |
| 487 | /* Return GENERIC_ERROR if something other than the final call to |
| 488 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 489 | * but it's good enough: callers will report it as a failed test anyway. */ |
| 490 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 491 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 492 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 493 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 494 | private_key_type = psa_get_key_type(&attributes); |
| 495 | key_bits = psa_get_key_bits(&attributes); |
| 496 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type); |
| 497 | public_key_length = |
| 498 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits); |
| 499 | ASSERT_ALLOC(public_key, public_key_length); |
| 500 | PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length, |
| 501 | &public_key_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 502 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 503 | status = psa_raw_key_agreement(alg, key, public_key, public_key_length, |
| 504 | output, sizeof(output), &output_length); |
| 505 | if (status == PSA_SUCCESS) { |
| 506 | TEST_ASSERT(output_length <= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( |
| 507 | private_key_type, key_bits)); |
| 508 | TEST_ASSERT(output_length <= PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 511 | exit: |
| 512 | /* |
| 513 | * Key attributes may have been returned by psa_get_key_attributes() |
| 514 | * thus reset them as required. |
| 515 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 516 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 517 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 518 | mbedtls_free(public_key); |
| 519 | return status; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 522 | static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key, |
| 523 | psa_key_usage_t usage, |
| 524 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 525 | { |
| 526 | int ok = 0; |
| 527 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 528 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 529 | /* We need two keys to exercise key agreement. Exercise the |
| 530 | * private key against its own public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 531 | PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 532 | } |
| 533 | ok = 1; |
| 534 | |
| 535 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 536 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 539 | static int exercise_key_agreement_key(mbedtls_svc_key_id_t key, |
| 540 | psa_key_usage_t usage, |
| 541 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 542 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 543 | psa_key_derivation_operation_t operation = |
| 544 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 545 | unsigned char output[1]; |
| 546 | int ok = 0; |
| 547 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 548 | if (usage & PSA_KEY_USAGE_DERIVE) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 549 | /* We need two keys to exercise key agreement. Exercise the |
| 550 | * private key against its own public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 551 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 552 | PSA_ASSERT(mbedtls_test_psa_key_agreement_with_self(&operation, key)); |
| 553 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, output, |
| 554 | sizeof(output))); |
| 555 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 556 | } |
| 557 | ok = 1; |
| 558 | |
| 559 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 560 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 563 | int mbedtls_test_psa_exported_key_sanity_check(psa_key_type_t type, |
| 564 | size_t bits, |
| 565 | const uint8_t *exported, |
| 566 | size_t exported_length) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 567 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 568 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 569 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 570 | if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) |
| 571 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 572 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 573 | # if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 574 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 575 | uint8_t *p = (uint8_t *)exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 576 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 577 | size_t len; |
| 578 | /* RSAPrivateKey ::= SEQUENCE { |
| 579 | * version INTEGER, -- must be 0 |
| 580 | * modulus INTEGER, -- n |
| 581 | * publicExponent INTEGER, -- e |
| 582 | * privateExponent INTEGER, -- d |
| 583 | * prime1 INTEGER, -- p |
| 584 | * prime2 INTEGER, -- q |
| 585 | * exponent1 INTEGER, -- d mod (p-1) |
| 586 | * exponent2 INTEGER, -- d mod (q-1) |
| 587 | * coefficient INTEGER, -- (inverse of q) mod p |
| 588 | * } |
| 589 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 590 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 591 | MBEDTLS_ASN1_SEQUENCE | |
| 592 | MBEDTLS_ASN1_CONSTRUCTED), |
| 593 | 0); |
| 594 | TEST_EQUAL(p + len, end); |
| 595 | if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 596 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 597 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 598 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 599 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 600 | goto exit; |
| 601 | /* Require d to be at least half the size of n. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 602 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 603 | goto exit; |
| 604 | /* Require p and q to be at most half the size of n, rounded up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 605 | 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] | 606 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 607 | 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] | 608 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 609 | 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] | 610 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 611 | 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] | 612 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 613 | 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] | 614 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 615 | TEST_EQUAL(p, end); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 616 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 617 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 618 | } else |
| 619 | # endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 620 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 621 | # if defined(MBEDTLS_ECP_C) |
| 622 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 623 | /* Just the secret value */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 624 | TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 625 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 626 | TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 627 | } else |
| 628 | # endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 629 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 630 | # if defined(MBEDTLS_RSA_C) |
| 631 | if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 632 | uint8_t *p = (uint8_t *)exported; |
Gilles Peskine | 5c2665b | 2021-02-14 01:22:56 +0100 | [diff] [blame] | 633 | const uint8_t *end = exported + exported_length; |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 634 | size_t len; |
| 635 | /* RSAPublicKey ::= SEQUENCE { |
| 636 | * modulus INTEGER, -- n |
| 637 | * publicExponent INTEGER } -- e |
| 638 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 639 | TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len, |
| 640 | MBEDTLS_ASN1_SEQUENCE | |
| 641 | MBEDTLS_ASN1_CONSTRUCTED), |
| 642 | 0); |
| 643 | TEST_EQUAL(p + len, end); |
| 644 | if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 645 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 646 | if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 647 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 648 | TEST_EQUAL(p, end); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 649 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 650 | TEST_ASSERT(exported_length <= |
| 651 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 652 | TEST_ASSERT(exported_length <= PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 653 | } else |
| 654 | # endif /* MBEDTLS_RSA_C */ |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 655 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 656 | # if defined(MBEDTLS_ECP_C) |
| 657 | if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 658 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 659 | TEST_ASSERT(exported_length <= |
| 660 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)); |
| 661 | TEST_ASSERT(exported_length <= PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 662 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 663 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 664 | /* The representation of an ECC Montgomery public key is |
| 665 | * the raw compressed point */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 666 | TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length); |
| 667 | } else { |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 668 | /* The representation of an ECC Weierstrass public key is: |
| 669 | * - The byte 0x04; |
| 670 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 671 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 672 | * - where m is the bit size associated with the curve. |
| 673 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 674 | TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length); |
| 675 | TEST_EQUAL(exported[0], 4); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 676 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 677 | } else |
| 678 | # endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 679 | |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 680 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 681 | TEST_ASSERT(!"Sanity check not implemented for this key type"); |
Gilles Peskine | ad557e5 | 2021-02-14 01:19:21 +0100 | [diff] [blame] | 682 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 683 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 684 | # if defined(MBEDTLS_DES_C) |
| 685 | if (type == PSA_KEY_TYPE_DES) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 686 | /* Check the parity bits. */ |
| 687 | unsigned i; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 688 | for (i = 0; i < bits / 8; i++) { |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 689 | unsigned bit_count = 0; |
| 690 | unsigned m; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 691 | for (m = 1; m <= 0x100; m <<= 1) { |
| 692 | if (exported[i] & m) |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 693 | ++bit_count; |
| 694 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 695 | TEST_ASSERT(bit_count % 2 != 0); |
Gilles Peskine | cc9db30 | 2021-02-14 01:29:52 +0100 | [diff] [blame] | 696 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 697 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 698 | # endif |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 699 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 700 | return 1; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 701 | |
| 702 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 703 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 706 | static int exercise_export_key(mbedtls_svc_key_id_t key, psa_key_usage_t usage) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 707 | { |
| 708 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 709 | uint8_t *exported = NULL; |
| 710 | size_t exported_size = 0; |
| 711 | size_t exported_length = 0; |
| 712 | int ok = 0; |
| 713 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 714 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 715 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 716 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(psa_get_key_type(&attributes), |
| 717 | psa_get_key_bits(&attributes)); |
| 718 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 719 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 720 | if ((usage & PSA_KEY_USAGE_EXPORT) == 0 && |
| 721 | !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) { |
| 722 | TEST_EQUAL(psa_export_key(key, exported, exported_size, |
| 723 | &exported_length), |
| 724 | PSA_ERROR_NOT_PERMITTED); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 725 | ok = 1; |
| 726 | goto exit; |
| 727 | } |
| 728 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 729 | PSA_ASSERT(psa_export_key(key, exported, exported_size, &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 730 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 731 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes), exported, |
| 732 | exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 733 | |
| 734 | exit: |
| 735 | /* |
| 736 | * Key attributes may have been returned by psa_get_key_attributes() |
| 737 | * thus reset them as required. |
| 738 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 739 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 740 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 741 | mbedtls_free(exported); |
| 742 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 745 | static int exercise_export_public_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 746 | { |
| 747 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 748 | psa_key_type_t public_type; |
| 749 | uint8_t *exported = NULL; |
| 750 | size_t exported_size = 0; |
| 751 | size_t exported_length = 0; |
| 752 | int ok = 0; |
| 753 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 754 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 755 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) { |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 756 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 757 | psa_get_key_type(&attributes), psa_get_key_bits(&attributes)); |
| 758 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 759 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 760 | TEST_EQUAL(psa_export_public_key(key, exported, exported_size, |
| 761 | &exported_length), |
| 762 | PSA_ERROR_INVALID_ARGUMENT); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 763 | ok = 1; |
| 764 | goto exit; |
| 765 | } |
| 766 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 767 | public_type = |
| 768 | PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_get_key_type(&attributes)); |
| 769 | exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( |
| 770 | public_type, psa_get_key_bits(&attributes)); |
| 771 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 772 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 773 | PSA_ASSERT( |
| 774 | psa_export_public_key(key, exported, exported_size, &exported_length)); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 775 | ok = mbedtls_test_psa_exported_key_sanity_check( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 776 | public_type, psa_get_key_bits(&attributes), exported, exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 777 | |
| 778 | exit: |
| 779 | /* |
| 780 | * Key attributes may have been returned by psa_get_key_attributes() |
| 781 | * thus reset them as required. |
| 782 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 783 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 784 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 785 | mbedtls_free(exported); |
| 786 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 789 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 790 | psa_key_usage_t usage, |
| 791 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 792 | { |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 793 | int ok = 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 794 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 795 | if (!check_key_attributes_sanity(key)) |
| 796 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 797 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 798 | if (alg == 0) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 799 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 800 | else if (PSA_ALG_IS_MAC(alg)) |
| 801 | ok = exercise_mac_key(key, usage, alg); |
| 802 | else if (PSA_ALG_IS_CIPHER(alg)) |
| 803 | ok = exercise_cipher_key(key, usage, alg); |
| 804 | else if (PSA_ALG_IS_AEAD(alg)) |
| 805 | ok = exercise_aead_key(key, usage, alg); |
| 806 | else if (PSA_ALG_IS_SIGN(alg)) |
| 807 | ok = exercise_signature_key(key, usage, alg); |
| 808 | else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) |
| 809 | ok = exercise_asymmetric_encryption_key(key, usage, alg); |
| 810 | else if (PSA_ALG_IS_KEY_DERIVATION(alg)) |
| 811 | ok = exercise_key_derivation_key(key, usage, alg); |
| 812 | else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) |
| 813 | ok = exercise_raw_key_agreement_key(key, usage, alg); |
| 814 | else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) |
| 815 | ok = exercise_key_agreement_key(key, usage, alg); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 816 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 817 | TEST_ASSERT(!"No code to exercise this category of algorithm"); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 818 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 819 | ok = ok && exercise_export_key(key, usage); |
| 820 | ok = ok && exercise_export_public_key(key); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 821 | |
Gilles Peskine | 2385f71 | 2021-02-14 01:34:21 +0100 | [diff] [blame] | 822 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 823 | return ok; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 826 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 827 | psa_algorithm_t alg) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 828 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 829 | if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) { |
| 830 | if (PSA_ALG_IS_HASH_AND_SIGN(alg)) { |
| 831 | if (PSA_ALG_SIGN_GET_HASH(alg)) |
| 832 | return (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 833 | PSA_KEY_USAGE_VERIFY_HASH | |
| 834 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 835 | PSA_KEY_USAGE_SIGN_HASH | |
| 836 | PSA_KEY_USAGE_VERIFY_HASH | |
| 837 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 838 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 839 | } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) |
| 840 | return (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 841 | PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 842 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 843 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
gabor-mezei-arm | 4c6a47a | 2021-04-26 20:12:17 +0200 | [diff] [blame] | 844 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 845 | return (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 846 | PSA_KEY_USAGE_VERIFY_HASH : |
| 847 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 848 | } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) || |
| 849 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) { |
| 850 | return (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ? |
| 851 | PSA_KEY_USAGE_ENCRYPT : |
| 852 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 853 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg) || |
| 854 | PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 855 | return PSA_KEY_USAGE_DERIVE; |
| 856 | } else { |
| 857 | return 0; |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 858 | } |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 859 | } |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 860 | |
| 861 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |