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