PolarSSL v1.3.9
test_suite_pkcs1_v21.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_PKCS1_V21
8#ifdef POLARSSL_RSA_C
9#ifdef POLARSSL_SHA1_C
10
11#include <polarssl/rsa.h>
12#include <polarssl/md.h>
13#endif /* POLARSSL_PKCS1_V21 */
14#endif /* POLARSSL_RSA_C */
15#endif /* POLARSSL_SHA1_C */
16
17
18#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19#include "polarssl/memory.h"
20#endif
21
22#if defined(POLARSSL_PLATFORM_C)
23#include "polarssl/platform.h"
24#else
25#define polarssl_malloc malloc
26#define polarssl_free free
27#endif
28
29#ifdef _MSC_VER
30#include <basetsd.h>
31typedef UINT32 uint32_t;
32#else
33#include <inttypes.h>
34#endif
35
36#include <assert.h>
37#include <stdlib.h>
38#include <string.h>
39
40/*
41 * 32-bit integer manipulation macros (big endian)
42 */
43#ifndef GET_UINT32_BE
44#define GET_UINT32_BE(n,b,i) \
45{ \
46 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
47 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
48 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
49 | ( (uint32_t) (b)[(i) + 3] ); \
50}
51#endif
52
53#ifndef PUT_UINT32_BE
54#define PUT_UINT32_BE(n,b,i) \
55{ \
56 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
57 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
58 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
59 (b)[(i) + 3] = (unsigned char) ( (n) ); \
60}
61#endif
62
63static int unhexify(unsigned char *obuf, const char *ibuf)
64{
65 unsigned char c, c2;
66 int len = strlen(ibuf) / 2;
67 assert(!(strlen(ibuf) %1)); // must be even number of bytes
68
69 while (*ibuf != 0)
70 {
71 c = *ibuf++;
72 if( c >= '0' && c <= '9' )
73 c -= '0';
74 else if( c >= 'a' && c <= 'f' )
75 c -= 'a' - 10;
76 else if( c >= 'A' && c <= 'F' )
77 c -= 'A' - 10;
78 else
79 assert( 0 );
80
81 c2 = *ibuf++;
82 if( c2 >= '0' && c2 <= '9' )
83 c2 -= '0';
84 else if( c2 >= 'a' && c2 <= 'f' )
85 c2 -= 'a' - 10;
86 else if( c2 >= 'A' && c2 <= 'F' )
87 c2 -= 'A' - 10;
88 else
89 assert( 0 );
90
91 *obuf++ = ( c << 4 ) | c2;
92 }
93
94 return len;
95}
96
97static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
98{
99 unsigned char l, h;
100
101 while (len != 0)
102 {
103 h = (*ibuf) / 16;
104 l = (*ibuf) % 16;
105
106 if( h < 10 )
107 *obuf++ = '0' + h;
108 else
109 *obuf++ = 'a' + h - 10;
110
111 if( l < 10 )
112 *obuf++ = '0' + l;
113 else
114 *obuf++ = 'a' + l - 10;
115
116 ++ibuf;
117 len--;
118 }
119}
120
128static unsigned char *zero_alloc( size_t len )
129{
130 void *p;
131 size_t actual_len = len != 0 ? len : 1;
132
133 p = polarssl_malloc( actual_len );
134 assert( p != NULL );
135
136 memset( p, 0x00, actual_len );
137
138 return( p );
139}
140
151static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
152{
153 unsigned char *obuf;
154
155 *olen = strlen(ibuf) / 2;
156
157 if( *olen == 0 )
158 return( zero_alloc( *olen ) );
159
160 obuf = polarssl_malloc( *olen );
161 assert( obuf != NULL );
162
163 (void) unhexify( obuf, ibuf );
164
165 return( obuf );
166}
167
177static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
178{
179#if !defined(__OpenBSD__)
180 size_t i;
181
182 if( rng_state != NULL )
183 rng_state = NULL;
184
185 for( i = 0; i < len; ++i )
186 output[i] = rand();
187#else
188 if( rng_state != NULL )
189 rng_state = NULL;
190
191 arc4random_buf( output, len );
192#endif /* !OpenBSD */
193
194 return( 0 );
195}
196
202static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
203{
204 if( rng_state != NULL )
205 rng_state = NULL;
206
207 memset( output, 0, len );
208
209 return( 0 );
210}
211
212typedef struct
213{
214 unsigned char *buf;
215 size_t length;
217
229static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
230{
231 rnd_buf_info *info = (rnd_buf_info *) rng_state;
232 size_t use_len;
233
234 if( rng_state == NULL )
235 return( rnd_std_rand( NULL, output, len ) );
236
237 use_len = len;
238 if( len > info->length )
239 use_len = info->length;
240
241 if( use_len )
242 {
243 memcpy( output, info->buf, use_len );
244 info->buf += use_len;
245 info->length -= use_len;
246 }
247
248 if( len - use_len > 0 )
249 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
250
251 return( 0 );
252}
253
261typedef struct
262{
263 uint32_t key[16];
264 uint32_t v0, v1;
266
275static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
276{
277 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
278 uint32_t i, *k, sum, delta=0x9E3779B9;
279 unsigned char result[4], *out = output;
280
281 if( rng_state == NULL )
282 return( rnd_std_rand( NULL, output, len ) );
283
284 k = info->key;
285
286 while( len > 0 )
287 {
288 size_t use_len = ( len > 4 ) ? 4 : len;
289 sum = 0;
290
291 for( i = 0; i < 32; i++ )
292 {
293 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
294 sum += delta;
295 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
296 }
297
298 PUT_UINT32_BE( info->v0, result, 0 );
299 memcpy( out, result, use_len );
300 len -= use_len;
301 out += 4;
302 }
303
304 return( 0 );
305}
306
307
308#include <stdio.h>
309#include <string.h>
310
311#if defined(POLARSSL_PLATFORM_C)
312#include "polarssl/platform.h"
313#else
314#define polarssl_printf printf
315#define polarssl_malloc malloc
316#define polarssl_free free
317#endif
318
319static int test_errors = 0;
320
321#ifdef POLARSSL_PKCS1_V21
322#ifdef POLARSSL_RSA_C
323#ifdef POLARSSL_SHA1_C
324
325#define TEST_SUITE_ACTIVE
326
327static int test_assert( int correct, const char *test )
328{
329 if( correct )
330 return( 0 );
331
332 test_errors++;
333 if( test_errors == 1 )
334 printf( "FAILED\n" );
335 printf( " %s\n", test );
336
337 return( 1 );
338}
339
340#define TEST_ASSERT( TEST ) \
341 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
342 if( test_errors) goto exit; \
343 } while (0)
344
345int verify_string( char **str )
346{
347 if( (*str)[0] != '"' ||
348 (*str)[strlen( *str ) - 1] != '"' )
349 {
350 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
351 return( -1 );
352 }
353
354 (*str)++;
355 (*str)[strlen( *str ) - 1] = '\0';
356
357 return( 0 );
358}
359
360int verify_int( char *str, int *value )
361{
362 size_t i;
363 int minus = 0;
364 int digits = 1;
365 int hex = 0;
366
367 for( i = 0; i < strlen( str ); i++ )
368 {
369 if( i == 0 && str[i] == '-' )
370 {
371 minus = 1;
372 continue;
373 }
374
375 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
376 str[i - 1] == '0' && str[i] == 'x' )
377 {
378 hex = 1;
379 continue;
380 }
381
382 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
383 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
384 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
385 {
386 digits = 0;
387 break;
388 }
389 }
390
391 if( digits )
392 {
393 if( hex )
394 *value = strtol( str, NULL, 16 );
395 else
396 *value = strtol( str, NULL, 10 );
397
398 return( 0 );
399 }
400
401 if( strcmp( str, "RSA_SALT_LEN_ANY" ) == 0 )
402 {
403 *value = ( RSA_SALT_LEN_ANY );
404 return( 0 );
405 }
406 if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
407 {
408 *value = ( POLARSSL_MD_SHA256 );
409 return( 0 );
410 }
411 if( strcmp( str, "POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
412 {
414 return( 0 );
415 }
416 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
417 {
418 *value = ( POLARSSL_MD_SHA1 );
419 return( 0 );
420 }
421 if( strcmp( str, "POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
422 {
424 return( 0 );
425 }
426 if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
427 {
428 *value = ( POLARSSL_MD_SHA512 );
429 return( 0 );
430 }
431 if( strcmp( str, "POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
432 {
434 return( 0 );
435 }
436 if( strcmp( str, "POLARSSL_MD_NONE" ) == 0 )
437 {
438 *value = ( POLARSSL_MD_NONE );
439 return( 0 );
440 }
441
442
443 printf( "Expected integer for parameter and got: %s\n", str );
444 return( -1 );
445}
446
447void test_suite_pkcs1_rsaes_oaep_encrypt( int mod, int radix_N, char *input_N, int radix_E,
448 char *input_E, int hash,
449 char *message_hex_string, char *seed,
450 char *result_hex_str, int result )
451{
452 unsigned char message_str[1000];
453 unsigned char output[1000];
454 unsigned char output_str[1000];
455 unsigned char rnd_buf[1000];
456 rsa_context ctx;
457 size_t msg_len;
458 rnd_buf_info info;
459
460 info.length = unhexify( rnd_buf, seed );
461 info.buf = rnd_buf;
462
463 rsa_init( &ctx, RSA_PKCS_V21, hash );
464 memset( message_str, 0x00, 1000 );
465 memset( output, 0x00, 1000 );
466 memset( output_str, 0x00, 1000 );
467
468 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
469 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
470 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
471
472 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
473
474 msg_len = unhexify( message_str, message_hex_string );
475
476 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, RSA_PUBLIC, msg_len, message_str, output ) == result );
477 if( result == 0 )
478 {
479 hexify( output_str, output, ctx.len );
480
481 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
482 }
483
484exit:
485 rsa_free( &ctx );
486}
487
488void test_suite_pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char *input_P,
489 int radix_Q, char *input_Q, int radix_N,
490 char *input_N, int radix_E, char *input_E,
491 int hash, char *result_hex_str, char *seed,
492 char *message_hex_string, int result )
493{
494 unsigned char message_str[1000];
495 unsigned char output[1000];
496 unsigned char output_str[1000];
497 rsa_context ctx;
498 mpi P1, Q1, H, G;
499 size_t output_len;
500 rnd_pseudo_info rnd_info;
501 ((void) seed);
502
503 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
504 rsa_init( &ctx, RSA_PKCS_V21, hash );
505
506 memset( message_str, 0x00, 1000 );
507 memset( output, 0x00, 1000 );
508 memset( output_str, 0x00, 1000 );
509 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
510
511 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
512 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
513 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
514 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
515 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
516
517 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
518 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
519 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
520 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
521 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
522 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
523 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
524 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
525
526 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
527
528 unhexify( message_str, message_hex_string );
529
530 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == result );
531 if( result == 0 )
532 {
533 hexify( output_str, output, ctx.len );
534
535 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
536 }
537
538exit:
539 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
540 rsa_free( &ctx );
541}
542
543void test_suite_pkcs1_rsassa_pss_sign( int mod, int radix_P, char *input_P, int radix_Q,
544 char *input_Q, int radix_N, char *input_N,
545 int radix_E, char *input_E, int digest, int hash,
546 char *message_hex_string, char *salt,
547 char *result_hex_str, int result )
548{
549 unsigned char message_str[1000];
550 unsigned char hash_result[1000];
551 unsigned char output[1000];
552 unsigned char output_str[1000];
553 unsigned char rnd_buf[1000];
554 rsa_context ctx;
555 mpi P1, Q1, H, G;
556 size_t msg_len;
557 rnd_buf_info info;
558
559 info.length = unhexify( rnd_buf, salt );
560 info.buf = rnd_buf;
561
562 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
563 rsa_init( &ctx, RSA_PKCS_V21, hash );
564
565 memset( message_str, 0x00, 1000 );
566 memset( hash_result, 0x00, 1000 );
567 memset( output, 0x00, 1000 );
568 memset( output_str, 0x00, 1000 );
569
570 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
571 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
572 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
573 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
574 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
575
576 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
577 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
578 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
579 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
580 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
581 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
582 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
583 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
584
585 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
586
587 msg_len = unhexify( message_str, message_hex_string );
588
589 if( md_info_from_type( digest ) != NULL )
590 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
591
592 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
593 if( result == 0 )
594 {
595 hexify( output_str, output, ctx.len);
596
597 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
598 }
599
600exit:
601 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
602 rsa_free( &ctx );
603}
604
605void test_suite_pkcs1_rsassa_pss_verify( int mod, int radix_N, char *input_N, int radix_E,
606 char *input_E, int digest, int hash,
607 char *message_hex_string, char *salt,
608 char *result_hex_str, int result )
609{
610 unsigned char message_str[1000];
611 unsigned char hash_result[1000];
612 unsigned char result_str[1000];
613 rsa_context ctx;
614 size_t msg_len;
615 ((void) salt);
616
617 rsa_init( &ctx, RSA_PKCS_V21, hash );
618 memset( message_str, 0x00, 1000 );
619 memset( hash_result, 0x00, 1000 );
620 memset( result_str, 0x00, 1000 );
621
622 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
623 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
624 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
625
626 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
627
628 msg_len = unhexify( message_str, message_hex_string );
629 unhexify( result_str, result_hex_str );
630
631 if( md_info_from_type( digest ) != NULL )
632 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
633
634 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
635
636exit:
637 rsa_free( &ctx );
638}
639
640void test_suite_pkcs1_rsassa_pss_verify_ext( int mod,
641 int radix_N, char *input_N,
642 int radix_E, char *input_E,
643 int msg_digest_id, int ctx_hash,
644 int mgf_hash, int salt_len,
645 char *message_hex_string,
646 char *result_hex_str,
647 int result_simple,
648 int result_full )
649{
650 unsigned char message_str[1000];
651 unsigned char hash_result[1000];
652 unsigned char result_str[1000];
653 rsa_context ctx;
654 size_t msg_len, hash_len;
655
656 rsa_init( &ctx, RSA_PKCS_V21, ctx_hash );
657 memset( message_str, 0x00, 1000 );
658 memset( hash_result, 0x00, 1000 );
659 memset( result_str, 0x00, 1000 );
660
661 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
662 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
663 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
664
665 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
666
667 msg_len = unhexify( message_str, message_hex_string );
668 unhexify( result_str, result_hex_str );
669
670 if( msg_digest_id != POLARSSL_MD_NONE )
671 {
672 TEST_ASSERT( md( md_info_from_type( msg_digest_id ),
673 message_str, msg_len, hash_result ) == 0 );
674 hash_len = 0;
675 }
676 else
677 {
678 memcpy( hash_result, message_str, msg_len );
679 hash_len = msg_len;
680 }
681
682 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC,
683 msg_digest_id, hash_len, hash_result,
684 result_str ) == result_simple );
685
687 msg_digest_id, hash_len, hash_result,
688 mgf_hash, salt_len,
689 result_str ) == result_full );
690
691exit:
692 rsa_free( &ctx );
693}
694
695
696#endif /* POLARSSL_PKCS1_V21 */
697#endif /* POLARSSL_RSA_C */
698#endif /* POLARSSL_SHA1_C */
699
700
701int dep_check( char *str )
702{
703 if( str == NULL )
704 return( 1 );
705
706 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
707 {
708#if defined(POLARSSL_SHA256_C)
709 return( 0 );
710#else
711 return( 1 );
712#endif
713 }
714
715
716 return( 1 );
717}
718
719int dispatch_test(int cnt, char *params[50])
720{
721 int ret;
722 ((void) cnt);
723 ((void) params);
724
725#if defined(TEST_SUITE_ACTIVE)
726 if( strcmp( params[0], "pkcs1_rsaes_oaep_encrypt" ) == 0 )
727 {
728
729 int param1;
730 int param2;
731 char *param3 = params[3];
732 int param4;
733 char *param5 = params[5];
734 int param6;
735 char *param7 = params[7];
736 char *param8 = params[8];
737 char *param9 = params[9];
738 int param10;
739
740 if( cnt != 11 )
741 {
742 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
743 return( 2 );
744 }
745
746 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
747 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
748 if( verify_string( &param3 ) != 0 ) return( 2 );
749 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
750 if( verify_string( &param5 ) != 0 ) return( 2 );
751 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
752 if( verify_string( &param7 ) != 0 ) return( 2 );
753 if( verify_string( &param8 ) != 0 ) return( 2 );
754 if( verify_string( &param9 ) != 0 ) return( 2 );
755 if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
756
757 test_suite_pkcs1_rsaes_oaep_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
758 return ( 0 );
759
760 return ( 3 );
761 }
762 else
763 if( strcmp( params[0], "pkcs1_rsaes_oaep_decrypt" ) == 0 )
764 {
765
766 int param1;
767 int param2;
768 char *param3 = params[3];
769 int param4;
770 char *param5 = params[5];
771 int param6;
772 char *param7 = params[7];
773 int param8;
774 char *param9 = params[9];
775 int param10;
776 char *param11 = params[11];
777 char *param12 = params[12];
778 char *param13 = params[13];
779 int param14;
780
781 if( cnt != 15 )
782 {
783 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 15 );
784 return( 2 );
785 }
786
787 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
788 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
789 if( verify_string( &param3 ) != 0 ) return( 2 );
790 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
791 if( verify_string( &param5 ) != 0 ) return( 2 );
792 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
793 if( verify_string( &param7 ) != 0 ) return( 2 );
794 if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
795 if( verify_string( &param9 ) != 0 ) return( 2 );
796 if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
797 if( verify_string( &param11 ) != 0 ) return( 2 );
798 if( verify_string( &param12 ) != 0 ) return( 2 );
799 if( verify_string( &param13 ) != 0 ) return( 2 );
800 if( verify_int( params[14], &param14 ) != 0 ) return( 2 );
801
802 test_suite_pkcs1_rsaes_oaep_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
803 return ( 0 );
804
805 return ( 3 );
806 }
807 else
808 if( strcmp( params[0], "pkcs1_rsassa_pss_sign" ) == 0 )
809 {
810
811 int param1;
812 int param2;
813 char *param3 = params[3];
814 int param4;
815 char *param5 = params[5];
816 int param6;
817 char *param7 = params[7];
818 int param8;
819 char *param9 = params[9];
820 int param10;
821 int param11;
822 char *param12 = params[12];
823 char *param13 = params[13];
824 char *param14 = params[14];
825 int param15;
826
827 if( cnt != 16 )
828 {
829 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 16 );
830 return( 2 );
831 }
832
833 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
834 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
835 if( verify_string( &param3 ) != 0 ) return( 2 );
836 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
837 if( verify_string( &param5 ) != 0 ) return( 2 );
838 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
839 if( verify_string( &param7 ) != 0 ) return( 2 );
840 if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
841 if( verify_string( &param9 ) != 0 ) return( 2 );
842 if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
843 if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
844 if( verify_string( &param12 ) != 0 ) return( 2 );
845 if( verify_string( &param13 ) != 0 ) return( 2 );
846 if( verify_string( &param14 ) != 0 ) return( 2 );
847 if( verify_int( params[15], &param15 ) != 0 ) return( 2 );
848
849 test_suite_pkcs1_rsassa_pss_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15 );
850 return ( 0 );
851
852 return ( 3 );
853 }
854 else
855 if( strcmp( params[0], "pkcs1_rsassa_pss_verify" ) == 0 )
856 {
857
858 int param1;
859 int param2;
860 char *param3 = params[3];
861 int param4;
862 char *param5 = params[5];
863 int param6;
864 int param7;
865 char *param8 = params[8];
866 char *param9 = params[9];
867 char *param10 = params[10];
868 int param11;
869
870 if( cnt != 12 )
871 {
872 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
873 return( 2 );
874 }
875
876 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
877 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
878 if( verify_string( &param3 ) != 0 ) return( 2 );
879 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
880 if( verify_string( &param5 ) != 0 ) return( 2 );
881 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
882 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
883 if( verify_string( &param8 ) != 0 ) return( 2 );
884 if( verify_string( &param9 ) != 0 ) return( 2 );
885 if( verify_string( &param10 ) != 0 ) return( 2 );
886 if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
887
888 test_suite_pkcs1_rsassa_pss_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
889 return ( 0 );
890
891 return ( 3 );
892 }
893 else
894 if( strcmp( params[0], "pkcs1_rsassa_pss_verify_ext" ) == 0 )
895 {
896
897 int param1;
898 int param2;
899 char *param3 = params[3];
900 int param4;
901 char *param5 = params[5];
902 int param6;
903 int param7;
904 int param8;
905 int param9;
906 char *param10 = params[10];
907 char *param11 = params[11];
908 int param12;
909 int param13;
910
911 if( cnt != 14 )
912 {
913 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 14 );
914 return( 2 );
915 }
916
917 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
918 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
919 if( verify_string( &param3 ) != 0 ) return( 2 );
920 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
921 if( verify_string( &param5 ) != 0 ) return( 2 );
922 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
923 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
924 if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
925 if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
926 if( verify_string( &param10 ) != 0 ) return( 2 );
927 if( verify_string( &param11 ) != 0 ) return( 2 );
928 if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
929 if( verify_int( params[13], &param13 ) != 0 ) return( 2 );
930
931 test_suite_pkcs1_rsassa_pss_verify_ext( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13 );
932 return ( 0 );
933
934 return ( 3 );
935 }
936 else
937
938 {
939 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
940 fflush( stdout );
941 return( 1 );
942 }
943#else
944 return( 3 );
945#endif
946 return( ret );
947}
948
949int get_line( FILE *f, char *buf, size_t len )
950{
951 char *ret;
952
953 ret = fgets( buf, len, f );
954 if( ret == NULL )
955 return( -1 );
956
957 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
958 buf[strlen(buf) - 1] = '\0';
959 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
960 buf[strlen(buf) - 1] = '\0';
961
962 return( 0 );
963}
964
965int parse_arguments( char *buf, size_t len, char *params[50] )
966{
967 int cnt = 0, i;
968 char *cur = buf;
969 char *p = buf, *q;
970
971 params[cnt++] = cur;
972
973 while( *p != '\0' && p < buf + len )
974 {
975 if( *p == '\\' )
976 {
977 p++;
978 p++;
979 continue;
980 }
981 if( *p == ':' )
982 {
983 if( p + 1 < buf + len )
984 {
985 cur = p + 1;
986 params[cnt++] = cur;
987 }
988 *p = '\0';
989 }
990
991 p++;
992 }
993
994 // Replace newlines, question marks and colons in strings
995 for( i = 0; i < cnt; i++ )
996 {
997 p = params[i];
998 q = params[i];
999
1000 while( *p != '\0' )
1001 {
1002 if( *p == '\\' && *(p + 1) == 'n' )
1003 {
1004 p += 2;
1005 *(q++) = '\n';
1006 }
1007 else if( *p == '\\' && *(p + 1) == ':' )
1008 {
1009 p += 2;
1010 *(q++) = ':';
1011 }
1012 else if( *p == '\\' && *(p + 1) == '?' )
1013 {
1014 p += 2;
1015 *(q++) = '?';
1016 }
1017 else
1018 *(q++) = *(p++);
1019 }
1020 *q = '\0';
1021 }
1022
1023 return( cnt );
1024}
1025
1026int main()
1027{
1028 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1029 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_pkcs1_v21.data";
1030 FILE *file;
1031 char buf[5000];
1032 char *params[50];
1033
1034#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1035 unsigned char alloc_buf[1000000];
1036 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1037#endif
1038
1039 file = fopen( filename, "r" );
1040 if( file == NULL )
1041 {
1042 fprintf( stderr, "Failed to open\n" );
1043 return( 1 );
1044 }
1045
1046 while( !feof( file ) )
1047 {
1048 int skip = 0;
1049
1050 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1051 break;
1052 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1053 fprintf( stdout, " " );
1054 for( i = strlen( buf ) + 1; i < 67; i++ )
1055 fprintf( stdout, "." );
1056 fprintf( stdout, " " );
1057 fflush( stdout );
1058
1059 total_tests++;
1060
1061 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1062 break;
1063 cnt = parse_arguments( buf, strlen(buf), params );
1064
1065 if( strcmp( params[0], "depends_on" ) == 0 )
1066 {
1067 for( i = 1; i < cnt; i++ )
1068 if( dep_check( params[i] ) != 0 )
1069 skip = 1;
1070
1071 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1072 break;
1073 cnt = parse_arguments( buf, strlen(buf), params );
1074 }
1075
1076 if( skip == 0 )
1077 {
1078 test_errors = 0;
1079 ret = dispatch_test( cnt, params );
1080 }
1081
1082 if( skip == 1 || ret == 3 )
1083 {
1084 total_skipped++;
1085 fprintf( stdout, "----\n" );
1086 fflush( stdout );
1087 }
1088 else if( ret == 0 && test_errors == 0 )
1089 {
1090 fprintf( stdout, "PASS\n" );
1091 fflush( stdout );
1092 }
1093 else if( ret == 2 )
1094 {
1095 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1096 fclose(file);
1097 exit( 2 );
1098 }
1099 else
1100 total_errors++;
1101
1102 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1103 break;
1104 if( strlen(buf) != 0 )
1105 {
1106 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1107 return( 1 );
1108 }
1109 }
1110 fclose(file);
1111
1112 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1113 if( total_errors == 0 )
1114 fprintf( stdout, "PASSED" );
1115 else
1116 fprintf( stdout, "FAILED" );
1117
1118 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1119 total_tests - total_errors, total_tests, total_skipped );
1120
1121#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1122#if defined(POLARSSL_MEMORY_DEBUG)
1123 memory_buffer_alloc_status();
1124#endif
1126#endif
1127
1128 return( total_errors != 0 );
1129}
1130
1131
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.
void mpi_free(mpi *X)
Unallocate one MPI.
Configuration options (set of defines)
Generic message digest wrapper.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
@ POLARSSL_MD_NONE
Definition md.h:52
@ POLARSSL_MD_SHA1
Definition md.h:56
@ POLARSSL_MD_SHA512
Definition md.h:60
@ POLARSSL_MD_SHA256
Definition md.h:58
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.
The RSA public-key cryptosystem.
int rsa_pkcs1_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
int rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
#define RSA_PUBLIC
Definition rsa.h:59
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
Definition rsa.h:47
int rsa_rsassa_pss_verify_ext(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, md_type_t mgf1_hash_id, int expected_salt_len, const unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) (This is the version with "full" options....
#define RSA_SALT_LEN_ANY
Definition rsa.h:68
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition rsa.h:52
int rsa_pkcs1_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
#define POLARSSL_ERR_RSA_BAD_INPUT_DATA
Bad input parameters to function.
Definition rsa.h:46
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
#define RSA_PKCS_V21
Definition rsa.h:63
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
#define RSA_PRIVATE
Definition rsa.h:60
MPI structure.
Definition bignum.h:183
unsigned char * buf
Info structure for the pseudo random function.
RSA context structure.
Definition rsa.h:84
mpi N
Definition rsa.h:88
mpi P
Definition rsa.h:92
mpi QP
Definition rsa.h:96
mpi DQ
Definition rsa.h:95
size_t len
Definition rsa.h:86
mpi Q
Definition rsa.h:93
mpi E
Definition rsa.h:89
mpi DP
Definition rsa.h:94
mpi D
Definition rsa.h:91
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.