]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/open_ssl/bn/bntest.c
2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/bn/bntest.c */
20 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
21 * All rights reserved.
23 * This package is an SSL implementation written
24 * by Eric Young (eay@cryptsoft.com).
25 * The implementation was written so as to conform with Netscapes SSL.
27 * This library is free for commercial and non-commercial use as long as
28 * the following conditions are aheared to. The following conditions
29 * apply to all code found in this distribution, be it the RC4, RSA,
30 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
31 * included with this distribution is covered by the same copyright terms
32 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
34 * Copyright remains Eric Young's, and as such any Copyright notices in
35 * the code are not to be removed.
36 * If this package is used in a product, Eric Young should be given attribution
37 * as the author of the parts of the library used.
38 * This can be in the form of a textual message at program startup or
39 * in documentation (online or textual) provided with the package.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * "This product includes cryptographic software written by
52 * Eric Young (eay@cryptsoft.com)"
53 * The word 'cryptographic' can be left out if the rouines from the library
54 * being used are not cryptographic related :-).
55 * 4. If you include any Windows specific code (or a derivative thereof) from
56 * the apps directory (application code) you must include an acknowledgement:
57 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
59 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * The licence and distribution terms for any publically available version or
72 * derivative of this code cannot be changed. i.e. this code cannot simply be
73 * copied and put under another distribution licence
74 * [including the GNU Public Licence.]
81 #include "openssl/e_os.h"
83 #include <openssl/bio_legacy.h>
84 #include <openssl/bn_legacy.h>
85 #include <openssl/rand.h>
86 #include <openssl/x509_legacy.h>
87 #include <openssl/opensslerr.h>
90 #include "../bio/bss_file.c"
93 const int num0
= 100; /* number of tests */
94 const int num1
= 50; /* additional tests for some functions */
95 const int num2
= 5; /* number of tests for slow functions */
97 int test_add(BIO
*bp
);
98 int test_sub(BIO
*bp
);
99 int test_lshift1(BIO
*bp
);
100 int test_lshift(BIO
*bp
,BN_CTX
*ctx
,BIGNUM
*a_
);
101 int test_rshift1(BIO
*bp
);
102 int test_rshift(BIO
*bp
,BN_CTX
*ctx
);
103 int test_div(BIO
*bp
,BN_CTX
*ctx
);
104 int test_div_recp(BIO
*bp
,BN_CTX
*ctx
);
105 int test_mul(BIO
*bp
);
106 int test_sqr(BIO
*bp
,BN_CTX
*ctx
);
107 int test_mont(BIO
*bp
,BN_CTX
*ctx
);
108 int test_mod(BIO
*bp
,BN_CTX
*ctx
);
109 int test_mod_mul(BIO
*bp
,BN_CTX
*ctx
);
110 int test_mod_exp(BIO
*bp
,BN_CTX
*ctx
);
111 int test_exp(BIO
*bp
,BN_CTX
*ctx
);
113 static int results
=0;
117 #include "bss_file.c"
120 static unsigned char lst
[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
121 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
123 static const char rnd_seed
[] = "string to make the random number generator think it has entropy";
125 static void message(BIO
*out
, char *m
)
127 fprintf(stderr
, "test %s\n", m
);
128 #if defined(linux) || defined(__FreeBSD__) /* can we use GNU bc features? */
129 BIO_puts(out
, "print \"test ");
131 BIO_puts(out
, "\\n\"\n");
135 int main(int argc
, char *argv
[])
143 RAND_seed(rnd_seed
, sizeof rnd_seed
); /* or BN_rand may fail, and we don't
144 * even check its return value
145 * (which we should) */
151 if (strcmp(*argv
,"-results") == 0)
153 else if (strcmp(*argv
,"-out") == 0)
155 if (--argc
< 1) break;
164 if (ctx
== NULL
) exit(1);
166 out
=BIO_new(BIO_s_file());
167 if (out
== NULL
) exit(1);
170 BIO_set_fp(out
,stdout
,BIO_NOCLOSE
);
174 if (!BIO_write_filename(out
,outfile
))
182 BIO_puts(out
,"obase=16\nibase=16\n");
184 message(out
,"BN_add");
185 if (!test_add(out
)) goto err
;
188 message(out
,"BN_sub");
189 if (!test_sub(out
)) goto err
;
192 message(out
,"BN_lshift1");
193 if (!test_lshift1(out
)) goto err
;
196 message(out
,"BN_lshift (fixed)");
197 if (!test_lshift(out
,ctx
,BN_bin2bn(lst
,sizeof(lst
)-1,NULL
)))
201 message(out
,"BN_lshift");
202 if (!test_lshift(out
,ctx
,NULL
)) goto err
;
205 message(out
,"BN_rshift1");
206 if (!test_rshift1(out
)) goto err
;
209 message(out
,"BN_rshift");
210 if (!test_rshift(out
,ctx
)) goto err
;
213 message(out
,"BN_sqr");
214 if (!test_sqr(out
,ctx
)) goto err
;
217 message(out
,"BN_mul");
218 if (!test_mul(out
)) goto err
;
221 message(out
,"BN_div");
222 if (!test_div(out
,ctx
)) goto err
;
225 message(out
,"BN_div_recp");
226 if (!test_div_recp(out
,ctx
)) goto err
;
229 message(out
,"BN_mod");
230 if (!test_mod(out
,ctx
)) goto err
;
233 message(out
,"BN_mod_mul");
234 if (!test_mod_mul(out
,ctx
)) goto err
;
237 message(out
,"BN_mont");
238 if (!test_mont(out
,ctx
)) goto err
;
241 message(out
,"BN_mod_exp");
242 if (!test_mod_exp(out
,ctx
)) goto err
;
245 message(out
,"BN_exp");
246 if (!test_exp(out
,ctx
)) goto err
;
255 BIO_puts(out
,"1\n"); /* make sure the Perl script fed by bc notices
256 * the failure, see test_bn in test/Makefile.ssl*/
258 ERR_load_crypto_strings();
259 ERR_print_errors_fp(stderr
);
264 int test_add(BIO
*bp
)
275 for (i
=0; i
<num0
; i
++)
277 BN_rand(&b
,450+i
,0,0);
281 for (j
=0; j
<10000; j
++)
302 fprintf(stderr
,"Add test failed!\n");
312 int test_sub(BIO
*bp
)
322 for (i
=0; i
<num0
+num1
; i
++)
328 if (BN_set_bit(&a
,i
)==0) return(0);
333 BN_rand(&b
,400+i
-num1
,0,0);
338 for (j
=0; j
<10000; j
++)
357 fprintf(stderr
,"Subtract test failed!\n");
367 int test_div(BIO
*bp
, BN_CTX
*ctx
)
379 for (i
=0; i
<num0
+num1
; i
++)
389 BN_rand(&b
,50+3*(i
-num1
),0,0);
393 for (j
=0; j
<100; j
++)
394 BN_div(&d
,&c
,&a
,&b
,ctx
);
395 BN_div(&d
,&c
,&a
,&b
,ctx
);
418 BN_mul(&e
,&d
,&b
,ctx
);
423 fprintf(stderr
,"Division test failed!\n");
435 int test_div_recp(BIO
*bp
, BN_CTX
*ctx
)
442 BN_RECP_CTX_init(&recp
);
449 for (i
=0; i
<num0
+num1
; i
++)
459 BN_rand(&b
,50+3*(i
-num1
),0,0);
462 BN_RECP_CTX_set(&recp
,&b
,ctx
);
464 for (j
=0; j
<100; j
++)
465 BN_div_recp(&d
,&c
,&a
,&recp
,ctx
);
466 BN_div_recp(&d
,&c
,&a
,&recp
,ctx
);
489 BN_mul(&e
,&d
,&b
,ctx
);
494 fprintf(stderr
,"Reciprocal division test failed!\n");
495 fprintf(stderr
,"a=");
496 BN_print_fp(stderr
,&a
);
497 fprintf(stderr
,"\nb=");
498 BN_print_fp(stderr
,&b
);
499 fprintf(stderr
,"\n");
508 BN_RECP_CTX_free(&recp
);
512 int test_mul(BIO
*bp
)
526 for (i
=0; i
<num0
+num1
; i
++)
534 BN_rand(&b
,i
-num1
,0,0);
538 for (j
=0; j
<100; j
++)
539 BN_mul(&c
,&a
,&b
,&ctx
);
540 BN_mul(&c
,&a
,&b
,&ctx
);
553 BN_div(&d
,&e
,&c
,&a
,&ctx
);
555 if(!BN_is_zero(&d
) || !BN_is_zero(&e
))
557 fprintf(stderr
,"Multiplication test failed!\n");
570 int test_sqr(BIO
*bp
, BN_CTX
*ctx
)
581 for (i
=0; i
<num0
; i
++)
583 BN_rand(&a
,40+i
*10,0,0);
586 for (j
=0; j
<100; j
++)
601 BN_div(&d
,&e
,&c
,&a
,ctx
);
603 if(!BN_is_zero(&d
) || !BN_is_zero(&e
))
605 fprintf(stderr
,"Square test failed!\n");
616 int test_mont(BIO
*bp
, BN_CTX
*ctx
)
632 mont
=BN_MONT_CTX_new();
634 BN_rand(&a
,100,0,0); /**/
635 BN_rand(&b
,100,0,0); /**/
636 for (i
=0; i
<num2
; i
++)
638 int bits
= (200*(i
+1))/num2
;
642 BN_rand(&n
,bits
,0,1);
643 BN_MONT_CTX_set(mont
,&n
,ctx
);
645 BN_to_montgomery(&A
,&a
,mont
,ctx
);
646 BN_to_montgomery(&B
,&b
,mont
,ctx
);
649 for (j
=0; j
<100; j
++)
650 BN_mod_mul_montgomery(&c
,&A
,&B
,mont
,ctx
);/**/
651 BN_mod_mul_montgomery(&c
,&A
,&B
,mont
,ctx
);/**/
652 BN_from_montgomery(&A
,&c
,mont
,ctx
);/**/
658 fprintf(stderr
,"%d * %d %% %d\n",
661 BN_num_bits(mont
->N
));
667 BN_print(bp
,&(mont
->N
));
673 BN_mod_mul(&d
,&a
,&b
,&n
,ctx
);
677 fprintf(stderr
,"Montgomery multiplication test failed!\n");
681 BN_MONT_CTX_free(mont
);
692 int test_mod(BIO
*bp
, BN_CTX
*ctx
)
694 BIGNUM
*a
,*b
,*c
,*d
,*e
;
704 BN_rand(a
,1024,0,0); /**/
705 for (i
=0; i
<num0
; i
++)
707 BN_rand(b
,450+i
*10,0,0); /**/
711 for (j
=0; j
<100; j
++)
712 BN_mod(c
,a
,b
,ctx
);/**/
713 BN_mod(c
,a
,b
,ctx
);/**/
730 fprintf(stderr
,"Modulo test failed!\n");
742 int test_mod_mul(BIO
*bp
, BN_CTX
*ctx
)
744 BIGNUM
*a
,*b
,*c
,*d
,*e
;
753 BN_rand(c
,1024,0,0); /**/
754 for (i
=0; i
<num0
; i
++)
756 BN_rand(a
,475+i
*10,0,0); /**/
757 BN_rand(b
,425+i
*11,0,0); /**/
761 for (j=0; j<100; j++)
762 BN_mod_mul(d,a,b,c,ctx);*/ /**/
764 if (!BN_mod_mul(e
,a
,b
,c
,ctx
))
768 while ((l
=ERR_get_error()))
769 fprintf(stderr
,"ERROR:%s\n",
770 ERR_error_string(l
,NULL
));
792 fprintf(stderr
,"Modulo multiply test failed!\n");
804 int test_mod_exp(BIO
*bp
, BN_CTX
*ctx
)
806 BIGNUM
*a
,*b
,*c
,*d
,*e
;
815 BN_rand(c
,30,0,1); /* must be odd for montgomery */
816 for (i
=0; i
<num2
; i
++)
818 BN_rand(a
,20+i
*5,0,0); /**/
819 BN_rand(b
,2+i
,0,0); /**/
821 if (!BN_mod_exp(d
,a
,b
,c
,ctx
))
843 fprintf(stderr
,"Modulo exponentiation test failed!\n");
855 int test_exp(BIO
*bp
, BN_CTX
*ctx
)
857 BIGNUM
*a
,*b
,*d
,*e
,*one
;
867 for (i
=0; i
<num2
; i
++)
869 BN_rand(a
,20+i
*5,0,0); /**/
870 BN_rand(b
,2+i
,0,0); /**/
872 if (!BN_exp(d
,a
,b
,ctx
))
888 for( ; !BN_is_zero(b
) ; BN_sub(b
,b
,one
))
893 fprintf(stderr
,"Exponentiation test failed!\n");
905 int test_lshift(BIO
*bp
,BN_CTX
*ctx
,BIGNUM
*a_
)
920 BN_rand(a
,200,0,0); /**/
923 for (i
=0; i
<num0
; i
++)
943 fprintf(stderr
,"Left shift test failed!\n");
944 fprintf(stderr
,"a=");
945 BN_print_fp(stderr
,a
);
946 fprintf(stderr
,"\nb=");
947 BN_print_fp(stderr
,b
);
948 fprintf(stderr
,"\nc=");
949 BN_print_fp(stderr
,c
);
950 fprintf(stderr
,"\nd=");
951 BN_print_fp(stderr
,d
);
952 fprintf(stderr
,"\n");
963 int test_lshift1(BIO
*bp
)
972 BN_rand(a
,200,0,0); /**/
974 for (i
=0; i
<num0
; i
++)
992 fprintf(stderr
,"Left shift one test failed!\n");
1004 int test_rshift(BIO
*bp
,BN_CTX
*ctx
)
1006 BIGNUM
*a
,*b
,*c
,*d
,*e
;
1016 BN_rand(a
,200,0,0); /**/
1018 for (i
=0; i
<num0
; i
++)
1034 BN_div(d
,e
,a
,c
,ctx
);
1038 fprintf(stderr
,"Right shift test failed!\n");
1050 int test_rshift1(BIO
*bp
)
1059 BN_rand(a
,200,0,0); /**/
1061 for (i
=0; i
<num0
; i
++)
1069 BIO_puts(bp
," / 2");
1077 if(!BN_is_zero(c
) && !BN_is_one(c
))
1079 fprintf(stderr
,"Right shift one test failed!\n");
1092 static unsigned int neg
=0;
1093 static int sign
[8]={0,0,0,1,1,0,1,1};
1095 return(sign
[(neg
++)%8
]);