PolarSSL v1.3.9
test_suite_cipher.ccm.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#ifdef POLARSSL_CIPHER_MODE_AEAD
399 if( strcmp( str, "POLARSSL_CIPHER_AES_128_CCM" ) == 0 )
400 {
401 *value = ( POLARSSL_CIPHER_AES_128_CCM );
402 return( 0 );
403 }
404#endif // POLARSSL_CIPHER_MODE_AEAD
405#ifdef POLARSSL_CIPHER_MODE_AEAD
406 if( strcmp( str, "POLARSSL_CIPHER_AES_192_CCM" ) == 0 )
407 {
408 *value = ( POLARSSL_CIPHER_AES_192_CCM );
409 return( 0 );
410 }
411#endif // POLARSSL_CIPHER_MODE_AEAD
412#ifdef POLARSSL_CIPHER_MODE_AEAD
413 if( strcmp( str, "POLARSSL_CIPHER_AES_256_CCM" ) == 0 )
414 {
415 *value = ( POLARSSL_CIPHER_AES_256_CCM );
416 return( 0 );
417 }
418#endif // POLARSSL_CIPHER_MODE_AEAD
419#ifdef POLARSSL_CIPHER_MODE_AEAD
420 if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CCM" ) == 0 )
421 {
423 return( 0 );
424 }
425#endif // POLARSSL_CIPHER_MODE_AEAD
426
427
428 printf( "Expected integer for parameter and got: %s\n", str );
429 return( -1 );
430}
431
432void test_suite_cipher_list( )
433{
434 const int *cipher_type;
435
436 for( cipher_type = cipher_list(); *cipher_type != 0; cipher_type++ )
437 TEST_ASSERT( cipher_info_from_type( *cipher_type ) != NULL );
438
439exit:
440 return;
441}
442
443void test_suite_cipher_null_args( )
444{
446 const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) );
447 unsigned char buf[1] = { 0 };
448 size_t olen;
449
450 cipher_init( &ctx );
451
452 TEST_ASSERT( cipher_get_block_size( NULL ) == 0 );
453 TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 );
454
457
458 TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 );
459 TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 );
460
461 TEST_ASSERT( cipher_info_from_string( NULL ) == NULL );
462
463 TEST_ASSERT( cipher_init_ctx( &ctx, NULL )
465 TEST_ASSERT( cipher_init_ctx( NULL, info )
467
472
473 TEST_ASSERT( cipher_set_iv( NULL, buf, 0 )
475 TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 )
477
480
481#if defined(POLARSSL_GCM_C)
482 TEST_ASSERT( cipher_update_ad( NULL, buf, 0 )
484 TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 )
486#endif
487
488 TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen )
490 TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen )
492
493 TEST_ASSERT( cipher_finish( NULL, buf, &olen )
495 TEST_ASSERT( cipher_finish( &ctx, buf, &olen )
497
498#if defined(POLARSSL_GCM_C)
499 TEST_ASSERT( cipher_write_tag( NULL, buf, olen )
501 TEST_ASSERT( cipher_write_tag( &ctx, buf, olen )
503
504 TEST_ASSERT( cipher_check_tag( NULL, buf, olen )
506 TEST_ASSERT( cipher_check_tag( &ctx, buf, olen )
508#endif
509
510exit:
511 return;
512}
513
514void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
515 int length_val, int pad_mode )
516{
517 size_t length = length_val, outlen, total_len, i;
518 unsigned char key[32];
519 unsigned char iv[16];
520 unsigned char ad[13];
521 unsigned char tag[16];
522 unsigned char inbuf[64];
523 unsigned char encbuf[64];
524 unsigned char decbuf[64];
525
526 const cipher_info_t *cipher_info;
527 cipher_context_t ctx_dec;
528 cipher_context_t ctx_enc;
529
530 /*
531 * Prepare contexts
532 */
533 cipher_init( &ctx_dec );
534 cipher_init( &ctx_enc );
535
536 memset( key, 0x2a, sizeof( key ) );
537
538 /* Check and get info structures */
539 cipher_info = cipher_info_from_type( cipher_id );
540 TEST_ASSERT( NULL != cipher_info );
541 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
542
543 /* Initialise enc and dec contexts */
544 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
545 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
546
547 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
548 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
549
550#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
551 if( -1 != pad_mode )
552 {
553 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
554 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
555 }
556#else
557 (void) pad_mode;
558#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
559
560 /*
561 * Do a few encode/decode cycles
562 */
563 for( i = 0; i < 3; i++ )
564 {
565 memset( iv , 0x00 + i, sizeof( iv ) );
566 memset( ad, 0x10 + i, sizeof( ad ) );
567 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
568
569 memset( encbuf, 0, sizeof( encbuf ) );
570 memset( decbuf, 0, sizeof( decbuf ) );
571 memset( tag, 0, sizeof( tag ) );
572
573 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
574 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
575
576 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
577 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
578
579#if defined(POLARSSL_GCM_C)
580 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
581 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
582#endif
583
584 /* encode length number of bytes from inbuf */
585 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
586 total_len = outlen;
587
588 TEST_ASSERT( total_len == length ||
589 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
590 total_len < length &&
591 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
592
593 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
594 total_len += outlen;
595
596#if defined(POLARSSL_GCM_C)
597 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
598#endif
599
600 TEST_ASSERT( total_len == length ||
601 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
602 total_len > length &&
603 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
604
605 /* decode the previously encoded string */
606 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
607 total_len = outlen;
608
609 TEST_ASSERT( total_len == length ||
610 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
611 total_len < length &&
612 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
613
614 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
615 total_len += outlen;
616
617#if defined(POLARSSL_GCM_C)
618 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
619#endif
620
621 /* check result */
622 TEST_ASSERT( total_len == length );
623 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
624 }
625
626 /*
627 * Done
628 */
629exit:
630 cipher_free( &ctx_dec );
631 cipher_free( &ctx_enc );
632}
633
634void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
635 int length_val, int ret )
636{
637 size_t length = length_val;
638 unsigned char key[32];
639 unsigned char iv[16];
640
641 const cipher_info_t *cipher_info;
643
644 unsigned char inbuf[64];
645 unsigned char encbuf[64];
646
647 size_t outlen = 0;
648
649 memset( key, 0, 32 );
650 memset( iv , 0, 16 );
651
652 cipher_init( &ctx );
653
654 memset( inbuf, 5, 64 );
655 memset( encbuf, 0, 64 );
656
657 /* Check and get info structures */
658 cipher_info = cipher_info_from_type( cipher_id );
659 TEST_ASSERT( NULL != cipher_info );
660
661 /* Initialise context */
662 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
663 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
664#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
665 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
666#else
667 (void) pad_mode;
668#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
669 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
670 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
671#if defined(POLARSSL_GCM_C)
672 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
673#endif
674
675 /* encode length number of bytes from inbuf */
676 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
677 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
678
679 /* done */
680exit:
681 cipher_free( &ctx );
682}
683
684void test_suite_dec_empty_buf()
685{
686 unsigned char key[32];
687 unsigned char iv[16];
688
689 cipher_context_t ctx_dec;
690 const cipher_info_t *cipher_info;
691
692 unsigned char encbuf[64];
693 unsigned char decbuf[64];
694
695 size_t outlen = 0;
696
697 memset( key, 0, 32 );
698 memset( iv , 0, 16 );
699
700 cipher_init( &ctx_dec );
701
702 memset( encbuf, 0, 64 );
703 memset( decbuf, 0, 64 );
704
705 /* Initialise context */
707 TEST_ASSERT( NULL != cipher_info);
708
709 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
710
711 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
712
713 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
714
715 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
716
717#if defined(POLARSSL_GCM_C)
718 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
719#endif
720
721 /* decode 0-byte string */
722 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
723 TEST_ASSERT( 0 == outlen );
725 &ctx_dec, decbuf + outlen, &outlen ) );
726 TEST_ASSERT( 0 == outlen );
727
728exit:
729 cipher_free( &ctx_dec );
730}
731
732void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
733 int second_length_val )
734{
735 size_t first_length = first_length_val;
736 size_t second_length = second_length_val;
737 size_t length = first_length + second_length;
738 unsigned char key[32];
739 unsigned char iv[16];
740
741 cipher_context_t ctx_dec;
742 cipher_context_t ctx_enc;
743 const cipher_info_t *cipher_info;
744
745 unsigned char inbuf[64];
746 unsigned char encbuf[64];
747 unsigned char decbuf[64];
748
749 size_t outlen = 0;
750 size_t totaloutlen = 0;
751
752 memset( key, 0, 32 );
753 memset( iv , 0, 16 );
754
755 cipher_init( &ctx_dec );
756 cipher_init( &ctx_enc );
757
758 memset( inbuf, 5, 64 );
759 memset( encbuf, 0, 64 );
760 memset( decbuf, 0, 64 );
761
762 /* Initialise enc and dec contexts */
763 cipher_info = cipher_info_from_type( cipher_id );
764 TEST_ASSERT( NULL != cipher_info);
765
766 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
767 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
768
769 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
770 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
771
772 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
773 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
774
775 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
776 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
777
778#if defined(POLARSSL_GCM_C)
779 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
780 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
781#endif
782
783 /* encode length number of bytes from inbuf */
784 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
785 totaloutlen = outlen;
786 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
787 totaloutlen += outlen;
788 TEST_ASSERT( totaloutlen == length ||
789 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
790 totaloutlen < length &&
791 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
792
793 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
794 totaloutlen += outlen;
795 TEST_ASSERT( totaloutlen == length ||
796 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
797 totaloutlen > length &&
798 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
799
800 /* decode the previously encoded string */
801 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
802 totaloutlen = outlen;
803
804 TEST_ASSERT( totaloutlen == length ||
805 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
806 totaloutlen < length &&
807 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
808
809 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
810 totaloutlen += outlen;
811
812 TEST_ASSERT( totaloutlen == length );
813
814 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
815
816exit:
817 cipher_free( &ctx_dec );
818 cipher_free( &ctx_enc );
819}
820
821void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
822 char *hex_key, char *hex_iv,
823 char *hex_cipher, char *hex_clear,
824 char *hex_ad, char *hex_tag,
825 int finish_result, int tag_result )
826{
827 unsigned char key[50];
828 unsigned char iv[50];
829 unsigned char cipher[200];
830 unsigned char clear[200];
831 unsigned char ad[200];
832 unsigned char tag[20];
833 size_t key_len, iv_len, cipher_len, clear_len;
834#if defined(POLARSSL_GCM_C)
835 size_t ad_len, tag_len;
836#endif
838 unsigned char output[200];
839 size_t outlen, total_len;
840
841 cipher_init( &ctx );
842
843 memset( key, 0x00, sizeof( key ) );
844 memset( iv, 0x00, sizeof( iv ) );
845 memset( cipher, 0x00, sizeof( cipher ) );
846 memset( clear, 0x00, sizeof( clear ) );
847 memset( ad, 0x00, sizeof( ad ) );
848 memset( tag, 0x00, sizeof( tag ) );
849 memset( output, 0x00, sizeof( output ) );
850
851 key_len = unhexify( key, hex_key );
852 iv_len = unhexify( iv, hex_iv );
853 cipher_len = unhexify( cipher, hex_cipher );
854 clear_len = unhexify( clear, hex_clear );
855#if defined(POLARSSL_GCM_C)
856 ad_len = unhexify( ad, hex_ad );
857 tag_len = unhexify( tag, hex_tag );
858#else
859 ((void) hex_ad);
860 ((void) hex_tag);
861#endif
862
863 /* Prepare context */
864 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
865 cipher_info_from_type( cipher_id ) ) );
866 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
867#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
868 if( pad_mode != -1 )
869 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
870#else
871 (void) pad_mode;
872#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
873 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
874 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
875#if defined(POLARSSL_GCM_C)
876 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
877#endif
878
879 /* decode buffer and check tag */
880 total_len = 0;
881 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
882 total_len += outlen;
883 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
884 &outlen ) );
885 total_len += outlen;
886#if defined(POLARSSL_GCM_C)
887 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
888#endif
889
890 /* check plaintext only if everything went fine */
891 if( 0 == finish_result && 0 == tag_result )
892 {
893 TEST_ASSERT( total_len == clear_len );
894 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
895 }
896
897exit:
898 cipher_free( &ctx );
899}
900
901#ifdef POLARSSL_CIPHER_MODE_AEAD
902void test_suite_auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
903 char *hex_ad, char *hex_cipher,
904 char *hex_tag, char *hex_clear )
905{
906 int ret;
907 unsigned char key[50];
908 unsigned char iv[50];
909 unsigned char cipher[200];
910 unsigned char clear[200];
911 unsigned char ad[200];
912 unsigned char tag[20];
913 unsigned char my_tag[20];
914 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
916 unsigned char output[200];
917 size_t outlen;
918
919 cipher_init( &ctx );
920
921 memset( key, 0x00, sizeof( key ) );
922 memset( iv, 0x00, sizeof( iv ) );
923 memset( cipher, 0x00, sizeof( cipher ) );
924 memset( clear, 0x00, sizeof( clear ) );
925 memset( ad, 0x00, sizeof( ad ) );
926 memset( tag, 0x00, sizeof( tag ) );
927 memset( my_tag, 0xFF, sizeof( my_tag ) );
928 memset( output, 0xFF, sizeof( output ) );
929
930 key_len = unhexify( key, hex_key );
931 iv_len = unhexify( iv, hex_iv );
932 cipher_len = unhexify( cipher, hex_cipher );
933 ad_len = unhexify( ad, hex_ad );
934 tag_len = unhexify( tag, hex_tag );
935
936 /* Prepare context */
937 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
938 cipher_info_from_type( cipher_id ) ) );
939 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
940
941 /* decode buffer and check tag */
942 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
943 cipher, cipher_len, output, &outlen,
944 tag, tag_len );
945
946 /* make sure we didn't overwrite */
947 TEST_ASSERT( output[outlen + 0] == 0xFF );
948 TEST_ASSERT( output[outlen + 1] == 0xFF );
949
950 /* make sure the message is rejected if it should be */
951 if( strcmp( hex_clear, "FAIL" ) == 0 )
952 {
954 goto exit;
955 }
956
957 /* otherwise, make sure it was decrypted properly */
958 TEST_ASSERT( ret == 0 );
959
960 clear_len = unhexify( clear, hex_clear );
961 TEST_ASSERT( outlen == clear_len );
962 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
963
964 /* then encrypt the clear and make sure we get the same ciphertext and tag */
965 memset( output, 0xFF, sizeof( output ) );
966 outlen = 0;
967
968 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
969 clear, clear_len, output, &outlen,
970 my_tag, tag_len );
971 TEST_ASSERT( ret == 0 );
972
973 TEST_ASSERT( outlen == clear_len );
974 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
975 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
976
977 /* make sure we didn't overwrite */
978 TEST_ASSERT( output[outlen + 0] == 0xFF );
979 TEST_ASSERT( output[outlen + 1] == 0xFF );
980 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
981 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
982
983
984exit:
985 cipher_free( &ctx );
986}
987#endif /* POLARSSL_CIPHER_MODE_AEAD */
988
989void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
990 char *hex_input, char *hex_result,
991 int finish_result )
992{
993 unsigned char key[50];
994 unsigned char input[16];
995 unsigned char result[16];
996 size_t key_len;
998 unsigned char output[32];
999 size_t outlen;
1000
1001 cipher_init( &ctx );
1002
1003 memset( key, 0x00, sizeof( key ) );
1004 memset( input, 0x00, sizeof( input ) );
1005 memset( result, 0x00, sizeof( result ) );
1006 memset( output, 0x00, sizeof( output ) );
1007
1008 /* Prepare context */
1009 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
1010 cipher_info_from_type( cipher_id ) ) );
1011
1012 key_len = unhexify( key, hex_key );
1013 TEST_ASSERT( unhexify( input, hex_input ) ==
1014 (int) cipher_get_block_size( &ctx ) );
1015 TEST_ASSERT( unhexify( result, hex_result ) ==
1016 (int) cipher_get_block_size( &ctx ) );
1017
1018 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
1019
1020 TEST_ASSERT( 0 == cipher_update( &ctx, input,
1021 cipher_get_block_size( &ctx ),
1022 output, &outlen ) );
1023 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
1024 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
1025 &outlen ) );
1026 TEST_ASSERT( 0 == outlen );
1027
1028 /* check plaintext only if everything went fine */
1029 if( 0 == finish_result )
1030 TEST_ASSERT( 0 == memcmp( output, result,
1031 cipher_get_block_size( &ctx ) ) );
1032
1033exit:
1034 cipher_free( &ctx );
1035}
1036
1037#ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1038void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
1039{
1040 const cipher_info_t *cipher_info;
1041 cipher_context_t ctx;
1042
1043 cipher_init( &ctx );
1044
1045 cipher_info = cipher_info_from_type( cipher_id );
1046 TEST_ASSERT( NULL != cipher_info );
1047 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
1048
1049 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
1050
1051exit:
1052 cipher_free( &ctx );
1053}
1054#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1055
1056#ifdef POLARSSL_CIPHER_MODE_CBC
1057void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
1058{
1059 cipher_info_t cipher_info;
1060 cipher_context_t ctx;
1061 unsigned char input[16];
1062 size_t ilen, dlen;
1063
1064 /* build a fake context just for getting access to get_padding */
1065 cipher_init( &ctx );
1066 cipher_info.mode = POLARSSL_MODE_CBC;
1067 ctx.cipher_info = &cipher_info;
1068
1069 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
1070
1071 ilen = unhexify( input, input_str );
1072
1073 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
1074 if( 0 == ret )
1075 TEST_ASSERT( dlen == (size_t) dlen_check );
1076
1077exit:
1078 return;
1079}
1080#endif /* POLARSSL_CIPHER_MODE_CBC */
1081
1082#ifdef POLARSSL_SELF_TEST
1083void test_suite_cipher_selftest()
1084{
1085 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
1086
1087exit:
1088 return;
1089}
1090#endif /* POLARSSL_SELF_TEST */
1091
1092
1093#endif /* POLARSSL_CIPHER_C */
1094
1095
1096int dep_check( char *str )
1097{
1098 if( str == NULL )
1099 return( 1 );
1100
1101 if( strcmp( str, "POLARSSL_CCM_C" ) == 0 )
1102 {
1103#if defined(POLARSSL_CCM_C)
1104 return( 0 );
1105#else
1106 return( 1 );
1107#endif
1108 }
1109 if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
1110 {
1111#if defined(POLARSSL_CAMELLIA_C)
1112 return( 0 );
1113#else
1114 return( 1 );
1115#endif
1116 }
1117 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
1118 {
1119#if defined(POLARSSL_AES_C)
1120 return( 0 );
1121#else
1122 return( 1 );
1123#endif
1124 }
1125
1126
1127 return( 1 );
1128}
1129
1130int dispatch_test(int cnt, char *params[50])
1131{
1132 int ret;
1133 ((void) cnt);
1134 ((void) params);
1135
1136#if defined(TEST_SUITE_ACTIVE)
1137 if( strcmp( params[0], "cipher_list" ) == 0 )
1138 {
1139
1140
1141 if( cnt != 1 )
1142 {
1143 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1144 return( 2 );
1145 }
1146
1147
1148 test_suite_cipher_list( );
1149 return ( 0 );
1150
1151 return ( 3 );
1152 }
1153 else
1154 if( strcmp( params[0], "cipher_null_args" ) == 0 )
1155 {
1156
1157
1158 if( cnt != 1 )
1159 {
1160 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1161 return( 2 );
1162 }
1163
1164
1165 test_suite_cipher_null_args( );
1166 return ( 0 );
1167
1168 return ( 3 );
1169 }
1170 else
1171 if( strcmp( params[0], "enc_dec_buf" ) == 0 )
1172 {
1173
1174 int param1;
1175 char *param2 = params[2];
1176 int param3;
1177 int param4;
1178 int param5;
1179
1180 if( cnt != 6 )
1181 {
1182 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1183 return( 2 );
1184 }
1185
1186 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1187 if( verify_string( &param2 ) != 0 ) return( 2 );
1188 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1189 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1190 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1191
1192 test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
1193 return ( 0 );
1194
1195 return ( 3 );
1196 }
1197 else
1198 if( strcmp( params[0], "enc_fail" ) == 0 )
1199 {
1200
1201 int param1;
1202 int param2;
1203 int param3;
1204 int param4;
1205 int param5;
1206
1207 if( cnt != 6 )
1208 {
1209 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1210 return( 2 );
1211 }
1212
1213 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1214 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1215 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1216 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1217 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1218
1219 test_suite_enc_fail( param1, param2, param3, param4, param5 );
1220 return ( 0 );
1221
1222 return ( 3 );
1223 }
1224 else
1225 if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1226 {
1227
1228
1229 if( cnt != 1 )
1230 {
1231 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1232 return( 2 );
1233 }
1234
1235
1236 test_suite_dec_empty_buf( );
1237 return ( 0 );
1238
1239 return ( 3 );
1240 }
1241 else
1242 if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1243 {
1244
1245 int param1;
1246 int param2;
1247 int param3;
1248 int param4;
1249
1250 if( cnt != 5 )
1251 {
1252 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1253 return( 2 );
1254 }
1255
1256 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1257 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1258 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1259 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1260
1261 test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1262 return ( 0 );
1263
1264 return ( 3 );
1265 }
1266 else
1267 if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1268 {
1269
1270 int param1;
1271 int param2;
1272 char *param3 = params[3];
1273 char *param4 = params[4];
1274 char *param5 = params[5];
1275 char *param6 = params[6];
1276 char *param7 = params[7];
1277 char *param8 = params[8];
1278 int param9;
1279 int param10;
1280
1281 if( cnt != 11 )
1282 {
1283 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1284 return( 2 );
1285 }
1286
1287 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1288 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1289 if( verify_string( &param3 ) != 0 ) return( 2 );
1290 if( verify_string( &param4 ) != 0 ) return( 2 );
1291 if( verify_string( &param5 ) != 0 ) return( 2 );
1292 if( verify_string( &param6 ) != 0 ) return( 2 );
1293 if( verify_string( &param7 ) != 0 ) return( 2 );
1294 if( verify_string( &param8 ) != 0 ) return( 2 );
1295 if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1296 if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1297
1298 test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1299 return ( 0 );
1300
1301 return ( 3 );
1302 }
1303 else
1304 if( strcmp( params[0], "auth_crypt_tv" ) == 0 )
1305 {
1306 #ifdef POLARSSL_CIPHER_MODE_AEAD
1307
1308 int param1;
1309 char *param2 = params[2];
1310 char *param3 = params[3];
1311 char *param4 = params[4];
1312 char *param5 = params[5];
1313 char *param6 = params[6];
1314 char *param7 = params[7];
1315
1316 if( cnt != 8 )
1317 {
1318 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1319 return( 2 );
1320 }
1321
1322 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1323 if( verify_string( &param2 ) != 0 ) return( 2 );
1324 if( verify_string( &param3 ) != 0 ) return( 2 );
1325 if( verify_string( &param4 ) != 0 ) return( 2 );
1326 if( verify_string( &param5 ) != 0 ) return( 2 );
1327 if( verify_string( &param6 ) != 0 ) return( 2 );
1328 if( verify_string( &param7 ) != 0 ) return( 2 );
1329
1330 test_suite_auth_crypt_tv( param1, param2, param3, param4, param5, param6, param7 );
1331 return ( 0 );
1332 #endif /* POLARSSL_CIPHER_MODE_AEAD */
1333
1334 return ( 3 );
1335 }
1336 else
1337 if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1338 {
1339
1340 int param1;
1341 int param2;
1342 char *param3 = params[3];
1343 char *param4 = params[4];
1344 char *param5 = params[5];
1345 int param6;
1346
1347 if( cnt != 7 )
1348 {
1349 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1350 return( 2 );
1351 }
1352
1353 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1354 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1355 if( verify_string( &param3 ) != 0 ) return( 2 );
1356 if( verify_string( &param4 ) != 0 ) return( 2 );
1357 if( verify_string( &param5 ) != 0 ) return( 2 );
1358 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1359
1360 test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1361 return ( 0 );
1362
1363 return ( 3 );
1364 }
1365 else
1366 if( strcmp( params[0], "set_padding" ) == 0 )
1367 {
1368 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1369
1370 int param1;
1371 int param2;
1372 int param3;
1373
1374 if( cnt != 4 )
1375 {
1376 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1377 return( 2 );
1378 }
1379
1380 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1381 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1382 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1383
1384 test_suite_set_padding( param1, param2, param3 );
1385 return ( 0 );
1386 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1387
1388 return ( 3 );
1389 }
1390 else
1391 if( strcmp( params[0], "check_padding" ) == 0 )
1392 {
1393 #ifdef POLARSSL_CIPHER_MODE_CBC
1394
1395 int param1;
1396 char *param2 = params[2];
1397 int param3;
1398 int param4;
1399
1400 if( cnt != 5 )
1401 {
1402 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1403 return( 2 );
1404 }
1405
1406 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1407 if( verify_string( &param2 ) != 0 ) return( 2 );
1408 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1409 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1410
1411 test_suite_check_padding( param1, param2, param3, param4 );
1412 return ( 0 );
1413 #endif /* POLARSSL_CIPHER_MODE_CBC */
1414
1415 return ( 3 );
1416 }
1417 else
1418 if( strcmp( params[0], "cipher_selftest" ) == 0 )
1419 {
1420 #ifdef POLARSSL_SELF_TEST
1421
1422
1423 if( cnt != 1 )
1424 {
1425 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1426 return( 2 );
1427 }
1428
1429
1430 test_suite_cipher_selftest( );
1431 return ( 0 );
1432 #endif /* POLARSSL_SELF_TEST */
1433
1434 return ( 3 );
1435 }
1436 else
1437
1438 {
1439 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1440 fflush( stdout );
1441 return( 1 );
1442 }
1443#else
1444 return( 3 );
1445#endif
1446 return( ret );
1447}
1448
1449int get_line( FILE *f, char *buf, size_t len )
1450{
1451 char *ret;
1452
1453 ret = fgets( buf, len, f );
1454 if( ret == NULL )
1455 return( -1 );
1456
1457 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1458 buf[strlen(buf) - 1] = '\0';
1459 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1460 buf[strlen(buf) - 1] = '\0';
1461
1462 return( 0 );
1463}
1464
1465int parse_arguments( char *buf, size_t len, char *params[50] )
1466{
1467 int cnt = 0, i;
1468 char *cur = buf;
1469 char *p = buf, *q;
1470
1471 params[cnt++] = cur;
1472
1473 while( *p != '\0' && p < buf + len )
1474 {
1475 if( *p == '\\' )
1476 {
1477 p++;
1478 p++;
1479 continue;
1480 }
1481 if( *p == ':' )
1482 {
1483 if( p + 1 < buf + len )
1484 {
1485 cur = p + 1;
1486 params[cnt++] = cur;
1487 }
1488 *p = '\0';
1489 }
1490
1491 p++;
1492 }
1493
1494 // Replace newlines, question marks and colons in strings
1495 for( i = 0; i < cnt; i++ )
1496 {
1497 p = params[i];
1498 q = params[i];
1499
1500 while( *p != '\0' )
1501 {
1502 if( *p == '\\' && *(p + 1) == 'n' )
1503 {
1504 p += 2;
1505 *(q++) = '\n';
1506 }
1507 else if( *p == '\\' && *(p + 1) == ':' )
1508 {
1509 p += 2;
1510 *(q++) = ':';
1511 }
1512 else if( *p == '\\' && *(p + 1) == '?' )
1513 {
1514 p += 2;
1515 *(q++) = '?';
1516 }
1517 else
1518 *(q++) = *(p++);
1519 }
1520 *q = '\0';
1521 }
1522
1523 return( cnt );
1524}
1525
1526int main()
1527{
1528 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1529 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_cipher.ccm.data";
1530 FILE *file;
1531 char buf[5000];
1532 char *params[50];
1533
1534#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1535 unsigned char alloc_buf[1000000];
1536 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1537#endif
1538
1539 file = fopen( filename, "r" );
1540 if( file == NULL )
1541 {
1542 fprintf( stderr, "Failed to open\n" );
1543 return( 1 );
1544 }
1545
1546 while( !feof( file ) )
1547 {
1548 int skip = 0;
1549
1550 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1551 break;
1552 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1553 fprintf( stdout, " " );
1554 for( i = strlen( buf ) + 1; i < 67; i++ )
1555 fprintf( stdout, "." );
1556 fprintf( stdout, " " );
1557 fflush( stdout );
1558
1559 total_tests++;
1560
1561 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1562 break;
1563 cnt = parse_arguments( buf, strlen(buf), params );
1564
1565 if( strcmp( params[0], "depends_on" ) == 0 )
1566 {
1567 for( i = 1; i < cnt; i++ )
1568 if( dep_check( params[i] ) != 0 )
1569 skip = 1;
1570
1571 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1572 break;
1573 cnt = parse_arguments( buf, strlen(buf), params );
1574 }
1575
1576 if( skip == 0 )
1577 {
1578 test_errors = 0;
1579 ret = dispatch_test( cnt, params );
1580 }
1581
1582 if( skip == 1 || ret == 3 )
1583 {
1584 total_skipped++;
1585 fprintf( stdout, "----\n" );
1586 fflush( stdout );
1587 }
1588 else if( ret == 0 && test_errors == 0 )
1589 {
1590 fprintf( stdout, "PASS\n" );
1591 fflush( stdout );
1592 }
1593 else if( ret == 2 )
1594 {
1595 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1596 fclose(file);
1597 exit( 2 );
1598 }
1599 else
1600 total_errors++;
1601
1602 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1603 break;
1604 if( strlen(buf) != 0 )
1605 {
1606 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1607 return( 1 );
1608 }
1609 }
1610 fclose(file);
1611
1612 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1613 if( total_errors == 0 )
1614 fprintf( stdout, "PASSED" );
1615 else
1616 fprintf( stdout, "FAILED" );
1617
1618 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1619 total_tests - total_errors, total_tests, total_skipped );
1620
1621#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1622#if defined(POLARSSL_MEMORY_DEBUG)
1623 memory_buffer_alloc_status();
1624#endif
1626#endif
1627
1628 return( total_errors != 0 );
1629}
1630
1631
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_AES_256_CCM
Definition cipher.h:128
@ POLARSSL_CIPHER_CAMELLIA_128_CCM
Definition cipher.h:129
@ POLARSSL_CIPHER_AES_128_CBC
Definition cipher.h:88
@ POLARSSL_CIPHER_AES_128_CCM
Definition cipher.h:126
@ POLARSSL_CIPHER_AES_192_CCM
Definition cipher.h:127
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.