Fix endianness issue in test helper function

This commit is contained in:
Manuel Pégourié-Gonnard 2013-10-21 13:56:40 +02:00 committed by Paul Bakker
parent 45a2c8d99a
commit 7446833626
3 changed files with 37 additions and 39 deletions

View file

@ -2,6 +2,14 @@
#include "polarssl/memory.h" #include "polarssl/memory.h"
#endif #endif
#if defined(WANT_NOT_RND_MPI)
#if defined(POLARSSL_BIGNUM_C)
#include "polarssl/bignum.h"
#else
#error "not_rnd_mpi() need bignum.c"
#endif
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
#include <basetsd.h> #include <basetsd.h>
typedef UINT32 uint32_t; typedef UINT32 uint32_t;
@ -225,48 +233,36 @@ static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
return( 0 ); return( 0 );
} }
#if defined(WANT_NOT_RND_MPI)
/** /**
* This function returns a buffer given as a hex string. * NOT random function, to match test vectors.
* *
* The buffer is reversed so that the following are equivalent: * The following are equivalent:
* mpi_fill_random( x, len, not_rnd, str ); * mpi_fill_random( x, strlen( str ) / 2, not_rnd, str );
* mpi_read_string( x, 16, str ); * mpi_read_string( x, 16, str );
* (So, not random at all. Usefull to match test vectors.) * Warning: no other use is supported!
* Based on unhexify(), just reversed (changes marked by "sic")
*/ */
static int not_rnd( void *in, unsigned char *out, size_t len ) #define ciL (sizeof(t_uint)) /* chars in limb */
#define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
{ {
unsigned char *obuf; char *str = (char *) in;
const char *ibuf = in; mpi X;
unsigned char c, c2;
assert( len == strlen(ibuf) / 2 );
assert(!(strlen(ibuf) %1)); // must be even number of bytes
obuf = out + (len - 1); // sic /*
while (*ibuf != 0) * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
{ * just reconstruct the rest in order to be able to call mpi_read_string()
c = *ibuf++; */
if( c >= '0' && c <= '9' ) X.s = 1;
c -= '0'; X.p = (t_uint *) out;
else if( c >= 'a' && c <= 'f' ) X.n = CHARS_TO_LIMBS( len );
c -= 'a' - 10;
else if( c >= 'A' && c <= 'F' )
c -= 'A' - 10;
else
assert( 0 );
c2 = *ibuf++; /*
if( c2 >= '0' && c2 <= '9' ) * If str is too long, mpi_read_string() will try to allocate a new buffer
c2 -= '0'; * for X.p, which we want to avoid at all costs.
else if( c2 >= 'a' && c2 <= 'f' ) */
c2 -= 'a' - 10; assert( strlen( str ) / 2 == len );
else if( c2 >= 'A' && c2 <= 'F' )
c2 -= 'A' - 10;
else
assert( 0 );
*obuf-- = ( c << 4 ) | c2; // sic return( mpi_read_string( &X, 16, str ) );
}
return( 0 );
} }
#endif /* WANT_NOT_RND_MPI */

View file

@ -1,5 +1,6 @@
/* BEGIN_HEADER */ /* BEGIN_HEADER */
#include <polarssl/ecdh.h> #include <polarssl/ecdh.h>
#define WANT_NOT_RND_MPI
/* END_HEADER */ /* END_HEADER */
/* BEGIN_DEPENDENCIES /* BEGIN_DEPENDENCIES
@ -57,14 +58,14 @@ void ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str,
TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 ); TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &not_rnd, dA_str ) == 0 ); TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &not_rnd_mpi, dA_str ) == 0 );
TEST_ASSERT( ! ecp_is_zero( &qA ) ); TEST_ASSERT( ! ecp_is_zero( &qA ) );
TEST_ASSERT( mpi_read_string( &check, 16, xA_str ) == 0 ); TEST_ASSERT( mpi_read_string( &check, 16, xA_str ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &qA.X, &check ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &qA.X, &check ) == 0 );
TEST_ASSERT( mpi_read_string( &check, 16, yA_str ) == 0 ); TEST_ASSERT( mpi_read_string( &check, 16, yA_str ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &qA.Y, &check ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &qA.Y, &check ) == 0 );
TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &not_rnd, dB_str ) == 0 ); TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &not_rnd_mpi, dB_str ) == 0 );
TEST_ASSERT( ! ecp_is_zero( &qB ) ); TEST_ASSERT( ! ecp_is_zero( &qB ) );
TEST_ASSERT( mpi_read_string( &check, 16, xB_str ) == 0 ); TEST_ASSERT( mpi_read_string( &check, 16, xB_str ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &qB.X, &check ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &qB.X, &check ) == 0 );

View file

@ -1,5 +1,6 @@
/* BEGIN_HEADER */ /* BEGIN_HEADER */
#include <polarssl/ecdsa.h> #include <polarssl/ecdsa.h>
#define WANT_NOT_RND_MPI
/* END_HEADER */ /* END_HEADER */
/* BEGIN_DEPENDENCIES /* BEGIN_DEPENDENCIES
@ -63,7 +64,7 @@ void ecdsa_prim_test_vectors( int id, char *d_str, char *xQ_str, char *yQ_str,
len = unhexify(buf, hash_str); len = unhexify(buf, hash_str);
TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, len, TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, len,
&not_rnd, k_str ) == 0 ); &not_rnd_mpi, k_str ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &r, &r_check ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &r, &r_check ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &s, &s_check ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &s, &s_check ) == 0 );