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);
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 int i = sizeof(w) * 8;
511 byte = (unsigned char)(w >> i);
513 fmt = byte ? "%X" : NULL;
518 BIO_printf(bp, fmt, byte);
521 /* If we haven't printed anything, at least print a zero! */
526 int test_div_word(BIO *bp)
535 for (i = 0; i < num0; i++) {
537 BN_bntest_rand(a, 512, -1, 0);
538 BN_bntest_rand(b, BN_BITS2, -1, 0);
539 } while (BN_is_zero(b));
543 r = BN_div_word(b, s);
567 if (!BN_is_zero(b)) {
568 fprintf(stderr, "Division (word) test failed!\n");
577 int test_div_recp(BIO *bp, BN_CTX *ctx)
579 BIGNUM *a, *b, *c, *d, *e;
583 recp = BN_RECP_CTX_new();
590 for (i = 0; i < num0 + num1; i++) {
592 BN_bntest_rand(a, 400, 0, 0);
597 BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
600 BN_RECP_CTX_set(recp, b, ctx);
601 BN_div_recp(d, c, a, recp, ctx);
621 BN_mul(e, d, b, ctx);
624 if (!BN_is_zero(d)) {
625 fprintf(stderr, "Reciprocal division test failed!\n");
626 fprintf(stderr, "a=");
627 BN_print_fp(stderr, a);
628 fprintf(stderr, "\nb=");
629 BN_print_fp(stderr, b);
630 fprintf(stderr, "\n");
639 BN_RECP_CTX_free(recp);
643 int test_mul(BIO *bp)
645 BIGNUM *a, *b, *c, *d, *e;
659 for (i = 0; i < num0 + num1; i++) {
661 BN_bntest_rand(a, 100, 0, 0);
662 BN_bntest_rand(b, 100, 0, 0);
664 BN_bntest_rand(b, i - num1, 0, 0);
667 BN_mul(c, a, b, ctx);
678 BN_div(d, e, c, a, ctx);
680 if (!BN_is_zero(d) || !BN_is_zero(e)) {
681 fprintf(stderr, "Multiplication test failed!\n");
694 int test_sqr(BIO *bp, BN_CTX *ctx)
696 BIGNUM *a, *c, *d, *e;
703 if (a == NULL || c == NULL || d == NULL || e == NULL) {
707 for (i = 0; i < num0; i++) {
708 BN_bntest_rand(a, 40 + i * 10, 0, 0);
721 BN_div(d, e, c, a, ctx);
723 if (!BN_is_zero(d) || !BN_is_zero(e)) {
724 fprintf(stderr, "Square test failed!\n");
729 /* Regression test for a BN_sqr overflow bug. */
731 "80000000000000008000000000000001"
732 "FFFFFFFFFFFFFFFE0000000000000000");
744 BN_mul(d, a, a, ctx);
746 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
747 "different results!\n");
751 /* Regression test for a BN_sqr overflow bug. */
753 "80000000000000000000000080000001"
754 "FFFFFFFE000000000000000000000000");
766 BN_mul(d, a, a, ctx);
768 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
769 "different results!\n");
781 int test_mont(BIO *bp, BN_CTX *ctx)
783 BIGNUM *a, *b, *c, *d, *A, *B;
796 mont = BN_MONT_CTX_new();
801 if (BN_MONT_CTX_set(mont, n, ctx)) {
802 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
807 if (BN_MONT_CTX_set(mont, n, ctx)) {
808 fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
812 BN_bntest_rand(a, 100, 0, 0);
813 BN_bntest_rand(b, 100, 0, 0);
814 for (i = 0; i < num2; i++) {
815 int bits = (200 * (i + 1)) / num2;
819 BN_bntest_rand(n, bits, 0, 1);
820 BN_MONT_CTX_set(mont, n, ctx);
822 BN_nnmod(a, a, n, ctx);
823 BN_nnmod(b, b, n, ctx);
825 BN_to_montgomery(A, a, mont, ctx);
826 BN_to_montgomery(B, b, mont, ctx);
828 BN_mod_mul_montgomery(c, A, B, mont, ctx);
829 BN_from_montgomery(A, c, mont, ctx);
836 BN_print(bp, &mont->N);
842 BN_mod_mul(d, a, b, n, ctx);
844 if (!BN_is_zero(d)) {
845 fprintf(stderr, "Montgomery multiplication test failed!\n");
849 BN_MONT_CTX_free(mont);
860 int test_mod(BIO *bp, BN_CTX *ctx)
862 BIGNUM *a, *b, *c, *d, *e;
871 BN_bntest_rand(a, 1024, 0, 0);
872 for (i = 0; i < num0; i++) {
873 BN_bntest_rand(b, 450 + i * 10, 0, 0);
876 BN_mod(c, a, b, ctx);
887 BN_div(d, e, a, b, ctx);
889 if (!BN_is_zero(e)) {
890 fprintf(stderr, "Modulo test failed!\n");
902 int test_mod_mul(BIO *bp, BN_CTX *ctx)
904 BIGNUM *a, *b, *c, *d, *e;
916 if (BN_mod_mul(e, a, b, c, ctx)) {
917 fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
921 for (j = 0; j < 3; j++) {
922 BN_bntest_rand(c, 1024, 0, 0);
923 for (i = 0; i < num0; i++) {
924 BN_bntest_rand(a, 475 + i * 10, 0, 0);
925 BN_bntest_rand(b, 425 + i * 11, 0, 0);
928 if (!BN_mod_mul(e, a, b, c, ctx)) {
931 while ((l = ERR_get_error()))
932 fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
942 if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
944 * If (a*b) % c is negative, c must be added in order
945 * to obtain the normalized remainder (new with
946 * OpenSSL 0.9.7, previous versions of BN_mod_mul
947 * could generate negative results)
957 BN_mul(d, a, b, ctx);
959 BN_div(a, b, d, c, ctx);
960 if (!BN_is_zero(b)) {
961 fprintf(stderr, "Modulo multiply test failed!\n");
962 ERR_print_errors_fp(stderr);
975 int test_mod_exp(BIO *bp, BN_CTX *ctx)
977 BIGNUM *a, *b, *c, *d, *e;
989 if (BN_mod_exp(d, a, b, c, ctx)) {
990 fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
994 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
995 for (i = 0; i < num2; i++) {
996 BN_bntest_rand(a, 20 + i * 5, 0, 0);
997 BN_bntest_rand(b, 2 + i, 0, 0);
999 if (!BN_mod_exp(d, a, b, c, ctx))
1005 BIO_puts(bp, " ^ ");
1007 BIO_puts(bp, " % ");
1009 BIO_puts(bp, " - ");
1014 BN_exp(e, a, b, ctx);
1016 BN_div(a, b, e, c, ctx);
1017 if (!BN_is_zero(b)) {
1018 fprintf(stderr, "Modulo exponentiation test failed!\n");
1023 /* Regression test for carry propagation bug in sqr8x_reduction */
1024 BN_hex2bn(&a, "050505050505");
1025 BN_hex2bn(&b, "02");
1027 "4141414141414141414141274141414141414141414141414141414141414141"
1028 "4141414141414141414141414141414141414141414141414141414141414141"
1029 "4141414141414141414141800000000000000000000000000000000000000000"
1030 "0000000000000000000000000000000000000000000000000000000000000000"
1031 "0000000000000000000000000000000000000000000000000000000000000000"
1032 "0000000000000000000000000000000000000000000000000000000001");
1033 BN_mod_exp(d, a, b, c, ctx);
1034 BN_mul(e, a, a, ctx);
1036 fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
1048 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1050 BIGNUM *a, *b, *c, *d, *e;
1062 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1063 fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
1069 if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
1070 fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
1075 BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1076 for (i = 0; i < num2; i++) {
1077 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1078 BN_bntest_rand(b, 2 + i, 0, 0);
1080 if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
1086 BIO_puts(bp, " ^ ");
1088 BIO_puts(bp, " % ");
1090 BIO_puts(bp, " - ");
1095 BN_exp(e, a, b, ctx);
1097 BN_div(a, b, e, c, ctx);
1098 if (!BN_is_zero(b)) {
1099 fprintf(stderr, "Modulo exponentiation test failed!\n");
1112 * Test constant-time modular exponentiation with 1024-bit inputs, which on
1113 * x86_64 cause a different code branch to be taken.
1115 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1117 BIGNUM *a, *p, *m, *d, *e;
1125 mont = BN_MONT_CTX_new();
1127 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
1129 BN_bntest_rand(a, 1024, 0, 0);
1131 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1133 if (!BN_is_one(d)) {
1134 fprintf(stderr, "Modular exponentiation test failed!\n");
1138 BN_bntest_rand(p, 1024, 0, 0);
1140 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1142 if (!BN_is_zero(d)) {
1143 fprintf(stderr, "Modular exponentiation test failed!\n");
1147 * Craft an input whose Montgomery representation is 1, i.e., shorter
1148 * than the modulus m, in order to test the const time precomputation
1149 * scattering/gathering.
1152 BN_MONT_CTX_set(mont, m, ctx);
1153 if (!BN_from_montgomery(e, a, mont, ctx))
1155 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1157 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1159 if (BN_cmp(a, d) != 0) {
1160 fprintf(stderr, "Modular exponentiation test failed!\n");
1163 /* Finally, some regular test vectors. */
1164 BN_bntest_rand(e, 1024, 0, 0);
1165 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1167 if (!BN_mod_exp_simple(a, e, p, m, ctx))
1169 if (BN_cmp(a, d) != 0) {
1170 fprintf(stderr, "Modular exponentiation test failed!\n");
1173 BN_MONT_CTX_free(mont);
1182 int test_exp(BIO *bp, BN_CTX *ctx)
1184 BIGNUM *a, *b, *d, *e, *one;
1194 for (i = 0; i < num2; i++) {
1195 BN_bntest_rand(a, 20 + i * 5, 0, 0);
1196 BN_bntest_rand(b, 2 + i, 0, 0);
1198 if (BN_exp(d, a, b, ctx) <= 0)
1204 BIO_puts(bp, " ^ ");
1206 BIO_puts(bp, " - ");
1212 for (; !BN_is_zero(b); BN_sub(b, b, one))
1213 BN_mul(e, e, a, ctx);
1215 if (!BN_is_zero(e)) {
1216 fprintf(stderr, "Exponentiation test failed!\n");
1228 #ifndef OPENSSL_NO_EC2M
1229 int test_gf2m_add(BIO *bp)
1238 for (i = 0; i < num0; i++) {
1239 BN_rand(a, 512, 0, 0);
1240 BN_copy(b, BN_value_one());
1241 a->neg = rand_neg();
1242 b->neg = rand_neg();
1243 BN_GF2m_add(c, a, b);
1244 /* Test that two added values have the correct parity. */
1245 if ((BN_is_odd(a) && BN_is_odd(c))
1246 || (!BN_is_odd(a) && !BN_is_odd(c))) {
1247 fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1250 BN_GF2m_add(c, c, c);
1251 /* Test that c + c = 0. */
1252 if (!BN_is_zero(c)) {
1253 fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1265 int test_gf2m_mod(BIO *bp)
1267 BIGNUM *a, *b[2], *c, *d, *e;
1269 int p0[] = { 163, 7, 6, 3, 0, -1 };
1270 int p1[] = { 193, 15, 0, -1 };
1279 BN_GF2m_arr2poly(p0, b[0]);
1280 BN_GF2m_arr2poly(p1, b[1]);
1282 for (i = 0; i < num0; i++) {
1283 BN_bntest_rand(a, 1024, 0, 0);
1284 for (j = 0; j < 2; j++) {
1285 BN_GF2m_mod(c, a, b[j]);
1286 BN_GF2m_add(d, a, c);
1287 BN_GF2m_mod(e, d, b[j]);
1288 /* Test that a + (a mod p) mod p == 0. */
1289 if (!BN_is_zero(e)) {
1290 fprintf(stderr, "GF(2^m) modulo test failed!\n");
1306 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1308 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
1310 int p0[] = { 163, 7, 6, 3, 0, -1 };
1311 int p1[] = { 193, 15, 0, -1 };
1323 BN_GF2m_arr2poly(p0, b[0]);
1324 BN_GF2m_arr2poly(p1, b[1]);
1326 for (i = 0; i < num0; i++) {
1327 BN_bntest_rand(a, 1024, 0, 0);
1328 BN_bntest_rand(c, 1024, 0, 0);
1329 BN_bntest_rand(d, 1024, 0, 0);
1330 for (j = 0; j < 2; j++) {
1331 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1332 BN_GF2m_add(f, a, d);
1333 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1334 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1335 BN_GF2m_add(f, e, g);
1336 BN_GF2m_add(f, f, h);
1337 /* Test that (a+d)*c = a*c + d*c. */
1338 if (!BN_is_zero(f)) {
1340 "GF(2^m) modular multiplication test failed!\n");
1359 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1361 BIGNUM *a, *b[2], *c, *d;
1363 int p0[] = { 163, 7, 6, 3, 0, -1 };
1364 int p1[] = { 193, 15, 0, -1 };
1372 BN_GF2m_arr2poly(p0, b[0]);
1373 BN_GF2m_arr2poly(p1, b[1]);
1375 for (i = 0; i < num0; i++) {
1376 BN_bntest_rand(a, 1024, 0, 0);
1377 for (j = 0; j < 2; j++) {
1378 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1380 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1381 BN_GF2m_add(d, c, d);
1382 /* Test that a*a = a^2. */
1383 if (!BN_is_zero(d)) {
1384 fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
1399 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1401 BIGNUM *a, *b[2], *c, *d;
1403 int p0[] = { 163, 7, 6, 3, 0, -1 };
1404 int p1[] = { 193, 15, 0, -1 };
1412 BN_GF2m_arr2poly(p0, b[0]);
1413 BN_GF2m_arr2poly(p1, b[1]);
1415 for (i = 0; i < num0; i++) {
1416 BN_bntest_rand(a, 512, 0, 0);
1417 for (j = 0; j < 2; j++) {
1418 BN_GF2m_mod_inv(c, a, b[j], ctx);
1419 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1420 /* Test that ((1/a)*a) = 1. */
1421 if (!BN_is_one(d)) {
1422 fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
1437 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1439 BIGNUM *a, *b[2], *c, *d, *e, *f;
1441 int p0[] = { 163, 7, 6, 3, 0, -1 };
1442 int p1[] = { 193, 15, 0, -1 };
1452 BN_GF2m_arr2poly(p0, b[0]);
1453 BN_GF2m_arr2poly(p1, b[1]);
1455 for (i = 0; i < num0; i++) {
1456 BN_bntest_rand(a, 512, 0, 0);
1457 BN_bntest_rand(c, 512, 0, 0);
1458 for (j = 0; j < 2; j++) {
1459 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1460 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1461 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1462 /* Test that ((a/c)*c)/a = 1. */
1463 if (!BN_is_one(f)) {
1464 fprintf(stderr, "GF(2^m) modular division test failed!\n");
1481 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1483 BIGNUM *a, *b[2], *c, *d, *e, *f;
1485 int p0[] = { 163, 7, 6, 3, 0, -1 };
1486 int p1[] = { 193, 15, 0, -1 };
1496 BN_GF2m_arr2poly(p0, b[0]);
1497 BN_GF2m_arr2poly(p1, b[1]);
1499 for (i = 0; i < num0; i++) {
1500 BN_bntest_rand(a, 512, 0, 0);
1501 BN_bntest_rand(c, 512, 0, 0);
1502 BN_bntest_rand(d, 512, 0, 0);
1503 for (j = 0; j < 2; j++) {
1504 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1505 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1506 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1508 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1509 BN_GF2m_add(f, e, f);
1510 /* Test that a^(c+d)=a^c*a^d. */
1511 if (!BN_is_zero(f)) {
1513 "GF(2^m) modular exponentiation test failed!\n");
1530 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1532 BIGNUM *a, *b[2], *c, *d, *e, *f;
1534 int p0[] = { 163, 7, 6, 3, 0, -1 };
1535 int p1[] = { 193, 15, 0, -1 };
1545 BN_GF2m_arr2poly(p0, b[0]);
1546 BN_GF2m_arr2poly(p1, b[1]);
1548 for (i = 0; i < num0; i++) {
1549 BN_bntest_rand(a, 512, 0, 0);
1550 for (j = 0; j < 2; j++) {
1551 BN_GF2m_mod(c, a, b[j]);
1552 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1553 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1554 BN_GF2m_add(f, c, e);
1555 /* Test that d^2 = a, where d = sqrt(a). */
1556 if (!BN_is_zero(f)) {
1557 fprintf(stderr, "GF(2^m) modular square root test failed!\n");
1574 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1576 BIGNUM *a, *b[2], *c, *d, *e;
1577 int i, j, s = 0, t, ret = 0;
1578 int p0[] = { 163, 7, 6, 3, 0, -1 };
1579 int p1[] = { 193, 15, 0, -1 };
1588 BN_GF2m_arr2poly(p0, b[0]);
1589 BN_GF2m_arr2poly(p1, b[1]);
1591 for (i = 0; i < num0; i++) {
1592 BN_bntest_rand(a, 512, 0, 0);
1593 for (j = 0; j < 2; j++) {
1594 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1597 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1598 BN_GF2m_add(d, c, d);
1599 BN_GF2m_mod(e, a, b[j]);
1600 BN_GF2m_add(e, e, d);
1602 * Test that solution of quadratic c satisfies c^2 + c = a.
1604 if (!BN_is_zero(e)) {
1606 "GF(2^m) modular solve quadratic test failed!\n");
1615 "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
1618 "this is very unlikely and probably indicates an error.\n");
1632 static int genprime_cb(int p, int n, BN_GENCB *arg)
1649 int test_kron(BIO *bp, BN_CTX *ctx)
1652 BIGNUM *a, *b, *r, *t;
1654 int legendre, kronecker;
1661 if (a == NULL || b == NULL || r == NULL || t == NULL)
1664 BN_GENCB_set(&cb, genprime_cb, NULL);
1667 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1668 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1669 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1670 * generate a random prime b and compare these values for a number of
1671 * random a's. (That is, we run the Solovay-Strassen primality test to
1672 * confirm that b is prime, except that we don't want to test whether b
1673 * is prime but whether BN_kronecker works.)
1676 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
1678 b->neg = rand_neg();
1681 for (i = 0; i < num0; i++) {
1682 if (!BN_bntest_rand(a, 512, 0, 0))
1684 a->neg = rand_neg();
1686 /* t := (|b|-1)/2 (note that b is odd) */
1690 if (!BN_sub_word(t, 1))
1692 if (!BN_rshift1(t, t))
1694 /* r := a^t mod b */
1697 if (!BN_mod_exp_recp(r, a, t, b, ctx))
1701 if (BN_is_word(r, 1))
1703 else if (BN_is_zero(r))
1706 if (!BN_add_word(r, 1))
1708 if (0 != BN_ucmp(r, b)) {
1709 fprintf(stderr, "Legendre symbol computation failed\n");
1715 kronecker = BN_kronecker(a, b, ctx);
1718 /* we actually need BN_kronecker(a, |b|) */
1719 if (a->neg && b->neg)
1720 kronecker = -kronecker;
1722 if (legendre != kronecker) {
1723 fprintf(stderr, "legendre != kronecker; a = ");
1724 BN_print_fp(stderr, a);
1725 fprintf(stderr, ", b = ");
1726 BN_print_fp(stderr, b);
1727 fprintf(stderr, "\n");
1746 int test_sqrt(BIO *bp, BN_CTX *ctx)
1756 if (a == NULL || p == NULL || r == NULL)
1759 BN_GENCB_set(&cb, genprime_cb, NULL);
1761 for (i = 0; i < 16; i++) {
1763 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1765 if (!BN_set_word(p, primes[i]))
1768 if (!BN_set_word(a, 32))
1770 if (!BN_set_word(r, 2 * i + 1))
1773 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
1777 p->neg = rand_neg();
1779 for (j = 0; j < num2; j++) {
1781 * construct 'a' such that it is a square modulo p, but in
1782 * general not a proper square and not reduced modulo p
1784 if (!BN_bntest_rand(r, 256, 0, 3))
1786 if (!BN_nnmod(r, r, p, ctx))
1788 if (!BN_mod_sqr(r, r, p, ctx))
1790 if (!BN_bntest_rand(a, 256, 0, 3))
1792 if (!BN_nnmod(a, a, p, ctx))
1794 if (!BN_mod_sqr(a, a, p, ctx))
1796 if (!BN_mul(a, a, r, ctx))
1799 if (!BN_sub(a, a, p))
1802 if (!BN_mod_sqrt(r, a, p, ctx))
1804 if (!BN_mod_sqr(r, r, p, ctx))
1807 if (!BN_nnmod(a, a, p, ctx))
1810 if (BN_cmp(a, r) != 0) {
1811 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1812 BN_print_fp(stderr, a);
1813 fprintf(stderr, ", r = ");
1814 BN_print_fp(stderr, r);
1815 fprintf(stderr, ", p = ");
1816 BN_print_fp(stderr, p);
1817 fprintf(stderr, "\n");
1836 int test_small_prime(BIO *bp, BN_CTX *ctx)
1838 static const int bits = 10;
1843 if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
1845 if (BN_num_bits(r) != bits) {
1846 BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
1858 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
1860 BIGNUM *a, *b, *c, *d;
1872 BN_bntest_rand(a, 200, 0, 0);
1873 a->neg = rand_neg();
1875 for (i = 0; i < num0; i++) {
1876 BN_lshift(b, a, i + 1);
1881 BIO_puts(bp, " * ");
1883 BIO_puts(bp, " - ");
1888 BN_mul(d, a, c, ctx);
1890 if (!BN_is_zero(d)) {
1891 fprintf(stderr, "Left shift test failed!\n");
1892 fprintf(stderr, "a=");
1893 BN_print_fp(stderr, a);
1894 fprintf(stderr, "\nb=");
1895 BN_print_fp(stderr, b);
1896 fprintf(stderr, "\nc=");
1897 BN_print_fp(stderr, c);
1898 fprintf(stderr, "\nd=");
1899 BN_print_fp(stderr, d);
1900 fprintf(stderr, "\n");
1911 int test_lshift1(BIO *bp)
1920 BN_bntest_rand(a, 200, 0, 0);
1921 a->neg = rand_neg();
1922 for (i = 0; i < num0; i++) {
1927 BIO_puts(bp, " * 2");
1928 BIO_puts(bp, " - ");
1935 if (!BN_is_zero(a)) {
1936 fprintf(stderr, "Left shift one test failed!\n");
1948 int test_rshift(BIO *bp, BN_CTX *ctx)
1950 BIGNUM *a, *b, *c, *d, *e;
1960 BN_bntest_rand(a, 200, 0, 0);
1961 a->neg = rand_neg();
1962 for (i = 0; i < num0; i++) {
1963 BN_rshift(b, a, i + 1);
1968 BIO_puts(bp, " / ");
1970 BIO_puts(bp, " - ");
1975 BN_div(d, e, a, c, ctx);
1977 if (!BN_is_zero(d)) {
1978 fprintf(stderr, "Right shift test failed!\n");
1990 int test_rshift1(BIO *bp)
1999 BN_bntest_rand(a, 200, 0, 0);
2000 a->neg = rand_neg();
2001 for (i = 0; i < num0; i++) {
2006 BIO_puts(bp, " / 2");
2007 BIO_puts(bp, " - ");
2014 if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
2015 fprintf(stderr, "Right shift one test failed!\n");
2028 static unsigned int neg = 0;
2029 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
2031 return (sign[(neg++) % 8]);