PolarSSL v1.3.9
test_suite_hmac_drbg.nopr.c
Go to the documentation of this file.
1#if !defined(POLARSSL_CONFIG_FILE)
2#include <polarssl/config.h>
3#else
4#include POLARSSL_CONFIG_FILE
5#endif
6
7#ifdef POLARSSL_HMAC_DRBG_C
8
10
11typedef struct
12{
13 unsigned char *p;
14 size_t len;
15} entropy_ctx;
16
17int entropy_func( void *data, unsigned char *buf, size_t len )
18{
19 entropy_ctx *ctx = (entropy_ctx *) data;
20
21 if( len > ctx->len )
22 return( -1 );
23
24 memcpy( buf, ctx->p, len );
25
26 ctx->p += len;
27 ctx->len -= len;
28
29 return( 0 );
30}
31#endif /* POLARSSL_HMAC_DRBG_C */
32
33
34#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
35#include "polarssl/memory.h"
36#endif
37
38#if defined(POLARSSL_PLATFORM_C)
39#include "polarssl/platform.h"
40#else
41#define polarssl_malloc malloc
42#define polarssl_free free
43#endif
44
45#ifdef _MSC_VER
46#include <basetsd.h>
47typedef UINT32 uint32_t;
48#else
49#include <inttypes.h>
50#endif
51
52#include <assert.h>
53#include <stdlib.h>
54#include <string.h>
55
56/*
57 * 32-bit integer manipulation macros (big endian)
58 */
59#ifndef GET_UINT32_BE
60#define GET_UINT32_BE(n,b,i) \
61{ \
62 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65 | ( (uint32_t) (b)[(i) + 3] ); \
66}
67#endif
68
69#ifndef PUT_UINT32_BE
70#define PUT_UINT32_BE(n,b,i) \
71{ \
72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76}
77#endif
78
79static int unhexify(unsigned char *obuf, const char *ibuf)
80{
81 unsigned char c, c2;
82 int len = strlen(ibuf) / 2;
83 assert(!(strlen(ibuf) %1)); // must be even number of bytes
84
85 while (*ibuf != 0)
86 {
87 c = *ibuf++;
88 if( c >= '0' && c <= '9' )
89 c -= '0';
90 else if( c >= 'a' && c <= 'f' )
91 c -= 'a' - 10;
92 else if( c >= 'A' && c <= 'F' )
93 c -= 'A' - 10;
94 else
95 assert( 0 );
96
97 c2 = *ibuf++;
98 if( c2 >= '0' && c2 <= '9' )
99 c2 -= '0';
100 else if( c2 >= 'a' && c2 <= 'f' )
101 c2 -= 'a' - 10;
102 else if( c2 >= 'A' && c2 <= 'F' )
103 c2 -= 'A' - 10;
104 else
105 assert( 0 );
106
107 *obuf++ = ( c << 4 ) | c2;
108 }
109
110 return len;
111}
112
113static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
114{
115 unsigned char l, h;
116
117 while (len != 0)
118 {
119 h = (*ibuf) / 16;
120 l = (*ibuf) % 16;
121
122 if( h < 10 )
123 *obuf++ = '0' + h;
124 else
125 *obuf++ = 'a' + h - 10;
126
127 if( l < 10 )
128 *obuf++ = '0' + l;
129 else
130 *obuf++ = 'a' + l - 10;
131
132 ++ibuf;
133 len--;
134 }
135}
136
144static unsigned char *zero_alloc( size_t len )
145{
146 void *p;
147 size_t actual_len = len != 0 ? len : 1;
148
149 p = polarssl_malloc( actual_len );
150 assert( p != NULL );
151
152 memset( p, 0x00, actual_len );
153
154 return( p );
155}
156
167static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
168{
169 unsigned char *obuf;
170
171 *olen = strlen(ibuf) / 2;
172
173 if( *olen == 0 )
174 return( zero_alloc( *olen ) );
175
176 obuf = polarssl_malloc( *olen );
177 assert( obuf != NULL );
178
179 (void) unhexify( obuf, ibuf );
180
181 return( obuf );
182}
183
193static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
194{
195#if !defined(__OpenBSD__)
196 size_t i;
197
198 if( rng_state != NULL )
199 rng_state = NULL;
200
201 for( i = 0; i < len; ++i )
202 output[i] = rand();
203#else
204 if( rng_state != NULL )
205 rng_state = NULL;
206
207 arc4random_buf( output, len );
208#endif /* !OpenBSD */
209
210 return( 0 );
211}
212
218static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
219{
220 if( rng_state != NULL )
221 rng_state = NULL;
222
223 memset( output, 0, len );
224
225 return( 0 );
226}
227
228typedef struct
229{
230 unsigned char *buf;
231 size_t length;
233
245static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
246{
247 rnd_buf_info *info = (rnd_buf_info *) rng_state;
248 size_t use_len;
249
250 if( rng_state == NULL )
251 return( rnd_std_rand( NULL, output, len ) );
252
253 use_len = len;
254 if( len > info->length )
255 use_len = info->length;
256
257 if( use_len )
258 {
259 memcpy( output, info->buf, use_len );
260 info->buf += use_len;
261 info->length -= use_len;
262 }
263
264 if( len - use_len > 0 )
265 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
266
267 return( 0 );
268}
269
277typedef struct
278{
279 uint32_t key[16];
280 uint32_t v0, v1;
282
291static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
292{
293 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
294 uint32_t i, *k, sum, delta=0x9E3779B9;
295 unsigned char result[4], *out = output;
296
297 if( rng_state == NULL )
298 return( rnd_std_rand( NULL, output, len ) );
299
300 k = info->key;
301
302 while( len > 0 )
303 {
304 size_t use_len = ( len > 4 ) ? 4 : len;
305 sum = 0;
306
307 for( i = 0; i < 32; i++ )
308 {
309 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
310 sum += delta;
311 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
312 }
313
314 PUT_UINT32_BE( info->v0, result, 0 );
315 memcpy( out, result, use_len );
316 len -= use_len;
317 out += 4;
318 }
319
320 return( 0 );
321}
322
323
324#include <stdio.h>
325#include <string.h>
326
327#if defined(POLARSSL_PLATFORM_C)
328#include "polarssl/platform.h"
329#else
330#define polarssl_printf printf
331#define polarssl_malloc malloc
332#define polarssl_free free
333#endif
334
335static int test_errors = 0;
336
337#ifdef POLARSSL_HMAC_DRBG_C
338
339#define TEST_SUITE_ACTIVE
340
341static int test_assert( int correct, const char *test )
342{
343 if( correct )
344 return( 0 );
345
346 test_errors++;
347 if( test_errors == 1 )
348 printf( "FAILED\n" );
349 printf( " %s\n", test );
350
351 return( 1 );
352}
353
354#define TEST_ASSERT( TEST ) \
355 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
356 if( test_errors) goto exit; \
357 } while (0)
358
359int verify_string( char **str )
360{
361 if( (*str)[0] != '"' ||
362 (*str)[strlen( *str ) - 1] != '"' )
363 {
364 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
365 return( -1 );
366 }
367
368 (*str)++;
369 (*str)[strlen( *str ) - 1] = '\0';
370
371 return( 0 );
372}
373
374int verify_int( char *str, int *value )
375{
376 size_t i;
377 int minus = 0;
378 int digits = 1;
379 int hex = 0;
380
381 for( i = 0; i < strlen( str ); i++ )
382 {
383 if( i == 0 && str[i] == '-' )
384 {
385 minus = 1;
386 continue;
387 }
388
389 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
390 str[i - 1] == '0' && str[i] == 'x' )
391 {
392 hex = 1;
393 continue;
394 }
395
396 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
397 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
398 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
399 {
400 digits = 0;
401 break;
402 }
403 }
404
405 if( digits )
406 {
407 if( hex )
408 *value = strtol( str, NULL, 16 );
409 else
410 *value = strtol( str, NULL, 10 );
411
412 return( 0 );
413 }
414
415 if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
416 {
417 *value = ( POLARSSL_MD_SHA224 );
418 return( 0 );
419 }
420 if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
421 {
422 *value = ( POLARSSL_MD_SHA256 );
423 return( 0 );
424 }
425 if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
426 {
427 *value = ( POLARSSL_MD_SHA384 );
428 return( 0 );
429 }
430 if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
431 {
432 *value = ( POLARSSL_MD_SHA512 );
433 return( 0 );
434 }
435 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
436 {
437 *value = ( POLARSSL_MD_SHA1 );
438 return( 0 );
439 }
440
441
442 printf( "Expected integer for parameter and got: %s\n", str );
443 return( -1 );
444}
445
446void test_suite_hmac_drbg_entropy_usage( int md_alg )
447{
448 unsigned char out[16];
449 unsigned char buf[1024];
450 const md_info_t *md_info;
452 entropy_ctx entropy;
453 size_t last_len, i, reps = 10;
454
455 memset( buf, 0, sizeof( buf ) );
456 memset( out, 0, sizeof( out ) );
457
458 entropy.len = sizeof( buf );
459 entropy.p = buf;
460
461 md_info = md_info_from_type( md_alg );
462 TEST_ASSERT( md_info != NULL );
463
464 /* Init must use entropy */
465 last_len = entropy.len;
466 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &entropy,
467 NULL, 0 ) == 0 );
468 TEST_ASSERT( entropy.len < last_len );
469
470 /* By default, PR is off and reseed_interval is large,
471 * so the next few calls should not use entropy */
472 last_len = entropy.len;
473 for( i = 0; i < reps; i++ )
474 {
475 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
476 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
477 buf, 16 ) == 0 );
478 }
479 TEST_ASSERT( entropy.len == last_len );
480
481 /* While at it, make sure we didn't write past the requested length */
482 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
483 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
484 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
485 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
486
487 /* Set reseed_interval to the number of calls done,
488 * so the next call should reseed */
489 hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
490 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
491 TEST_ASSERT( entropy.len < last_len );
492
493 /* The new few calls should not reseed */
494 last_len = entropy.len;
495 for( i = 0; i < reps / 2; i++ )
496 {
497 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
498 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
499 buf, 16 ) == 0 );
500 }
501 TEST_ASSERT( entropy.len == last_len );
502
503 /* Now enable PR, so the next few calls should all reseed */
505 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
506 TEST_ASSERT( entropy.len < last_len );
507
508 /* Finally, check setting entropy_len */
509 hmac_drbg_set_entropy_len( &ctx, 42 );
510 last_len = entropy.len;
511 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
512 TEST_ASSERT( (int) last_len - entropy.len == 42 );
513
514 hmac_drbg_set_entropy_len( &ctx, 13 );
515 last_len = entropy.len;
516 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
517 TEST_ASSERT( (int) last_len - entropy.len == 13 );
518
519exit:
520 hmac_drbg_free( &ctx );
521}
522
523#ifdef POLARSSL_FS_IO
524void test_suite_hmac_drbg_seed_file( int md_alg, char *path, int ret )
525{
526 const md_info_t *md_info;
528
529 md_info = md_info_from_type( md_alg );
530 TEST_ASSERT( md_info != NULL );
531
532 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
533 NULL, 0 ) == 0 );
534
535 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
536 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
537
538exit:
539 hmac_drbg_free( &ctx );
540}
541#endif /* POLARSSL_FS_IO */
542
543void test_suite_hmac_drbg_buf( int md_alg )
544{
545 unsigned char out[16];
546 unsigned char buf[100];
547 const md_info_t *md_info;
549 size_t i;
550
551 memset( buf, 0, sizeof( buf ) );
552 memset( out, 0, sizeof( out ) );
553
554 md_info = md_info_from_type( md_alg );
555 TEST_ASSERT( md_info != NULL );
556 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
557
558 /* Make sure it never tries to reseed (would segfault otherwise) */
561
562 for( i = 0; i < 30; i++ )
563 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
564
565exit:
566 hmac_drbg_free( &ctx );
567}
568
569void test_suite_hmac_drbg_no_reseed( int md_alg,
570 char *entropy_hex, char *custom_hex,
571 char *add1_hex, char *add2_hex,
572 char *output_hex )
573{
574 unsigned char data[1024];
575 unsigned char entropy[512];
576 unsigned char custom[512];
577 unsigned char add1[512];
578 unsigned char add2[512];
579 unsigned char output[512];
580 unsigned char my_output[512];
581 size_t custom_len, add1_len, add2_len, out_len;
582 entropy_ctx p_entropy;
583 const md_info_t *md_info;
585
586 memset( my_output, 0, sizeof my_output );
587
588 custom_len = unhexify( custom, custom_hex );
589 add1_len = unhexify( add1, add1_hex );
590 add2_len = unhexify( add2, add2_hex );
591 out_len = unhexify( output, output_hex );
592 p_entropy.len = unhexify( entropy, entropy_hex );
593 p_entropy.p = entropy;
594
595 md_info = md_info_from_type( md_alg );
596 TEST_ASSERT( md_info != NULL );
597
598 /* Test the simplified buffer-based variant */
599 memcpy( data, entropy, p_entropy.len );
600 memcpy( data + p_entropy.len, custom, custom_len );
601 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info,
602 data, p_entropy.len + custom_len ) == 0 );
603 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
604 add1, add1_len ) == 0 );
605 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
606 add2, add2_len ) == 0 );
607
608 /* clear for second run */
609 hmac_drbg_free( &ctx );
610
611 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
612
613 /* And now the normal entropy-based variant */
614 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
615 custom, custom_len ) == 0 );
616 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
617 add1, add1_len ) == 0 );
618 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
619 add2, add2_len ) == 0 );
620 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
621
622exit:
623 hmac_drbg_free( &ctx );
624}
625
626void test_suite_hmac_drbg_nopr( int md_alg,
627 char *entropy_hex, char *custom_hex,
628 char *add1_hex, char *add2_hex, char *add3_hex,
629 char *output_hex )
630{
631 unsigned char entropy[512];
632 unsigned char custom[512];
633 unsigned char add1[512];
634 unsigned char add2[512];
635 unsigned char add3[512];
636 unsigned char output[512];
637 unsigned char my_output[512];
638 size_t custom_len, add1_len, add2_len, add3_len, out_len;
639 entropy_ctx p_entropy;
640 const md_info_t *md_info;
642
643 memset( my_output, 0, sizeof my_output );
644
645 custom_len = unhexify( custom, custom_hex );
646 add1_len = unhexify( add1, add1_hex );
647 add2_len = unhexify( add2, add2_hex );
648 add3_len = unhexify( add3, add3_hex );
649 out_len = unhexify( output, output_hex );
650 p_entropy.len = unhexify( entropy, entropy_hex );
651 p_entropy.p = entropy;
652
653 md_info = md_info_from_type( md_alg );
654 TEST_ASSERT( md_info != NULL );
655
656 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
657 custom, custom_len ) == 0 );
658 TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
659 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
660 add2, add2_len ) == 0 );
661 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
662 add3, add3_len ) == 0 );
663
664 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
665
666exit:
667 hmac_drbg_free( &ctx );
668}
669
670void test_suite_hmac_drbg_pr( int md_alg,
671 char *entropy_hex, char *custom_hex,
672 char *add1_hex, char *add2_hex,
673 char *output_hex )
674{
675 unsigned char entropy[512];
676 unsigned char custom[512];
677 unsigned char add1[512];
678 unsigned char add2[512];
679 unsigned char output[512];
680 unsigned char my_output[512];
681 size_t custom_len, add1_len, add2_len, out_len;
682 entropy_ctx p_entropy;
683 const md_info_t *md_info;
685
686 memset( my_output, 0, sizeof my_output );
687
688 custom_len = unhexify( custom, custom_hex );
689 add1_len = unhexify( add1, add1_hex );
690 add2_len = unhexify( add2, add2_hex );
691 out_len = unhexify( output, output_hex );
692 p_entropy.len = unhexify( entropy, entropy_hex );
693 p_entropy.p = entropy;
694
695 md_info = md_info_from_type( md_alg );
696 TEST_ASSERT( md_info != NULL );
697
698 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
699 custom, custom_len ) == 0 );
701 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
702 add1, add1_len ) == 0 );
703 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
704 add2, add2_len ) == 0 );
705
706 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
707
708exit:
709 hmac_drbg_free( &ctx );
710}
711
712#ifdef POLARSSL_SELF_TEST
713void test_suite_hmac_drbg_selftest( )
714{
715 TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 );
716
717exit:
718 return;
719}
720#endif /* POLARSSL_SELF_TEST */
721
722
723#endif /* POLARSSL_HMAC_DRBG_C */
724
725
726int dep_check( char *str )
727{
728 if( str == NULL )
729 return( 1 );
730
731 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
732 {
733#if defined(POLARSSL_SHA512_C)
734 return( 0 );
735#else
736 return( 1 );
737#endif
738 }
739 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
740 {
741#if defined(POLARSSL_SHA256_C)
742 return( 0 );
743#else
744 return( 1 );
745#endif
746 }
747 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
748 {
749#if defined(POLARSSL_SHA1_C)
750 return( 0 );
751#else
752 return( 1 );
753#endif
754 }
755
756
757 return( 1 );
758}
759
760int dispatch_test(int cnt, char *params[50])
761{
762 int ret;
763 ((void) cnt);
764 ((void) params);
765
766#if defined(TEST_SUITE_ACTIVE)
767 if( strcmp( params[0], "hmac_drbg_entropy_usage" ) == 0 )
768 {
769
770 int param1;
771
772 if( cnt != 2 )
773 {
774 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
775 return( 2 );
776 }
777
778 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
779
780 test_suite_hmac_drbg_entropy_usage( param1 );
781 return ( 0 );
782
783 return ( 3 );
784 }
785 else
786 if( strcmp( params[0], "hmac_drbg_seed_file" ) == 0 )
787 {
788 #ifdef POLARSSL_FS_IO
789
790 int param1;
791 char *param2 = params[2];
792 int param3;
793
794 if( cnt != 4 )
795 {
796 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
797 return( 2 );
798 }
799
800 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
801 if( verify_string( &param2 ) != 0 ) return( 2 );
802 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
803
804 test_suite_hmac_drbg_seed_file( param1, param2, param3 );
805 return ( 0 );
806 #endif /* POLARSSL_FS_IO */
807
808 return ( 3 );
809 }
810 else
811 if( strcmp( params[0], "hmac_drbg_buf" ) == 0 )
812 {
813
814 int param1;
815
816 if( cnt != 2 )
817 {
818 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
819 return( 2 );
820 }
821
822 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
823
824 test_suite_hmac_drbg_buf( param1 );
825 return ( 0 );
826
827 return ( 3 );
828 }
829 else
830 if( strcmp( params[0], "hmac_drbg_no_reseed" ) == 0 )
831 {
832
833 int param1;
834 char *param2 = params[2];
835 char *param3 = params[3];
836 char *param4 = params[4];
837 char *param5 = params[5];
838 char *param6 = params[6];
839
840 if( cnt != 7 )
841 {
842 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
843 return( 2 );
844 }
845
846 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
847 if( verify_string( &param2 ) != 0 ) return( 2 );
848 if( verify_string( &param3 ) != 0 ) return( 2 );
849 if( verify_string( &param4 ) != 0 ) return( 2 );
850 if( verify_string( &param5 ) != 0 ) return( 2 );
851 if( verify_string( &param6 ) != 0 ) return( 2 );
852
853 test_suite_hmac_drbg_no_reseed( param1, param2, param3, param4, param5, param6 );
854 return ( 0 );
855
856 return ( 3 );
857 }
858 else
859 if( strcmp( params[0], "hmac_drbg_nopr" ) == 0 )
860 {
861
862 int param1;
863 char *param2 = params[2];
864 char *param3 = params[3];
865 char *param4 = params[4];
866 char *param5 = params[5];
867 char *param6 = params[6];
868 char *param7 = params[7];
869
870 if( cnt != 8 )
871 {
872 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
873 return( 2 );
874 }
875
876 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
877 if( verify_string( &param2 ) != 0 ) return( 2 );
878 if( verify_string( &param3 ) != 0 ) return( 2 );
879 if( verify_string( &param4 ) != 0 ) return( 2 );
880 if( verify_string( &param5 ) != 0 ) return( 2 );
881 if( verify_string( &param6 ) != 0 ) return( 2 );
882 if( verify_string( &param7 ) != 0 ) return( 2 );
883
884 test_suite_hmac_drbg_nopr( param1, param2, param3, param4, param5, param6, param7 );
885 return ( 0 );
886
887 return ( 3 );
888 }
889 else
890 if( strcmp( params[0], "hmac_drbg_pr" ) == 0 )
891 {
892
893 int param1;
894 char *param2 = params[2];
895 char *param3 = params[3];
896 char *param4 = params[4];
897 char *param5 = params[5];
898 char *param6 = params[6];
899
900 if( cnt != 7 )
901 {
902 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
903 return( 2 );
904 }
905
906 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
907 if( verify_string( &param2 ) != 0 ) return( 2 );
908 if( verify_string( &param3 ) != 0 ) return( 2 );
909 if( verify_string( &param4 ) != 0 ) return( 2 );
910 if( verify_string( &param5 ) != 0 ) return( 2 );
911 if( verify_string( &param6 ) != 0 ) return( 2 );
912
913 test_suite_hmac_drbg_pr( param1, param2, param3, param4, param5, param6 );
914 return ( 0 );
915
916 return ( 3 );
917 }
918 else
919 if( strcmp( params[0], "hmac_drbg_selftest" ) == 0 )
920 {
921 #ifdef POLARSSL_SELF_TEST
922
923
924 if( cnt != 1 )
925 {
926 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
927 return( 2 );
928 }
929
930
931 test_suite_hmac_drbg_selftest( );
932 return ( 0 );
933 #endif /* POLARSSL_SELF_TEST */
934
935 return ( 3 );
936 }
937 else
938
939 {
940 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
941 fflush( stdout );
942 return( 1 );
943 }
944#else
945 return( 3 );
946#endif
947 return( ret );
948}
949
950int get_line( FILE *f, char *buf, size_t len )
951{
952 char *ret;
953
954 ret = fgets( buf, len, f );
955 if( ret == NULL )
956 return( -1 );
957
958 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
959 buf[strlen(buf) - 1] = '\0';
960 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
961 buf[strlen(buf) - 1] = '\0';
962
963 return( 0 );
964}
965
966int parse_arguments( char *buf, size_t len, char *params[50] )
967{
968 int cnt = 0, i;
969 char *cur = buf;
970 char *p = buf, *q;
971
972 params[cnt++] = cur;
973
974 while( *p != '\0' && p < buf + len )
975 {
976 if( *p == '\\' )
977 {
978 p++;
979 p++;
980 continue;
981 }
982 if( *p == ':' )
983 {
984 if( p + 1 < buf + len )
985 {
986 cur = p + 1;
987 params[cnt++] = cur;
988 }
989 *p = '\0';
990 }
991
992 p++;
993 }
994
995 // Replace newlines, question marks and colons in strings
996 for( i = 0; i < cnt; i++ )
997 {
998 p = params[i];
999 q = params[i];
1000
1001 while( *p != '\0' )
1002 {
1003 if( *p == '\\' && *(p + 1) == 'n' )
1004 {
1005 p += 2;
1006 *(q++) = '\n';
1007 }
1008 else if( *p == '\\' && *(p + 1) == ':' )
1009 {
1010 p += 2;
1011 *(q++) = ':';
1012 }
1013 else if( *p == '\\' && *(p + 1) == '?' )
1014 {
1015 p += 2;
1016 *(q++) = '?';
1017 }
1018 else
1019 *(q++) = *(p++);
1020 }
1021 *q = '\0';
1022 }
1023
1024 return( cnt );
1025}
1026
1027int main()
1028{
1029 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1030 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_drbg.nopr.data";
1031 FILE *file;
1032 char buf[5000];
1033 char *params[50];
1034
1035#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1036 unsigned char alloc_buf[1000000];
1037 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1038#endif
1039
1040 file = fopen( filename, "r" );
1041 if( file == NULL )
1042 {
1043 fprintf( stderr, "Failed to open\n" );
1044 return( 1 );
1045 }
1046
1047 while( !feof( file ) )
1048 {
1049 int skip = 0;
1050
1051 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1052 break;
1053 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1054 fprintf( stdout, " " );
1055 for( i = strlen( buf ) + 1; i < 67; i++ )
1056 fprintf( stdout, "." );
1057 fprintf( stdout, " " );
1058 fflush( stdout );
1059
1060 total_tests++;
1061
1062 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1063 break;
1064 cnt = parse_arguments( buf, strlen(buf), params );
1065
1066 if( strcmp( params[0], "depends_on" ) == 0 )
1067 {
1068 for( i = 1; i < cnt; i++ )
1069 if( dep_check( params[i] ) != 0 )
1070 skip = 1;
1071
1072 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1073 break;
1074 cnt = parse_arguments( buf, strlen(buf), params );
1075 }
1076
1077 if( skip == 0 )
1078 {
1079 test_errors = 0;
1080 ret = dispatch_test( cnt, params );
1081 }
1082
1083 if( skip == 1 || ret == 3 )
1084 {
1085 total_skipped++;
1086 fprintf( stdout, "----\n" );
1087 fflush( stdout );
1088 }
1089 else if( ret == 0 && test_errors == 0 )
1090 {
1091 fprintf( stdout, "PASS\n" );
1092 fflush( stdout );
1093 }
1094 else if( ret == 2 )
1095 {
1096 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1097 fclose(file);
1098 exit( 2 );
1099 }
1100 else
1101 total_errors++;
1102
1103 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1104 break;
1105 if( strlen(buf) != 0 )
1106 {
1107 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1108 return( 1 );
1109 }
1110 }
1111 fclose(file);
1112
1113 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1114 if( total_errors == 0 )
1115 fprintf( stdout, "PASSED" );
1116 else
1117 fprintf( stdout, "FAILED" );
1118
1119 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1120 total_tests - total_errors, total_tests, total_skipped );
1121
1122#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1123#if defined(POLARSSL_MEMORY_DEBUG)
1124 memory_buffer_alloc_status();
1125#endif
1127#endif
1128
1129 return( total_errors != 0 );
1130}
1131
1132
Configuration options (set of defines)
int entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
HMAC_DRBG (NIST SP 800-90A)
void hmac_drbg_set_entropy_len(hmac_drbg_context *ctx, size_t len)
Set the amount of entropy grabbed on each reseed (Default: given by the security strength,...
void hmac_drbg_free(hmac_drbg_context *ctx)
Free an HMAC_DRBG context.
#define POLARSSL_HMAC_DRBG_PR_ON
Prediction resistance enabled
Definition hmac_drbg.h:67
int hmac_drbg_reseed(hmac_drbg_context *ctx, const unsigned char *additional, size_t len)
HMAC_DRBG reseeding (extracts data from entropy source)
int hmac_drbg_init(hmac_drbg_context *ctx, const md_info_t *md_info, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
HMAC_DRBG initialisation.
void hmac_drbg_set_prediction_resistance(hmac_drbg_context *ctx, int resistance)
Enable / disable prediction resistance (Default: Off)
int hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
HMAC_DRBG generate random.
int hmac_drbg_init_buf(hmac_drbg_context *ctx, const md_info_t *md_info, const unsigned char *data, size_t data_len)
Initilisation of simpified HMAC_DRBG (never reseeds).
int hmac_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
HMAC_DRBG generate random with additional update input.
void hmac_drbg_set_reseed_interval(hmac_drbg_context *ctx, int interval)
Set the reseed interval (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
@ POLARSSL_MD_SHA224
Definition md.h:57
@ POLARSSL_MD_SHA1
Definition md.h:56
@ POLARSSL_MD_SHA384
Definition md.h:59
@ POLARSSL_MD_SHA512
Definition md.h:60
@ POLARSSL_MD_SHA256
Definition md.h:58
Memory allocation layer (Deprecated to platform layer)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
PolarSSL Platform abstraction layer.
HMAC_DRBG context.
Definition hmac_drbg.h:77
Message digest information.
Definition md.h:74
unsigned char * buf
Info structure for the pseudo random function.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int dep_check(char *str)
int dispatch_test(int cnt, char *params[50])
#define polarssl_malloc
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int get_line(FILE *f, char *buf, size_t len)
static int test_errors
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 unhexify(unsigned char *obuf, const char *ibuf)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_int(char *str, int *value)
static int test_assert(int correct, const char *test)
int verify_string(char **str)
#define TEST_ASSERT(TEST)