PolarSSL v1.3.9
test_suite_mpi.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_BIGNUM_C
8
9#include <polarssl/bignum.h>
10#endif /* POLARSSL_BIGNUM_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_BIGNUM_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 if( strcmp( str, "+1" ) == 0 )
395 {
396 *value = ( +1 );
397 return( 0 );
398 }
399 if( strcmp( str, "POLARSSL_ERR_MPI_BUFFER_TOO_SMALL" ) == 0 )
400 {
402 return( 0 );
403 }
404 if( strcmp( str, "-3" ) == 0 )
405 {
406 *value = ( -3 );
407 return( 0 );
408 }
409 if( strcmp( str, "POLARSSL_ERR_MPI_NEGATIVE_VALUE" ) == 0 )
410 {
412 return( 0 );
413 }
414 if( strcmp( str, "-34" ) == 0 )
415 {
416 *value = ( -34 );
417 return( 0 );
418 }
419 if( strcmp( str, "POLARSSL_ERR_MPI_INVALID_CHARACTER" ) == 0 )
420 {
422 return( 0 );
423 }
424 if( strcmp( str, "POLARSSL_ERR_MPI_NOT_ACCEPTABLE" ) == 0 )
425 {
427 return( 0 );
428 }
429#ifdef POLARSSL_FS_IO
430 if( strcmp( str, "POLARSSL_ERR_MPI_FILE_IO_ERROR" ) == 0 )
431 {
433 return( 0 );
434 }
435#endif // POLARSSL_FS_IO
436 if( strcmp( str, "-13" ) == 0 )
437 {
438 *value = ( -13 );
439 return( 0 );
440 }
441 if( strcmp( str, "POLARSSL_ERR_MPI_DIVISION_BY_ZERO" ) == 0 )
442 {
444 return( 0 );
445 }
446 if( strcmp( str, "-1" ) == 0 )
447 {
448 *value = ( -1 );
449 return( 0 );
450 }
451 if( strcmp( str, "POLARSSL_ERR_MPI_BAD_INPUT_DATA" ) == 0 )
452 {
454 return( 0 );
455 }
456 if( strcmp( str, "-9871232" ) == 0 )
457 {
458 *value = ( -9871232 );
459 return( 0 );
460 }
461 if( strcmp( str, "-2" ) == 0 )
462 {
463 *value = ( -2 );
464 return( 0 );
465 }
466
467
468 printf( "Expected integer for parameter and got: %s\n", str );
469 return( -1 );
470}
471
472void test_suite_mpi_read_write_string( int radix_X, char *input_X, int radix_A,
473 char *input_A, int output_size, int result_read,
474 int result_write )
475{
476 mpi X;
477 char str[1000];
478 size_t len = output_size;
479
480 mpi_init( &X );
481
482 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == result_read );
483 if( result_read == 0 )
484 {
485 TEST_ASSERT( mpi_write_string( &X, radix_A, str, &len ) == result_write );
486 if( result_write == 0 )
487 {
488 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
489 }
490 }
491
492exit:
493 mpi_free( &X );
494}
495
496void test_suite_mpi_read_binary( char *input_X, int radix_A, char *input_A )
497{
498 mpi X;
499 unsigned char str[1000];
500 unsigned char buf[1000];
501 size_t len = 1000;
502 size_t input_len;
503
504 mpi_init( &X );
505
506 input_len = unhexify( buf, input_X );
507
508 TEST_ASSERT( mpi_read_binary( &X, buf, input_len ) == 0 );
509 TEST_ASSERT( mpi_write_string( &X, radix_A, (char *) str, &len ) == 0 );
510 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
511
512exit:
513 mpi_free( &X );
514}
515
516void test_suite_mpi_write_binary( int radix_X, char *input_X, char *input_A,
517 int output_size, int result )
518{
519 mpi X;
520 unsigned char str[1000];
521 unsigned char buf[1000];
522 size_t buflen;
523
524 memset( buf, 0x00, 1000 );
525 memset( str, 0x00, 1000 );
526
527 mpi_init( &X );
528
529 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
530
531 buflen = mpi_size( &X );
532 if( buflen > (size_t) output_size )
533 buflen = (size_t) output_size;
534
535 TEST_ASSERT( mpi_write_binary( &X, buf, buflen ) == result );
536 if( result == 0)
537 {
538 hexify( str, buf, buflen );
539
540 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
541 }
542
543exit:
544 mpi_free( &X );
545}
546
547#ifdef POLARSSL_FS_IO
548void test_suite_mpi_read_file( int radix_X, char *input_file, char *input_A,
549 int result )
550{
551 mpi X;
552 unsigned char str[1000];
553 unsigned char buf[1000];
554 size_t buflen;
555 FILE *file;
556
557 memset( buf, 0x00, 1000 );
558 memset( str, 0x00, 1000 );
559
560 mpi_init( &X );
561
562 file = fopen( input_file, "r" );
563 TEST_ASSERT( file != NULL );
564 TEST_ASSERT( mpi_read_file( &X, radix_X, file ) == result );
565 fclose(file);
566
567 if( result == 0 )
568 {
569 buflen = mpi_size( &X );
570 TEST_ASSERT( mpi_write_binary( &X, buf, buflen ) == 0 );
571
572 hexify( str, buf, buflen );
573
574 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
575 }
576
577exit:
578 mpi_free( &X );
579}
580#endif /* POLARSSL_FS_IO */
581
582#ifdef POLARSSL_FS_IO
583void test_suite_mpi_write_file( int radix_X, char *input_X, int output_radix,
584 char *output_file )
585{
586 mpi X, Y;
587 FILE *file_out, *file_in;
588
589 mpi_init( &X ); mpi_init( &Y );
590
591 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
592
593 file_out = fopen( output_file, "w" );
594 TEST_ASSERT( file_out != NULL );
595 TEST_ASSERT( mpi_write_file( NULL, &X, output_radix, file_out ) == 0 );
596 fclose(file_out);
597
598 file_in = fopen( output_file, "r" );
599 TEST_ASSERT( file_in != NULL );
600 TEST_ASSERT( mpi_read_file( &Y, output_radix, file_in ) == 0 );
601 fclose(file_in);
602
603 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
604
605exit:
606 mpi_free( &X ); mpi_free( &Y );
607}
608#endif /* POLARSSL_FS_IO */
609
610void test_suite_mpi_get_bit( int radix_X, char *input_X, int pos, int val )
611{
612 mpi X;
613 mpi_init( &X );
614 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
615 TEST_ASSERT( mpi_get_bit( &X, pos ) == val );
616
617exit:
618 mpi_free( &X );
619}
620
621void test_suite_mpi_set_bit( int radix_X, char *input_X, int pos, int val, int radix_Y,
622 char *output_Y )
623{
624 mpi X, Y;
625 mpi_init( &X ); mpi_init( &Y );
626
627 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
628 TEST_ASSERT( mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
629 TEST_ASSERT( mpi_set_bit( &X, pos, val ) == 0 );
630 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
631
632exit:
633 mpi_free( &X ); mpi_free( &Y );
634}
635
636void test_suite_mpi_lsb( int radix_X, char *input_X, int nr_bits )
637{
638 mpi X;
639 mpi_init( &X );
640
641 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
642 TEST_ASSERT( mpi_lsb( &X ) == (size_t) nr_bits );
643
644exit:
645 mpi_free( &X );
646}
647
648void test_suite_mpi_msb( int radix_X, char *input_X, int nr_bits )
649{
650 mpi X;
651 mpi_init( &X );
652
653 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
654 TEST_ASSERT( mpi_msb( &X ) == (size_t) nr_bits );
655
656exit:
657 mpi_free( &X );
658}
659
660void test_suite_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
661 int radix_A, char *input_A )
662{
663 mpi A, X, Y, Z;
664 mpi_init( &A ); mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
665
666 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
667 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
668 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
669 TEST_ASSERT( mpi_gcd( &Z, &X, &Y ) == 0 );
670 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
671
672exit:
673 mpi_free( &A ); mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
674}
675
676void test_suite_mpi_cmp_int( int input_X, int input_A, int result_CMP )
677{
678 mpi X;
679 mpi_init( &X );
680
681 TEST_ASSERT( mpi_lset( &X, input_X ) == 0);
682 TEST_ASSERT( mpi_cmp_int( &X, input_A ) == result_CMP);
683
684exit:
685 mpi_free( &X );
686}
687
688void test_suite_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
689 int input_A )
690{
691 mpi X, Y;
692 mpi_init( &X ); mpi_init( &Y );
693
694 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
695 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
696 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == input_A );
697
698exit:
699 mpi_free( &X ); mpi_free( &Y );
700}
701
702void test_suite_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
703 int input_A )
704{
705 mpi X, Y;
706 mpi_init( &X ); mpi_init( &Y );
707
708 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
709 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
710 TEST_ASSERT( mpi_cmp_abs( &X, &Y ) == input_A );
711
712exit:
713 mpi_free( &X ); mpi_free( &Y );
714}
715
716void test_suite_mpi_copy( int input_X, int input_A )
717{
718 mpi X, Y, A;
719 mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
720
721 TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
722 TEST_ASSERT( mpi_lset( &Y, input_A ) == 0 );
723 TEST_ASSERT( mpi_lset( &A, input_A ) == 0 );
724 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
725 TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
726 TEST_ASSERT( mpi_copy( &Y, &X ) == 0 );
727 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
728 TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) != 0 );
729
730exit:
731 mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
732}
733
734void test_suite_mpi_copy_self( int input_X )
735{
736 mpi X;
737 mpi_init( &X );
738
739 TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
740 TEST_ASSERT( mpi_copy( &X, &X ) == 0 );
741 TEST_ASSERT( mpi_cmp_int( &X, input_X ) == 0 );
742
743exit:
744 mpi_free( &X );
745}
746
747void test_suite_mpi_shrink( int before, int used, int min, int after )
748{
749 mpi X;
750 mpi_init( &X );
751
752 TEST_ASSERT( mpi_grow( &X, before ) == 0 );
753 TEST_ASSERT( used <= before );
754 memset( X.p, 0x2a, used * sizeof( t_uint ) );
755 TEST_ASSERT( mpi_shrink( &X, min ) == 0 );
756 TEST_ASSERT( X.n == (size_t) after );
757
758exit:
759 mpi_free( &X );
760}
761
762void test_suite_mpi_safe_cond_assign( int x_sign, char *x_str,
763 int y_sign, char *y_str )
764{
765 mpi X, Y, XX;
766 mpi_init( &X ); mpi_init( &Y ); mpi_init( &XX );
767
768 TEST_ASSERT( mpi_read_string( &X, 16, x_str ) == 0 );
769 X.s = x_sign;
770 TEST_ASSERT( mpi_read_string( &Y, 16, y_str ) == 0 );
771 Y.s = y_sign;
772 TEST_ASSERT( mpi_copy( &XX, &X ) == 0 );
773
774 TEST_ASSERT( mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
775 TEST_ASSERT( mpi_cmp_mpi( &X, &XX ) == 0 );
776
777 TEST_ASSERT( mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
778 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
779
780exit:
781 mpi_free( &X ); mpi_free( &Y ); mpi_free( &XX );
782}
783
784void test_suite_mpi_safe_cond_swap( int x_sign, char *x_str,
785 int y_sign, char *y_str )
786{
787 mpi X, Y, XX, YY;
788
789 mpi_init( &X ); mpi_init( &Y );
790 mpi_init( &XX ); mpi_init( &YY );
791
792 TEST_ASSERT( mpi_read_string( &X, 16, x_str ) == 0 );
793 X.s = x_sign;
794 TEST_ASSERT( mpi_read_string( &Y, 16, y_str ) == 0 );
795 Y.s = y_sign;
796
797 TEST_ASSERT( mpi_copy( &XX, &X ) == 0 );
798 TEST_ASSERT( mpi_copy( &YY, &Y ) == 0 );
799
800 TEST_ASSERT( mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
801 TEST_ASSERT( mpi_cmp_mpi( &X, &XX ) == 0 );
802 TEST_ASSERT( mpi_cmp_mpi( &Y, &YY ) == 0 );
803
804 TEST_ASSERT( mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
805 TEST_ASSERT( mpi_cmp_mpi( &Y, &XX ) == 0 );
806 TEST_ASSERT( mpi_cmp_mpi( &X, &YY ) == 0 );
807
808exit:
809 mpi_free( &X ); mpi_free( &Y );
810 mpi_free( &XX ); mpi_free( &YY );
811}
812
813void test_suite_mpi_swap( int input_X, int input_Y )
814{
815 mpi X, Y, A;
816 mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
817
818 TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
819 TEST_ASSERT( mpi_lset( &Y, input_Y ) == 0 );
820 TEST_ASSERT( mpi_lset( &A, input_X ) == 0 );
821 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
822 TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
823 mpi_swap( &X, &Y );
824 TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
825 TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
826
827exit:
828 mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
829}
830
831void test_suite_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
832 int radix_A, char *input_A )
833{
834 mpi X, Y, Z, A;
835 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
836
837 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
838 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
839 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
840 TEST_ASSERT( mpi_add_mpi( &Z, &X, &Y ) == 0 );
841 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
842
843exit:
844 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
845}
846
847void test_suite_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
848 int radix_A, char *input_A )
849{
850 mpi X, Y, Z, A;
851 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
852
853 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
854 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
855 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
856 TEST_ASSERT( mpi_add_abs( &Z, &X, &Y ) == 0 );
857 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
858
859exit:
860 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
861}
862
863void test_suite_mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
864 char *input_Y, int radix_A, char *input_A )
865{
866 mpi X, Y, A;
867 mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
868
869 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
870 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
871 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
872 TEST_ASSERT( mpi_add_abs( &X, &X, &Y ) == 0 );
873 TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
874
875exit:
876 mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
877}
878
879void test_suite_mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
880 char *input_Y, int radix_A, char *input_A )
881{
882 mpi X, Y, A;
883 mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
884
885 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
886 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
887 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
888 TEST_ASSERT( mpi_add_abs( &Y, &X, &Y ) == 0 );
889 TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
890
891exit:
892 mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
893}
894
895void test_suite_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
896 char *input_A )
897{
898 mpi X, Z, A;
899 mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
900
901 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
902 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
903 TEST_ASSERT( mpi_add_int( &Z, &X, input_Y ) == 0 );
904 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
905
906exit:
907 mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
908}
909
910void test_suite_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
911 int radix_A, char *input_A )
912{
913 mpi X, Y, Z, A;
914 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
915
916 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
917 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
918 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
919 TEST_ASSERT( mpi_sub_mpi( &Z, &X, &Y ) == 0 );
920 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
921
922exit:
923 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
924}
925
926void test_suite_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
927 int radix_A, char *input_A, int sub_result )
928{
929 mpi X, Y, Z, A;
930 int res;
931 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
932
933 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
934 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
935 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
936
937 res = mpi_sub_abs( &Z, &X, &Y );
938 TEST_ASSERT( res == sub_result );
939 if( res == 0 )
940 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
941
942exit:
943 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
944}
945
946void test_suite_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
947 char *input_A )
948{
949 mpi X, Z, A;
950 mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
951
952 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
953 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
954 TEST_ASSERT( mpi_sub_int( &Z, &X, input_Y ) == 0 );
955 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
956
957exit:
958 mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
959}
960
961void test_suite_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
962 int radix_A, char *input_A )
963{
964 mpi X, Y, Z, A;
965 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
966
967 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
968 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
969 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
970 TEST_ASSERT( mpi_mul_mpi( &Z, &X, &Y ) == 0 );
971 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
972
973exit:
974 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
975}
976
977void test_suite_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
978 char *input_A, char *result_comparison )
979{
980 mpi X, Z, A;
981 mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
982
983 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
984 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
985 TEST_ASSERT( mpi_mul_int( &Z, &X, input_Y ) == 0 );
986 if( strcmp( result_comparison, "==" ) == 0 )
987 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
988 else if( strcmp( result_comparison, "!=" ) == 0 )
989 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) != 0 );
990 else
991 TEST_ASSERT( "unknown operator" == 0 );
992
993exit:
994 mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
995}
996
997void test_suite_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
998 int radix_A, char *input_A, int radix_B, char *input_B,
999 int div_result )
1000{
1001 mpi X, Y, Q, R, A, B;
1002 int res;
1003 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Q ); mpi_init( &R );
1004 mpi_init( &A ); mpi_init( &B );
1005
1006 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1007 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1008 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1009 TEST_ASSERT( mpi_read_string( &B, radix_B, input_B ) == 0 );
1010 res = mpi_div_mpi( &Q, &R, &X, &Y );
1011 TEST_ASSERT( res == div_result );
1012 if( res == 0 )
1013 {
1014 TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
1015 TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
1016 }
1017
1018exit:
1019 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Q ); mpi_free( &R );
1020 mpi_free( &A ); mpi_free( &B );
1021}
1022
1023void test_suite_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
1024 char *input_A, int radix_B, char *input_B, int div_result )
1025{
1026 mpi X, Q, R, A, B;
1027 int res;
1028 mpi_init( &X ); mpi_init( &Q ); mpi_init( &R ); mpi_init( &A );
1029 mpi_init( &B );
1030
1031 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1032 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1033 TEST_ASSERT( mpi_read_string( &B, radix_B, input_B ) == 0 );
1034 res = mpi_div_int( &Q, &R, &X, input_Y );
1035 TEST_ASSERT( res == div_result );
1036 if( res == 0 )
1037 {
1038 TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
1039 TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
1040 }
1041
1042exit:
1043 mpi_free( &X ); mpi_free( &Q ); mpi_free( &R ); mpi_free( &A );
1044 mpi_free( &B );
1045}
1046
1047void test_suite_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
1048 int radix_A, char *input_A, int div_result )
1049{
1050 mpi X, Y, A;
1051 int res;
1052 mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
1053
1054 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1055 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1056 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1057 res = mpi_mod_mpi( &X, &X, &Y );
1058 TEST_ASSERT( res == div_result );
1059 if( res == 0 )
1060 {
1061 TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
1062 }
1063
1064exit:
1065 mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
1066}
1067
1068void test_suite_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
1069 int div_result )
1070{
1071 mpi X;
1072 int res;
1073 t_uint r;
1074 mpi_init( &X );
1075
1076 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1077 res = mpi_mod_int( &r, &X, input_Y );
1078 TEST_ASSERT( res == div_result );
1079 if( res == 0 )
1080 {
1081 TEST_ASSERT( r == (t_uint) input_A );
1082 }
1083
1084exit:
1085 mpi_free( &X );
1086}
1087
1088void test_suite_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
1089 int radix_N, char *input_N, int radix_RR, char *input_RR,
1090 int radix_X, char *input_X, int div_result )
1091{
1092 mpi A, E, N, RR, Z, X;
1093 int res;
1094 mpi_init( &A ); mpi_init( &E ); mpi_init( &N );
1095 mpi_init( &RR ); mpi_init( &Z ); mpi_init( &X );
1096
1097 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1098 TEST_ASSERT( mpi_read_string( &E, radix_E, input_E ) == 0 );
1099 TEST_ASSERT( mpi_read_string( &N, radix_N, input_N ) == 0 );
1100 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1101
1102 if( strlen( input_RR ) )
1103 TEST_ASSERT( mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1104
1105 res = mpi_exp_mod( &Z, &A, &E, &N, &RR );
1106 TEST_ASSERT( res == div_result );
1107 if( res == 0 )
1108 {
1109 TEST_ASSERT( mpi_cmp_mpi( &Z, &X ) == 0 );
1110 }
1111
1112exit:
1113 mpi_free( &A ); mpi_free( &E ); mpi_free( &N );
1114 mpi_free( &RR ); mpi_free( &Z ); mpi_free( &X );
1115}
1116
1117void test_suite_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
1118 int radix_A, char *input_A, int div_result )
1119{
1120 mpi X, Y, Z, A;
1121 int res;
1122 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
1123
1124 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1125 TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1126 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1127 res = mpi_inv_mod( &Z, &X, &Y );
1128 TEST_ASSERT( res == div_result );
1129 if( res == 0 )
1130 {
1131 TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
1132 }
1133
1134exit:
1135 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
1136}
1137
1138#ifdef POLARSSL_GENPRIME
1139void test_suite_mpi_is_prime( int radix_X, char *input_X, int div_result )
1140{
1141 mpi X;
1142 int res;
1143 mpi_init( &X );
1144
1145 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1146 res = mpi_is_prime( &X, rnd_std_rand, NULL );
1147 TEST_ASSERT( res == div_result );
1148
1149exit:
1150 mpi_free( &X );
1151}
1152#endif /* POLARSSL_GENPRIME */
1153
1154#ifdef POLARSSL_GENPRIME
1155void test_suite_mpi_gen_prime( int bits, int safe, int ref_ret )
1156{
1157 mpi X;
1158 int my_ret;
1159
1160 mpi_init( &X );
1161
1162 my_ret = mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
1163 TEST_ASSERT( my_ret == ref_ret );
1164
1165 if( ref_ret == 0 )
1166 {
1167 size_t actual_bits = mpi_msb( &X );
1168
1169 TEST_ASSERT( actual_bits >= (size_t) bits );
1170 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1171
1172 TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
1173 if( safe )
1174 {
1175 mpi_shift_r( &X, 1 ); /* X = ( X - 1 ) / 2 */
1176 TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
1177 }
1178 }
1179
1180exit:
1181 mpi_free( &X );
1182}
1183#endif /* POLARSSL_GENPRIME */
1184
1185void test_suite_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
1186 char *input_A)
1187{
1188 mpi X, A;
1189 mpi_init( &X ); mpi_init( &A );
1190
1191 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1192 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1193 TEST_ASSERT( mpi_shift_l( &X, shift_X ) == 0 );
1194 TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
1195
1196exit:
1197 mpi_free( &X ); mpi_free( &A );
1198}
1199
1200void test_suite_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
1201 char *input_A )
1202{
1203 mpi X, A;
1204 mpi_init( &X ); mpi_init( &A );
1205
1206 TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
1207 TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
1208 TEST_ASSERT( mpi_shift_r( &X, shift_X ) == 0 );
1209 TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
1210
1211exit:
1212 mpi_free( &X ); mpi_free( &A );
1213}
1214
1215#ifdef POLARSSL_SELF_TEST
1216void test_suite_mpi_selftest()
1217{
1218 TEST_ASSERT( mpi_self_test( 0 ) == 0 );
1219
1220exit:
1221 return;
1222}
1223#endif /* POLARSSL_SELF_TEST */
1224
1225
1226#endif /* POLARSSL_BIGNUM_C */
1227
1228
1229int dep_check( char *str )
1230{
1231 if( str == NULL )
1232 return( 1 );
1233
1234 if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
1235 {
1236#if defined(POLARSSL_SELF_TEST)
1237 return( 0 );
1238#else
1239 return( 1 );
1240#endif
1241 }
1242 if( strcmp( str, "POLARSSL_GENPRIME" ) == 0 )
1243 {
1244#if defined(POLARSSL_GENPRIME)
1245 return( 0 );
1246#else
1247 return( 1 );
1248#endif
1249 }
1250
1251
1252 return( 1 );
1253}
1254
1255int dispatch_test(int cnt, char *params[50])
1256{
1257 int ret;
1258 ((void) cnt);
1259 ((void) params);
1260
1261#if defined(TEST_SUITE_ACTIVE)
1262 if( strcmp( params[0], "mpi_read_write_string" ) == 0 )
1263 {
1264
1265 int param1;
1266 char *param2 = params[2];
1267 int param3;
1268 char *param4 = params[4];
1269 int param5;
1270 int param6;
1271 int param7;
1272
1273 if( cnt != 8 )
1274 {
1275 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1276 return( 2 );
1277 }
1278
1279 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1280 if( verify_string( &param2 ) != 0 ) return( 2 );
1281 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1282 if( verify_string( &param4 ) != 0 ) return( 2 );
1283 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1284 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1285 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1286
1287 test_suite_mpi_read_write_string( param1, param2, param3, param4, param5, param6, param7 );
1288 return ( 0 );
1289
1290 return ( 3 );
1291 }
1292 else
1293 if( strcmp( params[0], "mpi_read_binary" ) == 0 )
1294 {
1295
1296 char *param1 = params[1];
1297 int param2;
1298 char *param3 = params[3];
1299
1300 if( cnt != 4 )
1301 {
1302 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1303 return( 2 );
1304 }
1305
1306 if( verify_string( &param1 ) != 0 ) return( 2 );
1307 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1308 if( verify_string( &param3 ) != 0 ) return( 2 );
1309
1310 test_suite_mpi_read_binary( param1, param2, param3 );
1311 return ( 0 );
1312
1313 return ( 3 );
1314 }
1315 else
1316 if( strcmp( params[0], "mpi_write_binary" ) == 0 )
1317 {
1318
1319 int param1;
1320 char *param2 = params[2];
1321 char *param3 = params[3];
1322 int param4;
1323 int param5;
1324
1325 if( cnt != 6 )
1326 {
1327 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1328 return( 2 );
1329 }
1330
1331 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1332 if( verify_string( &param2 ) != 0 ) return( 2 );
1333 if( verify_string( &param3 ) != 0 ) return( 2 );
1334 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1335 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1336
1337 test_suite_mpi_write_binary( param1, param2, param3, param4, param5 );
1338 return ( 0 );
1339
1340 return ( 3 );
1341 }
1342 else
1343 if( strcmp( params[0], "mpi_read_file" ) == 0 )
1344 {
1345 #ifdef POLARSSL_FS_IO
1346
1347 int param1;
1348 char *param2 = params[2];
1349 char *param3 = params[3];
1350 int param4;
1351
1352 if( cnt != 5 )
1353 {
1354 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1355 return( 2 );
1356 }
1357
1358 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1359 if( verify_string( &param2 ) != 0 ) return( 2 );
1360 if( verify_string( &param3 ) != 0 ) return( 2 );
1361 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1362
1363 test_suite_mpi_read_file( param1, param2, param3, param4 );
1364 return ( 0 );
1365 #endif /* POLARSSL_FS_IO */
1366
1367 return ( 3 );
1368 }
1369 else
1370 if( strcmp( params[0], "mpi_write_file" ) == 0 )
1371 {
1372 #ifdef POLARSSL_FS_IO
1373
1374 int param1;
1375 char *param2 = params[2];
1376 int param3;
1377 char *param4 = params[4];
1378
1379 if( cnt != 5 )
1380 {
1381 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1382 return( 2 );
1383 }
1384
1385 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1386 if( verify_string( &param2 ) != 0 ) return( 2 );
1387 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1388 if( verify_string( &param4 ) != 0 ) return( 2 );
1389
1390 test_suite_mpi_write_file( param1, param2, param3, param4 );
1391 return ( 0 );
1392 #endif /* POLARSSL_FS_IO */
1393
1394 return ( 3 );
1395 }
1396 else
1397 if( strcmp( params[0], "mpi_get_bit" ) == 0 )
1398 {
1399
1400 int param1;
1401 char *param2 = params[2];
1402 int param3;
1403 int param4;
1404
1405 if( cnt != 5 )
1406 {
1407 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1408 return( 2 );
1409 }
1410
1411 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1412 if( verify_string( &param2 ) != 0 ) return( 2 );
1413 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1414 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1415
1416 test_suite_mpi_get_bit( param1, param2, param3, param4 );
1417 return ( 0 );
1418
1419 return ( 3 );
1420 }
1421 else
1422 if( strcmp( params[0], "mpi_set_bit" ) == 0 )
1423 {
1424
1425 int param1;
1426 char *param2 = params[2];
1427 int param3;
1428 int param4;
1429 int param5;
1430 char *param6 = params[6];
1431
1432 if( cnt != 7 )
1433 {
1434 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1435 return( 2 );
1436 }
1437
1438 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1439 if( verify_string( &param2 ) != 0 ) return( 2 );
1440 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1441 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1442 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1443 if( verify_string( &param6 ) != 0 ) return( 2 );
1444
1445 test_suite_mpi_set_bit( param1, param2, param3, param4, param5, param6 );
1446 return ( 0 );
1447
1448 return ( 3 );
1449 }
1450 else
1451 if( strcmp( params[0], "mpi_lsb" ) == 0 )
1452 {
1453
1454 int param1;
1455 char *param2 = params[2];
1456 int param3;
1457
1458 if( cnt != 4 )
1459 {
1460 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1461 return( 2 );
1462 }
1463
1464 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1465 if( verify_string( &param2 ) != 0 ) return( 2 );
1466 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1467
1468 test_suite_mpi_lsb( param1, param2, param3 );
1469 return ( 0 );
1470
1471 return ( 3 );
1472 }
1473 else
1474 if( strcmp( params[0], "mpi_msb" ) == 0 )
1475 {
1476
1477 int param1;
1478 char *param2 = params[2];
1479 int param3;
1480
1481 if( cnt != 4 )
1482 {
1483 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1484 return( 2 );
1485 }
1486
1487 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1488 if( verify_string( &param2 ) != 0 ) return( 2 );
1489 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1490
1491 test_suite_mpi_msb( param1, param2, param3 );
1492 return ( 0 );
1493
1494 return ( 3 );
1495 }
1496 else
1497 if( strcmp( params[0], "mpi_gcd" ) == 0 )
1498 {
1499
1500 int param1;
1501 char *param2 = params[2];
1502 int param3;
1503 char *param4 = params[4];
1504 int param5;
1505 char *param6 = params[6];
1506
1507 if( cnt != 7 )
1508 {
1509 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1510 return( 2 );
1511 }
1512
1513 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1514 if( verify_string( &param2 ) != 0 ) return( 2 );
1515 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1516 if( verify_string( &param4 ) != 0 ) return( 2 );
1517 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1518 if( verify_string( &param6 ) != 0 ) return( 2 );
1519
1520 test_suite_mpi_gcd( param1, param2, param3, param4, param5, param6 );
1521 return ( 0 );
1522
1523 return ( 3 );
1524 }
1525 else
1526 if( strcmp( params[0], "mpi_cmp_int" ) == 0 )
1527 {
1528
1529 int param1;
1530 int param2;
1531 int param3;
1532
1533 if( cnt != 4 )
1534 {
1535 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1536 return( 2 );
1537 }
1538
1539 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1540 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1541 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1542
1543 test_suite_mpi_cmp_int( param1, param2, param3 );
1544 return ( 0 );
1545
1546 return ( 3 );
1547 }
1548 else
1549 if( strcmp( params[0], "mpi_cmp_mpi" ) == 0 )
1550 {
1551
1552 int param1;
1553 char *param2 = params[2];
1554 int param3;
1555 char *param4 = params[4];
1556 int param5;
1557
1558 if( cnt != 6 )
1559 {
1560 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1561 return( 2 );
1562 }
1563
1564 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1565 if( verify_string( &param2 ) != 0 ) return( 2 );
1566 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1567 if( verify_string( &param4 ) != 0 ) return( 2 );
1568 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1569
1570 test_suite_mpi_cmp_mpi( param1, param2, param3, param4, param5 );
1571 return ( 0 );
1572
1573 return ( 3 );
1574 }
1575 else
1576 if( strcmp( params[0], "mpi_cmp_abs" ) == 0 )
1577 {
1578
1579 int param1;
1580 char *param2 = params[2];
1581 int param3;
1582 char *param4 = params[4];
1583 int param5;
1584
1585 if( cnt != 6 )
1586 {
1587 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1588 return( 2 );
1589 }
1590
1591 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1592 if( verify_string( &param2 ) != 0 ) return( 2 );
1593 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1594 if( verify_string( &param4 ) != 0 ) return( 2 );
1595 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1596
1597 test_suite_mpi_cmp_abs( param1, param2, param3, param4, param5 );
1598 return ( 0 );
1599
1600 return ( 3 );
1601 }
1602 else
1603 if( strcmp( params[0], "mpi_copy" ) == 0 )
1604 {
1605
1606 int param1;
1607 int param2;
1608
1609 if( cnt != 3 )
1610 {
1611 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1612 return( 2 );
1613 }
1614
1615 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1616 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1617
1618 test_suite_mpi_copy( param1, param2 );
1619 return ( 0 );
1620
1621 return ( 3 );
1622 }
1623 else
1624 if( strcmp( params[0], "mpi_copy_self" ) == 0 )
1625 {
1626
1627 int param1;
1628
1629 if( cnt != 2 )
1630 {
1631 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1632 return( 2 );
1633 }
1634
1635 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1636
1637 test_suite_mpi_copy_self( param1 );
1638 return ( 0 );
1639
1640 return ( 3 );
1641 }
1642 else
1643 if( strcmp( params[0], "mpi_shrink" ) == 0 )
1644 {
1645
1646 int param1;
1647 int param2;
1648 int param3;
1649 int param4;
1650
1651 if( cnt != 5 )
1652 {
1653 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1654 return( 2 );
1655 }
1656
1657 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1658 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1659 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1660 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1661
1662 test_suite_mpi_shrink( param1, param2, param3, param4 );
1663 return ( 0 );
1664
1665 return ( 3 );
1666 }
1667 else
1668 if( strcmp( params[0], "mpi_safe_cond_assign" ) == 0 )
1669 {
1670
1671 int param1;
1672 char *param2 = params[2];
1673 int param3;
1674 char *param4 = params[4];
1675
1676 if( cnt != 5 )
1677 {
1678 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1679 return( 2 );
1680 }
1681
1682 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1683 if( verify_string( &param2 ) != 0 ) return( 2 );
1684 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1685 if( verify_string( &param4 ) != 0 ) return( 2 );
1686
1687 test_suite_mpi_safe_cond_assign( param1, param2, param3, param4 );
1688 return ( 0 );
1689
1690 return ( 3 );
1691 }
1692 else
1693 if( strcmp( params[0], "mpi_safe_cond_swap" ) == 0 )
1694 {
1695
1696 int param1;
1697 char *param2 = params[2];
1698 int param3;
1699 char *param4 = params[4];
1700
1701 if( cnt != 5 )
1702 {
1703 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1704 return( 2 );
1705 }
1706
1707 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1708 if( verify_string( &param2 ) != 0 ) return( 2 );
1709 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1710 if( verify_string( &param4 ) != 0 ) return( 2 );
1711
1712 test_suite_mpi_safe_cond_swap( param1, param2, param3, param4 );
1713 return ( 0 );
1714
1715 return ( 3 );
1716 }
1717 else
1718 if( strcmp( params[0], "mpi_swap" ) == 0 )
1719 {
1720
1721 int param1;
1722 int param2;
1723
1724 if( cnt != 3 )
1725 {
1726 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1727 return( 2 );
1728 }
1729
1730 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1731 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1732
1733 test_suite_mpi_swap( param1, param2 );
1734 return ( 0 );
1735
1736 return ( 3 );
1737 }
1738 else
1739 if( strcmp( params[0], "mpi_add_mpi" ) == 0 )
1740 {
1741
1742 int param1;
1743 char *param2 = params[2];
1744 int param3;
1745 char *param4 = params[4];
1746 int param5;
1747 char *param6 = params[6];
1748
1749 if( cnt != 7 )
1750 {
1751 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1752 return( 2 );
1753 }
1754
1755 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1756 if( verify_string( &param2 ) != 0 ) return( 2 );
1757 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1758 if( verify_string( &param4 ) != 0 ) return( 2 );
1759 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1760 if( verify_string( &param6 ) != 0 ) return( 2 );
1761
1762 test_suite_mpi_add_mpi( param1, param2, param3, param4, param5, param6 );
1763 return ( 0 );
1764
1765 return ( 3 );
1766 }
1767 else
1768 if( strcmp( params[0], "mpi_add_abs" ) == 0 )
1769 {
1770
1771 int param1;
1772 char *param2 = params[2];
1773 int param3;
1774 char *param4 = params[4];
1775 int param5;
1776 char *param6 = params[6];
1777
1778 if( cnt != 7 )
1779 {
1780 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1781 return( 2 );
1782 }
1783
1784 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1785 if( verify_string( &param2 ) != 0 ) return( 2 );
1786 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1787 if( verify_string( &param4 ) != 0 ) return( 2 );
1788 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1789 if( verify_string( &param6 ) != 0 ) return( 2 );
1790
1791 test_suite_mpi_add_abs( param1, param2, param3, param4, param5, param6 );
1792 return ( 0 );
1793
1794 return ( 3 );
1795 }
1796 else
1797 if( strcmp( params[0], "mpi_add_abs_add_first" ) == 0 )
1798 {
1799
1800 int param1;
1801 char *param2 = params[2];
1802 int param3;
1803 char *param4 = params[4];
1804 int param5;
1805 char *param6 = params[6];
1806
1807 if( cnt != 7 )
1808 {
1809 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1810 return( 2 );
1811 }
1812
1813 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1814 if( verify_string( &param2 ) != 0 ) return( 2 );
1815 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1816 if( verify_string( &param4 ) != 0 ) return( 2 );
1817 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1818 if( verify_string( &param6 ) != 0 ) return( 2 );
1819
1820 test_suite_mpi_add_abs_add_first( param1, param2, param3, param4, param5, param6 );
1821 return ( 0 );
1822
1823 return ( 3 );
1824 }
1825 else
1826 if( strcmp( params[0], "mpi_add_abs_add_second" ) == 0 )
1827 {
1828
1829 int param1;
1830 char *param2 = params[2];
1831 int param3;
1832 char *param4 = params[4];
1833 int param5;
1834 char *param6 = params[6];
1835
1836 if( cnt != 7 )
1837 {
1838 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1839 return( 2 );
1840 }
1841
1842 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1843 if( verify_string( &param2 ) != 0 ) return( 2 );
1844 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1845 if( verify_string( &param4 ) != 0 ) return( 2 );
1846 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1847 if( verify_string( &param6 ) != 0 ) return( 2 );
1848
1849 test_suite_mpi_add_abs_add_second( param1, param2, param3, param4, param5, param6 );
1850 return ( 0 );
1851
1852 return ( 3 );
1853 }
1854 else
1855 if( strcmp( params[0], "mpi_add_int" ) == 0 )
1856 {
1857
1858 int param1;
1859 char *param2 = params[2];
1860 int param3;
1861 int param4;
1862 char *param5 = params[5];
1863
1864 if( cnt != 6 )
1865 {
1866 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1867 return( 2 );
1868 }
1869
1870 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1871 if( verify_string( &param2 ) != 0 ) return( 2 );
1872 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1873 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1874 if( verify_string( &param5 ) != 0 ) return( 2 );
1875
1876 test_suite_mpi_add_int( param1, param2, param3, param4, param5 );
1877 return ( 0 );
1878
1879 return ( 3 );
1880 }
1881 else
1882 if( strcmp( params[0], "mpi_sub_mpi" ) == 0 )
1883 {
1884
1885 int param1;
1886 char *param2 = params[2];
1887 int param3;
1888 char *param4 = params[4];
1889 int param5;
1890 char *param6 = params[6];
1891
1892 if( cnt != 7 )
1893 {
1894 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1895 return( 2 );
1896 }
1897
1898 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1899 if( verify_string( &param2 ) != 0 ) return( 2 );
1900 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1901 if( verify_string( &param4 ) != 0 ) return( 2 );
1902 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1903 if( verify_string( &param6 ) != 0 ) return( 2 );
1904
1905 test_suite_mpi_sub_mpi( param1, param2, param3, param4, param5, param6 );
1906 return ( 0 );
1907
1908 return ( 3 );
1909 }
1910 else
1911 if( strcmp( params[0], "mpi_sub_abs" ) == 0 )
1912 {
1913
1914 int param1;
1915 char *param2 = params[2];
1916 int param3;
1917 char *param4 = params[4];
1918 int param5;
1919 char *param6 = params[6];
1920 int param7;
1921
1922 if( cnt != 8 )
1923 {
1924 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1925 return( 2 );
1926 }
1927
1928 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1929 if( verify_string( &param2 ) != 0 ) return( 2 );
1930 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1931 if( verify_string( &param4 ) != 0 ) return( 2 );
1932 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1933 if( verify_string( &param6 ) != 0 ) return( 2 );
1934 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1935
1936 test_suite_mpi_sub_abs( param1, param2, param3, param4, param5, param6, param7 );
1937 return ( 0 );
1938
1939 return ( 3 );
1940 }
1941 else
1942 if( strcmp( params[0], "mpi_sub_int" ) == 0 )
1943 {
1944
1945 int param1;
1946 char *param2 = params[2];
1947 int param3;
1948 int param4;
1949 char *param5 = params[5];
1950
1951 if( cnt != 6 )
1952 {
1953 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1954 return( 2 );
1955 }
1956
1957 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1958 if( verify_string( &param2 ) != 0 ) return( 2 );
1959 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1960 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1961 if( verify_string( &param5 ) != 0 ) return( 2 );
1962
1963 test_suite_mpi_sub_int( param1, param2, param3, param4, param5 );
1964 return ( 0 );
1965
1966 return ( 3 );
1967 }
1968 else
1969 if( strcmp( params[0], "mpi_mul_mpi" ) == 0 )
1970 {
1971
1972 int param1;
1973 char *param2 = params[2];
1974 int param3;
1975 char *param4 = params[4];
1976 int param5;
1977 char *param6 = params[6];
1978
1979 if( cnt != 7 )
1980 {
1981 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1982 return( 2 );
1983 }
1984
1985 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1986 if( verify_string( &param2 ) != 0 ) return( 2 );
1987 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1988 if( verify_string( &param4 ) != 0 ) return( 2 );
1989 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1990 if( verify_string( &param6 ) != 0 ) return( 2 );
1991
1992 test_suite_mpi_mul_mpi( param1, param2, param3, param4, param5, param6 );
1993 return ( 0 );
1994
1995 return ( 3 );
1996 }
1997 else
1998 if( strcmp( params[0], "mpi_mul_int" ) == 0 )
1999 {
2000
2001 int param1;
2002 char *param2 = params[2];
2003 int param3;
2004 int param4;
2005 char *param5 = params[5];
2006 char *param6 = params[6];
2007
2008 if( cnt != 7 )
2009 {
2010 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
2011 return( 2 );
2012 }
2013
2014 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2015 if( verify_string( &param2 ) != 0 ) return( 2 );
2016 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2017 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
2018 if( verify_string( &param5 ) != 0 ) return( 2 );
2019 if( verify_string( &param6 ) != 0 ) return( 2 );
2020
2021 test_suite_mpi_mul_int( param1, param2, param3, param4, param5, param6 );
2022 return ( 0 );
2023
2024 return ( 3 );
2025 }
2026 else
2027 if( strcmp( params[0], "mpi_div_mpi" ) == 0 )
2028 {
2029
2030 int param1;
2031 char *param2 = params[2];
2032 int param3;
2033 char *param4 = params[4];
2034 int param5;
2035 char *param6 = params[6];
2036 int param7;
2037 char *param8 = params[8];
2038 int param9;
2039
2040 if( cnt != 10 )
2041 {
2042 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
2043 return( 2 );
2044 }
2045
2046 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2047 if( verify_string( &param2 ) != 0 ) return( 2 );
2048 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2049 if( verify_string( &param4 ) != 0 ) return( 2 );
2050 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
2051 if( verify_string( &param6 ) != 0 ) return( 2 );
2052 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
2053 if( verify_string( &param8 ) != 0 ) return( 2 );
2054 if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
2055
2056 test_suite_mpi_div_mpi( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
2057 return ( 0 );
2058
2059 return ( 3 );
2060 }
2061 else
2062 if( strcmp( params[0], "mpi_div_int" ) == 0 )
2063 {
2064
2065 int param1;
2066 char *param2 = params[2];
2067 int param3;
2068 int param4;
2069 char *param5 = params[5];
2070 int param6;
2071 char *param7 = params[7];
2072 int param8;
2073
2074 if( cnt != 9 )
2075 {
2076 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
2077 return( 2 );
2078 }
2079
2080 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2081 if( verify_string( &param2 ) != 0 ) return( 2 );
2082 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2083 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
2084 if( verify_string( &param5 ) != 0 ) return( 2 );
2085 if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
2086 if( verify_string( &param7 ) != 0 ) return( 2 );
2087 if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
2088
2089 test_suite_mpi_div_int( param1, param2, param3, param4, param5, param6, param7, param8 );
2090 return ( 0 );
2091
2092 return ( 3 );
2093 }
2094 else
2095 if( strcmp( params[0], "mpi_mod_mpi" ) == 0 )
2096 {
2097
2098 int param1;
2099 char *param2 = params[2];
2100 int param3;
2101 char *param4 = params[4];
2102 int param5;
2103 char *param6 = params[6];
2104 int param7;
2105
2106 if( cnt != 8 )
2107 {
2108 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
2109 return( 2 );
2110 }
2111
2112 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2113 if( verify_string( &param2 ) != 0 ) return( 2 );
2114 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2115 if( verify_string( &param4 ) != 0 ) return( 2 );
2116 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
2117 if( verify_string( &param6 ) != 0 ) return( 2 );
2118 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
2119
2120 test_suite_mpi_mod_mpi( param1, param2, param3, param4, param5, param6, param7 );
2121 return ( 0 );
2122
2123 return ( 3 );
2124 }
2125 else
2126 if( strcmp( params[0], "mpi_mod_int" ) == 0 )
2127 {
2128
2129 int param1;
2130 char *param2 = params[2];
2131 int param3;
2132 int param4;
2133 int param5;
2134
2135 if( cnt != 6 )
2136 {
2137 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2138 return( 2 );
2139 }
2140
2141 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2142 if( verify_string( &param2 ) != 0 ) return( 2 );
2143 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2144 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
2145 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
2146
2147 test_suite_mpi_mod_int( param1, param2, param3, param4, param5 );
2148 return ( 0 );
2149
2150 return ( 3 );
2151 }
2152 else
2153 if( strcmp( params[0], "mpi_exp_mod" ) == 0 )
2154 {
2155
2156 int param1;
2157 char *param2 = params[2];
2158 int param3;
2159 char *param4 = params[4];
2160 int param5;
2161 char *param6 = params[6];
2162 int param7;
2163 char *param8 = params[8];
2164 int param9;
2165 char *param10 = params[10];
2166 int param11;
2167
2168 if( cnt != 12 )
2169 {
2170 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
2171 return( 2 );
2172 }
2173
2174 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2175 if( verify_string( &param2 ) != 0 ) return( 2 );
2176 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2177 if( verify_string( &param4 ) != 0 ) return( 2 );
2178 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
2179 if( verify_string( &param6 ) != 0 ) return( 2 );
2180 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
2181 if( verify_string( &param8 ) != 0 ) return( 2 );
2182 if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
2183 if( verify_string( &param10 ) != 0 ) return( 2 );
2184 if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
2185
2186 test_suite_mpi_exp_mod( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
2187 return ( 0 );
2188
2189 return ( 3 );
2190 }
2191 else
2192 if( strcmp( params[0], "mpi_inv_mod" ) == 0 )
2193 {
2194
2195 int param1;
2196 char *param2 = params[2];
2197 int param3;
2198 char *param4 = params[4];
2199 int param5;
2200 char *param6 = params[6];
2201 int param7;
2202
2203 if( cnt != 8 )
2204 {
2205 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
2206 return( 2 );
2207 }
2208
2209 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2210 if( verify_string( &param2 ) != 0 ) return( 2 );
2211 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2212 if( verify_string( &param4 ) != 0 ) return( 2 );
2213 if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
2214 if( verify_string( &param6 ) != 0 ) return( 2 );
2215 if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
2216
2217 test_suite_mpi_inv_mod( param1, param2, param3, param4, param5, param6, param7 );
2218 return ( 0 );
2219
2220 return ( 3 );
2221 }
2222 else
2223 if( strcmp( params[0], "mpi_is_prime" ) == 0 )
2224 {
2225 #ifdef POLARSSL_GENPRIME
2226
2227 int param1;
2228 char *param2 = params[2];
2229 int param3;
2230
2231 if( cnt != 4 )
2232 {
2233 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
2234 return( 2 );
2235 }
2236
2237 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2238 if( verify_string( &param2 ) != 0 ) return( 2 );
2239 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2240
2241 test_suite_mpi_is_prime( param1, param2, param3 );
2242 return ( 0 );
2243 #endif /* POLARSSL_GENPRIME */
2244
2245 return ( 3 );
2246 }
2247 else
2248 if( strcmp( params[0], "mpi_gen_prime" ) == 0 )
2249 {
2250 #ifdef POLARSSL_GENPRIME
2251
2252 int param1;
2253 int param2;
2254 int param3;
2255
2256 if( cnt != 4 )
2257 {
2258 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
2259 return( 2 );
2260 }
2261
2262 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2263 if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
2264 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2265
2266 test_suite_mpi_gen_prime( param1, param2, param3 );
2267 return ( 0 );
2268 #endif /* POLARSSL_GENPRIME */
2269
2270 return ( 3 );
2271 }
2272 else
2273 if( strcmp( params[0], "mpi_shift_l" ) == 0 )
2274 {
2275
2276 int param1;
2277 char *param2 = params[2];
2278 int param3;
2279 int param4;
2280 char *param5 = params[5];
2281
2282 if( cnt != 6 )
2283 {
2284 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2285 return( 2 );
2286 }
2287
2288 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2289 if( verify_string( &param2 ) != 0 ) return( 2 );
2290 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2291 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
2292 if( verify_string( &param5 ) != 0 ) return( 2 );
2293
2294 test_suite_mpi_shift_l( param1, param2, param3, param4, param5 );
2295 return ( 0 );
2296
2297 return ( 3 );
2298 }
2299 else
2300 if( strcmp( params[0], "mpi_shift_r" ) == 0 )
2301 {
2302
2303 int param1;
2304 char *param2 = params[2];
2305 int param3;
2306 int param4;
2307 char *param5 = params[5];
2308
2309 if( cnt != 6 )
2310 {
2311 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2312 return( 2 );
2313 }
2314
2315 if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
2316 if( verify_string( &param2 ) != 0 ) return( 2 );
2317 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
2318 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
2319 if( verify_string( &param5 ) != 0 ) return( 2 );
2320
2321 test_suite_mpi_shift_r( param1, param2, param3, param4, param5 );
2322 return ( 0 );
2323
2324 return ( 3 );
2325 }
2326 else
2327 if( strcmp( params[0], "mpi_selftest" ) == 0 )
2328 {
2329 #ifdef POLARSSL_SELF_TEST
2330
2331
2332 if( cnt != 1 )
2333 {
2334 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
2335 return( 2 );
2336 }
2337
2338
2339 test_suite_mpi_selftest( );
2340 return ( 0 );
2341 #endif /* POLARSSL_SELF_TEST */
2342
2343 return ( 3 );
2344 }
2345 else
2346
2347 {
2348 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
2349 fflush( stdout );
2350 return( 1 );
2351 }
2352#else
2353 return( 3 );
2354#endif
2355 return( ret );
2356}
2357
2358int get_line( FILE *f, char *buf, size_t len )
2359{
2360 char *ret;
2361
2362 ret = fgets( buf, len, f );
2363 if( ret == NULL )
2364 return( -1 );
2365
2366 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
2367 buf[strlen(buf) - 1] = '\0';
2368 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
2369 buf[strlen(buf) - 1] = '\0';
2370
2371 return( 0 );
2372}
2373
2374int parse_arguments( char *buf, size_t len, char *params[50] )
2375{
2376 int cnt = 0, i;
2377 char *cur = buf;
2378 char *p = buf, *q;
2379
2380 params[cnt++] = cur;
2381
2382 while( *p != '\0' && p < buf + len )
2383 {
2384 if( *p == '\\' )
2385 {
2386 p++;
2387 p++;
2388 continue;
2389 }
2390 if( *p == ':' )
2391 {
2392 if( p + 1 < buf + len )
2393 {
2394 cur = p + 1;
2395 params[cnt++] = cur;
2396 }
2397 *p = '\0';
2398 }
2399
2400 p++;
2401 }
2402
2403 // Replace newlines, question marks and colons in strings
2404 for( i = 0; i < cnt; i++ )
2405 {
2406 p = params[i];
2407 q = params[i];
2408
2409 while( *p != '\0' )
2410 {
2411 if( *p == '\\' && *(p + 1) == 'n' )
2412 {
2413 p += 2;
2414 *(q++) = '\n';
2415 }
2416 else if( *p == '\\' && *(p + 1) == ':' )
2417 {
2418 p += 2;
2419 *(q++) = ':';
2420 }
2421 else if( *p == '\\' && *(p + 1) == '?' )
2422 {
2423 p += 2;
2424 *(q++) = '?';
2425 }
2426 else
2427 *(q++) = *(p++);
2428 }
2429 *q = '\0';
2430 }
2431
2432 return( cnt );
2433}
2434
2435int main()
2436{
2437 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
2438 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_mpi.data";
2439 FILE *file;
2440 char buf[5000];
2441 char *params[50];
2442
2443#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2444 unsigned char alloc_buf[1000000];
2445 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
2446#endif
2447
2448 file = fopen( filename, "r" );
2449 if( file == NULL )
2450 {
2451 fprintf( stderr, "Failed to open\n" );
2452 return( 1 );
2453 }
2454
2455 while( !feof( file ) )
2456 {
2457 int skip = 0;
2458
2459 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
2460 break;
2461 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
2462 fprintf( stdout, " " );
2463 for( i = strlen( buf ) + 1; i < 67; i++ )
2464 fprintf( stdout, "." );
2465 fprintf( stdout, " " );
2466 fflush( stdout );
2467
2468 total_tests++;
2469
2470 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
2471 break;
2472 cnt = parse_arguments( buf, strlen(buf), params );
2473
2474 if( strcmp( params[0], "depends_on" ) == 0 )
2475 {
2476 for( i = 1; i < cnt; i++ )
2477 if( dep_check( params[i] ) != 0 )
2478 skip = 1;
2479
2480 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
2481 break;
2482 cnt = parse_arguments( buf, strlen(buf), params );
2483 }
2484
2485 if( skip == 0 )
2486 {
2487 test_errors = 0;
2488 ret = dispatch_test( cnt, params );
2489 }
2490
2491 if( skip == 1 || ret == 3 )
2492 {
2493 total_skipped++;
2494 fprintf( stdout, "----\n" );
2495 fflush( stdout );
2496 }
2497 else if( ret == 0 && test_errors == 0 )
2498 {
2499 fprintf( stdout, "PASS\n" );
2500 fflush( stdout );
2501 }
2502 else if( ret == 2 )
2503 {
2504 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
2505 fclose(file);
2506 exit( 2 );
2507 }
2508 else
2509 total_errors++;
2510
2511 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
2512 break;
2513 if( strlen(buf) != 0 )
2514 {
2515 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
2516 return( 1 );
2517 }
2518 }
2519 fclose(file);
2520
2521 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
2522 if( total_errors == 0 )
2523 fprintf( stdout, "PASSED" );
2524 else
2525 fprintf( stdout, "FAILED" );
2526
2527 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
2528 total_tests - total_errors, total_tests, total_skipped );
2529
2530#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2531#if defined(POLARSSL_MEMORY_DEBUG)
2532 memory_buffer_alloc_status();
2533#endif
2535#endif
2536
2537 return( total_errors != 0 );
2538}
2539
2540
Multi-precision integer library.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
int mpi_shift_r(mpi *X, size_t count)
Right-shift: X >>= count.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed subtraction: X = A - B.
int mpi_shift_l(mpi *X, size_t count)
Left-shift: X <<= count.
int mpi_sub_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned subtraction: X = |A| - |B|.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_gen_prime(mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
int mpi_add_int(mpi *X, const mpi *A, t_sint b)
Signed addition: X = A + b.
int mpi_safe_cond_assign(mpi *X, const mpi *Y, unsigned char assign)
Safe conditional assignement X = Y if assign is 1.
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
#define POLARSSL_ERR_MPI_FILE_IO_ERROR
An error occurred while reading from or writing to a file.
Definition bignum.h:56
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
int mpi_div_int(mpi *Q, mpi *R, const mpi *A, t_sint b)
Division by int: A = Q * b + R.
int mpi_add_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned addition: X = |A| + |B|.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int mpi_write_file(const char *p, const mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
int mpi_cmp_abs(const mpi *X, const mpi *Y)
Compare unsigned values.
size_t mpi_lsb(const mpi *X)
Return the number of zero-bits before the least significant '1' bit.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int mpi_mul_int(mpi *X, const mpi *A, t_sint b)
Baseline multiplication: X = A * b Note: despite the functon signature, b is treated as a t_uint.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int mpi_mod_int(t_uint *r, const mpi *A, t_sint b)
Modulo: r = A mod b.
int mpi_safe_cond_swap(mpi *X, mpi *Y, unsigned char assign)
Safe conditional swap X <-> Y if swap is 1.
int mpi_get_bit(const mpi *X, size_t pos)
Get a specific bit from X.
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO
The input argument for division is zero, which is not allowed.
Definition bignum.h:61
int mpi_read_file(mpi *X, int radix, FILE *fin)
Read X from an opened file.
int mpi_write_string(const mpi *X, int radix, char *s, size_t *slen)
Export into an ASCII string.
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA
Bad input parameters to function.
Definition bignum.h:57
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL
The buffer is too small to write to.
Definition bignum.h:59
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
int mpi_self_test(int verbose)
Checkup routine.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE
The input arguments are not acceptable.
Definition bignum.h:62
int mpi_grow(mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mpi_set_bit(mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.
uint32_t t_uint
Definition bignum.h:160
int mpi_shrink(mpi *X, size_t nblimbs)
Resize down, keeping at least the specified number of limbs.
int mpi_is_prime(mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
void mpi_free(mpi *X)
Unallocate one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
#define POLARSSL_ERR_MPI_INVALID_CHARACTER
There is an invalid character in the digit string.
Definition bignum.h:58
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE
The input arguments are negative or result in illegal output.
Definition bignum.h:60
Configuration options (set of defines)
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.
MPI structure.
Definition bignum.h:183
t_uint * p
Definition bignum.h:186
size_t n
Definition bignum.h:185
int s
Definition bignum.h:184
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.