diff options
-rw-r--r-- | src/regress/lib/libcrypto/bn/general/bntest.c | 1600 |
1 files changed, 834 insertions, 766 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/bntest.c b/src/regress/lib/libcrypto/bn/general/bntest.c index ad000fcf56..6882702e93 100644 --- a/src/regress/lib/libcrypto/bn/general/bntest.c +++ b/src/regress/lib/libcrypto/bn/general/bntest.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -58,13 +58,13 @@ | |||
58 | /* ==================================================================== | 58 | /* ==================================================================== |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
60 | * | 60 | * |
61 | * Portions of the attached software ("Contribution") are developed by | 61 | * Portions of the attached software ("Contribution") are developed by |
62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | 62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
63 | * | 63 | * |
64 | * The Contribution is licensed pursuant to the Eric Young open source | 64 | * The Contribution is licensed pursuant to the Eric Young open source |
65 | * license provided above. | 65 | * license provided above. |
66 | * | 66 | * |
67 | * The binary polynomial arithmetic software is originally written by | 67 | * The binary polynomial arithmetic software is originally written by |
68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. | 68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
69 | * | 69 | * |
70 | */ | 70 | */ |
@@ -91,34 +91,34 @@ const int num2 = 5; /* number of tests for slow functions */ | |||
91 | int test_add(BIO *bp); | 91 | int test_add(BIO *bp); |
92 | int test_sub(BIO *bp); | 92 | int test_sub(BIO *bp); |
93 | int test_lshift1(BIO *bp); | 93 | int test_lshift1(BIO *bp); |
94 | int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_); | 94 | int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_); |
95 | int test_rshift1(BIO *bp); | 95 | int test_rshift1(BIO *bp); |
96 | int test_rshift(BIO *bp,BN_CTX *ctx); | 96 | int test_rshift(BIO *bp, BN_CTX *ctx); |
97 | int test_div(BIO *bp,BN_CTX *ctx); | 97 | int test_div(BIO *bp, BN_CTX *ctx); |
98 | int test_div_word(BIO *bp); | 98 | int test_div_word(BIO *bp); |
99 | int test_div_recp(BIO *bp,BN_CTX *ctx); | 99 | int test_div_recp(BIO *bp, BN_CTX *ctx); |
100 | int test_mul(BIO *bp); | 100 | int test_mul(BIO *bp); |
101 | int test_sqr(BIO *bp,BN_CTX *ctx); | 101 | int test_sqr(BIO *bp, BN_CTX *ctx); |
102 | int test_mont(BIO *bp,BN_CTX *ctx); | 102 | int test_mont(BIO *bp, BN_CTX *ctx); |
103 | int test_mod(BIO *bp,BN_CTX *ctx); | 103 | int test_mod(BIO *bp, BN_CTX *ctx); |
104 | int test_mod_mul(BIO *bp,BN_CTX *ctx); | 104 | int test_mod_mul(BIO *bp, BN_CTX *ctx); |
105 | int test_mod_exp(BIO *bp,BN_CTX *ctx); | 105 | int test_mod_exp(BIO *bp, BN_CTX *ctx); |
106 | int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx); | 106 | int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx); |
107 | int test_exp(BIO *bp,BN_CTX *ctx); | 107 | int test_exp(BIO *bp, BN_CTX *ctx); |
108 | int test_gf2m_add(BIO *bp); | 108 | int test_gf2m_add(BIO *bp); |
109 | int test_gf2m_mod(BIO *bp); | 109 | int test_gf2m_mod(BIO *bp); |
110 | int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx); | 110 | int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx); |
111 | int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx); | 111 | int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx); |
112 | int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx); | 112 | int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx); |
113 | int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx); | 113 | int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx); |
114 | int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx); | 114 | int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx); |
115 | int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx); | 115 | int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx); |
116 | int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx); | 116 | int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx); |
117 | int test_kron(BIO *bp,BN_CTX *ctx); | 117 | int test_kron(BIO *bp, BN_CTX *ctx); |
118 | int test_sqrt(BIO *bp,BN_CTX *ctx); | 118 | int test_sqrt(BIO *bp, BN_CTX *ctx); |
119 | int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx); | 119 | int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx); |
120 | int rand_neg(void); | 120 | int rand_neg(void); |
121 | static int results=0; | 121 | static int results = 0; |
122 | 122 | ||
123 | static const unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9" | 123 | static const unsigned char lst[]="\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"; | 124 | "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0"; |
@@ -137,17 +137,18 @@ main(int argc, char *argv[]) | |||
137 | { | 137 | { |
138 | BN_CTX *ctx; | 138 | BN_CTX *ctx; |
139 | BIO *out; | 139 | BIO *out; |
140 | char *outfile=NULL; | 140 | char *outfile = NULL; |
141 | 141 | ||
142 | results = 0; | 142 | results = 0; |
143 | 143 | ||
144 | argc--; | 144 | argc--; |
145 | argv++; | 145 | argv++; |
146 | while (argc >= 1) { | 146 | while (argc >= 1) { |
147 | if (strcmp(*argv,"-results") == 0) | 147 | if (strcmp(*argv, "-results") == 0) |
148 | results=1; | 148 | results = 1; |
149 | else if (strcmp(*argv,"-out") == 0) { | 149 | else if (strcmp(*argv, "-out") == 0) { |
150 | if (--argc < 1) break; | 150 | if (--argc < 1) |
151 | break; | ||
151 | outfile= *(++argv); | 152 | outfile= *(++argv); |
152 | } | 153 | } |
153 | argc--; | 154 | argc--; |
@@ -155,102 +156,123 @@ main(int argc, char *argv[]) | |||
155 | } | 156 | } |
156 | 157 | ||
157 | 158 | ||
158 | ctx=BN_CTX_new(); | 159 | ctx = BN_CTX_new(); |
159 | if (ctx == NULL) exit(1); | 160 | if (ctx == NULL) |
161 | exit(1); | ||
160 | 162 | ||
161 | out=BIO_new(BIO_s_file()); | 163 | out = BIO_new(BIO_s_file()); |
162 | if (out == NULL) exit(1); | 164 | if (out == NULL) |
165 | exit(1); | ||
163 | if (outfile == NULL) { | 166 | if (outfile == NULL) { |
164 | BIO_set_fp(out,stdout,BIO_NOCLOSE); | 167 | BIO_set_fp(out, stdout, BIO_NOCLOSE); |
165 | } else { | 168 | } else { |
166 | if (!BIO_write_filename(out,outfile)) { | 169 | if (!BIO_write_filename(out, outfile)) { |
167 | perror(outfile); | 170 | perror(outfile); |
168 | exit(1); | 171 | exit(1); |
169 | } | 172 | } |
170 | } | 173 | } |
171 | 174 | ||
172 | if (!results) | 175 | if (!results) |
173 | BIO_puts(out,"obase=16\nibase=16\n"); | 176 | BIO_puts(out, "obase=16\nibase=16\n"); |
174 | 177 | ||
175 | message(out,"BN_add"); | 178 | message(out, "BN_add"); |
176 | if (!test_add(out)) goto err; | 179 | if (!test_add(out)) |
180 | goto err; | ||
177 | (void)BIO_flush(out); | 181 | (void)BIO_flush(out); |
178 | 182 | ||
179 | message(out,"BN_sub"); | 183 | message(out, "BN_sub"); |
180 | if (!test_sub(out)) goto err; | 184 | if (!test_sub(out)) |
185 | goto err; | ||
181 | (void)BIO_flush(out); | 186 | (void)BIO_flush(out); |
182 | 187 | ||
183 | message(out,"BN_lshift1"); | 188 | message(out, "BN_lshift1"); |
184 | if (!test_lshift1(out)) goto err; | 189 | if (!test_lshift1(out)) |
190 | goto err; | ||
185 | (void)BIO_flush(out); | 191 | (void)BIO_flush(out); |
186 | 192 | ||
187 | message(out,"BN_lshift (fixed)"); | 193 | message(out, "BN_lshift (fixed)"); |
188 | if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL))) | 194 | if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL))) |
189 | goto err; | 195 | goto err; |
190 | (void)BIO_flush(out); | 196 | (void)BIO_flush(out); |
191 | 197 | ||
192 | message(out,"BN_lshift"); | 198 | message(out, "BN_lshift"); |
193 | if (!test_lshift(out,ctx,NULL)) goto err; | 199 | if (!test_lshift(out, ctx, NULL)) |
200 | goto err; | ||
194 | (void)BIO_flush(out); | 201 | (void)BIO_flush(out); |
195 | 202 | ||
196 | message(out,"BN_rshift1"); | 203 | message(out, "BN_rshift1"); |
197 | if (!test_rshift1(out)) goto err; | 204 | if (!test_rshift1(out)) |
205 | goto err; | ||
198 | (void)BIO_flush(out); | 206 | (void)BIO_flush(out); |
199 | 207 | ||
200 | message(out,"BN_rshift"); | 208 | message(out, "BN_rshift"); |
201 | if (!test_rshift(out,ctx)) goto err; | 209 | if (!test_rshift(out, ctx)) |
210 | goto err; | ||
202 | (void)BIO_flush(out); | 211 | (void)BIO_flush(out); |
203 | 212 | ||
204 | message(out,"BN_sqr"); | 213 | message(out, "BN_sqr"); |
205 | if (!test_sqr(out,ctx)) goto err; | 214 | if (!test_sqr(out, ctx)) |
215 | goto err; | ||
206 | (void)BIO_flush(out); | 216 | (void)BIO_flush(out); |
207 | 217 | ||
208 | message(out,"BN_mul"); | 218 | message(out, "BN_mul"); |
209 | if (!test_mul(out)) goto err; | 219 | if (!test_mul(out)) |
220 | goto err; | ||
210 | (void)BIO_flush(out); | 221 | (void)BIO_flush(out); |
211 | 222 | ||
212 | message(out,"BN_div"); | 223 | message(out, "BN_div"); |
213 | if (!test_div(out,ctx)) goto err; | 224 | if (!test_div(out, ctx)) |
225 | goto err; | ||
214 | (void)BIO_flush(out); | 226 | (void)BIO_flush(out); |
215 | 227 | ||
216 | message(out,"BN_div_word"); | 228 | message(out, "BN_div_word"); |
217 | if (!test_div_word(out)) goto err; | 229 | if (!test_div_word(out)) |
230 | goto err; | ||
218 | (void)BIO_flush(out); | 231 | (void)BIO_flush(out); |
219 | 232 | ||
220 | message(out,"BN_div_recp"); | 233 | message(out, "BN_div_recp"); |
221 | if (!test_div_recp(out,ctx)) goto err; | 234 | if (!test_div_recp(out, ctx)) |
235 | goto err; | ||
222 | (void)BIO_flush(out); | 236 | (void)BIO_flush(out); |
223 | 237 | ||
224 | message(out,"BN_mod"); | 238 | message(out, "BN_mod"); |
225 | if (!test_mod(out,ctx)) goto err; | 239 | if (!test_mod(out, ctx)) |
240 | goto err; | ||
226 | (void)BIO_flush(out); | 241 | (void)BIO_flush(out); |
227 | 242 | ||
228 | message(out,"BN_mod_mul"); | 243 | message(out, "BN_mod_mul"); |
229 | if (!test_mod_mul(out,ctx)) goto err; | 244 | if (!test_mod_mul(out, ctx)) |
245 | goto err; | ||
230 | (void)BIO_flush(out); | 246 | (void)BIO_flush(out); |
231 | 247 | ||
232 | message(out,"BN_mont"); | 248 | message(out, "BN_mont"); |
233 | if (!test_mont(out,ctx)) goto err; | 249 | if (!test_mont(out, ctx)) |
250 | goto err; | ||
234 | (void)BIO_flush(out); | 251 | (void)BIO_flush(out); |
235 | 252 | ||
236 | message(out,"BN_mod_exp"); | 253 | message(out, "BN_mod_exp"); |
237 | if (!test_mod_exp(out,ctx)) goto err; | 254 | if (!test_mod_exp(out, ctx)) |
255 | goto err; | ||
238 | (void)BIO_flush(out); | 256 | (void)BIO_flush(out); |
239 | 257 | ||
240 | message(out,"BN_mod_exp_mont_consttime"); | 258 | message(out, "BN_mod_exp_mont_consttime"); |
241 | if (!test_mod_exp_mont_consttime(out,ctx)) goto err; | 259 | if (!test_mod_exp_mont_consttime(out, ctx)) |
260 | goto err; | ||
242 | (void)BIO_flush(out); | 261 | (void)BIO_flush(out); |
243 | 262 | ||
244 | message(out,"BN_exp"); | 263 | message(out, "BN_exp"); |
245 | if (!test_exp(out,ctx)) goto err; | 264 | if (!test_exp(out, ctx)) |
265 | goto err; | ||
246 | (void)BIO_flush(out); | 266 | (void)BIO_flush(out); |
247 | 267 | ||
248 | message(out,"BN_kronecker"); | 268 | message(out, "BN_kronecker"); |
249 | if (!test_kron(out,ctx)) goto err; | 269 | if (!test_kron(out, ctx)) |
270 | goto err; | ||
250 | (void)BIO_flush(out); | 271 | (void)BIO_flush(out); |
251 | 272 | ||
252 | message(out,"BN_mod_sqrt"); | 273 | message(out, "BN_mod_sqrt"); |
253 | if (!test_sqrt(out,ctx)) goto err; | 274 | if (!test_sqrt(out, ctx)) |
275 | goto err; | ||
254 | (void)BIO_flush(out); | 276 | (void)BIO_flush(out); |
255 | 277 | ||
256 | message(out, "Modexp with different sizes"); | 278 | message(out, "Modexp with different sizes"); |
@@ -259,40 +281,49 @@ main(int argc, char *argv[]) | |||
259 | (void)BIO_flush(out); | 281 | (void)BIO_flush(out); |
260 | 282 | ||
261 | #ifndef OPENSSL_NO_EC2M | 283 | #ifndef OPENSSL_NO_EC2M |
262 | message(out,"BN_GF2m_add"); | 284 | message(out, "BN_GF2m_add"); |
263 | if (!test_gf2m_add(out)) goto err; | 285 | if (!test_gf2m_add(out)) |
286 | goto err; | ||
264 | (void)BIO_flush(out); | 287 | (void)BIO_flush(out); |
265 | 288 | ||
266 | message(out,"BN_GF2m_mod"); | 289 | message(out, "BN_GF2m_mod"); |
267 | if (!test_gf2m_mod(out)) goto err; | 290 | if (!test_gf2m_mod(out)) |
291 | goto err; | ||
268 | (void)BIO_flush(out); | 292 | (void)BIO_flush(out); |
269 | 293 | ||
270 | message(out,"BN_GF2m_mod_mul"); | 294 | message(out, "BN_GF2m_mod_mul"); |
271 | if (!test_gf2m_mod_mul(out,ctx)) goto err; | 295 | if (!test_gf2m_mod_mul(out, ctx)) |
296 | goto err; | ||
272 | (void)BIO_flush(out); | 297 | (void)BIO_flush(out); |
273 | 298 | ||
274 | message(out,"BN_GF2m_mod_sqr"); | 299 | message(out, "BN_GF2m_mod_sqr"); |
275 | if (!test_gf2m_mod_sqr(out,ctx)) goto err; | 300 | if (!test_gf2m_mod_sqr(out, ctx)) |
301 | goto err; | ||
276 | (void)BIO_flush(out); | 302 | (void)BIO_flush(out); |
277 | 303 | ||
278 | message(out,"BN_GF2m_mod_inv"); | 304 | message(out, "BN_GF2m_mod_inv"); |
279 | if (!test_gf2m_mod_inv(out,ctx)) goto err; | 305 | if (!test_gf2m_mod_inv(out, ctx)) |
306 | goto err; | ||
280 | (void)BIO_flush(out); | 307 | (void)BIO_flush(out); |
281 | 308 | ||
282 | message(out,"BN_GF2m_mod_div"); | 309 | message(out, "BN_GF2m_mod_div"); |
283 | if (!test_gf2m_mod_div(out,ctx)) goto err; | 310 | if (!test_gf2m_mod_div(out, ctx)) |
311 | goto err; | ||
284 | (void)BIO_flush(out); | 312 | (void)BIO_flush(out); |
285 | 313 | ||
286 | message(out,"BN_GF2m_mod_exp"); | 314 | message(out, "BN_GF2m_mod_exp"); |
287 | if (!test_gf2m_mod_exp(out,ctx)) goto err; | 315 | if (!test_gf2m_mod_exp(out, ctx)) |
316 | goto err; | ||
288 | (void)BIO_flush(out); | 317 | (void)BIO_flush(out); |
289 | 318 | ||
290 | message(out,"BN_GF2m_mod_sqrt"); | 319 | message(out, "BN_GF2m_mod_sqrt"); |
291 | if (!test_gf2m_mod_sqrt(out,ctx)) goto err; | 320 | if (!test_gf2m_mod_sqrt(out, ctx)) |
321 | goto err; | ||
292 | (void)BIO_flush(out); | 322 | (void)BIO_flush(out); |
293 | 323 | ||
294 | message(out,"BN_GF2m_mod_solve_quad"); | 324 | message(out, "BN_GF2m_mod_solve_quad"); |
295 | if (!test_gf2m_mod_solve_quad(out,ctx)) goto err; | 325 | if (!test_gf2m_mod_solve_quad(out, ctx)) |
326 | goto err; | ||
296 | (void)BIO_flush(out); | 327 | (void)BIO_flush(out); |
297 | #endif | 328 | #endif |
298 | BN_CTX_free(ctx); | 329 | BN_CTX_free(ctx); |
@@ -312,7 +343,7 @@ err: | |||
312 | int | 343 | int |
313 | test_add(BIO *bp) | 344 | test_add(BIO *bp) |
314 | { | 345 | { |
315 | BIGNUM a,b,c; | 346 | BIGNUM a, b, c; |
316 | int i; | 347 | int i; |
317 | int rc = 1; | 348 | int rc = 1; |
318 | 349 | ||
@@ -320,42 +351,42 @@ test_add(BIO *bp) | |||
320 | BN_init(&b); | 351 | BN_init(&b); |
321 | BN_init(&c); | 352 | BN_init(&c); |
322 | 353 | ||
323 | BN_bntest_rand(&a,512,0,0); | 354 | BN_bntest_rand(&a, 512, 0, 0); |
324 | for (i=0; i<num0; i++) { | 355 | for (i = 0; i < num0; i++) { |
325 | BN_bntest_rand(&b,450+i,0,0); | 356 | BN_bntest_rand(&b, 450 + i, 0, 0); |
326 | a.neg=rand_neg(); | 357 | a.neg = rand_neg(); |
327 | b.neg=rand_neg(); | 358 | b.neg = rand_neg(); |
328 | BN_add(&c,&a,&b); | 359 | BN_add(&c, &a, &b); |
329 | if (bp != NULL) { | 360 | if (bp != NULL) { |
330 | if (!results) { | 361 | if (!results) { |
331 | BN_print(bp,&a); | 362 | BN_print(bp, &a); |
332 | BIO_puts(bp," + "); | 363 | BIO_puts(bp, " + "); |
333 | BN_print(bp,&b); | 364 | BN_print(bp, &b); |
334 | BIO_puts(bp," - "); | 365 | BIO_puts(bp, " - "); |
335 | } | 366 | } |
336 | BN_print(bp,&c); | 367 | BN_print(bp, &c); |
337 | BIO_puts(bp,"\n"); | 368 | BIO_puts(bp, "\n"); |
338 | } | 369 | } |
339 | a.neg=!a.neg; | 370 | a.neg=!a.neg; |
340 | b.neg=!b.neg; | 371 | b.neg=!b.neg; |
341 | BN_add(&c,&c,&b); | 372 | BN_add(&c, &c, &b); |
342 | BN_add(&c,&c,&a); | 373 | BN_add(&c, &c, &a); |
343 | if(!BN_is_zero(&c)) { | 374 | if (!BN_is_zero(&c)) { |
344 | fprintf(stderr,"Add test failed!\n"); | 375 | fprintf(stderr, "Add test failed!\n"); |
345 | rc = 0; | 376 | rc = 0; |
346 | break; | 377 | break; |
347 | } | 378 | } |
348 | } | 379 | } |
349 | BN_free(&a); | 380 | BN_free(&a); |
350 | BN_free(&b); | 381 | BN_free(&b); |
351 | BN_free(&c); | 382 | BN_free(&c); |
352 | return(rc); | 383 | return (rc); |
353 | } | 384 | } |
354 | 385 | ||
355 | int | 386 | int |
356 | test_sub(BIO *bp) | 387 | test_sub(BIO *bp) |
357 | { | 388 | { |
358 | BIGNUM a,b,c; | 389 | BIGNUM a, b, c; |
359 | int i; | 390 | int i; |
360 | int rc = 1; | 391 | int rc = 1; |
361 | 392 | ||
@@ -363,49 +394,49 @@ test_sub(BIO *bp) | |||
363 | BN_init(&b); | 394 | BN_init(&b); |
364 | BN_init(&c); | 395 | BN_init(&c); |
365 | 396 | ||
366 | for (i=0; i<num0+num1; i++) { | 397 | for (i = 0; i < num0 + num1; i++) { |
367 | if (i < num1) { | 398 | if (i < num1) { |
368 | BN_bntest_rand(&a,512,0,0); | 399 | BN_bntest_rand(&a, 512, 0, 0); |
369 | BN_copy(&b,&a); | 400 | BN_copy(&b, &a); |
370 | if (BN_set_bit(&a,i)==0) { | 401 | if (BN_set_bit(&a, i) == 0) { |
371 | rc = 0; | 402 | rc = 0; |
372 | break; | 403 | break; |
373 | } | 404 | } |
374 | BN_add_word(&b,i); | 405 | BN_add_word(&b, i); |
375 | } else { | 406 | } else { |
376 | BN_bntest_rand(&b,400+i-num1,0,0); | 407 | BN_bntest_rand(&b, 400 + i - num1, 0, 0); |
377 | a.neg=rand_neg(); | 408 | a.neg = rand_neg(); |
378 | b.neg=rand_neg(); | 409 | b.neg = rand_neg(); |
379 | } | 410 | } |
380 | BN_sub(&c,&a,&b); | 411 | BN_sub(&c, &a, &b); |
381 | if (bp != NULL) { | 412 | if (bp != NULL) { |
382 | if (!results) { | 413 | if (!results) { |
383 | BN_print(bp,&a); | 414 | BN_print(bp, &a); |
384 | BIO_puts(bp," - "); | 415 | BIO_puts(bp, " - "); |
385 | BN_print(bp,&b); | 416 | BN_print(bp, &b); |
386 | BIO_puts(bp," - "); | 417 | BIO_puts(bp, " - "); |
387 | } | 418 | } |
388 | BN_print(bp,&c); | 419 | BN_print(bp, &c); |
389 | BIO_puts(bp,"\n"); | 420 | BIO_puts(bp, "\n"); |
390 | } | 421 | } |
391 | BN_add(&c,&c,&b); | 422 | BN_add(&c, &c, &b); |
392 | BN_sub(&c,&c,&a); | 423 | BN_sub(&c, &c, &a); |
393 | if(!BN_is_zero(&c)) { | 424 | if (!BN_is_zero(&c)) { |
394 | fprintf(stderr,"Subtract test failed!\n"); | 425 | fprintf(stderr, "Subtract test failed!\n"); |
395 | rc = 0; | 426 | rc = 0; |
396 | break; | 427 | break; |
397 | } | 428 | } |
398 | } | 429 | } |
399 | BN_free(&a); | 430 | BN_free(&a); |
400 | BN_free(&b); | 431 | BN_free(&b); |
401 | BN_free(&c); | 432 | BN_free(&c); |
402 | return(rc); | 433 | return (rc); |
403 | } | 434 | } |
404 | 435 | ||
405 | int | 436 | int |
406 | test_div(BIO *bp, BN_CTX *ctx) | 437 | test_div(BIO *bp, BN_CTX *ctx) |
407 | { | 438 | { |
408 | BIGNUM a,b,c,d,e; | 439 | BIGNUM a, b,c, d, e; |
409 | int i; | 440 | int i; |
410 | int rc = 1; | 441 | int rc = 1; |
411 | 442 | ||
@@ -415,43 +446,43 @@ test_div(BIO *bp, BN_CTX *ctx) | |||
415 | BN_init(&d); | 446 | BN_init(&d); |
416 | BN_init(&e); | 447 | BN_init(&e); |
417 | 448 | ||
418 | for (i=0; i<num0+num1; i++) { | 449 | for (i = 0; i < num0 + num1; i++) { |
419 | if (i < num1) { | 450 | if (i < num1) { |
420 | BN_bntest_rand(&a,400,0,0); | 451 | BN_bntest_rand(&a, 400, 0, 0); |
421 | BN_copy(&b,&a); | 452 | BN_copy(&b, &a); |
422 | BN_lshift(&a,&a,i); | 453 | BN_lshift(&a, &a, i); |
423 | BN_add_word(&a,i); | 454 | BN_add_word(&a, i); |
424 | } else | 455 | } else |
425 | BN_bntest_rand(&b,50+3*(i-num1),0,0); | 456 | BN_bntest_rand(&b, 50 + 3*(i - num1), 0, 0); |
426 | a.neg=rand_neg(); | 457 | a.neg = rand_neg(); |
427 | b.neg=rand_neg(); | 458 | b.neg = rand_neg(); |
428 | BN_div(&d,&c,&a,&b,ctx); | 459 | BN_div(&d, &c, &a, &b, ctx); |
429 | if (bp != NULL) { | 460 | if (bp != NULL) { |
430 | if (!results) { | 461 | if (!results) { |
431 | BN_print(bp,&a); | 462 | BN_print(bp, &a); |
432 | BIO_puts(bp," / "); | 463 | BIO_puts(bp, " / "); |
433 | BN_print(bp,&b); | 464 | BN_print(bp, &b); |
434 | BIO_puts(bp," - "); | 465 | BIO_puts(bp, " - "); |
435 | } | 466 | } |
436 | BN_print(bp,&d); | 467 | BN_print(bp, &d); |
437 | BIO_puts(bp,"\n"); | 468 | BIO_puts(bp, "\n"); |
438 | 469 | ||
439 | if (!results) { | 470 | if (!results) { |
440 | BN_print(bp,&a); | 471 | BN_print(bp, &a); |
441 | BIO_puts(bp," % "); | 472 | BIO_puts(bp, " % "); |
442 | BN_print(bp,&b); | 473 | BN_print(bp, &b); |
443 | BIO_puts(bp," - "); | 474 | BIO_puts(bp, " - "); |
444 | } | 475 | } |
445 | BN_print(bp,&c); | 476 | BN_print(bp, &c); |
446 | BIO_puts(bp,"\n"); | 477 | BIO_puts(bp, "\n"); |
447 | } | 478 | } |
448 | BN_mul(&e,&d,&b,ctx); | 479 | BN_mul(&e, &d, &b, ctx); |
449 | BN_add(&d,&e,&c); | 480 | BN_add(&d, &e, &c); |
450 | BN_sub(&d,&d,&a); | 481 | BN_sub(&d, &d, &a); |
451 | if(!BN_is_zero(&d)) { | 482 | if (!BN_is_zero(&d)) { |
452 | fprintf(stderr,"Division test failed!\n"); | 483 | fprintf(stderr, "Division test failed!\n"); |
453 | rc = 0; | 484 | rc = 0; |
454 | break; | 485 | break; |
455 | } | 486 | } |
456 | } | 487 | } |
457 | BN_free(&a); | 488 | BN_free(&a); |
@@ -459,41 +490,41 @@ test_div(BIO *bp, BN_CTX *ctx) | |||
459 | BN_free(&c); | 490 | BN_free(&c); |
460 | BN_free(&d); | 491 | BN_free(&d); |
461 | BN_free(&e); | 492 | BN_free(&e); |
462 | return(rc); | 493 | return (rc); |
463 | } | 494 | } |
464 | 495 | ||
465 | static void | 496 | static void |
466 | print_word(BIO *bp,BN_ULONG w) | 497 | print_word(BIO *bp, BN_ULONG w) |
467 | { | 498 | { |
468 | #ifdef SIXTY_FOUR_BIT | 499 | #ifdef SIXTY_FOUR_BIT |
469 | if (sizeof(w) > sizeof(unsigned long)) { | 500 | if (sizeof(w) > sizeof(unsigned long)) { |
470 | unsigned long h=(unsigned long)(w>>32), l=(unsigned long)(w); | 501 | unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w); |
471 | 502 | ||
472 | if (h) | 503 | if (h) |
473 | BIO_printf(bp,"%lX%08lX",h,l); | 504 | BIO_printf(bp, "%lX%08lX",h,l); |
474 | else | 505 | else |
475 | BIO_printf(bp,"%lX",l); | 506 | BIO_printf(bp, "%lX",l); |
476 | return; | 507 | return; |
477 | } | 508 | } |
478 | #endif | 509 | #endif |
479 | BIO_printf(bp,BN_HEX_FMT1,w); | 510 | BIO_printf(bp, BN_HEX_FMT1, w); |
480 | } | 511 | } |
481 | 512 | ||
482 | int | 513 | int |
483 | test_div_word(BIO *bp) | 514 | test_div_word(BIO *bp) |
484 | { | 515 | { |
485 | BIGNUM a,b; | 516 | BIGNUM a, b; |
486 | BN_ULONG r,s; | 517 | BN_ULONG r, s; |
487 | int i; | 518 | int i; |
488 | int rc = 1; | 519 | int rc = 1; |
489 | 520 | ||
490 | BN_init(&a); | 521 | BN_init(&a); |
491 | BN_init(&b); | 522 | BN_init(&b); |
492 | 523 | ||
493 | for (i=0; i<num0; i++) { | 524 | for (i = 0; i < num0; i++) { |
494 | do { | 525 | do { |
495 | BN_bntest_rand(&a,512,-1,0); | 526 | BN_bntest_rand(&a, 512, -1, 0); |
496 | BN_bntest_rand(&b,BN_BITS2,-1,0); | 527 | BN_bntest_rand(&b, BN_BITS2, -1, 0); |
497 | s = b.d[0]; | 528 | s = b.d[0]; |
498 | } while (!s); | 529 | } while (!s); |
499 | 530 | ||
@@ -502,41 +533,41 @@ test_div_word(BIO *bp) | |||
502 | 533 | ||
503 | if (bp != NULL) { | 534 | if (bp != NULL) { |
504 | if (!results) { | 535 | if (!results) { |
505 | BN_print(bp,&a); | 536 | BN_print(bp, &a); |
506 | BIO_puts(bp," / "); | 537 | BIO_puts(bp, " / "); |
507 | print_word(bp,s); | 538 | print_word(bp, s); |
508 | BIO_puts(bp," - "); | 539 | BIO_puts(bp, " - "); |
509 | } | 540 | } |
510 | BN_print(bp,&b); | 541 | BN_print(bp, &b); |
511 | BIO_puts(bp,"\n"); | 542 | BIO_puts(bp, "\n"); |
512 | 543 | ||
513 | if (!results) { | 544 | if (!results) { |
514 | BN_print(bp,&a); | 545 | BN_print(bp, &a); |
515 | BIO_puts(bp," % "); | 546 | BIO_puts(bp, " % "); |
516 | print_word(bp,s); | 547 | print_word(bp, s); |
517 | BIO_puts(bp," - "); | 548 | BIO_puts(bp, " - "); |
518 | } | 549 | } |
519 | print_word(bp,r); | 550 | print_word(bp, r); |
520 | BIO_puts(bp,"\n"); | 551 | BIO_puts(bp, "\n"); |
521 | } | 552 | } |
522 | BN_mul_word(&b,s); | 553 | BN_mul_word(&b, s); |
523 | BN_add_word(&b,r); | 554 | BN_add_word(&b, r); |
524 | BN_sub(&b,&a,&b); | 555 | BN_sub(&b, &a, &b); |
525 | if(!BN_is_zero(&b)) { | 556 | if (!BN_is_zero(&b)) { |
526 | fprintf(stderr,"Division (word) test failed!\n"); | 557 | fprintf(stderr, "Division (word) test failed!\n"); |
527 | rc = 0; | 558 | rc = 0; |
528 | break; | 559 | break; |
529 | } | 560 | } |
530 | } | 561 | } |
531 | BN_free(&a); | 562 | BN_free(&a); |
532 | BN_free(&b); | 563 | BN_free(&b); |
533 | return(rc); | 564 | return (rc); |
534 | } | 565 | } |
535 | 566 | ||
536 | int | 567 | int |
537 | test_div_recp(BIO *bp, BN_CTX *ctx) | 568 | test_div_recp(BIO *bp, BN_CTX *ctx) |
538 | { | 569 | { |
539 | BIGNUM a,b,c,d,e; | 570 | BIGNUM a, b,c, d, e; |
540 | BN_RECP_CTX recp; | 571 | BN_RECP_CTX recp; |
541 | int i; | 572 | int i; |
542 | int rc = 1; | 573 | int rc = 1; |
@@ -548,49 +579,49 @@ test_div_recp(BIO *bp, BN_CTX *ctx) | |||
548 | BN_init(&d); | 579 | BN_init(&d); |
549 | BN_init(&e); | 580 | BN_init(&e); |
550 | 581 | ||
551 | for (i=0; i<num0+num1; i++) { | 582 | for (i = 0; i < num0 + num1; i++) { |
552 | if (i < num1) { | 583 | if (i < num1) { |
553 | BN_bntest_rand(&a,400,0,0); | 584 | BN_bntest_rand(&a, 400, 0, 0); |
554 | BN_copy(&b,&a); | 585 | BN_copy(&b, &a); |
555 | BN_lshift(&a,&a,i); | 586 | BN_lshift(&a, &a, i); |
556 | BN_add_word(&a,i); | 587 | BN_add_word(&a, i); |
557 | } else | 588 | } else |
558 | BN_bntest_rand(&b,50+3*(i-num1),0,0); | 589 | BN_bntest_rand(&b, 50 + 3*(i - num1), 0, 0); |
559 | a.neg=rand_neg(); | 590 | a.neg = rand_neg(); |
560 | b.neg=rand_neg(); | 591 | b.neg = rand_neg(); |
561 | BN_RECP_CTX_set(&recp,&b,ctx); | 592 | BN_RECP_CTX_set(&recp, &b, ctx); |
562 | BN_div_recp(&d,&c,&a,&recp,ctx); | 593 | BN_div_recp(&d, &c, &a, &recp, ctx); |
563 | if (bp != NULL) { | 594 | if (bp != NULL) { |
564 | if (!results) { | 595 | if (!results) { |
565 | BN_print(bp,&a); | 596 | BN_print(bp, &a); |
566 | BIO_puts(bp," / "); | 597 | BIO_puts(bp, " / "); |
567 | BN_print(bp,&b); | 598 | BN_print(bp, &b); |
568 | BIO_puts(bp," - "); | 599 | BIO_puts(bp, " - "); |
569 | } | 600 | } |
570 | BN_print(bp,&d); | 601 | BN_print(bp, &d); |
571 | BIO_puts(bp,"\n"); | 602 | BIO_puts(bp, "\n"); |
572 | 603 | ||
573 | if (!results) { | 604 | if (!results) { |
574 | BN_print(bp,&a); | 605 | BN_print(bp, &a); |
575 | BIO_puts(bp," % "); | 606 | BIO_puts(bp, " % "); |
576 | BN_print(bp,&b); | 607 | BN_print(bp, &b); |
577 | BIO_puts(bp," - "); | 608 | BIO_puts(bp, " - "); |
578 | } | 609 | } |
579 | BN_print(bp,&c); | 610 | BN_print(bp, &c); |
580 | BIO_puts(bp,"\n"); | 611 | BIO_puts(bp, "\n"); |
581 | } | 612 | } |
582 | BN_mul(&e,&d,&b,ctx); | 613 | BN_mul(&e, &d, &b, ctx); |
583 | BN_add(&d,&e,&c); | 614 | BN_add(&d, &e, &c); |
584 | BN_sub(&d,&d,&a); | 615 | BN_sub(&d, &d, &a); |
585 | if(!BN_is_zero(&d)) { | 616 | if (!BN_is_zero(&d)) { |
586 | fprintf(stderr,"Reciprocal division test failed!\n"); | 617 | fprintf(stderr, "Reciprocal division test failed!\n"); |
587 | fprintf(stderr,"a="); | 618 | fprintf(stderr, "a="); |
588 | BN_print_fp(stderr,&a); | 619 | BN_print_fp(stderr, &a); |
589 | fprintf(stderr,"\nb="); | 620 | fprintf(stderr, "\nb="); |
590 | BN_print_fp(stderr,&b); | 621 | BN_print_fp(stderr, &b); |
591 | fprintf(stderr,"\n"); | 622 | fprintf(stderr, "\n"); |
592 | rc = 0; | 623 | rc = 0; |
593 | break; | 624 | break; |
594 | } | 625 | } |
595 | } | 626 | } |
596 | BN_free(&a); | 627 | BN_free(&a); |
@@ -599,51 +630,52 @@ test_div_recp(BIO *bp, BN_CTX *ctx) | |||
599 | BN_free(&d); | 630 | BN_free(&d); |
600 | BN_free(&e); | 631 | BN_free(&e); |
601 | BN_RECP_CTX_free(&recp); | 632 | BN_RECP_CTX_free(&recp); |
602 | return(rc); | 633 | return (rc); |
603 | } | 634 | } |
604 | 635 | ||
605 | int | 636 | int |
606 | test_mul(BIO *bp) | 637 | test_mul(BIO *bp) |
607 | { | 638 | { |
608 | BIGNUM a,b,c,d,e; | 639 | BIGNUM a, b,c, d, e; |
609 | int i; | 640 | int i; |
610 | int rc = 1; | 641 | int rc = 1; |
611 | BN_CTX *ctx; | 642 | BN_CTX *ctx; |
612 | 643 | ||
613 | ctx = BN_CTX_new(); | 644 | ctx = BN_CTX_new(); |
614 | if (ctx == NULL) exit(1); | 645 | if (ctx == NULL) |
615 | 646 | exit(1); | |
647 | |||
616 | BN_init(&a); | 648 | BN_init(&a); |
617 | BN_init(&b); | 649 | BN_init(&b); |
618 | BN_init(&c); | 650 | BN_init(&c); |
619 | BN_init(&d); | 651 | BN_init(&d); |
620 | BN_init(&e); | 652 | BN_init(&e); |
621 | 653 | ||
622 | for (i=0; i<num0+num1; i++) { | 654 | for (i = 0; i < num0 + num1; i++) { |
623 | if (i <= num1) { | 655 | if (i <= num1) { |
624 | BN_bntest_rand(&a,100,0,0); | 656 | BN_bntest_rand(&a, 100, 0, 0); |
625 | BN_bntest_rand(&b,100,0,0); | 657 | BN_bntest_rand(&b, 100, 0, 0); |
626 | } else | 658 | } else |
627 | BN_bntest_rand(&b,i-num1,0,0); | 659 | BN_bntest_rand(&b, i - num1, 0, 0); |
628 | a.neg=rand_neg(); | 660 | a.neg = rand_neg(); |
629 | b.neg=rand_neg(); | 661 | b.neg = rand_neg(); |
630 | BN_mul(&c,&a,&b,ctx); | 662 | BN_mul(&c, &a, &b, ctx); |
631 | if (bp != NULL) { | 663 | if (bp != NULL) { |
632 | if (!results) { | 664 | if (!results) { |
633 | BN_print(bp,&a); | 665 | BN_print(bp, &a); |
634 | BIO_puts(bp," * "); | 666 | BIO_puts(bp, " * "); |
635 | BN_print(bp,&b); | 667 | BN_print(bp, &b); |
636 | BIO_puts(bp," - "); | 668 | BIO_puts(bp, " - "); |
637 | } | 669 | } |
638 | BN_print(bp,&c); | 670 | BN_print(bp, &c); |
639 | BIO_puts(bp,"\n"); | 671 | BIO_puts(bp, "\n"); |
640 | } | 672 | } |
641 | BN_div(&d,&e,&c,&a,ctx); | 673 | BN_div(&d, &e, &c, &a, ctx); |
642 | BN_sub(&d,&d,&b); | 674 | BN_sub(&d, &d, &b); |
643 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) { | 675 | if (!BN_is_zero(&d) || !BN_is_zero(&e)) { |
644 | fprintf(stderr,"Multiplication test failed!\n"); | 676 | fprintf(stderr, "Multiplication test failed!\n"); |
645 | rc = 0; | 677 | rc = 0; |
646 | break; | 678 | break; |
647 | } | 679 | } |
648 | } | 680 | } |
649 | BN_free(&a); | 681 | BN_free(&a); |
@@ -652,13 +684,13 @@ test_mul(BIO *bp) | |||
652 | BN_free(&d); | 684 | BN_free(&d); |
653 | BN_free(&e); | 685 | BN_free(&e); |
654 | BN_CTX_free(ctx); | 686 | BN_CTX_free(ctx); |
655 | return(rc); | 687 | return (rc); |
656 | } | 688 | } |
657 | 689 | ||
658 | int | 690 | int |
659 | test_sqr(BIO *bp, BN_CTX *ctx) | 691 | test_sqr(BIO *bp, BN_CTX *ctx) |
660 | { | 692 | { |
661 | BIGNUM a,c,d,e; | 693 | BIGNUM a, c,d, e; |
662 | int i; | 694 | int i; |
663 | int rc = 1; | 695 | int rc = 1; |
664 | 696 | ||
@@ -667,45 +699,45 @@ test_sqr(BIO *bp, BN_CTX *ctx) | |||
667 | BN_init(&d); | 699 | BN_init(&d); |
668 | BN_init(&e); | 700 | BN_init(&e); |
669 | 701 | ||
670 | for (i=0; i<num0; i++) { | 702 | for (i = 0; i < num0; i++) { |
671 | BN_bntest_rand(&a,40+i*10,0,0); | 703 | BN_bntest_rand(&a, 40 + i*10, 0, 0); |
672 | a.neg=rand_neg(); | 704 | a.neg = rand_neg(); |
673 | BN_sqr(&c,&a,ctx); | 705 | BN_sqr(&c, &a, ctx); |
674 | if (bp != NULL) { | 706 | if (bp != NULL) { |
675 | if (!results) { | 707 | if (!results) { |
676 | BN_print(bp,&a); | 708 | BN_print(bp, &a); |
677 | BIO_puts(bp," * "); | 709 | BIO_puts(bp, " * "); |
678 | BN_print(bp,&a); | 710 | BN_print(bp, &a); |
679 | BIO_puts(bp," - "); | 711 | BIO_puts(bp, " - "); |
680 | } | 712 | } |
681 | BN_print(bp,&c); | 713 | BN_print(bp, &c); |
682 | BIO_puts(bp,"\n"); | 714 | BIO_puts(bp, "\n"); |
683 | } | 715 | } |
684 | BN_div(&d,&e,&c,&a,ctx); | 716 | BN_div(&d, &e, &c, &a, ctx); |
685 | BN_sub(&d,&d,&a); | 717 | BN_sub(&d, &d, &a); |
686 | if(!BN_is_zero(&d) || !BN_is_zero(&e)) { | 718 | if (!BN_is_zero(&d) || !BN_is_zero(&e)) { |
687 | fprintf(stderr,"Square test failed!\n"); | 719 | fprintf(stderr, "Square test failed!\n"); |
688 | rc = 0; | 720 | rc = 0; |
689 | break; | 721 | break; |
690 | } | 722 | } |
691 | } | 723 | } |
692 | BN_free(&a); | 724 | BN_free(&a); |
693 | BN_free(&c); | 725 | BN_free(&c); |
694 | BN_free(&d); | 726 | BN_free(&d); |
695 | BN_free(&e); | 727 | BN_free(&e); |
696 | return(rc); | 728 | return (rc); |
697 | } | 729 | } |
698 | 730 | ||
699 | int | 731 | int |
700 | test_mont(BIO *bp, BN_CTX *ctx) | 732 | test_mont(BIO *bp, BN_CTX *ctx) |
701 | { | 733 | { |
702 | BIGNUM a,b,c,d,A,B; | 734 | BIGNUM a, b,c, d,A, B; |
703 | BIGNUM n; | 735 | BIGNUM n; |
704 | int i; | 736 | int i; |
705 | int rc = 1; | 737 | int rc = 1; |
706 | BN_MONT_CTX *mont; | 738 | BN_MONT_CTX *mont; |
707 | 739 | ||
708 | mont=BN_MONT_CTX_new(); | 740 | mont = BN_MONT_CTX_new(); |
709 | if (mont == NULL) | 741 | if (mont == NULL) |
710 | return 0; | 742 | return 0; |
711 | 743 | ||
@@ -719,40 +751,40 @@ test_mont(BIO *bp, BN_CTX *ctx) | |||
719 | 751 | ||
720 | BN_bntest_rand(&a,100,0,0); /**/ | 752 | BN_bntest_rand(&a,100,0,0); /**/ |
721 | BN_bntest_rand(&b,100,0,0); /**/ | 753 | BN_bntest_rand(&b,100,0,0); /**/ |
722 | for (i=0; i<num2; i++) { | 754 | for (i = 0; i < num2; i++) { |
723 | int bits = (200*(i+1))/num2; | 755 | int bits = (200*(i + 1))/num2; |
724 | 756 | ||
725 | if (bits == 0) | 757 | if (bits == 0) |
726 | continue; | 758 | continue; |
727 | BN_bntest_rand(&n,bits,0,1); | 759 | BN_bntest_rand(&n, bits, 0, 1); |
728 | BN_MONT_CTX_set(mont,&n,ctx); | 760 | BN_MONT_CTX_set(mont, &n, ctx); |
729 | 761 | ||
730 | BN_nnmod(&a,&a,&n,ctx); | 762 | BN_nnmod(&a, &a, &n, ctx); |
731 | BN_nnmod(&b,&b,&n,ctx); | 763 | BN_nnmod(&b, &b, &n, ctx); |
732 | 764 | ||
733 | BN_to_montgomery(&A,&a,mont,ctx); | 765 | BN_to_montgomery(&A, &a, mont, ctx); |
734 | BN_to_montgomery(&B,&b,mont,ctx); | 766 | BN_to_montgomery(&B, &b, mont, ctx); |
735 | 767 | ||
736 | BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ | 768 | BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/ |
737 | BN_from_montgomery(&A,&c,mont,ctx);/**/ | 769 | BN_from_montgomery(&A,&c,mont,ctx);/**/ |
738 | if (bp != NULL) { | 770 | if (bp != NULL) { |
739 | if (!results) { | 771 | if (!results) { |
740 | BN_print(bp,&a); | 772 | BN_print(bp, &a); |
741 | BIO_puts(bp," * "); | 773 | BIO_puts(bp, " * "); |
742 | BN_print(bp,&b); | 774 | BN_print(bp, &b); |
743 | BIO_puts(bp," % "); | 775 | BIO_puts(bp, " % "); |
744 | BN_print(bp,&(mont->N)); | 776 | BN_print(bp, &(mont->N)); |
745 | BIO_puts(bp," - "); | 777 | BIO_puts(bp, " - "); |
746 | } | 778 | } |
747 | BN_print(bp,&A); | 779 | BN_print(bp, &A); |
748 | BIO_puts(bp,"\n"); | 780 | BIO_puts(bp, "\n"); |
749 | } | 781 | } |
750 | BN_mod_mul(&d,&a,&b,&n,ctx); | 782 | BN_mod_mul(&d, &a, &b, &n, ctx); |
751 | BN_sub(&d,&d,&A); | 783 | BN_sub(&d, &d, &A); |
752 | if(!BN_is_zero(&d)) { | 784 | if (!BN_is_zero(&d)) { |
753 | fprintf(stderr,"Montgomery multiplication test failed!\n"); | 785 | fprintf(stderr, "Montgomery multiplication test failed!\n"); |
754 | rc = 0; | 786 | rc = 0; |
755 | break; | 787 | break; |
756 | } | 788 | } |
757 | } | 789 | } |
758 | BN_MONT_CTX_free(mont); | 790 | BN_MONT_CTX_free(mont); |
@@ -763,44 +795,44 @@ test_mont(BIO *bp, BN_CTX *ctx) | |||
763 | BN_free(&A); | 795 | BN_free(&A); |
764 | BN_free(&B); | 796 | BN_free(&B); |
765 | BN_free(&n); | 797 | BN_free(&n); |
766 | return(rc); | 798 | return (rc); |
767 | } | 799 | } |
768 | 800 | ||
769 | int | 801 | int |
770 | test_mod(BIO *bp, BN_CTX *ctx) | 802 | test_mod(BIO *bp, BN_CTX *ctx) |
771 | { | 803 | { |
772 | BIGNUM *a,*b,*c,*d,*e; | 804 | BIGNUM *a, *b, *c, *d, *e; |
773 | int i; | 805 | int i; |
774 | int rc = 1; | 806 | int rc = 1; |
775 | 807 | ||
776 | a=BN_new(); | 808 | a = BN_new(); |
777 | b=BN_new(); | 809 | b = BN_new(); |
778 | c=BN_new(); | 810 | c = BN_new(); |
779 | d=BN_new(); | 811 | d = BN_new(); |
780 | e=BN_new(); | 812 | e = BN_new(); |
781 | 813 | ||
782 | BN_bntest_rand(a,1024,0,0); /**/ | 814 | BN_bntest_rand(a,1024,0,0); /**/ |
783 | for (i=0; i<num0; i++) { | 815 | for (i = 0; i < num0; i++) { |
784 | BN_bntest_rand(b,450+i*10,0,0); /**/ | 816 | BN_bntest_rand(b,450+i*10,0,0); /**/ |
785 | a->neg=rand_neg(); | 817 | a->neg = rand_neg(); |
786 | b->neg=rand_neg(); | 818 | b->neg = rand_neg(); |
787 | BN_mod(c,a,b,ctx);/**/ | 819 | BN_mod(c,a,b,ctx);/**/ |
788 | if (bp != NULL) { | 820 | if (bp != NULL) { |
789 | if (!results) { | 821 | if (!results) { |
790 | BN_print(bp,a); | 822 | BN_print(bp, a); |
791 | BIO_puts(bp," % "); | 823 | BIO_puts(bp, " % "); |
792 | BN_print(bp,b); | 824 | BN_print(bp, b); |
793 | BIO_puts(bp," - "); | 825 | BIO_puts(bp, " - "); |
794 | } | 826 | } |
795 | BN_print(bp,c); | 827 | BN_print(bp, c); |
796 | BIO_puts(bp,"\n"); | 828 | BIO_puts(bp, "\n"); |
797 | } | 829 | } |
798 | BN_div(d,e,a,b,ctx); | 830 | BN_div(d, e,a, b, ctx); |
799 | BN_sub(e,e,c); | 831 | BN_sub(e, e, c); |
800 | if(!BN_is_zero(e)) { | 832 | if (!BN_is_zero(e)) { |
801 | fprintf(stderr,"Modulo test failed!\n"); | 833 | fprintf(stderr, "Modulo test failed!\n"); |
802 | rc = 0; | 834 | rc = 0; |
803 | break; | 835 | break; |
804 | } | 836 | } |
805 | } | 837 | } |
806 | BN_free(a); | 838 | BN_free(a); |
@@ -808,66 +840,66 @@ test_mod(BIO *bp, BN_CTX *ctx) | |||
808 | BN_free(c); | 840 | BN_free(c); |
809 | BN_free(d); | 841 | BN_free(d); |
810 | BN_free(e); | 842 | BN_free(e); |
811 | return(rc); | 843 | return (rc); |
812 | } | 844 | } |
813 | 845 | ||
814 | int | 846 | int |
815 | test_mod_mul(BIO *bp, BN_CTX *ctx) | 847 | test_mod_mul(BIO *bp, BN_CTX *ctx) |
816 | { | 848 | { |
817 | BIGNUM *a,*b,*c,*d,*e; | 849 | BIGNUM *a, *b, *c, *d, *e; |
818 | int i,j; | 850 | int i, j; |
819 | int rc = 1; | 851 | int rc = 1; |
820 | 852 | ||
821 | a=BN_new(); | 853 | a = BN_new(); |
822 | b=BN_new(); | 854 | b = BN_new(); |
823 | c=BN_new(); | 855 | c = BN_new(); |
824 | d=BN_new(); | 856 | d = BN_new(); |
825 | e=BN_new(); | 857 | e = BN_new(); |
826 | 858 | ||
827 | for (j=0; j<3; j++) { | 859 | for (j = 0; j < 3; j++) { |
828 | BN_bntest_rand(c,1024,0,0); /**/ | 860 | BN_bntest_rand(c,1024,0,0); /**/ |
829 | for (i=0; i<num0; i++) { | 861 | for (i = 0; i < num0; i++) { |
830 | BN_bntest_rand(a,475+i*10,0,0); /**/ | 862 | BN_bntest_rand(a,475+i*10,0,0); /**/ |
831 | BN_bntest_rand(b,425+i*11,0,0); /**/ | 863 | BN_bntest_rand(b,425+i*11,0,0); /**/ |
832 | a->neg=rand_neg(); | 864 | a->neg = rand_neg(); |
833 | b->neg=rand_neg(); | 865 | b->neg = rand_neg(); |
834 | if (!BN_mod_mul(e,a,b,c,ctx)) { | 866 | if (!BN_mod_mul(e, a,b, c, ctx)) { |
835 | unsigned long l; | 867 | unsigned long l; |
836 | 868 | ||
837 | while ((l=ERR_get_error())) | 869 | while ((l = ERR_get_error())) |
838 | fprintf(stderr,"ERROR:%s\n", | 870 | fprintf(stderr, "ERROR:%s\n", |
839 | ERR_error_string(l,NULL)); | 871 | ERR_error_string(l, NULL)); |
840 | exit(1); | 872 | exit(1); |
841 | } | 873 | } |
842 | if (bp != NULL) { | 874 | if (bp != NULL) { |
843 | if (!results) { | 875 | if (!results) { |
844 | BN_print(bp,a); | 876 | BN_print(bp, a); |
845 | BIO_puts(bp," * "); | 877 | BIO_puts(bp, " * "); |
846 | BN_print(bp,b); | 878 | BN_print(bp, b); |
847 | BIO_puts(bp," % "); | 879 | BIO_puts(bp, " % "); |
848 | BN_print(bp,c); | 880 | BN_print(bp, c); |
849 | if ((a->neg ^ b->neg) && !BN_is_zero(e)) { | 881 | if ((a->neg ^ b->neg) && !BN_is_zero(e)) { |
850 | /* If (a*b) % c is negative, c must be added | 882 | /* If (a*b) % c is negative, c must be added |
851 | * in order to obtain the normalized remainder | 883 | * in order to obtain the normalized remainder |
852 | * (new with OpenSSL 0.9.7, previous versions of | 884 | * (new with OpenSSL 0.9.7, previous versions of |
853 | * BN_mod_mul could generate negative results) | 885 | * BN_mod_mul could generate negative results) |
854 | */ | 886 | */ |
855 | BIO_puts(bp," + "); | 887 | BIO_puts(bp, " + "); |
856 | BN_print(bp,c); | 888 | BN_print(bp, c); |
857 | } | 889 | } |
858 | BIO_puts(bp," - "); | 890 | BIO_puts(bp, " - "); |
859 | } | 891 | } |
860 | BN_print(bp,e); | 892 | BN_print(bp, e); |
861 | BIO_puts(bp,"\n"); | 893 | BIO_puts(bp, "\n"); |
862 | } | 894 | } |
863 | BN_mul(d,a,b,ctx); | 895 | BN_mul(d, a,b, ctx); |
864 | BN_sub(d,d,e); | 896 | BN_sub(d, d, e); |
865 | BN_div(a,b,d,c,ctx); | 897 | BN_div(a, b,d, c, ctx); |
866 | if(!BN_is_zero(b)) { | 898 | if (!BN_is_zero(b)) { |
867 | fprintf(stderr,"Modulo multiply test failed!\n"); | 899 | fprintf(stderr, "Modulo multiply test failed!\n"); |
868 | ERR_print_errors_fp(stderr); | 900 | ERR_print_errors_fp(stderr); |
869 | rc = 0; | 901 | rc = 0; |
870 | goto done; | 902 | goto done; |
871 | } | 903 | } |
872 | } | 904 | } |
873 | } | 905 | } |
@@ -877,51 +909,51 @@ done: | |||
877 | BN_free(c); | 909 | BN_free(c); |
878 | BN_free(d); | 910 | BN_free(d); |
879 | BN_free(e); | 911 | BN_free(e); |
880 | return(rc); | 912 | return (rc); |
881 | } | 913 | } |
882 | 914 | ||
883 | int | 915 | int |
884 | test_mod_exp(BIO *bp, BN_CTX *ctx) | 916 | test_mod_exp(BIO *bp, BN_CTX *ctx) |
885 | { | 917 | { |
886 | BIGNUM *a,*b,*c,*d,*e; | 918 | BIGNUM *a, *b, *c, *d, *e; |
887 | int i; | 919 | int i; |
888 | int rc = 1; | 920 | int rc = 1; |
889 | 921 | ||
890 | a=BN_new(); | 922 | a = BN_new(); |
891 | b=BN_new(); | 923 | b = BN_new(); |
892 | c=BN_new(); | 924 | c = BN_new(); |
893 | d=BN_new(); | 925 | d = BN_new(); |
894 | e=BN_new(); | 926 | e = BN_new(); |
895 | 927 | ||
896 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ | 928 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
897 | for (i=0; i<num2; i++) { | 929 | for (i = 0; i < num2; i++) { |
898 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 930 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
899 | BN_bntest_rand(b,2+i,0,0); /**/ | 931 | BN_bntest_rand(b,2+i,0,0); /**/ |
900 | 932 | ||
901 | if (!BN_mod_exp(d,a,b,c,ctx)) { | 933 | if (!BN_mod_exp(d, a,b, c, ctx)) { |
902 | rc = 0; | 934 | rc = 0; |
903 | break; | 935 | break; |
904 | } | 936 | } |
905 | 937 | ||
906 | if (bp != NULL) { | 938 | if (bp != NULL) { |
907 | if (!results) { | 939 | if (!results) { |
908 | BN_print(bp,a); | 940 | BN_print(bp, a); |
909 | BIO_puts(bp," ^ "); | 941 | BIO_puts(bp, " ^ "); |
910 | BN_print(bp,b); | 942 | BN_print(bp, b); |
911 | BIO_puts(bp," % "); | 943 | BIO_puts(bp, " % "); |
912 | BN_print(bp,c); | 944 | BN_print(bp, c); |
913 | BIO_puts(bp," - "); | 945 | BIO_puts(bp, " - "); |
914 | } | 946 | } |
915 | BN_print(bp,d); | 947 | BN_print(bp, d); |
916 | BIO_puts(bp,"\n"); | 948 | BIO_puts(bp, "\n"); |
917 | } | 949 | } |
918 | BN_exp(e,a,b,ctx); | 950 | BN_exp(e, a,b, ctx); |
919 | BN_sub(e,e,d); | 951 | BN_sub(e, e, d); |
920 | BN_div(a,b,e,c,ctx); | 952 | BN_div(a, b,e, c, ctx); |
921 | if(!BN_is_zero(b)) { | 953 | if (!BN_is_zero(b)) { |
922 | fprintf(stderr,"Modulo exponentiation test failed!\n"); | 954 | fprintf(stderr, "Modulo exponentiation test failed!\n"); |
923 | rc = 0; | 955 | rc = 0; |
924 | break; | 956 | break; |
925 | } | 957 | } |
926 | } | 958 | } |
927 | BN_free(a); | 959 | BN_free(a); |
@@ -929,51 +961,51 @@ test_mod_exp(BIO *bp, BN_CTX *ctx) | |||
929 | BN_free(c); | 961 | BN_free(c); |
930 | BN_free(d); | 962 | BN_free(d); |
931 | BN_free(e); | 963 | BN_free(e); |
932 | return(rc); | 964 | return (rc); |
933 | } | 965 | } |
934 | 966 | ||
935 | int | 967 | int |
936 | test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) | 968 | test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) |
937 | { | 969 | { |
938 | BIGNUM *a,*b,*c,*d,*e; | 970 | BIGNUM *a, *b, *c, *d, *e; |
939 | int i; | 971 | int i; |
940 | int rc = 1; | 972 | int rc = 1; |
941 | 973 | ||
942 | a=BN_new(); | 974 | a = BN_new(); |
943 | b=BN_new(); | 975 | b = BN_new(); |
944 | c=BN_new(); | 976 | c = BN_new(); |
945 | d=BN_new(); | 977 | d = BN_new(); |
946 | e=BN_new(); | 978 | e = BN_new(); |
947 | 979 | ||
948 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ | 980 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
949 | for (i=0; i<num2; i++) { | 981 | for (i = 0; i < num2; i++) { |
950 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 982 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
951 | BN_bntest_rand(b,2+i,0,0); /**/ | 983 | BN_bntest_rand(b,2+i,0,0); /**/ |
952 | 984 | ||
953 | if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL)) { | 985 | if (!BN_mod_exp_mont_consttime(d, a,b, c,ctx, NULL)) { |
954 | rc = 0; | 986 | rc = 0; |
955 | break; | 987 | break; |
956 | } | 988 | } |
957 | 989 | ||
958 | if (bp != NULL) { | 990 | if (bp != NULL) { |
959 | if (!results) { | 991 | if (!results) { |
960 | BN_print(bp,a); | 992 | BN_print(bp, a); |
961 | BIO_puts(bp," ^ "); | 993 | BIO_puts(bp, " ^ "); |
962 | BN_print(bp,b); | 994 | BN_print(bp, b); |
963 | BIO_puts(bp," % "); | 995 | BIO_puts(bp, " % "); |
964 | BN_print(bp,c); | 996 | BN_print(bp, c); |
965 | BIO_puts(bp," - "); | 997 | BIO_puts(bp, " - "); |
966 | } | 998 | } |
967 | BN_print(bp,d); | 999 | BN_print(bp, d); |
968 | BIO_puts(bp,"\n"); | 1000 | BIO_puts(bp, "\n"); |
969 | } | 1001 | } |
970 | BN_exp(e,a,b,ctx); | 1002 | BN_exp(e, a,b, ctx); |
971 | BN_sub(e,e,d); | 1003 | BN_sub(e, e, d); |
972 | BN_div(a,b,e,c,ctx); | 1004 | BN_div(a, b,e, c, ctx); |
973 | if(!BN_is_zero(b)) { | 1005 | if (!BN_is_zero(b)) { |
974 | fprintf(stderr,"Modulo exponentiation test failed!\n"); | 1006 | fprintf(stderr, "Modulo exponentiation test failed!\n"); |
975 | rc = 0; | 1007 | rc = 0; |
976 | break; | 1008 | break; |
977 | } | 1009 | } |
978 | } | 1010 | } |
979 | BN_free(a); | 1011 | BN_free(a); |
@@ -981,50 +1013,50 @@ test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) | |||
981 | BN_free(c); | 1013 | BN_free(c); |
982 | BN_free(d); | 1014 | BN_free(d); |
983 | BN_free(e); | 1015 | BN_free(e); |
984 | return(rc); | 1016 | return (rc); |
985 | } | 1017 | } |
986 | 1018 | ||
987 | int | 1019 | int |
988 | test_exp(BIO *bp, BN_CTX *ctx) | 1020 | test_exp(BIO *bp, BN_CTX *ctx) |
989 | { | 1021 | { |
990 | BIGNUM *a,*b,*d,*e,*one; | 1022 | BIGNUM *a, *b, *d, *e, *one; |
991 | int i; | 1023 | int i; |
992 | int rc = 1; | 1024 | int rc = 1; |
993 | 1025 | ||
994 | a=BN_new(); | 1026 | a = BN_new(); |
995 | b=BN_new(); | 1027 | b = BN_new(); |
996 | d=BN_new(); | 1028 | d = BN_new(); |
997 | e=BN_new(); | 1029 | e = BN_new(); |
998 | one=BN_new(); | 1030 | one = BN_new(); |
999 | BN_one(one); | 1031 | BN_one(one); |
1000 | 1032 | ||
1001 | for (i=0; i<num2; i++) { | 1033 | for (i = 0; i < num2; i++) { |
1002 | BN_bntest_rand(a,20+i*5,0,0); /**/ | 1034 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
1003 | BN_bntest_rand(b,2+i,0,0); /**/ | 1035 | BN_bntest_rand(b,2+i,0,0); /**/ |
1004 | 1036 | ||
1005 | if (BN_exp(d,a,b,ctx) <= 0) { | 1037 | if (BN_exp(d, a,b, ctx) <= 0) { |
1006 | rc = 0; | 1038 | rc = 0; |
1007 | break; | 1039 | break; |
1008 | } | 1040 | } |
1009 | 1041 | ||
1010 | if (bp != NULL) { | 1042 | if (bp != NULL) { |
1011 | if (!results) { | 1043 | if (!results) { |
1012 | BN_print(bp,a); | 1044 | BN_print(bp, a); |
1013 | BIO_puts(bp," ^ "); | 1045 | BIO_puts(bp, " ^ "); |
1014 | BN_print(bp,b); | 1046 | BN_print(bp, b); |
1015 | BIO_puts(bp," - "); | 1047 | BIO_puts(bp, " - "); |
1016 | } | 1048 | } |
1017 | BN_print(bp,d); | 1049 | BN_print(bp, d); |
1018 | BIO_puts(bp,"\n"); | 1050 | BIO_puts(bp, "\n"); |
1019 | } | 1051 | } |
1020 | BN_one(e); | 1052 | BN_one(e); |
1021 | for( ; !BN_is_zero(b) ; BN_sub(b,b,one)) | 1053 | for (; !BN_is_zero(b); BN_sub(b, b, one)) |
1022 | BN_mul(e,e,a,ctx); | 1054 | BN_mul(e, e,a, ctx); |
1023 | BN_sub(e,e,d); | 1055 | BN_sub(e, e, d); |
1024 | if(!BN_is_zero(e)) { | 1056 | if (!BN_is_zero(e)) { |
1025 | fprintf(stderr,"Exponentiation test failed!\n"); | 1057 | fprintf(stderr, "Exponentiation test failed!\n"); |
1026 | rc = 0; | 1058 | rc = 0; |
1027 | break; | 1059 | break; |
1028 | } | 1060 | } |
1029 | } | 1061 | } |
1030 | BN_free(a); | 1062 | BN_free(a); |
@@ -1032,47 +1064,47 @@ test_exp(BIO *bp, BN_CTX *ctx) | |||
1032 | BN_free(d); | 1064 | BN_free(d); |
1033 | BN_free(e); | 1065 | BN_free(e); |
1034 | BN_free(one); | 1066 | BN_free(one); |
1035 | return(rc); | 1067 | return (rc); |
1036 | } | 1068 | } |
1037 | 1069 | ||
1038 | #ifndef OPENSSL_NO_EC2M | 1070 | #ifndef OPENSSL_NO_EC2M |
1039 | int | 1071 | int |
1040 | test_gf2m_add(BIO *bp) | 1072 | test_gf2m_add(BIO *bp) |
1041 | { | 1073 | { |
1042 | BIGNUM a,b,c; | 1074 | BIGNUM a, b, c; |
1043 | int i, ret = 0; | 1075 | int i, ret = 0; |
1044 | 1076 | ||
1045 | BN_init(&a); | 1077 | BN_init(&a); |
1046 | BN_init(&b); | 1078 | BN_init(&b); |
1047 | BN_init(&c); | 1079 | BN_init(&c); |
1048 | 1080 | ||
1049 | for (i=0; i<num0; i++) { | 1081 | for (i = 0; i < num0; i++) { |
1050 | BN_rand(&a,512,0,0); | 1082 | BN_rand(&a, 512, 0, 0); |
1051 | BN_copy(&b, BN_value_one()); | 1083 | BN_copy(&b, BN_value_one()); |
1052 | a.neg=rand_neg(); | 1084 | a.neg = rand_neg(); |
1053 | b.neg=rand_neg(); | 1085 | b.neg = rand_neg(); |
1054 | BN_GF2m_add(&c,&a,&b); | 1086 | BN_GF2m_add(&c, &a, &b); |
1055 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1087 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1056 | if (bp != NULL) { | 1088 | if (bp != NULL) { |
1057 | if (!results) { | 1089 | if (!results) { |
1058 | BN_print(bp,&a); | 1090 | BN_print(bp, &a); |
1059 | BIO_puts(bp," ^ "); | 1091 | BIO_puts(bp, " ^ "); |
1060 | BN_print(bp,&b); | 1092 | BN_print(bp, &b); |
1061 | BIO_puts(bp," = "); | 1093 | BIO_puts(bp, " = "); |
1062 | } | 1094 | } |
1063 | BN_print(bp,&c); | 1095 | BN_print(bp, &c); |
1064 | BIO_puts(bp,"\n"); | 1096 | BIO_puts(bp, "\n"); |
1065 | } | 1097 | } |
1066 | #endif | 1098 | #endif |
1067 | /* Test that two added values have the correct parity. */ | 1099 | /* Test that two added values have the correct parity. */ |
1068 | if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) { | 1100 | if ((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) { |
1069 | fprintf(stderr,"GF(2^m) addition test (a) failed!\n"); | 1101 | fprintf(stderr, "GF(2^m) addition test (a) failed!\n"); |
1070 | goto err; | 1102 | goto err; |
1071 | } | 1103 | } |
1072 | BN_GF2m_add(&c,&c,&c); | 1104 | BN_GF2m_add(&c, &c, &c); |
1073 | /* Test that c + c = 0. */ | 1105 | /* Test that c + c = 0. */ |
1074 | if(!BN_is_zero(&c)) { | 1106 | if (!BN_is_zero(&c)) { |
1075 | fprintf(stderr,"GF(2^m) addition test (b) failed!\n"); | 1107 | fprintf(stderr, "GF(2^m) addition test (b) failed!\n"); |
1076 | goto err; | 1108 | goto err; |
1077 | } | 1109 | } |
1078 | } | 1110 | } |
@@ -1087,42 +1119,42 @@ err: | |||
1087 | int | 1119 | int |
1088 | test_gf2m_mod(BIO *bp) | 1120 | test_gf2m_mod(BIO *bp) |
1089 | { | 1121 | { |
1090 | BIGNUM *a,*b[2],*c,*d,*e; | 1122 | BIGNUM *a, *b[2], *c, *d, *e; |
1091 | int i, j, ret = 0; | 1123 | int i, j, ret = 0; |
1092 | int p0[] = {163,7,6,3,0,-1}; | 1124 | int p0[] = {163, 7,6, 3,0, -1}; |
1093 | int p1[] = {193,15,0,-1}; | 1125 | int p1[] = {193, 15, 0, -1}; |
1094 | 1126 | ||
1095 | a=BN_new(); | 1127 | a = BN_new(); |
1096 | b[0]=BN_new(); | 1128 | b[0] = BN_new(); |
1097 | b[1]=BN_new(); | 1129 | b[1] = BN_new(); |
1098 | c=BN_new(); | 1130 | c = BN_new(); |
1099 | d=BN_new(); | 1131 | d = BN_new(); |
1100 | e=BN_new(); | 1132 | e = BN_new(); |
1101 | 1133 | ||
1102 | BN_GF2m_arr2poly(p0, b[0]); | 1134 | BN_GF2m_arr2poly(p0, b[0]); |
1103 | BN_GF2m_arr2poly(p1, b[1]); | 1135 | BN_GF2m_arr2poly(p1, b[1]); |
1104 | 1136 | ||
1105 | for (i=0; i<num0; i++) { | 1137 | for (i = 0; i < num0; i++) { |
1106 | BN_bntest_rand(a, 1024, 0, 0); | 1138 | BN_bntest_rand(a, 1024, 0, 0); |
1107 | for (j=0; j < 2; j++) { | 1139 | for (j = 0; j < 2; j++) { |
1108 | BN_GF2m_mod(c, a, b[j]); | 1140 | BN_GF2m_mod(c, a, b[j]); |
1109 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1141 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1110 | if (bp != NULL) { | 1142 | if (bp != NULL) { |
1111 | if (!results) { | 1143 | if (!results) { |
1112 | BN_print(bp,a); | 1144 | BN_print(bp, a); |
1113 | BIO_puts(bp," % "); | 1145 | BIO_puts(bp, " % "); |
1114 | BN_print(bp,b[j]); | 1146 | BN_print(bp, b[j]); |
1115 | BIO_puts(bp," - "); | 1147 | BIO_puts(bp, " - "); |
1116 | BN_print(bp,c); | 1148 | BN_print(bp, c); |
1117 | BIO_puts(bp,"\n"); | 1149 | BIO_puts(bp, "\n"); |
1118 | } | 1150 | } |
1119 | } | 1151 | } |
1120 | #endif | 1152 | #endif |
1121 | BN_GF2m_add(d, a, c); | 1153 | BN_GF2m_add(d, a, c); |
1122 | BN_GF2m_mod(e, d, b[j]); | 1154 | BN_GF2m_mod(e, d, b[j]); |
1123 | /* Test that a + (a mod p) mod p == 0. */ | 1155 | /* Test that a + (a mod p) mod p == 0. */ |
1124 | if(!BN_is_zero(e)) { | 1156 | if (!BN_is_zero(e)) { |
1125 | fprintf(stderr,"GF(2^m) modulo test failed!\n"); | 1157 | fprintf(stderr, "GF(2^m) modulo test failed!\n"); |
1126 | goto err; | 1158 | goto err; |
1127 | } | 1159 | } |
1128 | } | 1160 | } |
@@ -1139,43 +1171,43 @@ err: | |||
1139 | } | 1171 | } |
1140 | 1172 | ||
1141 | int | 1173 | int |
1142 | test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | 1174 | test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx) |
1143 | { | 1175 | { |
1144 | BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h; | 1176 | BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h; |
1145 | int i, j, ret = 0; | 1177 | int i, j, ret = 0; |
1146 | int p0[] = {163,7,6,3,0,-1}; | 1178 | int p0[] = {163, 7,6, 3,0, -1}; |
1147 | int p1[] = {193,15,0,-1}; | 1179 | int p1[] = {193, 15, 0, -1}; |
1148 | 1180 | ||
1149 | a=BN_new(); | 1181 | a = BN_new(); |
1150 | b[0]=BN_new(); | 1182 | b[0] = BN_new(); |
1151 | b[1]=BN_new(); | 1183 | b[1] = BN_new(); |
1152 | c=BN_new(); | 1184 | c = BN_new(); |
1153 | d=BN_new(); | 1185 | d = BN_new(); |
1154 | e=BN_new(); | 1186 | e = BN_new(); |
1155 | f=BN_new(); | 1187 | f = BN_new(); |
1156 | g=BN_new(); | 1188 | g = BN_new(); |
1157 | h=BN_new(); | 1189 | h = BN_new(); |
1158 | 1190 | ||
1159 | BN_GF2m_arr2poly(p0, b[0]); | 1191 | BN_GF2m_arr2poly(p0, b[0]); |
1160 | BN_GF2m_arr2poly(p1, b[1]); | 1192 | BN_GF2m_arr2poly(p1, b[1]); |
1161 | 1193 | ||
1162 | for (i=0; i<num0; i++) { | 1194 | for (i = 0; i < num0; i++) { |
1163 | BN_bntest_rand(a, 1024, 0, 0); | 1195 | BN_bntest_rand(a, 1024, 0, 0); |
1164 | BN_bntest_rand(c, 1024, 0, 0); | 1196 | BN_bntest_rand(c, 1024, 0, 0); |
1165 | BN_bntest_rand(d, 1024, 0, 0); | 1197 | BN_bntest_rand(d, 1024, 0, 0); |
1166 | for (j=0; j < 2; j++) { | 1198 | for (j = 0; j < 2; j++) { |
1167 | BN_GF2m_mod_mul(e, a, c, b[j], ctx); | 1199 | BN_GF2m_mod_mul(e, a, c, b[j], ctx); |
1168 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1200 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1169 | if (bp != NULL) { | 1201 | if (bp != NULL) { |
1170 | if (!results) { | 1202 | if (!results) { |
1171 | BN_print(bp,a); | 1203 | BN_print(bp, a); |
1172 | BIO_puts(bp," * "); | 1204 | BIO_puts(bp, " * "); |
1173 | BN_print(bp,c); | 1205 | BN_print(bp, c); |
1174 | BIO_puts(bp," % "); | 1206 | BIO_puts(bp, " % "); |
1175 | BN_print(bp,b[j]); | 1207 | BN_print(bp, b[j]); |
1176 | BIO_puts(bp," - "); | 1208 | BIO_puts(bp, " - "); |
1177 | BN_print(bp,e); | 1209 | BN_print(bp, e); |
1178 | BIO_puts(bp,"\n"); | 1210 | BIO_puts(bp, "\n"); |
1179 | } | 1211 | } |
1180 | } | 1212 | } |
1181 | #endif | 1213 | #endif |
@@ -1185,8 +1217,8 @@ test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx) | |||
1185 | BN_GF2m_add(f, e, g); | 1217 | BN_GF2m_add(f, e, g); |
1186 | BN_GF2m_add(f, f, h); | 1218 | BN_GF2m_add(f, f, h); |
1187 | /* Test that (a+d)*c = a*c + d*c. */ | 1219 | /* Test that (a+d)*c = a*c + d*c. */ |
1188 | if(!BN_is_zero(f)) { | 1220 | if (!BN_is_zero(f)) { |
1189 | fprintf(stderr,"GF(2^m) modular multiplication test failed!\n"); | 1221 | fprintf(stderr, "GF(2^m) modular multiplication test failed!\n"); |
1190 | goto err; | 1222 | goto err; |
1191 | } | 1223 | } |
1192 | } | 1224 | } |
@@ -1206,46 +1238,46 @@ err: | |||
1206 | } | 1238 | } |
1207 | 1239 | ||
1208 | int | 1240 | int |
1209 | test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx) | 1241 | test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx) |
1210 | { | 1242 | { |
1211 | BIGNUM *a,*b[2],*c,*d; | 1243 | BIGNUM *a, *b[2], *c, *d; |
1212 | int i, j, ret = 0; | 1244 | int i, j, ret = 0; |
1213 | int p0[] = {163,7,6,3,0,-1}; | 1245 | int p0[] = {163, 7,6, 3,0, -1}; |
1214 | int p1[] = {193,15,0,-1}; | 1246 | int p1[] = {193, 15, 0, -1}; |
1215 | 1247 | ||
1216 | a=BN_new(); | 1248 | a = BN_new(); |
1217 | b[0]=BN_new(); | 1249 | b[0] = BN_new(); |
1218 | b[1]=BN_new(); | 1250 | b[1] = BN_new(); |
1219 | c=BN_new(); | 1251 | c = BN_new(); |
1220 | d=BN_new(); | 1252 | d = BN_new(); |
1221 | 1253 | ||
1222 | BN_GF2m_arr2poly(p0, b[0]); | 1254 | BN_GF2m_arr2poly(p0, b[0]); |
1223 | BN_GF2m_arr2poly(p1, b[1]); | 1255 | BN_GF2m_arr2poly(p1, b[1]); |
1224 | 1256 | ||
1225 | for (i=0; i<num0; i++) { | 1257 | for (i = 0; i < num0; i++) { |
1226 | BN_bntest_rand(a, 1024, 0, 0); | 1258 | BN_bntest_rand(a, 1024, 0, 0); |
1227 | for (j=0; j < 2; j++) { | 1259 | for (j = 0; j < 2; j++) { |
1228 | BN_GF2m_mod_sqr(c, a, b[j], ctx); | 1260 | BN_GF2m_mod_sqr(c, a, b[j], ctx); |
1229 | BN_copy(d, a); | 1261 | BN_copy(d, a); |
1230 | BN_GF2m_mod_mul(d, a, d, b[j], ctx); | 1262 | BN_GF2m_mod_mul(d, a, d, b[j], ctx); |
1231 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1263 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1232 | if (bp != NULL) { | 1264 | if (bp != NULL) { |
1233 | if (!results) { | 1265 | if (!results) { |
1234 | BN_print(bp,a); | 1266 | BN_print(bp, a); |
1235 | BIO_puts(bp," ^ 2 % "); | 1267 | BIO_puts(bp, " ^ 2 % "); |
1236 | BN_print(bp,b[j]); | 1268 | BN_print(bp, b[j]); |
1237 | BIO_puts(bp, " = "); | 1269 | BIO_puts(bp, " = "); |
1238 | BN_print(bp,c); | 1270 | BN_print(bp, c); |
1239 | BIO_puts(bp,"; a * a = "); | 1271 | BIO_puts(bp, "; a * a = "); |
1240 | BN_print(bp,d); | 1272 | BN_print(bp, d); |
1241 | BIO_puts(bp,"\n"); | 1273 | BIO_puts(bp, "\n"); |
1242 | } | 1274 | } |
1243 | } | 1275 | } |
1244 | #endif | 1276 | #endif |
1245 | BN_GF2m_add(d, c, d); | 1277 | BN_GF2m_add(d, c, d); |
1246 | /* Test that a*a = a^2. */ | 1278 | /* Test that a*a = a^2. */ |
1247 | if(!BN_is_zero(d)) { | 1279 | if (!BN_is_zero(d)) { |
1248 | fprintf(stderr,"GF(2^m) modular squaring test failed!\n"); | 1280 | fprintf(stderr, "GF(2^m) modular squaring test failed!\n"); |
1249 | goto err; | 1281 | goto err; |
1250 | } | 1282 | } |
1251 | } | 1283 | } |
@@ -1261,42 +1293,42 @@ err: | |||
1261 | } | 1293 | } |
1262 | 1294 | ||
1263 | int | 1295 | int |
1264 | test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx) | 1296 | test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx) |
1265 | { | 1297 | { |
1266 | BIGNUM *a,*b[2],*c,*d; | 1298 | BIGNUM *a, *b[2], *c, *d; |
1267 | int i, j, ret = 0; | 1299 | int i, j, ret = 0; |
1268 | int p0[] = {163,7,6,3,0,-1}; | 1300 | int p0[] = {163, 7,6, 3,0, -1}; |
1269 | int p1[] = {193,15,0,-1}; | 1301 | int p1[] = {193, 15, 0, -1}; |
1270 | 1302 | ||
1271 | a=BN_new(); | 1303 | a = BN_new(); |
1272 | b[0]=BN_new(); | 1304 | b[0] = BN_new(); |
1273 | b[1]=BN_new(); | 1305 | b[1] = BN_new(); |
1274 | c=BN_new(); | 1306 | c = BN_new(); |
1275 | d=BN_new(); | 1307 | d = BN_new(); |
1276 | 1308 | ||
1277 | BN_GF2m_arr2poly(p0, b[0]); | 1309 | BN_GF2m_arr2poly(p0, b[0]); |
1278 | BN_GF2m_arr2poly(p1, b[1]); | 1310 | BN_GF2m_arr2poly(p1, b[1]); |
1279 | 1311 | ||
1280 | for (i=0; i<num0; i++) { | 1312 | for (i = 0; i < num0; i++) { |
1281 | BN_bntest_rand(a, 512, 0, 0); | 1313 | BN_bntest_rand(a, 512, 0, 0); |
1282 | for (j=0; j < 2; j++) { | 1314 | for (j = 0; j < 2; j++) { |
1283 | BN_GF2m_mod_inv(c, a, b[j], ctx); | 1315 | BN_GF2m_mod_inv(c, a, b[j], ctx); |
1284 | BN_GF2m_mod_mul(d, a, c, b[j], ctx); | 1316 | BN_GF2m_mod_mul(d, a, c, b[j], ctx); |
1285 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1317 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1286 | if (bp != NULL) { | 1318 | if (bp != NULL) { |
1287 | if (!results) { | 1319 | if (!results) { |
1288 | BN_print(bp,a); | 1320 | BN_print(bp, a); |
1289 | BIO_puts(bp, " * "); | 1321 | BIO_puts(bp, " * "); |
1290 | BN_print(bp,c); | 1322 | BN_print(bp, c); |
1291 | BIO_puts(bp," - 1 % "); | 1323 | BIO_puts(bp, " - 1 % "); |
1292 | BN_print(bp,b[j]); | 1324 | BN_print(bp, b[j]); |
1293 | BIO_puts(bp,"\n"); | 1325 | BIO_puts(bp, "\n"); |
1294 | } | 1326 | } |
1295 | } | 1327 | } |
1296 | #endif | 1328 | #endif |
1297 | /* Test that ((1/a)*a) = 1. */ | 1329 | /* Test that ((1/a)*a) = 1. */ |
1298 | if(!BN_is_one(d)) { | 1330 | if (!BN_is_one(d)) { |
1299 | fprintf(stderr,"GF(2^m) modular inversion test failed!\n"); | 1331 | fprintf(stderr, "GF(2^m) modular inversion test failed!\n"); |
1300 | goto err; | 1332 | goto err; |
1301 | } | 1333 | } |
1302 | } | 1334 | } |
@@ -1312,48 +1344,48 @@ err: | |||
1312 | } | 1344 | } |
1313 | 1345 | ||
1314 | int | 1346 | int |
1315 | test_gf2m_mod_div(BIO *bp,BN_CTX *ctx) | 1347 | test_gf2m_mod_div(BIO *bp, BN_CTX *ctx) |
1316 | { | 1348 | { |
1317 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1349 | BIGNUM *a, *b[2], *c, *d, *e, *f; |
1318 | int i, j, ret = 0; | 1350 | int i, j, ret = 0; |
1319 | int p0[] = {163,7,6,3,0,-1}; | 1351 | int p0[] = {163, 7,6, 3,0, -1}; |
1320 | int p1[] = {193,15,0,-1}; | 1352 | int p1[] = {193, 15, 0, -1}; |
1321 | 1353 | ||
1322 | a=BN_new(); | 1354 | a = BN_new(); |
1323 | b[0]=BN_new(); | 1355 | b[0] = BN_new(); |
1324 | b[1]=BN_new(); | 1356 | b[1] = BN_new(); |
1325 | c=BN_new(); | 1357 | c = BN_new(); |
1326 | d=BN_new(); | 1358 | d = BN_new(); |
1327 | e=BN_new(); | 1359 | e = BN_new(); |
1328 | f=BN_new(); | 1360 | f = BN_new(); |
1329 | 1361 | ||
1330 | BN_GF2m_arr2poly(p0, b[0]); | 1362 | BN_GF2m_arr2poly(p0, b[0]); |
1331 | BN_GF2m_arr2poly(p1, b[1]); | 1363 | BN_GF2m_arr2poly(p1, b[1]); |
1332 | 1364 | ||
1333 | for (i=0; i<num0; i++) { | 1365 | for (i = 0; i < num0; i++) { |
1334 | BN_bntest_rand(a, 512, 0, 0); | 1366 | BN_bntest_rand(a, 512, 0, 0); |
1335 | BN_bntest_rand(c, 512, 0, 0); | 1367 | BN_bntest_rand(c, 512, 0, 0); |
1336 | for (j=0; j < 2; j++) { | 1368 | for (j = 0; j < 2; j++) { |
1337 | BN_GF2m_mod_div(d, a, c, b[j], ctx); | 1369 | BN_GF2m_mod_div(d, a, c, b[j], ctx); |
1338 | BN_GF2m_mod_mul(e, d, c, b[j], ctx); | 1370 | BN_GF2m_mod_mul(e, d, c, b[j], ctx); |
1339 | BN_GF2m_mod_div(f, a, e, b[j], ctx); | 1371 | BN_GF2m_mod_div(f, a, e, b[j], ctx); |
1340 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1372 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1341 | if (bp != NULL) { | 1373 | if (bp != NULL) { |
1342 | if (!results) { | 1374 | if (!results) { |
1343 | BN_print(bp,a); | 1375 | BN_print(bp, a); |
1344 | BIO_puts(bp, " = "); | 1376 | BIO_puts(bp, " = "); |
1345 | BN_print(bp,c); | 1377 | BN_print(bp, c); |
1346 | BIO_puts(bp," * "); | 1378 | BIO_puts(bp, " * "); |
1347 | BN_print(bp,d); | 1379 | BN_print(bp, d); |
1348 | BIO_puts(bp, " % "); | 1380 | BIO_puts(bp, " % "); |
1349 | BN_print(bp,b[j]); | 1381 | BN_print(bp, b[j]); |
1350 | BIO_puts(bp,"\n"); | 1382 | BIO_puts(bp, "\n"); |
1351 | } | 1383 | } |
1352 | } | 1384 | } |
1353 | #endif | 1385 | #endif |
1354 | /* Test that ((a/c)*c)/a = 1. */ | 1386 | /* Test that ((a/c)*c)/a = 1. */ |
1355 | if(!BN_is_one(f)) { | 1387 | if (!BN_is_one(f)) { |
1356 | fprintf(stderr,"GF(2^m) modular division test failed!\n"); | 1388 | fprintf(stderr, "GF(2^m) modular division test failed!\n"); |
1357 | goto err; | 1389 | goto err; |
1358 | } | 1390 | } |
1359 | } | 1391 | } |
@@ -1371,29 +1403,29 @@ err: | |||
1371 | } | 1403 | } |
1372 | 1404 | ||
1373 | int | 1405 | int |
1374 | test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | 1406 | test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx) |
1375 | { | 1407 | { |
1376 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1408 | BIGNUM *a, *b[2], *c, *d, *e, *f; |
1377 | int i, j, ret = 0; | 1409 | int i, j, ret = 0; |
1378 | int p0[] = {163,7,6,3,0,-1}; | 1410 | int p0[] = {163, 7,6, 3,0, -1}; |
1379 | int p1[] = {193,15,0,-1}; | 1411 | int p1[] = {193, 15, 0, -1}; |
1380 | 1412 | ||
1381 | a=BN_new(); | 1413 | a = BN_new(); |
1382 | b[0]=BN_new(); | 1414 | b[0] = BN_new(); |
1383 | b[1]=BN_new(); | 1415 | b[1] = BN_new(); |
1384 | c=BN_new(); | 1416 | c = BN_new(); |
1385 | d=BN_new(); | 1417 | d = BN_new(); |
1386 | e=BN_new(); | 1418 | e = BN_new(); |
1387 | f=BN_new(); | 1419 | f = BN_new(); |
1388 | 1420 | ||
1389 | BN_GF2m_arr2poly(p0, b[0]); | 1421 | BN_GF2m_arr2poly(p0, b[0]); |
1390 | BN_GF2m_arr2poly(p1, b[1]); | 1422 | BN_GF2m_arr2poly(p1, b[1]); |
1391 | 1423 | ||
1392 | for (i=0; i<num0; i++) { | 1424 | for (i = 0; i < num0; i++) { |
1393 | BN_bntest_rand(a, 512, 0, 0); | 1425 | BN_bntest_rand(a, 512, 0, 0); |
1394 | BN_bntest_rand(c, 512, 0, 0); | 1426 | BN_bntest_rand(c, 512, 0, 0); |
1395 | BN_bntest_rand(d, 512, 0, 0); | 1427 | BN_bntest_rand(d, 512, 0, 0); |
1396 | for (j=0; j < 2; j++) { | 1428 | for (j = 0; j < 2; j++) { |
1397 | BN_GF2m_mod_exp(e, a, c, b[j], ctx); | 1429 | BN_GF2m_mod_exp(e, a, c, b[j], ctx); |
1398 | BN_GF2m_mod_exp(f, a, d, b[j], ctx); | 1430 | BN_GF2m_mod_exp(f, a, d, b[j], ctx); |
1399 | BN_GF2m_mod_mul(e, e, f, b[j], ctx); | 1431 | BN_GF2m_mod_mul(e, e, f, b[j], ctx); |
@@ -1402,25 +1434,25 @@ test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx) | |||
1402 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1434 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1403 | if (bp != NULL) { | 1435 | if (bp != NULL) { |
1404 | if (!results) { | 1436 | if (!results) { |
1405 | BN_print(bp,a); | 1437 | BN_print(bp, a); |
1406 | BIO_puts(bp, " ^ ("); | 1438 | BIO_puts(bp, " ^ ("); |
1407 | BN_print(bp,c); | 1439 | BN_print(bp, c); |
1408 | BIO_puts(bp," + "); | 1440 | BIO_puts(bp, " + "); |
1409 | BN_print(bp,d); | 1441 | BN_print(bp, d); |
1410 | BIO_puts(bp, ") = "); | 1442 | BIO_puts(bp, ") = "); |
1411 | BN_print(bp,e); | 1443 | BN_print(bp, e); |
1412 | BIO_puts(bp, "; - "); | 1444 | BIO_puts(bp, "; - "); |
1413 | BN_print(bp,f); | 1445 | BN_print(bp, f); |
1414 | BIO_puts(bp, " % "); | 1446 | BIO_puts(bp, " % "); |
1415 | BN_print(bp,b[j]); | 1447 | BN_print(bp, b[j]); |
1416 | BIO_puts(bp,"\n"); | 1448 | BIO_puts(bp, "\n"); |
1417 | } | 1449 | } |
1418 | } | 1450 | } |
1419 | #endif | 1451 | #endif |
1420 | BN_GF2m_add(f, e, f); | 1452 | BN_GF2m_add(f, e, f); |
1421 | /* Test that a^(c+d)=a^c*a^d. */ | 1453 | /* Test that a^(c+d)=a^c*a^d. */ |
1422 | if(!BN_is_zero(f)) { | 1454 | if (!BN_is_zero(f)) { |
1423 | fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n"); | 1455 | fprintf(stderr, "GF(2^m) modular exponentiation test failed!\n"); |
1424 | goto err; | 1456 | goto err; |
1425 | } | 1457 | } |
1426 | } | 1458 | } |
@@ -1438,44 +1470,44 @@ err: | |||
1438 | } | 1470 | } |
1439 | 1471 | ||
1440 | int | 1472 | int |
1441 | test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx) | 1473 | test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx) |
1442 | { | 1474 | { |
1443 | BIGNUM *a,*b[2],*c,*d,*e,*f; | 1475 | BIGNUM *a, *b[2], *c, *d, *e, *f; |
1444 | int i, j, ret = 0; | 1476 | int i, j, ret = 0; |
1445 | int p0[] = {163,7,6,3,0,-1}; | 1477 | int p0[] = {163, 7,6, 3,0, -1}; |
1446 | int p1[] = {193,15,0,-1}; | 1478 | int p1[] = {193, 15, 0, -1}; |
1447 | 1479 | ||
1448 | a=BN_new(); | 1480 | a = BN_new(); |
1449 | b[0]=BN_new(); | 1481 | b[0] = BN_new(); |
1450 | b[1]=BN_new(); | 1482 | b[1] = BN_new(); |
1451 | c=BN_new(); | 1483 | c = BN_new(); |
1452 | d=BN_new(); | 1484 | d = BN_new(); |
1453 | e=BN_new(); | 1485 | e = BN_new(); |
1454 | f=BN_new(); | 1486 | f = BN_new(); |
1455 | 1487 | ||
1456 | BN_GF2m_arr2poly(p0, b[0]); | 1488 | BN_GF2m_arr2poly(p0, b[0]); |
1457 | BN_GF2m_arr2poly(p1, b[1]); | 1489 | BN_GF2m_arr2poly(p1, b[1]); |
1458 | 1490 | ||
1459 | for (i=0; i<num0; i++) { | 1491 | for (i = 0; i < num0; i++) { |
1460 | BN_bntest_rand(a, 512, 0, 0); | 1492 | BN_bntest_rand(a, 512, 0, 0); |
1461 | for (j=0; j < 2; j++) { | 1493 | for (j = 0; j < 2; j++) { |
1462 | BN_GF2m_mod(c, a, b[j]); | 1494 | BN_GF2m_mod(c, a, b[j]); |
1463 | BN_GF2m_mod_sqrt(d, a, b[j], ctx); | 1495 | BN_GF2m_mod_sqrt(d, a, b[j], ctx); |
1464 | BN_GF2m_mod_sqr(e, d, b[j], ctx); | 1496 | BN_GF2m_mod_sqr(e, d, b[j], ctx); |
1465 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1497 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1466 | if (bp != NULL) { | 1498 | if (bp != NULL) { |
1467 | if (!results) { | 1499 | if (!results) { |
1468 | BN_print(bp,d); | 1500 | BN_print(bp, d); |
1469 | BIO_puts(bp, " ^ 2 - "); | 1501 | BIO_puts(bp, " ^ 2 - "); |
1470 | BN_print(bp,a); | 1502 | BN_print(bp, a); |
1471 | BIO_puts(bp,"\n"); | 1503 | BIO_puts(bp, "\n"); |
1472 | } | 1504 | } |
1473 | } | 1505 | } |
1474 | #endif | 1506 | #endif |
1475 | BN_GF2m_add(f, c, e); | 1507 | BN_GF2m_add(f, c, e); |
1476 | /* Test that d^2 = a, where d = sqrt(a). */ | 1508 | /* Test that d^2 = a, where d = sqrt(a). */ |
1477 | if(!BN_is_zero(f)) { | 1509 | if (!BN_is_zero(f)) { |
1478 | fprintf(stderr,"GF(2^m) modular square root test failed!\n"); | 1510 | fprintf(stderr, "GF(2^m) modular square root test failed!\n"); |
1479 | goto err; | 1511 | goto err; |
1480 | } | 1512 | } |
1481 | } | 1513 | } |
@@ -1493,26 +1525,26 @@ err: | |||
1493 | } | 1525 | } |
1494 | 1526 | ||
1495 | int | 1527 | int |
1496 | test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | 1528 | test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx) |
1497 | { | 1529 | { |
1498 | BIGNUM *a,*b[2],*c,*d,*e; | 1530 | BIGNUM *a, *b[2], *c, *d, *e; |
1499 | int i, j, s = 0, t, ret = 0; | 1531 | int i, j, s = 0, t, ret = 0; |
1500 | int p0[] = {163,7,6,3,0,-1}; | 1532 | int p0[] = {163, 7,6, 3,0, -1}; |
1501 | int p1[] = {193,15,0,-1}; | 1533 | int p1[] = {193, 15, 0, -1}; |
1502 | 1534 | ||
1503 | a=BN_new(); | 1535 | a = BN_new(); |
1504 | b[0]=BN_new(); | 1536 | b[0] = BN_new(); |
1505 | b[1]=BN_new(); | 1537 | b[1] = BN_new(); |
1506 | c=BN_new(); | 1538 | c = BN_new(); |
1507 | d=BN_new(); | 1539 | d = BN_new(); |
1508 | e=BN_new(); | 1540 | e = BN_new(); |
1509 | 1541 | ||
1510 | BN_GF2m_arr2poly(p0, b[0]); | 1542 | BN_GF2m_arr2poly(p0, b[0]); |
1511 | BN_GF2m_arr2poly(p1, b[1]); | 1543 | BN_GF2m_arr2poly(p1, b[1]); |
1512 | 1544 | ||
1513 | for (i=0; i<num0; i++) { | 1545 | for (i = 0; i < num0; i++) { |
1514 | BN_bntest_rand(a, 512, 0, 0); | 1546 | BN_bntest_rand(a, 512, 0, 0); |
1515 | for (j=0; j < 2; j++) { | 1547 | for (j = 0; j < 2; j++) { |
1516 | t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx); | 1548 | t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx); |
1517 | if (t) { | 1549 | if (t) { |
1518 | s++; | 1550 | s++; |
@@ -1522,19 +1554,19 @@ test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | |||
1522 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ | 1554 | #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */ |
1523 | if (bp != NULL) { | 1555 | if (bp != NULL) { |
1524 | if (!results) { | 1556 | if (!results) { |
1525 | BN_print(bp,c); | 1557 | BN_print(bp, c); |
1526 | BIO_puts(bp, " is root of z^2 + z = "); | 1558 | BIO_puts(bp, " is root of z^2 + z = "); |
1527 | BN_print(bp,a); | 1559 | BN_print(bp, a); |
1528 | BIO_puts(bp, " % "); | 1560 | BIO_puts(bp, " % "); |
1529 | BN_print(bp,b[j]); | 1561 | BN_print(bp, b[j]); |
1530 | BIO_puts(bp, "\n"); | 1562 | BIO_puts(bp, "\n"); |
1531 | } | 1563 | } |
1532 | } | 1564 | } |
1533 | #endif | 1565 | #endif |
1534 | BN_GF2m_add(e, e, d); | 1566 | BN_GF2m_add(e, e, d); |
1535 | /* Test that solution of quadratic c satisfies c^2 + c = a. */ | 1567 | /* Test that solution of quadratic c satisfies c^2 + c = a. */ |
1536 | if(!BN_is_zero(e)) { | 1568 | if (!BN_is_zero(e)) { |
1537 | fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n"); | 1569 | fprintf(stderr, "GF(2^m) modular solve quadratic test failed!\n"); |
1538 | goto err; | 1570 | goto err; |
1539 | } | 1571 | } |
1540 | } else { | 1572 | } else { |
@@ -1542,9 +1574,9 @@ test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | |||
1542 | if (bp != NULL) { | 1574 | if (bp != NULL) { |
1543 | if (!results) { | 1575 | if (!results) { |
1544 | BIO_puts(bp, "There are no roots of z^2 + z = "); | 1576 | BIO_puts(bp, "There are no roots of z^2 + z = "); |
1545 | BN_print(bp,a); | 1577 | BN_print(bp, a); |
1546 | BIO_puts(bp, " % "); | 1578 | BIO_puts(bp, " % "); |
1547 | BN_print(bp,b[j]); | 1579 | BN_print(bp, b[j]); |
1548 | BIO_puts(bp, "\n"); | 1580 | BIO_puts(bp, "\n"); |
1549 | } | 1581 | } |
1550 | } | 1582 | } |
@@ -1552,9 +1584,9 @@ test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx) | |||
1552 | } | 1584 | } |
1553 | } | 1585 | } |
1554 | } | 1586 | } |
1555 | if (s == 0) { | 1587 | if (s == 0) { |
1556 | fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0); | 1588 | fprintf(stderr, "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0); |
1557 | fprintf(stderr,"this is very unlikely and probably indicates an error.\n"); | 1589 | fprintf(stderr, "this is very unlikely and probably indicates an error.\n"); |
1558 | goto err; | 1590 | goto err; |
1559 | } | 1591 | } |
1560 | ret = 1; | 1592 | ret = 1; |
@@ -1574,10 +1606,14 @@ genprime_cb(int p, int n, BN_GENCB *arg) | |||
1574 | { | 1606 | { |
1575 | char c='*'; | 1607 | char c='*'; |
1576 | 1608 | ||
1577 | if (p == 0) c='.'; | 1609 | if (p == 0) |
1578 | if (p == 1) c='+'; | 1610 | c='.'; |
1579 | if (p == 2) c='*'; | 1611 | if (p == 1) |
1580 | if (p == 3) c='\n'; | 1612 | c='+'; |
1613 | if (p == 2) | ||
1614 | c='*'; | ||
1615 | if (p == 3) | ||
1616 | c='\n'; | ||
1581 | putc(c, stderr); | 1617 | putc(c, stderr); |
1582 | fflush(stderr); | 1618 | fflush(stderr); |
1583 | return 1; | 1619 | return 1; |
@@ -1587,7 +1623,7 @@ int | |||
1587 | test_kron(BIO *bp, BN_CTX *ctx) | 1623 | test_kron(BIO *bp, BN_CTX *ctx) |
1588 | { | 1624 | { |
1589 | BN_GENCB cb; | 1625 | BN_GENCB cb; |
1590 | BIGNUM *a,*b,*r,*t; | 1626 | BIGNUM *a, *b, *r, *t; |
1591 | int i; | 1627 | int i; |
1592 | int legendre, kronecker; | 1628 | int legendre, kronecker; |
1593 | int ret = 0; | 1629 | int ret = 0; |
@@ -1596,10 +1632,11 @@ test_kron(BIO *bp, BN_CTX *ctx) | |||
1596 | b = BN_new(); | 1632 | b = BN_new(); |
1597 | r = BN_new(); | 1633 | r = BN_new(); |
1598 | t = BN_new(); | 1634 | t = BN_new(); |
1599 | if (a == NULL || b == NULL || r == NULL || t == NULL) goto err; | 1635 | if (a == NULL || b == NULL || r == NULL || t == NULL) |
1636 | goto err; | ||
1600 | 1637 | ||
1601 | BN_GENCB_set(&cb, genprime_cb, NULL); | 1638 | BN_GENCB_set(&cb, genprime_cb, NULL); |
1602 | 1639 | ||
1603 | /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). | 1640 | /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). |
1604 | * In this case we know that if b is prime, then BN_kronecker(a, b, ctx) | 1641 | * In this case we know that if b is prime, then BN_kronecker(a, b, ctx) |
1605 | * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). | 1642 | * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). |
@@ -1609,44 +1646,52 @@ test_kron(BIO *bp, BN_CTX *ctx) | |||
1609 | * don't want to test whether b is prime but whether BN_kronecker | 1646 | * don't want to test whether b is prime but whether BN_kronecker |
1610 | * works.) */ | 1647 | * works.) */ |
1611 | 1648 | ||
1612 | if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err; | 1649 | if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) |
1650 | goto err; | ||
1613 | b->neg = rand_neg(); | 1651 | b->neg = rand_neg(); |
1614 | putc('\n', stderr); | 1652 | putc('\n', stderr); |
1615 | 1653 | ||
1616 | for (i = 0; i < num0; i++) { | 1654 | for (i = 0; i < num0; i++) { |
1617 | if (!BN_bntest_rand(a, 512, 0, 0)) goto err; | 1655 | if (!BN_bntest_rand(a, 512, 0, 0)) |
1656 | goto err; | ||
1618 | a->neg = rand_neg(); | 1657 | a->neg = rand_neg(); |
1619 | 1658 | ||
1620 | /* t := (|b|-1)/2 (note that b is odd) */ | 1659 | /* t := (|b|-1)/2 (note that b is odd) */ |
1621 | if (!BN_copy(t, b)) goto err; | 1660 | if (!BN_copy(t, b)) |
1661 | goto err; | ||
1622 | t->neg = 0; | 1662 | t->neg = 0; |
1623 | if (!BN_sub_word(t, 1)) goto err; | 1663 | if (!BN_sub_word(t, 1)) |
1624 | if (!BN_rshift1(t, t)) goto err; | 1664 | goto err; |
1665 | if (!BN_rshift1(t, t)) | ||
1666 | goto err; | ||
1625 | /* r := a^t mod b */ | 1667 | /* r := a^t mod b */ |
1626 | b->neg=0; | 1668 | b->neg = 0; |
1627 | 1669 | ||
1628 | if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err; | 1670 | if (!BN_mod_exp_recp(r, a, t, b, ctx)) |
1629 | b->neg=1; | 1671 | goto err; |
1672 | b->neg = 1; | ||
1630 | 1673 | ||
1631 | if (BN_is_word(r, 1)) | 1674 | if (BN_is_word(r, 1)) |
1632 | legendre = 1; | 1675 | legendre = 1; |
1633 | else if (BN_is_zero(r)) | 1676 | else if (BN_is_zero(r)) |
1634 | legendre = 0; | 1677 | legendre = 0; |
1635 | else { | 1678 | else { |
1636 | if (!BN_add_word(r, 1)) goto err; | 1679 | if (!BN_add_word(r, 1)) |
1680 | goto err; | ||
1637 | if (0 != BN_ucmp(r, b)) { | 1681 | if (0 != BN_ucmp(r, b)) { |
1638 | fprintf(stderr, "Legendre symbol computation failed\n"); | 1682 | fprintf(stderr, "Legendre symbol computation failed\n"); |
1639 | goto err; | 1683 | goto err; |
1640 | } | 1684 | } |
1641 | legendre = -1; | 1685 | legendre = -1; |
1642 | } | 1686 | } |
1643 | 1687 | ||
1644 | kronecker = BN_kronecker(a, b, ctx); | 1688 | kronecker = BN_kronecker(a, b, ctx); |
1645 | if (kronecker < -1) goto err; | 1689 | if (kronecker < -1) |
1690 | goto err; | ||
1646 | /* we actually need BN_kronecker(a, |b|) */ | 1691 | /* we actually need BN_kronecker(a, |b|) */ |
1647 | if (a->neg && b->neg) | 1692 | if (a->neg && b->neg) |
1648 | kronecker = -kronecker; | 1693 | kronecker = -kronecker; |
1649 | 1694 | ||
1650 | if (legendre != kronecker) { | 1695 | if (legendre != kronecker) { |
1651 | fprintf(stderr, "legendre != kronecker; a = "); | 1696 | fprintf(stderr, "legendre != kronecker; a = "); |
1652 | BN_print_fp(stderr, a); | 1697 | BN_print_fp(stderr, a); |
@@ -1664,10 +1709,14 @@ test_kron(BIO *bp, BN_CTX *ctx) | |||
1664 | fflush(stderr); | 1709 | fflush(stderr); |
1665 | ret = 1; | 1710 | ret = 1; |
1666 | err: | 1711 | err: |
1667 | if (a != NULL) BN_free(a); | 1712 | if (a != NULL) |
1668 | if (b != NULL) BN_free(b); | 1713 | BN_free(a); |
1669 | if (r != NULL) BN_free(r); | 1714 | if (b != NULL) |
1670 | if (t != NULL) BN_free(t); | 1715 | BN_free(b); |
1716 | if (r != NULL) | ||
1717 | BN_free(r); | ||
1718 | if (t != NULL) | ||
1719 | BN_free(t); | ||
1671 | return ret; | 1720 | return ret; |
1672 | } | 1721 | } |
1673 | 1722 | ||
@@ -1675,27 +1724,32 @@ int | |||
1675 | test_sqrt(BIO *bp, BN_CTX *ctx) | 1724 | test_sqrt(BIO *bp, BN_CTX *ctx) |
1676 | { | 1725 | { |
1677 | BN_GENCB cb; | 1726 | BN_GENCB cb; |
1678 | BIGNUM *a,*p,*r; | 1727 | BIGNUM *a, *p, *r; |
1679 | int i, j; | 1728 | int i, j; |
1680 | int ret = 0; | 1729 | int ret = 0; |
1681 | 1730 | ||
1682 | a = BN_new(); | 1731 | a = BN_new(); |
1683 | p = BN_new(); | 1732 | p = BN_new(); |
1684 | r = BN_new(); | 1733 | r = BN_new(); |
1685 | if (a == NULL || p == NULL || r == NULL) goto err; | 1734 | if (a == NULL || p == NULL || r == NULL) |
1735 | goto err; | ||
1686 | 1736 | ||
1687 | BN_GENCB_set(&cb, genprime_cb, NULL); | 1737 | BN_GENCB_set(&cb, genprime_cb, NULL); |
1688 | 1738 | ||
1689 | for (i = 0; i < 16; i++) { | 1739 | for (i = 0; i < 16; i++) { |
1690 | if (i < 8) { | 1740 | if (i < 8) { |
1691 | unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 }; | 1741 | unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 }; |
1692 | 1742 | ||
1693 | if (!BN_set_word(p, primes[i])) goto err; | 1743 | if (!BN_set_word(p, primes[i])) |
1744 | goto err; | ||
1694 | } else { | 1745 | } else { |
1695 | if (!BN_set_word(a, 32)) goto err; | 1746 | if (!BN_set_word(a, 32)) |
1696 | if (!BN_set_word(r, 2*i + 1)) goto err; | 1747 | goto err; |
1697 | 1748 | if (!BN_set_word(r, 2*i + 1)) | |
1698 | if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err; | 1749 | goto err; |
1750 | |||
1751 | if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) | ||
1752 | goto err; | ||
1699 | putc('\n', stderr); | 1753 | putc('\n', stderr); |
1700 | } | 1754 | } |
1701 | p->neg = rand_neg(); | 1755 | p->neg = rand_neg(); |
@@ -1703,20 +1757,31 @@ test_sqrt(BIO *bp, BN_CTX *ctx) | |||
1703 | for (j = 0; j < num2; j++) { | 1757 | for (j = 0; j < num2; j++) { |
1704 | /* construct 'a' such that it is a square modulo p, | 1758 | /* construct 'a' such that it is a square modulo p, |
1705 | * but in general not a proper square and not reduced modulo p */ | 1759 | * but in general not a proper square and not reduced modulo p */ |
1706 | if (!BN_bntest_rand(r, 256, 0, 3)) goto err; | 1760 | if (!BN_bntest_rand(r, 256, 0, 3)) |
1707 | if (!BN_nnmod(r, r, p, ctx)) goto err; | 1761 | goto err; |
1708 | if (!BN_mod_sqr(r, r, p, ctx)) goto err; | 1762 | if (!BN_nnmod(r, r, p, ctx)) |
1709 | if (!BN_bntest_rand(a, 256, 0, 3)) goto err; | 1763 | goto err; |
1710 | if (!BN_nnmod(a, a, p, ctx)) goto err; | 1764 | if (!BN_mod_sqr(r, r, p, ctx)) |
1711 | if (!BN_mod_sqr(a, a, p, ctx)) goto err; | 1765 | goto err; |
1712 | if (!BN_mul(a, a, r, ctx)) goto err; | 1766 | if (!BN_bntest_rand(a, 256, 0, 3)) |
1767 | goto err; | ||
1768 | if (!BN_nnmod(a, a, p, ctx)) | ||
1769 | goto err; | ||
1770 | if (!BN_mod_sqr(a, a, p, ctx)) | ||
1771 | goto err; | ||
1772 | if (!BN_mul(a, a, r, ctx)) | ||
1773 | goto err; | ||
1713 | if (rand_neg()) | 1774 | if (rand_neg()) |
1714 | if (!BN_sub(a, a, p)) goto err; | 1775 | if (!BN_sub(a, a, p)) |
1776 | goto err; | ||
1715 | 1777 | ||
1716 | if (!BN_mod_sqrt(r, a, p, ctx)) goto err; | 1778 | if (!BN_mod_sqrt(r, a, p, ctx)) |
1717 | if (!BN_mod_sqr(r, r, p, ctx)) goto err; | 1779 | goto err; |
1780 | if (!BN_mod_sqr(r, r, p, ctx)) | ||
1781 | goto err; | ||
1718 | 1782 | ||
1719 | if (!BN_nnmod(a, a, p, ctx)) goto err; | 1783 | if (!BN_nnmod(a, a, p, ctx)) |
1784 | goto err; | ||
1720 | 1785 | ||
1721 | if (BN_cmp(a, r) != 0) { | 1786 | if (BN_cmp(a, r) != 0) { |
1722 | fprintf(stderr, "BN_mod_sqrt failed: a = "); | 1787 | fprintf(stderr, "BN_mod_sqrt failed: a = "); |
@@ -1732,149 +1797,152 @@ test_sqrt(BIO *bp, BN_CTX *ctx) | |||
1732 | putc('.', stderr); | 1797 | putc('.', stderr); |
1733 | fflush(stderr); | 1798 | fflush(stderr); |
1734 | } | 1799 | } |
1735 | 1800 | ||
1736 | putc('\n', stderr); | 1801 | putc('\n', stderr); |
1737 | fflush(stderr); | 1802 | fflush(stderr); |
1738 | } | 1803 | } |
1739 | ret = 1; | 1804 | ret = 1; |
1740 | err: | 1805 | err: |
1741 | if (a != NULL) BN_free(a); | 1806 | if (a != NULL) |
1742 | if (p != NULL) BN_free(p); | 1807 | BN_free(a); |
1743 | if (r != NULL) BN_free(r); | 1808 | if (p != NULL) |
1809 | BN_free(p); | ||
1810 | if (r != NULL) | ||
1811 | BN_free(r); | ||
1744 | return ret; | 1812 | return ret; |
1745 | } | 1813 | } |
1746 | 1814 | ||
1747 | int | 1815 | int |
1748 | test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) | 1816 | test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_) |
1749 | { | 1817 | { |
1750 | BIGNUM *a,*b,*c,*d; | 1818 | BIGNUM *a, *b, *c, *d; |
1751 | int i; | 1819 | int i; |
1752 | int rc = 1; | 1820 | int rc = 1; |
1753 | 1821 | ||
1754 | b=BN_new(); | 1822 | b = BN_new(); |
1755 | c=BN_new(); | 1823 | c = BN_new(); |
1756 | d=BN_new(); | 1824 | d = BN_new(); |
1757 | BN_one(c); | 1825 | BN_one(c); |
1758 | 1826 | ||
1759 | if(a_) | 1827 | if (a_) |
1760 | a=a_; | 1828 | a = a_; |
1761 | else { | 1829 | else { |
1762 | a=BN_new(); | 1830 | a = BN_new(); |
1763 | BN_bntest_rand(a,200,0,0); /**/ | 1831 | BN_bntest_rand(a,200,0,0); /**/ |
1764 | a->neg=rand_neg(); | 1832 | a->neg = rand_neg(); |
1765 | } | 1833 | } |
1766 | for (i=0; i<num0; i++) { | 1834 | for (i = 0; i < num0; i++) { |
1767 | BN_lshift(b,a,i+1); | 1835 | BN_lshift(b, a, i + 1); |
1768 | BN_add(c,c,c); | 1836 | BN_add(c, c, c); |
1769 | if (bp != NULL) { | 1837 | if (bp != NULL) { |
1770 | if (!results) { | 1838 | if (!results) { |
1771 | BN_print(bp,a); | 1839 | BN_print(bp, a); |
1772 | BIO_puts(bp," * "); | 1840 | BIO_puts(bp, " * "); |
1773 | BN_print(bp,c); | 1841 | BN_print(bp, c); |
1774 | BIO_puts(bp," - "); | 1842 | BIO_puts(bp, " - "); |
1775 | } | 1843 | } |
1776 | BN_print(bp,b); | 1844 | BN_print(bp, b); |
1777 | BIO_puts(bp,"\n"); | 1845 | BIO_puts(bp, "\n"); |
1778 | } | 1846 | } |
1779 | BN_mul(d,a,c,ctx); | 1847 | BN_mul(d, a,c, ctx); |
1780 | BN_sub(d,d,b); | 1848 | BN_sub(d, d, b); |
1781 | if(!BN_is_zero(d)) { | 1849 | if (!BN_is_zero(d)) { |
1782 | fprintf(stderr,"Left shift test failed!\n"); | 1850 | fprintf(stderr, "Left shift test failed!\n"); |
1783 | fprintf(stderr,"a="); | 1851 | fprintf(stderr, "a="); |
1784 | BN_print_fp(stderr,a); | 1852 | BN_print_fp(stderr, a); |
1785 | fprintf(stderr,"\nb="); | 1853 | fprintf(stderr, "\nb="); |
1786 | BN_print_fp(stderr,b); | 1854 | BN_print_fp(stderr, b); |
1787 | fprintf(stderr,"\nc="); | 1855 | fprintf(stderr, "\nc="); |
1788 | BN_print_fp(stderr,c); | 1856 | BN_print_fp(stderr, c); |
1789 | fprintf(stderr,"\nd="); | 1857 | fprintf(stderr, "\nd="); |
1790 | BN_print_fp(stderr,d); | 1858 | BN_print_fp(stderr, d); |
1791 | fprintf(stderr,"\n"); | 1859 | fprintf(stderr, "\n"); |
1792 | rc = 0; | 1860 | rc = 0; |
1793 | break; | 1861 | break; |
1794 | } | 1862 | } |
1795 | } | 1863 | } |
1796 | BN_free(a); | 1864 | BN_free(a); |
1797 | BN_free(b); | 1865 | BN_free(b); |
1798 | BN_free(c); | 1866 | BN_free(c); |
1799 | BN_free(d); | 1867 | BN_free(d); |
1800 | return(rc); | 1868 | return (rc); |
1801 | } | 1869 | } |
1802 | 1870 | ||
1803 | int | 1871 | int |
1804 | test_lshift1(BIO *bp) | 1872 | test_lshift1(BIO *bp) |
1805 | { | 1873 | { |
1806 | BIGNUM *a,*b,*c; | 1874 | BIGNUM *a, *b, *c; |
1807 | int i; | 1875 | int i; |
1808 | int rc = 1; | 1876 | int rc = 1; |
1809 | 1877 | ||
1810 | a=BN_new(); | 1878 | a = BN_new(); |
1811 | b=BN_new(); | 1879 | b = BN_new(); |
1812 | c=BN_new(); | 1880 | c = BN_new(); |
1813 | 1881 | ||
1814 | BN_bntest_rand(a,200,0,0); /**/ | 1882 | BN_bntest_rand(a,200,0,0); /**/ |
1815 | a->neg=rand_neg(); | 1883 | a->neg = rand_neg(); |
1816 | for (i=0; i<num0; i++) { | 1884 | for (i = 0; i < num0; i++) { |
1817 | BN_lshift1(b,a); | 1885 | BN_lshift1(b, a); |
1818 | if (bp != NULL) { | 1886 | if (bp != NULL) { |
1819 | if (!results) { | 1887 | if (!results) { |
1820 | BN_print(bp,a); | 1888 | BN_print(bp, a); |
1821 | BIO_puts(bp," * 2"); | 1889 | BIO_puts(bp, " * 2"); |
1822 | BIO_puts(bp," - "); | 1890 | BIO_puts(bp, " - "); |
1823 | } | 1891 | } |
1824 | BN_print(bp,b); | 1892 | BN_print(bp, b); |
1825 | BIO_puts(bp,"\n"); | 1893 | BIO_puts(bp, "\n"); |
1826 | } | 1894 | } |
1827 | BN_add(c,a,a); | 1895 | BN_add(c, a, a); |
1828 | BN_sub(a,b,c); | 1896 | BN_sub(a, b, c); |
1829 | if(!BN_is_zero(a)) { | 1897 | if (!BN_is_zero(a)) { |
1830 | fprintf(stderr,"Left shift one test failed!\n"); | 1898 | fprintf(stderr, "Left shift one test failed!\n"); |
1831 | rc = 0; | 1899 | rc = 0; |
1832 | break; | 1900 | break; |
1833 | } | 1901 | } |
1834 | 1902 | ||
1835 | BN_copy(a,b); | 1903 | BN_copy(a, b); |
1836 | } | 1904 | } |
1837 | BN_free(a); | 1905 | BN_free(a); |
1838 | BN_free(b); | 1906 | BN_free(b); |
1839 | BN_free(c); | 1907 | BN_free(c); |
1840 | return(rc); | 1908 | return (rc); |
1841 | } | 1909 | } |
1842 | 1910 | ||
1843 | int | 1911 | int |
1844 | test_rshift(BIO *bp,BN_CTX *ctx) | 1912 | test_rshift(BIO *bp, BN_CTX *ctx) |
1845 | { | 1913 | { |
1846 | BIGNUM *a,*b,*c,*d,*e; | 1914 | BIGNUM *a, *b, *c, *d, *e; |
1847 | int i; | 1915 | int i; |
1848 | int rc = 1; | 1916 | int rc = 1; |
1849 | 1917 | ||
1850 | a=BN_new(); | 1918 | a = BN_new(); |
1851 | b=BN_new(); | 1919 | b = BN_new(); |
1852 | c=BN_new(); | 1920 | c = BN_new(); |
1853 | d=BN_new(); | 1921 | d = BN_new(); |
1854 | e=BN_new(); | 1922 | e = BN_new(); |
1855 | BN_one(c); | 1923 | BN_one(c); |
1856 | 1924 | ||
1857 | BN_bntest_rand(a,200,0,0); /**/ | 1925 | BN_bntest_rand(a,200,0,0); /**/ |
1858 | a->neg=rand_neg(); | 1926 | a->neg = rand_neg(); |
1859 | for (i=0; i<num0; i++) { | 1927 | for (i = 0; i < num0; i++) { |
1860 | BN_rshift(b,a,i+1); | 1928 | BN_rshift(b, a, i + 1); |
1861 | BN_add(c,c,c); | 1929 | BN_add(c, c, c); |
1862 | if (bp != NULL) { | 1930 | if (bp != NULL) { |
1863 | if (!results) { | 1931 | if (!results) { |
1864 | BN_print(bp,a); | 1932 | BN_print(bp, a); |
1865 | BIO_puts(bp," / "); | 1933 | BIO_puts(bp, " / "); |
1866 | BN_print(bp,c); | 1934 | BN_print(bp, c); |
1867 | BIO_puts(bp," - "); | 1935 | BIO_puts(bp, " - "); |
1868 | } | 1936 | } |
1869 | BN_print(bp,b); | 1937 | BN_print(bp, b); |
1870 | BIO_puts(bp,"\n"); | 1938 | BIO_puts(bp, "\n"); |
1871 | } | 1939 | } |
1872 | BN_div(d,e,a,c,ctx); | 1940 | BN_div(d, e,a, c, ctx); |
1873 | BN_sub(d,d,b); | 1941 | BN_sub(d, d, b); |
1874 | if(!BN_is_zero(d)) { | 1942 | if (!BN_is_zero(d)) { |
1875 | fprintf(stderr,"Right shift test failed!\n"); | 1943 | fprintf(stderr, "Right shift test failed!\n"); |
1876 | rc = 0; | 1944 | rc = 0; |
1877 | break; | 1945 | break; |
1878 | } | 1946 | } |
1879 | } | 1947 | } |
1880 | BN_free(a); | 1948 | BN_free(a); |
@@ -1882,55 +1950,55 @@ test_rshift(BIO *bp,BN_CTX *ctx) | |||
1882 | BN_free(c); | 1950 | BN_free(c); |
1883 | BN_free(d); | 1951 | BN_free(d); |
1884 | BN_free(e); | 1952 | BN_free(e); |
1885 | return(rc); | 1953 | return (rc); |
1886 | } | 1954 | } |
1887 | 1955 | ||
1888 | int | 1956 | int |
1889 | test_rshift1(BIO *bp) | 1957 | test_rshift1(BIO *bp) |
1890 | { | 1958 | { |
1891 | BIGNUM *a,*b,*c; | 1959 | BIGNUM *a, *b, *c; |
1892 | int i; | 1960 | int i; |
1893 | int rc = 1; | 1961 | int rc = 1; |
1894 | 1962 | ||
1895 | a=BN_new(); | 1963 | a = BN_new(); |
1896 | b=BN_new(); | 1964 | b = BN_new(); |
1897 | c=BN_new(); | 1965 | c = BN_new(); |
1898 | 1966 | ||
1899 | BN_bntest_rand(a,200,0,0); /**/ | 1967 | BN_bntest_rand(a,200,0,0); /**/ |
1900 | a->neg=rand_neg(); | 1968 | a->neg = rand_neg(); |
1901 | for (i=0; i<num0; i++) { | 1969 | for (i = 0; i < num0; i++) { |
1902 | BN_rshift1(b,a); | 1970 | BN_rshift1(b, a); |
1903 | if (bp != NULL) { | 1971 | if (bp != NULL) { |
1904 | if (!results) { | 1972 | if (!results) { |
1905 | BN_print(bp,a); | 1973 | BN_print(bp, a); |
1906 | BIO_puts(bp," / 2"); | 1974 | BIO_puts(bp, " / 2"); |
1907 | BIO_puts(bp," - "); | 1975 | BIO_puts(bp, " - "); |
1908 | } | 1976 | } |
1909 | BN_print(bp,b); | 1977 | BN_print(bp, b); |
1910 | BIO_puts(bp,"\n"); | 1978 | BIO_puts(bp, "\n"); |
1911 | } | 1979 | } |
1912 | BN_sub(c,a,b); | 1980 | BN_sub(c, a, b); |
1913 | BN_sub(c,c,b); | 1981 | BN_sub(c, c, b); |
1914 | if(!BN_is_zero(c) && !BN_abs_is_word(c, 1)) { | 1982 | if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) { |
1915 | fprintf(stderr,"Right shift one test failed!\n"); | 1983 | fprintf(stderr, "Right shift one test failed!\n"); |
1916 | rc = 0; | 1984 | rc = 0; |
1917 | break; | 1985 | break; |
1918 | } | 1986 | } |
1919 | BN_copy(a,b); | 1987 | BN_copy(a, b); |
1920 | } | 1988 | } |
1921 | BN_free(a); | 1989 | BN_free(a); |
1922 | BN_free(b); | 1990 | BN_free(b); |
1923 | BN_free(c); | 1991 | BN_free(c); |
1924 | return(rc); | 1992 | return (rc); |
1925 | } | 1993 | } |
1926 | 1994 | ||
1927 | int | 1995 | int |
1928 | rand_neg(void) | 1996 | rand_neg(void) |
1929 | { | 1997 | { |
1930 | static unsigned int neg=0; | 1998 | static unsigned int neg = 0; |
1931 | static int sign[8]={0,0,0,1,1,0,1,1}; | 1999 | static int sign[8] = {0, 0,0, 1,1, 0,1, 1}; |
1932 | 2000 | ||
1933 | return(sign[(neg++)%8]); | 2001 | return (sign[(neg++) % 8]); |
1934 | } | 2002 | } |
1935 | 2003 | ||
1936 | int | 2004 | int |