diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/bn/Makefile | 4 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mod_exp.c | 219 |
2 files changed, 1 insertions, 222 deletions
diff --git a/src/regress/lib/libcrypto/bn/Makefile b/src/regress/lib/libcrypto/bn/Makefile index bcdfe3ed4f..7e50894076 100644 --- a/src/regress/lib/libcrypto/bn/Makefile +++ b/src/regress/lib/libcrypto/bn/Makefile | |||
| @@ -1,9 +1,8 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.22 2023/03/11 14:04:21 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.23 2023/03/18 09:04:05 tb Exp $ |
| 2 | 2 | ||
| 3 | PROGS += bn_add_sub | 3 | PROGS += bn_add_sub |
| 4 | PROGS += bn_cmp | 4 | PROGS += bn_cmp |
| 5 | PROGS += bn_isqrt | 5 | PROGS += bn_isqrt |
| 6 | PROGS += bn_mod_exp | ||
| 7 | PROGS += bn_mod_exp2_mont | 6 | PROGS += bn_mod_exp2_mont |
| 8 | PROGS += bn_mod_exp_zero | 7 | PROGS += bn_mod_exp_zero |
| 9 | PROGS += bn_mod_sqrt | 8 | PROGS += bn_mod_sqrt |
| @@ -18,7 +17,6 @@ PROGS += bn_unit | |||
| 18 | PROGS += bn_word | 17 | PROGS += bn_word |
| 19 | 18 | ||
| 20 | STATIC_LINK += bn_isqrt | 19 | STATIC_LINK += bn_isqrt |
| 21 | STATIC_LINK += bn_mod_exp | ||
| 22 | STATIC_LINK += bn_mod_exp_zero | 20 | STATIC_LINK += bn_mod_exp_zero |
| 23 | STATIC_LINK += bn_primes | 21 | STATIC_LINK += bn_primes |
| 24 | STATIC_LINK += bn_rand_interval | 22 | STATIC_LINK += bn_rand_interval |
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_exp.c b/src/regress/lib/libcrypto/bn/bn_mod_exp.c deleted file mode 100644 index a2dc2a4e36..0000000000 --- a/src/regress/lib/libcrypto/bn/bn_mod_exp.c +++ /dev/null | |||
| @@ -1,219 +0,0 @@ | |||
| 1 | /* $OpenBSD: bn_mod_exp.c,v 1.12 2023/03/15 04:26:23 jsing 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 | |||
| 62 | #include <openssl/bn.h> | ||
| 63 | #include <openssl/err.h> | ||
| 64 | |||
| 65 | #include "bn_local.h" | ||
| 66 | |||
| 67 | #define NUM_BITS (BN_BITS*2) | ||
| 68 | |||
| 69 | #define INIT_MOD_EXP_FN(f) { .name = #f, .mod_exp_fn = (f), } | ||
| 70 | #define INIT_MOD_EXP_MONT_FN(f) { .name = #f, .mod_exp_mont_fn = (f), } | ||
| 71 | |||
| 72 | static const struct mod_exp_test { | ||
| 73 | const char *name; | ||
| 74 | int (*mod_exp_fn)(BIGNUM *,const BIGNUM *, const BIGNUM *, | ||
| 75 | const BIGNUM *, BN_CTX *); | ||
| 76 | int (*mod_exp_mont_fn)(BIGNUM *,const BIGNUM *, const BIGNUM *, | ||
| 77 | const BIGNUM *, BN_CTX *, BN_MONT_CTX *); | ||
| 78 | } mod_exp_fn[] = { | ||
| 79 | INIT_MOD_EXP_FN(BN_mod_exp), | ||
| 80 | INIT_MOD_EXP_FN(BN_mod_exp_ct), | ||
| 81 | INIT_MOD_EXP_FN(BN_mod_exp_nonct), | ||
| 82 | INIT_MOD_EXP_FN(BN_mod_exp_recp), | ||
| 83 | INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont), | ||
| 84 | INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_ct), | ||
| 85 | INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_consttime), | ||
| 86 | INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_nonct), | ||
| 87 | }; | ||
| 88 | |||
| 89 | #define N_MOD_EXP_FN (sizeof(mod_exp_fn) / sizeof(mod_exp_fn[0])) | ||
| 90 | |||
| 91 | static int | ||
| 92 | rand_neg(void) | ||
| 93 | { | ||
| 94 | static unsigned int neg = 0; | ||
| 95 | static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 }; | ||
| 96 | |||
| 97 | return (sign[(neg++) % 8]); | ||
| 98 | } | ||
| 99 | |||
| 100 | static int | ||
| 101 | test_mod_exp(const BIGNUM *result_simple, const BIGNUM *a, const BIGNUM *b, | ||
| 102 | const BIGNUM *m, BN_CTX *ctx, const struct mod_exp_test *test) | ||
| 103 | { | ||
| 104 | BIGNUM *result; | ||
| 105 | int ret = 0; | ||
| 106 | |||
| 107 | BN_CTX_start(ctx); | ||
| 108 | |||
| 109 | if ((result = BN_CTX_get(ctx)) == NULL) | ||
| 110 | goto err; | ||
| 111 | |||
| 112 | if (test->mod_exp_fn != NULL) { | ||
| 113 | if (!test->mod_exp_fn(result, a, b, m, ctx)) { | ||
| 114 | printf("%s() problems\n", test->name); | ||
| 115 | goto err; | ||
| 116 | } | ||
| 117 | } else { | ||
| 118 | if (!test->mod_exp_mont_fn(result, a, b, m, ctx, NULL)) { | ||
| 119 | printf("%s() problems\n", test->name); | ||
| 120 | goto err; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | if (BN_cmp(result_simple, result) != 0) { | ||
| 125 | printf("\nResults from BN_mod_exp_simple and %s differ\n", | ||
| 126 | test->name); | ||
| 127 | |||
| 128 | printf("a (%3d) = ", BN_num_bits(a)); | ||
| 129 | BN_print_fp(stdout, a); | ||
| 130 | printf("\nb (%3d) = ", BN_num_bits(b)); | ||
| 131 | BN_print_fp(stdout, b); | ||
| 132 | printf("\nm (%3d) = ", BN_num_bits(m)); | ||
| 133 | BN_print_fp(stdout, m); | ||
| 134 | printf("\nsimple = "); | ||
| 135 | BN_print_fp(stdout, result_simple); | ||
| 136 | printf("\nresult = "); | ||
| 137 | BN_print_fp(stdout, result); | ||
| 138 | printf("\n"); | ||
| 139 | |||
| 140 | goto err; | ||
| 141 | } | ||
| 142 | |||
| 143 | ret = 1; | ||
| 144 | |||
| 145 | err: | ||
| 146 | BN_CTX_end(ctx); | ||
| 147 | |||
| 148 | return ret; | ||
| 149 | } | ||
| 150 | |||
| 151 | int | ||
| 152 | main(int argc, char *argv[]) | ||
| 153 | { | ||
| 154 | BIGNUM *result_simple, *a, *b, *m; | ||
| 155 | BN_CTX *ctx; | ||
| 156 | int c, i; | ||
| 157 | size_t j; | ||
| 158 | |||
| 159 | if ((ctx = BN_CTX_new()) == NULL) | ||
| 160 | goto err; | ||
| 161 | |||
| 162 | BN_CTX_start(ctx); | ||
| 163 | |||
| 164 | if ((a = BN_CTX_get(ctx)) == NULL) | ||
| 165 | goto err; | ||
| 166 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
| 167 | goto err; | ||
| 168 | if ((m = BN_CTX_get(ctx)) == NULL) | ||
| 169 | goto err; | ||
| 170 | if ((result_simple = BN_CTX_get(ctx)) == NULL) | ||
| 171 | goto err; | ||
| 172 | |||
| 173 | for (i = 0; i < 1000; i++) { | ||
| 174 | c = arc4random() % BN_BITS - BN_BITS2; | ||
| 175 | if (!BN_rand(a, NUM_BITS + c, 0, 0)) | ||
| 176 | goto err; | ||
| 177 | |||
| 178 | BN_set_negative(a, rand_neg()); | ||
| 179 | |||
| 180 | c = arc4random() % BN_BITS - BN_BITS2; | ||
| 181 | if (!BN_rand(b, NUM_BITS + c, 0, 0)) | ||
| 182 | goto err; | ||
| 183 | |||
| 184 | BN_set_negative(b, rand_neg()); | ||
| 185 | |||
| 186 | c = arc4random() % BN_BITS - BN_BITS2; | ||
| 187 | if (!BN_rand(m, NUM_BITS + c, 0, 1)) | ||
| 188 | goto err; | ||
| 189 | |||
| 190 | if (!BN_mod(a, a, m, ctx)) | ||
| 191 | goto err; | ||
| 192 | if (!BN_mod(b, b, m, ctx)) | ||
| 193 | goto err; | ||
| 194 | |||
| 195 | if ((BN_mod_exp_simple(result_simple, a, b, m, ctx)) <= 0) { | ||
| 196 | printf("BN_mod_exp_simple() problems\n"); | ||
| 197 | goto err; | ||
| 198 | } | ||
| 199 | |||
| 200 | for (j = 0; j < N_MOD_EXP_FN; j++) { | ||
| 201 | const struct mod_exp_test *test = &mod_exp_fn[j]; | ||
| 202 | |||
| 203 | if (!test_mod_exp(result_simple, a, b, m, ctx, test)) | ||
| 204 | goto err; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | BN_CTX_end(ctx); | ||
| 209 | BN_CTX_free(ctx); | ||
| 210 | |||
| 211 | return 0; | ||
| 212 | |||
| 213 | err: | ||
| 214 | BN_CTX_end(ctx); | ||
| 215 | BN_CTX_free(ctx); | ||
| 216 | ERR_print_errors_fp(stdout); | ||
| 217 | |||
| 218 | return 1; | ||
| 219 | } | ||
