1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Portions of the attached software ("Contribution") are developed by
61 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 * The Contribution is licensed pursuant to the Eric Young open source
64 * license provided above.
66 * The binary polynomial arithmetic software is originally written by
67 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
77 #include <openssl/bio.h>
78 #include <openssl/bn.h>
79 #include <openssl/rand.h>
80 #include <openssl/x509.h>
81 #include <openssl/err.h>
83 #include "../crypto/bn/bn_lcl.h"
85 static const int num0 = 100; /* number of tests */
86 static const int num1 = 50; /* additional tests for some functions */
87 static const int num2 = 5; /* number of tests for slow functions */
89 int test_add(BIO *bp);
90 int test_sub(BIO *bp);
91 int test_lshift1(BIO *bp);
92 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
93 int test_rshift1(BIO *bp);
94 int test_rshift(BIO *bp, BN_CTX *ctx);
95 int test_div(BIO *bp, BN_CTX *ctx);
96 int test_div_word(BIO *bp);
97 int test_div_recp(BIO *bp, BN_CTX *ctx);
98 int test_mul(BIO *bp);
99 int test_sqr(BIO *bp, BN_CTX *ctx);
100 int test_mont(BIO *bp, BN_CTX *ctx);
101 int test_mod(BIO *bp, BN_CTX *ctx);
102 int test_mod_mul(BIO *bp, BN_CTX *ctx);
103 int test_mod_exp(BIO *bp, BN_CTX *ctx);
104 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
105 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
106 int test_exp(BIO *bp, BN_CTX *ctx);
107 int test_gf2m_add(BIO *bp);
108 int test_gf2m_mod(BIO *bp);
109 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
110 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
111 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
112 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx);
113 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx);
114 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx);
115 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx);
116 int test_kron(BIO *bp, BN_CTX *ctx);
117 int test_sqrt(BIO *bp, BN_CTX *ctx);
118 int test_small_prime(BIO *bp, BN_CTX *ctx);
120 static int results = 0;
122 static unsigned char lst[] =
123 "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
124 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
126 static const char rnd_seed[] =
127 "string to make the random number generator think it has entropy";
129 static void message(BIO *out, char *m)
131 fprintf(stderr, "test %s\n", m);
132 BIO_puts(out, "print \"test ");
134 BIO_puts(out, "\\n\"\n");
137 int main(int argc, char *argv[])
141 char *outfile = NULL;
145 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
150 if (strcmp(*argv, "-results") == 0)
152 else if (strcmp(*argv, "-out") == 0) {
165 out = BIO_new(BIO_s_file());
168 if (outfile == NULL) {
169 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
171 if (!BIO_write_filename(out, outfile)) {
176 #ifdef OPENSSL_SYS_VMS
178 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
179 out = BIO_push(tmpbio, out);
184 BIO_puts(out, "obase=16\nibase=16\n");
186 message(out, "BN_add");
189 (void)BIO_flush(out);
191 message(out, "BN_sub");
194 (void)BIO_flush(out);
196 message(out, "BN_lshift1");
197 if (!test_lshift1(out))
199 (void)BIO_flush(out);
201 message(out, "BN_lshift (fixed)");
202 if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
204 (void)BIO_flush(out);
206 message(out, "BN_lshift");
207 if (!test_lshift(out, ctx, NULL))
209 (void)BIO_flush(out);
211 message(out, "BN_rshift1");
212 if (!test_rshift1(out))
214 (void)BIO_flush(out);
216 message(out, "BN_rshift");
217 if (!test_rshift(out, ctx))
219 (void)BIO_flush(out);
221 message(out, "BN_sqr");
222 if (!test_sqr(out, ctx))
224 (void)BIO_flush(out);
226 message(out, "BN_mul");
229 (void)BIO_flush(out);
231 message(out, "BN_div");
232 if (!test_div(out, ctx))
234 (void)BIO_flush(out);
236 message(out, "BN_div_word");
237 if (!test_div_word(out))
239 (void)BIO_flush(out);
241 message(out, "BN_div_recp");
242 if (!test_div_recp(out, ctx))
244 (void)BIO_flush(out);
246 message(out, "BN_mod");
247 if (!test_mod(out, ctx))
249 (void)BIO_flush(out);
251 message(out, "BN_mod_mul");
252 if (!test_mod_mul(out, ctx))
254 (void)BIO_flush(out);
256 message(out, "BN_mont");
257 if (!test_mont(out, ctx))
259 (void)BIO_flush(out);
261 message(out, "BN_mod_exp");
262 if (!test_mod_exp(out, ctx))
264 (void)BIO_flush(out);
266 message(out, "BN_mod_exp_mont_consttime");
267 if (!test_mod_exp_mont_consttime(out, ctx))
269 if (!test_mod_exp_mont5(out, ctx))
271 (void)BIO_flush(out);
273 message(out, "BN_exp");
274 if (!test_exp(out, ctx))
276 (void)BIO_flush(out);
278 message(out, "BN_kronecker");
279 if (!test_kron(out, ctx))
281 (void)BIO_flush(out);
283 message(out, "BN_mod_sqrt");
284 if (!test_sqrt(out, ctx))
286 (void)BIO_flush(out);
288 message(out, "Small prime generation");
289 if (!test_small_prime(out, ctx))
291 (void)BIO_flush(out);
293 #ifndef OPENSSL_NO_EC2M
294 message(out, "BN_GF2m_add");
295 if (!test_gf2m_add(out))
297 (void)BIO_flush(out);
299 message(out, "BN_GF2m_mod");
300 if (!test_gf2m_mod(out))
302 (void)BIO_flush(out);
304 message(out, "BN_GF2m_mod_mul");
305 if (!test_gf2m_mod_mul(out, ctx))
307 (void)BIO_flush(out);
309 message(out, "BN_GF2m_mod_sqr");
310 if (!test_gf2m_mod_sqr(out, ctx))
312 (void)BIO_flush(out);
314 message(out, "BN_GF2m_mod_inv");
315 if (!test_gf2m_mod_inv(out, ctx))
317 (void)BIO_flush(out);
319 message(out, "BN_GF2m_mod_div");
320 if (!test_gf2m_mod_div(out, ctx))
322 (void)BIO_flush(out);
324 message(out, "BN_GF2m_mod_exp");
325 if (!test_gf2m_mod_exp(out, ctx))
327 (void)BIO_flush(out);
329 message(out, "BN_GF2m_mod_sqrt");
330 if (!test_gf2m_mod_sqrt(out, ctx))
332 (void)BIO_flush(out);
334 message(out, "BN_GF2m_mod_solve_quad");
335 if (!test_gf2m_mod_solve_quad(out, ctx))
337 (void)BIO_flush(out);
344 BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc
345 * notices the failure, see test_bn in
346 * test/Makefile.ssl */
347 (void)BIO_flush(out);
348 ERR_load_crypto_strings();
349 ERR_print_errors_fp(stderr);
353 int test_add(BIO *bp)
362 BN_bntest_rand(a, 512, 0, 0);
363 for (i = 0; i < num0; i++) {
364 BN_bntest_rand(b, 450 + i, 0, 0);
382 if (!BN_is_zero(c)) {
383 fprintf(stderr, "Add test failed!\n");
393 int test_sub(BIO *bp)
402 for (i = 0; i < num0 + num1; i++) {
404 BN_bntest_rand(a, 512, 0, 0);
406 if (BN_set_bit(a, i) == 0)
410 BN_bntest_rand(b, 400 + i - num1, 0, 0);
427 if (!BN_is_zero(c)) {
428 fprintf(stderr, "Subtract test failed!\n");
438 int test_div(BIO *bp, BN_CTX *ctx)
440 BIGNUM *a, *b, *c, *d, *e;
452 if (BN_div(d, c, a, b, ctx)) {
453 fprintf(stderr, "Division by zero succeeded!\n");
457 for (i = 0; i < num0 + num1; i++) {
459 BN_bntest_rand(a, 400, 0, 0);
464 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
467 BN_div(d, c, a, b, ctx);
487 BN_mul(e, d, b, ctx);
490 if (!BN_is_zero(d)) {
491 fprintf(stderr, "Division test failed!\n");
503 static void print_word(BIO *bp, BN_ULONG w)
505 #ifdef SIXTY_FOUR_BIT
506 if (sizeof(w) > sizeof(unsigned long)) {
507 unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
510 BIO_printf(bp, "%lX%08lX", h, l);
512 BIO_printf(bp, "%lX", l);
516 BIO_printf(bp, BN_HEX_FMT1, w);
519 int test_div_word(BIO *bp)
528 for (i = 0; i < num0; i++) {
530 BN_bntest_rand(a, 512, -1, 0);
531 BN_bntest_rand(b, BN_BITS2, -1, 0);
532 } while (BN_is_zero(b));
536 r = BN_div_word(b, s);
560 if (!BN_is_zero(b)) {
561 fprintf(stderr, "Division (word) test failed!\n");
570 int test_div_recp(BIO *bp, BN_CTX *ctx)
572 BIGNUM *a, *b, *c, *d, *e;
576 recp = BN_RECP_CTX_new();
583 for (i = 0; i < num0 + num1; i++) {
585 BN_bntest_rand(a, 400, 0, 0);
590 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
593 BN_RECP_CTX_set(recp, b, ctx);
594 BN_div_recp(d, c, a, recp, ctx);
614 BN_mul(e, d, b, ctx);
617 if (!BN_is_zero(d)) {
618 fprintf(stderr, "Reciprocal division test failed!\n");
619 fprintf(stderr, "a=");
620 BN_print_fp(stderr, a);
621 fprintf(stderr, "\nb=");
622 BN_print_fp(stderr, b);
623 fprintf(stderr, "\n");
632 BN_RECP_CTX_free(recp);
636 int test_mul(BIO *bp)
638 BIGNUM *a, *b, *c, *d, *e;
652 for (i = 0; i < num0 + num1; i++) {
654 BN_bntest_rand(a, 100, 0, 0);
655 BN_bntest_rand(b, 100, 0, 0);
657 BN_bntest_rand(b, i - num1, 0, 0);
660 BN_mul(c, a, b, ctx);
671 BN_div(d, e, c, a, ctx);
673 if (!BN_is_zero(d) || !BN_is_zero(e)) {
674 fprintf(stderr, "Multiplication test failed!\n");
687 int test_sqr(BIO *bp, BN_CTX *ctx)
689 BIGNUM *a, *c, *d, *e;
696 if (a == NULL || c == NULL || d == NULL || e == NULL) {
700 for (i = 0; i < num0; i++) {
701 BN_bntest_rand(a, 40 + i * 10, 0, 0);
714 BN_div(d, e, c, a, ctx);
716 if (!BN_is_zero(d) || !BN_is_zero(e)) {
717 fprintf(stderr, "Square test failed!\n");
722 /* Regression test for a BN_sqr overflow bug. */
724 "80000000000000008000000000000001"
725 "FFFFFFFFFFFFFFFE0000000000000000");
737 BN_mul(d, a, a, ctx);
739 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
740 "different results!\n");
744 /* Regression test for a BN_sqr overflow bug. */
746 "80000000000000000000000080000001"
747 "FFFFFFFE000000000000000000000000");
759 BN_mul(d, a, a, ctx);
761 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
762 "different results!\n");
774 int test_mont(BIO *bp, BN_CTX *ctx)
776 BIGNUM *a, *b, *c, *d, *A, *B;
789 mont = BN_MONT_CTX_new();
794 if (BN_MONT_CTX_set(mont, n, ctx)) {
795 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
800 if (BN_MONT_CTX_set(mont, n, ctx)) {
801 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
805 BN_bntest_rand(a, 100, 0, 0);
806 BN_bntest_rand(b, 100, 0, 0);
807 for (i = 0; i < num2; i++) {
808 int bits = (200 * (i + 1)) / num2;
812 BN_bntest_rand(n, bits, 0, 1);
813 BN_MONT_CTX_set(mont, n, ctx);
815 BN_nnmod(a, a, n, ctx);
816 BN_nnmod(b, b, n, ctx);
818 BN_to_montgomery(A, a, mont, ctx);
819 BN_to_montgomery(B, b, mont, ctx);
821 BN_mod_mul_montgomery(c, A, B, mont, ctx);
822 BN_from_montgomery(A, c, mont, ctx);
829 BN_print(bp, &mont->N);
835 BN_mod_mul(d, a, b, n, ctx);
837 if (!BN_is_zero(d)) {
838 fprintf(stderr, "Montgomery multiplication test failed!\n");
842 BN_MONT_CTX_free(mont);
853 int test_mod(BIO *bp, BN_CTX *ctx)
855 BIGNUM *a, *b, *c, *d, *e;
864 BN_bntest_rand(a, 1024, 0, 0);
865 for (i = 0; i < num0; i++) {
866 BN_bntest_rand(b, 450 + i * 10, 0, 0);
869 BN_mod(c, a, b, ctx);
880 BN_div(d, e, a, b, ctx);
882 if (!BN_is_zero(e)) {
883 fprintf(stderr, "Modulo test failed!\n");
895 int test_mod_mul(BIO *bp, BN_CTX *ctx)
897 BIGNUM *a, *b, *c, *d, *e;
909 if (BN_mod_mul(e, a, b, c, ctx)) {
910 fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
914 for (j = 0; j < 3; j++) {
915 BN_bntest_rand(c, 1024, 0, 0);
916 for (i = 0; i < num0; i++) {
917 BN_bntest_rand(a, 475 + i * 10, 0, 0);
918 BN_bntest_rand(b, 425 + i * 11, 0, 0);
921 if (!BN_mod_mul(e, a, b, c, ctx)) {
924 while ((l = ERR_get_error()))
925 fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
935 if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
937 * If (a*b) % c is negative, c must be added in order
938 * to obtain the normalized remainder (new with
939 * OpenSSL 0.9.7, previous versions of BN_mod_mul
940 * could generate negative results)
950 BN_mul(d, a, b, ctx);
952 BN_div(a, b, d, c, ctx);
953 if (!BN_is_zero(b)) {
954 fprintf(stderr, "Modulo multiply test failed!\n");
955 ERR_print_errors_fp(stderr);
968 int test_mod_exp(BIO *bp, BN_CTX *ctx)
970 BIGNUM *a, *b, *c, *d, *e;
982 if (BN_mod_exp(d, a, b, c, ctx)) {
983 fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
987 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
988 for (i = 0; i < num2; i++) {
989 BN_bntest_rand(a, 20 + i * 5, 0, 0);
990 BN_bntest_rand(b, 2 + i, 0, 0);
992 if (!BN_mod_exp(d, a, b, c, ctx))
1000 BIO_puts(bp, " % ");
1002 BIO_puts(bp, " - ");
1007 BN_exp(e, a, b, ctx);
1009 BN_div(a, b, e, c, ctx);
1010 if (!BN_is_zero(b)) {
1011 fprintf(stderr, "Modulo exponentiation test failed!\n");
1016 /* Regression test for carry propagation bug in sqr8x_reduction */
1017 BN_hex2bn(&a, "050505050505");
1018 BN_hex2bn(&b, "02");
1020 "4141414141414141414141274141414141414141414141414141414141414141"
1021 "4141414141414141414141414141414141414141414141414141414141414141"
1022 "4141414141414141414141800000000000000000000000000000000000000000"
1023 "0000000000000000000000000000000000000000000000000000000000000000"
1024 "0000000000000000000000000000000000000000000000000000000000000000"
1025 "0000000000000000000000000000000000000000000000000000000001");
1026 BN_mod_exp(d, a, b, c, ctx);
1027 BN_mul(e, a, a, ctx);
1029 fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
1041 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1043 BIGNUM *a, *b, *c, *d, *e;
1055 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1056 fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
1062 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1063 fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
1068 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1069 for (i = 0; i < num2; i++) {
1070 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1071 BN_bntest_rand(b, 2 + i, 0, 0);
1073 if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
1079 BIO_puts(bp, " ^ ");
1081 BIO_puts(bp, " % ");
1083 BIO_puts(bp, " - ");
1088 BN_exp(e, a, b, ctx);
1090 BN_div(a, b, e, c, ctx);
1091 if (!BN_is_zero(b)) {
1092 fprintf(stderr, "Modulo exponentiation test failed!\n");
1105 * Test constant-time modular exponentiation with 1024-bit inputs, which on
1106 * x86_64 cause a different code branch to be taken.
1108 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1110 BIGNUM *a, *p, *m, *d, *e;
1118 mont = BN_MONT_CTX_new();
1120 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
1122 BN_bntest_rand(a, 1024, 0, 0);
1124 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1126 if (!BN_is_one(d)) {
1127 fprintf(stderr, "Modular exponentiation test failed!\n");
1131 BN_bntest_rand(p, 1024, 0, 0);
1133 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1135 if (!BN_is_zero(d)) {
1136 fprintf(stderr, "Modular exponentiation test failed!\n");
1140 * Craft an input whose Montgomery representation is 1, i.e., shorter
1141 * than the modulus m, in order to test the const time precomputation
1142 * scattering/gathering.
1145 BN_MONT_CTX_set(mont, m, ctx);
1146 if (!BN_from_montgomery(e, a, mont, ctx))
1148 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1150 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1152 if (BN_cmp(a, d) != 0) {
1153 fprintf(stderr, "Modular exponentiation test failed!\n");
1156 /* Finally, some regular test vectors. */
1157 BN_bntest_rand(e, 1024, 0, 0);
1158 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1160 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1162 if (BN_cmp(a, d) != 0) {
1163 fprintf(stderr, "Modular exponentiation test failed!\n");
1166 BN_MONT_CTX_free(mont);
1175 int test_exp(BIO *bp, BN_CTX *ctx)
1177 BIGNUM *a, *b, *d, *e, *one;
1187 for (i = 0; i < num2; i++) {
1188 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1189 BN_bntest_rand(b, 2 + i, 0, 0);
1191 if (BN_exp(d, a, b, ctx) <= 0)
1197 BIO_puts(bp, " ^ ");
1199 BIO_puts(bp, " - ");
1205 for (; !BN_is_zero(b); BN_sub(b, b, one))
1206 BN_mul(e, e, a, ctx);
1208 if (!BN_is_zero(e)) {
1209 fprintf(stderr, "Exponentiation test failed!\n");
1221 #ifndef OPENSSL_NO_EC2M
1222 int test_gf2m_add(BIO *bp)
1231 for (i = 0; i < num0; i++) {
1232 BN_rand(a, 512, 0, 0);
1233 BN_copy(b, BN_value_one());
1234 a->neg = rand_neg();
1235 b->neg = rand_neg();
1236 BN_GF2m_add(c, a, b);
1237 /* Test that two added values have the correct parity. */
1238 if ((BN_is_odd(a) && BN_is_odd(c))
1239 || (!BN_is_odd(a) && !BN_is_odd(c))) {
1240 fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1243 BN_GF2m_add(c, c, c);
1244 /* Test that c + c = 0. */
1245 if (!BN_is_zero(c)) {
1246 fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1258 int test_gf2m_mod(BIO *bp)
1260 BIGNUM *a, *b[2], *c, *d, *e;
1262 int p0[] = { 163, 7, 6, 3, 0, -1 };
1263 int p1[] = { 193, 15, 0, -1 };
1272 BN_GF2m_arr2poly(p0, b[0]);
1273 BN_GF2m_arr2poly(p1, b[1]);
1275 for (i = 0; i < num0; i++) {
1276 BN_bntest_rand(a, 1024, 0, 0);
1277 for (j = 0; j < 2; j++) {
1278 BN_GF2m_mod(c, a, b[j]);
1279 BN_GF2m_add(d, a, c);
1280 BN_GF2m_mod(e, d, b[j]);
1281 /* Test that a + (a mod p) mod p == 0. */
1282 if (!BN_is_zero(e)) {
1283 fprintf(stderr, "GF(2^m) modulo test failed!\n");
1299 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1301 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
1303 int p0[] = { 163, 7, 6, 3, 0, -1 };
1304 int p1[] = { 193, 15, 0, -1 };
1316 BN_GF2m_arr2poly(p0, b[0]);
1317 BN_GF2m_arr2poly(p1, b[1]);
1319 for (i = 0; i < num0; i++) {
1320 BN_bntest_rand(a, 1024, 0, 0);
1321 BN_bntest_rand(c, 1024, 0, 0);
1322 BN_bntest_rand(d, 1024, 0, 0);
1323 for (j = 0; j < 2; j++) {
1324 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1325 BN_GF2m_add(f, a, d);
1326 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1327 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1328 BN_GF2m_add(f, e, g);
1329 BN_GF2m_add(f, f, h);
1330 /* Test that (a+d)*c = a*c + d*c. */
1331 if (!BN_is_zero(f)) {
1333 "GF(2^m) modular multiplication test failed!\n");
1352 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1354 BIGNUM *a, *b[2], *c, *d;
1356 int p0[] = { 163, 7, 6, 3, 0, -1 };
1357 int p1[] = { 193, 15, 0, -1 };
1365 BN_GF2m_arr2poly(p0, b[0]);
1366 BN_GF2m_arr2poly(p1, b[1]);
1368 for (i = 0; i < num0; i++) {
1369 BN_bntest_rand(a, 1024, 0, 0);
1370 for (j = 0; j < 2; j++) {
1371 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1373 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1374 BN_GF2m_add(d, c, d);
1375 /* Test that a*a = a^2. */
1376 if (!BN_is_zero(d)) {
1377 fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
1392 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1394 BIGNUM *a, *b[2], *c, *d;
1396 int p0[] = { 163, 7, 6, 3, 0, -1 };
1397 int p1[] = { 193, 15, 0, -1 };
1405 BN_GF2m_arr2poly(p0, b[0]);
1406 BN_GF2m_arr2poly(p1, b[1]);
1408 for (i = 0; i < num0; i++) {
1409 BN_bntest_rand(a, 512, 0, 0);
1410 for (j = 0; j < 2; j++) {
1411 BN_GF2m_mod_inv(c, a, b[j], ctx);
1412 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1413 /* Test that ((1/a)*a) = 1. */
1414 if (!BN_is_one(d)) {
1415 fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
1430 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1432 BIGNUM *a, *b[2], *c, *d, *e, *f;
1434 int p0[] = { 163, 7, 6, 3, 0, -1 };
1435 int p1[] = { 193, 15, 0, -1 };
1445 BN_GF2m_arr2poly(p0, b[0]);
1446 BN_GF2m_arr2poly(p1, b[1]);
1448 for (i = 0; i < num0; i++) {
1449 BN_bntest_rand(a, 512, 0, 0);
1450 BN_bntest_rand(c, 512, 0, 0);
1451 for (j = 0; j < 2; j++) {
1452 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1453 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1454 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1455 /* Test that ((a/c)*c)/a = 1. */
1456 if (!BN_is_one(f)) {
1457 fprintf(stderr, "GF(2^m) modular division test failed!\n");
1474 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1476 BIGNUM *a, *b[2], *c, *d, *e, *f;
1478 int p0[] = { 163, 7, 6, 3, 0, -1 };
1479 int p1[] = { 193, 15, 0, -1 };
1489 BN_GF2m_arr2poly(p0, b[0]);
1490 BN_GF2m_arr2poly(p1, b[1]);
1492 for (i = 0; i < num0; i++) {
1493 BN_bntest_rand(a, 512, 0, 0);
1494 BN_bntest_rand(c, 512, 0, 0);
1495 BN_bntest_rand(d, 512, 0, 0);
1496 for (j = 0; j < 2; j++) {
1497 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1498 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1499 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1501 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1502 BN_GF2m_add(f, e, f);
1503 /* Test that a^(c+d)=a^c*a^d. */
1504 if (!BN_is_zero(f)) {
1506 "GF(2^m) modular exponentiation test failed!\n");
1523 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1525 BIGNUM *a, *b[2], *c, *d, *e, *f;
1527 int p0[] = { 163, 7, 6, 3, 0, -1 };
1528 int p1[] = { 193, 15, 0, -1 };
1538 BN_GF2m_arr2poly(p0, b[0]);
1539 BN_GF2m_arr2poly(p1, b[1]);
1541 for (i = 0; i < num0; i++) {
1542 BN_bntest_rand(a, 512, 0, 0);
1543 for (j = 0; j < 2; j++) {
1544 BN_GF2m_mod(c, a, b[j]);
1545 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1546 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1547 BN_GF2m_add(f, c, e);
1548 /* Test that d^2 = a, where d = sqrt(a). */
1549 if (!BN_is_zero(f)) {
1550 fprintf(stderr, "GF(2^m) modular square root test failed!\n");
1567 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1569 BIGNUM *a, *b[2], *c, *d, *e;
1570 int i, j, s = 0, t, ret = 0;
1571 int p0[] = { 163, 7, 6, 3, 0, -1 };
1572 int p1[] = { 193, 15, 0, -1 };
1581 BN_GF2m_arr2poly(p0, b[0]);
1582 BN_GF2m_arr2poly(p1, b[1]);
1584 for (i = 0; i < num0; i++) {
1585 BN_bntest_rand(a, 512, 0, 0);
1586 for (j = 0; j < 2; j++) {
1587 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1590 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1591 BN_GF2m_add(d, c, d);
1592 BN_GF2m_mod(e, a, b[j]);
1593 BN_GF2m_add(e, e, d);
1595 * Test that solution of quadratic c satisfies c^2 + c = a.
1597 if (!BN_is_zero(e)) {
1599 "GF(2^m) modular solve quadratic test failed!\n");
1608 "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
1611 "this is very unlikely and probably indicates an error.\n");
1625 static int genprime_cb(int p, int n, BN_GENCB *arg)
1642 int test_kron(BIO *bp, BN_CTX *ctx)
1645 BIGNUM *a, *b, *r, *t;
1647 int legendre, kronecker;
1654 if (a == NULL || b == NULL || r == NULL || t == NULL)
1657 BN_GENCB_set(&cb, genprime_cb, NULL);
1660 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1661 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1662 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1663 * generate a random prime b and compare these values for a number of
1664 * random a's. (That is, we run the Solovay-Strassen primality test to
1665 * confirm that b is prime, except that we don't want to test whether b
1666 * is prime but whether BN_kronecker works.)
1669 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
1671 b->neg = rand_neg();
1674 for (i = 0; i < num0; i++) {
1675 if (!BN_bntest_rand(a, 512, 0, 0))
1677 a->neg = rand_neg();
1679 /* t := (|b|-1)/2 (note that b is odd) */
1683 if (!BN_sub_word(t, 1))
1685 if (!BN_rshift1(t, t))
1687 /* r := a^t mod b */
1690 if (!BN_mod_exp_recp(r, a, t, b, ctx))
1694 if (BN_is_word(r, 1))
1696 else if (BN_is_zero(r))
1699 if (!BN_add_word(r, 1))
1701 if (0 != BN_ucmp(r, b)) {
1702 fprintf(stderr, "Legendre symbol computation failed\n");
1708 kronecker = BN_kronecker(a, b, ctx);
1711 /* we actually need BN_kronecker(a, |b|) */
1712 if (a->neg && b->neg)
1713 kronecker = -kronecker;
1715 if (legendre != kronecker) {
1716 fprintf(stderr, "legendre != kronecker; a = ");
1717 BN_print_fp(stderr, a);
1718 fprintf(stderr, ", b = ");
1719 BN_print_fp(stderr, b);
1720 fprintf(stderr, "\n");
1739 int test_sqrt(BIO *bp, BN_CTX *ctx)
1749 if (a == NULL || p == NULL || r == NULL)
1752 BN_GENCB_set(&cb, genprime_cb, NULL);
1754 for (i = 0; i < 16; i++) {
1756 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1758 if (!BN_set_word(p, primes[i]))
1761 if (!BN_set_word(a, 32))
1763 if (!BN_set_word(r, 2 * i + 1))
1766 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
1770 p->neg = rand_neg();
1772 for (j = 0; j < num2; j++) {
1774 * construct 'a' such that it is a square modulo p, but in
1775 * general not a proper square and not reduced modulo p
1777 if (!BN_bntest_rand(r, 256, 0, 3))
1779 if (!BN_nnmod(r, r, p, ctx))
1781 if (!BN_mod_sqr(r, r, p, ctx))
1783 if (!BN_bntest_rand(a, 256, 0, 3))
1785 if (!BN_nnmod(a, a, p, ctx))
1787 if (!BN_mod_sqr(a, a, p, ctx))
1789 if (!BN_mul(a, a, r, ctx))
1792 if (!BN_sub(a, a, p))
1795 if (!BN_mod_sqrt(r, a, p, ctx))
1797 if (!BN_mod_sqr(r, r, p, ctx))
1800 if (!BN_nnmod(a, a, p, ctx))
1803 if (BN_cmp(a, r) != 0) {
1804 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1805 BN_print_fp(stderr, a);
1806 fprintf(stderr, ", r = ");
1807 BN_print_fp(stderr, r);
1808 fprintf(stderr, ", p = ");
1809 BN_print_fp(stderr, p);
1810 fprintf(stderr, "\n");
1829 int test_small_prime(BIO *bp, BN_CTX *ctx)
1831 static const int bits = 10;
1836 if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
1838 if (BN_num_bits(r) != bits) {
1839 BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
1851 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
1853 BIGNUM *a, *b, *c, *d;
1865 BN_bntest_rand(a, 200, 0, 0);
1866 a->neg = rand_neg();
1868 for (i = 0; i < num0; i++) {
1869 BN_lshift(b, a, i + 1);
1874 BIO_puts(bp, " * ");
1876 BIO_puts(bp, " - ");
1881 BN_mul(d, a, c, ctx);
1883 if (!BN_is_zero(d)) {
1884 fprintf(stderr, "Left shift test failed!\n");
1885 fprintf(stderr, "a=");
1886 BN_print_fp(stderr, a);
1887 fprintf(stderr, "\nb=");
1888 BN_print_fp(stderr, b);
1889 fprintf(stderr, "\nc=");
1890 BN_print_fp(stderr, c);
1891 fprintf(stderr, "\nd=");
1892 BN_print_fp(stderr, d);
1893 fprintf(stderr, "\n");
1904 int test_lshift1(BIO *bp)
1913 BN_bntest_rand(a, 200, 0, 0);
1914 a->neg = rand_neg();
1915 for (i = 0; i < num0; i++) {
1920 BIO_puts(bp, " * 2");
1921 BIO_puts(bp, " - ");
1928 if (!BN_is_zero(a)) {
1929 fprintf(stderr, "Left shift one test failed!\n");
1941 int test_rshift(BIO *bp, BN_CTX *ctx)
1943 BIGNUM *a, *b, *c, *d, *e;
1953 BN_bntest_rand(a, 200, 0, 0);
1954 a->neg = rand_neg();
1955 for (i = 0; i < num0; i++) {
1956 BN_rshift(b, a, i + 1);
1961 BIO_puts(bp, " / ");
1963 BIO_puts(bp, " - ");
1968 BN_div(d, e, a, c, ctx);
1970 if (!BN_is_zero(d)) {
1971 fprintf(stderr, "Right shift test failed!\n");
1983 int test_rshift1(BIO *bp)
1992 BN_bntest_rand(a, 200, 0, 0);
1993 a->neg = rand_neg();
1994 for (i = 0; i < num0; i++) {
1999 BIO_puts(bp, " / 2");
2000 BIO_puts(bp, " - ");
2007 if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
2008 fprintf(stderr, "Right shift one test failed!\n");
2021 static unsigned int neg = 0;
2022 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
2024 return (sign[(neg++) % 8]);