Rm useless use of MD in ECDSA test functions
We had a message in the data file, and were computing its hash in the test function. It is more efficient (and simpler when it comes to dependencies) to directly have the message hash in the data file. It was probably this way because some test vectors provide the message for the sake of all-in-one implementation that hash-and-sign at once. But our API gets a hash as the input and signs it. In unit tests, this should be reflected in the signature of the test function, which should take a hash as input. The changes to the .data file were done using the following python script: import hashlib suite = 'ecdsa' functions = { 'ecdsa_det_test_vectors': (3, 4), 'ecdsa_write_restart': (3, 4), } def hash_ctx(s): if s == 'MBEDTLS_MD_MD5': return hashlib.md5() if s == 'MBEDTLS_MD_SHA1': return hashlib.sha1() if s == 'MBEDTLS_MD_SHA224': return hashlib.sha224() if s == 'MBEDTLS_MD_SHA256': return hashlib.sha256() if s == 'MBEDTLS_MD_SHA384': return hashlib.sha384() if s == 'MBEDTLS_MD_SHA512': return hashlib.sha512() def fix(l): parts = l.rstrip().split(":") fun = parts[0] if fun not in functions: return l (alg_idx, msg_idx) = functions[fun] alg_str = parts[alg_idx] if alg_str == "MBEDTLS_MD_NONE": return l h = hash_ctx(alg_str) msg_str = parts[msg_idx][1:-1] h.update(msg_str.encode('ascii')) msg_hash = h.hexdigest() msg_hash_str = '"' + msg_hash.upper() + '"' parts[msg_idx] = msg_hash_str return ":".join(parts) + '\n' filename = 'tests/suites/test_suite_' + suite + '.data' with open(filename) as f: lines = f.readlines() lines = [fix(l) for l in lines] with open(filename, 'w') as f: f.writelines(lines) Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
This commit is contained in:
parent
d5b1eb51db
commit
5ea65173fb
2 changed files with 62 additions and 82 deletions
|
@ -192,34 +192,25 @@ exit:
|
|||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
void ecdsa_det_test_vectors( int id, char * d_str, int md_alg, char * msg,
|
||||
void ecdsa_det_test_vectors( int id, char * d_str, int md_alg, data_t * hash,
|
||||
char * r_str, char * s_str )
|
||||
{
|
||||
mbedtls_ecp_group grp;
|
||||
mbedtls_mpi d, r, s, r_check, s_check;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
size_t hlen;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
|
||||
mbedtls_ecp_group_init( &grp );
|
||||
mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
|
||||
mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
|
||||
memset( hash, 0, sizeof( hash ) );
|
||||
|
||||
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
||||
TEST_ASSERT( mbedtls_test_read_mpi( &d, 16, d_str ) == 0 );
|
||||
TEST_ASSERT( mbedtls_test_read_mpi( &r_check, 16, r_str ) == 0 );
|
||||
TEST_ASSERT( mbedtls_test_read_mpi( &s_check, 16, s_str ) == 0 );
|
||||
|
||||
md_info = mbedtls_md_info_from_type( md_alg );
|
||||
TEST_ASSERT( md_info != NULL );
|
||||
hlen = mbedtls_md_get_size( md_info );
|
||||
TEST_ASSERT( mbedtls_md( md_info, (const unsigned char *) msg,
|
||||
strlen( msg ), hash ) == 0 );
|
||||
|
||||
TEST_ASSERT(
|
||||
mbedtls_ecdsa_sign_det_ext( &grp, &r, &s, &d, hash, hlen,
|
||||
md_alg, mbedtls_test_rnd_std_rand,
|
||||
mbedtls_ecdsa_sign_det_ext( &grp, &r, &s, &d,
|
||||
hash->x, hash->len, md_alg,
|
||||
mbedtls_test_rnd_std_rand,
|
||||
NULL )
|
||||
== 0 );
|
||||
|
||||
|
@ -421,40 +412,29 @@ exit:
|
|||
|
||||
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
void ecdsa_write_restart( int id, char *d_str, int md_alg,
|
||||
char *msg, data_t *sig_check,
|
||||
data_t *hash, data_t *sig_check,
|
||||
int max_ops, int min_restart, int max_restart )
|
||||
{
|
||||
int ret, cnt_restart;
|
||||
mbedtls_ecdsa_restart_ctx rs_ctx;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
||||
size_t hlen, slen;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
size_t slen;
|
||||
|
||||
mbedtls_ecdsa_restart_init( &rs_ctx );
|
||||
mbedtls_ecdsa_init( &ctx );
|
||||
memset( hash, 0, sizeof( hash ) );
|
||||
memset( sig, 0, sizeof( sig ) );
|
||||
|
||||
TEST_ASSERT( mbedtls_ecp_group_load( &ctx.grp, id ) == 0 );
|
||||
TEST_ASSERT( mbedtls_test_read_mpi( &ctx.d, 16, d_str ) == 0 );
|
||||
|
||||
md_info = mbedtls_md_info_from_type( md_alg );
|
||||
TEST_ASSERT( md_info != NULL );
|
||||
|
||||
hlen = mbedtls_md_get_size( md_info );
|
||||
TEST_ASSERT( mbedtls_md( md_info,
|
||||
(const unsigned char *) msg, strlen( msg ),
|
||||
hash ) == 0 );
|
||||
|
||||
mbedtls_ecp_set_max_ops( max_ops );
|
||||
|
||||
slen = sizeof( sig );
|
||||
cnt_restart = 0;
|
||||
do {
|
||||
ret = mbedtls_ecdsa_write_signature_restartable( &ctx,
|
||||
md_alg, hash, hlen, sig, sizeof( sig ), &slen,
|
||||
md_alg, hash->x, hash->len, sig, sizeof( sig ), &slen,
|
||||
mbedtls_test_rnd_std_rand, NULL, &rs_ctx );
|
||||
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
||||
|
||||
|
@ -470,7 +450,7 @@ void ecdsa_write_restart( int id, char *d_str, int md_alg,
|
|||
if( min_restart > 0 )
|
||||
{
|
||||
ret = mbedtls_ecdsa_write_signature_restartable( &ctx,
|
||||
md_alg, hash, hlen, sig, sizeof( sig ), &slen,
|
||||
md_alg, hash->x, hash->len, sig, sizeof( sig ), &slen,
|
||||
mbedtls_test_rnd_std_rand, NULL, &rs_ctx );
|
||||
TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue