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