1/* BEGIN_HEADER */ 2 3#include "psa/crypto.h" 4#include "test/psa_crypto_helpers.h" 5 6static int test_equal_status( const char *test, 7 int line_no, const char* filename, 8 psa_status_t value1, 9 psa_status_t value2 ) 10{ 11 if( ( value1 == PSA_ERROR_INVALID_ARGUMENT && 12 value2 == PSA_ERROR_NOT_SUPPORTED ) || 13 ( value1 == PSA_ERROR_NOT_SUPPORTED && 14 value2 == PSA_ERROR_INVALID_ARGUMENT ) ) 15 { 16 return( 1 ); 17 } 18 return( mbedtls_test_equal( test, line_no, filename, value1, value2 ) ); 19} 20 21/** Like #TEST_EQUAL, but expects #psa_status_t values and treats 22 * #PSA_ERROR_INVALID_ARGUMENT and #PSA_ERROR_NOT_SUPPORTED as 23 * interchangeable. 24 * 25 * This test suite currently allows NOT_SUPPORTED and INVALID_ARGUMENT 26 * to be interchangeable in places where the library's behavior does not 27 * match the strict expectations of the test case generator. In the long 28 * run, it would be better to clarify the expectations and reconcile the 29 * library and the test case generator. 30 */ 31#define TEST_STATUS( expr1, expr2 ) \ 32 do { \ 33 if( ! test_equal_status( #expr1 " == " #expr2, __LINE__, __FILE__, \ 34 expr1, expr2 ) ) \ 35 goto exit; \ 36 } while( 0 ) 37 38/* END_HEADER */ 39 40/* BEGIN_DEPENDENCIES 41 * depends_on:MBEDTLS_PSA_CRYPTO_C 42 * END_DEPENDENCIES 43 */ 44 45/* BEGIN_CASE */ 46void hash_fail( int alg_arg, int expected_status_arg ) 47{ 48 psa_status_t expected_status = expected_status_arg; 49 psa_algorithm_t alg = alg_arg; 50 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; 51 uint8_t input[1] = {'A'}; 52 uint8_t output[PSA_HASH_MAX_SIZE] = {0}; 53 size_t length = SIZE_MAX; 54 55 PSA_INIT( ); 56 57 TEST_EQUAL( expected_status, 58 psa_hash_setup( &operation, alg ) ); 59 TEST_EQUAL( expected_status, 60 psa_hash_compute( alg, input, sizeof( input ), 61 output, sizeof( output ), &length ) ); 62 TEST_EQUAL( expected_status, 63 psa_hash_compare( alg, input, sizeof( input ), 64 output, sizeof( output ) ) ); 65 66exit: 67 psa_hash_abort( &operation ); 68 PSA_DONE( ); 69} 70/* END_CASE */ 71 72/* BEGIN_CASE */ 73void mac_fail( int key_type_arg, data_t *key_data, 74 int alg_arg, int expected_status_arg ) 75{ 76 psa_status_t expected_status = expected_status_arg; 77 psa_key_type_t key_type = key_type_arg; 78 psa_algorithm_t alg = alg_arg; 79 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; 80 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 81 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 82 uint8_t input[1] = {'A'}; 83 uint8_t output[PSA_MAC_MAX_SIZE] = {0}; 84 size_t length = SIZE_MAX; 85 86 PSA_INIT( ); 87 88 psa_set_key_type( &attributes, key_type ); 89 psa_set_key_usage_flags( &attributes, 90 PSA_KEY_USAGE_SIGN_HASH | 91 PSA_KEY_USAGE_VERIFY_HASH ); 92 psa_set_key_algorithm( &attributes, alg ); 93 PSA_ASSERT( psa_import_key( &attributes, 94 key_data->x, key_data->len, 95 &key_id ) ); 96 97 TEST_STATUS( expected_status, 98 psa_mac_sign_setup( &operation, key_id, alg ) ); 99 TEST_STATUS( expected_status, 100 psa_mac_verify_setup( &operation, key_id, alg ) ); 101 TEST_STATUS( expected_status, 102 psa_mac_compute( key_id, alg, 103 input, sizeof( input ), 104 output, sizeof( output ), &length ) ); 105 TEST_STATUS( expected_status, 106 psa_mac_verify( key_id, alg, 107 input, sizeof( input ), 108 output, sizeof( output ) ) ); 109 110exit: 111 psa_mac_abort( &operation ); 112 psa_destroy_key( key_id ); 113 psa_reset_key_attributes( &attributes ); 114 PSA_DONE( ); 115} 116/* END_CASE */ 117 118/* BEGIN_CASE */ 119void cipher_fail( int key_type_arg, data_t *key_data, 120 int alg_arg, int expected_status_arg ) 121{ 122 psa_status_t expected_status = expected_status_arg; 123 psa_key_type_t key_type = key_type_arg; 124 psa_algorithm_t alg = alg_arg; 125 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 127 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 128 uint8_t input[1] = {'A'}; 129 uint8_t output[64] = {0}; 130 size_t length = SIZE_MAX; 131 132 PSA_INIT( ); 133 134 psa_set_key_type( &attributes, key_type ); 135 psa_set_key_usage_flags( &attributes, 136 PSA_KEY_USAGE_ENCRYPT | 137 PSA_KEY_USAGE_DECRYPT ); 138 psa_set_key_algorithm( &attributes, alg ); 139 PSA_ASSERT( psa_import_key( &attributes, 140 key_data->x, key_data->len, 141 &key_id ) ); 142 143 TEST_STATUS( expected_status, 144 psa_cipher_encrypt_setup( &operation, key_id, alg ) ); 145 TEST_STATUS( expected_status, 146 psa_cipher_decrypt_setup( &operation, key_id, alg ) ); 147 TEST_STATUS( expected_status, 148 psa_cipher_encrypt( key_id, alg, 149 input, sizeof( input ), 150 output, sizeof( output ), &length ) ); 151 TEST_STATUS( expected_status, 152 psa_cipher_decrypt( key_id, alg, 153 input, sizeof( input ), 154 output, sizeof( output ), &length ) ); 155 156exit: 157 psa_cipher_abort( &operation ); 158 psa_destroy_key( key_id ); 159 psa_reset_key_attributes( &attributes ); 160 PSA_DONE( ); 161} 162/* END_CASE */ 163 164/* BEGIN_CASE */ 165void aead_fail( int key_type_arg, data_t *key_data, 166 int alg_arg, int expected_status_arg ) 167{ 168 psa_status_t expected_status = expected_status_arg; 169 psa_key_type_t key_type = key_type_arg; 170 psa_algorithm_t alg = alg_arg; 171 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; 172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 173 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 174 uint8_t input[16] = "ABCDEFGHIJKLMNO"; 175 uint8_t output[64] = {0}; 176 size_t length = SIZE_MAX; 177 178 PSA_INIT( ); 179 180 psa_set_key_type( &attributes, key_type ); 181 psa_set_key_usage_flags( &attributes, 182 PSA_KEY_USAGE_ENCRYPT | 183 PSA_KEY_USAGE_DECRYPT ); 184 psa_set_key_algorithm( &attributes, alg ); 185 PSA_ASSERT( psa_import_key( &attributes, 186 key_data->x, key_data->len, 187 &key_id ) ); 188 189 TEST_STATUS( expected_status, 190 psa_aead_encrypt_setup( &operation, key_id, alg ) ); 191 TEST_STATUS( expected_status, 192 psa_aead_decrypt_setup( &operation, key_id, alg ) ); 193 TEST_STATUS( expected_status, 194 psa_aead_encrypt( key_id, alg, 195 input, sizeof( input ), 196 NULL, 0, input, sizeof( input ), 197 output, sizeof( output ), &length ) ); 198 TEST_STATUS( expected_status, 199 psa_aead_decrypt( key_id, alg, 200 input, sizeof( input ), 201 NULL, 0, input, sizeof( input ), 202 output, sizeof( output ), &length ) ); 203 204exit: 205 psa_aead_abort( &operation ); 206 psa_destroy_key( key_id ); 207 psa_reset_key_attributes( &attributes ); 208 PSA_DONE( ); 209} 210/* END_CASE */ 211 212/* BEGIN_CASE */ 213void sign_fail( int key_type_arg, data_t *key_data, 214 int alg_arg, int private_only, 215 int expected_status_arg ) 216{ 217 psa_status_t expected_status = expected_status_arg; 218 psa_key_type_t key_type = key_type_arg; 219 psa_algorithm_t alg = alg_arg; 220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 221 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 222 uint8_t input[1] = {'A'}; 223 uint8_t output[PSA_SIGNATURE_MAX_SIZE] = {0}; 224 size_t length = SIZE_MAX; 225 226 PSA_INIT( ); 227 228 psa_set_key_type( &attributes, key_type ); 229 psa_set_key_usage_flags( &attributes, 230 PSA_KEY_USAGE_SIGN_HASH | 231 PSA_KEY_USAGE_VERIFY_HASH ); 232 psa_set_key_algorithm( &attributes, alg ); 233 PSA_ASSERT( psa_import_key( &attributes, 234 key_data->x, key_data->len, 235 &key_id ) ); 236 237 TEST_STATUS( expected_status, 238 psa_sign_hash( key_id, alg, 239 input, sizeof( input ), 240 output, sizeof( output ), &length ) ); 241 if( ! private_only ) 242 { 243 /* Determine a plausible signature size to avoid an INVALID_SIGNATURE 244 * error based on this. */ 245 PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) ); 246 size_t key_bits = psa_get_key_bits( &attributes ); 247 size_t output_length = sizeof( output ); 248 if( PSA_KEY_TYPE_IS_RSA( key_type ) ) 249 output_length = PSA_BITS_TO_BYTES( key_bits ); 250 else if( PSA_KEY_TYPE_IS_ECC( key_type ) ) 251 output_length = 2 * PSA_BITS_TO_BYTES( key_bits ); 252 TEST_ASSERT( output_length <= sizeof( output ) ); 253 TEST_STATUS( expected_status, 254 psa_verify_hash( key_id, alg, 255 input, sizeof( input ), 256 output, output_length ) ); 257 } 258 259exit: 260 psa_destroy_key( key_id ); 261 psa_reset_key_attributes( &attributes ); 262 PSA_DONE( ); 263} 264/* END_CASE */ 265 266/* BEGIN_CASE */ 267void asymmetric_encryption_fail( int key_type_arg, data_t *key_data, 268 int alg_arg, int private_only, 269 int expected_status_arg ) 270{ 271 psa_status_t expected_status = expected_status_arg; 272 psa_key_type_t key_type = key_type_arg; 273 psa_algorithm_t alg = alg_arg; 274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 275 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 276 uint8_t plaintext[PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE] = {0}; 277 uint8_t ciphertext[PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE] = {0}; 278 size_t length = SIZE_MAX; 279 280 PSA_INIT( ); 281 282 psa_set_key_type( &attributes, key_type ); 283 psa_set_key_usage_flags( &attributes, 284 PSA_KEY_USAGE_ENCRYPT | 285 PSA_KEY_USAGE_DECRYPT ); 286 psa_set_key_algorithm( &attributes, alg ); 287 PSA_ASSERT( psa_import_key( &attributes, 288 key_data->x, key_data->len, 289 &key_id ) ); 290 291 if( ! private_only ) 292 { 293 TEST_STATUS( expected_status, 294 psa_asymmetric_encrypt( key_id, alg, 295 plaintext, 1, 296 NULL, 0, 297 ciphertext, sizeof( ciphertext ), 298 &length ) ); 299 } 300 TEST_STATUS( expected_status, 301 psa_asymmetric_decrypt( key_id, alg, 302 ciphertext, sizeof( ciphertext ), 303 NULL, 0, 304 plaintext, sizeof( plaintext ), 305 &length ) ); 306 307exit: 308 psa_destroy_key( key_id ); 309 psa_reset_key_attributes( &attributes ); 310 PSA_DONE( ); 311} 312/* END_CASE */ 313 314/* BEGIN_CASE */ 315void key_derivation_fail( int alg_arg, int expected_status_arg ) 316{ 317 psa_status_t expected_status = expected_status_arg; 318 psa_algorithm_t alg = alg_arg; 319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; 320 321 PSA_INIT( ); 322 323 TEST_EQUAL( expected_status, 324 psa_key_derivation_setup( &operation, alg ) ); 325 326exit: 327 psa_key_derivation_abort( &operation ); 328 PSA_DONE( ); 329} 330/* END_CASE */ 331 332/* BEGIN_CASE */ 333void key_agreement_fail( int key_type_arg, data_t *key_data, 334 int alg_arg, int private_only, 335 int expected_status_arg ) 336{ 337 psa_status_t expected_status = expected_status_arg; 338 psa_key_type_t key_type = key_type_arg; 339 psa_algorithm_t alg = alg_arg; 340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 341 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 342 uint8_t public_key[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE] = {0}; 343 size_t public_key_length = SIZE_MAX; 344 uint8_t output[PSA_SIGNATURE_MAX_SIZE] = {0}; 345 size_t length = SIZE_MAX; 346 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; 347 348 PSA_INIT( ); 349 350 psa_set_key_type( &attributes, key_type ); 351 psa_set_key_usage_flags( &attributes, 352 PSA_KEY_USAGE_DERIVE ); 353 psa_set_key_algorithm( &attributes, alg ); 354 PSA_ASSERT( psa_import_key( &attributes, 355 key_data->x, key_data->len, 356 &key_id ) ); 357 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) || 358 PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) ) 359 { 360 PSA_ASSERT( psa_export_public_key( key_id, 361 public_key, sizeof( public_key ), 362 &public_key_length ) ); 363 } 364 365 TEST_STATUS( expected_status, 366 psa_raw_key_agreement( alg, key_id, 367 public_key, public_key_length, 368 output, sizeof( output ), &length ) ); 369 370#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) 371 PSA_ASSERT( psa_key_derivation_setup( &operation, 372 PSA_ALG_HKDF( PSA_ALG_SHA_256 ) ) ); 373 TEST_STATUS( expected_status, 374 psa_key_derivation_key_agreement( 375 &operation, 376 PSA_KEY_DERIVATION_INPUT_SECRET, 377 key_id, 378 public_key, public_key_length ) ); 379#endif 380 381 /* There are no public-key operations. */ 382 (void) private_only; 383 384exit: 385 psa_key_derivation_abort( &operation ); 386 psa_destroy_key( key_id ); 387 psa_reset_key_attributes( &attributes ); 388 PSA_DONE( ); 389} 390/* END_CASE */ 391