2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the Eric Young open source
17 * license provided above.
19 * The binary polynomial arithmetic software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
30 #include <openssl/bio.h>
31 #include <openssl/bn.h>
32 #include <openssl/rand.h>
33 #include <openssl/x509.h>
34 #include <openssl/err.h>
37 * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
38 * This is fine in itself, it will end up as an unused static function in
39 * the worst case. However, it referenses bn_expand2(), which is a private
40 * function in libcrypto and therefore unavailable on some systems. This
41 * may result in a linker error because of unresolved symbols.
43 * To avoid this, we define a dummy variant of bn_expand2() here, and to
44 * avoid possible clashes with libcrypto, we rename it first, using a macro.
46 #define bn_expand2 dummy_bn_expand2
47 BIGNUM *bn_expand2(BIGNUM *b, int words);
48 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
50 #include "../crypto/bn/bn_lcl.h"
52 static const int num0 = 100; /* number of tests */
53 static const int num1 = 50; /* additional tests for some functions */
54 static const int num2 = 5; /* number of tests for slow functions */
56 int test_add(BIO *bp);
57 int test_sub(BIO *bp);
58 int test_lshift1(BIO *bp);
59 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
60 int test_rshift1(BIO *bp);
61 int test_rshift(BIO *bp, BN_CTX *ctx);
62 int test_div(BIO *bp, BN_CTX *ctx);
63 int test_div_word(BIO *bp);
64 int test_div_recp(BIO *bp, BN_CTX *ctx);
65 int test_mul(BIO *bp);
66 int test_sqr(BIO *bp, BN_CTX *ctx);
67 int test_mont(BIO *bp, BN_CTX *ctx);
68 int test_mod(BIO *bp, BN_CTX *ctx);
69 int test_mod_mul(BIO *bp, BN_CTX *ctx);
70 int test_mod_exp(BIO *bp, BN_CTX *ctx);
71 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
72 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
73 int test_exp(BIO *bp, BN_CTX *ctx);
74 int test_gf2m_add(BIO *bp);
75 int test_gf2m_mod(BIO *bp);
76 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
77 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
78 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
79 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx);
80 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx);
81 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx);
82 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx);
83 int test_kron(BIO *bp, BN_CTX *ctx);
84 int test_sqrt(BIO *bp, BN_CTX *ctx);
85 int test_small_prime(BIO *bp, BN_CTX *ctx);
87 static int results = 0;
89 static unsigned char lst[] =
90 "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
91 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
93 static const char rnd_seed[] =
94 "string to make the random number generator think it has entropy";
96 static void message(BIO *out, char *m)
98 fprintf(stderr, "test %s\n", m);
99 BIO_puts(out, "print \"test ");
101 BIO_puts(out, "\\n\"\n");
104 int main(int argc, char *argv[])
108 char *outfile = NULL;
110 CRYPTO_set_mem_debug(1);
111 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
115 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
120 if (strcmp(*argv, "-results") == 0)
122 else if (strcmp(*argv, "-out") == 0) {
135 out = BIO_new(BIO_s_file());
138 if (outfile == NULL) {
139 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
141 if (!BIO_write_filename(out, outfile)) {
146 #ifdef OPENSSL_SYS_VMS
148 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
149 out = BIO_push(tmpbio, out);
154 BIO_puts(out, "obase=16\nibase=16\n");
156 message(out, "BN_add");
159 (void)BIO_flush(out);
161 message(out, "BN_sub");
164 (void)BIO_flush(out);
166 message(out, "BN_lshift1");
167 if (!test_lshift1(out))
169 (void)BIO_flush(out);
171 message(out, "BN_lshift (fixed)");
172 if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
174 (void)BIO_flush(out);
176 message(out, "BN_lshift");
177 if (!test_lshift(out, ctx, NULL))
179 (void)BIO_flush(out);
181 message(out, "BN_rshift1");
182 if (!test_rshift1(out))
184 (void)BIO_flush(out);
186 message(out, "BN_rshift");
187 if (!test_rshift(out, ctx))
189 (void)BIO_flush(out);
191 message(out, "BN_sqr");
192 if (!test_sqr(out, ctx))
194 (void)BIO_flush(out);
196 message(out, "BN_mul");
199 (void)BIO_flush(out);
201 message(out, "BN_div");
202 if (!test_div(out, ctx))
204 (void)BIO_flush(out);
206 message(out, "BN_div_word");
207 if (!test_div_word(out))
209 (void)BIO_flush(out);
211 message(out, "BN_div_recp");
212 if (!test_div_recp(out, ctx))
214 (void)BIO_flush(out);
216 message(out, "BN_mod");
217 if (!test_mod(out, ctx))
219 (void)BIO_flush(out);
221 message(out, "BN_mod_mul");
222 if (!test_mod_mul(out, ctx))
224 (void)BIO_flush(out);
226 message(out, "BN_mont");
227 if (!test_mont(out, ctx))
229 (void)BIO_flush(out);
231 message(out, "BN_mod_exp");
232 if (!test_mod_exp(out, ctx))
234 (void)BIO_flush(out);
236 message(out, "BN_mod_exp_mont_consttime");
237 if (!test_mod_exp_mont_consttime(out, ctx))
239 if (!test_mod_exp_mont5(out, ctx))
241 (void)BIO_flush(out);
243 message(out, "BN_exp");
244 if (!test_exp(out, ctx))
246 (void)BIO_flush(out);
248 message(out, "BN_kronecker");
249 if (!test_kron(out, ctx))
251 (void)BIO_flush(out);
253 message(out, "BN_mod_sqrt");
254 if (!test_sqrt(out, ctx))
256 (void)BIO_flush(out);
258 message(out, "Small prime generation");
259 if (!test_small_prime(out, ctx))
261 (void)BIO_flush(out);
263 #ifndef OPENSSL_NO_EC2M
264 message(out, "BN_GF2m_add");
265 if (!test_gf2m_add(out))
267 (void)BIO_flush(out);
269 message(out, "BN_GF2m_mod");
270 if (!test_gf2m_mod(out))
272 (void)BIO_flush(out);
274 message(out, "BN_GF2m_mod_mul");
275 if (!test_gf2m_mod_mul(out, ctx))
277 (void)BIO_flush(out);
279 message(out, "BN_GF2m_mod_sqr");
280 if (!test_gf2m_mod_sqr(out, ctx))
282 (void)BIO_flush(out);
284 message(out, "BN_GF2m_mod_inv");
285 if (!test_gf2m_mod_inv(out, ctx))
287 (void)BIO_flush(out);
289 message(out, "BN_GF2m_mod_div");
290 if (!test_gf2m_mod_div(out, ctx))
292 (void)BIO_flush(out);
294 message(out, "BN_GF2m_mod_exp");
295 if (!test_gf2m_mod_exp(out, ctx))
297 (void)BIO_flush(out);
299 message(out, "BN_GF2m_mod_sqrt");
300 if (!test_gf2m_mod_sqrt(out, ctx))
302 (void)BIO_flush(out);
304 message(out, "BN_GF2m_mod_solve_quad");
305 if (!test_gf2m_mod_solve_quad(out, ctx))
307 (void)BIO_flush(out);
312 ERR_print_errors_fp(stderr);
314 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
315 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
320 BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc
321 * notices the failure, see test_bn in
322 * test/Makefile.ssl */
323 (void)BIO_flush(out);
327 ERR_print_errors_fp(stderr);
331 int test_add(BIO *bp)
340 BN_bntest_rand(a, 512, 0, 0);
341 for (i = 0; i < num0; i++) {
342 BN_bntest_rand(b, 450 + i, 0, 0);
360 if (!BN_is_zero(c)) {
361 fprintf(stderr, "Add test failed!\n");
371 int test_sub(BIO *bp)
380 for (i = 0; i < num0 + num1; i++) {
382 BN_bntest_rand(a, 512, 0, 0);
384 if (BN_set_bit(a, i) == 0)
388 BN_bntest_rand(b, 400 + i - num1, 0, 0);
405 if (!BN_is_zero(c)) {
406 fprintf(stderr, "Subtract test failed!\n");
416 int test_div(BIO *bp, BN_CTX *ctx)
418 BIGNUM *a, *b, *c, *d, *e;
430 if (BN_div(d, c, a, b, ctx)) {
431 fprintf(stderr, "Division by zero succeeded!\n");
435 for (i = 0; i < num0 + num1; i++) {
437 BN_bntest_rand(a, 400, 0, 0);
442 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
445 BN_div(d, c, a, b, ctx);
465 BN_mul(e, d, b, ctx);
468 if (!BN_is_zero(d)) {
469 fprintf(stderr, "Division test failed!\n");
481 static void print_word(BIO *bp, BN_ULONG w)
483 int i = sizeof(w) * 8;
489 byte = (unsigned char)(w >> i);
491 fmt = byte ? "%X" : NULL;
496 BIO_printf(bp, fmt, byte);
499 /* If we haven't printed anything, at least print a zero! */
504 int test_div_word(BIO *bp)
513 for (i = 0; i < num0; i++) {
515 BN_bntest_rand(a, 512, -1, 0);
516 BN_bntest_rand(b, BN_BITS2, -1, 0);
517 } while (BN_is_zero(b));
521 rmod = BN_mod_word(b, s);
522 r = BN_div_word(b, s);
525 fprintf(stderr, "Mod (word) test failed!\n");
551 if (!BN_is_zero(b)) {
552 fprintf(stderr, "Division (word) test failed!\n");
561 int test_div_recp(BIO *bp, BN_CTX *ctx)
563 BIGNUM *a, *b, *c, *d, *e;
567 recp = BN_RECP_CTX_new();
574 for (i = 0; i < num0 + num1; i++) {
576 BN_bntest_rand(a, 400, 0, 0);
581 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
584 BN_RECP_CTX_set(recp, b, ctx);
585 BN_div_recp(d, c, a, recp, ctx);
605 BN_mul(e, d, b, ctx);
608 if (!BN_is_zero(d)) {
609 fprintf(stderr, "Reciprocal division test failed!\n");
610 fprintf(stderr, "a=");
611 BN_print_fp(stderr, a);
612 fprintf(stderr, "\nb=");
613 BN_print_fp(stderr, b);
614 fprintf(stderr, "\n");
623 BN_RECP_CTX_free(recp);
627 int test_mul(BIO *bp)
629 BIGNUM *a, *b, *c, *d, *e;
643 for (i = 0; i < num0 + num1; i++) {
645 BN_bntest_rand(a, 100, 0, 0);
646 BN_bntest_rand(b, 100, 0, 0);
648 BN_bntest_rand(b, i - num1, 0, 0);
651 BN_mul(c, a, b, ctx);
662 BN_div(d, e, c, a, ctx);
664 if (!BN_is_zero(d) || !BN_is_zero(e)) {
665 fprintf(stderr, "Multiplication test failed!\n");
678 int test_sqr(BIO *bp, BN_CTX *ctx)
680 BIGNUM *a, *c, *d, *e;
687 if (a == NULL || c == NULL || d == NULL || e == NULL) {
691 for (i = 0; i < num0; i++) {
692 BN_bntest_rand(a, 40 + i * 10, 0, 0);
705 BN_div(d, e, c, a, ctx);
707 if (!BN_is_zero(d) || !BN_is_zero(e)) {
708 fprintf(stderr, "Square test failed!\n");
713 /* Regression test for a BN_sqr overflow bug. */
715 "80000000000000008000000000000001"
716 "FFFFFFFFFFFFFFFE0000000000000000");
728 BN_mul(d, a, a, ctx);
730 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
731 "different results!\n");
735 /* Regression test for a BN_sqr overflow bug. */
737 "80000000000000000000000080000001"
738 "FFFFFFFE000000000000000000000000");
750 BN_mul(d, a, a, ctx);
752 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
753 "different results!\n");
765 int test_mont(BIO *bp, BN_CTX *ctx)
767 BIGNUM *a, *b, *c, *d, *A, *B;
780 mont = BN_MONT_CTX_new();
785 if (BN_MONT_CTX_set(mont, n, ctx)) {
786 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
791 if (BN_MONT_CTX_set(mont, n, ctx)) {
792 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
796 BN_bntest_rand(a, 100, 0, 0);
797 BN_bntest_rand(b, 100, 0, 0);
798 for (i = 0; i < num2; i++) {
799 int bits = (200 * (i + 1)) / num2;
803 BN_bntest_rand(n, bits, 0, 1);
804 BN_MONT_CTX_set(mont, n, ctx);
806 BN_nnmod(a, a, n, ctx);
807 BN_nnmod(b, b, n, ctx);
809 BN_to_montgomery(A, a, mont, ctx);
810 BN_to_montgomery(B, b, mont, ctx);
812 BN_mod_mul_montgomery(c, A, B, mont, ctx);
813 BN_from_montgomery(A, c, mont, ctx);
820 BN_print(bp, &mont->N);
826 BN_mod_mul(d, a, b, n, ctx);
828 if (!BN_is_zero(d)) {
829 fprintf(stderr, "Montgomery multiplication test failed!\n");
833 BN_MONT_CTX_free(mont);
844 int test_mod(BIO *bp, BN_CTX *ctx)
846 BIGNUM *a, *b, *c, *d, *e;
855 BN_bntest_rand(a, 1024, 0, 0);
856 for (i = 0; i < num0; i++) {
857 BN_bntest_rand(b, 450 + i * 10, 0, 0);
860 BN_mod(c, a, b, ctx);
871 BN_div(d, e, a, b, ctx);
873 if (!BN_is_zero(e)) {
874 fprintf(stderr, "Modulo test failed!\n");
886 int test_mod_mul(BIO *bp, BN_CTX *ctx)
888 BIGNUM *a, *b, *c, *d, *e;
900 if (BN_mod_mul(e, a, b, c, ctx)) {
901 fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
905 for (j = 0; j < 3; j++) {
906 BN_bntest_rand(c, 1024, 0, 0);
907 for (i = 0; i < num0; i++) {
908 BN_bntest_rand(a, 475 + i * 10, 0, 0);
909 BN_bntest_rand(b, 425 + i * 11, 0, 0);
912 if (!BN_mod_mul(e, a, b, c, ctx)) {
915 while ((l = ERR_get_error()))
916 fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
926 if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
928 * If (a*b) % c is negative, c must be added in order
929 * to obtain the normalized remainder (new with
930 * OpenSSL 0.9.7, previous versions of BN_mod_mul
931 * could generate negative results)
941 BN_mul(d, a, b, ctx);
943 BN_div(a, b, d, c, ctx);
944 if (!BN_is_zero(b)) {
945 fprintf(stderr, "Modulo multiply test failed!\n");
946 ERR_print_errors_fp(stderr);
959 int test_mod_exp(BIO *bp, BN_CTX *ctx)
961 BIGNUM *a, *b, *c, *d, *e;
973 if (BN_mod_exp(d, a, b, c, ctx)) {
974 fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
978 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
979 for (i = 0; i < num2; i++) {
980 BN_bntest_rand(a, 20 + i * 5, 0, 0);
981 BN_bntest_rand(b, 2 + i, 0, 0);
983 if (!BN_mod_exp(d, a, b, c, ctx))
998 BN_exp(e, a, b, ctx);
1000 BN_div(a, b, e, c, ctx);
1001 if (!BN_is_zero(b)) {
1002 fprintf(stderr, "Modulo exponentiation test failed!\n");
1007 /* Regression test for carry propagation bug in sqr8x_reduction */
1008 BN_hex2bn(&a, "050505050505");
1009 BN_hex2bn(&b, "02");
1011 "4141414141414141414141274141414141414141414141414141414141414141"
1012 "4141414141414141414141414141414141414141414141414141414141414141"
1013 "4141414141414141414141800000000000000000000000000000000000000000"
1014 "0000000000000000000000000000000000000000000000000000000000000000"
1015 "0000000000000000000000000000000000000000000000000000000000000000"
1016 "0000000000000000000000000000000000000000000000000000000001");
1017 BN_mod_exp(d, a, b, c, ctx);
1018 BN_mul(e, a, a, ctx);
1020 fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
1032 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1034 BIGNUM *a, *b, *c, *d, *e;
1046 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1047 fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
1053 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1054 fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
1059 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1060 for (i = 0; i < num2; i++) {
1061 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1062 BN_bntest_rand(b, 2 + i, 0, 0);
1064 if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
1070 BIO_puts(bp, " ^ ");
1072 BIO_puts(bp, " % ");
1074 BIO_puts(bp, " - ");
1079 BN_exp(e, a, b, ctx);
1081 BN_div(a, b, e, c, ctx);
1082 if (!BN_is_zero(b)) {
1083 fprintf(stderr, "Modulo exponentiation test failed!\n");
1096 * Test constant-time modular exponentiation with 1024-bit inputs, which on
1097 * x86_64 cause a different code branch to be taken.
1099 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1101 BIGNUM *a, *p, *m, *d, *e;
1109 mont = BN_MONT_CTX_new();
1111 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
1113 BN_bntest_rand(a, 1024, 0, 0);
1115 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1117 if (!BN_is_one(d)) {
1118 fprintf(stderr, "Modular exponentiation test failed!\n");
1122 BN_bntest_rand(p, 1024, 0, 0);
1124 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1126 if (!BN_is_zero(d)) {
1127 fprintf(stderr, "Modular exponentiation test failed!\n");
1131 * Craft an input whose Montgomery representation is 1, i.e., shorter
1132 * than the modulus m, in order to test the const time precomputation
1133 * scattering/gathering.
1136 BN_MONT_CTX_set(mont, m, ctx);
1137 if (!BN_from_montgomery(e, a, mont, ctx))
1139 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1141 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1143 if (BN_cmp(a, d) != 0) {
1144 fprintf(stderr, "Modular exponentiation test failed!\n");
1147 /* Finally, some regular test vectors. */
1148 BN_bntest_rand(e, 1024, 0, 0);
1149 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1151 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1153 if (BN_cmp(a, d) != 0) {
1154 fprintf(stderr, "Modular exponentiation test failed!\n");
1157 BN_MONT_CTX_free(mont);
1166 int test_exp(BIO *bp, BN_CTX *ctx)
1168 BIGNUM *a, *b, *d, *e, *one;
1178 for (i = 0; i < num2; i++) {
1179 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1180 BN_bntest_rand(b, 2 + i, 0, 0);
1182 if (BN_exp(d, a, b, ctx) <= 0)
1188 BIO_puts(bp, " ^ ");
1190 BIO_puts(bp, " - ");
1196 for (; !BN_is_zero(b); BN_sub(b, b, one))
1197 BN_mul(e, e, a, ctx);
1199 if (!BN_is_zero(e)) {
1200 fprintf(stderr, "Exponentiation test failed!\n");
1212 #ifndef OPENSSL_NO_EC2M
1213 int test_gf2m_add(BIO *bp)
1222 for (i = 0; i < num0; i++) {
1223 BN_rand(a, 512, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
1224 BN_copy(b, BN_value_one());
1225 a->neg = rand_neg();
1226 b->neg = rand_neg();
1227 BN_GF2m_add(c, a, b);
1228 /* Test that two added values have the correct parity. */
1229 if ((BN_is_odd(a) && BN_is_odd(c))
1230 || (!BN_is_odd(a) && !BN_is_odd(c))) {
1231 fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1234 BN_GF2m_add(c, c, c);
1235 /* Test that c + c = 0. */
1236 if (!BN_is_zero(c)) {
1237 fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1249 int test_gf2m_mod(BIO *bp)
1251 BIGNUM *a, *b[2], *c, *d, *e;
1253 int p0[] = { 163, 7, 6, 3, 0, -1 };
1254 int p1[] = { 193, 15, 0, -1 };
1263 BN_GF2m_arr2poly(p0, b[0]);
1264 BN_GF2m_arr2poly(p1, b[1]);
1266 for (i = 0; i < num0; i++) {
1267 BN_bntest_rand(a, 1024, 0, 0);
1268 for (j = 0; j < 2; j++) {
1269 BN_GF2m_mod(c, a, b[j]);
1270 BN_GF2m_add(d, a, c);
1271 BN_GF2m_mod(e, d, b[j]);
1272 /* Test that a + (a mod p) mod p == 0. */
1273 if (!BN_is_zero(e)) {
1274 fprintf(stderr, "GF(2^m) modulo test failed!\n");
1290 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1292 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
1294 int p0[] = { 163, 7, 6, 3, 0, -1 };
1295 int p1[] = { 193, 15, 0, -1 };
1307 BN_GF2m_arr2poly(p0, b[0]);
1308 BN_GF2m_arr2poly(p1, b[1]);
1310 for (i = 0; i < num0; i++) {
1311 BN_bntest_rand(a, 1024, 0, 0);
1312 BN_bntest_rand(c, 1024, 0, 0);
1313 BN_bntest_rand(d, 1024, 0, 0);
1314 for (j = 0; j < 2; j++) {
1315 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1316 BN_GF2m_add(f, a, d);
1317 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1318 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1319 BN_GF2m_add(f, e, g);
1320 BN_GF2m_add(f, f, h);
1321 /* Test that (a+d)*c = a*c + d*c. */
1322 if (!BN_is_zero(f)) {
1324 "GF(2^m) modular multiplication test failed!\n");
1343 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1345 BIGNUM *a, *b[2], *c, *d;
1347 int p0[] = { 163, 7, 6, 3, 0, -1 };
1348 int p1[] = { 193, 15, 0, -1 };
1356 BN_GF2m_arr2poly(p0, b[0]);
1357 BN_GF2m_arr2poly(p1, b[1]);
1359 for (i = 0; i < num0; i++) {
1360 BN_bntest_rand(a, 1024, 0, 0);
1361 for (j = 0; j < 2; j++) {
1362 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1364 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1365 BN_GF2m_add(d, c, d);
1366 /* Test that a*a = a^2. */
1367 if (!BN_is_zero(d)) {
1368 fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
1383 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1385 BIGNUM *a, *b[2], *c, *d;
1387 int p0[] = { 163, 7, 6, 3, 0, -1 };
1388 int p1[] = { 193, 15, 0, -1 };
1396 BN_GF2m_arr2poly(p0, b[0]);
1397 BN_GF2m_arr2poly(p1, b[1]);
1399 for (i = 0; i < num0; i++) {
1400 BN_bntest_rand(a, 512, 0, 0);
1401 for (j = 0; j < 2; j++) {
1402 BN_GF2m_mod_inv(c, a, b[j], ctx);
1403 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1404 /* Test that ((1/a)*a) = 1. */
1405 if (!BN_is_one(d)) {
1406 fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
1421 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1423 BIGNUM *a, *b[2], *c, *d, *e, *f;
1425 int p0[] = { 163, 7, 6, 3, 0, -1 };
1426 int p1[] = { 193, 15, 0, -1 };
1436 BN_GF2m_arr2poly(p0, b[0]);
1437 BN_GF2m_arr2poly(p1, b[1]);
1439 for (i = 0; i < num0; i++) {
1440 BN_bntest_rand(a, 512, 0, 0);
1441 BN_bntest_rand(c, 512, 0, 0);
1442 for (j = 0; j < 2; j++) {
1443 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1444 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1445 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1446 /* Test that ((a/c)*c)/a = 1. */
1447 if (!BN_is_one(f)) {
1448 fprintf(stderr, "GF(2^m) modular division test failed!\n");
1465 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1467 BIGNUM *a, *b[2], *c, *d, *e, *f;
1469 int p0[] = { 163, 7, 6, 3, 0, -1 };
1470 int p1[] = { 193, 15, 0, -1 };
1480 BN_GF2m_arr2poly(p0, b[0]);
1481 BN_GF2m_arr2poly(p1, b[1]);
1483 for (i = 0; i < num0; i++) {
1484 BN_bntest_rand(a, 512, 0, 0);
1485 BN_bntest_rand(c, 512, 0, 0);
1486 BN_bntest_rand(d, 512, 0, 0);
1487 for (j = 0; j < 2; j++) {
1488 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1489 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1490 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1492 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1493 BN_GF2m_add(f, e, f);
1494 /* Test that a^(c+d)=a^c*a^d. */
1495 if (!BN_is_zero(f)) {
1497 "GF(2^m) modular exponentiation test failed!\n");
1514 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1516 BIGNUM *a, *b[2], *c, *d, *e, *f;
1518 int p0[] = { 163, 7, 6, 3, 0, -1 };
1519 int p1[] = { 193, 15, 0, -1 };
1529 BN_GF2m_arr2poly(p0, b[0]);
1530 BN_GF2m_arr2poly(p1, b[1]);
1532 for (i = 0; i < num0; i++) {
1533 BN_bntest_rand(a, 512, 0, 0);
1534 for (j = 0; j < 2; j++) {
1535 BN_GF2m_mod(c, a, b[j]);
1536 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1537 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1538 BN_GF2m_add(f, c, e);
1539 /* Test that d^2 = a, where d = sqrt(a). */
1540 if (!BN_is_zero(f)) {
1541 fprintf(stderr, "GF(2^m) modular square root test failed!\n");
1558 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1560 BIGNUM *a, *b[2], *c, *d, *e;
1561 int i, j, s = 0, t, ret = 0;
1562 int p0[] = { 163, 7, 6, 3, 0, -1 };
1563 int p1[] = { 193, 15, 0, -1 };
1572 BN_GF2m_arr2poly(p0, b[0]);
1573 BN_GF2m_arr2poly(p1, b[1]);
1575 for (i = 0; i < num0; i++) {
1576 BN_bntest_rand(a, 512, 0, 0);
1577 for (j = 0; j < 2; j++) {
1578 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1581 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1582 BN_GF2m_add(d, c, d);
1583 BN_GF2m_mod(e, a, b[j]);
1584 BN_GF2m_add(e, e, d);
1586 * Test that solution of quadratic c satisfies c^2 + c = a.
1588 if (!BN_is_zero(e)) {
1590 "GF(2^m) modular solve quadratic test failed!\n");
1599 "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
1602 "this is very unlikely and probably indicates an error.\n");
1616 static int genprime_cb(int p, int n, BN_GENCB *arg)
1633 int test_kron(BIO *bp, BN_CTX *ctx)
1636 BIGNUM *a, *b, *r, *t;
1638 int legendre, kronecker;
1645 if (a == NULL || b == NULL || r == NULL || t == NULL)
1648 BN_GENCB_set(&cb, genprime_cb, NULL);
1651 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1652 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1653 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1654 * generate a random prime b and compare these values for a number of
1655 * random a's. (That is, we run the Solovay-Strassen primality test to
1656 * confirm that b is prime, except that we don't want to test whether b
1657 * is prime but whether BN_kronecker works.)
1660 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
1662 b->neg = rand_neg();
1665 for (i = 0; i < num0; i++) {
1666 if (!BN_bntest_rand(a, 512, 0, 0))
1668 a->neg = rand_neg();
1670 /* t := (|b|-1)/2 (note that b is odd) */
1674 if (!BN_sub_word(t, 1))
1676 if (!BN_rshift1(t, t))
1678 /* r := a^t mod b */
1681 if (!BN_mod_exp_recp(r, a, t, b, ctx))
1685 if (BN_is_word(r, 1))
1687 else if (BN_is_zero(r))
1690 if (!BN_add_word(r, 1))
1692 if (0 != BN_ucmp(r, b)) {
1693 fprintf(stderr, "Legendre symbol computation failed\n");
1699 kronecker = BN_kronecker(a, b, ctx);
1702 /* we actually need BN_kronecker(a, |b|) */
1703 if (a->neg && b->neg)
1704 kronecker = -kronecker;
1706 if (legendre != kronecker) {
1707 fprintf(stderr, "legendre != kronecker; a = ");
1708 BN_print_fp(stderr, a);
1709 fprintf(stderr, ", b = ");
1710 BN_print_fp(stderr, b);
1711 fprintf(stderr, "\n");
1730 int test_sqrt(BIO *bp, BN_CTX *ctx)
1740 if (a == NULL || p == NULL || r == NULL)
1743 BN_GENCB_set(&cb, genprime_cb, NULL);
1745 for (i = 0; i < 16; i++) {
1747 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1749 if (!BN_set_word(p, primes[i]))
1752 if (!BN_set_word(a, 32))
1754 if (!BN_set_word(r, 2 * i + 1))
1757 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
1761 p->neg = rand_neg();
1763 for (j = 0; j < num2; j++) {
1765 * construct 'a' such that it is a square modulo p, but in
1766 * general not a proper square and not reduced modulo p
1768 if (!BN_bntest_rand(r, 256, 0, 3))
1770 if (!BN_nnmod(r, r, p, ctx))
1772 if (!BN_mod_sqr(r, r, p, ctx))
1774 if (!BN_bntest_rand(a, 256, 0, 3))
1776 if (!BN_nnmod(a, a, p, ctx))
1778 if (!BN_mod_sqr(a, a, p, ctx))
1780 if (!BN_mul(a, a, r, ctx))
1783 if (!BN_sub(a, a, p))
1786 if (!BN_mod_sqrt(r, a, p, ctx))
1788 if (!BN_mod_sqr(r, r, p, ctx))
1791 if (!BN_nnmod(a, a, p, ctx))
1794 if (BN_cmp(a, r) != 0) {
1795 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1796 BN_print_fp(stderr, a);
1797 fprintf(stderr, ", r = ");
1798 BN_print_fp(stderr, r);
1799 fprintf(stderr, ", p = ");
1800 BN_print_fp(stderr, p);
1801 fprintf(stderr, "\n");
1820 int test_small_prime(BIO *bp, BN_CTX *ctx)
1822 static const int bits = 10;
1827 if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
1829 if (BN_num_bits(r) != bits) {
1830 BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
1842 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
1844 BIGNUM *a, *b, *c, *d;
1856 BN_bntest_rand(a, 200, 0, 0);
1857 a->neg = rand_neg();
1859 for (i = 0; i < num0; i++) {
1860 BN_lshift(b, a, i + 1);
1865 BIO_puts(bp, " * ");
1867 BIO_puts(bp, " - ");
1872 BN_mul(d, a, c, ctx);
1874 if (!BN_is_zero(d)) {
1875 fprintf(stderr, "Left shift test failed!\n");
1876 fprintf(stderr, "a=");
1877 BN_print_fp(stderr, a);
1878 fprintf(stderr, "\nb=");
1879 BN_print_fp(stderr, b);
1880 fprintf(stderr, "\nc=");
1881 BN_print_fp(stderr, c);
1882 fprintf(stderr, "\nd=");
1883 BN_print_fp(stderr, d);
1884 fprintf(stderr, "\n");
1895 int test_lshift1(BIO *bp)
1904 BN_bntest_rand(a, 200, 0, 0);
1905 a->neg = rand_neg();
1906 for (i = 0; i < num0; i++) {
1911 BIO_puts(bp, " * 2");
1912 BIO_puts(bp, " - ");
1919 if (!BN_is_zero(a)) {
1920 fprintf(stderr, "Left shift one test failed!\n");
1932 int test_rshift(BIO *bp, BN_CTX *ctx)
1934 BIGNUM *a, *b, *c, *d, *e;
1944 BN_bntest_rand(a, 200, 0, 0);
1945 a->neg = rand_neg();
1946 for (i = 0; i < num0; i++) {
1947 BN_rshift(b, a, i + 1);
1952 BIO_puts(bp, " / ");
1954 BIO_puts(bp, " - ");
1959 BN_div(d, e, a, c, ctx);
1961 if (!BN_is_zero(d)) {
1962 fprintf(stderr, "Right shift test failed!\n");
1974 int test_rshift1(BIO *bp)
1983 BN_bntest_rand(a, 200, 0, 0);
1984 a->neg = rand_neg();
1985 for (i = 0; i < num0; i++) {
1990 BIO_puts(bp, " / 2");
1991 BIO_puts(bp, " - ");
1998 if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
1999 fprintf(stderr, "Right shift one test failed!\n");
2012 static unsigned int neg = 0;
2013 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
2015 return (sign[(neg++) % 8]);