Add test for ECP group metadata

Test cases added to check curve parameters and information for each
curve supported. Expected parameters are taken from references: SEC 2
for NIST, RFC 5639 for Brainpool, and RFC 7748 for curve25519/curve448.

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis 2022-08-15 11:43:56 +01:00
parent 7b1be55484
commit e54046c9c2
2 changed files with 158 additions and 0 deletions

View file

@ -309,6 +309,58 @@ ECP tls write-read point #2
depends_on:MBEDTLS_ECP_DP_SECP521R1_ENABLED
ecp_tls_write_read_point:MBEDTLS_ECP_DP_SECP521R1
Check ECP group metadata #1 secp192k1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP192K1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP192K1:192:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"fffffffffffffffffffffffffffffffffffffffeffffee37":"0":"3":"db4ff10ec057e9ae26b07d0280b7f4341da5d1b1eae06c7d":"9b2f2f6d9c5628a7844163d015be86344082aa88d95e2f9d":"fffffffffffffffffffffffe26f2fc170f69466a74defd8d":18
Check ECP group metadata #2 secp192r1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP192R1:192:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"fffffffffffffffffffffffffffffffeffffffffffffffff":"":"64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1":"188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012":"7192b95ffc8da78631011ed6b24cdd573f977a11e794811":"ffffffffffffffffffffffff99def836146bc9b1b4d22831":19
Check ECP group metadata #3 secp224k1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP224K1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP224K1:224:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"fffffffffffffffffffffffffffffffffffffffffffffffeffffe56d":"0":"5":"a1455b334df099df30fc28a169a467e9e47075a90f7e650eb6b7a45c":"7e089fed7fba344282cafbd6f7e319f7c0b0bd59e2ca4bdb556d61a5":"10000000000000000000000000001dce8d2ec6184caf0a971769fb1f7":20
Check ECP group metadata #4 secp224r1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP224R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP224R1:224:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"ffffffffffffffffffffffffffffffff000000000000000000000001":"":"b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4":"b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21":"bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34":"ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d":21
Check ECP group metadata #5 secp256k1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP256K1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP256K1:256:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f":"0":"7":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798":"483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8":"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141":22
Check ECP group metadata #6 secp256r1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP256R1:256:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"ffffffff00000001000000000000000000000000ffffffffffffffffffffffff":"":"5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b":"6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296":"4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5":"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551":23
Check ECP group metadata #7 secp384r1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP384R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP384R1:384:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff":"":"b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef":"aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7":"3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f":"ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973":24
Check ECP group metadata #8 secp521r1 (SEC 2)
depends_on:MBEDTLS_ECP_DP_SECP521R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_SECP521R1:521:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff":"":"51953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00":"c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66":"11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650":"1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409":25
Check ECP group metadata #9 bp256r1 (RFC 5639)
depends_on:MBEDTLS_ECP_DP_BP256R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_BP256R1:256:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"a9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377":"7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9":"26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6":"8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262":"547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997":"a9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7":26
Check ECP group metadata #10 bp384r1 (RFC 5639)
depends_on:MBEDTLS_ECP_DP_BP384R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_BP384R1:384:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53":"7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826":"4a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11":"1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e":"8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315":"8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565":27
Check ECP group metadata #11 bp512r1 (RFC 5639)
depends_on:MBEDTLS_ECP_DP_BP521R1_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_BP512R1:512:MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3":"7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca":"3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723":"81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822":"7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892":"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069":28
Check ECP group metadata #12 curve25519 (RFC 7748)
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_CURVE25519:256:MBEDTLS_ECP_TYPE_MONTGOMERY:"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed":"1db42":"":"9":"":"1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed":29
Check ECP group metadata #13 curve448 (RFC 7748)
depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED
mbedtls_ecp_group_metadata:MBEDTLS_ECP_DP_CURVE448:448:MBEDTLS_ECP_TYPE_MONTGOMERY:"fffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff":"98aa":"":"5":"":"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3":30
ECP tls read group #1 (record too short)
mbedtls_ecp_tls_read_group:"0313":MBEDTLS_ERR_ECP_BAD_INPUT_DATA:0:0

View file

@ -1,5 +1,7 @@
/* BEGIN_HEADER */
#include "mbedtls/ecp.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/ecdh.h"
#include "ecp_invasive.h"
@ -788,6 +790,110 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECDH_C:MBEDTLS_ECDSA_C */
void mbedtls_ecp_group_metadata( int id, int bit_size, int crv_type,
char* P, char* A, char* B,
char* G_x, char* G_y, char* N,
int tls_id )
{
mbedtls_ecp_group grp, grp_read, grp_cpy;
const mbedtls_ecp_group_id *g_id;
const mbedtls_ecp_curve_info *crv, *crv_tls_id, *crv_name;
mbedtls_mpi exp_P, exp_A, exp_B, exp_G_x, exp_G_y, exp_N;
unsigned char buf[3], ecparameters[3] = { 3, 0, tls_id };
const unsigned char *vbuf = buf;
size_t olen;
mbedtls_ecp_group_init( &grp );
mbedtls_ecp_group_init( &grp_read );
mbedtls_ecp_group_init( &grp_cpy );
mbedtls_mpi_init( &exp_P );
mbedtls_mpi_init( &exp_A );
mbedtls_mpi_init( &exp_B );
mbedtls_mpi_init( &exp_G_x );
mbedtls_mpi_init( &exp_G_y );
mbedtls_mpi_init( &exp_N );
// Read expected parameters
TEST_EQUAL( mbedtls_test_read_mpi( &exp_P, P ), 0 );
TEST_EQUAL( mbedtls_test_read_mpi( &exp_A, A ), 0 );
TEST_EQUAL( mbedtls_test_read_mpi( &exp_G_x, G_x ), 0 );
TEST_EQUAL( mbedtls_test_read_mpi( &exp_N, N ), 0 );
TEST_EQUAL( mbedtls_test_read_mpi( &exp_B, B ), 0 );
TEST_EQUAL( mbedtls_test_read_mpi( &exp_G_y, G_y ), 0 );
// Load group
TEST_EQUAL( mbedtls_ecp_group_load( &grp, id ), 0 );
// Compare group with expected parameters
// A is NULL for SECPxxxR1 curves
// B and G_y are NULL for curve25519 and curve448
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_P, &grp.P ), 0 );
if( *A != 0 )
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_A, &grp.A ), 0 );
if( *B != 0 )
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_B, &grp.B ), 0 );
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_G_x, &grp.G.X ), 0 );
if( *G_y != 0 )
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_G_y, &grp.G.Y ), 0 );
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &exp_N, &grp.N ), 0 );
// Load curve info and compare with known values
crv = mbedtls_ecp_curve_info_from_grp_id( id );
TEST_EQUAL( crv->grp_id, id );
TEST_EQUAL( crv->bit_size, bit_size );
TEST_EQUAL( crv->tls_id, tls_id );
// Load curve from TLS ID and name, and compare IDs
crv_tls_id = mbedtls_ecp_curve_info_from_tls_id( crv->tls_id );
crv_name = mbedtls_ecp_curve_info_from_name( crv->name );
TEST_EQUAL( crv_tls_id->grp_id, id );
TEST_EQUAL( crv_name->grp_id, id );
// Validate write_group against test data, read result
TEST_EQUAL( mbedtls_ecp_tls_write_group( &grp, &olen,
buf, sizeof( buf ) ),
0 );
TEST_EQUAL( mbedtls_test_hexcmp( buf, ecparameters, olen,
sizeof( ecparameters ) ),
0 );
TEST_EQUAL( mbedtls_ecp_tls_read_group( &grp_read, &vbuf, olen ),
0 );
TEST_EQUAL( grp_read.id, id );
// Check curve type, and if it can be used for ECDH/ECDSA
TEST_EQUAL( mbedtls_ecp_get_type( &grp ), crv_type );
TEST_EQUAL( mbedtls_ecdh_can_do( id ), 1 );
TEST_EQUAL( mbedtls_ecdsa_can_do( id ),
crv_type == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS );
// Copy group and compare with original
TEST_EQUAL( mbedtls_ecp_group_copy( &grp_cpy, &grp ), 0 );
TEST_EQUAL( mbedtls_ecp_group_cmp( &grp, &grp_cpy ), 0 );
// Check curve is in curve list and group ID list
for( crv = mbedtls_ecp_curve_list( );
crv->grp_id != MBEDTLS_ECP_DP_NONE &&
crv->grp_id != (unsigned) id;
crv++ );
TEST_EQUAL( crv->grp_id, id );
for( g_id = mbedtls_ecp_grp_id_list( );
*g_id != MBEDTLS_ECP_DP_NONE && *g_id != (unsigned) id;
g_id++ );
TEST_EQUAL( *g_id, (unsigned) id );
exit:
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_group_free( &grp_cpy );
mbedtls_ecp_group_free( &grp_read );
mbedtls_mpi_free( &exp_P ); mbedtls_mpi_free( &exp_A );
mbedtls_mpi_free( &exp_B ); mbedtls_mpi_free( &exp_G_x );
mbedtls_mpi_free( &exp_G_y ); mbedtls_mpi_free( &exp_N );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_check_privkey( int id, char * key_hex, int ret )
{