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