PolarSSL v1.3.9
test_suite_cipher.gcm.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_CIPHER_C
8
9#include <polarssl/cipher.h>
10
11#if defined(POLARSSL_GCM_C)
12#include <polarssl/gcm.h>
13#endif
14#endif /* POLARSSL_CIPHER_C */
15
16
17#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
18#include "polarssl/memory.h"
19#endif
20
21#if defined(POLARSSL_PLATFORM_C)
22#include "polarssl/platform.h"
23#else
24#define polarssl_malloc malloc
25#define polarssl_free free
26#endif
27
28#ifdef _MSC_VER
29#include <basetsd.h>
30typedef UINT32 uint32_t;
31#else
32#include <inttypes.h>
33#endif
34
35#include <assert.h>
36#include <stdlib.h>
37#include <string.h>
38
39/*
40 * 32-bit integer manipulation macros (big endian)
41 */
42#ifndef GET_UINT32_BE
43#define GET_UINT32_BE(n,b,i) \
44{ \
45 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48 | ( (uint32_t) (b)[(i) + 3] ); \
49}
50#endif
51
52#ifndef PUT_UINT32_BE
53#define PUT_UINT32_BE(n,b,i) \
54{ \
55 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58 (b)[(i) + 3] = (unsigned char) ( (n) ); \
59}
60#endif
61
62static int unhexify(unsigned char *obuf, const char *ibuf)
63{
64 unsigned char c, c2;
65 int len = strlen(ibuf) / 2;
66 assert(!(strlen(ibuf) %1)); // must be even number of bytes
67
68 while (*ibuf != 0)
69 {
70 c = *ibuf++;
71 if( c >= '0' && c <= '9' )
72 c -= '0';
73 else if( c >= 'a' && c <= 'f' )
74 c -= 'a' - 10;
75 else if( c >= 'A' && c <= 'F' )
76 c -= 'A' - 10;
77 else
78 assert( 0 );
79
80 c2 = *ibuf++;
81 if( c2 >= '0' && c2 <= '9' )
82 c2 -= '0';
83 else if( c2 >= 'a' && c2 <= 'f' )
84 c2 -= 'a' - 10;
85 else if( c2 >= 'A' && c2 <= 'F' )
86 c2 -= 'A' - 10;
87 else
88 assert( 0 );
89
90 *obuf++ = ( c << 4 ) | c2;
91 }
92
93 return len;
94}
95
96static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
97{
98 unsigned char l, h;
99
100 while (len != 0)
101 {
102 h = (*ibuf) / 16;
103 l = (*ibuf) % 16;
104
105 if( h < 10 )
106 *obuf++ = '0' + h;
107 else
108 *obuf++ = 'a' + h - 10;
109
110 if( l < 10 )
111 *obuf++ = '0' + l;
112 else
113 *obuf++ = 'a' + l - 10;
114
115 ++ibuf;
116 len--;
117 }
118}
119
127static unsigned char *zero_alloc( size_t len )
128{
129 void *p;
130 size_t actual_len = len != 0 ? len : 1;
131
132 p = polarssl_malloc( actual_len );
133 assert( p != NULL );
134
135 memset( p, 0x00, actual_len );
136
137 return( p );
138}
139
150static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
151{
152 unsigned char *obuf;
153
154 *olen = strlen(ibuf) / 2;
155
156 if( *olen == 0 )
157 return( zero_alloc( *olen ) );
158
159 obuf = polarssl_malloc( *olen );
160 assert( obuf != NULL );
161
162 (void) unhexify( obuf, ibuf );
163
164 return( obuf );
165}
166
176static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
177{
178#if !defined(__OpenBSD__)
179 size_t i;
180
181 if( rng_state != NULL )
182 rng_state = NULL;
183
184 for( i = 0; i < len; ++i )
185 output[i] = rand();
186#else
187 if( rng_state != NULL )
188 rng_state = NULL;
189
190 arc4random_buf( output, len );
191#endif /* !OpenBSD */
192
193 return( 0 );
194}
195
201static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
202{
203 if( rng_state != NULL )
204 rng_state = NULL;
205
206 memset( output, 0, len );
207
208 return( 0 );
209}
210
211typedef struct
212{
213 unsigned char *buf;
214 size_t length;
216
228static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
229{
230 rnd_buf_info *info = (rnd_buf_info *) rng_state;
231 size_t use_len;
232
233 if( rng_state == NULL )
234 return( rnd_std_rand( NULL, output, len ) );
235
236 use_len = len;
237 if( len > info->length )
238 use_len = info->length;
239
240 if( use_len )
241 {
242 memcpy( output, info->buf, use_len );
243 info->buf += use_len;
244 info->length -= use_len;
245 }
246
247 if( len - use_len > 0 )
248 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
249
250 return( 0 );
251}
252
260typedef struct
261{
262 uint32_t key[16];
263 uint32_t v0, v1;
265
274static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
275{
276 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
277 uint32_t i, *k, sum, delta=0x9E3779B9;
278 unsigned char result[4], *out = output;
279
280 if( rng_state == NULL )
281 return( rnd_std_rand( NULL, output, len ) );
282
283 k = info->key;
284
285 while( len > 0 )
286 {
287 size_t use_len = ( len > 4 ) ? 4 : len;
288 sum = 0;
289
290 for( i = 0; i < 32; i++ )
291 {
292 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
293 sum += delta;
294 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
295 }
296
297 PUT_UINT32_BE( info->v0, result, 0 );
298 memcpy( out, result, use_len );
299 len -= use_len;
300 out += 4;
301 }
302
303 return( 0 );
304}
305
306
307#include <stdio.h>
308#include <string.h>
309
310#if defined(POLARSSL_PLATFORM_C)
311#include "polarssl/platform.h"
312#else
313#define polarssl_printf printf
314#define polarssl_malloc malloc
315#define polarssl_free free
316#endif
317
318static int test_errors = 0;
319
320#ifdef POLARSSL_CIPHER_C
321
322#define TEST_SUITE_ACTIVE
323
324static int test_assert( int correct, const char *test )
325{
326 if( correct )
327 return( 0 );
328
329 test_errors++;
330 if( test_errors == 1 )
331 printf( "FAILED\n" );
332 printf( " %s\n", test );
333
334 return( 1 );
335}
336
337#define TEST_ASSERT( TEST ) \
338 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
339 if( test_errors) goto exit; \
340 } while (0)
341
342int verify_string( char **str )
343{
344 if( (*str)[0] != '"' ||
345 (*str)[strlen( *str ) - 1] != '"' )
346 {
347 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
348 return( -1 );
349 }
350
351 (*str)++;
352 (*str)[strlen( *str ) - 1] = '\0';
353
354 return( 0 );
355}
356
357int verify_int( char *str, int *value )
358{
359 size_t i;
360 int minus = 0;
361 int digits = 1;
362 int hex = 0;
363
364 for( i = 0; i < strlen( str ); i++ )
365 {
366 if( i == 0 && str[i] == '-' )
367 {
368 minus = 1;
369 continue;
370 }
371
372 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
373 str[i - 1] == '0' && str[i] == 'x' )
374 {
375 hex = 1;
376 continue;
377 }
378
379 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
380 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
381 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
382 {
383 digits = 0;
384 break;
385 }
386 }
387
388 if( digits )
389 {
390 if( hex )
391 *value = strtol( str, NULL, 16 );
392 else
393 *value = strtol( str, NULL, 10 );
394
395 return( 0 );
396 }
397
398 if( strcmp( str, "POLARSSL_CIPHER_AES_192_GCM" ) == 0 )
399 {
400 *value = ( POLARSSL_CIPHER_AES_192_GCM );
401 return( 0 );
402 }
403 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_192_GCM" ) == 0 )
404 {
406 return( 0 );
407 }
408 if( strcmp( str, "-1" ) == 0 )
409 {
410 *value = ( -1 );
411 return( 0 );
412 }
413 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_GCM" ) == 0 )
414 {
416 return( 0 );
417 }
418 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_256_GCM" ) == 0 )
419 {
421 return( 0 );
422 }
423 if( strcmp( str, "POLARSSL_CIPHER_AES_256_GCM" ) == 0 )
424 {
425 *value = ( POLARSSL_CIPHER_AES_256_GCM );
426 return( 0 );
427 }
428 if( strcmp( str, "POLARSSL_CIPHER_AES_128_GCM" ) == 0 )
429 {
430 *value = ( POLARSSL_CIPHER_AES_128_GCM );
431 return( 0 );
432 }
433 if( strcmp( str, "POLARSSL_ERR_CIPHER_AUTH_FAILED" ) == 0 )
434 {
436 return( 0 );
437 }
438
439
440 printf( "Expected integer for parameter and got: %s\n", str );
441 return( -1 );
442}
443
444void test_suite_cipher_list( )
445{
446 const int *cipher_type;
447
448 for( cipher_type = cipher_list(); *cipher_type != 0; cipher_type++ )
449 TEST_ASSERT( cipher_info_from_type( *cipher_type ) != NULL );
450
451exit:
452 return;
453}
454
455void test_suite_cipher_null_args( )
456{
458 const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) );
459 unsigned char buf[1] = { 0 };
460 size_t olen;
461
462 cipher_init( &ctx );
463
464 TEST_ASSERT( cipher_get_block_size( NULL ) == 0 );
465 TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 );
466
469
470 TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 );
471 TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 );
472
473 TEST_ASSERT( cipher_info_from_string( NULL ) == NULL );
474
475 TEST_ASSERT( cipher_init_ctx( &ctx, NULL )
477 TEST_ASSERT( cipher_init_ctx( NULL, info )
479
484
485 TEST_ASSERT( cipher_set_iv( NULL, buf, 0 )
487 TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 )
489
492
493#if defined(POLARSSL_GCM_C)
494 TEST_ASSERT( cipher_update_ad( NULL, buf, 0 )
496 TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 )
498#endif
499
500 TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen )
502 TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen )
504
505 TEST_ASSERT( cipher_finish( NULL, buf, &olen )
507 TEST_ASSERT( cipher_finish( &ctx, buf, &olen )
509
510#if defined(POLARSSL_GCM_C)
511 TEST_ASSERT( cipher_write_tag( NULL, buf, olen )
513 TEST_ASSERT( cipher_write_tag( &ctx, buf, olen )
515
516 TEST_ASSERT( cipher_check_tag( NULL, buf, olen )
518 TEST_ASSERT( cipher_check_tag( &ctx, buf, olen )
520#endif
521
522exit:
523 return;
524}
525
526void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
527 int length_val, int pad_mode )
528{
529 size_t length = length_val, outlen, total_len, i;
530 unsigned char key[32];
531 unsigned char iv[16];
532 unsigned char ad[13];
533 unsigned char tag[16];
534 unsigned char inbuf[64];
535 unsigned char encbuf[64];
536 unsigned char decbuf[64];
537
538 const cipher_info_t *cipher_info;
539 cipher_context_t ctx_dec;
540 cipher_context_t ctx_enc;
541
542 /*
543 * Prepare contexts
544 */
545 cipher_init( &ctx_dec );
546 cipher_init( &ctx_enc );
547
548 memset( key, 0x2a, sizeof( key ) );
549
550 /* Check and get info structures */
551 cipher_info = cipher_info_from_type( cipher_id );
552 TEST_ASSERT( NULL != cipher_info );
553 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
554
555 /* Initialise enc and dec contexts */
556 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
557 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
558
559 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
560 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
561
562#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
563 if( -1 != pad_mode )
564 {
565 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
566 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
567 }
568#else
569 (void) pad_mode;
570#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
571
572 /*
573 * Do a few encode/decode cycles
574 */
575 for( i = 0; i < 3; i++ )
576 {
577 memset( iv , 0x00 + i, sizeof( iv ) );
578 memset( ad, 0x10 + i, sizeof( ad ) );
579 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
580
581 memset( encbuf, 0, sizeof( encbuf ) );
582 memset( decbuf, 0, sizeof( decbuf ) );
583 memset( tag, 0, sizeof( tag ) );
584
585 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
586 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
587
588 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
589 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
590
591#if defined(POLARSSL_GCM_C)
592 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
593 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
594#endif
595
596 /* encode length number of bytes from inbuf */
597 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
598 total_len = outlen;
599
600 TEST_ASSERT( total_len == length ||
601 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
602 total_len < length &&
603 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
604
605 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
606 total_len += outlen;
607
608#if defined(POLARSSL_GCM_C)
609 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
610#endif
611
612 TEST_ASSERT( total_len == length ||
613 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
614 total_len > length &&
615 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
616
617 /* decode the previously encoded string */
618 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
619 total_len = outlen;
620
621 TEST_ASSERT( total_len == length ||
622 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
623 total_len < length &&
624 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
625
626 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
627 total_len += outlen;
628
629#if defined(POLARSSL_GCM_C)
630 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
631#endif
632
633 /* check result */
634 TEST_ASSERT( total_len == length );
635 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
636 }
637
638 /*
639 * Done
640 */
641exit:
642 cipher_free( &ctx_dec );
643 cipher_free( &ctx_enc );
644}
645
646void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
647 int length_val, int ret )
648{
649 size_t length = length_val;
650 unsigned char key[32];
651 unsigned char iv[16];
652
653 const cipher_info_t *cipher_info;
655
656 unsigned char inbuf[64];
657 unsigned char encbuf[64];
658
659 size_t outlen = 0;
660
661 memset( key, 0, 32 );
662 memset( iv , 0, 16 );
663
664 cipher_init( &ctx );
665
666 memset( inbuf, 5, 64 );
667 memset( encbuf, 0, 64 );
668
669 /* Check and get info structures */
670 cipher_info = cipher_info_from_type( cipher_id );
671 TEST_ASSERT( NULL != cipher_info );
672
673 /* Initialise context */
674 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
675 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
676#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
677 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
678#else
679 (void) pad_mode;
680#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
681 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
682 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
683#if defined(POLARSSL_GCM_C)
684 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
685#endif
686
687 /* encode length number of bytes from inbuf */
688 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
689 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
690
691 /* done */
692exit:
693 cipher_free( &ctx );
694}
695
696void test_suite_dec_empty_buf()
697{
698 unsigned char key[32];
699 unsigned char iv[16];
700
701 cipher_context_t ctx_dec;
702 const cipher_info_t *cipher_info;
703
704 unsigned char encbuf[64];
705 unsigned char decbuf[64];
706
707 size_t outlen = 0;
708
709 memset( key, 0, 32 );
710 memset( iv , 0, 16 );
711
712 cipher_init( &ctx_dec );
713
714 memset( encbuf, 0, 64 );
715 memset( decbuf, 0, 64 );
716
717 /* Initialise context */
719 TEST_ASSERT( NULL != cipher_info);
720
721 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
722
723 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
724
725 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
726
727 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
728
729#if defined(POLARSSL_GCM_C)
730 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
731#endif
732
733 /* decode 0-byte string */
734 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
735 TEST_ASSERT( 0 == outlen );
737 &ctx_dec, decbuf + outlen, &outlen ) );
738 TEST_ASSERT( 0 == outlen );
739
740exit:
741 cipher_free( &ctx_dec );
742}
743
744void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
745 int second_length_val )
746{
747 size_t first_length = first_length_val;
748 size_t second_length = second_length_val;
749 size_t length = first_length + second_length;
750 unsigned char key[32];
751 unsigned char iv[16];
752
753 cipher_context_t ctx_dec;
754 cipher_context_t ctx_enc;
755 const cipher_info_t *cipher_info;
756
757 unsigned char inbuf[64];
758 unsigned char encbuf[64];
759 unsigned char decbuf[64];
760
761 size_t outlen = 0;
762 size_t totaloutlen = 0;
763
764 memset( key, 0, 32 );
765 memset( iv , 0, 16 );
766
767 cipher_init( &ctx_dec );
768 cipher_init( &ctx_enc );
769
770 memset( inbuf, 5, 64 );
771 memset( encbuf, 0, 64 );
772 memset( decbuf, 0, 64 );
773
774 /* Initialise enc and dec contexts */
775 cipher_info = cipher_info_from_type( cipher_id );
776 TEST_ASSERT( NULL != cipher_info);
777
778 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
779 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
780
781 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
782 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
783
784 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
785 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
786
787 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
788 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
789
790#if defined(POLARSSL_GCM_C)
791 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
792 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
793#endif
794
795 /* encode length number of bytes from inbuf */
796 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
797 totaloutlen = outlen;
798 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
799 totaloutlen += outlen;
800 TEST_ASSERT( totaloutlen == length ||
801 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
802 totaloutlen < length &&
803 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
804
805 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
806 totaloutlen += outlen;
807 TEST_ASSERT( totaloutlen == length ||
808 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
809 totaloutlen > length &&
810 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
811
812 /* decode the previously encoded string */
813 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
814 totaloutlen = outlen;
815
816 TEST_ASSERT( totaloutlen == length ||
817 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
818 totaloutlen < length &&
819 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
820
821 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
822 totaloutlen += outlen;
823
824 TEST_ASSERT( totaloutlen == length );
825
826 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
827
828exit:
829 cipher_free( &ctx_dec );
830 cipher_free( &ctx_enc );
831}
832
833void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
834 char *hex_key, char *hex_iv,
835 char *hex_cipher, char *hex_clear,
836 char *hex_ad, char *hex_tag,
837 int finish_result, int tag_result )
838{
839 unsigned char key[50];
840 unsigned char iv[50];
841 unsigned char cipher[200];
842 unsigned char clear[200];
843 unsigned char ad[200];
844 unsigned char tag[20];
845 size_t key_len, iv_len, cipher_len, clear_len;
846#if defined(POLARSSL_GCM_C)
847 size_t ad_len, tag_len;
848#endif
850 unsigned char output[200];
851 size_t outlen, total_len;
852
853 cipher_init( &ctx );
854
855 memset( key, 0x00, sizeof( key ) );
856 memset( iv, 0x00, sizeof( iv ) );
857 memset( cipher, 0x00, sizeof( cipher ) );
858 memset( clear, 0x00, sizeof( clear ) );
859 memset( ad, 0x00, sizeof( ad ) );
860 memset( tag, 0x00, sizeof( tag ) );
861 memset( output, 0x00, sizeof( output ) );
862
863 key_len = unhexify( key, hex_key );
864 iv_len = unhexify( iv, hex_iv );
865 cipher_len = unhexify( cipher, hex_cipher );
866 clear_len = unhexify( clear, hex_clear );
867#if defined(POLARSSL_GCM_C)
868 ad_len = unhexify( ad, hex_ad );
869 tag_len = unhexify( tag, hex_tag );
870#else
871 ((void) hex_ad);
872 ((void) hex_tag);
873#endif
874
875 /* Prepare context */
876 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
877 cipher_info_from_type( cipher_id ) ) );
878 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
879#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
880 if( pad_mode != -1 )
881 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
882#else
883 (void) pad_mode;
884#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
885 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
886 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
887#if defined(POLARSSL_GCM_C)
888 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
889#endif
890
891 /* decode buffer and check tag */
892 total_len = 0;
893 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
894 total_len += outlen;
895 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
896 &outlen ) );
897 total_len += outlen;
898#if defined(POLARSSL_GCM_C)
899 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
900#endif
901
902 /* check plaintext only if everything went fine */
903 if( 0 == finish_result && 0 == tag_result )
904 {
905 TEST_ASSERT( total_len == clear_len );
906 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
907 }
908
909exit:
910 cipher_free( &ctx );
911}
912
913#ifdef POLARSSL_CIPHER_MODE_AEAD
914void test_suite_auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
915 char *hex_ad, char *hex_cipher,
916 char *hex_tag, char *hex_clear )
917{
918 int ret;
919 unsigned char key[50];
920 unsigned char iv[50];
921 unsigned char cipher[200];
922 unsigned char clear[200];
923 unsigned char ad[200];
924 unsigned char tag[20];
925 unsigned char my_tag[20];
926 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
928 unsigned char output[200];
929 size_t outlen;
930
931 cipher_init( &ctx );
932
933 memset( key, 0x00, sizeof( key ) );
934 memset( iv, 0x00, sizeof( iv ) );
935 memset( cipher, 0x00, sizeof( cipher ) );
936 memset( clear, 0x00, sizeof( clear ) );
937 memset( ad, 0x00, sizeof( ad ) );
938 memset( tag, 0x00, sizeof( tag ) );
939 memset( my_tag, 0xFF, sizeof( my_tag ) );
940 memset( output, 0xFF, sizeof( output ) );
941
942 key_len = unhexify( key, hex_key );
943 iv_len = unhexify( iv, hex_iv );
944 cipher_len = unhexify( cipher, hex_cipher );
945 ad_len = unhexify( ad, hex_ad );
946 tag_len = unhexify( tag, hex_tag );
947
948 /* Prepare context */
949 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
950 cipher_info_from_type( cipher_id ) ) );
951 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
952
953 /* decode buffer and check tag */
954 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
955 cipher, cipher_len, output, &outlen,
956 tag, tag_len );
957
958 /* make sure we didn't overwrite */
959 TEST_ASSERT( output[outlen + 0] == 0xFF );
960 TEST_ASSERT( output[outlen + 1] == 0xFF );
961
962 /* make sure the message is rejected if it should be */
963 if( strcmp( hex_clear, "FAIL" ) == 0 )
964 {
966 goto exit;
967 }
968
969 /* otherwise, make sure it was decrypted properly */
970 TEST_ASSERT( ret == 0 );
971
972 clear_len = unhexify( clear, hex_clear );
973 TEST_ASSERT( outlen == clear_len );
974 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
975
976 /* then encrypt the clear and make sure we get the same ciphertext and tag */
977 memset( output, 0xFF, sizeof( output ) );
978 outlen = 0;
979
980 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
981 clear, clear_len, output, &outlen,
982 my_tag, tag_len );
983 TEST_ASSERT( ret == 0 );
984
985 TEST_ASSERT( outlen == clear_len );
986 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
987 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
988
989 /* make sure we didn't overwrite */
990 TEST_ASSERT( output[outlen + 0] == 0xFF );
991 TEST_ASSERT( output[outlen + 1] == 0xFF );
992 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
993 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
994
995
996exit:
997 cipher_free( &ctx );
998}
999#endif /* POLARSSL_CIPHER_MODE_AEAD */
1000
1001void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
1002 char *hex_input, char *hex_result,
1003 int finish_result )
1004{
1005 unsigned char key[50];
1006 unsigned char input[16];
1007 unsigned char result[16];
1008 size_t key_len;
1009 cipher_context_t ctx;
1010 unsigned char output[32];
1011 size_t outlen;
1012
1013 cipher_init( &ctx );
1014
1015 memset( key, 0x00, sizeof( key ) );
1016 memset( input, 0x00, sizeof( input ) );
1017 memset( result, 0x00, sizeof( result ) );
1018 memset( output, 0x00, sizeof( output ) );
1019
1020 /* Prepare context */
1021 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
1022 cipher_info_from_type( cipher_id ) ) );
1023
1024 key_len = unhexify( key, hex_key );
1025 TEST_ASSERT( unhexify( input, hex_input ) ==
1026 (int) cipher_get_block_size( &ctx ) );
1027 TEST_ASSERT( unhexify( result, hex_result ) ==
1028 (int) cipher_get_block_size( &ctx ) );
1029
1030 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
1031
1032 TEST_ASSERT( 0 == cipher_update( &ctx, input,
1033 cipher_get_block_size( &ctx ),
1034 output, &outlen ) );
1035 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
1036 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
1037 &outlen ) );
1038 TEST_ASSERT( 0 == outlen );
1039
1040 /* check plaintext only if everything went fine */
1041 if( 0 == finish_result )
1042 TEST_ASSERT( 0 == memcmp( output, result,
1043 cipher_get_block_size( &ctx ) ) );
1044
1045exit:
1046 cipher_free( &ctx );
1047}
1048
1049#ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1050void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
1051{
1052 const cipher_info_t *cipher_info;
1053 cipher_context_t ctx;
1054
1055 cipher_init( &ctx );
1056
1057 cipher_info = cipher_info_from_type( cipher_id );
1058 TEST_ASSERT( NULL != cipher_info );
1059 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
1060
1061 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
1062
1063exit:
1064 cipher_free( &ctx );
1065}
1066#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1067
1068#ifdef POLARSSL_CIPHER_MODE_CBC
1069void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
1070{
1071 cipher_info_t cipher_info;
1072 cipher_context_t ctx;
1073 unsigned char input[16];
1074 size_t ilen, dlen;
1075
1076 /* build a fake context just for getting access to get_padding */
1077 cipher_init( &ctx );
1078 cipher_info.mode = POLARSSL_MODE_CBC;
1079 ctx.cipher_info = &cipher_info;
1080
1081 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
1082
1083 ilen = unhexify( input, input_str );
1084
1085 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
1086 if( 0 == ret )
1087 TEST_ASSERT( dlen == (size_t) dlen_check );
1088
1089exit:
1090 return;
1091}
1092#endif /* POLARSSL_CIPHER_MODE_CBC */
1093
1094#ifdef POLARSSL_SELF_TEST
1095void test_suite_cipher_selftest()
1096{
1097 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
1098
1099exit:
1100 return;
1101}
1102#endif /* POLARSSL_SELF_TEST */
1103
1104
1105#endif /* POLARSSL_CIPHER_C */
1106
1107
1108int dep_check( char *str )
1109{
1110 if( str == NULL )
1111 return( 1 );
1112
1113 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
1114 {
1115#if defined(POLARSSL_AES_C)
1116 return( 0 );
1117#else
1118 return( 1 );
1119#endif
1120 }
1121 if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
1122 {
1123#if defined(POLARSSL_CAMELLIA_C)
1124 return( 0 );
1125#else
1126 return( 1 );
1127#endif
1128 }
1129 if( strcmp( str, "POLARSSL_GCM_C" ) == 0 )
1130 {
1131#if defined(POLARSSL_GCM_C)
1132 return( 0 );
1133#else
1134 return( 1 );
1135#endif
1136 }
1137
1138
1139 return( 1 );
1140}
1141
1142int dispatch_test(int cnt, char *params[50])
1143{
1144 int ret;
1145 ((void) cnt);
1146 ((void) params);
1147
1148#if defined(TEST_SUITE_ACTIVE)
1149 if( strcmp( params[0], "cipher_list" ) == 0 )
1150 {
1151
1152
1153 if( cnt != 1 )
1154 {
1155 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1156 return( 2 );
1157 }
1158
1159
1160 test_suite_cipher_list( );
1161 return ( 0 );
1162
1163 return ( 3 );
1164 }
1165 else
1166 if( strcmp( params[0], "cipher_null_args" ) == 0 )
1167 {
1168
1169
1170 if( cnt != 1 )
1171 {
1172 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1173 return( 2 );
1174 }
1175
1176
1177 test_suite_cipher_null_args( );
1178 return ( 0 );
1179
1180 return ( 3 );
1181 }
1182 else
1183 if( strcmp( params[0], "enc_dec_buf" ) == 0 )
1184 {
1185
1186 int param1;
1187 char *param2 = params[2];
1188 int param3;
1189 int param4;
1190 int param5;
1191
1192 if( cnt != 6 )
1193 {
1194 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1195 return( 2 );
1196 }
1197
1198 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1199 if( verify_string( &param2 ) != 0 ) return( 2 );
1200 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1201 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1202 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1203
1204 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1205 return ( 0 );
1206
1207 return ( 3 );
1208 }
1209 else
1210 if( strcmp( params[0], "enc_fail" ) == 0 )
1211 {
1212
1213 int param1;
1214 int param2;
1215 int param3;
1216 int param4;
1217 int param5;
1218
1219 if( cnt != 6 )
1220 {
1221 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1222 return( 2 );
1223 }
1224
1225 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1226 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1227 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1228 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1229 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1230
1231 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1232 return ( 0 );
1233
1234 return ( 3 );
1235 }
1236 else
1237 if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1238 {
1239
1240
1241 if( cnt != 1 )
1242 {
1243 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1244 return( 2 );
1245 }
1246
1247
1248 test_suite_dec_empty_buf( );
1249 return ( 0 );
1250
1251 return ( 3 );
1252 }
1253 else
1254 if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1255 {
1256
1257 int param1;
1258 int param2;
1259 int param3;
1260 int param4;
1261
1262 if( cnt != 5 )
1263 {
1264 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1265 return( 2 );
1266 }
1267
1268 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1269 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1270 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1271 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1272
1273 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1274 return ( 0 );
1275
1276 return ( 3 );
1277 }
1278 else
1279 if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1280 {
1281
1282 int param1;
1283 int param2;
1284 char *param3 = params[3];
1285 char *param4 = params[4];
1286 char *param5 = params[5];
1287 char *param6 = params[6];
1288 char *param7 = params[7];
1289 char *param8 = params[8];
1290 int param9;
1291 int param10;
1292
1293 if( cnt != 11 )
1294 {
1295 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1296 return( 2 );
1297 }
1298
1299 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1300 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1301 if( verify_string( &param3 ) != 0 ) return( 2 );
1302 if( verify_string( &param4 ) != 0 ) return( 2 );
1303 if( verify_string( &param5 ) != 0 ) return( 2 );
1304 if( verify_string( &param6 ) != 0 ) return( 2 );
1305 if( verify_string( &param7 ) != 0 ) return( 2 );
1306 if( verify_string( &param8 ) != 0 ) return( 2 );
1307 if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1308 if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1309
1310 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1311 return ( 0 );
1312
1313 return ( 3 );
1314 }
1315 else
1316 if( strcmp( params[0], "auth_crypt_tv" ) == 0 )
1317 {
1318 #ifdef POLARSSL_CIPHER_MODE_AEAD
1319
1320 int param1;
1321 char *param2 = params[2];
1322 char *param3 = params[3];
1323 char *param4 = params[4];
1324 char *param5 = params[5];
1325 char *param6 = params[6];
1326 char *param7 = params[7];
1327
1328 if( cnt != 8 )
1329 {
1330 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1331 return( 2 );
1332 }
1333
1334 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1335 if( verify_string( &param2 ) != 0 ) return( 2 );
1336 if( verify_string( &param3 ) != 0 ) return( 2 );
1337 if( verify_string( &param4 ) != 0 ) return( 2 );
1338 if( verify_string( &param5 ) != 0 ) return( 2 );
1339 if( verify_string( &param6 ) != 0 ) return( 2 );
1340 if( verify_string( &param7 ) != 0 ) return( 2 );
1341
1342 test_suite_auth_crypt_tv( param1, param2, param3, param4, param5, param6, param7 );
1343 return ( 0 );
1344 #endif /* POLARSSL_CIPHER_MODE_AEAD */
1345
1346 return ( 3 );
1347 }
1348 else
1349 if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1350 {
1351
1352 int param1;
1353 int param2;
1354 char *param3 = params[3];
1355 char *param4 = params[4];
1356 char *param5 = params[5];
1357 int param6;
1358
1359 if( cnt != 7 )
1360 {
1361 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1362 return( 2 );
1363 }
1364
1365 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1366 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1367 if( verify_string( &param3 ) != 0 ) return( 2 );
1368 if( verify_string( &param4 ) != 0 ) return( 2 );
1369 if( verify_string( &param5 ) != 0 ) return( 2 );
1370 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1371
1372 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1373 return ( 0 );
1374
1375 return ( 3 );
1376 }
1377 else
1378 if( strcmp( params[0], "set_padding" ) == 0 )
1379 {
1380 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1381
1382 int param1;
1383 int param2;
1384 int param3;
1385
1386 if( cnt != 4 )
1387 {
1388 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1389 return( 2 );
1390 }
1391
1392 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1393 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1394 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1395
1396 test_suite_set_padding( param1, param2, param3 );
1397 return ( 0 );
1398 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1399
1400 return ( 3 );
1401 }
1402 else
1403 if( strcmp( params[0], "check_padding" ) == 0 )
1404 {
1405 #ifdef POLARSSL_CIPHER_MODE_CBC
1406
1407 int param1;
1408 char *param2 = params[2];
1409 int param3;
1410 int param4;
1411
1412 if( cnt != 5 )
1413 {
1414 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1415 return( 2 );
1416 }
1417
1418 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1419 if( verify_string( &param2 ) != 0 ) return( 2 );
1420 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1421 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1422
1423 test_suite_check_padding( param1, param2, param3, param4 );
1424 return ( 0 );
1425 #endif /* POLARSSL_CIPHER_MODE_CBC */
1426
1427 return ( 3 );
1428 }
1429 else
1430 if( strcmp( params[0], "cipher_selftest" ) == 0 )
1431 {
1432 #ifdef POLARSSL_SELF_TEST
1433
1434
1435 if( cnt != 1 )
1436 {
1437 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1438 return( 2 );
1439 }
1440
1441
1442 test_suite_cipher_selftest( );
1443 return ( 0 );
1444 #endif /* POLARSSL_SELF_TEST */
1445
1446 return ( 3 );
1447 }
1448 else
1449
1450 {
1451 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1452 fflush( stdout );
1453 return( 1 );
1454 }
1455#else
1456 return( 3 );
1457#endif
1458 return( ret );
1459}
1460
1461int get_line( FILE *f, char *buf, size_t len )
1462{
1463 char *ret;
1464
1465 ret = fgets( buf, len, f );
1466 if( ret == NULL )
1467 return( -1 );
1468
1469 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1470 buf[strlen(buf) - 1] = '\0';
1471 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1472 buf[strlen(buf) - 1] = '\0';
1473
1474 return( 0 );
1475}
1476
1477int parse_arguments( char *buf, size_t len, char *params[50] )
1478{
1479 int cnt = 0, i;
1480 char *cur = buf;
1481 char *p = buf, *q;
1482
1483 params[cnt++] = cur;
1484
1485 while( *p != '\0' && p < buf + len )
1486 {
1487 if( *p == '\\' )
1488 {
1489 p++;
1490 p++;
1491 continue;
1492 }
1493 if( *p == ':' )
1494 {
1495 if( p + 1 < buf + len )
1496 {
1497 cur = p + 1;
1498 params[cnt++] = cur;
1499 }
1500 *p = '\0';
1501 }
1502
1503 p++;
1504 }
1505
1506 // Replace newlines, question marks and colons in strings
1507 for( i = 0; i < cnt; i++ )
1508 {
1509 p = params[i];
1510 q = params[i];
1511
1512 while( *p != '\0' )
1513 {
1514 if( *p == '\\' && *(p + 1) == 'n' )
1515 {
1516 p += 2;
1517 *(q++) = '\n';
1518 }
1519 else if( *p == '\\' && *(p + 1) == ':' )
1520 {
1521 p += 2;
1522 *(q++) = ':';
1523 }
1524 else if( *p == '\\' && *(p + 1) == '?' )
1525 {
1526 p += 2;
1527 *(q++) = '?';
1528 }
1529 else
1530 *(q++) = *(p++);
1531 }
1532 *q = '\0';
1533 }
1534
1535 return( cnt );
1536}
1537
1538int main()
1539{
1540 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1541 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_cipher.gcm.data";
1542 FILE *file;
1543 char buf[5000];
1544 char *params[50];
1545
1546#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1547 unsigned char alloc_buf[1000000];
1548 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1549#endif
1550
1551 file = fopen( filename, "r" );
1552 if( file == NULL )
1553 {
1554 fprintf( stderr, "Failed to open\n" );
1555 return( 1 );
1556 }
1557
1558 while( !feof( file ) )
1559 {
1560 int skip = 0;
1561
1562 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1563 break;
1564 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1565 fprintf( stdout, " " );
1566 for( i = strlen( buf ) + 1; i < 67; i++ )
1567 fprintf( stdout, "." );
1568 fprintf( stdout, " " );
1569 fflush( stdout );
1570
1571 total_tests++;
1572
1573 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1574 break;
1575 cnt = parse_arguments( buf, strlen(buf), params );
1576
1577 if( strcmp( params[0], "depends_on" ) == 0 )
1578 {
1579 for( i = 1; i < cnt; i++ )
1580 if( dep_check( params[i] ) != 0 )
1581 skip = 1;
1582
1583 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1584 break;
1585 cnt = parse_arguments( buf, strlen(buf), params );
1586 }
1587
1588 if( skip == 0 )
1589 {
1590 test_errors = 0;
1591 ret = dispatch_test( cnt, params );
1592 }
1593
1594 if( skip == 1 || ret == 3 )
1595 {
1596 total_skipped++;
1597 fprintf( stdout, "----\n" );
1598 fflush( stdout );
1599 }
1600 else if( ret == 0 && test_errors == 0 )
1601 {
1602 fprintf( stdout, "PASS\n" );
1603 fflush( stdout );
1604 }
1605 else if( ret == 2 )
1606 {
1607 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1608 fclose(file);
1609 exit( 2 );
1610 }
1611 else
1612 total_errors++;
1613
1614 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1615 break;
1616 if( strlen(buf) != 0 )
1617 {
1618 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1619 return( 1 );
1620 }
1621 }
1622 fclose(file);
1623
1624 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1625 if( total_errors == 0 )
1626 fprintf( stdout, "PASSED" );
1627 else
1628 fprintf( stdout, "FAILED" );
1629
1630 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1631 total_tests - total_errors, total_tests, total_skipped );
1632
1633#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1634#if defined(POLARSSL_MEMORY_DEBUG)
1635 memory_buffer_alloc_status();
1636#endif
1638#endif
1639
1640 return( total_errors != 0 );
1641}
1642
1643
Generic cipher wrapper.
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher's IV/NONCE in bytes.
Definition cipher.h:418
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).
Definition cipher.h:62
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
int cipher_auth_encrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, unsigned char *tag, size_t tag_len)
Generic autenticated encryption (AEAD ciphers).
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
Definition cipher.h:58
@ POLARSSL_ENCRYPT
Definition cipher.h:157
@ POLARSSL_DECRYPT
Definition cipher.h:156
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
@ POLARSSL_CIPHER_CAMELLIA_256_GCM
Definition cipher.h:114
@ POLARSSL_CIPHER_AES_128_GCM
Definition cipher.h:97
@ POLARSSL_CIPHER_CAMELLIA_128_GCM
Definition cipher.h:112
@ POLARSSL_CIPHER_AES_256_GCM
Definition cipher.h:99
@ POLARSSL_CIPHER_CAMELLIA_192_GCM
Definition cipher.h:113
@ POLARSSL_CIPHER_AES_128_CBC
Definition cipher.h:88
@ POLARSSL_CIPHER_AES_192_GCM
Definition cipher.h:98
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition cipher.h:401
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition cipher.h:384
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
int cipher_auth_decrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, const unsigned char *tag, size_t tag_len)
Generic autenticated decryption (AEAD ciphers).
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition cipher.h:61
int cipher_self_test(int verbose)
Checkup routine.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
@ POLARSSL_MODE_NONE
Definition cipher.h:135
@ POLARSSL_MODE_CBC
Definition cipher.h:137
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
Configuration options (set of defines)
Galois/Counter mode for 128-bit block ciphers.
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 cipher context.
Definition cipher.h:258
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Definition cipher.h:270
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition cipher.h:260
Cipher information.
Definition cipher.h:226
cipher_mode_t mode
Cipher mode (e.g.
Definition cipher.h:231
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)
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.