PolarSSL v1.3.9
test_suite_md.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_MD_C
8
9#include <polarssl/md.h>
10#endif /* POLARSSL_MD_C */
11
12
13#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14#include "polarssl/memory.h"
15#endif
16
17#if defined(POLARSSL_PLATFORM_C)
18#include "polarssl/platform.h"
19#else
20#define polarssl_malloc malloc
21#define polarssl_free free
22#endif
23
24#ifdef _MSC_VER
25#include <basetsd.h>
26typedef UINT32 uint32_t;
27#else
28#include <inttypes.h>
29#endif
30
31#include <assert.h>
32#include <stdlib.h>
33#include <string.h>
34
35/*
36 * 32-bit integer manipulation macros (big endian)
37 */
38#ifndef GET_UINT32_BE
39#define GET_UINT32_BE(n,b,i) \
40{ \
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] ); \
45}
46#endif
47
48#ifndef PUT_UINT32_BE
49#define PUT_UINT32_BE(n,b,i) \
50{ \
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) ); \
55}
56#endif
57
58static int unhexify(unsigned char *obuf, const char *ibuf)
59{
60 unsigned char c, c2;
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1)); // must be even number of bytes
63
64 while (*ibuf != 0)
65 {
66 c = *ibuf++;
67 if( c >= '0' && c <= '9' )
68 c -= '0';
69 else if( c >= 'a' && c <= 'f' )
70 c -= 'a' - 10;
71 else if( c >= 'A' && c <= 'F' )
72 c -= 'A' - 10;
73 else
74 assert( 0 );
75
76 c2 = *ibuf++;
77 if( c2 >= '0' && c2 <= '9' )
78 c2 -= '0';
79 else if( c2 >= 'a' && c2 <= 'f' )
80 c2 -= 'a' - 10;
81 else if( c2 >= 'A' && c2 <= 'F' )
82 c2 -= 'A' - 10;
83 else
84 assert( 0 );
85
86 *obuf++ = ( c << 4 ) | c2;
87 }
88
89 return len;
90}
91
92static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93{
94 unsigned char l, h;
95
96 while (len != 0)
97 {
98 h = (*ibuf) / 16;
99 l = (*ibuf) % 16;
100
101 if( h < 10 )
102 *obuf++ = '0' + h;
103 else
104 *obuf++ = 'a' + h - 10;
105
106 if( l < 10 )
107 *obuf++ = '0' + l;
108 else
109 *obuf++ = 'a' + l - 10;
110
111 ++ibuf;
112 len--;
113 }
114}
115
123static unsigned char *zero_alloc( size_t len )
124{
125 void *p;
126 size_t actual_len = len != 0 ? len : 1;
127
128 p = polarssl_malloc( actual_len );
129 assert( p != NULL );
130
131 memset( p, 0x00, actual_len );
132
133 return( p );
134}
135
146static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147{
148 unsigned char *obuf;
149
150 *olen = strlen(ibuf) / 2;
151
152 if( *olen == 0 )
153 return( zero_alloc( *olen ) );
154
155 obuf = polarssl_malloc( *olen );
156 assert( obuf != NULL );
157
158 (void) unhexify( obuf, ibuf );
159
160 return( obuf );
161}
162
172static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173{
174#if !defined(__OpenBSD__)
175 size_t i;
176
177 if( rng_state != NULL )
178 rng_state = NULL;
179
180 for( i = 0; i < len; ++i )
181 output[i] = rand();
182#else
183 if( rng_state != NULL )
184 rng_state = NULL;
185
186 arc4random_buf( output, len );
187#endif /* !OpenBSD */
188
189 return( 0 );
190}
191
197static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198{
199 if( rng_state != NULL )
200 rng_state = NULL;
201
202 memset( output, 0, len );
203
204 return( 0 );
205}
206
207typedef struct
208{
209 unsigned char *buf;
210 size_t length;
212
224static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225{
226 rnd_buf_info *info = (rnd_buf_info *) rng_state;
227 size_t use_len;
228
229 if( rng_state == NULL )
230 return( rnd_std_rand( NULL, output, len ) );
231
232 use_len = len;
233 if( len > info->length )
234 use_len = info->length;
235
236 if( use_len )
237 {
238 memcpy( output, info->buf, use_len );
239 info->buf += use_len;
240 info->length -= use_len;
241 }
242
243 if( len - use_len > 0 )
244 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245
246 return( 0 );
247}
248
256typedef struct
257{
258 uint32_t key[16];
259 uint32_t v0, v1;
261
270static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271{
272 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
275
276 if( rng_state == NULL )
277 return( rnd_std_rand( NULL, output, len ) );
278
279 k = info->key;
280
281 while( len > 0 )
282 {
283 size_t use_len = ( len > 4 ) ? 4 : len;
284 sum = 0;
285
286 for( i = 0; i < 32; i++ )
287 {
288 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289 sum += delta;
290 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291 }
292
293 PUT_UINT32_BE( info->v0, result, 0 );
294 memcpy( out, result, use_len );
295 len -= use_len;
296 out += 4;
297 }
298
299 return( 0 );
300}
301
302
303#include <stdio.h>
304#include <string.h>
305
306#if defined(POLARSSL_PLATFORM_C)
307#include "polarssl/platform.h"
308#else
309#define polarssl_printf printf
310#define polarssl_malloc malloc
311#define polarssl_free free
312#endif
313
314static int test_errors = 0;
315
316#ifdef POLARSSL_MD_C
317
318#define TEST_SUITE_ACTIVE
319
320static int test_assert( int correct, const char *test )
321{
322 if( correct )
323 return( 0 );
324
325 test_errors++;
326 if( test_errors == 1 )
327 printf( "FAILED\n" );
328 printf( " %s\n", test );
329
330 return( 1 );
331}
332
333#define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
336 } while (0)
337
338int verify_string( char **str )
339{
340 if( (*str)[0] != '"' ||
341 (*str)[strlen( *str ) - 1] != '"' )
342 {
343 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
344 return( -1 );
345 }
346
347 (*str)++;
348 (*str)[strlen( *str ) - 1] = '\0';
349
350 return( 0 );
351}
352
353int verify_int( char *str, int *value )
354{
355 size_t i;
356 int minus = 0;
357 int digits = 1;
358 int hex = 0;
359
360 for( i = 0; i < strlen( str ); i++ )
361 {
362 if( i == 0 && str[i] == '-' )
363 {
364 minus = 1;
365 continue;
366 }
367
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] == '0' && str[i] == 'x' )
370 {
371 hex = 1;
372 continue;
373 }
374
375 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
376 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
377 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
378 {
379 digits = 0;
380 break;
381 }
382 }
383
384 if( digits )
385 {
386 if( hex )
387 *value = strtol( str, NULL, 16 );
388 else
389 *value = strtol( str, NULL, 10 );
390
391 return( 0 );
392 }
393
394 if( strcmp( str, "POLARSSL_MD_MD5" ) == 0 )
395 {
396 *value = ( POLARSSL_MD_MD5 );
397 return( 0 );
398 }
399 if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
400 {
401 *value = ( POLARSSL_MD_SHA256 );
402 return( 0 );
403 }
404 if( strcmp( str, "POLARSSL_MD_MD2" ) == 0 )
405 {
406 *value = ( POLARSSL_MD_MD2 );
407 return( 0 );
408 }
409 if( strcmp( str, "POLARSSL_MD_MD4" ) == 0 )
410 {
411 *value = ( POLARSSL_MD_MD4 );
412 return( 0 );
413 }
414 if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
415 {
416 *value = ( POLARSSL_MD_SHA384 );
417 return( 0 );
418 }
419 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
420 {
421 *value = ( POLARSSL_MD_SHA1 );
422 return( 0 );
423 }
424 if( strcmp( str, "POLARSSL_MD_RIPEMD160" ) == 0 )
425 {
426 *value = ( POLARSSL_MD_RIPEMD160 );
427 return( 0 );
428 }
429 if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
430 {
431 *value = ( POLARSSL_MD_SHA512 );
432 return( 0 );
433 }
434 if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
435 {
436 *value = ( POLARSSL_MD_SHA224 );
437 return( 0 );
438 }
439
440
441 printf( "Expected integer for parameter and got: %s\n", str );
442 return( -1 );
443}
444
445void test_suite_md_process( )
446{
447 const int *md_type_ptr;
448 const md_info_t *info;
449 md_context_t ctx;
450 unsigned char buf[150];
451
452 md_init( &ctx );
453
454 /*
455 * Very minimal testing of md_process, just make sure the various
456 * xxx_process_wrap() function pointers are valid. (Testing that they
457 * indeed do the right thing whould require messing with the internal
458 * state of the underlying md/sha context.)
459 *
460 * Also tests that md_list() only returns valid MDs.
461 */
462 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
463 {
464 info = md_info_from_type( *md_type_ptr );
465 TEST_ASSERT( info != NULL );
466 TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
467 TEST_ASSERT( md_process( &ctx, buf ) == 0 );
468 md_free( &ctx );
469 }
470
471exit:
472 md_free( &ctx );
473}
474
475void test_suite_md_null_args( )
476{
477 md_context_t ctx;
478 const md_info_t *info = md_info_from_type( *( md_list() ) );
479 unsigned char buf[1] = { 0 };
480
481 md_init( &ctx );
482
483 TEST_ASSERT( md_get_size( NULL ) == 0 );
484
486
487 TEST_ASSERT( md_info_from_string( NULL ) == NULL );
488
491
494
497
500
501 TEST_ASSERT( md( NULL, buf, 1, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
502
504
505 TEST_ASSERT( md_hmac_starts( NULL, buf, 1 )
507 TEST_ASSERT( md_hmac_starts( &ctx, buf, 1 )
509
510 TEST_ASSERT( md_hmac_update( NULL, buf, 1 )
512 TEST_ASSERT( md_hmac_update( &ctx, buf, 1 )
514
515 TEST_ASSERT( md_hmac_finish( NULL, buf )
517 TEST_ASSERT( md_hmac_finish( &ctx, buf )
519
522
523 TEST_ASSERT( md_hmac( NULL, buf, 1, buf, 1, buf )
525
528
529exit:
530 return;
531}
532
533void test_suite_md_info( int md_type, char *md_name, int md_size )
534{
535 const md_info_t *md_info;
536 const int *md_type_ptr;
537 int found;
538
539 md_info = md_info_from_type( md_type );
540 TEST_ASSERT( md_info != NULL );
541 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
542
543 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
544 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
545
546 found = 0;
547 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
548 if( *md_type_ptr == md_type )
549 found = 1;
550 TEST_ASSERT( found == 1 );
551
552exit:
553 return;
554}
555
556void test_suite_md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
557{
558 char md_name[100];
559 unsigned char src_str[1000];
560 unsigned char hash_str[1000];
561 unsigned char output[100];
562 const md_info_t *md_info = NULL;
563
564 memset(md_name, 0x00, 100);
565 memset(src_str, 0x00, 1000);
566 memset(hash_str, 0x00, 1000);
567 memset(output, 0x00, 100);
568
569 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
570 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
571 md_info = md_info_from_string(md_name);
572 TEST_ASSERT( md_info != NULL );
573
574 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
575 hexify( hash_str, output, md_get_size(md_info) );
576
577 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
578
579exit:
580 return;
581}
582
583void test_suite_md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
584{
585 char md_name[100];
586 unsigned char src_str[10000];
587 unsigned char hash_str[10000];
588 unsigned char output[100];
589 int src_len;
590 const md_info_t *md_info = NULL;
591
592 memset(md_name, 0x00, 100);
593 memset(src_str, 0x00, 10000);
594 memset(hash_str, 0x00, 10000);
595 memset(output, 0x00, 100);
596
597 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
598 md_info = md_info_from_string(md_name);
599 TEST_ASSERT( md_info != NULL );
600
601 src_len = unhexify( src_str, hex_src_string );
602 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
603
604 hexify( hash_str, output, md_get_size(md_info) );
605
606 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
607
608exit:
609 return;
610}
611
612void test_suite_md_text_multi( char *text_md_name, char *text_src_string,
613 char *hex_hash_string )
614{
615 char md_name[100];
616 unsigned char src_str[1000];
617 unsigned char hash_str[1000];
618 unsigned char output[100];
619
620 const md_info_t *md_info = NULL;
621 md_context_t ctx;
622
623 md_init( &ctx );
624
625 memset(md_name, 0x00, 100);
626 memset(src_str, 0x00, 1000);
627 memset(hash_str, 0x00, 1000);
628 memset(output, 0x00, 100);
629
630 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
631 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
632 md_info = md_info_from_string(md_name);
633 TEST_ASSERT( md_info != NULL );
634 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
635
636 TEST_ASSERT ( 0 == md_starts( &ctx ) );
637 TEST_ASSERT ( ctx.md_ctx != NULL );
638 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
639 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
640
641 hexify( hash_str, output, md_get_size(md_info) );
642
643 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
644
645exit:
646 md_free( &ctx );
647}
648
649void test_suite_md_hex_multi( char *text_md_name, char *hex_src_string,
650 char *hex_hash_string )
651{
652 char md_name[100];
653 unsigned char src_str[10000];
654 unsigned char hash_str[10000];
655 unsigned char output[100];
656 int src_len;
657 const md_info_t *md_info = NULL;
658 md_context_t ctx;
659
660 md_init( &ctx );
661
662 memset(md_name, 0x00, 100);
663 memset(src_str, 0x00, 10000);
664 memset(hash_str, 0x00, 10000);
665 memset(output, 0x00, 100);
666
667 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
668 md_info = md_info_from_string(md_name);
669 TEST_ASSERT( md_info != NULL );
670 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
671
672 src_len = unhexify( src_str, hex_src_string );
673
674 TEST_ASSERT ( 0 == md_starts( &ctx ) );
675 TEST_ASSERT ( ctx.md_ctx != NULL );
676 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
677 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
678
679 hexify( hash_str, output, md_get_size(md_info) );
680
681 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
682
683exit:
684 md_free( &ctx );
685}
686
687void test_suite_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
688 char *hex_src_string, char *hex_hash_string )
689{
690 char md_name[100];
691 unsigned char src_str[10000];
692 unsigned char key_str[10000];
693 unsigned char hash_str[10000];
694 unsigned char output[100];
695 int key_len, src_len;
696 const md_info_t *md_info = NULL;
697
698 memset(md_name, 0x00, 100);
699 memset(src_str, 0x00, 10000);
700 memset(key_str, 0x00, 10000);
701 memset(hash_str, 0x00, 10000);
702 memset(output, 0x00, 100);
703
704 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
705 md_info = md_info_from_string( md_name );
706 TEST_ASSERT( md_info != NULL );
707
708 key_len = unhexify( key_str, hex_key_string );
709 src_len = unhexify( src_str, hex_src_string );
710
711 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
712 hexify( hash_str, output, md_get_size(md_info) );
713
714 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
715
716exit:
717 return;
718}
719
720void test_suite_md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
721 char *hex_src_string, char *hex_hash_string )
722{
723 char md_name[100];
724 unsigned char src_str[10000];
725 unsigned char key_str[10000];
726 unsigned char hash_str[10000];
727 unsigned char output[100];
728 int key_len, src_len;
729 const md_info_t *md_info = NULL;
730 md_context_t ctx;
731
732 md_init( &ctx );
733
734 memset(md_name, 0x00, 100);
735 memset(src_str, 0x00, 10000);
736 memset(key_str, 0x00, 10000);
737 memset(hash_str, 0x00, 10000);
738 memset(output, 0x00, 100);
739
740 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
741 md_info = md_info_from_string( md_name );
742 TEST_ASSERT( md_info != NULL );
743 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
744
745 key_len = unhexify( key_str, hex_key_string );
746 src_len = unhexify( src_str, hex_src_string );
747
748 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
749 TEST_ASSERT ( ctx.md_ctx != NULL );
750 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
751 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
752
753 hexify( hash_str, output, md_get_size(md_info) );
754 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
755
756 /* Test again, for reset() */
757 memset(hash_str, 0x00, 10000);
758 memset(output, 0x00, 100);
759
760 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
761 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
762 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
763
764 hexify( hash_str, output, md_get_size(md_info) );
765 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
766
767exit:
768 md_free( &ctx );
769}
770
771#ifdef POLARSSL_FS_IO
772void test_suite_md_file( char *text_md_name, char *filename, char *hex_hash_string )
773{
774 char md_name[100];
775 unsigned char hash_str[1000];
776 unsigned char output[100];
777 const md_info_t *md_info = NULL;
778
779 memset(md_name, 0x00, 100);
780 memset(hash_str, 0x00, 1000);
781 memset(output, 0x00, 100);
782
783 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
784 md_info = md_info_from_string( md_name );
785 TEST_ASSERT( md_info != NULL );
786
787 md_file( md_info, filename, output);
788 hexify( hash_str, output, md_get_size(md_info) );
789
790 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
791
792exit:
793 return;
794}
795#endif /* POLARSSL_FS_IO */
796
797
798#endif /* POLARSSL_MD_C */
799
800
801int dep_check( char *str )
802{
803 if( str == NULL )
804 return( 1 );
805
806 if( strcmp( str, "POLARSSL_RIPEMD160_C" ) == 0 )
807 {
808#if defined(POLARSSL_RIPEMD160_C)
809 return( 0 );
810#else
811 return( 1 );
812#endif
813 }
814 if( strcmp( str, "POLARSSL_MD_C" ) == 0 )
815 {
816#if defined(POLARSSL_MD_C)
817 return( 0 );
818#else
819 return( 1 );
820#endif
821 }
822 if( strcmp( str, "POLARSSL_MD2_C" ) == 0 )
823 {
824#if defined(POLARSSL_MD2_C)
825 return( 0 );
826#else
827 return( 1 );
828#endif
829 }
830 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
831 {
832#if defined(POLARSSL_SHA512_C)
833 return( 0 );
834#else
835 return( 1 );
836#endif
837 }
838 if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
839 {
840#if defined(POLARSSL_MD4_C)
841 return( 0 );
842#else
843 return( 1 );
844#endif
845 }
846 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
847 {
848#if defined(POLARSSL_SHA256_C)
849 return( 0 );
850#else
851 return( 1 );
852#endif
853 }
854 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
855 {
856#if defined(POLARSSL_SHA1_C)
857 return( 0 );
858#else
859 return( 1 );
860#endif
861 }
862 if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
863 {
864#if defined(POLARSSL_MD5_C)
865 return( 0 );
866#else
867 return( 1 );
868#endif
869 }
870
871
872 return( 1 );
873}
874
875int dispatch_test(int cnt, char *params[50])
876{
877 int ret;
878 ((void) cnt);
879 ((void) params);
880
881#if defined(TEST_SUITE_ACTIVE)
882 if( strcmp( params[0], "md_process" ) == 0 )
883 {
884
885
886 if( cnt != 1 )
887 {
888 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
889 return( 2 );
890 }
891
892
893 test_suite_md_process( );
894 return ( 0 );
895
896 return ( 3 );
897 }
898 else
899 if( strcmp( params[0], "md_null_args" ) == 0 )
900 {
901
902
903 if( cnt != 1 )
904 {
905 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
906 return( 2 );
907 }
908
909
910 test_suite_md_null_args( );
911 return ( 0 );
912
913 return ( 3 );
914 }
915 else
916 if( strcmp( params[0], "md_info" ) == 0 )
917 {
918
919 int param1;
920 char *param2 = params[2];
921 int param3;
922
923 if( cnt != 4 )
924 {
925 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
926 return( 2 );
927 }
928
929 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
930 if( verify_string( &param2 ) != 0 ) return( 2 );
931 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
932
933 test_suite_md_info( param1, param2, param3 );
934 return ( 0 );
935
936 return ( 3 );
937 }
938 else
939 if( strcmp( params[0], "md_text" ) == 0 )
940 {
941
942 char *param1 = params[1];
943 char *param2 = params[2];
944 char *param3 = params[3];
945
946 if( cnt != 4 )
947 {
948 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
949 return( 2 );
950 }
951
952 if( verify_string( &param1 ) != 0 ) return( 2 );
953 if( verify_string( &param2 ) != 0 ) return( 2 );
954 if( verify_string( &param3 ) != 0 ) return( 2 );
955
956 test_suite_md_text( param1, param2, param3 );
957 return ( 0 );
958
959 return ( 3 );
960 }
961 else
962 if( strcmp( params[0], "md_hex" ) == 0 )
963 {
964
965 char *param1 = params[1];
966 char *param2 = params[2];
967 char *param3 = params[3];
968
969 if( cnt != 4 )
970 {
971 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
972 return( 2 );
973 }
974
975 if( verify_string( &param1 ) != 0 ) return( 2 );
976 if( verify_string( &param2 ) != 0 ) return( 2 );
977 if( verify_string( &param3 ) != 0 ) return( 2 );
978
979 test_suite_md_hex( param1, param2, param3 );
980 return ( 0 );
981
982 return ( 3 );
983 }
984 else
985 if( strcmp( params[0], "md_text_multi" ) == 0 )
986 {
987
988 char *param1 = params[1];
989 char *param2 = params[2];
990 char *param3 = params[3];
991
992 if( cnt != 4 )
993 {
994 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
995 return( 2 );
996 }
997
998 if( verify_string( &param1 ) != 0 ) return( 2 );
999 if( verify_string( &param2 ) != 0 ) return( 2 );
1000 if( verify_string( &param3 ) != 0 ) return( 2 );
1001
1002 test_suite_md_text_multi( param1, param2, param3 );
1003 return ( 0 );
1004
1005 return ( 3 );
1006 }
1007 else
1008 if( strcmp( params[0], "md_hex_multi" ) == 0 )
1009 {
1010
1011 char *param1 = params[1];
1012 char *param2 = params[2];
1013 char *param3 = params[3];
1014
1015 if( cnt != 4 )
1016 {
1017 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1018 return( 2 );
1019 }
1020
1021 if( verify_string( &param1 ) != 0 ) return( 2 );
1022 if( verify_string( &param2 ) != 0 ) return( 2 );
1023 if( verify_string( &param3 ) != 0 ) return( 2 );
1024
1025 test_suite_md_hex_multi( param1, param2, param3 );
1026 return ( 0 );
1027
1028 return ( 3 );
1029 }
1030 else
1031 if( strcmp( params[0], "md_hmac" ) == 0 )
1032 {
1033
1034 char *param1 = params[1];
1035 int param2;
1036 char *param3 = params[3];
1037 char *param4 = params[4];
1038 char *param5 = params[5];
1039
1040 if( cnt != 6 )
1041 {
1042 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1043 return( 2 );
1044 }
1045
1046 if( verify_string( &param1 ) != 0 ) return( 2 );
1047 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1048 if( verify_string( &param3 ) != 0 ) return( 2 );
1049 if( verify_string( &param4 ) != 0 ) return( 2 );
1050 if( verify_string( &param5 ) != 0 ) return( 2 );
1051
1052 test_suite_md_hmac( param1, param2, param3, param4, param5 );
1053 return ( 0 );
1054
1055 return ( 3 );
1056 }
1057 else
1058 if( strcmp( params[0], "md_hmac_multi" ) == 0 )
1059 {
1060
1061 char *param1 = params[1];
1062 int param2;
1063 char *param3 = params[3];
1064 char *param4 = params[4];
1065 char *param5 = params[5];
1066
1067 if( cnt != 6 )
1068 {
1069 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1070 return( 2 );
1071 }
1072
1073 if( verify_string( &param1 ) != 0 ) return( 2 );
1074 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1075 if( verify_string( &param3 ) != 0 ) return( 2 );
1076 if( verify_string( &param4 ) != 0 ) return( 2 );
1077 if( verify_string( &param5 ) != 0 ) return( 2 );
1078
1079 test_suite_md_hmac_multi( param1, param2, param3, param4, param5 );
1080 return ( 0 );
1081
1082 return ( 3 );
1083 }
1084 else
1085 if( strcmp( params[0], "md_file" ) == 0 )
1086 {
1087 #ifdef POLARSSL_FS_IO
1088
1089 char *param1 = params[1];
1090 char *param2 = params[2];
1091 char *param3 = params[3];
1092
1093 if( cnt != 4 )
1094 {
1095 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1096 return( 2 );
1097 }
1098
1099 if( verify_string( &param1 ) != 0 ) return( 2 );
1100 if( verify_string( &param2 ) != 0 ) return( 2 );
1101 if( verify_string( &param3 ) != 0 ) return( 2 );
1102
1103 test_suite_md_file( param1, param2, param3 );
1104 return ( 0 );
1105 #endif /* POLARSSL_FS_IO */
1106
1107 return ( 3 );
1108 }
1109 else
1110
1111 {
1112 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1113 fflush( stdout );
1114 return( 1 );
1115 }
1116#else
1117 return( 3 );
1118#endif
1119 return( ret );
1120}
1121
1122int get_line( FILE *f, char *buf, size_t len )
1123{
1124 char *ret;
1125
1126 ret = fgets( buf, len, f );
1127 if( ret == NULL )
1128 return( -1 );
1129
1130 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1131 buf[strlen(buf) - 1] = '\0';
1132 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1133 buf[strlen(buf) - 1] = '\0';
1134
1135 return( 0 );
1136}
1137
1138int parse_arguments( char *buf, size_t len, char *params[50] )
1139{
1140 int cnt = 0, i;
1141 char *cur = buf;
1142 char *p = buf, *q;
1143
1144 params[cnt++] = cur;
1145
1146 while( *p != '\0' && p < buf + len )
1147 {
1148 if( *p == '\\' )
1149 {
1150 p++;
1151 p++;
1152 continue;
1153 }
1154 if( *p == ':' )
1155 {
1156 if( p + 1 < buf + len )
1157 {
1158 cur = p + 1;
1159 params[cnt++] = cur;
1160 }
1161 *p = '\0';
1162 }
1163
1164 p++;
1165 }
1166
1167 // Replace newlines, question marks and colons in strings
1168 for( i = 0; i < cnt; i++ )
1169 {
1170 p = params[i];
1171 q = params[i];
1172
1173 while( *p != '\0' )
1174 {
1175 if( *p == '\\' && *(p + 1) == 'n' )
1176 {
1177 p += 2;
1178 *(q++) = '\n';
1179 }
1180 else if( *p == '\\' && *(p + 1) == ':' )
1181 {
1182 p += 2;
1183 *(q++) = ':';
1184 }
1185 else if( *p == '\\' && *(p + 1) == '?' )
1186 {
1187 p += 2;
1188 *(q++) = '?';
1189 }
1190 else
1191 *(q++) = *(p++);
1192 }
1193 *q = '\0';
1194 }
1195
1196 return( cnt );
1197}
1198
1199int main()
1200{
1201 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1202 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_md.data";
1203 FILE *file;
1204 char buf[5000];
1205 char *params[50];
1206
1207#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1208 unsigned char alloc_buf[1000000];
1209 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1210#endif
1211
1212 file = fopen( filename, "r" );
1213 if( file == NULL )
1214 {
1215 fprintf( stderr, "Failed to open\n" );
1216 return( 1 );
1217 }
1218
1219 while( !feof( file ) )
1220 {
1221 int skip = 0;
1222
1223 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1224 break;
1225 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1226 fprintf( stdout, " " );
1227 for( i = strlen( buf ) + 1; i < 67; i++ )
1228 fprintf( stdout, "." );
1229 fprintf( stdout, " " );
1230 fflush( stdout );
1231
1232 total_tests++;
1233
1234 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1235 break;
1236 cnt = parse_arguments( buf, strlen(buf), params );
1237
1238 if( strcmp( params[0], "depends_on" ) == 0 )
1239 {
1240 for( i = 1; i < cnt; i++ )
1241 if( dep_check( params[i] ) != 0 )
1242 skip = 1;
1243
1244 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1245 break;
1246 cnt = parse_arguments( buf, strlen(buf), params );
1247 }
1248
1249 if( skip == 0 )
1250 {
1251 test_errors = 0;
1252 ret = dispatch_test( cnt, params );
1253 }
1254
1255 if( skip == 1 || ret == 3 )
1256 {
1257 total_skipped++;
1258 fprintf( stdout, "----\n" );
1259 fflush( stdout );
1260 }
1261 else if( ret == 0 && test_errors == 0 )
1262 {
1263 fprintf( stdout, "PASS\n" );
1264 fflush( stdout );
1265 }
1266 else if( ret == 2 )
1267 {
1268 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1269 fclose(file);
1270 exit( 2 );
1271 }
1272 else
1273 total_errors++;
1274
1275 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1276 break;
1277 if( strlen(buf) != 0 )
1278 {
1279 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1280 return( 1 );
1281 }
1282 }
1283 fclose(file);
1284
1285 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1286 if( total_errors == 0 )
1287 fprintf( stdout, "PASSED" );
1288 else
1289 fprintf( stdout, "FAILED" );
1290
1291 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1292 total_tests - total_errors, total_tests, total_skipped );
1293
1294#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1295#if defined(POLARSSL_MEMORY_DEBUG)
1296 memory_buffer_alloc_status();
1297#endif
1299#endif
1300
1301 return( total_errors != 0 );
1302}
1303
1304
Configuration options (set of defines)
Generic message digest wrapper.
const md_info_t * md_info_from_string(const char *md_name)
Returns the message digest information associated with the given digest name.
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
Definition: md.h:225
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
const int * md_list(void)
Returns the list of digests supported by the generic digest module.
static md_type_t md_get_type(const md_info_t *md_info)
Returns the type of the message digest output.
Definition: md.h:240
int md_file(const md_info_t *md_info, const char *path, unsigned char *output)
Output = message_digest( file contents )
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
#define POLARSSL_ERR_MD_BAD_INPUT_DATA
Bad input parameters to function.
Definition: md.h:43
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
int md_process(md_context_t *ctx, const unsigned char *data)
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int md_hmac(const md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
Output = Generic_HMAC( hmac key, input buffer )
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
md_type_t
Definition: md.h:51
@ POLARSSL_MD_MD5
Definition: md.h:55
@ POLARSSL_MD_NONE
Definition: md.h:52
@ POLARSSL_MD_SHA224
Definition: md.h:57
@ POLARSSL_MD_SHA1
Definition: md.h:56
@ POLARSSL_MD_SHA384
Definition: md.h:59
@ POLARSSL_MD_MD2
Definition: md.h:53
@ POLARSSL_MD_MD4
Definition: md.h:54
@ POLARSSL_MD_RIPEMD160
Definition: md.h:61
@ 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.
Generic message digest context.
Definition: md.h:132
void * md_ctx
Digest-specific context.
Definition: md.h:137
Message digest information.
Definition: md.h:74
unsigned char * buf
Info structure for the pseudo random function.
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)
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)
Definition: test_suite_md.c:92
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
Definition: test_suite_md.c:49
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)
Definition: test_suite_md.c:58
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().
int main()
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.