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