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