diff options
Diffstat (limited to 'src/regress/lib/libcrypto/exp/exptest.c')
| -rw-r--r-- | src/regress/lib/libcrypto/exp/exptest.c | 353 |
1 files changed, 0 insertions, 353 deletions
diff --git a/src/regress/lib/libcrypto/exp/exptest.c b/src/regress/lib/libcrypto/exp/exptest.c deleted file mode 100644 index 9c683464f6..0000000000 --- a/src/regress/lib/libcrypto/exp/exptest.c +++ /dev/null | |||
| @@ -1,353 +0,0 @@ | |||
| 1 | /* $OpenBSD: exptest.c,v 1.5 2018/07/17 17:06:49 tb Exp $ */ | ||
| 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 | #include <stdio.h> | ||
| 60 | #include <stdlib.h> | ||
| 61 | #include <string.h> | ||
| 62 | |||
| 63 | #include <openssl/bio.h> | ||
| 64 | #include <openssl/bn.h> | ||
| 65 | #include <openssl/err.h> | ||
| 66 | |||
| 67 | int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 68 | const BIGNUM *m, BN_CTX *ctx); | ||
| 69 | int BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 70 | const BIGNUM *m, BN_CTX *ctx); | ||
| 71 | int BN_mod_exp_mont_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 72 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
| 73 | int BN_mod_exp_mont_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 74 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
| 75 | |||
| 76 | #define NUM_BITS (BN_BITS*2) | ||
| 77 | |||
| 78 | /* | ||
| 79 | * Test that r == 0 in test_exp_mod_zero(). Returns one on success, | ||
| 80 | * returns zero and prints debug output otherwise. | ||
| 81 | */ | ||
| 82 | static int a_is_zero_mod_one(const char *method, const BIGNUM *r, | ||
| 83 | const BIGNUM *a) { | ||
| 84 | if (!BN_is_zero(r)) { | ||
| 85 | fprintf(stderr, "%s failed:\n", method); | ||
| 86 | fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n"); | ||
| 87 | fprintf(stderr, "a = "); | ||
| 88 | BN_print_fp(stderr, a); | ||
| 89 | fprintf(stderr, "\nr = "); | ||
| 90 | BN_print_fp(stderr, r); | ||
| 91 | fprintf(stderr, "\n"); | ||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | /* | ||
| 98 | * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success. | ||
| 99 | */ | ||
| 100 | static int test_exp_mod_zero(void) | ||
| 101 | { | ||
| 102 | BIGNUM a, p, m; | ||
| 103 | BIGNUM r; | ||
| 104 | BN_ULONG one_word = 1; | ||
| 105 | BN_CTX *ctx = BN_CTX_new(); | ||
| 106 | int ret = 1, failed = 0; | ||
| 107 | |||
| 108 | BN_init(&m); | ||
| 109 | BN_one(&m); | ||
| 110 | |||
| 111 | BN_init(&a); | ||
| 112 | BN_one(&a); | ||
| 113 | |||
| 114 | BN_init(&p); | ||
| 115 | BN_zero(&p); | ||
| 116 | |||
| 117 | BN_init(&r); | ||
| 118 | |||
| 119 | if (!BN_rand(&a, 1024, 0, 0)) | ||
| 120 | goto err; | ||
| 121 | |||
| 122 | if (!BN_mod_exp(&r, &a, &p, &m, ctx)) | ||
| 123 | goto err; | ||
| 124 | |||
| 125 | if (!a_is_zero_mod_one("BN_mod_exp", &r, &a)) | ||
| 126 | failed = 1; | ||
| 127 | |||
| 128 | if (!BN_mod_exp_ct(&r, &a, &p, &m, ctx)) | ||
| 129 | goto err; | ||
| 130 | |||
| 131 | if (!a_is_zero_mod_one("BN_mod_exp_ct", &r, &a)) | ||
| 132 | failed = 1; | ||
| 133 | |||
| 134 | if (!BN_mod_exp_nonct(&r, &a, &p, &m, ctx)) | ||
| 135 | goto err; | ||
| 136 | |||
| 137 | if (!a_is_zero_mod_one("BN_mod_exp_nonct", &r, &a)) | ||
| 138 | failed = 1; | ||
| 139 | |||
| 140 | if (!BN_mod_exp_recp(&r, &a, &p, &m, ctx)) | ||
| 141 | goto err; | ||
| 142 | |||
| 143 | if (!a_is_zero_mod_one("BN_mod_exp_recp", &r, &a)) | ||
| 144 | failed = 1; | ||
| 145 | |||
| 146 | if (!BN_mod_exp_simple(&r, &a, &p, &m, ctx)) | ||
| 147 | goto err; | ||
| 148 | |||
| 149 | if (!a_is_zero_mod_one("BN_mod_exp_simple", &r, &a)) | ||
| 150 | failed = 1; | ||
| 151 | |||
| 152 | if (!BN_mod_exp_mont(&r, &a, &p, &m, ctx, NULL)) | ||
| 153 | goto err; | ||
| 154 | |||
| 155 | if (!a_is_zero_mod_one("BN_mod_exp_mont", &r, &a)) | ||
| 156 | failed = 1; | ||
| 157 | |||
| 158 | if (!BN_mod_exp_mont_ct(&r, &a, &p, &m, ctx, NULL)) | ||
| 159 | goto err; | ||
| 160 | |||
| 161 | if (!a_is_zero_mod_one("BN_mod_exp_mont_ct", &r, &a)) | ||
| 162 | failed = 1; | ||
| 163 | |||
| 164 | if (!BN_mod_exp_mont_nonct(&r, &a, &p, &m, ctx, NULL)) | ||
| 165 | goto err; | ||
| 166 | |||
| 167 | if (!a_is_zero_mod_one("BN_mod_exp_mont_nonct", &r, &a)) | ||
| 168 | failed = 1; | ||
| 169 | |||
| 170 | if (!BN_mod_exp_mont_consttime(&r, &a, &p, &m, ctx, NULL)) { | ||
| 171 | goto err; | ||
| 172 | } | ||
| 173 | |||
| 174 | if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", &r, &a)) | ||
| 175 | failed = 1; | ||
| 176 | |||
| 177 | /* | ||
| 178 | * A different codepath exists for single word multiplication | ||
| 179 | * in non-constant-time only. | ||
| 180 | */ | ||
| 181 | if (!BN_mod_exp_mont_word(&r, one_word, &p, &m, ctx, NULL)) | ||
| 182 | goto err; | ||
| 183 | |||
| 184 | if (!BN_is_zero(&r)) { | ||
| 185 | fprintf(stderr, "BN_mod_exp_mont_word failed:\n"); | ||
| 186 | fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n"); | ||
| 187 | fprintf(stderr, "r = "); | ||
| 188 | BN_print_fp(stderr, &r); | ||
| 189 | fprintf(stderr, "\n"); | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | ret = failed; | ||
| 194 | |||
| 195 | err: | ||
| 196 | BN_free(&r); | ||
| 197 | BN_free(&a); | ||
| 198 | BN_free(&p); | ||
| 199 | BN_free(&m); | ||
| 200 | BN_CTX_free(ctx); | ||
| 201 | |||
| 202 | return ret; | ||
| 203 | } | ||
| 204 | |||
| 205 | int main(int argc, char *argv[]) | ||
| 206 | { | ||
| 207 | BN_CTX *ctx; | ||
| 208 | BIO *out = NULL; | ||
| 209 | int i, ret; | ||
| 210 | unsigned char c; | ||
| 211 | BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, | ||
| 212 | *r_mont_ct, *r_mont_nonct, *a, *b, *m; | ||
| 213 | |||
| 214 | ERR_load_BN_strings(); | ||
| 215 | |||
| 216 | ctx = BN_CTX_new(); | ||
| 217 | if (ctx == NULL) | ||
| 218 | exit(1); | ||
| 219 | r_mont = BN_new(); | ||
| 220 | r_mont_const = BN_new(); | ||
| 221 | r_mont_ct = BN_new(); | ||
| 222 | r_mont_nonct = BN_new(); | ||
| 223 | r_recp = BN_new(); | ||
| 224 | r_simple = BN_new(); | ||
| 225 | a = BN_new(); | ||
| 226 | b = BN_new(); | ||
| 227 | m = BN_new(); | ||
| 228 | if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL)) | ||
| 229 | goto err; | ||
| 230 | |||
| 231 | out = BIO_new(BIO_s_file()); | ||
| 232 | |||
| 233 | if (out == NULL) | ||
| 234 | exit(1); | ||
| 235 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | ||
| 236 | |||
| 237 | for (i = 0; i < 200; i++) { | ||
| 238 | arc4random_buf(&c, 1); | ||
| 239 | c = (c % BN_BITS) - BN_BITS2; | ||
| 240 | BN_rand(a, NUM_BITS + c, 0, 0); | ||
| 241 | |||
| 242 | arc4random_buf(&c, 1); | ||
| 243 | c = (c % BN_BITS) - BN_BITS2; | ||
| 244 | BN_rand(b, NUM_BITS + c, 0, 0); | ||
| 245 | |||
| 246 | arc4random_buf(&c, 1); | ||
| 247 | c = (c % BN_BITS) - BN_BITS2; | ||
| 248 | BN_rand(m, NUM_BITS + c, 0, 1); | ||
| 249 | |||
| 250 | BN_mod(a, a, m, ctx); | ||
| 251 | BN_mod(b, b, m, ctx); | ||
| 252 | |||
| 253 | ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL); | ||
| 254 | if (ret <= 0) { | ||
| 255 | printf("BN_mod_exp_mont() problems\n"); | ||
| 256 | ERR_print_errors(out); | ||
| 257 | exit(1); | ||
| 258 | } | ||
| 259 | |||
| 260 | ret = BN_mod_exp_mont_ct(r_mont_ct, a, b, m, ctx, NULL); | ||
| 261 | if (ret <= 0) { | ||
| 262 | printf("BN_mod_exp_mont_ct() problems\n"); | ||
| 263 | ERR_print_errors(out); | ||
| 264 | exit(1); | ||
| 265 | } | ||
| 266 | |||
| 267 | ret = BN_mod_exp_mont_nonct(r_mont_nonct, a, b, m, ctx, NULL); | ||
| 268 | if (ret <= 0) { | ||
| 269 | printf("BN_mod_exp_mont_nonct() problems\n"); | ||
| 270 | ERR_print_errors(out); | ||
| 271 | exit(1); | ||
| 272 | } | ||
| 273 | |||
| 274 | ret = BN_mod_exp_recp(r_recp, a, b, m, ctx); | ||
| 275 | if (ret <= 0) { | ||
| 276 | printf("BN_mod_exp_recp() problems\n"); | ||
| 277 | ERR_print_errors(out); | ||
| 278 | exit(1); | ||
| 279 | } | ||
| 280 | |||
| 281 | ret = BN_mod_exp_simple(r_simple, a, b, m, ctx); | ||
| 282 | if (ret <= 0) { | ||
| 283 | printf("BN_mod_exp_simple() problems\n"); | ||
| 284 | ERR_print_errors(out); | ||
| 285 | exit(1); | ||
| 286 | } | ||
| 287 | |||
| 288 | ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL); | ||
| 289 | if (ret <= 0) { | ||
| 290 | printf("BN_mod_exp_mont_consttime() problems\n"); | ||
| 291 | ERR_print_errors(out); | ||
| 292 | exit(1); | ||
| 293 | } | ||
| 294 | |||
| 295 | if (BN_cmp(r_simple, r_mont) == 0 | ||
| 296 | && BN_cmp(r_simple, r_recp) == 0 | ||
| 297 | && BN_cmp(r_simple, r_mont_const) == 0) { | ||
| 298 | printf("."); | ||
| 299 | fflush(stdout); | ||
| 300 | } else { | ||
| 301 | if (BN_cmp(r_simple, r_mont) != 0) | ||
| 302 | printf("\nsimple and mont results differ\n"); | ||
| 303 | if (BN_cmp(r_simple, r_mont_const) != 0) | ||
| 304 | printf("\nsimple and mont const time results differ\n"); | ||
| 305 | if (BN_cmp(r_simple, r_recp) != 0) | ||
| 306 | printf("\nsimple and recp results differ\n"); | ||
| 307 | if (BN_cmp(r_mont, r_mont_ct) != 0) | ||
| 308 | printf("\nmont_ct and mont results differ\n"); | ||
| 309 | if (BN_cmp(r_mont_ct, r_mont_nonct) != 0) | ||
| 310 | printf("\nmont_ct and mont_nonct results differ\n"); | ||
| 311 | |||
| 312 | printf("a (%3d) = ", BN_num_bits(a)); | ||
| 313 | BN_print(out, a); | ||
| 314 | printf("\nb (%3d) = ", BN_num_bits(b)); | ||
| 315 | BN_print(out, b); | ||
| 316 | printf("\nm (%3d) = ", BN_num_bits(m)); | ||
| 317 | BN_print(out, m); | ||
| 318 | printf("\nsimple ="); | ||
| 319 | BN_print(out, r_simple); | ||
| 320 | printf("\nrecp ="); | ||
| 321 | BN_print(out, r_recp); | ||
| 322 | printf("\nmont ="); | ||
| 323 | BN_print(out, r_mont); | ||
| 324 | printf("\nmont_ct ="); | ||
| 325 | BN_print(out, r_mont_const); | ||
| 326 | printf("\n"); | ||
| 327 | exit(1); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | BN_free(r_mont); | ||
| 331 | BN_free(r_mont_const); | ||
| 332 | BN_free(r_recp); | ||
| 333 | BN_free(r_simple); | ||
| 334 | BN_free(a); | ||
| 335 | BN_free(b); | ||
| 336 | BN_free(m); | ||
| 337 | BN_CTX_free(ctx); | ||
| 338 | ERR_remove_thread_state(NULL); | ||
| 339 | CRYPTO_mem_leaks(out); | ||
| 340 | BIO_free(out); | ||
| 341 | printf("\n"); | ||
| 342 | |||
| 343 | if (test_exp_mod_zero() != 0) | ||
| 344 | goto err; | ||
| 345 | |||
| 346 | printf("done\n"); | ||
| 347 | |||
| 348 | return (0); | ||
| 349 | err: | ||
| 350 | ERR_load_crypto_strings(); | ||
| 351 | ERR_print_errors(out); | ||
| 352 | return (1); | ||
| 353 | } | ||
