1/* BEGIN_HEADER */ 2#include "mbedtls/pk.h" 3 4/* For error codes */ 5#include "mbedtls/asn1.h" 6#include "mbedtls/base64.h" 7#include "mbedtls/ecp.h" 8#include "mbedtls/rsa.h" 9 10#include <limits.h> 11#include <stdint.h> 12 13/* Needed only for test case data under #if defined(MBEDTLS_USE_PSA_CRYPTO), 14 * but the test code generator requires test case data to be valid C code 15 * unconditionally (https://github.com/ARMmbed/mbedtls/issues/2023). */ 16#include "psa/crypto.h" 17 18#define RSA_KEY_SIZE 512 19#define RSA_KEY_LEN 64 20 21/** Generate a key of the desired type. 22 * 23 * \param pk The PK object to fill. It must have been initialized 24 * with mbedtls_pk_setup(). 25 * \param parameter - For RSA keys, the key size in bits. 26 * - For EC keys, the curve (\c MBEDTLS_ECP_DP_xxx). 27 * 28 * \return The status from the underlying type-specific key 29 * generation function. 30 * \return -1 if the key type is not recognized. 31 */ 32static int pk_genkey( mbedtls_pk_context *pk, int parameter ) 33{ 34 ((void) pk); 35 (void) parameter; 36 37#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) 38 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA ) 39 return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ), 40 mbedtls_test_rnd_std_rand, NULL, 41 parameter, 3 ); 42#endif 43#if defined(MBEDTLS_ECP_C) 44 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY || 45 mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY_DH || 46 mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECDSA ) 47 { 48 int ret; 49 if( ( ret = mbedtls_ecp_group_load( &mbedtls_pk_ec( *pk )->grp, 50 parameter ) ) != 0 ) 51 return( ret ); 52 53 return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp, 54 &mbedtls_pk_ec( *pk )->d, 55 &mbedtls_pk_ec( *pk )->Q, 56 mbedtls_test_rnd_std_rand, NULL ); 57 } 58#endif 59 return( -1 ); 60} 61 62#if defined(MBEDTLS_RSA_C) 63int mbedtls_rsa_decrypt_func( void *ctx, size_t *olen, 64 const unsigned char *input, unsigned char *output, 65 size_t output_max_len ) 66{ 67 return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, 68 mbedtls_test_rnd_std_rand, NULL, 69 olen, input, output, output_max_len ) ); 70} 71int mbedtls_rsa_sign_func( void *ctx, 72 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 73 mbedtls_md_type_t md_alg, unsigned int hashlen, 74 const unsigned char *hash, unsigned char *sig ) 75{ 76 ((void) f_rng); 77 ((void) p_rng); 78 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, 79 mbedtls_test_rnd_std_rand, NULL, 80 md_alg, hashlen, hash, sig ) ); 81} 82size_t mbedtls_rsa_key_len_func( void *ctx ) 83{ 84 return( ((const mbedtls_rsa_context *) ctx)->len ); 85} 86#endif /* MBEDTLS_RSA_C */ 87 88#if defined(MBEDTLS_USE_PSA_CRYPTO) 89 90/* 91 * Generate a key using PSA and return the key identifier of that key, 92 * or 0 if the key generation failed. 93 * The key uses NIST P-256 and is usable for signing with SHA-256. 94 */ 95mbedtls_svc_key_id_t pk_psa_genkey( void ) 96{ 97 mbedtls_svc_key_id_t key; 98 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 99 const psa_key_type_t type = 100 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ); 101 const size_t bits = 256; 102 103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); 104 psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256) ); 105 psa_set_key_type( &attributes, type ); 106 psa_set_key_bits( &attributes, bits ); 107 PSA_ASSERT( psa_generate_key( &attributes, &key ) ); 108 109exit: 110 return( key ); 111} 112#endif /* MBEDTLS_USE_PSA_CRYPTO */ 113/* END_HEADER */ 114 115/* BEGIN_DEPENDENCIES 116 * depends_on:MBEDTLS_PK_C 117 * END_DEPENDENCIES 118 */ 119 120/* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED */ 121void pk_psa_utils( ) 122{ 123 mbedtls_pk_context pk, pk2; 124 mbedtls_svc_key_id_t key; 125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 126 127 const char * const name = "Opaque"; 128 const size_t bitlen = 256; /* harcoded in genkey() */ 129 130 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; 131 unsigned char b1[1], b2[1]; 132 size_t len; 133 mbedtls_pk_debug_item dbg; 134 135 PSA_ASSERT( psa_crypto_init( ) ); 136 137 mbedtls_pk_init( &pk ); 138 mbedtls_pk_init( &pk2 ); 139 140 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); 141 142 TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, MBEDTLS_SVC_KEY_ID_INIT ) == 143 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 144 145 mbedtls_pk_free( &pk ); 146 mbedtls_pk_init( &pk ); 147 148 key = pk_psa_genkey(); 149 if( mbedtls_svc_key_id_is_null( key ) ) 150 goto exit; 151 152 TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, key ) == 0 ); 153 154 TEST_ASSERT( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_OPAQUE ); 155 TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 ); 156 157 TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == bitlen ); 158 TEST_ASSERT( mbedtls_pk_get_len( &pk ) == bitlen / 8 ); 159 160 TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECKEY ) == 1 ); 161 TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) == 1 ); 162 TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) == 0 ); 163 164 /* unsupported operations: verify, decrypt, encrypt */ 165 TEST_ASSERT( mbedtls_pk_verify( &pk, md_alg, 166 b1, sizeof( b1), b2, sizeof( b2 ) ) 167 == MBEDTLS_ERR_PK_TYPE_MISMATCH ); 168 TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ), 169 b2, &len, sizeof( b2 ), 170 NULL, NULL ) 171 == MBEDTLS_ERR_PK_TYPE_MISMATCH ); 172 TEST_ASSERT( mbedtls_pk_encrypt( &pk, b1, sizeof( b1 ), 173 b2, &len, sizeof( b2 ), 174 NULL, NULL ) 175 == MBEDTLS_ERR_PK_TYPE_MISMATCH ); 176 177 /* unsupported functions: check_pair, debug */ 178 TEST_ASSERT( mbedtls_pk_setup( &pk2, 179 mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 ); 180 TEST_ASSERT( mbedtls_pk_check_pair( &pk, &pk2, 181 mbedtls_test_rnd_std_rand, NULL ) 182 == MBEDTLS_ERR_PK_TYPE_MISMATCH ); 183 TEST_ASSERT( mbedtls_pk_debug( &pk, &dbg ) 184 == MBEDTLS_ERR_PK_TYPE_MISMATCH ); 185 186 /* test that freeing the context does not destroy the key */ 187 mbedtls_pk_free( &pk ); 188 TEST_ASSERT( PSA_SUCCESS == psa_get_key_attributes( key, &attributes ) ); 189 TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key ) ); 190 191exit: 192 /* 193 * Key attributes may have been returned by psa_get_key_attributes() 194 * thus reset them as required. 195 */ 196 psa_reset_key_attributes( &attributes ); 197 198 mbedtls_pk_free( &pk ); /* redundant except upon error */ 199 mbedtls_pk_free( &pk2 ); 200 USE_PSA_DONE( ); 201} 202/* END_CASE */ 203 204 205/* BEGIN_CASE */ 206void valid_parameters( ) 207{ 208 mbedtls_pk_context pk; 209 unsigned char buf[1]; 210 size_t len; 211 void *options = NULL; 212 213 mbedtls_pk_init( &pk ); 214 215 TEST_ASSERT( mbedtls_pk_setup( &pk, NULL ) == 216 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 217 218 /* In informational functions, we accept NULL where a context pointer 219 * is expected because that's what the library has done forever. 220 * We do not document that NULL is accepted, so we may wish to change 221 * the behavior in a future version. */ 222 TEST_ASSERT( mbedtls_pk_get_bitlen( NULL ) == 0 ); 223 TEST_ASSERT( mbedtls_pk_get_len( NULL ) == 0 ); 224 TEST_ASSERT( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 ); 225 226 TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, 227 MBEDTLS_MD_NONE, 228 NULL, 0, 229 buf, sizeof( buf ), &len, 230 mbedtls_test_rnd_std_rand, NULL, 231 NULL ) == 232 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 233 234 TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, 235 MBEDTLS_MD_NONE, 236 NULL, 0, 237 buf, sizeof( buf ), &len, 238 mbedtls_test_rnd_std_rand, NULL, 239 NULL ) == 240 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 241 242 TEST_ASSERT( mbedtls_pk_sign( &pk, 243 MBEDTLS_MD_NONE, 244 NULL, 0, 245 buf, sizeof( buf ), &len, 246 mbedtls_test_rnd_std_rand, NULL ) == 247 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 248 249 TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, 250 MBEDTLS_MD_NONE, 251 NULL, 0, 252 buf, sizeof( buf ), 253 NULL ) == 254 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 255 256 TEST_ASSERT( mbedtls_pk_verify( &pk, 257 MBEDTLS_MD_NONE, 258 NULL, 0, 259 buf, sizeof( buf ) ) == 260 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 261 262 TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, 263 &pk, 264 MBEDTLS_MD_NONE, 265 NULL, 0, 266 buf, sizeof( buf ) ) == 267 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 268 269 TEST_ASSERT( mbedtls_pk_encrypt( &pk, 270 NULL, 0, 271 NULL, &len, 0, 272 mbedtls_test_rnd_std_rand, NULL ) == 273 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 274 275 TEST_ASSERT( mbedtls_pk_decrypt( &pk, 276 NULL, 0, 277 NULL, &len, 0, 278 mbedtls_test_rnd_std_rand, NULL ) == 279 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 280 281#if defined(MBEDTLS_PK_PARSE_C) 282 TEST_ASSERT( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1, 283 mbedtls_test_rnd_std_rand, NULL ) == 284 MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); 285 286 TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, NULL, 0 ) == 287 MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); 288#endif /* MBEDTLS_PK_PARSE_C */ 289} 290/* END_CASE */ 291 292/* BEGIN_CASE depends_on:MBEDTLS_PK_WRITE_C */ 293void valid_parameters_pkwrite( data_t *key_data ) 294{ 295 mbedtls_pk_context pk; 296 297 /* For the write tests to be effective, we need a valid key pair. */ 298 mbedtls_pk_init( &pk ); 299 TEST_ASSERT( mbedtls_pk_parse_key( &pk, 300 key_data->x, key_data->len, NULL, 0, 301 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 302 303 TEST_ASSERT( mbedtls_pk_write_key_der( &pk, NULL, 0 ) == 304 MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 305 306 TEST_ASSERT( mbedtls_pk_write_pubkey_der( &pk, NULL, 0 ) == 307 MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 308 309#if defined(MBEDTLS_PEM_WRITE_C) 310 TEST_ASSERT( mbedtls_pk_write_key_pem( &pk, NULL, 0 ) == 311 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); 312 313 TEST_ASSERT( mbedtls_pk_write_pubkey_pem( &pk, NULL, 0 ) == 314 MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); 315#endif /* MBEDTLS_PEM_WRITE_C */ 316 317exit: 318 mbedtls_pk_free( &pk ); 319} 320/* END_CASE */ 321 322/* BEGIN_CASE */ 323void pk_utils( int type, int parameter, int bitlen, int len, char * name ) 324{ 325 mbedtls_pk_context pk; 326 327 mbedtls_pk_init( &pk ); 328 329 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); 330 TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 ); 331 332 TEST_ASSERT( (int) mbedtls_pk_get_type( &pk ) == type ); 333 TEST_ASSERT( mbedtls_pk_can_do( &pk, type ) ); 334 TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) bitlen ); 335 TEST_ASSERT( mbedtls_pk_get_len( &pk ) == (unsigned) len ); 336 TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 ); 337 338exit: 339 mbedtls_pk_free( &pk ); 340} 341/* END_CASE */ 342 343/* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */ 344void mbedtls_pk_check_pair( char * pub_file, char * prv_file, int ret ) 345{ 346 mbedtls_pk_context pub, prv, alt; 347 348 mbedtls_pk_init( &pub ); 349 mbedtls_pk_init( &prv ); 350 mbedtls_pk_init( &alt ); 351 352 TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &pub, pub_file ) == 0 ); 353 TEST_ASSERT( mbedtls_pk_parse_keyfile( &prv, prv_file, NULL, 354 mbedtls_test_rnd_std_rand, NULL ) 355 == 0 ); 356 357 TEST_ASSERT( mbedtls_pk_check_pair( &pub, &prv, 358 mbedtls_test_rnd_std_rand, NULL ) 359 == ret ); 360 361#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 362 if( mbedtls_pk_get_type( &prv ) == MBEDTLS_PK_RSA ) 363 { 364 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, mbedtls_pk_rsa( prv ), 365 mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, 366 mbedtls_rsa_key_len_func ) == 0 ); 367 TEST_ASSERT( mbedtls_pk_check_pair( &pub, &alt, 368 mbedtls_test_rnd_std_rand, NULL ) 369 == ret ); 370 } 371#endif 372 373 mbedtls_pk_free( &pub ); 374 mbedtls_pk_free( &prv ); 375 mbedtls_pk_free( &alt ); 376} 377/* END_CASE */ 378 379/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 380void pk_rsa_verify_test_vec( data_t * message_str, int digest, int mod, 381 int radix_N, char * input_N, int radix_E, 382 char * input_E, data_t * result_str, 383 int result ) 384{ 385 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; 386 mbedtls_rsa_context *rsa; 387 mbedtls_pk_context pk; 388 mbedtls_pk_restart_ctx *rs_ctx = NULL; 389#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 390 mbedtls_pk_restart_ctx ctx; 391 392 rs_ctx = &ctx; 393 mbedtls_pk_restart_init( rs_ctx ); 394 // this setting would ensure restart would happen if ECC was used 395 mbedtls_ecp_set_max_ops( 1 ); 396#endif 397 398 mbedtls_pk_init( &pk ); 399 400 memset( hash_result, 0x00, MBEDTLS_MD_MAX_SIZE ); 401 402 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 403 rsa = mbedtls_pk_rsa( pk ); 404 405 rsa->len = mod / 8; 406 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 ); 407 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 ); 408 409 410 if( mbedtls_md_info_from_type( digest ) != NULL ) 411 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 ); 412 413 TEST_ASSERT( mbedtls_pk_verify( &pk, digest, hash_result, 0, 414 result_str->x, mbedtls_pk_get_len( &pk ) ) == result ); 415 416 TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, digest, hash_result, 0, 417 result_str->x, mbedtls_pk_get_len( &pk ), rs_ctx ) == result ); 418 419exit: 420#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 421 mbedtls_pk_restart_free( rs_ctx ); 422#endif 423 mbedtls_pk_free( &pk ); 424} 425/* END_CASE */ 426 427/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 428void pk_rsa_verify_ext_test_vec( data_t * message_str, int digest, 429 int mod, int radix_N, char * input_N, 430 int radix_E, char * input_E, 431 data_t * result_str, int pk_type, 432 int mgf1_hash_id, int salt_len, int result ) 433{ 434 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; 435 mbedtls_rsa_context *rsa; 436 mbedtls_pk_context pk; 437 mbedtls_pk_rsassa_pss_options pss_opts; 438 void *options; 439 size_t hash_len; 440 441 mbedtls_pk_init( &pk ); 442 443 memset( hash_result, 0x00, sizeof( hash_result ) ); 444 445 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 446 rsa = mbedtls_pk_rsa( pk ); 447 448 rsa->len = mod / 8; 449 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 ); 450 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 ); 451 452 453 if( digest != MBEDTLS_MD_NONE ) 454 { 455 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( digest ); 456 TEST_ASSERT( mbedtls_md( md_info, message_str->x, message_str->len, 457 hash_result ) == 0 ); 458 hash_len = mbedtls_md_get_size( md_info ); 459 } 460 else 461 { 462 memcpy( hash_result, message_str->x, message_str->len ); 463 hash_len = message_str->len; 464 } 465 466 if( mgf1_hash_id < 0 ) 467 { 468 options = NULL; 469 } 470 else 471 { 472 options = &pss_opts; 473 474 pss_opts.mgf1_hash_id = mgf1_hash_id; 475 pss_opts.expected_salt_len = salt_len; 476 } 477 478 TEST_ASSERT( mbedtls_pk_verify_ext( pk_type, options, &pk, 479 digest, hash_result, hash_len, 480 result_str->x, mbedtls_pk_get_len( &pk ) ) == result ); 481 482exit: 483 mbedtls_pk_free( &pk ); 484} 485/* END_CASE */ 486 487/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C */ 488void pk_ec_test_vec( int type, int id, data_t * key, data_t * hash, 489 data_t * sig, int ret ) 490{ 491 mbedtls_pk_context pk; 492 mbedtls_ecp_keypair *eckey; 493 494 mbedtls_pk_init( &pk ); 495 USE_PSA_INIT( ); 496 497 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); 498 499 TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) ); 500 eckey = mbedtls_pk_ec( pk ); 501 502 TEST_ASSERT( mbedtls_ecp_group_load( &eckey->grp, id ) == 0 ); 503 TEST_ASSERT( mbedtls_ecp_point_read_binary( &eckey->grp, &eckey->Q, 504 key->x, key->len ) == 0 ); 505 506 // MBEDTLS_MD_NONE is used since it will be ignored. 507 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, 508 hash->x, hash->len, sig->x, sig->len ) == ret ); 509 510exit: 511 mbedtls_pk_free( &pk ); 512 USE_PSA_DONE( ); 513} 514/* END_CASE */ 515 516/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC */ 517void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str, 518 char *QX_str, char *QY_str, 519 int md_alg, char *msg, data_t *sig_check, 520 int max_ops, int min_restart, int max_restart ) 521{ 522 int ret, cnt_restart; 523 mbedtls_pk_restart_ctx rs_ctx; 524 mbedtls_pk_context prv, pub; 525 unsigned char hash[MBEDTLS_MD_MAX_SIZE]; 526 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; 527 size_t hlen, slen; 528 const mbedtls_md_info_t *md_info; 529 530 mbedtls_pk_restart_init( &rs_ctx ); 531 mbedtls_pk_init( &prv ); 532 mbedtls_pk_init( &pub ); 533 memset( hash, 0, sizeof( hash ) ); 534 memset( sig, 0, sizeof( sig ) ); 535 536 TEST_ASSERT( mbedtls_pk_setup( &prv, mbedtls_pk_info_from_type( pk_type ) ) == 0 ); 537 TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( prv )->grp, grp_id ) == 0 ); 538 TEST_ASSERT( mbedtls_test_read_mpi( &mbedtls_pk_ec( prv )->d, 16, d_str ) == 0 ); 539 540 TEST_ASSERT( mbedtls_pk_setup( &pub, mbedtls_pk_info_from_type( pk_type ) ) == 0 ); 541 TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( pub )->grp, grp_id ) == 0 ); 542 TEST_ASSERT( mbedtls_ecp_point_read_string( &mbedtls_pk_ec( pub )->Q, 16, QX_str, QY_str ) == 0 ); 543 544 md_info = mbedtls_md_info_from_type( md_alg ); 545 TEST_ASSERT( md_info != NULL ); 546 547 hlen = mbedtls_md_get_size( md_info ); 548 TEST_ASSERT( mbedtls_md( md_info, 549 (const unsigned char *) msg, strlen( msg ), 550 hash ) == 0 ); 551 552 mbedtls_ecp_set_max_ops( max_ops ); 553 554 slen = sizeof( sig ); 555 cnt_restart = 0; 556 do { 557 ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen, 558 sig, sizeof( sig ), &slen, 559 mbedtls_test_rnd_std_rand, NULL, 560 &rs_ctx ); 561 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 562 563 TEST_ASSERT( ret == 0 ); 564 TEST_ASSERT( slen == sig_check->len ); 565 TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 ); 566 567 TEST_ASSERT( cnt_restart >= min_restart ); 568 TEST_ASSERT( cnt_restart <= max_restart ); 569 570 cnt_restart = 0; 571 do { 572 ret = mbedtls_pk_verify_restartable( &pub, md_alg, 573 hash, hlen, sig, slen, &rs_ctx ); 574 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 575 576 TEST_ASSERT( ret == 0 ); 577 TEST_ASSERT( cnt_restart >= min_restart ); 578 TEST_ASSERT( cnt_restart <= max_restart ); 579 580 hash[0]++; 581 do { 582 ret = mbedtls_pk_verify_restartable( &pub, md_alg, 583 hash, hlen, sig, slen, &rs_ctx ); 584 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); 585 TEST_ASSERT( ret != 0 ); 586 hash[0]--; 587 588 sig[0]++; 589 do { 590 ret = mbedtls_pk_verify_restartable( &pub, md_alg, 591 hash, hlen, sig, slen, &rs_ctx ); 592 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); 593 TEST_ASSERT( ret != 0 ); 594 sig[0]--; 595 596 /* Do we leak memory when aborting? try verify then sign 597 * This test only makes sense when we actually restart */ 598 if( min_restart > 0 ) 599 { 600 ret = mbedtls_pk_verify_restartable( &pub, md_alg, 601 hash, hlen, sig, slen, &rs_ctx ); 602 TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); 603 mbedtls_pk_restart_free( &rs_ctx ); 604 605 slen = sizeof( sig ); 606 ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen, 607 sig, sizeof sig, &slen, 608 mbedtls_test_rnd_std_rand, NULL, 609 &rs_ctx ); 610 TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); 611 } 612 613exit: 614 mbedtls_pk_restart_free( &rs_ctx ); 615 mbedtls_pk_free( &prv ); 616 mbedtls_pk_free( &pub ); 617} 618/* END_CASE */ 619 620/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ 621void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret ) 622{ 623 mbedtls_pk_context pk; 624 size_t sig_len; 625 unsigned char hash[32]; // Hard-coded for SHA256 626 size_t hash_len = sizeof( hash ); 627 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; 628 void *rs_ctx = NULL; 629#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 630 mbedtls_pk_restart_ctx ctx; 631 632 rs_ctx = &ctx; 633 mbedtls_pk_restart_init( rs_ctx ); 634 /* This value is large enough that the operation will complete in one run. 635 * See comments at the top of ecp_test_vect_restart in 636 * test_suite_ecp.function for estimates of operation counts. */ 637 mbedtls_ecp_set_max_ops( 42000 ); 638#endif 639 640 mbedtls_pk_init( &pk ); 641 USE_PSA_INIT( ); 642 643 memset( hash, 0x2a, sizeof hash ); 644 memset( sig, 0, sizeof sig ); 645 646 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); 647 TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 ); 648 649 TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_SHA256, 650 hash, hash_len, 651 sig, sizeof sig, &sig_len, 652 mbedtls_test_rnd_std_rand, NULL, 653 rs_ctx ) == sign_ret ); 654 if( sign_ret == 0 ) 655 TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE ); 656 else 657 sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; 658 659 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, 660 hash, hash_len, sig, sig_len ) == verify_ret ); 661 662 if( verify_ret == 0 ) 663 { 664 hash[0]++; 665 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, 666 hash, hash_len, sig, sig_len ) != 0 ); 667 hash[0]--; 668 669 sig[0]++; 670 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, 671 hash, hash_len, sig, sig_len ) != 0 ); 672 sig[0]--; 673 } 674 675 TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, hash_len, 676 sig, sizeof sig, &sig_len, 677 mbedtls_test_rnd_std_rand, 678 NULL ) == sign_ret ); 679 if( sign_ret == 0 ) 680 TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE ); 681 else 682 sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; 683 684 TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, 685 hash, hash_len, sig, sig_len, rs_ctx ) == verify_ret ); 686 687 if( verify_ret == 0 ) 688 { 689 hash[0]++; 690 TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, 691 hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); 692 hash[0]--; 693 694 sig[0]++; 695 TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, 696 hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); 697 sig[0]--; 698 } 699 700exit: 701#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 702 mbedtls_pk_restart_free( rs_ctx ); 703#endif 704 mbedtls_pk_free( &pk ); 705 USE_PSA_DONE( ); 706} 707/* END_CASE */ 708 709/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 710void pk_rsa_encrypt_test_vec( data_t * message, int mod, int radix_N, 711 char * input_N, int radix_E, char * input_E, 712 data_t * result, int ret ) 713{ 714 unsigned char output[300]; 715 mbedtls_test_rnd_pseudo_info rnd_info; 716 mbedtls_rsa_context *rsa; 717 mbedtls_pk_context pk; 718 size_t olen; 719 720 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 721 memset( output, 0, sizeof( output ) ); 722 723 724 mbedtls_pk_init( &pk ); 725 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 726 rsa = mbedtls_pk_rsa( pk ); 727 728 rsa->len = mod / 8; 729 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 ); 730 TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 ); 731 732 TEST_ASSERT( mbedtls_pk_encrypt( &pk, message->x, message->len, 733 output, &olen, sizeof( output ), 734 mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); 735 TEST_ASSERT( olen == result->len ); 736 TEST_ASSERT( memcmp( output, result->x, olen ) == 0 ); 737 738exit: 739 mbedtls_pk_free( &pk ); 740} 741/* END_CASE */ 742 743/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 744void pk_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P, 745 char * input_P, int radix_Q, char * input_Q, 746 int radix_N, char * input_N, int radix_E, 747 char * input_E, data_t * clear, int ret ) 748{ 749 unsigned char output[256]; 750 mbedtls_test_rnd_pseudo_info rnd_info; 751 mbedtls_mpi N, P, Q, E; 752 mbedtls_rsa_context *rsa; 753 mbedtls_pk_context pk; 754 size_t olen; 755 756 mbedtls_pk_init( &pk ); 757 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); 758 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); 759 760 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 761 762 763 /* init pk-rsa context */ 764 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 765 rsa = mbedtls_pk_rsa( pk ); 766 767 /* load public key */ 768 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 769 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 770 771 /* load private key */ 772 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 773 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 774 TEST_ASSERT( mbedtls_rsa_import( rsa, &N, &P, &Q, NULL, &E ) == 0 ); 775 TEST_ASSERT( mbedtls_rsa_get_len( rsa ) == (size_t) ( mod / 8 ) ); 776 TEST_ASSERT( mbedtls_rsa_complete( rsa ) == 0 ); 777 778 /* decryption test */ 779 memset( output, 0, sizeof( output ) ); 780 olen = 0; 781 TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher->x, cipher->len, 782 output, &olen, sizeof( output ), 783 mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); 784 if( ret == 0 ) 785 { 786 TEST_ASSERT( olen == clear->len ); 787 TEST_ASSERT( memcmp( output, clear->x, olen ) == 0 ); 788 } 789 790exit: 791 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); 792 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); 793 mbedtls_pk_free( &pk ); 794} 795/* END_CASE */ 796 797/* BEGIN_CASE */ 798void pk_ec_nocrypt( int type ) 799{ 800 mbedtls_pk_context pk; 801 unsigned char output[100]; 802 unsigned char input[100]; 803 mbedtls_test_rnd_pseudo_info rnd_info; 804 size_t olen = 0; 805 int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; 806 807 mbedtls_pk_init( &pk ); 808 809 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 810 memset( output, 0, sizeof( output ) ); 811 memset( input, 0, sizeof( input ) ); 812 813 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); 814 815 TEST_ASSERT( mbedtls_pk_encrypt( &pk, input, sizeof( input ), 816 output, &olen, sizeof( output ), 817 mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); 818 819 TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ), 820 output, &olen, sizeof( output ), 821 mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); 822 823exit: 824 mbedtls_pk_free( &pk ); 825} 826/* END_CASE */ 827 828/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 829void pk_rsa_overflow( ) 830{ 831 mbedtls_pk_context pk; 832 size_t hash_len = SIZE_MAX, sig_len = SIZE_MAX; 833 unsigned char hash[50], sig[100]; 834 835 if( SIZE_MAX <= UINT_MAX ) 836 return; 837 838 memset( hash, 0x2a, sizeof hash ); 839 memset( sig, 0, sizeof sig ); 840 841 mbedtls_pk_init( &pk ); 842 843 TEST_ASSERT( mbedtls_pk_setup( &pk, 844 mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 845 846#if defined(MBEDTLS_PKCS1_V21) 847 TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, NULL, &pk, 848 MBEDTLS_MD_NONE, hash, hash_len, sig, sig_len ) == 849 MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 850#endif /* MBEDTLS_PKCS1_V21 */ 851 852 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, hash, hash_len, 853 sig, sig_len ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 854 855 TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, hash, hash_len, 856 sig, sizeof sig, &sig_len, 857 mbedtls_test_rnd_std_rand, NULL ) 858 == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 859 860exit: 861 mbedtls_pk_free( &pk ); 862} 863/* END_CASE */ 864 865/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */ 866void pk_rsa_alt( ) 867{ 868 /* 869 * An rsa_alt context can only do private operations (decrypt, sign). 870 * Test it against the public operations (encrypt, verify) of a 871 * corresponding rsa context. 872 */ 873 mbedtls_rsa_context raw; 874 mbedtls_pk_context rsa, alt; 875 mbedtls_pk_debug_item dbg_items[10]; 876 unsigned char hash[50], sig[64]; 877 unsigned char msg[50], ciph[64], test[50]; 878 size_t sig_len, ciph_len, test_len; 879 int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; 880 881 mbedtls_rsa_init( &raw ); 882 mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt ); 883 884 memset( hash, 0x2a, sizeof hash ); 885 memset( sig, 0, sizeof sig ); 886 memset( msg, 0x2a, sizeof msg ); 887 memset( ciph, 0, sizeof ciph ); 888 memset( test, 0, sizeof test ); 889 890 /* Initiliaze PK RSA context with random key */ 891 TEST_ASSERT( mbedtls_pk_setup( &rsa, 892 mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); 893 TEST_ASSERT( pk_genkey( &rsa, RSA_KEY_SIZE ) == 0 ); 894 895 /* Extract key to the raw rsa context */ 896 TEST_ASSERT( mbedtls_rsa_copy( &raw, mbedtls_pk_rsa( rsa ) ) == 0 ); 897 898 /* Initialize PK RSA_ALT context */ 899 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw, 900 mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 ); 901 902 /* Test administrative functions */ 903 TEST_ASSERT( mbedtls_pk_can_do( &alt, MBEDTLS_PK_RSA ) ); 904 TEST_ASSERT( mbedtls_pk_get_bitlen( &alt ) == RSA_KEY_SIZE ); 905 TEST_ASSERT( mbedtls_pk_get_len( &alt ) == RSA_KEY_LEN ); 906 TEST_ASSERT( mbedtls_pk_get_type( &alt ) == MBEDTLS_PK_RSA_ALT ); 907 TEST_ASSERT( strcmp( mbedtls_pk_get_name( &alt ), "RSA-alt" ) == 0 ); 908 909 /* Test signature */ 910#if SIZE_MAX > UINT_MAX 911 TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, SIZE_MAX, 912 sig, sizeof sig, &sig_len, 913 mbedtls_test_rnd_std_rand, NULL ) 914 == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); 915#endif /* SIZE_MAX > UINT_MAX */ 916 TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash, 917 sig, sizeof sig, &sig_len, 918 mbedtls_test_rnd_std_rand, NULL ) 919 == 0 ); 920 TEST_ASSERT( sig_len == RSA_KEY_LEN ); 921 TEST_ASSERT( mbedtls_pk_verify( &rsa, MBEDTLS_MD_NONE, 922 hash, sizeof hash, sig, sig_len ) == 0 ); 923 924 /* Test decrypt */ 925 TEST_ASSERT( mbedtls_pk_encrypt( &rsa, msg, sizeof msg, 926 ciph, &ciph_len, sizeof ciph, 927 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 928 TEST_ASSERT( mbedtls_pk_decrypt( &alt, ciph, ciph_len, 929 test, &test_len, sizeof test, 930 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 931 TEST_ASSERT( test_len == sizeof msg ); 932 TEST_ASSERT( memcmp( test, msg, test_len ) == 0 ); 933 934 /* Test forbidden operations */ 935 TEST_ASSERT( mbedtls_pk_encrypt( &alt, msg, sizeof msg, 936 ciph, &ciph_len, sizeof ciph, 937 mbedtls_test_rnd_std_rand, NULL ) == ret ); 938 TEST_ASSERT( mbedtls_pk_verify( &alt, MBEDTLS_MD_NONE, 939 hash, sizeof hash, sig, sig_len ) == ret ); 940 TEST_ASSERT( mbedtls_pk_debug( &alt, dbg_items ) == ret ); 941 942exit: 943 mbedtls_rsa_free( &raw ); 944 mbedtls_pk_free( &rsa ); mbedtls_pk_free( &alt ); 945} 946/* END_CASE */ 947 948/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C */ 949void pk_psa_sign( int grpid_arg, 950 int psa_curve_arg, int expected_bits_arg ) 951{ 952 mbedtls_ecp_group_id grpid = grpid_arg; 953 mbedtls_pk_context pk; 954 unsigned char hash[32]; 955 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; 956 unsigned char pkey_legacy[200]; 957 unsigned char pkey_psa[200]; 958 unsigned char *pkey_legacy_start, *pkey_psa_start; 959 size_t sig_len, klen_legacy, klen_psa; 960 int ret; 961 mbedtls_svc_key_id_t key_id; 962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 963 psa_key_type_t expected_type = PSA_KEY_TYPE_ECC_KEY_PAIR( psa_curve_arg ); 964 size_t expected_bits = expected_bits_arg; 965 966 /* 967 * This tests making signatures with a wrapped PSA key: 968 * - generate a fresh ECP legacy PK context 969 * - wrap it in a PK context and make a signature this way 970 * - extract the public key 971 * - parse it to a PK context and verify the signature this way 972 */ 973 974 PSA_ASSERT( psa_crypto_init( ) ); 975 976 /* Create legacy EC public/private key in PK context. */ 977 mbedtls_pk_init( &pk ); 978 TEST_ASSERT( mbedtls_pk_setup( &pk, 979 mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 ); 980 TEST_ASSERT( mbedtls_ecp_gen_key( grpid, 981 (mbedtls_ecp_keypair*) pk.pk_ctx, 982 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 983 984 /* Export underlying public key for re-importing in a legacy context. */ 985 ret = mbedtls_pk_write_pubkey_der( &pk, pkey_legacy, 986 sizeof( pkey_legacy ) ); 987 TEST_ASSERT( ret >= 0 ); 988 klen_legacy = (size_t) ret; 989 /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */ 990 pkey_legacy_start = pkey_legacy + sizeof( pkey_legacy ) - klen_legacy; 991 992 /* Turn PK context into an opaque one. */ 993 TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &pk, &key_id, 994 PSA_ALG_SHA_256 ) == 0 ); 995 996 PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) ); 997 TEST_EQUAL( psa_get_key_type( &attributes ), expected_type ); 998 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); 999 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 1000 PSA_KEY_LIFETIME_VOLATILE ); 1001 1002 memset( hash, 0x2a, sizeof hash ); 1003 memset( sig, 0, sizeof sig ); 1004 1005 TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, 1006 hash, sizeof hash, sig, sizeof sig, &sig_len, 1007 NULL, NULL ) == 0 ); 1008 1009 /* Export underlying public key for re-importing in a psa context. */ 1010 ret = mbedtls_pk_write_pubkey_der( &pk, pkey_psa, 1011 sizeof( pkey_psa ) ); 1012 TEST_ASSERT( ret >= 0 ); 1013 klen_psa = (size_t) ret; 1014 /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */ 1015 pkey_psa_start = pkey_psa + sizeof( pkey_psa ) - klen_psa; 1016 1017 TEST_ASSERT( klen_psa == klen_legacy ); 1018 TEST_ASSERT( memcmp( pkey_psa_start, pkey_legacy_start, klen_psa ) == 0 ); 1019 1020 mbedtls_pk_free( &pk ); 1021 TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key_id ) ); 1022 1023 mbedtls_pk_init( &pk ); 1024 TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, pkey_legacy_start, 1025 klen_legacy ) == 0 ); 1026 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, 1027 hash, sizeof hash, sig, sig_len ) == 0 ); 1028 1029exit: 1030 /* 1031 * Key attributes may have been returned by psa_get_key_attributes() 1032 * thus reset them as required. 1033 */ 1034 psa_reset_key_attributes( &attributes ); 1035 1036 mbedtls_pk_free( &pk ); 1037 USE_PSA_DONE( ); 1038} 1039/* END_CASE */ 1040