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