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>
84 * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
85 * This is fine in itself, it will end up as an unused static function in
86 * the worst case. However, it referenses bn_expand2(), which is a private
87 * function in libcrypto and therefore unavailable on some systems. This
88 * may result in a linker error because of unresolved symbols.
90 * To avoid this, we define a dummy variant of bn_expand2() here, and to
91 * avoid possible clashes with libcrypto, we rename it first, using a macro.
93 #define bn_expand2 dummy_bn_expand2
94 BIGNUM *bn_expand2(BIGNUM *b, int words);
95 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
97 #include "../crypto/bn/bn_lcl.h"
99 static const int num0 = 100; /* number of tests */
100 static const int num1 = 50; /* additional tests for some functions */
101 static const int num2 = 5; /* number of tests for slow functions */
103 int test_add(BIO *bp);
104 int test_sub(BIO *bp);
105 int test_lshift1(BIO *bp);
106 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
107 int test_rshift1(BIO *bp);
108 int test_rshift(BIO *bp, BN_CTX *ctx);
109 int test_div(BIO *bp, BN_CTX *ctx);
110 int test_div_word(BIO *bp);
111 int test_div_recp(BIO *bp, BN_CTX *ctx);
112 int test_mul(BIO *bp);
113 int test_sqr(BIO *bp, BN_CTX *ctx);
114 int test_mont(BIO *bp, BN_CTX *ctx);
115 int test_mod(BIO *bp, BN_CTX *ctx);
116 int test_mod_mul(BIO *bp, BN_CTX *ctx);
117 int test_mod_exp(BIO *bp, BN_CTX *ctx);
118 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
119 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
120 int test_exp(BIO *bp, BN_CTX *ctx);
121 int test_gf2m_add(BIO *bp);
122 int test_gf2m_mod(BIO *bp);
123 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
124 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
125 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
126 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx);
127 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx);
128 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx);
129 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx);
130 int test_kron(BIO *bp, BN_CTX *ctx);
131 int test_sqrt(BIO *bp, BN_CTX *ctx);
132 int test_small_prime(BIO *bp, BN_CTX *ctx);
134 static int results = 0;
136 static unsigned char lst[] =
137 "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
138 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
140 static const char rnd_seed[] =
141 "string to make the random number generator think it has entropy";
143 static void message(BIO *out, char *m)
145 fprintf(stderr, "test %s\n", m);
146 BIO_puts(out, "print \"test ");
148 BIO_puts(out, "\\n\"\n");
151 int main(int argc, char *argv[])
155 char *outfile = NULL;
159 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
164 if (strcmp(*argv, "-results") == 0)
166 else if (strcmp(*argv, "-out") == 0) {
179 out = BIO_new(BIO_s_file());
182 if (outfile == NULL) {
183 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
185 if (!BIO_write_filename(out, outfile)) {
190 #ifdef OPENSSL_SYS_VMS
192 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
193 out = BIO_push(tmpbio, out);
198 BIO_puts(out, "obase=16\nibase=16\n");
200 message(out, "BN_add");
203 (void)BIO_flush(out);
205 message(out, "BN_sub");
208 (void)BIO_flush(out);
210 message(out, "BN_lshift1");
211 if (!test_lshift1(out))
213 (void)BIO_flush(out);
215 message(out, "BN_lshift (fixed)");
216 if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
218 (void)BIO_flush(out);
220 message(out, "BN_lshift");
221 if (!test_lshift(out, ctx, NULL))
223 (void)BIO_flush(out);
225 message(out, "BN_rshift1");
226 if (!test_rshift1(out))
228 (void)BIO_flush(out);
230 message(out, "BN_rshift");
231 if (!test_rshift(out, ctx))
233 (void)BIO_flush(out);
235 message(out, "BN_sqr");
236 if (!test_sqr(out, ctx))
238 (void)BIO_flush(out);
240 message(out, "BN_mul");
243 (void)BIO_flush(out);
245 message(out, "BN_div");
246 if (!test_div(out, ctx))
248 (void)BIO_flush(out);
250 message(out, "BN_div_word");
251 if (!test_div_word(out))
253 (void)BIO_flush(out);
255 message(out, "BN_div_recp");
256 if (!test_div_recp(out, ctx))
258 (void)BIO_flush(out);
260 message(out, "BN_mod");
261 if (!test_mod(out, ctx))
263 (void)BIO_flush(out);
265 message(out, "BN_mod_mul");
266 if (!test_mod_mul(out, ctx))
268 (void)BIO_flush(out);
270 message(out, "BN_mont");
271 if (!test_mont(out, ctx))
273 (void)BIO_flush(out);
275 message(out, "BN_mod_exp");
276 if (!test_mod_exp(out, ctx))
278 (void)BIO_flush(out);
280 message(out, "BN_mod_exp_mont_consttime");
281 if (!test_mod_exp_mont_consttime(out, ctx))
283 if (!test_mod_exp_mont5(out, ctx))
285 (void)BIO_flush(out);
287 message(out, "BN_exp");
288 if (!test_exp(out, ctx))
290 (void)BIO_flush(out);
292 message(out, "BN_kronecker");
293 if (!test_kron(out, ctx))
295 (void)BIO_flush(out);
297 message(out, "BN_mod_sqrt");
298 if (!test_sqrt(out, ctx))
300 (void)BIO_flush(out);
302 message(out, "Small prime generation");
303 if (!test_small_prime(out, ctx))
305 (void)BIO_flush(out);
307 #ifndef OPENSSL_NO_EC2M
308 message(out, "BN_GF2m_add");
309 if (!test_gf2m_add(out))
311 (void)BIO_flush(out);
313 message(out, "BN_GF2m_mod");
314 if (!test_gf2m_mod(out))
316 (void)BIO_flush(out);
318 message(out, "BN_GF2m_mod_mul");
319 if (!test_gf2m_mod_mul(out, ctx))
321 (void)BIO_flush(out);
323 message(out, "BN_GF2m_mod_sqr");
324 if (!test_gf2m_mod_sqr(out, ctx))
326 (void)BIO_flush(out);
328 message(out, "BN_GF2m_mod_inv");
329 if (!test_gf2m_mod_inv(out, ctx))
331 (void)BIO_flush(out);
333 message(out, "BN_GF2m_mod_div");
334 if (!test_gf2m_mod_div(out, ctx))
336 (void)BIO_flush(out);
338 message(out, "BN_GF2m_mod_exp");
339 if (!test_gf2m_mod_exp(out, ctx))
341 (void)BIO_flush(out);
343 message(out, "BN_GF2m_mod_sqrt");
344 if (!test_gf2m_mod_sqrt(out, ctx))
346 (void)BIO_flush(out);
348 message(out, "BN_GF2m_mod_solve_quad");
349 if (!test_gf2m_mod_solve_quad(out, ctx))
351 (void)BIO_flush(out);
358 BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc
359 * notices the failure, see test_bn in
360 * test/Makefile.ssl */
361 (void)BIO_flush(out);
363 ERR_print_errors_fp(stderr);
367 int test_add(BIO *bp)
376 BN_bntest_rand(a, 512, 0, 0);
377 for (i = 0; i < num0; i++) {
378 BN_bntest_rand(b, 450 + i, 0, 0);
396 if (!BN_is_zero(c)) {
397 fprintf(stderr, "Add test failed!\n");
407 int test_sub(BIO *bp)
416 for (i = 0; i < num0 + num1; i++) {
418 BN_bntest_rand(a, 512, 0, 0);
420 if (BN_set_bit(a, i) == 0)
424 BN_bntest_rand(b, 400 + i - num1, 0, 0);
441 if (!BN_is_zero(c)) {
442 fprintf(stderr, "Subtract test failed!\n");
452 int test_div(BIO *bp, BN_CTX *ctx)
454 BIGNUM *a, *b, *c, *d, *e;
466 if (BN_div(d, c, a, b, ctx)) {
467 fprintf(stderr, "Division by zero succeeded!\n");
471 for (i = 0; i < num0 + num1; i++) {
473 BN_bntest_rand(a, 400, 0, 0);
478 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
481 BN_div(d, c, a, b, ctx);
501 BN_mul(e, d, b, ctx);
504 if (!BN_is_zero(d)) {
505 fprintf(stderr, "Division test failed!\n");
517 static void print_word(BIO *bp, BN_ULONG w)
519 int i = sizeof(w) * 8;
525 byte = (unsigned char)(w >> i);
527 fmt = byte ? "%X" : NULL;
532 BIO_printf(bp, fmt, byte);
535 /* If we haven't printed anything, at least print a zero! */
540 int test_div_word(BIO *bp)
549 for (i = 0; i < num0; i++) {
551 BN_bntest_rand(a, 512, -1, 0);
552 BN_bntest_rand(b, BN_BITS2, -1, 0);
553 } while (BN_is_zero(b));
557 r = BN_div_word(b, s);
581 if (!BN_is_zero(b)) {
582 fprintf(stderr, "Division (word) test failed!\n");
591 int test_div_recp(BIO *bp, BN_CTX *ctx)
593 BIGNUM *a, *b, *c, *d, *e;
597 recp = BN_RECP_CTX_new();
604 for (i = 0; i < num0 + num1; i++) {
606 BN_bntest_rand(a, 400, 0, 0);
611 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
614 BN_RECP_CTX_set(recp, b, ctx);
615 BN_div_recp(d, c, a, recp, ctx);
635 BN_mul(e, d, b, ctx);
638 if (!BN_is_zero(d)) {
639 fprintf(stderr, "Reciprocal division test failed!\n");
640 fprintf(stderr, "a=");
641 BN_print_fp(stderr, a);
642 fprintf(stderr, "\nb=");
643 BN_print_fp(stderr, b);
644 fprintf(stderr, "\n");
653 BN_RECP_CTX_free(recp);
657 int test_mul(BIO *bp)
659 BIGNUM *a, *b, *c, *d, *e;
673 for (i = 0; i < num0 + num1; i++) {
675 BN_bntest_rand(a, 100, 0, 0);
676 BN_bntest_rand(b, 100, 0, 0);
678 BN_bntest_rand(b, i - num1, 0, 0);
681 BN_mul(c, a, b, ctx);
692 BN_div(d, e, c, a, ctx);
694 if (!BN_is_zero(d) || !BN_is_zero(e)) {
695 fprintf(stderr, "Multiplication test failed!\n");
708 int test_sqr(BIO *bp, BN_CTX *ctx)
710 BIGNUM *a, *c, *d, *e;
717 if (a == NULL || c == NULL || d == NULL || e == NULL) {
721 for (i = 0; i < num0; i++) {
722 BN_bntest_rand(a, 40 + i * 10, 0, 0);
735 BN_div(d, e, c, a, ctx);
737 if (!BN_is_zero(d) || !BN_is_zero(e)) {
738 fprintf(stderr, "Square test failed!\n");
743 /* Regression test for a BN_sqr overflow bug. */
745 "80000000000000008000000000000001"
746 "FFFFFFFFFFFFFFFE0000000000000000");
758 BN_mul(d, a, a, ctx);
760 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
761 "different results!\n");
765 /* Regression test for a BN_sqr overflow bug. */
767 "80000000000000000000000080000001"
768 "FFFFFFFE000000000000000000000000");
780 BN_mul(d, a, a, ctx);
782 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
783 "different results!\n");
795 int test_mont(BIO *bp, BN_CTX *ctx)
797 BIGNUM *a, *b, *c, *d, *A, *B;
810 mont = BN_MONT_CTX_new();
815 if (BN_MONT_CTX_set(mont, n, ctx)) {
816 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
821 if (BN_MONT_CTX_set(mont, n, ctx)) {
822 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
826 BN_bntest_rand(a, 100, 0, 0);
827 BN_bntest_rand(b, 100, 0, 0);
828 for (i = 0; i < num2; i++) {
829 int bits = (200 * (i + 1)) / num2;
833 BN_bntest_rand(n, bits, 0, 1);
834 BN_MONT_CTX_set(mont, n, ctx);
836 BN_nnmod(a, a, n, ctx);
837 BN_nnmod(b, b, n, ctx);
839 BN_to_montgomery(A, a, mont, ctx);
840 BN_to_montgomery(B, b, mont, ctx);
842 BN_mod_mul_montgomery(c, A, B, mont, ctx);
843 BN_from_montgomery(A, c, mont, ctx);
850 BN_print(bp, &mont->N);
856 BN_mod_mul(d, a, b, n, ctx);
858 if (!BN_is_zero(d)) {
859 fprintf(stderr, "Montgomery multiplication test failed!\n");
863 BN_MONT_CTX_free(mont);
874 int test_mod(BIO *bp, BN_CTX *ctx)
876 BIGNUM *a, *b, *c, *d, *e;
885 BN_bntest_rand(a, 1024, 0, 0);
886 for (i = 0; i < num0; i++) {
887 BN_bntest_rand(b, 450 + i * 10, 0, 0);
890 BN_mod(c, a, b, ctx);
901 BN_div(d, e, a, b, ctx);
903 if (!BN_is_zero(e)) {
904 fprintf(stderr, "Modulo test failed!\n");
916 int test_mod_mul(BIO *bp, BN_CTX *ctx)
918 BIGNUM *a, *b, *c, *d, *e;
930 if (BN_mod_mul(e, a, b, c, ctx)) {
931 fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
935 for (j = 0; j < 3; j++) {
936 BN_bntest_rand(c, 1024, 0, 0);
937 for (i = 0; i < num0; i++) {
938 BN_bntest_rand(a, 475 + i * 10, 0, 0);
939 BN_bntest_rand(b, 425 + i * 11, 0, 0);
942 if (!BN_mod_mul(e, a, b, c, ctx)) {
945 while ((l = ERR_get_error()))
946 fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
956 if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
958 * If (a*b) % c is negative, c must be added in order
959 * to obtain the normalized remainder (new with
960 * OpenSSL 0.9.7, previous versions of BN_mod_mul
961 * could generate negative results)
971 BN_mul(d, a, b, ctx);
973 BN_div(a, b, d, c, ctx);
974 if (!BN_is_zero(b)) {
975 fprintf(stderr, "Modulo multiply test failed!\n");
976 ERR_print_errors_fp(stderr);
989 int test_mod_exp(BIO *bp, BN_CTX *ctx)
991 BIGNUM *a, *b, *c, *d, *e;
1003 if (BN_mod_exp(d, a, b, c, ctx)) {
1004 fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
1008 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1009 for (i = 0; i < num2; i++) {
1010 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1011 BN_bntest_rand(b, 2 + i, 0, 0);
1013 if (!BN_mod_exp(d, a, b, c, ctx))
1019 BIO_puts(bp, " ^ ");
1021 BIO_puts(bp, " % ");
1023 BIO_puts(bp, " - ");
1028 BN_exp(e, a, b, ctx);
1030 BN_div(a, b, e, c, ctx);
1031 if (!BN_is_zero(b)) {
1032 fprintf(stderr, "Modulo exponentiation test failed!\n");
1037 /* Regression test for carry propagation bug in sqr8x_reduction */
1038 BN_hex2bn(&a, "050505050505");
1039 BN_hex2bn(&b, "02");
1041 "4141414141414141414141274141414141414141414141414141414141414141"
1042 "4141414141414141414141414141414141414141414141414141414141414141"
1043 "4141414141414141414141800000000000000000000000000000000000000000"
1044 "0000000000000000000000000000000000000000000000000000000000000000"
1045 "0000000000000000000000000000000000000000000000000000000000000000"
1046 "0000000000000000000000000000000000000000000000000000000001");
1047 BN_mod_exp(d, a, b, c, ctx);
1048 BN_mul(e, a, a, ctx);
1050 fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
1062 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1064 BIGNUM *a, *b, *c, *d, *e;
1076 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1077 fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
1083 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1084 fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
1089 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1090 for (i = 0; i < num2; i++) {
1091 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1092 BN_bntest_rand(b, 2 + i, 0, 0);
1094 if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
1100 BIO_puts(bp, " ^ ");
1102 BIO_puts(bp, " % ");
1104 BIO_puts(bp, " - ");
1109 BN_exp(e, a, b, ctx);
1111 BN_div(a, b, e, c, ctx);
1112 if (!BN_is_zero(b)) {
1113 fprintf(stderr, "Modulo exponentiation test failed!\n");
1126 * Test constant-time modular exponentiation with 1024-bit inputs, which on
1127 * x86_64 cause a different code branch to be taken.
1129 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1131 BIGNUM *a, *p, *m, *d, *e;
1139 mont = BN_MONT_CTX_new();
1141 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
1143 BN_bntest_rand(a, 1024, 0, 0);
1145 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1147 if (!BN_is_one(d)) {
1148 fprintf(stderr, "Modular exponentiation test failed!\n");
1152 BN_bntest_rand(p, 1024, 0, 0);
1154 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1156 if (!BN_is_zero(d)) {
1157 fprintf(stderr, "Modular exponentiation test failed!\n");
1161 * Craft an input whose Montgomery representation is 1, i.e., shorter
1162 * than the modulus m, in order to test the const time precomputation
1163 * scattering/gathering.
1166 BN_MONT_CTX_set(mont, m, ctx);
1167 if (!BN_from_montgomery(e, a, mont, ctx))
1169 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1171 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1173 if (BN_cmp(a, d) != 0) {
1174 fprintf(stderr, "Modular exponentiation test failed!\n");
1177 /* Finally, some regular test vectors. */
1178 BN_bntest_rand(e, 1024, 0, 0);
1179 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1181 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1183 if (BN_cmp(a, d) != 0) {
1184 fprintf(stderr, "Modular exponentiation test failed!\n");
1187 BN_MONT_CTX_free(mont);
1196 int test_exp(BIO *bp, BN_CTX *ctx)
1198 BIGNUM *a, *b, *d, *e, *one;
1208 for (i = 0; i < num2; i++) {
1209 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1210 BN_bntest_rand(b, 2 + i, 0, 0);
1212 if (BN_exp(d, a, b, ctx) <= 0)
1218 BIO_puts(bp, " ^ ");
1220 BIO_puts(bp, " - ");
1226 for (; !BN_is_zero(b); BN_sub(b, b, one))
1227 BN_mul(e, e, a, ctx);
1229 if (!BN_is_zero(e)) {
1230 fprintf(stderr, "Exponentiation test failed!\n");
1242 #ifndef OPENSSL_NO_EC2M
1243 int test_gf2m_add(BIO *bp)
1252 for (i = 0; i < num0; i++) {
1253 BN_rand(a, 512, 0, 0);
1254 BN_copy(b, BN_value_one());
1255 a->neg = rand_neg();
1256 b->neg = rand_neg();
1257 BN_GF2m_add(c, a, b);
1258 /* Test that two added values have the correct parity. */
1259 if ((BN_is_odd(a) && BN_is_odd(c))
1260 || (!BN_is_odd(a) && !BN_is_odd(c))) {
1261 fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1264 BN_GF2m_add(c, c, c);
1265 /* Test that c + c = 0. */
1266 if (!BN_is_zero(c)) {
1267 fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1279 int test_gf2m_mod(BIO *bp)
1281 BIGNUM *a, *b[2], *c, *d, *e;
1283 int p0[] = { 163, 7, 6, 3, 0, -1 };
1284 int p1[] = { 193, 15, 0, -1 };
1293 BN_GF2m_arr2poly(p0, b[0]);
1294 BN_GF2m_arr2poly(p1, b[1]);
1296 for (i = 0; i < num0; i++) {
1297 BN_bntest_rand(a, 1024, 0, 0);
1298 for (j = 0; j < 2; j++) {
1299 BN_GF2m_mod(c, a, b[j]);
1300 BN_GF2m_add(d, a, c);
1301 BN_GF2m_mod(e, d, b[j]);
1302 /* Test that a + (a mod p) mod p == 0. */
1303 if (!BN_is_zero(e)) {
1304 fprintf(stderr, "GF(2^m) modulo test failed!\n");
1320 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1322 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
1324 int p0[] = { 163, 7, 6, 3, 0, -1 };
1325 int p1[] = { 193, 15, 0, -1 };
1337 BN_GF2m_arr2poly(p0, b[0]);
1338 BN_GF2m_arr2poly(p1, b[1]);
1340 for (i = 0; i < num0; i++) {
1341 BN_bntest_rand(a, 1024, 0, 0);
1342 BN_bntest_rand(c, 1024, 0, 0);
1343 BN_bntest_rand(d, 1024, 0, 0);
1344 for (j = 0; j < 2; j++) {
1345 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1346 BN_GF2m_add(f, a, d);
1347 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1348 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1349 BN_GF2m_add(f, e, g);
1350 BN_GF2m_add(f, f, h);
1351 /* Test that (a+d)*c = a*c + d*c. */
1352 if (!BN_is_zero(f)) {
1354 "GF(2^m) modular multiplication test failed!\n");
1373 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1375 BIGNUM *a, *b[2], *c, *d;
1377 int p0[] = { 163, 7, 6, 3, 0, -1 };
1378 int p1[] = { 193, 15, 0, -1 };
1386 BN_GF2m_arr2poly(p0, b[0]);
1387 BN_GF2m_arr2poly(p1, b[1]);
1389 for (i = 0; i < num0; i++) {
1390 BN_bntest_rand(a, 1024, 0, 0);
1391 for (j = 0; j < 2; j++) {
1392 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1394 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1395 BN_GF2m_add(d, c, d);
1396 /* Test that a*a = a^2. */
1397 if (!BN_is_zero(d)) {
1398 fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
1413 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1415 BIGNUM *a, *b[2], *c, *d;
1417 int p0[] = { 163, 7, 6, 3, 0, -1 };
1418 int p1[] = { 193, 15, 0, -1 };
1426 BN_GF2m_arr2poly(p0, b[0]);
1427 BN_GF2m_arr2poly(p1, b[1]);
1429 for (i = 0; i < num0; i++) {
1430 BN_bntest_rand(a, 512, 0, 0);
1431 for (j = 0; j < 2; j++) {
1432 BN_GF2m_mod_inv(c, a, b[j], ctx);
1433 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1434 /* Test that ((1/a)*a) = 1. */
1435 if (!BN_is_one(d)) {
1436 fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
1451 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1453 BIGNUM *a, *b[2], *c, *d, *e, *f;
1455 int p0[] = { 163, 7, 6, 3, 0, -1 };
1456 int p1[] = { 193, 15, 0, -1 };
1466 BN_GF2m_arr2poly(p0, b[0]);
1467 BN_GF2m_arr2poly(p1, b[1]);
1469 for (i = 0; i < num0; i++) {
1470 BN_bntest_rand(a, 512, 0, 0);
1471 BN_bntest_rand(c, 512, 0, 0);
1472 for (j = 0; j < 2; j++) {
1473 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1474 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1475 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1476 /* Test that ((a/c)*c)/a = 1. */
1477 if (!BN_is_one(f)) {
1478 fprintf(stderr, "GF(2^m) modular division test failed!\n");
1495 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1497 BIGNUM *a, *b[2], *c, *d, *e, *f;
1499 int p0[] = { 163, 7, 6, 3, 0, -1 };
1500 int p1[] = { 193, 15, 0, -1 };
1510 BN_GF2m_arr2poly(p0, b[0]);
1511 BN_GF2m_arr2poly(p1, b[1]);
1513 for (i = 0; i < num0; i++) {
1514 BN_bntest_rand(a, 512, 0, 0);
1515 BN_bntest_rand(c, 512, 0, 0);
1516 BN_bntest_rand(d, 512, 0, 0);
1517 for (j = 0; j < 2; j++) {
1518 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1519 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1520 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1522 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1523 BN_GF2m_add(f, e, f);
1524 /* Test that a^(c+d)=a^c*a^d. */
1525 if (!BN_is_zero(f)) {
1527 "GF(2^m) modular exponentiation test failed!\n");
1544 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1546 BIGNUM *a, *b[2], *c, *d, *e, *f;
1548 int p0[] = { 163, 7, 6, 3, 0, -1 };
1549 int p1[] = { 193, 15, 0, -1 };
1559 BN_GF2m_arr2poly(p0, b[0]);
1560 BN_GF2m_arr2poly(p1, b[1]);
1562 for (i = 0; i < num0; i++) {
1563 BN_bntest_rand(a, 512, 0, 0);
1564 for (j = 0; j < 2; j++) {
1565 BN_GF2m_mod(c, a, b[j]);
1566 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1567 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1568 BN_GF2m_add(f, c, e);
1569 /* Test that d^2 = a, where d = sqrt(a). */
1570 if (!BN_is_zero(f)) {
1571 fprintf(stderr, "GF(2^m) modular square root test failed!\n");
1588 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1590 BIGNUM *a, *b[2], *c, *d, *e;
1591 int i, j, s = 0, t, ret = 0;
1592 int p0[] = { 163, 7, 6, 3, 0, -1 };
1593 int p1[] = { 193, 15, 0, -1 };
1602 BN_GF2m_arr2poly(p0, b[0]);
1603 BN_GF2m_arr2poly(p1, b[1]);
1605 for (i = 0; i < num0; i++) {
1606 BN_bntest_rand(a, 512, 0, 0);
1607 for (j = 0; j < 2; j++) {
1608 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1611 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1612 BN_GF2m_add(d, c, d);
1613 BN_GF2m_mod(e, a, b[j]);
1614 BN_GF2m_add(e, e, d);
1616 * Test that solution of quadratic c satisfies c^2 + c = a.
1618 if (!BN_is_zero(e)) {
1620 "GF(2^m) modular solve quadratic test failed!\n");
1629 "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
1632 "this is very unlikely and probably indicates an error.\n");
1646 static int genprime_cb(int p, int n, BN_GENCB *arg)
1663 int test_kron(BIO *bp, BN_CTX *ctx)
1666 BIGNUM *a, *b, *r, *t;
1668 int legendre, kronecker;
1675 if (a == NULL || b == NULL || r == NULL || t == NULL)
1678 BN_GENCB_set(&cb, genprime_cb, NULL);
1681 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1682 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1683 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1684 * generate a random prime b and compare these values for a number of
1685 * random a's. (That is, we run the Solovay-Strassen primality test to
1686 * confirm that b is prime, except that we don't want to test whether b
1687 * is prime but whether BN_kronecker works.)
1690 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
1692 b->neg = rand_neg();
1695 for (i = 0; i < num0; i++) {
1696 if (!BN_bntest_rand(a, 512, 0, 0))
1698 a->neg = rand_neg();
1700 /* t := (|b|-1)/2 (note that b is odd) */
1704 if (!BN_sub_word(t, 1))
1706 if (!BN_rshift1(t, t))
1708 /* r := a^t mod b */
1711 if (!BN_mod_exp_recp(r, a, t, b, ctx))
1715 if (BN_is_word(r, 1))
1717 else if (BN_is_zero(r))
1720 if (!BN_add_word(r, 1))
1722 if (0 != BN_ucmp(r, b)) {
1723 fprintf(stderr, "Legendre symbol computation failed\n");
1729 kronecker = BN_kronecker(a, b, ctx);
1732 /* we actually need BN_kronecker(a, |b|) */
1733 if (a->neg && b->neg)
1734 kronecker = -kronecker;
1736 if (legendre != kronecker) {
1737 fprintf(stderr, "legendre != kronecker; a = ");
1738 BN_print_fp(stderr, a);
1739 fprintf(stderr, ", b = ");
1740 BN_print_fp(stderr, b);
1741 fprintf(stderr, "\n");
1760 int test_sqrt(BIO *bp, BN_CTX *ctx)
1770 if (a == NULL || p == NULL || r == NULL)
1773 BN_GENCB_set(&cb, genprime_cb, NULL);
1775 for (i = 0; i < 16; i++) {
1777 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1779 if (!BN_set_word(p, primes[i]))
1782 if (!BN_set_word(a, 32))
1784 if (!BN_set_word(r, 2 * i + 1))
1787 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
1791 p->neg = rand_neg();
1793 for (j = 0; j < num2; j++) {
1795 * construct 'a' such that it is a square modulo p, but in
1796 * general not a proper square and not reduced modulo p
1798 if (!BN_bntest_rand(r, 256, 0, 3))
1800 if (!BN_nnmod(r, r, p, ctx))
1802 if (!BN_mod_sqr(r, r, p, ctx))
1804 if (!BN_bntest_rand(a, 256, 0, 3))
1806 if (!BN_nnmod(a, a, p, ctx))
1808 if (!BN_mod_sqr(a, a, p, ctx))
1810 if (!BN_mul(a, a, r, ctx))
1813 if (!BN_sub(a, a, p))
1816 if (!BN_mod_sqrt(r, a, p, ctx))
1818 if (!BN_mod_sqr(r, r, p, ctx))
1821 if (!BN_nnmod(a, a, p, ctx))
1824 if (BN_cmp(a, r) != 0) {
1825 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1826 BN_print_fp(stderr, a);
1827 fprintf(stderr, ", r = ");
1828 BN_print_fp(stderr, r);
1829 fprintf(stderr, ", p = ");
1830 BN_print_fp(stderr, p);
1831 fprintf(stderr, "\n");
1850 int test_small_prime(BIO *bp, BN_CTX *ctx)
1852 static const int bits = 10;
1857 if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
1859 if (BN_num_bits(r) != bits) {
1860 BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
1872 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
1874 BIGNUM *a, *b, *c, *d;
1886 BN_bntest_rand(a, 200, 0, 0);
1887 a->neg = rand_neg();
1889 for (i = 0; i < num0; i++) {
1890 BN_lshift(b, a, i + 1);
1895 BIO_puts(bp, " * ");
1897 BIO_puts(bp, " - ");
1902 BN_mul(d, a, c, ctx);
1904 if (!BN_is_zero(d)) {
1905 fprintf(stderr, "Left shift test failed!\n");
1906 fprintf(stderr, "a=");
1907 BN_print_fp(stderr, a);
1908 fprintf(stderr, "\nb=");
1909 BN_print_fp(stderr, b);
1910 fprintf(stderr, "\nc=");
1911 BN_print_fp(stderr, c);
1912 fprintf(stderr, "\nd=");
1913 BN_print_fp(stderr, d);
1914 fprintf(stderr, "\n");
1925 int test_lshift1(BIO *bp)
1934 BN_bntest_rand(a, 200, 0, 0);
1935 a->neg = rand_neg();
1936 for (i = 0; i < num0; i++) {
1941 BIO_puts(bp, " * 2");
1942 BIO_puts(bp, " - ");
1949 if (!BN_is_zero(a)) {
1950 fprintf(stderr, "Left shift one test failed!\n");
1962 int test_rshift(BIO *bp, BN_CTX *ctx)
1964 BIGNUM *a, *b, *c, *d, *e;
1974 BN_bntest_rand(a, 200, 0, 0);
1975 a->neg = rand_neg();
1976 for (i = 0; i < num0; i++) {
1977 BN_rshift(b, a, i + 1);
1982 BIO_puts(bp, " / ");
1984 BIO_puts(bp, " - ");
1989 BN_div(d, e, a, c, ctx);
1991 if (!BN_is_zero(d)) {
1992 fprintf(stderr, "Right shift test failed!\n");
2004 int test_rshift1(BIO *bp)
2013 BN_bntest_rand(a, 200, 0, 0);
2014 a->neg = rand_neg();
2015 for (i = 0; i < num0; i++) {
2020 BIO_puts(bp, " / 2");
2021 BIO_puts(bp, " - ");
2028 if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
2029 fprintf(stderr, "Right shift one test failed!\n");
2042 static unsigned int neg = 0;
2043 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
2045 return (sign[(neg++) % 8]);