1 #if !defined(POLARSSL_CONFIG_FILE) 4 #include POLARSSL_CONFIG_FILE 13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) 17 #if defined(POLARSSL_PLATFORM_C) 20 #define polarssl_malloc malloc 21 #define polarssl_free free 26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \ 41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 44 | ( (uint32_t) (b)[(i) + 3] ); \ 49 #define PUT_UINT32_BE(n,b,i) \ 51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 54 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__) 177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C) 309 #define polarssl_printf printf 310 #define polarssl_malloc malloc 311 #define polarssl_free free 316 #ifdef POLARSSL_GCM_C 318 #define TEST_SUITE_ACTIVE 320 static int test_assert(
int correct,
const char *test )
327 printf(
"FAILED\n" );
328 printf(
" %s\n", test );
333 #define TEST_ASSERT( TEST ) \ 334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \ 335 if( test_errors) goto exit; \ 340 if( (*str)[0] !=
'"' ||
341 (*str)[strlen( *str ) - 1] !=
'"' )
343 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
348 (*str)[strlen( *str ) - 1] =
'\0';
360 for( i = 0; i < strlen( str ); i++ )
362 if( i == 0 && str[i] ==
'-' )
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] ==
'0' && str[i] ==
'x' )
375 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
376 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
377 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
387 *value = strtol( str, NULL, 16 );
389 *value = strtol( str, NULL, 10 );
394 if( strcmp( str,
"POLARSSL_CIPHER_ID_AES" ) == 0 )
401 printf(
"Expected integer for parameter and got: %s\n", str );
405 void test_suite_gcm_encrypt_and_tag(
int cipher_id,
406 char *hex_key_string,
char *hex_src_string,
407 char *hex_iv_string,
char *hex_add_string,
408 char *hex_dst_string,
int tag_len_bits,
409 char *hex_tag_string,
int init_result )
411 unsigned char key_str[128];
412 unsigned char src_str[128];
413 unsigned char dst_str[257];
414 unsigned char iv_str[128];
415 unsigned char add_str[128];
416 unsigned char tag_str[128];
417 unsigned char output[128];
418 unsigned char tag_output[16];
420 unsigned int key_len;
421 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
423 memset(key_str, 0x00, 128);
424 memset(src_str, 0x00, 128);
425 memset(dst_str, 0x00, 257);
426 memset(iv_str, 0x00, 128);
427 memset(add_str, 0x00, 128);
428 memset(tag_str, 0x00, 128);
429 memset(output, 0x00, 128);
430 memset(tag_output, 0x00, 16);
432 key_len =
unhexify( key_str, hex_key_string );
433 pt_len =
unhexify( src_str, hex_src_string );
434 iv_len =
unhexify( iv_str, hex_iv_string );
435 add_len =
unhexify( add_str, hex_add_string );
438 if( init_result == 0 )
440 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 );
441 hexify( dst_str, output, pt_len );
442 hexify( tag_str, tag_output, tag_len );
444 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
445 TEST_ASSERT( strcmp( (
char *) tag_str, hex_tag_string ) == 0 );
452 void test_suite_gcm_decrypt_and_verify(
int cipher_id,
453 char *hex_key_string,
char *hex_src_string,
454 char *hex_iv_string,
char *hex_add_string,
455 int tag_len_bits,
char *hex_tag_string,
456 char *pt_result,
int init_result )
458 unsigned char key_str[128];
459 unsigned char src_str[128];
460 unsigned char dst_str[257];
461 unsigned char iv_str[128];
462 unsigned char add_str[128];
463 unsigned char tag_str[128];
464 unsigned char output[128];
466 unsigned int key_len;
467 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
470 memset(key_str, 0x00, 128);
471 memset(src_str, 0x00, 128);
472 memset(dst_str, 0x00, 257);
473 memset(iv_str, 0x00, 128);
474 memset(add_str, 0x00, 128);
475 memset(tag_str, 0x00, 128);
476 memset(output, 0x00, 128);
478 key_len =
unhexify( key_str, hex_key_string );
479 pt_len =
unhexify( src_str, hex_src_string );
480 iv_len =
unhexify( iv_str, hex_iv_string );
481 add_len =
unhexify( add_str, hex_add_string );
482 unhexify( tag_str, hex_tag_string );
485 if( init_result == 0 )
487 ret =
gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
489 if( strcmp(
"FAIL", pt_result ) == 0 )
496 hexify( dst_str, output, pt_len );
498 TEST_ASSERT( strcmp( (
char *) dst_str, pt_result ) == 0 );
506 #ifdef POLARSSL_SELF_TEST 507 void test_suite_gcm_selftest()
525 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
527 #if defined(POLARSSL_AES_C) 544 #if defined(TEST_SUITE_ACTIVE) 545 if( strcmp( params[0],
"gcm_encrypt_and_tag" ) == 0 )
549 char *param2 = params[2];
550 char *param3 = params[3];
551 char *param4 = params[4];
552 char *param5 = params[5];
553 char *param6 = params[6];
555 char *param8 = params[8];
560 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
564 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
570 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
572 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
574 test_suite_gcm_encrypt_and_tag( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
580 if( strcmp( params[0],
"gcm_decrypt_and_verify" ) == 0 )
584 char *param2 = params[2];
585 char *param3 = params[3];
586 char *param4 = params[4];
587 char *param5 = params[5];
589 char *param7 = params[7];
590 char *param8 = params[8];
595 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
599 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
604 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
607 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
609 test_suite_gcm_decrypt_and_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
615 if( strcmp( params[0],
"gcm_selftest" ) == 0 )
617 #ifdef POLARSSL_SELF_TEST 622 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
627 test_suite_gcm_selftest( );
636 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
650 ret = fgets( buf, len, f );
654 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
655 buf[strlen(buf) - 1] =
'\0';
656 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
657 buf[strlen(buf) - 1] =
'\0';
670 while( *p !=
'\0' && p < buf + len )
680 if( p + 1 < buf + len )
692 for( i = 0; i < cnt; i++ )
699 if( *p ==
'\\' && *(p + 1) ==
'n' )
704 else if( *p ==
'\\' && *(p + 1) ==
':' )
709 else if( *p ==
'\\' && *(p + 1) ==
'?' )
725 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
726 const char *filename =
"/root/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_gcm.aes192_de.data";
731 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) 732 unsigned char alloc_buf[1000000];
736 file = fopen( filename,
"r" );
739 fprintf( stderr,
"Failed to open\n" );
743 while( !feof( file ) )
747 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
749 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
750 fprintf( stdout,
" " );
751 for( i = strlen( buf ) + 1; i < 67; i++ )
752 fprintf( stdout,
"." );
753 fprintf( stdout,
" " );
758 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
762 if( strcmp( params[0],
"depends_on" ) == 0 )
764 for( i = 1; i < cnt; i++ )
768 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
779 if( skip == 1 || ret == 3 )
782 fprintf( stdout,
"----\n" );
787 fprintf( stdout,
"PASS\n" );
792 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
799 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
801 if( strlen(buf) != 0 )
803 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
809 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
810 if( total_errors == 0 )
811 fprintf( stdout,
"PASSED" );
813 fprintf( stdout,
"FAILED" );
815 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
816 total_tests - total_errors, total_tests, total_skipped );
818 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) 819 #if defined(POLARSSL_MEMORY_DEBUG) 820 memory_buffer_alloc_status();
825 return( total_errors != 0 );
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
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.
Memory allocation layer (Deprecated to platform layer)
int parse_arguments(char *buf, size_t len, char *params[50])
int get_line(FILE *f, char *buf, size_t len)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int gcm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
int dispatch_test(int cnt, char *params[50])
static int test_assert(int correct, const char *test)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
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.
#define PUT_UINT32_BE(n, b, i)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
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_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int gcm_init(gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
GCM initialization (encryption)
#define TEST_ASSERT(TEST)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
#define POLARSSL_ERR_GCM_AUTH_FAILED
Authenticated decryption failed.
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 rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_string(char **str)
void gcm_free(gcm_context *ctx)
Free a GCM context and underlying cipher sub-context.
static int unhexify(unsigned char *obuf, const char *ibuf)
Galois/Counter mode for 128-bit block ciphers.
int verify_int(char *str, int *value)