00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_GCM_C
00004
00005 #include <polarssl/gcm.h>
00006 #endif
00007
00008
00009 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00010 #include "polarssl/memory.h"
00011 #endif
00012
00013 #if defined(WANT_NOT_RND_MPI)
00014 #if defined(POLARSSL_BIGNUM_C)
00015 #include "polarssl/bignum.h"
00016 #else
00017 #error "not_rnd_mpi() need bignum.c"
00018 #endif
00019 #endif
00020
00021 #ifdef _MSC_VER
00022 #include <basetsd.h>
00023 typedef UINT32 uint32_t;
00024 #else
00025 #include <inttypes.h>
00026 #endif
00027
00028 #include <assert.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031
00032
00033
00034
00035 #ifndef GET_UINT32_BE
00036 #define GET_UINT32_BE(n,b,i) \
00037 { \
00038 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00039 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00040 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00041 | ( (uint32_t) (b)[(i) + 3] ); \
00042 }
00043 #endif
00044
00045 #ifndef PUT_UINT32_BE
00046 #define PUT_UINT32_BE(n,b,i) \
00047 { \
00048 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00049 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00050 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00051 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00052 }
00053 #endif
00054
00055 static int unhexify(unsigned char *obuf, const char *ibuf)
00056 {
00057 unsigned char c, c2;
00058 int len = strlen(ibuf) / 2;
00059 assert(!(strlen(ibuf) %1));
00060
00061 while (*ibuf != 0)
00062 {
00063 c = *ibuf++;
00064 if( c >= '0' && c <= '9' )
00065 c -= '0';
00066 else if( c >= 'a' && c <= 'f' )
00067 c -= 'a' - 10;
00068 else if( c >= 'A' && c <= 'F' )
00069 c -= 'A' - 10;
00070 else
00071 assert( 0 );
00072
00073 c2 = *ibuf++;
00074 if( c2 >= '0' && c2 <= '9' )
00075 c2 -= '0';
00076 else if( c2 >= 'a' && c2 <= 'f' )
00077 c2 -= 'a' - 10;
00078 else if( c2 >= 'A' && c2 <= 'F' )
00079 c2 -= 'A' - 10;
00080 else
00081 assert( 0 );
00082
00083 *obuf++ = ( c << 4 ) | c2;
00084 }
00085
00086 return len;
00087 }
00088
00089 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00090 {
00091 unsigned char l, h;
00092
00093 while (len != 0)
00094 {
00095 h = (*ibuf) / 16;
00096 l = (*ibuf) % 16;
00097
00098 if( h < 10 )
00099 *obuf++ = '0' + h;
00100 else
00101 *obuf++ = 'a' + h - 10;
00102
00103 if( l < 10 )
00104 *obuf++ = '0' + l;
00105 else
00106 *obuf++ = 'a' + l - 10;
00107
00108 ++ibuf;
00109 len--;
00110 }
00111 }
00112
00122 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00123 {
00124 size_t i;
00125
00126 if( rng_state != NULL )
00127 rng_state = NULL;
00128
00129 for( i = 0; i < len; ++i )
00130 output[i] = rand();
00131
00132 return( 0 );
00133 }
00134
00140 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00141 {
00142 if( rng_state != NULL )
00143 rng_state = NULL;
00144
00145 memset( output, 0, len );
00146
00147 return( 0 );
00148 }
00149
00150 typedef struct
00151 {
00152 unsigned char *buf;
00153 size_t length;
00154 } rnd_buf_info;
00155
00167 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00168 {
00169 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00170 size_t use_len;
00171
00172 if( rng_state == NULL )
00173 return( rnd_std_rand( NULL, output, len ) );
00174
00175 use_len = len;
00176 if( len > info->length )
00177 use_len = info->length;
00178
00179 if( use_len )
00180 {
00181 memcpy( output, info->buf, use_len );
00182 info->buf += use_len;
00183 info->length -= use_len;
00184 }
00185
00186 if( len - use_len > 0 )
00187 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00188
00189 return( 0 );
00190 }
00191
00199 typedef struct
00200 {
00201 uint32_t key[16];
00202 uint32_t v0, v1;
00203 } rnd_pseudo_info;
00204
00213 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00214 {
00215 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00216 uint32_t i, *k, sum, delta=0x9E3779B9;
00217 unsigned char result[4];
00218
00219 if( rng_state == NULL )
00220 return( rnd_std_rand( NULL, output, len ) );
00221
00222 k = info->key;
00223
00224 while( len > 0 )
00225 {
00226 size_t use_len = ( len > 4 ) ? 4 : len;
00227 sum = 0;
00228
00229 for( i = 0; i < 32; i++ )
00230 {
00231 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00232 sum += delta;
00233 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00234 }
00235
00236 PUT_UINT32_BE( info->v0, result, 0 );
00237 memcpy( output, result, use_len );
00238 len -= use_len;
00239 }
00240
00241 return( 0 );
00242 }
00243
00244 #if defined(WANT_NOT_RND_MPI)
00245
00253 #define ciL (sizeof(t_uint))
00254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00255 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00256 {
00257 char *str = (char *) in;
00258 mpi X;
00259
00260
00261
00262
00263
00264 X.s = 1;
00265 X.p = (t_uint *) out;
00266 X.n = CHARS_TO_LIMBS( len );
00267
00268
00269
00270
00271
00272 assert( strlen( str ) / 2 == len );
00273
00274 return( mpi_read_string( &X, 16, str ) );
00275 }
00276 #endif
00277
00278
00279 #include <stdio.h>
00280 #include <string.h>
00281
00282 static int test_errors = 0;
00283
00284 #ifdef POLARSSL_GCM_C
00285
00286 #define TEST_SUITE_ACTIVE
00287
00288 static int test_assert( int correct, char *test )
00289 {
00290 if( correct )
00291 return( 0 );
00292
00293 test_errors++;
00294 if( test_errors == 1 )
00295 printf( "FAILED\n" );
00296 printf( " %s\n", test );
00297
00298 return( 1 );
00299 }
00300
00301 #define TEST_ASSERT( TEST ) \
00302 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00303 if( test_errors) return; \
00304 } while (0)
00305
00306 int verify_string( char **str )
00307 {
00308 if( (*str)[0] != '"' ||
00309 (*str)[strlen( *str ) - 1] != '"' )
00310 {
00311 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00312 return( -1 );
00313 }
00314
00315 (*str)++;
00316 (*str)[strlen( *str ) - 1] = '\0';
00317
00318 return( 0 );
00319 }
00320
00321 int verify_int( char *str, int *value )
00322 {
00323 size_t i;
00324 int minus = 0;
00325 int digits = 1;
00326 int hex = 0;
00327
00328 for( i = 0; i < strlen( str ); i++ )
00329 {
00330 if( i == 0 && str[i] == '-' )
00331 {
00332 minus = 1;
00333 continue;
00334 }
00335
00336 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00337 str[i - 1] == '0' && str[i] == 'x' )
00338 {
00339 hex = 1;
00340 continue;
00341 }
00342
00343 if( str[i] < '0' || str[i] > '9' )
00344 {
00345 digits = 0;
00346 break;
00347 }
00348 }
00349
00350 if( digits )
00351 {
00352 if( hex )
00353 *value = strtol( str, NULL, 16 );
00354 else
00355 *value = strtol( str, NULL, 10 );
00356
00357 return( 0 );
00358 }
00359
00360 if( strcmp( str, "POLARSSL_CIPHER_ID_AES" ) == 0 )
00361 {
00362 *value = ( POLARSSL_CIPHER_ID_AES );
00363 return( 0 );
00364 }
00365
00366
00367 printf( "Expected integer for parameter and got: %s\n", str );
00368 return( -1 );
00369 }
00370
00371 void test_suite_gcm_encrypt_and_tag( int cipher_id,
00372 char *hex_key_string, char *hex_src_string,
00373 char *hex_iv_string, char *hex_add_string,
00374 char *hex_dst_string, int tag_len_bits,
00375 char *hex_tag_string, int init_result )
00376 {
00377 unsigned char key_str[128];
00378 unsigned char src_str[128];
00379 unsigned char dst_str[257];
00380 unsigned char iv_str[128];
00381 unsigned char add_str[128];
00382 unsigned char tag_str[128];
00383 unsigned char output[128];
00384 unsigned char tag_output[16];
00385 gcm_context ctx;
00386 unsigned int key_len;
00387 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
00388
00389 memset(key_str, 0x00, 128);
00390 memset(src_str, 0x00, 128);
00391 memset(dst_str, 0x00, 257);
00392 memset(iv_str, 0x00, 128);
00393 memset(add_str, 0x00, 128);
00394 memset(tag_str, 0x00, 128);
00395 memset(output, 0x00, 128);
00396 memset(tag_output, 0x00, 16);
00397
00398 key_len = unhexify( key_str, hex_key_string );
00399 pt_len = unhexify( src_str, hex_src_string );
00400 iv_len = unhexify( iv_str, hex_iv_string );
00401 add_len = unhexify( add_str, hex_add_string );
00402
00403 TEST_ASSERT( gcm_init( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
00404 if( init_result == 0 )
00405 {
00406 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 );
00407 hexify( dst_str, output, pt_len );
00408 hexify( tag_str, tag_output, tag_len );
00409
00410 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
00411 TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
00412 }
00413
00414 gcm_free( &ctx );
00415 }
00416
00417 void test_suite_gcm_decrypt_and_verify( int cipher_id,
00418 char *hex_key_string, char *hex_src_string,
00419 char *hex_iv_string, char *hex_add_string,
00420 int tag_len_bits, char *hex_tag_string,
00421 char *pt_result, int init_result )
00422 {
00423 unsigned char key_str[128];
00424 unsigned char src_str[128];
00425 unsigned char dst_str[257];
00426 unsigned char iv_str[128];
00427 unsigned char add_str[128];
00428 unsigned char tag_str[128];
00429 unsigned char output[128];
00430 gcm_context ctx;
00431 unsigned int key_len;
00432 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
00433 int ret;
00434
00435 memset(key_str, 0x00, 128);
00436 memset(src_str, 0x00, 128);
00437 memset(dst_str, 0x00, 257);
00438 memset(iv_str, 0x00, 128);
00439 memset(add_str, 0x00, 128);
00440 memset(tag_str, 0x00, 128);
00441 memset(output, 0x00, 128);
00442
00443 key_len = unhexify( key_str, hex_key_string );
00444 pt_len = unhexify( src_str, hex_src_string );
00445 iv_len = unhexify( iv_str, hex_iv_string );
00446 add_len = unhexify( add_str, hex_add_string );
00447 unhexify( tag_str, hex_tag_string );
00448
00449 TEST_ASSERT( gcm_init( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
00450 if( init_result == 0 )
00451 {
00452 ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
00453
00454 if( strcmp( "FAIL", pt_result ) == 0 )
00455 {
00456 TEST_ASSERT( ret == POLARSSL_ERR_GCM_AUTH_FAILED );
00457 }
00458 else
00459 {
00460 TEST_ASSERT( ret == 0 );
00461 hexify( dst_str, output, pt_len );
00462
00463 TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
00464 }
00465 }
00466
00467 gcm_free( &ctx );
00468 }
00469
00470 #ifdef POLARSSL_SELF_TEST
00471 void test_suite_gcm_selftest()
00472 {
00473 TEST_ASSERT( gcm_self_test( 0 ) == 0 );
00474 }
00475 #endif
00476
00477
00478 #endif
00479
00480
00481 int dep_check( char *str )
00482 {
00483 if( str == NULL )
00484 return( 1 );
00485
00486 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
00487 {
00488 #if defined(POLARSSL_AES_C)
00489 return( 0 );
00490 #else
00491 return( 1 );
00492 #endif
00493 }
00494
00495
00496 return( 1 );
00497 }
00498
00499 int dispatch_test(int cnt, char *params[50])
00500 {
00501 int ret;
00502 ((void) cnt);
00503 ((void) params);
00504
00505 #if defined(TEST_SUITE_ACTIVE)
00506 if( strcmp( params[0], "gcm_encrypt_and_tag" ) == 0 )
00507 {
00508
00509 int param1;
00510 char *param2 = params[2];
00511 char *param3 = params[3];
00512 char *param4 = params[4];
00513 char *param5 = params[5];
00514 char *param6 = params[6];
00515 int param7;
00516 char *param8 = params[8];
00517 int param9;
00518
00519 if( cnt != 10 )
00520 {
00521 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
00522 return( 2 );
00523 }
00524
00525 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00526 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00527 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00528 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00529 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00530 if( verify_string( ¶m6 ) != 0 ) return( 2 );
00531 if( verify_int( params[7], ¶m7 ) != 0 ) return( 2 );
00532 if( verify_string( ¶m8 ) != 0 ) return( 2 );
00533 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
00534
00535 test_suite_gcm_encrypt_and_tag( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
00536 return ( 0 );
00537
00538 return ( 3 );
00539 }
00540 else
00541 if( strcmp( params[0], "gcm_decrypt_and_verify" ) == 0 )
00542 {
00543
00544 int param1;
00545 char *param2 = params[2];
00546 char *param3 = params[3];
00547 char *param4 = params[4];
00548 char *param5 = params[5];
00549 int param6;
00550 char *param7 = params[7];
00551 char *param8 = params[8];
00552 int param9;
00553
00554 if( cnt != 10 )
00555 {
00556 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
00557 return( 2 );
00558 }
00559
00560 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00561 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00562 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00563 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00564 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00565 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
00566 if( verify_string( ¶m7 ) != 0 ) return( 2 );
00567 if( verify_string( ¶m8 ) != 0 ) return( 2 );
00568 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
00569
00570 test_suite_gcm_decrypt_and_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
00571 return ( 0 );
00572
00573 return ( 3 );
00574 }
00575 else
00576 if( strcmp( params[0], "gcm_selftest" ) == 0 )
00577 {
00578 #ifdef POLARSSL_SELF_TEST
00579
00580
00581 if( cnt != 1 )
00582 {
00583 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
00584 return( 2 );
00585 }
00586
00587
00588 test_suite_gcm_selftest( );
00589 return ( 0 );
00590 #endif
00591
00592 return ( 3 );
00593 }
00594 else
00595
00596 {
00597 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00598 fflush( stdout );
00599 return( 1 );
00600 }
00601 #else
00602 return( 3 );
00603 #endif
00604 return( ret );
00605 }
00606
00607 int get_line( FILE *f, char *buf, size_t len )
00608 {
00609 char *ret;
00610
00611 ret = fgets( buf, len, f );
00612 if( ret == NULL )
00613 return( -1 );
00614
00615 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00616 buf[strlen(buf) - 1] = '\0';
00617 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00618 buf[strlen(buf) - 1] = '\0';
00619
00620 return( 0 );
00621 }
00622
00623 int parse_arguments( char *buf, size_t len, char *params[50] )
00624 {
00625 int cnt = 0, i;
00626 char *cur = buf;
00627 char *p = buf, *q;
00628
00629 params[cnt++] = cur;
00630
00631 while( *p != '\0' && p < buf + len )
00632 {
00633 if( *p == '\\' )
00634 {
00635 *p++;
00636 *p++;
00637 continue;
00638 }
00639 if( *p == ':' )
00640 {
00641 if( p + 1 < buf + len )
00642 {
00643 cur = p + 1;
00644 params[cnt++] = cur;
00645 }
00646 *p = '\0';
00647 }
00648
00649 *p++;
00650 }
00651
00652
00653 for( i = 0; i < cnt; i++ )
00654 {
00655 p = params[i];
00656 q = params[i];
00657
00658 while( *p != '\0' )
00659 {
00660 if( *p == '\\' && *(p + 1) == 'n' )
00661 {
00662 p += 2;
00663 *(q++) = '\n';
00664 }
00665 else if( *p == '\\' && *(p + 1) == ':' )
00666 {
00667 p += 2;
00668 *(q++) = ':';
00669 }
00670 else if( *p == '\\' && *(p + 1) == '?' )
00671 {
00672 p += 2;
00673 *(q++) = '?';
00674 }
00675 else
00676 *(q++) = *(p++);
00677 }
00678 *q = '\0';
00679 }
00680
00681 return( cnt );
00682 }
00683
00684 int main()
00685 {
00686 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00687 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_gcm.aes128_de.data";
00688 FILE *file;
00689 char buf[5000];
00690 char *params[50];
00691
00692 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00693 unsigned char alloc_buf[1000000];
00694 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00695 #endif
00696
00697 file = fopen( filename, "r" );
00698 if( file == NULL )
00699 {
00700 fprintf( stderr, "Failed to open\n" );
00701 return( 1 );
00702 }
00703
00704 while( !feof( file ) )
00705 {
00706 int skip = 0;
00707
00708 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00709 break;
00710 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00711 fprintf( stdout, " " );
00712 for( i = strlen( buf ) + 1; i < 67; i++ )
00713 fprintf( stdout, "." );
00714 fprintf( stdout, " " );
00715 fflush( stdout );
00716
00717 total_tests++;
00718
00719 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00720 break;
00721 cnt = parse_arguments( buf, strlen(buf), params );
00722
00723 if( strcmp( params[0], "depends_on" ) == 0 )
00724 {
00725 for( i = 1; i < cnt; i++ )
00726 if( dep_check( params[i] ) != 0 )
00727 skip = 1;
00728
00729 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00730 break;
00731 cnt = parse_arguments( buf, strlen(buf), params );
00732 }
00733
00734 if( skip == 0 )
00735 {
00736 test_errors = 0;
00737 ret = dispatch_test( cnt, params );
00738 }
00739
00740 if( skip == 1 || ret == 3 )
00741 {
00742 total_skipped++;
00743 fprintf( stdout, "----\n" );
00744 fflush( stdout );
00745 }
00746 else if( ret == 0 && test_errors == 0 )
00747 {
00748 fprintf( stdout, "PASS\n" );
00749 fflush( stdout );
00750 }
00751 else if( ret == 2 )
00752 {
00753 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
00754 fclose(file);
00755 exit( 2 );
00756 }
00757 else
00758 total_errors++;
00759
00760 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00761 break;
00762 if( strlen(buf) != 0 )
00763 {
00764 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
00765 return( 1 );
00766 }
00767 }
00768 fclose(file);
00769
00770 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
00771 if( total_errors == 0 )
00772 fprintf( stdout, "PASSED" );
00773 else
00774 fprintf( stdout, "FAILED" );
00775
00776 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
00777 total_tests - total_errors, total_tests, total_skipped );
00778
00779 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00780 #if defined(POLARSSL_MEMORY_DEBUG)
00781 memory_buffer_alloc_status();
00782 #endif
00783 memory_buffer_alloc_free();
00784 #endif
00785
00786 return( total_errors != 0 );
00787 }
00788
00789