9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_GCM_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
366 printf(
"Expected integer for parameter and got: %s\n", str );
370 void test_suite_gcm_encrypt_and_tag(
char *hex_key_string,
char *hex_src_string,
371 char *hex_iv_string,
char *hex_add_string,
372 char *hex_dst_string,
int tag_len_bits,
373 char *hex_tag_string,
int init_result )
375 unsigned char key_str[128];
376 unsigned char src_str[128];
377 unsigned char dst_str[257];
378 unsigned char iv_str[128];
379 unsigned char add_str[128];
380 unsigned char tag_str[128];
381 unsigned char output[128];
382 unsigned char tag_output[16];
384 unsigned int key_len;
385 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
387 memset(key_str, 0x00, 128);
388 memset(src_str, 0x00, 128);
389 memset(dst_str, 0x00, 257);
390 memset(iv_str, 0x00, 128);
391 memset(add_str, 0x00, 128);
392 memset(tag_str, 0x00, 128);
393 memset(output, 0x00, 128);
394 memset(tag_output, 0x00, 16);
396 key_len =
unhexify( key_str, hex_key_string );
397 pt_len =
unhexify( src_str, hex_src_string );
398 iv_len =
unhexify( iv_str, hex_iv_string );
399 add_len =
unhexify( add_str, hex_add_string );
402 if( init_result == 0 )
404 TEST_ASSERT(
gcm_crypt_and_tag( &ctx,
GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 );
405 hexify( dst_str, output, pt_len );
406 hexify( tag_str, tag_output, tag_len );
408 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
409 TEST_ASSERT( strcmp( (
char *) tag_str, hex_tag_string ) == 0 );
415 void test_suite_gcm_decrypt_and_verify(
char *hex_key_string,
char *hex_src_string,
416 char *hex_iv_string,
char *hex_add_string,
417 int tag_len_bits,
char *hex_tag_string,
418 char *pt_result,
int init_result )
420 unsigned char key_str[128];
421 unsigned char src_str[128];
422 unsigned char dst_str[257];
423 unsigned char iv_str[128];
424 unsigned char add_str[128];
425 unsigned char tag_str[128];
426 unsigned char output[128];
428 unsigned int key_len;
429 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
432 memset(key_str, 0x00, 128);
433 memset(src_str, 0x00, 128);
434 memset(dst_str, 0x00, 257);
435 memset(iv_str, 0x00, 128);
436 memset(add_str, 0x00, 128);
437 memset(tag_str, 0x00, 128);
438 memset(output, 0x00, 128);
440 key_len =
unhexify( key_str, hex_key_string );
441 pt_len =
unhexify( src_str, hex_src_string );
442 iv_len =
unhexify( iv_str, hex_iv_string );
443 add_len =
unhexify( add_str, hex_add_string );
444 unhexify( tag_str, hex_tag_string );
447 if( init_result == 0 )
449 ret =
gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
451 if( strcmp(
"FAIL", pt_result ) == 0 )
458 hexify( dst_str, output, pt_len );
460 TEST_ASSERT( strcmp( (
char *) dst_str, pt_result ) == 0 );
467 #ifdef POLARSSL_SELF_TEST
468 void test_suite_gcm_selftest()
494 #if defined(TEST_SUITE_ACTIVE)
495 if( strcmp( params[0],
"gcm_encrypt_and_tag" ) == 0 )
498 char *param1 = params[1];
499 char *param2 = params[2];
500 char *param3 = params[3];
501 char *param4 = params[4];
502 char *param5 = params[5];
504 char *param7 = params[7];
509 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
518 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
520 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
522 test_suite_gcm_encrypt_and_tag( param1, param2, param3, param4, param5, param6, param7, param8 );
528 if( strcmp( params[0],
"gcm_decrypt_and_verify" ) == 0 )
531 char *param1 = params[1];
532 char *param2 = params[2];
533 char *param3 = params[3];
534 char *param4 = params[4];
536 char *param6 = params[6];
537 char *param7 = params[7];
542 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
550 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
553 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
555 test_suite_gcm_decrypt_and_verify( param1, param2, param3, param4, param5, param6, param7, param8 );
561 if( strcmp( params[0],
"gcm_selftest" ) == 0 )
563 #ifdef POLARSSL_SELF_TEST
568 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
573 test_suite_gcm_selftest( );
582 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
596 ret = fgets( buf, len, f );
600 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
601 buf[strlen(buf) - 1] =
'\0';
602 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
603 buf[strlen(buf) - 1] =
'\0';
616 while( *p !=
'\0' && p < buf + len )
626 if( p + 1 < buf + len )
638 for( i = 0; i < cnt; i++ )
645 if( *p ==
'\\' && *(p + 1) ==
'n' )
650 else if( *p ==
'\\' && *(p + 1) ==
':' )
655 else if( *p ==
'\\' && *(p + 1) ==
'?' )
671 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
672 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_gcm.encrypt_256.data";
677 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
678 unsigned char alloc_buf[1000000];
679 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
682 file = fopen( filename,
"r" );
685 fprintf( stderr,
"Failed to open\n" );
689 while( !feof( file ) )
693 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
695 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
696 fprintf( stdout,
" " );
697 for( i = strlen( buf ) + 1; i < 67; i++ )
698 fprintf( stdout,
"." );
699 fprintf( stdout,
" " );
704 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
708 if( strcmp( params[0],
"depends_on" ) == 0 )
710 for( i = 1; i < cnt; i++ )
714 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
725 if( skip == 1 || ret == 3 )
728 fprintf( stdout,
"----\n" );
733 fprintf( stdout,
"PASS\n" );
738 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
745 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
747 if( strlen(buf) != 0 )
749 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
755 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
756 if( total_errors == 0 )
757 fprintf( stdout,
"PASSED" );
759 fprintf( stdout,
"FAILED" );
761 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
762 total_tests - total_errors, total_tests, total_skipped );
764 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
765 #if defined(POLARSSL_MEMORY_DEBUG)
766 memory_buffer_alloc_status();
768 memory_buffer_alloc_free();
771 return( total_errors != 0 );
static int unhexify(unsigned char *obuf, const char *ibuf)
int gcm_auth_decrypt(gcm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *tag, size_t tag_len, const unsigned char *input, unsigned char *output)
GCM buffer authenticated decryption using a block cipher.
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Info structure for the pseudo random function.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int gcm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
#define TEST_ASSERT(TEST)
int gcm_crypt_and_tag(gcm_context *ctx, int mode, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, size_t tag_len, unsigned char *tag)
GCM buffer encryption/decryption using a block cipher.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int gcm_init(gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
GCM initialization (encryption)
int parse_arguments(char *buf, size_t len, char *params[50])
#define POLARSSL_ERR_GCM_AUTH_FAILED
Authenticated decryption failed.
int verify_string(char **str)
#define PUT_UINT32_BE(n, b, i)
void gcm_free(gcm_context *ctx)
Free a GCM context and underlying cipher sub-context.
int dispatch_test(int cnt, char *params[50])
Galois/Counter mode for 128-bit block ciphers.
int verify_int(char *str, int *value)
int get_line(FILE *f, char *buf, size_t len)