Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
Richard Levitte | 55f78ba | 2002-11-28 18:54:30 +0000 | [diff] [blame] | 13 | |
| 14 | #include "../e_os.h" |
| 15 | |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 16 | #include <openssl/bio.h> |
| 17 | #include <openssl/bn.h> |
| 18 | #include <openssl/rand.h> |
| 19 | #include <openssl/err.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 20 | |
Rich Salz | 94af0cd | 2016-01-28 10:13:21 -0500 | [diff] [blame] | 21 | #define NUM_BITS (BN_BITS2 * 4) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 22 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 23 | static const char rnd_seed[] = |
| 24 | "string to make the random number generator think it has entropy"; |
Bodo Möller | 0c50e02 | 2000-01-14 17:55:37 +0000 | [diff] [blame] | 25 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 26 | /* |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 27 | * Test that r == 0 in test_exp_mod_zero(). Returns one on success, |
| 28 | * returns zero and prints debug output otherwise. |
| 29 | */ |
| 30 | static int a_is_zero_mod_one(const char *method, const BIGNUM *r, |
| 31 | const BIGNUM *a) { |
| 32 | if (!BN_is_zero(r)) { |
| 33 | fprintf(stderr, "%s failed:\n", method); |
| 34 | fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n"); |
| 35 | fprintf(stderr, "a = "); |
| 36 | BN_print_fp(stderr, a); |
| 37 | fprintf(stderr, "\nr = "); |
| 38 | BN_print_fp(stderr, r); |
| 39 | fprintf(stderr, "\n"); |
| 40 | return 0; |
| 41 | } |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | /* |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 46 | * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success. |
| 47 | */ |
| 48 | static int test_exp_mod_zero() |
| 49 | { |
| 50 | BIGNUM *a = NULL, *p = NULL, *m = NULL; |
| 51 | BIGNUM *r = NULL; |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 52 | BN_ULONG one_word = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | BN_CTX *ctx = BN_CTX_new(); |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 54 | int ret = 1, failed = 0; |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 55 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 56 | m = BN_new(); |
| 57 | if (!m) |
| 58 | goto err; |
| 59 | BN_one(m); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 60 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | a = BN_new(); |
| 62 | if (!a) |
| 63 | goto err; |
| 64 | BN_one(a); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 65 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 66 | p = BN_new(); |
| 67 | if (!p) |
| 68 | goto err; |
| 69 | BN_zero(p); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 70 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 71 | r = BN_new(); |
| 72 | if (!r) |
| 73 | goto err; |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 74 | |
Rich Salz | 2301d91d | 2016-08-08 22:12:28 -0400 | [diff] [blame] | 75 | if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 76 | goto err; |
| 77 | |
| 78 | if (!BN_mod_exp(r, a, p, m, ctx)) |
| 79 | goto err; |
| 80 | |
| 81 | if (!a_is_zero_mod_one("BN_mod_exp", r, a)) |
| 82 | failed = 1; |
| 83 | |
| 84 | if (!BN_mod_exp_recp(r, a, p, m, ctx)) |
| 85 | goto err; |
| 86 | |
| 87 | if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a)) |
| 88 | failed = 1; |
| 89 | |
| 90 | if (!BN_mod_exp_simple(r, a, p, m, ctx)) |
| 91 | goto err; |
| 92 | |
| 93 | if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a)) |
| 94 | failed = 1; |
| 95 | |
| 96 | if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL)) |
| 97 | goto err; |
| 98 | |
| 99 | if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a)) |
| 100 | failed = 1; |
| 101 | |
| 102 | if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) { |
| 103 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 104 | } |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 105 | |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 106 | if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)) |
| 107 | failed = 1; |
| 108 | |
| 109 | /* |
| 110 | * A different codepath exists for single word multiplication |
| 111 | * in non-constant-time only. |
| 112 | */ |
| 113 | if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)) |
| 114 | goto err; |
| 115 | |
| 116 | if (!BN_is_zero(r)) { |
| 117 | fprintf(stderr, "BN_mod_exp_mont_word failed:\n"); |
| 118 | fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n"); |
| 119 | fprintf(stderr, "r = "); |
| 120 | BN_print_fp(stderr, r); |
| 121 | fprintf(stderr, "\n"); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | ret = failed; |
| 126 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 127 | err: |
| 128 | BN_free(r); |
| 129 | BN_free(a); |
| 130 | BN_free(p); |
| 131 | BN_free(m); |
Emilia Kasper | d911097 | 2015-12-14 16:38:15 +0100 | [diff] [blame] | 132 | BN_CTX_free(ctx); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 133 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 134 | return ret; |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 137 | int main(int argc, char *argv[]) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 138 | { |
| 139 | BN_CTX *ctx; |
| 140 | BIO *out = NULL; |
| 141 | int i, ret; |
| 142 | unsigned char c; |
| 143 | BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 144 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 145 | RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we |
| 146 | * don't even check its return |
| 147 | * value (which we should) */ |
Bodo Möller | 0c50e02 | 2000-01-14 17:55:37 +0000 | [diff] [blame] | 148 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 149 | ctx = BN_CTX_new(); |
| 150 | if (ctx == NULL) |
| 151 | EXIT(1); |
| 152 | r_mont = BN_new(); |
| 153 | r_mont_const = BN_new(); |
| 154 | r_recp = BN_new(); |
| 155 | r_simple = BN_new(); |
| 156 | a = BN_new(); |
| 157 | b = BN_new(); |
| 158 | m = BN_new(); |
| 159 | if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL)) |
| 160 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 161 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 162 | out = BIO_new(BIO_s_file()); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 163 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 164 | if (out == NULL) |
| 165 | EXIT(1); |
Richard Levitte | 0f81f5f | 2015-09-04 14:07:57 +0200 | [diff] [blame] | 166 | BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 167 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 168 | for (i = 0; i < 200; i++) { |
| 169 | RAND_bytes(&c, 1); |
| 170 | c = (c % BN_BITS) - BN_BITS2; |
Rich Salz | 2301d91d | 2016-08-08 22:12:28 -0400 | [diff] [blame] | 171 | BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 172 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 173 | RAND_bytes(&c, 1); |
| 174 | c = (c % BN_BITS) - BN_BITS2; |
Rich Salz | 2301d91d | 2016-08-08 22:12:28 -0400 | [diff] [blame] | 175 | BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 176 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 177 | RAND_bytes(&c, 1); |
| 178 | c = (c % BN_BITS) - BN_BITS2; |
Rich Salz | 2301d91d | 2016-08-08 22:12:28 -0400 | [diff] [blame] | 179 | BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 180 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 181 | BN_mod(a, a, m, ctx); |
| 182 | BN_mod(b, b, m, ctx); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 183 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL); |
| 185 | if (ret <= 0) { |
| 186 | printf("BN_mod_exp_mont() problems\n"); |
| 187 | ERR_print_errors(out); |
| 188 | EXIT(1); |
| 189 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 190 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 191 | ret = BN_mod_exp_recp(r_recp, a, b, m, ctx); |
| 192 | if (ret <= 0) { |
| 193 | printf("BN_mod_exp_recp() problems\n"); |
| 194 | ERR_print_errors(out); |
| 195 | EXIT(1); |
| 196 | } |
Ulf Möller | a79b03c | 1999-04-29 16:07:56 +0000 | [diff] [blame] | 197 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 198 | ret = BN_mod_exp_simple(r_simple, a, b, m, ctx); |
| 199 | if (ret <= 0) { |
| 200 | printf("BN_mod_exp_simple() problems\n"); |
| 201 | ERR_print_errors(out); |
| 202 | EXIT(1); |
| 203 | } |
Ulf Möller | a79b03c | 1999-04-29 16:07:56 +0000 | [diff] [blame] | 204 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 205 | ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL); |
| 206 | if (ret <= 0) { |
| 207 | printf("BN_mod_exp_mont_consttime() problems\n"); |
| 208 | ERR_print_errors(out); |
| 209 | EXIT(1); |
| 210 | } |
Bodo Möller | 46a6437 | 2005-05-16 01:43:31 +0000 | [diff] [blame] | 211 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 212 | if (BN_cmp(r_simple, r_mont) == 0 |
| 213 | && BN_cmp(r_simple, r_recp) == 0 |
| 214 | && BN_cmp(r_simple, r_mont_const) == 0) { |
| 215 | printf("."); |
| 216 | fflush(stdout); |
| 217 | } else { |
| 218 | if (BN_cmp(r_simple, r_mont) != 0) |
| 219 | printf("\nsimple and mont results differ\n"); |
| 220 | if (BN_cmp(r_simple, r_mont_const) != 0) |
| 221 | printf("\nsimple and mont const time results differ\n"); |
| 222 | if (BN_cmp(r_simple, r_recp) != 0) |
| 223 | printf("\nsimple and recp results differ\n"); |
Ulf Möller | a79b03c | 1999-04-29 16:07:56 +0000 | [diff] [blame] | 224 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 225 | printf("a (%3d) = ", BN_num_bits(a)); |
| 226 | BN_print(out, a); |
| 227 | printf("\nb (%3d) = ", BN_num_bits(b)); |
| 228 | BN_print(out, b); |
| 229 | printf("\nm (%3d) = ", BN_num_bits(m)); |
| 230 | BN_print(out, m); |
| 231 | printf("\nsimple ="); |
| 232 | BN_print(out, r_simple); |
| 233 | printf("\nrecp ="); |
| 234 | BN_print(out, r_recp); |
| 235 | printf("\nmont ="); |
| 236 | BN_print(out, r_mont); |
| 237 | printf("\nmont_ct ="); |
| 238 | BN_print(out, r_mont_const); |
| 239 | printf("\n"); |
| 240 | EXIT(1); |
| 241 | } |
| 242 | } |
| 243 | BN_free(r_mont); |
| 244 | BN_free(r_mont_const); |
| 245 | BN_free(r_recp); |
| 246 | BN_free(r_simple); |
| 247 | BN_free(a); |
| 248 | BN_free(b); |
| 249 | BN_free(m); |
| 250 | BN_CTX_free(ctx); |
Matt Caswell | 8793f01 | 2016-02-08 16:45:35 +0000 | [diff] [blame] | 251 | |
Matt Caswell | bfd53c3 | 2016-03-09 00:03:50 +0000 | [diff] [blame] | 252 | if (test_exp_mod_zero() != 0) |
| 253 | goto err; |
| 254 | |
Viktor Dukhovni | c2e2731 | 2016-01-10 14:42:10 -0500 | [diff] [blame] | 255 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
Dr. Stephen Henson | 541e956 | 2016-01-14 22:00:03 +0000 | [diff] [blame] | 256 | if (CRYPTO_mem_leaks(out) <= 0) |
| 257 | goto err; |
Rich Salz | 7644a9a | 2015-12-16 16:12:24 -0500 | [diff] [blame] | 258 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 259 | BIO_free(out); |
| 260 | printf("\n"); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 261 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 262 | printf("done\n"); |
Adam Langley | 2b0180c | 2013-04-23 12:13:51 -0400 | [diff] [blame] | 263 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 264 | EXIT(0); |
| 265 | err: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 266 | ERR_print_errors(out); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 267 | EXIT(1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 268 | } |