PolarSSL v1.3.9
test_suite_hmac_drbg.misc.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_SHA512" ) == 0 )
416 {
417 *value = ( POLARSSL_MD_SHA512 );
418 return( 0 );
419 }
420 if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
421 {
422 *value = ( POLARSSL_MD_SHA384 );
423 return( 0 );
424 }
425#ifdef POLARSSL_FS_IO
426 if( strcmp( str, "POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR" ) == 0 )
427 {
429 return( 0 );
430 }
431#endif // POLARSSL_FS_IO
432 if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
433 {
434 *value = ( POLARSSL_MD_SHA224 );
435 return( 0 );
436 }
437 if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
438 {
439 *value = ( POLARSSL_MD_SHA256 );
440 return( 0 );
441 }
442 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
443 {
444 *value = ( POLARSSL_MD_SHA1 );
445 return( 0 );
446 }
447
448
449 printf( "Expected integer for parameter and got: %s\n", str );
450 return( -1 );
451}
452
453void test_suite_hmac_drbg_entropy_usage( int md_alg )
454{
455 unsigned char out[16];
456 unsigned char buf[1024];
457 const md_info_t *md_info;
459 entropy_ctx entropy;
460 size_t last_len, i, reps = 10;
461
462 memset( buf, 0, sizeof( buf ) );
463 memset( out, 0, sizeof( out ) );
464
465 entropy.len = sizeof( buf );
466 entropy.p = buf;
467
468 md_info = md_info_from_type( md_alg );
469 TEST_ASSERT( md_info != NULL );
470
471 /* Init must use entropy */
472 last_len = entropy.len;
473 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &entropy,
474 NULL, 0 ) == 0 );
475 TEST_ASSERT( entropy.len < last_len );
476
477 /* By default, PR is off and reseed_interval is large,
478 * so the next few calls should not use entropy */
479 last_len = entropy.len;
480 for( i = 0; i < reps; i++ )
481 {
482 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
483 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
484 buf, 16 ) == 0 );
485 }
486 TEST_ASSERT( entropy.len == last_len );
487
488 /* While at it, make sure we didn't write past the requested length */
489 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
490 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
491 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
492 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
493
494 /* Set reseed_interval to the number of calls done,
495 * so the next call should reseed */
496 hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
497 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
498 TEST_ASSERT( entropy.len < last_len );
499
500 /* The new few calls should not reseed */
501 last_len = entropy.len;
502 for( i = 0; i < reps / 2; i++ )
503 {
504 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
505 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
506 buf, 16 ) == 0 );
507 }
508 TEST_ASSERT( entropy.len == last_len );
509
510 /* Now enable PR, so the next few calls should all reseed */
512 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
513 TEST_ASSERT( entropy.len < last_len );
514
515 /* Finally, check setting entropy_len */
516 hmac_drbg_set_entropy_len( &ctx, 42 );
517 last_len = entropy.len;
518 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
519 TEST_ASSERT( (int) last_len - entropy.len == 42 );
520
521 hmac_drbg_set_entropy_len( &ctx, 13 );
522 last_len = entropy.len;
523 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
524 TEST_ASSERT( (int) last_len - entropy.len == 13 );
525
526exit:
527 hmac_drbg_free( &ctx );
528}
529
530#ifdef POLARSSL_FS_IO
531void test_suite_hmac_drbg_seed_file( int md_alg, char *path, int ret )
532{
533 const md_info_t *md_info;
535
536 md_info = md_info_from_type( md_alg );
537 TEST_ASSERT( md_info != NULL );
538
539 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
540 NULL, 0 ) == 0 );
541
542 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
543 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
544
545exit:
546 hmac_drbg_free( &ctx );
547}
548#endif /* POLARSSL_FS_IO */
549
550void test_suite_hmac_drbg_buf( int md_alg )
551{
552 unsigned char out[16];
553 unsigned char buf[100];
554 const md_info_t *md_info;
556 size_t i;
557
558 memset( buf, 0, sizeof( buf ) );
559 memset( out, 0, sizeof( out ) );
560
561 md_info = md_info_from_type( md_alg );
562 TEST_ASSERT( md_info != NULL );
563 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
564
565 /* Make sure it never tries to reseed (would segfault otherwise) */
568
569 for( i = 0; i < 30; i++ )
570 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
571
572exit:
573 hmac_drbg_free( &ctx );
574}
575
576void test_suite_hmac_drbg_no_reseed( int md_alg,
577 char *entropy_hex, char *custom_hex,
578 char *add1_hex, char *add2_hex,
579 char *output_hex )
580{
581 unsigned char data[1024];
582 unsigned char entropy[512];
583 unsigned char custom[512];
584 unsigned char add1[512];
585 unsigned char add2[512];
586 unsigned char output[512];
587 unsigned char my_output[512];
588 size_t custom_len, add1_len, add2_len, out_len;
589 entropy_ctx p_entropy;
590 const md_info_t *md_info;
592
593 memset( my_output, 0, sizeof my_output );
594
595 custom_len = unhexify( custom, custom_hex );
596 add1_len = unhexify( add1, add1_hex );
597 add2_len = unhexify( add2, add2_hex );
598 out_len = unhexify( output, output_hex );
599 p_entropy.len = unhexify( entropy, entropy_hex );
600 p_entropy.p = entropy;
601
602 md_info = md_info_from_type( md_alg );
603 TEST_ASSERT( md_info != NULL );
604
605 /* Test the simplified buffer-based variant */
606 memcpy( data, entropy, p_entropy.len );
607 memcpy( data + p_entropy.len, custom, custom_len );
608 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info,
609 data, p_entropy.len + custom_len ) == 0 );
610 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
611 add1, add1_len ) == 0 );
612 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
613 add2, add2_len ) == 0 );
614
615 /* clear for second run */
616 hmac_drbg_free( &ctx );
617
618 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
619
620 /* And now the normal entropy-based variant */
621 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
622 custom, custom_len ) == 0 );
623 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
624 add1, add1_len ) == 0 );
625 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
626 add2, add2_len ) == 0 );
627 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
628
629exit:
630 hmac_drbg_free( &ctx );
631}
632
633void test_suite_hmac_drbg_nopr( int md_alg,
634 char *entropy_hex, char *custom_hex,
635 char *add1_hex, char *add2_hex, char *add3_hex,
636 char *output_hex )
637{
638 unsigned char entropy[512];
639 unsigned char custom[512];
640 unsigned char add1[512];
641 unsigned char add2[512];
642 unsigned char add3[512];
643 unsigned char output[512];
644 unsigned char my_output[512];
645 size_t custom_len, add1_len, add2_len, add3_len, out_len;
646 entropy_ctx p_entropy;
647 const md_info_t *md_info;
649
650 memset( my_output, 0, sizeof my_output );
651
652 custom_len = unhexify( custom, custom_hex );
653 add1_len = unhexify( add1, add1_hex );
654 add2_len = unhexify( add2, add2_hex );
655 add3_len = unhexify( add3, add3_hex );
656 out_len = unhexify( output, output_hex );
657 p_entropy.len = unhexify( entropy, entropy_hex );
658 p_entropy.p = entropy;
659
660 md_info = md_info_from_type( md_alg );
661 TEST_ASSERT( md_info != NULL );
662
663 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
664 custom, custom_len ) == 0 );
665 TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
666 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
667 add2, add2_len ) == 0 );
668 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
669 add3, add3_len ) == 0 );
670
671 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
672
673exit:
674 hmac_drbg_free( &ctx );
675}
676
677void test_suite_hmac_drbg_pr( int md_alg,
678 char *entropy_hex, char *custom_hex,
679 char *add1_hex, char *add2_hex,
680 char *output_hex )
681{
682 unsigned char entropy[512];
683 unsigned char custom[512];
684 unsigned char add1[512];
685 unsigned char add2[512];
686 unsigned char output[512];
687 unsigned char my_output[512];
688 size_t custom_len, add1_len, add2_len, out_len;
689 entropy_ctx p_entropy;
690 const md_info_t *md_info;
692
693 memset( my_output, 0, sizeof my_output );
694
695 custom_len = unhexify( custom, custom_hex );
696 add1_len = unhexify( add1, add1_hex );
697 add2_len = unhexify( add2, add2_hex );
698 out_len = unhexify( output, output_hex );
699 p_entropy.len = unhexify( entropy, entropy_hex );
700 p_entropy.p = entropy;
701
702 md_info = md_info_from_type( md_alg );
703 TEST_ASSERT( md_info != NULL );
704
705 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
706 custom, custom_len ) == 0 );
708 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
709 add1, add1_len ) == 0 );
710 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
711 add2, add2_len ) == 0 );
712
713 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
714
715exit:
716 hmac_drbg_free( &ctx );
717}
718
719#ifdef POLARSSL_SELF_TEST
720void test_suite_hmac_drbg_selftest( )
721{
722 TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 );
723
724exit:
725 return;
726}
727#endif /* POLARSSL_SELF_TEST */
728
729
730#endif /* POLARSSL_HMAC_DRBG_C */
731
732
733int dep_check( char *str )
734{
735 if( str == NULL )
736 return( 1 );
737
738 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
739 {
740#if defined(POLARSSL_SHA512_C)
741 return( 0 );
742#else
743 return( 1 );
744#endif
745 }
746 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
747 {
748#if defined(POLARSSL_SHA1_C)
749 return( 0 );
750#else
751 return( 1 );
752#endif
753 }
754 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
755 {
756#if defined(POLARSSL_SHA256_C)
757 return( 0 );
758#else
759 return( 1 );
760#endif
761 }
762
763
764 return( 1 );
765}
766
767int dispatch_test(int cnt, char *params[50])
768{
769 int ret;
770 ((void) cnt);
771 ((void) params);
772
773#if defined(TEST_SUITE_ACTIVE)
774 if( strcmp( params[0], "hmac_drbg_entropy_usage" ) == 0 )
775 {
776
777 int param1;
778
779 if( cnt != 2 )
780 {
781 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
782 return( 2 );
783 }
784
785 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
786
787 test_suite_hmac_drbg_entropy_usage( param1 );
788 return ( 0 );
789
790 return ( 3 );
791 }
792 else
793 if( strcmp( params[0], "hmac_drbg_seed_file" ) == 0 )
794 {
795 #ifdef POLARSSL_FS_IO
796
797 int param1;
798 char *param2 = params[2];
799 int param3;
800
801 if( cnt != 4 )
802 {
803 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
804 return( 2 );
805 }
806
807 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
808 if( verify_string( &param2 ) != 0 ) return( 2 );
809 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
810
811 test_suite_hmac_drbg_seed_file( param1, param2, param3 );
812 return ( 0 );
813 #endif /* POLARSSL_FS_IO */
814
815 return ( 3 );
816 }
817 else
818 if( strcmp( params[0], "hmac_drbg_buf" ) == 0 )
819 {
820
821 int param1;
822
823 if( cnt != 2 )
824 {
825 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
826 return( 2 );
827 }
828
829 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
830
831 test_suite_hmac_drbg_buf( param1 );
832 return ( 0 );
833
834 return ( 3 );
835 }
836 else
837 if( strcmp( params[0], "hmac_drbg_no_reseed" ) == 0 )
838 {
839
840 int param1;
841 char *param2 = params[2];
842 char *param3 = params[3];
843 char *param4 = params[4];
844 char *param5 = params[5];
845 char *param6 = params[6];
846
847 if( cnt != 7 )
848 {
849 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
850 return( 2 );
851 }
852
853 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
854 if( verify_string( &param2 ) != 0 ) return( 2 );
855 if( verify_string( &param3 ) != 0 ) return( 2 );
856 if( verify_string( &param4 ) != 0 ) return( 2 );
857 if( verify_string( &param5 ) != 0 ) return( 2 );
858 if( verify_string( &param6 ) != 0 ) return( 2 );
859
860 test_suite_hmac_drbg_no_reseed( param1, param2, param3, param4, param5, param6 );
861 return ( 0 );
862
863 return ( 3 );
864 }
865 else
866 if( strcmp( params[0], "hmac_drbg_nopr" ) == 0 )
867 {
868
869 int param1;
870 char *param2 = params[2];
871 char *param3 = params[3];
872 char *param4 = params[4];
873 char *param5 = params[5];
874 char *param6 = params[6];
875 char *param7 = params[7];
876
877 if( cnt != 8 )
878 {
879 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
880 return( 2 );
881 }
882
883 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
884 if( verify_string( &param2 ) != 0 ) return( 2 );
885 if( verify_string( &param3 ) != 0 ) return( 2 );
886 if( verify_string( &param4 ) != 0 ) return( 2 );
887 if( verify_string( &param5 ) != 0 ) return( 2 );
888 if( verify_string( &param6 ) != 0 ) return( 2 );
889 if( verify_string( &param7 ) != 0 ) return( 2 );
890
891 test_suite_hmac_drbg_nopr( param1, param2, param3, param4, param5, param6, param7 );
892 return ( 0 );
893
894 return ( 3 );
895 }
896 else
897 if( strcmp( params[0], "hmac_drbg_pr" ) == 0 )
898 {
899
900 int param1;
901 char *param2 = params[2];
902 char *param3 = params[3];
903 char *param4 = params[4];
904 char *param5 = params[5];
905 char *param6 = params[6];
906
907 if( cnt != 7 )
908 {
909 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
910 return( 2 );
911 }
912
913 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
914 if( verify_string( &param2 ) != 0 ) return( 2 );
915 if( verify_string( &param3 ) != 0 ) return( 2 );
916 if( verify_string( &param4 ) != 0 ) return( 2 );
917 if( verify_string( &param5 ) != 0 ) return( 2 );
918 if( verify_string( &param6 ) != 0 ) return( 2 );
919
920 test_suite_hmac_drbg_pr( param1, param2, param3, param4, param5, param6 );
921 return ( 0 );
922
923 return ( 3 );
924 }
925 else
926 if( strcmp( params[0], "hmac_drbg_selftest" ) == 0 )
927 {
928 #ifdef POLARSSL_SELF_TEST
929
930
931 if( cnt != 1 )
932 {
933 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
934 return( 2 );
935 }
936
937
938 test_suite_hmac_drbg_selftest( );
939 return ( 0 );
940 #endif /* POLARSSL_SELF_TEST */
941
942 return ( 3 );
943 }
944 else
945
946 {
947 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
948 fflush( stdout );
949 return( 1 );
950 }
951#else
952 return( 3 );
953#endif
954 return( ret );
955}
956
957int get_line( FILE *f, char *buf, size_t len )
958{
959 char *ret;
960
961 ret = fgets( buf, len, f );
962 if( ret == NULL )
963 return( -1 );
964
965 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
966 buf[strlen(buf) - 1] = '\0';
967 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
968 buf[strlen(buf) - 1] = '\0';
969
970 return( 0 );
971}
972
973int parse_arguments( char *buf, size_t len, char *params[50] )
974{
975 int cnt = 0, i;
976 char *cur = buf;
977 char *p = buf, *q;
978
979 params[cnt++] = cur;
980
981 while( *p != '\0' && p < buf + len )
982 {
983 if( *p == '\\' )
984 {
985 p++;
986 p++;
987 continue;
988 }
989 if( *p == ':' )
990 {
991 if( p + 1 < buf + len )
992 {
993 cur = p + 1;
994 params[cnt++] = cur;
995 }
996 *p = '\0';
997 }
998
999 p++;
1000 }
1001
1002 // Replace newlines, question marks and colons in strings
1003 for( i = 0; i < cnt; i++ )
1004 {
1005 p = params[i];
1006 q = params[i];
1007
1008 while( *p != '\0' )
1009 {
1010 if( *p == '\\' && *(p + 1) == 'n' )
1011 {
1012 p += 2;
1013 *(q++) = '\n';
1014 }
1015 else if( *p == '\\' && *(p + 1) == ':' )
1016 {
1017 p += 2;
1018 *(q++) = ':';
1019 }
1020 else if( *p == '\\' && *(p + 1) == '?' )
1021 {
1022 p += 2;
1023 *(q++) = '?';
1024 }
1025 else
1026 *(q++) = *(p++);
1027 }
1028 *q = '\0';
1029 }
1030
1031 return( cnt );
1032}
1033
1034int main()
1035{
1036 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1037 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_drbg.misc.data";
1038 FILE *file;
1039 char buf[5000];
1040 char *params[50];
1041
1042#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1043 unsigned char alloc_buf[1000000];
1044 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1045#endif
1046
1047 file = fopen( filename, "r" );
1048 if( file == NULL )
1049 {
1050 fprintf( stderr, "Failed to open\n" );
1051 return( 1 );
1052 }
1053
1054 while( !feof( file ) )
1055 {
1056 int skip = 0;
1057
1058 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1059 break;
1060 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1061 fprintf( stdout, " " );
1062 for( i = strlen( buf ) + 1; i < 67; i++ )
1063 fprintf( stdout, "." );
1064 fprintf( stdout, " " );
1065 fflush( stdout );
1066
1067 total_tests++;
1068
1069 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1070 break;
1071 cnt = parse_arguments( buf, strlen(buf), params );
1072
1073 if( strcmp( params[0], "depends_on" ) == 0 )
1074 {
1075 for( i = 1; i < cnt; i++ )
1076 if( dep_check( params[i] ) != 0 )
1077 skip = 1;
1078
1079 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1080 break;
1081 cnt = parse_arguments( buf, strlen(buf), params );
1082 }
1083
1084 if( skip == 0 )
1085 {
1086 test_errors = 0;
1087 ret = dispatch_test( cnt, params );
1088 }
1089
1090 if( skip == 1 || ret == 3 )
1091 {
1092 total_skipped++;
1093 fprintf( stdout, "----\n" );
1094 fflush( stdout );
1095 }
1096 else if( ret == 0 && test_errors == 0 )
1097 {
1098 fprintf( stdout, "PASS\n" );
1099 fflush( stdout );
1100 }
1101 else if( ret == 2 )
1102 {
1103 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1104 fclose(file);
1105 exit( 2 );
1106 }
1107 else
1108 total_errors++;
1109
1110 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1111 break;
1112 if( strlen(buf) != 0 )
1113 {
1114 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1115 return( 1 );
1116 }
1117 }
1118 fclose(file);
1119
1120 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1121 if( total_errors == 0 )
1122 fprintf( stdout, "PASSED" );
1123 else
1124 fprintf( stdout, "FAILED" );
1125
1126 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1127 total_tests - total_errors, total_tests, total_skipped );
1128
1129#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1130#if defined(POLARSSL_MEMORY_DEBUG)
1131 memory_buffer_alloc_status();
1132#endif
1134#endif
1135
1136 return( total_errors != 0 );
1137}
1138
1139
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
#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR
Read/write error in file.
Definition: hmac_drbg.h:37
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)