PolarSSL v1.3.9
test_suite_des.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_DES_C
8
9#include <polarssl/des.h>
10#endif /* POLARSSL_DES_C */
11
12
13#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14#include "polarssl/memory.h"
15#endif
16
17#if defined(POLARSSL_PLATFORM_C)
18#include "polarssl/platform.h"
19#else
20#define polarssl_malloc malloc
21#define polarssl_free free
22#endif
23
24#ifdef _MSC_VER
25#include <basetsd.h>
26typedef UINT32 uint32_t;
27#else
28#include <inttypes.h>
29#endif
30
31#include <assert.h>
32#include <stdlib.h>
33#include <string.h>
34
35/*
36 * 32-bit integer manipulation macros (big endian)
37 */
38#ifndef GET_UINT32_BE
39#define GET_UINT32_BE(n,b,i) \
40{ \
41 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
45}
46#endif
47
48#ifndef PUT_UINT32_BE
49#define PUT_UINT32_BE(n,b,i) \
50{ \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55}
56#endif
57
58static int unhexify(unsigned char *obuf, const char *ibuf)
59{
60 unsigned char c, c2;
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1)); // must be even number of bytes
63
64 while (*ibuf != 0)
65 {
66 c = *ibuf++;
67 if( c >= '0' && c <= '9' )
68 c -= '0';
69 else if( c >= 'a' && c <= 'f' )
70 c -= 'a' - 10;
71 else if( c >= 'A' && c <= 'F' )
72 c -= 'A' - 10;
73 else
74 assert( 0 );
75
76 c2 = *ibuf++;
77 if( c2 >= '0' && c2 <= '9' )
78 c2 -= '0';
79 else if( c2 >= 'a' && c2 <= 'f' )
80 c2 -= 'a' - 10;
81 else if( c2 >= 'A' && c2 <= 'F' )
82 c2 -= 'A' - 10;
83 else
84 assert( 0 );
85
86 *obuf++ = ( c << 4 ) | c2;
87 }
88
89 return len;
90}
91
92static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93{
94 unsigned char l, h;
95
96 while (len != 0)
97 {
98 h = (*ibuf) / 16;
99 l = (*ibuf) % 16;
100
101 if( h < 10 )
102 *obuf++ = '0' + h;
103 else
104 *obuf++ = 'a' + h - 10;
105
106 if( l < 10 )
107 *obuf++ = '0' + l;
108 else
109 *obuf++ = 'a' + l - 10;
110
111 ++ibuf;
112 len--;
113 }
114}
115
123static unsigned char *zero_alloc( size_t len )
124{
125 void *p;
126 size_t actual_len = len != 0 ? len : 1;
127
128 p = polarssl_malloc( actual_len );
129 assert( p != NULL );
130
131 memset( p, 0x00, actual_len );
132
133 return( p );
134}
135
146static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147{
148 unsigned char *obuf;
149
150 *olen = strlen(ibuf) / 2;
151
152 if( *olen == 0 )
153 return( zero_alloc( *olen ) );
154
155 obuf = polarssl_malloc( *olen );
156 assert( obuf != NULL );
157
158 (void) unhexify( obuf, ibuf );
159
160 return( obuf );
161}
162
172static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173{
174#if !defined(__OpenBSD__)
175 size_t i;
176
177 if( rng_state != NULL )
178 rng_state = NULL;
179
180 for( i = 0; i < len; ++i )
181 output[i] = rand();
182#else
183 if( rng_state != NULL )
184 rng_state = NULL;
185
186 arc4random_buf( output, len );
187#endif /* !OpenBSD */
188
189 return( 0 );
190}
191
197static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198{
199 if( rng_state != NULL )
200 rng_state = NULL;
201
202 memset( output, 0, len );
203
204 return( 0 );
205}
206
207typedef struct
208{
209 unsigned char *buf;
210 size_t length;
212
224static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225{
226 rnd_buf_info *info = (rnd_buf_info *) rng_state;
227 size_t use_len;
228
229 if( rng_state == NULL )
230 return( rnd_std_rand( NULL, output, len ) );
231
232 use_len = len;
233 if( len > info->length )
234 use_len = info->length;
235
236 if( use_len )
237 {
238 memcpy( output, info->buf, use_len );
239 info->buf += use_len;
240 info->length -= use_len;
241 }
242
243 if( len - use_len > 0 )
244 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245
246 return( 0 );
247}
248
256typedef struct
257{
258 uint32_t key[16];
259 uint32_t v0, v1;
261
270static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271{
272 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
275
276 if( rng_state == NULL )
277 return( rnd_std_rand( NULL, output, len ) );
278
279 k = info->key;
280
281 while( len > 0 )
282 {
283 size_t use_len = ( len > 4 ) ? 4 : len;
284 sum = 0;
285
286 for( i = 0; i < 32; i++ )
287 {
288 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289 sum += delta;
290 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291 }
292
293 PUT_UINT32_BE( info->v0, result, 0 );
294 memcpy( out, result, use_len );
295 len -= use_len;
296 out += 4;
297 }
298
299 return( 0 );
300}
301
302
303#include <stdio.h>
304#include <string.h>
305
306#if defined(POLARSSL_PLATFORM_C)
307#include "polarssl/platform.h"
308#else
309#define polarssl_printf printf
310#define polarssl_malloc malloc
311#define polarssl_free free
312#endif
313
314static int test_errors = 0;
315
316#ifdef POLARSSL_DES_C
317
318#define TEST_SUITE_ACTIVE
319
320static int test_assert( int correct, const char *test )
321{
322 if( correct )
323 return( 0 );
324
325 test_errors++;
326 if( test_errors == 1 )
327 printf( "FAILED\n" );
328 printf( " %s\n", test );
329
330 return( 1 );
331}
332
333#define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
336 } while (0)
337
338int verify_string( char **str )
339{
340 if( (*str)[0] != '"' ||
341 (*str)[strlen( *str ) - 1] != '"' )
342 {
343 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
344 return( -1 );
345 }
346
347 (*str)++;
348 (*str)[strlen( *str ) - 1] = '\0';
349
350 return( 0 );
351}
352
353int verify_int( char *str, int *value )
354{
355 size_t i;
356 int minus = 0;
357 int digits = 1;
358 int hex = 0;
359
360 for( i = 0; i < strlen( str ); i++ )
361 {
362 if( i == 0 && str[i] == '-' )
363 {
364 minus = 1;
365 continue;
366 }
367
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] == '0' && str[i] == 'x' )
370 {
371 hex = 1;
372 continue;
373 }
374
375 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
376 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
377 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
378 {
379 digits = 0;
380 break;
381 }
382 }
383
384 if( digits )
385 {
386 if( hex )
387 *value = strtol( str, NULL, 16 );
388 else
389 *value = strtol( str, NULL, 10 );
390
391 return( 0 );
392 }
393
394#ifdef POLARSSL_CIPHER_MODE_CBC
395 if( strcmp( str, "POLARSSL_ERR_DES_INVALID_INPUT_LENGTH" ) == 0 )
396 {
398 return( 0 );
399 }
400#endif // POLARSSL_CIPHER_MODE_CBC
401
402
403 printf( "Expected integer for parameter and got: %s\n", str );
404 return( -1 );
405}
406
407void test_suite_des_check_weak( char *key_hex, int ret )
408{
409 unsigned char key[DES_KEY_SIZE];
410
411 memset( key, 0, sizeof key );
412
413 unhexify( key, key_hex );
414
415 TEST_ASSERT( des_key_check_weak( key ) == ret );
416
417exit:
418 return;
419}
420
421void test_suite_des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
422 char *hex_dst_string )
423{
424 unsigned char key_str[100];
425 unsigned char src_str[100];
426 unsigned char dst_str[100];
427 unsigned char output[100];
428 des_context ctx;
429
430 memset(key_str, 0x00, 100);
431 memset(src_str, 0x00, 100);
432 memset(dst_str, 0x00, 100);
433 memset(output, 0x00, 100);
434 des_init( &ctx );
435
436 unhexify( key_str, hex_key_string );
437 unhexify( src_str, hex_src_string );
438
439 des_setkey_enc( &ctx, key_str );
440 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
441 hexify( dst_str, output, 8 );
442
443 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
444
445exit:
446 des_free( &ctx );
447}
448
449void test_suite_des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
450 char *hex_dst_string )
451{
452 unsigned char key_str[100];
453 unsigned char src_str[100];
454 unsigned char dst_str[100];
455 unsigned char output[100];
456 des_context ctx;
457
458 memset(key_str, 0x00, 100);
459 memset(src_str, 0x00, 100);
460 memset(dst_str, 0x00, 100);
461 memset(output, 0x00, 100);
462 des_init( &ctx );
463
464 unhexify( key_str, hex_key_string );
465 unhexify( src_str, hex_src_string );
466
467 des_setkey_dec( &ctx, key_str );
468 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
469 hexify( dst_str, output, 8 );
470
471 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
472
473exit:
474 des_free( &ctx );
475}
476
477#ifdef POLARSSL_CIPHER_MODE_CBC
478void test_suite_des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
479 char *hex_src_string, char *hex_dst_string, int cbc_result )
480{
481 unsigned char key_str[100];
482 unsigned char iv_str[100];
483 unsigned char src_str[100];
484 unsigned char dst_str[100];
485 unsigned char output[100];
486 des_context ctx;
487 int src_len;
488
489 memset(key_str, 0x00, 100);
490 memset(iv_str, 0x00, 100);
491 memset(src_str, 0x00, 100);
492 memset(dst_str, 0x00, 100);
493 memset(output, 0x00, 100);
494 des_init( &ctx );
495
496 unhexify( key_str, hex_key_string );
497 unhexify( iv_str, hex_iv_string );
498 src_len = unhexify( src_str, hex_src_string );
499
500 des_setkey_enc( &ctx, key_str );
501 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
502 if( cbc_result == 0 )
503 {
504 hexify( dst_str, output, src_len );
505
506 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
507 }
508
509exit:
510 des_free( &ctx );
511}
512#endif /* POLARSSL_CIPHER_MODE_CBC */
513
514#ifdef POLARSSL_CIPHER_MODE_CBC
515void test_suite_des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
516 char *hex_src_string, char *hex_dst_string, int cbc_result )
517{
518 unsigned char key_str[100];
519 unsigned char iv_str[100];
520 unsigned char src_str[100];
521 unsigned char dst_str[100];
522 unsigned char output[100];
523 des_context ctx;
524 int src_len;
525
526 memset(key_str, 0x00, 100);
527 memset(iv_str, 0x00, 100);
528 memset(src_str, 0x00, 100);
529 memset(dst_str, 0x00, 100);
530 memset(output, 0x00, 100);
531 des_init( &ctx );
532
533 unhexify( key_str, hex_key_string );
534 unhexify( iv_str, hex_iv_string );
535 src_len = unhexify( src_str, hex_src_string );
536
537 des_setkey_dec( &ctx, key_str );
538 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
539 if( cbc_result == 0 )
540 {
541 hexify( dst_str, output, src_len );
542
543 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
544 }
545
546exit:
547 des_free( &ctx );
548}
549#endif /* POLARSSL_CIPHER_MODE_CBC */
550
551void test_suite_des3_encrypt_ecb( int key_count, char *hex_key_string,
552 char *hex_src_string, char *hex_dst_string )
553{
554 unsigned char key_str[100];
555 unsigned char src_str[100];
556 unsigned char dst_str[100];
557 unsigned char output[100];
558 des3_context ctx;
559
560 memset(key_str, 0x00, 100);
561 memset(src_str, 0x00, 100);
562 memset(dst_str, 0x00, 100);
563 memset(output, 0x00, 100);
564 des3_init( &ctx );
565
566 unhexify( key_str, hex_key_string );
567 unhexify( src_str, hex_src_string );
568
569 if( key_count == 2 )
570 des3_set2key_enc( &ctx, key_str );
571 else if( key_count == 3 )
572 des3_set3key_enc( &ctx, key_str );
573 else
574 TEST_ASSERT( 0 );
575
576 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
577 hexify( dst_str, output, 8 );
578
579 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
580
581exit:
582 des3_free( &ctx );
583}
584
585void test_suite_des3_decrypt_ecb( int key_count, char *hex_key_string,
586 char *hex_src_string, char *hex_dst_string )
587{
588 unsigned char key_str[100];
589 unsigned char src_str[100];
590 unsigned char dst_str[100];
591 unsigned char output[100];
592 des3_context ctx;
593
594 memset(key_str, 0x00, 100);
595 memset(src_str, 0x00, 100);
596 memset(dst_str, 0x00, 100);
597 memset(output, 0x00, 100);
598 des3_init( &ctx );
599
600 unhexify( key_str, hex_key_string );
601 unhexify( src_str, hex_src_string );
602
603 if( key_count == 2 )
604 des3_set2key_dec( &ctx, key_str );
605 else if( key_count == 3 )
606 des3_set3key_dec( &ctx, key_str );
607 else
608 TEST_ASSERT( 0 );
609
610 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
611 hexify( dst_str, output, 8 );
612
613 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
614
615exit:
616 des3_free( &ctx );
617}
618
619#ifdef POLARSSL_CIPHER_MODE_CBC
620void test_suite_des3_encrypt_cbc( int key_count, char *hex_key_string,
621 char *hex_iv_string, char *hex_src_string,
622 char *hex_dst_string, int cbc_result )
623{
624 unsigned char key_str[100];
625 unsigned char iv_str[100];
626 unsigned char src_str[100];
627 unsigned char dst_str[100];
628 unsigned char output[100];
629 des3_context ctx;
630 int src_len;
631
632 memset(key_str, 0x00, 100);
633 memset(iv_str, 0x00, 100);
634 memset(src_str, 0x00, 100);
635 memset(dst_str, 0x00, 100);
636 memset(output, 0x00, 100);
637 des3_init( &ctx );
638
639 unhexify( key_str, hex_key_string );
640 unhexify( iv_str, hex_iv_string );
641 src_len = unhexify( src_str, hex_src_string );
642
643 if( key_count == 2 )
644 des3_set2key_enc( &ctx, key_str );
645 else if( key_count == 3 )
646 des3_set3key_enc( &ctx, key_str );
647 else
648 TEST_ASSERT( 0 );
649
650 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
651
652 if( cbc_result == 0 )
653 {
654 hexify( dst_str, output, src_len );
655
656 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
657 }
658
659exit:
660 des3_free( &ctx );
661}
662#endif /* POLARSSL_CIPHER_MODE_CBC */
663
664#ifdef POLARSSL_CIPHER_MODE_CBC
665void test_suite_des3_decrypt_cbc( int key_count, char *hex_key_string,
666 char *hex_iv_string, char *hex_src_string,
667 char *hex_dst_string, int cbc_result )
668{
669 unsigned char key_str[100];
670 unsigned char iv_str[100];
671 unsigned char src_str[100];
672 unsigned char dst_str[100];
673 unsigned char output[100];
674 des3_context ctx;
675 int src_len;
676
677 memset(key_str, 0x00, 100);
678 memset(iv_str, 0x00, 100);
679 memset(src_str, 0x00, 100);
680 memset(dst_str, 0x00, 100);
681 memset(output, 0x00, 100);
682 des3_init( &ctx );
683
684 unhexify( key_str, hex_key_string );
685 unhexify( iv_str, hex_iv_string );
686 src_len = unhexify( src_str, hex_src_string );
687
688 if( key_count == 2 )
689 des3_set2key_dec( &ctx, key_str );
690 else if( key_count == 3 )
691 des3_set3key_dec( &ctx, key_str );
692 else
693 TEST_ASSERT( 0 );
694
695 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
696
697 if( cbc_result == 0 )
698 {
699 hexify( dst_str, output, src_len );
700
701 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
702 }
703
704exit:
705 des3_free( &ctx );
706}
707#endif /* POLARSSL_CIPHER_MODE_CBC */
708
709void test_suite_des_key_parity_run()
710{
711 int i, j, cnt;
712 unsigned char key[DES_KEY_SIZE];
713 unsigned int parity;
714
715 memset( key, 0, DES_KEY_SIZE );
716 cnt = 0;
717
718 // Iterate through all possible byte values
719 //
720 for( i = 0; i < 32; i++ )
721 {
722 for( j = 0; j < 8; j++ )
723 key[j] = cnt++;
724
725 // Set the key parity according to the table
726 //
727 des_key_set_parity( key );
728
729 // Check the parity with a function
730 //
731 for( j = 0; j < 8; j++ )
732 {
733 parity = key[j] ^ ( key[j] >> 4 );
734 parity = parity ^
735 ( parity >> 1 ) ^
736 ( parity >> 2 ) ^
737 ( parity >> 3 );
738 parity &= 1;
739
740 if( parity != 1 )
741 TEST_ASSERT( 0 );
742 }
743
744 // Check the parity with the table
745 //
747 }
748
749exit:
750 return;
751}
752
753#ifdef POLARSSL_SELF_TEST
754void test_suite_des_selftest()
755{
756 TEST_ASSERT( des_self_test( 0 ) == 0 );
757
758exit:
759 return;
760}
761#endif /* POLARSSL_SELF_TEST */
762
763
764#endif /* POLARSSL_DES_C */
765
766
767int dep_check( char *str )
768{
769 if( str == NULL )
770 return( 1 );
771
772
773
774 return( 1 );
775}
776
777int dispatch_test(int cnt, char *params[50])
778{
779 int ret;
780 ((void) cnt);
781 ((void) params);
782
783#if defined(TEST_SUITE_ACTIVE)
784 if( strcmp( params[0], "des_check_weak" ) == 0 )
785 {
786
787 char *param1 = params[1];
788 int param2;
789
790 if( cnt != 3 )
791 {
792 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
793 return( 2 );
794 }
795
796 if( verify_string( &param1 ) != 0 ) return( 2 );
797 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
798
799 test_suite_des_check_weak( param1, param2 );
800 return ( 0 );
801
802 return ( 3 );
803 }
804 else
805 if( strcmp( params[0], "des_encrypt_ecb" ) == 0 )
806 {
807
808 char *param1 = params[1];
809 char *param2 = params[2];
810 char *param3 = params[3];
811
812 if( cnt != 4 )
813 {
814 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
815 return( 2 );
816 }
817
818 if( verify_string( &param1 ) != 0 ) return( 2 );
819 if( verify_string( &param2 ) != 0 ) return( 2 );
820 if( verify_string( &param3 ) != 0 ) return( 2 );
821
822 test_suite_des_encrypt_ecb( param1, param2, param3 );
823 return ( 0 );
824
825 return ( 3 );
826 }
827 else
828 if( strcmp( params[0], "des_decrypt_ecb" ) == 0 )
829 {
830
831 char *param1 = params[1];
832 char *param2 = params[2];
833 char *param3 = params[3];
834
835 if( cnt != 4 )
836 {
837 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
838 return( 2 );
839 }
840
841 if( verify_string( &param1 ) != 0 ) return( 2 );
842 if( verify_string( &param2 ) != 0 ) return( 2 );
843 if( verify_string( &param3 ) != 0 ) return( 2 );
844
845 test_suite_des_decrypt_ecb( param1, param2, param3 );
846 return ( 0 );
847
848 return ( 3 );
849 }
850 else
851 if( strcmp( params[0], "des_encrypt_cbc" ) == 0 )
852 {
853 #ifdef POLARSSL_CIPHER_MODE_CBC
854
855 char *param1 = params[1];
856 char *param2 = params[2];
857 char *param3 = params[3];
858 char *param4 = params[4];
859 int param5;
860
861 if( cnt != 6 )
862 {
863 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
864 return( 2 );
865 }
866
867 if( verify_string( &param1 ) != 0 ) return( 2 );
868 if( verify_string( &param2 ) != 0 ) return( 2 );
869 if( verify_string( &param3 ) != 0 ) return( 2 );
870 if( verify_string( &param4 ) != 0 ) return( 2 );
871 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
872
873 test_suite_des_encrypt_cbc( param1, param2, param3, param4, param5 );
874 return ( 0 );
875 #endif /* POLARSSL_CIPHER_MODE_CBC */
876
877 return ( 3 );
878 }
879 else
880 if( strcmp( params[0], "des_decrypt_cbc" ) == 0 )
881 {
882 #ifdef POLARSSL_CIPHER_MODE_CBC
883
884 char *param1 = params[1];
885 char *param2 = params[2];
886 char *param3 = params[3];
887 char *param4 = params[4];
888 int param5;
889
890 if( cnt != 6 )
891 {
892 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
893 return( 2 );
894 }
895
896 if( verify_string( &param1 ) != 0 ) return( 2 );
897 if( verify_string( &param2 ) != 0 ) return( 2 );
898 if( verify_string( &param3 ) != 0 ) return( 2 );
899 if( verify_string( &param4 ) != 0 ) return( 2 );
900 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
901
902 test_suite_des_decrypt_cbc( param1, param2, param3, param4, param5 );
903 return ( 0 );
904 #endif /* POLARSSL_CIPHER_MODE_CBC */
905
906 return ( 3 );
907 }
908 else
909 if( strcmp( params[0], "des3_encrypt_ecb" ) == 0 )
910 {
911
912 int param1;
913 char *param2 = params[2];
914 char *param3 = params[3];
915 char *param4 = params[4];
916
917 if( cnt != 5 )
918 {
919 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
920 return( 2 );
921 }
922
923 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
924 if( verify_string( &param2 ) != 0 ) return( 2 );
925 if( verify_string( &param3 ) != 0 ) return( 2 );
926 if( verify_string( &param4 ) != 0 ) return( 2 );
927
928 test_suite_des3_encrypt_ecb( param1, param2, param3, param4 );
929 return ( 0 );
930
931 return ( 3 );
932 }
933 else
934 if( strcmp( params[0], "des3_decrypt_ecb" ) == 0 )
935 {
936
937 int param1;
938 char *param2 = params[2];
939 char *param3 = params[3];
940 char *param4 = params[4];
941
942 if( cnt != 5 )
943 {
944 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
945 return( 2 );
946 }
947
948 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
949 if( verify_string( &param2 ) != 0 ) return( 2 );
950 if( verify_string( &param3 ) != 0 ) return( 2 );
951 if( verify_string( &param4 ) != 0 ) return( 2 );
952
953 test_suite_des3_decrypt_ecb( param1, param2, param3, param4 );
954 return ( 0 );
955
956 return ( 3 );
957 }
958 else
959 if( strcmp( params[0], "des3_encrypt_cbc" ) == 0 )
960 {
961 #ifdef POLARSSL_CIPHER_MODE_CBC
962
963 int param1;
964 char *param2 = params[2];
965 char *param3 = params[3];
966 char *param4 = params[4];
967 char *param5 = params[5];
968 int param6;
969
970 if( cnt != 7 )
971 {
972 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
973 return( 2 );
974 }
975
976 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
977 if( verify_string( &param2 ) != 0 ) return( 2 );
978 if( verify_string( &param3 ) != 0 ) return( 2 );
979 if( verify_string( &param4 ) != 0 ) return( 2 );
980 if( verify_string( &param5 ) != 0 ) return( 2 );
981 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
982
983 test_suite_des3_encrypt_cbc( param1, param2, param3, param4, param5, param6 );
984 return ( 0 );
985 #endif /* POLARSSL_CIPHER_MODE_CBC */
986
987 return ( 3 );
988 }
989 else
990 if( strcmp( params[0], "des3_decrypt_cbc" ) == 0 )
991 {
992 #ifdef POLARSSL_CIPHER_MODE_CBC
993
994 int param1;
995 char *param2 = params[2];
996 char *param3 = params[3];
997 char *param4 = params[4];
998 char *param5 = params[5];
999 int param6;
1000
1001 if( cnt != 7 )
1002 {
1003 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1004 return( 2 );
1005 }
1006
1007 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1008 if( verify_string( &param2 ) != 0 ) return( 2 );
1009 if( verify_string( &param3 ) != 0 ) return( 2 );
1010 if( verify_string( &param4 ) != 0 ) return( 2 );
1011 if( verify_string( &param5 ) != 0 ) return( 2 );
1012 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1013
1014 test_suite_des3_decrypt_cbc( param1, param2, param3, param4, param5, param6 );
1015 return ( 0 );
1016 #endif /* POLARSSL_CIPHER_MODE_CBC */
1017
1018 return ( 3 );
1019 }
1020 else
1021 if( strcmp( params[0], "des_key_parity_run" ) == 0 )
1022 {
1023
1024
1025 if( cnt != 1 )
1026 {
1027 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1028 return( 2 );
1029 }
1030
1031
1032 test_suite_des_key_parity_run( );
1033 return ( 0 );
1034
1035 return ( 3 );
1036 }
1037 else
1038 if( strcmp( params[0], "des_selftest" ) == 0 )
1039 {
1040 #ifdef POLARSSL_SELF_TEST
1041
1042
1043 if( cnt != 1 )
1044 {
1045 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1046 return( 2 );
1047 }
1048
1049
1050 test_suite_des_selftest( );
1051 return ( 0 );
1052 #endif /* POLARSSL_SELF_TEST */
1053
1054 return ( 3 );
1055 }
1056 else
1057
1058 {
1059 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1060 fflush( stdout );
1061 return( 1 );
1062 }
1063#else
1064 return( 3 );
1065#endif
1066 return( ret );
1067}
1068
1069int get_line( FILE *f, char *buf, size_t len )
1070{
1071 char *ret;
1072
1073 ret = fgets( buf, len, f );
1074 if( ret == NULL )
1075 return( -1 );
1076
1077 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1078 buf[strlen(buf) - 1] = '\0';
1079 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1080 buf[strlen(buf) - 1] = '\0';
1081
1082 return( 0 );
1083}
1084
1085int parse_arguments( char *buf, size_t len, char *params[50] )
1086{
1087 int cnt = 0, i;
1088 char *cur = buf;
1089 char *p = buf, *q;
1090
1091 params[cnt++] = cur;
1092
1093 while( *p != '\0' && p < buf + len )
1094 {
1095 if( *p == '\\' )
1096 {
1097 p++;
1098 p++;
1099 continue;
1100 }
1101 if( *p == ':' )
1102 {
1103 if( p + 1 < buf + len )
1104 {
1105 cur = p + 1;
1106 params[cnt++] = cur;
1107 }
1108 *p = '\0';
1109 }
1110
1111 p++;
1112 }
1113
1114 // Replace newlines, question marks and colons in strings
1115 for( i = 0; i < cnt; i++ )
1116 {
1117 p = params[i];
1118 q = params[i];
1119
1120 while( *p != '\0' )
1121 {
1122 if( *p == '\\' && *(p + 1) == 'n' )
1123 {
1124 p += 2;
1125 *(q++) = '\n';
1126 }
1127 else if( *p == '\\' && *(p + 1) == ':' )
1128 {
1129 p += 2;
1130 *(q++) = ':';
1131 }
1132 else if( *p == '\\' && *(p + 1) == '?' )
1133 {
1134 p += 2;
1135 *(q++) = '?';
1136 }
1137 else
1138 *(q++) = *(p++);
1139 }
1140 *q = '\0';
1141 }
1142
1143 return( cnt );
1144}
1145
1146int main()
1147{
1148 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1149 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_des.data";
1150 FILE *file;
1151 char buf[5000];
1152 char *params[50];
1153
1154#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1155 unsigned char alloc_buf[1000000];
1156 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1157#endif
1158
1159 file = fopen( filename, "r" );
1160 if( file == NULL )
1161 {
1162 fprintf( stderr, "Failed to open\n" );
1163 return( 1 );
1164 }
1165
1166 while( !feof( file ) )
1167 {
1168 int skip = 0;
1169
1170 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1171 break;
1172 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1173 fprintf( stdout, " " );
1174 for( i = strlen( buf ) + 1; i < 67; i++ )
1175 fprintf( stdout, "." );
1176 fprintf( stdout, " " );
1177 fflush( stdout );
1178
1179 total_tests++;
1180
1181 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1182 break;
1183 cnt = parse_arguments( buf, strlen(buf), params );
1184
1185 if( strcmp( params[0], "depends_on" ) == 0 )
1186 {
1187 for( i = 1; i < cnt; i++ )
1188 if( dep_check( params[i] ) != 0 )
1189 skip = 1;
1190
1191 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1192 break;
1193 cnt = parse_arguments( buf, strlen(buf), params );
1194 }
1195
1196 if( skip == 0 )
1197 {
1198 test_errors = 0;
1199 ret = dispatch_test( cnt, params );
1200 }
1201
1202 if( skip == 1 || ret == 3 )
1203 {
1204 total_skipped++;
1205 fprintf( stdout, "----\n" );
1206 fflush( stdout );
1207 }
1208 else if( ret == 0 && test_errors == 0 )
1209 {
1210 fprintf( stdout, "PASS\n" );
1211 fflush( stdout );
1212 }
1213 else if( ret == 2 )
1214 {
1215 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1216 fclose(file);
1217 exit( 2 );
1218 }
1219 else
1220 total_errors++;
1221
1222 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1223 break;
1224 if( strlen(buf) != 0 )
1225 {
1226 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1227 return( 1 );
1228 }
1229 }
1230 fclose(file);
1231
1232 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1233 if( total_errors == 0 )
1234 fprintf( stdout, "PASSED" );
1235 else
1236 fprintf( stdout, "FAILED" );
1237
1238 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1239 total_tests - total_errors, total_tests, total_skipped );
1240
1241#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1242#if defined(POLARSSL_MEMORY_DEBUG)
1243 memory_buffer_alloc_status();
1244#endif
1246#endif
1247
1248 return( total_errors != 0 );
1249}
1250
1251
Configuration options (set of defines)
DES block cipher.
int des_setkey_enc(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, encryption)
void des_init(des_context *ctx)
Initialize DES context.
int des_self_test(int verbose)
Checkup routine.
int des3_set2key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, encryption)
void des3_init(des3_context *ctx)
Initialize Triple-DES context.
int des3_set3key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, decryption)
int des_crypt_cbc(des_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
DES-CBC buffer encryption/decryption.
#define DES_DECRYPT
Definition des.h:46
#define DES_ENCRYPT
Definition des.h:45
#define DES_KEY_SIZE
Definition des.h:50
int des_crypt_ecb(des_context *ctx, const unsigned char input[8], unsigned char output[8])
DES-ECB block encryption/decryption.
int des3_set3key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, encryption)
void des3_free(des3_context *ctx)
Clear Triple-DES context.
int des3_set2key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, decryption)
int des_setkey_dec(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, decryption)
int des3_crypt_cbc(des3_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
3DES-CBC buffer encryption/decryption
#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
The data input has an invalid length.
Definition des.h:48
int des3_crypt_ecb(des3_context *ctx, const unsigned char input[8], unsigned char output[8])
3DES-ECB block encryption/decryption
int des_key_check_weak(const unsigned char key[DES_KEY_SIZE])
Check that key is not a weak or semi-weak DES key.
void des_free(des_context *ctx)
Clear DES context.
void des_key_set_parity(unsigned char key[DES_KEY_SIZE])
Set key parity on the given key to odd.
int des_key_check_key_parity(const unsigned char key[DES_KEY_SIZE])
Check that key parity on the given key is odd.
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.
Triple-DES context structure.
Definition des.h:74
DES context structure.
Definition des.h:64
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().
int main()
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.