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