PolarSSL v1.3.9
test_suite_hmac_shax.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 
8 #include <polarssl/sha1.h>
9 #include <polarssl/sha256.h>
10 #include <polarssl/sha512.h>
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>
26 typedef 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 
58 static 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 
92 static 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 
123 static 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 
146 static 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 
172 static 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 
197 static 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 
207 typedef struct
208 {
209  unsigned char *buf;
210  size_t length;
211 } rnd_buf_info;
212 
224 static 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 
256 typedef struct
257 {
258  uint32_t key[16];
259  uint32_t v0, v1;
261 
270 static 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 
314 static int test_errors = 0;
315 
316 
317 #define TEST_SUITE_ACTIVE
318 
319 static int test_assert( int correct, const char *test )
320 {
321  if( correct )
322  return( 0 );
323 
324  test_errors++;
325  if( test_errors == 1 )
326  printf( "FAILED\n" );
327  printf( " %s\n", test );
328 
329  return( 1 );
330 }
331 
332 #define TEST_ASSERT( TEST ) \
333  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
334  if( test_errors) goto exit; \
335  } while (0)
336 
337 int verify_string( char **str )
338 {
339  if( (*str)[0] != '"' ||
340  (*str)[strlen( *str ) - 1] != '"' )
341  {
342  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
343  return( -1 );
344  }
345 
346  (*str)++;
347  (*str)[strlen( *str ) - 1] = '\0';
348 
349  return( 0 );
350 }
351 
352 int verify_int( char *str, int *value )
353 {
354  size_t i;
355  int minus = 0;
356  int digits = 1;
357  int hex = 0;
358 
359  for( i = 0; i < strlen( str ); i++ )
360  {
361  if( i == 0 && str[i] == '-' )
362  {
363  minus = 1;
364  continue;
365  }
366 
367  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
368  str[i - 1] == '0' && str[i] == 'x' )
369  {
370  hex = 1;
371  continue;
372  }
373 
374  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
375  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
376  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
377  {
378  digits = 0;
379  break;
380  }
381  }
382 
383  if( digits )
384  {
385  if( hex )
386  *value = strtol( str, NULL, 16 );
387  else
388  *value = strtol( str, NULL, 10 );
389 
390  return( 0 );
391  }
392 
393 
394 
395  printf( "Expected integer for parameter and got: %s\n", str );
396  return( -1 );
397 }
398 
399 #ifdef POLARSSL_SHA1_C
400 void test_suite_sha1_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
401  char *hex_hash_string)
402 {
403  unsigned char src_str[10000];
404  unsigned char key_str[10000];
405  unsigned char hash_str[41];
406  unsigned char output[20];
407  int key_len, src_len;
408  sha1_context ctx;
409 
410  memset(src_str, 0x00, sizeof src_str);
411  memset(key_str, 0x00, sizeof key_str);
412  sha1_init( &ctx );
413 
414  key_len = unhexify( key_str, hex_key_string );
415  src_len = unhexify( src_str, hex_src_string );
416 
417  /* Test the all-in-one interface */
418  memset(hash_str, 0x00, sizeof hash_str);
419  memset(output, 0x00, sizeof output);
420 
421  sha1_hmac( key_str, key_len, src_str, src_len, output );
422 
423  hexify( hash_str, output, sizeof output );
424  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
425 
426  /* Also test the "streaming" interface */
427  memset( hash_str, 0x00, sizeof hash_str );
428  memset( output, 0x00, sizeof output );
429  memset( &ctx, 0x00, sizeof ctx );
430 
431  sha1_hmac_starts( &ctx, key_str, key_len );
432  sha1_hmac_update( &ctx, src_str, 0 );
433  sha1_hmac_update( &ctx, src_str, src_len / 2 );
434  sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
435  sha1_hmac_update( &ctx, src_str + src_len, 0 );
436  sha1_hmac_finish( &ctx, output );
437 
438  hexify( hash_str, output, sizeof output );
439  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
440 
441  /* Again, to test hmac_reset() */
442  memset( hash_str, 0x00, sizeof hash_str );
443  memset( output, 0x00, sizeof output );
444 
445  sha1_hmac_reset( &ctx );
446  sha1_hmac_update( &ctx, src_str, src_len / 2 );
447  sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
448  sha1_hmac_finish( &ctx, output );
449 
450  hexify( hash_str, output, sizeof output );
451  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
452 
453 exit:
454  sha1_free( &ctx );
455 }
456 #endif /* POLARSSL_SHA1_C */
457 
458 #ifdef POLARSSL_SHA256_C
459 void test_suite_sha224_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
460  char *hex_hash_string)
461 {
462  unsigned char src_str[10000];
463  unsigned char key_str[10000];
464  unsigned char hash_str[57];
465  unsigned char output[28];
466  int key_len, src_len;
467  sha256_context ctx;
468 
469  memset(src_str, 0x00, sizeof src_str);
470  memset(key_str, 0x00, sizeof key_str);
471  sha256_init( &ctx );
472 
473  key_len = unhexify( key_str, hex_key_string );
474  src_len = unhexify( src_str, hex_src_string );
475 
476  /* Test the all-in-one interface */
477  memset(hash_str, 0x00, sizeof hash_str);
478  memset(output, 0x00, sizeof output);
479 
480  sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
481 
482  hexify( hash_str, output, sizeof output );
483  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
484 
485  /* Also test the "streaming" interface */
486  memset( hash_str, 0x00, sizeof hash_str );
487  memset( output, 0x00, sizeof output );
488  memset( &ctx, 0x00, sizeof ctx );
489 
490  sha256_hmac_starts( &ctx, key_str, key_len, 1 );
491  sha256_hmac_update( &ctx, src_str, 0 );
492  sha256_hmac_update( &ctx, src_str, src_len / 2 );
493  sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
494  sha256_hmac_update( &ctx, src_str + src_len, 0 );
495  sha256_hmac_finish( &ctx, output );
496 
497  hexify( hash_str, output, sizeof output );
498  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
499 
500  /* Again, to test hmac_reset() */
501  memset( hash_str, 0x00, sizeof hash_str );
502  memset( output, 0x00, sizeof output );
503 
504  sha256_hmac_reset( &ctx );
505  sha256_hmac_update( &ctx, src_str, src_len / 2 );
506  sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
507  sha256_hmac_finish( &ctx, output );
508 
509  hexify( hash_str, output, sizeof output );
510  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
511 
512 exit:
513  sha256_free( &ctx );
514 }
515 #endif /* POLARSSL_SHA256_C */
516 
517 #ifdef POLARSSL_SHA256_C
518 void test_suite_sha256_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
519  char *hex_hash_string)
520 {
521  unsigned char src_str[10000];
522  unsigned char key_str[10000];
523  unsigned char hash_str[65];
524  unsigned char output[32];
525  int key_len, src_len;
526  sha256_context ctx;
527 
528  memset(src_str, 0x00, sizeof src_str);
529  memset(key_str, 0x00, sizeof key_str);
530  sha256_init( &ctx );
531 
532  key_len = unhexify( key_str, hex_key_string );
533  src_len = unhexify( src_str, hex_src_string );
534 
535  /* Test the all-in-one interface */
536  memset(hash_str, 0x00, sizeof hash_str);
537  memset(output, 0x00, sizeof output);
538 
539  sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
540 
541  hexify( hash_str, output, sizeof output );
542  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
543 
544  /* Also test the "streaming" interface */
545  memset( hash_str, 0x00, sizeof hash_str );
546  memset( output, 0x00, sizeof output );
547  memset( &ctx, 0x00, sizeof ctx );
548 
549  sha256_hmac_starts( &ctx, key_str, key_len, 0 );
550  sha256_hmac_update( &ctx, src_str, 0 );
551  sha256_hmac_update( &ctx, src_str, src_len / 2 );
552  sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
553  sha256_hmac_update( &ctx, src_str + src_len, 0 );
554  sha256_hmac_finish( &ctx, output );
555 
556  hexify( hash_str, output, sizeof output );
557  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
558 
559  /* Again, to test hmac_reset() */
560  memset( hash_str, 0x00, sizeof hash_str );
561  memset( output, 0x00, sizeof output );
562 
563  sha256_hmac_reset( &ctx );
564  sha256_hmac_update( &ctx, src_str, src_len / 2 );
565  sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
566  sha256_hmac_finish( &ctx, output );
567 
568  hexify( hash_str, output, sizeof output );
569  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
570 
571 exit:
572  sha256_free( &ctx );
573 }
574 #endif /* POLARSSL_SHA256_C */
575 
576 #ifdef POLARSSL_SHA512_C
577 void test_suite_sha384_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
578  char *hex_hash_string)
579 {
580  unsigned char src_str[10000];
581  unsigned char key_str[10000];
582  unsigned char hash_str[97];
583  unsigned char output[48];
584  int key_len, src_len;
585  sha512_context ctx;
586 
587  memset(src_str, 0x00, sizeof src_str);
588  memset(key_str, 0x00, sizeof key_str);
589  sha512_init( &ctx );
590 
591  key_len = unhexify( key_str, hex_key_string );
592  src_len = unhexify( src_str, hex_src_string );
593 
594  /* Test the all-in-one interface */
595  memset(hash_str, 0x00, sizeof hash_str);
596  memset(output, 0x00, sizeof output);
597 
598  sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
599 
600  hexify( hash_str, output, sizeof output );
601  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
602 
603  /* Also test the "streaming" interface */
604  memset( hash_str, 0x00, sizeof hash_str );
605  memset( output, 0x00, sizeof output );
606  memset( &ctx, 0x00, sizeof ctx );
607 
608  sha512_hmac_starts( &ctx, key_str, key_len, 1 );
609  sha512_hmac_update( &ctx, src_str, 0 );
610  sha512_hmac_update( &ctx, src_str, src_len / 2 );
611  sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
612  sha512_hmac_update( &ctx, src_str + src_len, 0 );
613  sha512_hmac_finish( &ctx, output );
614 
615  hexify( hash_str, output, sizeof output );
616  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
617 
618  /* Again, to test hmac_reset() */
619  memset( hash_str, 0x00, sizeof hash_str );
620  memset( output, 0x00, sizeof output );
621 
622  sha512_hmac_reset( &ctx );
623  sha512_hmac_update( &ctx, src_str, src_len / 2 );
624  sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
625  sha512_hmac_finish( &ctx, output );
626 
627  hexify( hash_str, output, sizeof output );
628  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
629 
630 exit:
631  sha512_free( &ctx );
632 }
633 #endif /* POLARSSL_SHA512_C */
634 
635 #ifdef POLARSSL_SHA512_C
636 void test_suite_sha512_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
637  char *hex_hash_string)
638 {
639  unsigned char src_str[10000];
640  unsigned char key_str[10000];
641  unsigned char hash_str[129];
642  unsigned char output[64];
643  int key_len, src_len;
644  sha512_context ctx;
645 
646  memset(src_str, 0x00, sizeof src_str);
647  memset(key_str, 0x00, sizeof key_str);
648  sha512_init( &ctx );
649 
650  key_len = unhexify( key_str, hex_key_string );
651  src_len = unhexify( src_str, hex_src_string );
652 
653  /* Test the all-in-one interface */
654  memset(hash_str, 0x00, sizeof hash_str);
655  memset(output, 0x00, sizeof output);
656 
657  sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
658 
659  hexify( hash_str, output, sizeof output );
660  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
661 
662  /* Also test the "streaming" interface */
663  memset( hash_str, 0x00, sizeof hash_str );
664  memset( output, 0x00, sizeof output );
665  memset( &ctx, 0x00, sizeof ctx );
666 
667  sha512_hmac_starts( &ctx, key_str, key_len, 0 );
668  sha512_hmac_update( &ctx, src_str, 0 );
669  sha512_hmac_update( &ctx, src_str, src_len / 2 );
670  sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
671  sha512_hmac_update( &ctx, src_str + src_len, 0 );
672  sha512_hmac_finish( &ctx, output );
673 
674  hexify( hash_str, output, sizeof output );
675  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
676 
677  /* Again, to test hmac_reset() */
678  memset( hash_str, 0x00, sizeof hash_str );
679  memset( output, 0x00, sizeof output );
680 
681  sha512_hmac_reset( &ctx );
682  sha512_hmac_update( &ctx, src_str, src_len / 2 );
683  sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
684  sha512_hmac_finish( &ctx, output );
685 
686  hexify( hash_str, output, sizeof output );
687  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
688 
689 exit:
690  sha512_free( &ctx );
691 }
692 #endif /* POLARSSL_SHA512_C */
693 
694 
695 
696 
697 int dep_check( char *str )
698 {
699  if( str == NULL )
700  return( 1 );
701 
702  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
703  {
704 #if defined(POLARSSL_SHA256_C)
705  return( 0 );
706 #else
707  return( 1 );
708 #endif
709  }
710  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
711  {
712 #if defined(POLARSSL_SHA1_C)
713  return( 0 );
714 #else
715  return( 1 );
716 #endif
717  }
718  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
719  {
720 #if defined(POLARSSL_SHA512_C)
721  return( 0 );
722 #else
723  return( 1 );
724 #endif
725  }
726 
727 
728  return( 1 );
729 }
730 
731 int dispatch_test(int cnt, char *params[50])
732 {
733  int ret;
734  ((void) cnt);
735  ((void) params);
736 
737 #if defined(TEST_SUITE_ACTIVE)
738  if( strcmp( params[0], "sha1_hmac" ) == 0 )
739  {
740  #ifdef POLARSSL_SHA1_C
741 
742  int param1;
743  char *param2 = params[2];
744  char *param3 = params[3];
745  char *param4 = params[4];
746 
747  if( cnt != 5 )
748  {
749  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
750  return( 2 );
751  }
752 
753  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
754  if( verify_string( &param2 ) != 0 ) return( 2 );
755  if( verify_string( &param3 ) != 0 ) return( 2 );
756  if( verify_string( &param4 ) != 0 ) return( 2 );
757 
758  test_suite_sha1_hmac( param1, param2, param3, param4 );
759  return ( 0 );
760  #endif /* POLARSSL_SHA1_C */
761 
762  return ( 3 );
763  }
764  else
765  if( strcmp( params[0], "sha224_hmac" ) == 0 )
766  {
767  #ifdef POLARSSL_SHA256_C
768 
769  int param1;
770  char *param2 = params[2];
771  char *param3 = params[3];
772  char *param4 = params[4];
773 
774  if( cnt != 5 )
775  {
776  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
777  return( 2 );
778  }
779 
780  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
781  if( verify_string( &param2 ) != 0 ) return( 2 );
782  if( verify_string( &param3 ) != 0 ) return( 2 );
783  if( verify_string( &param4 ) != 0 ) return( 2 );
784 
785  test_suite_sha224_hmac( param1, param2, param3, param4 );
786  return ( 0 );
787  #endif /* POLARSSL_SHA256_C */
788 
789  return ( 3 );
790  }
791  else
792  if( strcmp( params[0], "sha256_hmac" ) == 0 )
793  {
794  #ifdef POLARSSL_SHA256_C
795 
796  int param1;
797  char *param2 = params[2];
798  char *param3 = params[3];
799  char *param4 = params[4];
800 
801  if( cnt != 5 )
802  {
803  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
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_string( &param3 ) != 0 ) return( 2 );
810  if( verify_string( &param4 ) != 0 ) return( 2 );
811 
812  test_suite_sha256_hmac( param1, param2, param3, param4 );
813  return ( 0 );
814  #endif /* POLARSSL_SHA256_C */
815 
816  return ( 3 );
817  }
818  else
819  if( strcmp( params[0], "sha384_hmac" ) == 0 )
820  {
821  #ifdef POLARSSL_SHA512_C
822 
823  int param1;
824  char *param2 = params[2];
825  char *param3 = params[3];
826  char *param4 = params[4];
827 
828  if( cnt != 5 )
829  {
830  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
831  return( 2 );
832  }
833 
834  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
835  if( verify_string( &param2 ) != 0 ) return( 2 );
836  if( verify_string( &param3 ) != 0 ) return( 2 );
837  if( verify_string( &param4 ) != 0 ) return( 2 );
838 
839  test_suite_sha384_hmac( param1, param2, param3, param4 );
840  return ( 0 );
841  #endif /* POLARSSL_SHA512_C */
842 
843  return ( 3 );
844  }
845  else
846  if( strcmp( params[0], "sha512_hmac" ) == 0 )
847  {
848  #ifdef POLARSSL_SHA512_C
849 
850  int param1;
851  char *param2 = params[2];
852  char *param3 = params[3];
853  char *param4 = params[4];
854 
855  if( cnt != 5 )
856  {
857  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
858  return( 2 );
859  }
860 
861  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
862  if( verify_string( &param2 ) != 0 ) return( 2 );
863  if( verify_string( &param3 ) != 0 ) return( 2 );
864  if( verify_string( &param4 ) != 0 ) return( 2 );
865 
866  test_suite_sha512_hmac( param1, param2, param3, param4 );
867  return ( 0 );
868  #endif /* POLARSSL_SHA512_C */
869 
870  return ( 3 );
871  }
872  else
873 
874  {
875  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
876  fflush( stdout );
877  return( 1 );
878  }
879 #else
880  return( 3 );
881 #endif
882  return( ret );
883 }
884 
885 int get_line( FILE *f, char *buf, size_t len )
886 {
887  char *ret;
888 
889  ret = fgets( buf, len, f );
890  if( ret == NULL )
891  return( -1 );
892 
893  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
894  buf[strlen(buf) - 1] = '\0';
895  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
896  buf[strlen(buf) - 1] = '\0';
897 
898  return( 0 );
899 }
900 
901 int parse_arguments( char *buf, size_t len, char *params[50] )
902 {
903  int cnt = 0, i;
904  char *cur = buf;
905  char *p = buf, *q;
906 
907  params[cnt++] = cur;
908 
909  while( *p != '\0' && p < buf + len )
910  {
911  if( *p == '\\' )
912  {
913  p++;
914  p++;
915  continue;
916  }
917  if( *p == ':' )
918  {
919  if( p + 1 < buf + len )
920  {
921  cur = p + 1;
922  params[cnt++] = cur;
923  }
924  *p = '\0';
925  }
926 
927  p++;
928  }
929 
930  // Replace newlines, question marks and colons in strings
931  for( i = 0; i < cnt; i++ )
932  {
933  p = params[i];
934  q = params[i];
935 
936  while( *p != '\0' )
937  {
938  if( *p == '\\' && *(p + 1) == 'n' )
939  {
940  p += 2;
941  *(q++) = '\n';
942  }
943  else if( *p == '\\' && *(p + 1) == ':' )
944  {
945  p += 2;
946  *(q++) = ':';
947  }
948  else if( *p == '\\' && *(p + 1) == '?' )
949  {
950  p += 2;
951  *(q++) = '?';
952  }
953  else
954  *(q++) = *(p++);
955  }
956  *q = '\0';
957  }
958 
959  return( cnt );
960 }
961 
962 int main()
963 {
964  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
965  const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_hmac_shax.data";
966  FILE *file;
967  char buf[5000];
968  char *params[50];
969 
970 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
971  unsigned char alloc_buf[1000000];
972  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
973 #endif
974 
975  file = fopen( filename, "r" );
976  if( file == NULL )
977  {
978  fprintf( stderr, "Failed to open\n" );
979  return( 1 );
980  }
981 
982  while( !feof( file ) )
983  {
984  int skip = 0;
985 
986  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
987  break;
988  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
989  fprintf( stdout, " " );
990  for( i = strlen( buf ) + 1; i < 67; i++ )
991  fprintf( stdout, "." );
992  fprintf( stdout, " " );
993  fflush( stdout );
994 
995  total_tests++;
996 
997  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
998  break;
999  cnt = parse_arguments( buf, strlen(buf), params );
1000 
1001  if( strcmp( params[0], "depends_on" ) == 0 )
1002  {
1003  for( i = 1; i < cnt; i++ )
1004  if( dep_check( params[i] ) != 0 )
1005  skip = 1;
1006 
1007  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1008  break;
1009  cnt = parse_arguments( buf, strlen(buf), params );
1010  }
1011 
1012  if( skip == 0 )
1013  {
1014  test_errors = 0;
1015  ret = dispatch_test( cnt, params );
1016  }
1017 
1018  if( skip == 1 || ret == 3 )
1019  {
1020  total_skipped++;
1021  fprintf( stdout, "----\n" );
1022  fflush( stdout );
1023  }
1024  else if( ret == 0 && test_errors == 0 )
1025  {
1026  fprintf( stdout, "PASS\n" );
1027  fflush( stdout );
1028  }
1029  else if( ret == 2 )
1030  {
1031  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1032  fclose(file);
1033  exit( 2 );
1034  }
1035  else
1036  total_errors++;
1037 
1038  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1039  break;
1040  if( strlen(buf) != 0 )
1041  {
1042  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1043  return( 1 );
1044  }
1045  }
1046  fclose(file);
1047 
1048  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1049  if( total_errors == 0 )
1050  fprintf( stdout, "PASSED" );
1051  else
1052  fprintf( stdout, "FAILED" );
1053 
1054  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1055  total_tests - total_errors, total_tests, total_skipped );
1056 
1057 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1058 #if defined(POLARSSL_MEMORY_DEBUG)
1059  memory_buffer_alloc_status();
1060 #endif
1062 #endif
1063 
1064  return( total_errors != 0 );
1065 }
1066 
1067 
void sha256_hmac_update(sha256_context *ctx, const unsigned char *input, size_t ilen)
SHA-256 HMAC process buffer.
void sha512_hmac_update(sha512_context *ctx, const unsigned char *input, size_t ilen)
SHA-512 HMAC process buffer.
void sha1_hmac_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 HMAC final digest.
#define polarssl_malloc
Memory allocation layer (Deprecated to platform layer)
SHA-1 context structure.
Definition: sha1.h:58
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void sha256_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = HMAC-SHA-256( hmac key, input buffer )
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void sha1_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[20])
Output = HMAC-SHA-1( hmac key, input buffer )
Configuration options (set of defines)
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
void sha256_hmac_finish(sha256_context *ctx, unsigned char output[32])
SHA-256 HMAC final digest.
#define TEST_ASSERT(TEST)
void sha256_hmac_starts(sha256_context *ctx, const unsigned char *key, size_t keylen, int is224)
SHA-256 HMAC context setup.
void sha1_hmac_reset(sha1_context *ctx)
SHA-1 HMAC context reset.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
SHA-512 context structure.
Definition: sha512.h:59
void sha512_hmac_finish(sha512_context *ctx, unsigned char output[64])
SHA-512 HMAC final digest.
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
int dep_check(char *str)
void sha1_hmac_starts(sha1_context *ctx, const unsigned char *key, size_t keylen)
SHA-1 HMAC context setup.
static int test_errors
int get_line(FILE *f, char *buf, size_t len)
void sha512_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = HMAC-SHA-512( hmac key, input buffer )
void sha256_hmac_reset(sha256_context *ctx)
SHA-256 HMAC context reset.
int dispatch_test(int cnt, char *params[50])
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
void sha256_init(sha256_context *ctx)
Initialize SHA-256 context.
int verify_string(char **str)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void sha256_free(sha256_context *ctx)
Clear SHA-256 context.
SHA-1 cryptographic hash function.
unsigned char * buf
SHA-384 and SHA-512 cryptographic hash function.
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
#define PUT_UINT32_BE(n, b, i)
void sha512_hmac_starts(sha512_context *ctx, const unsigned char *key, size_t keylen, int is384)
SHA-512 HMAC context setup.
void sha512_hmac_reset(sha512_context *ctx)
SHA-512 HMAC context reset.
SHA-256 context structure.
Definition: sha256.h:58
void sha512_free(sha512_context *ctx)
Clear SHA-512 context.
void sha1_hmac_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 HMAC process buffer.
int verify_int(char *str, int *value)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
SHA-224 and SHA-256 cryptographic hash function.
int main()
int parse_arguments(char *buf, size_t len, char *params[50])
void sha512_init(sha512_context *ctx)
Initialize SHA-512 context.