blob: e58e0c29aa819df5913dc08b3dc91918aa34b9a2 [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz440e5d82016-05-17 14:20:24 -04004 * 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. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
Richard Levitte55f78ba2002-11-28 18:54:30 +000013
14#include "../e_os.h"
15
Bodo Möllerec577821999-04-23 22:13:45 +000016#include <openssl/bio.h>
17#include <openssl/bn.h>
18#include <openssl/rand.h>
19#include <openssl/err.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000020
Rich Salz94af0cd2016-01-28 10:13:21 -050021#define NUM_BITS (BN_BITS2 * 4)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000022
Matt Caswell0f113f32015-01-22 03:40:55 +000023static const char rnd_seed[] =
24 "string to make the random number generator think it has entropy";
Bodo Möller0c50e022000-01-14 17:55:37 +000025
Matt Caswell0f113f32015-01-22 03:40:55 +000026/*
Emilia Kasperd9110972015-12-14 16:38:15 +010027 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
28 * returns zero and prints debug output otherwise.
29 */
30static 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 Caswell0f113f32015-01-22 03:40:55 +000046 * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
47 */
48static int test_exp_mod_zero()
49{
50 BIGNUM *a = NULL, *p = NULL, *m = NULL;
51 BIGNUM *r = NULL;
Emilia Kasperd9110972015-12-14 16:38:15 +010052 BN_ULONG one_word = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +000053 BN_CTX *ctx = BN_CTX_new();
Emilia Kasperd9110972015-12-14 16:38:15 +010054 int ret = 1, failed = 0;
Adam Langley2b0180c2013-04-23 12:13:51 -040055
Matt Caswell0f113f32015-01-22 03:40:55 +000056 m = BN_new();
57 if (!m)
58 goto err;
59 BN_one(m);
Adam Langley2b0180c2013-04-23 12:13:51 -040060
Matt Caswell0f113f32015-01-22 03:40:55 +000061 a = BN_new();
62 if (!a)
63 goto err;
64 BN_one(a);
Adam Langley2b0180c2013-04-23 12:13:51 -040065
Matt Caswell0f113f32015-01-22 03:40:55 +000066 p = BN_new();
67 if (!p)
68 goto err;
69 BN_zero(p);
Adam Langley2b0180c2013-04-23 12:13:51 -040070
Matt Caswell0f113f32015-01-22 03:40:55 +000071 r = BN_new();
72 if (!r)
73 goto err;
Adam Langley2b0180c2013-04-23 12:13:51 -040074
Rich Salz2301d91d2016-08-08 22:12:28 -040075 if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
Emilia Kasperd9110972015-12-14 16:38:15 +010076 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 Caswell0f113f32015-01-22 03:40:55 +0000104 }
Adam Langley2b0180c2013-04-23 12:13:51 -0400105
Emilia Kasperd9110972015-12-14 16:38:15 +0100106 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 Caswell0f113f32015-01-22 03:40:55 +0000127 err:
128 BN_free(r);
129 BN_free(a);
130 BN_free(p);
131 BN_free(m);
Emilia Kasperd9110972015-12-14 16:38:15 +0100132 BN_CTX_free(ctx);
Adam Langley2b0180c2013-04-23 12:13:51 -0400133
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 return ret;
Adam Langley2b0180c2013-04-23 12:13:51 -0400135}
136
Ulf Möller6b691a51999-04-19 21:31:43 +0000137int main(int argc, char *argv[])
Matt Caswell0f113f32015-01-22 03:40:55 +0000138{
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. Engelschalld02b48c1998-12-21 10:52:47 +0000144
Matt Caswell0f113f32015-01-22 03:40:55 +0000145 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öller0c50e022000-01-14 17:55:37 +0000148
Matt Caswell0f113f32015-01-22 03:40:55 +0000149 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. Engelschalld02b48c1998-12-21 10:52:47 +0000161
Matt Caswell0f113f32015-01-22 03:40:55 +0000162 out = BIO_new(BIO_s_file());
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000163
Matt Caswell0f113f32015-01-22 03:40:55 +0000164 if (out == NULL)
165 EXIT(1);
Richard Levitte0f81f5f2015-09-04 14:07:57 +0200166 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000167
Matt Caswell0f113f32015-01-22 03:40:55 +0000168 for (i = 0; i < 200; i++) {
169 RAND_bytes(&c, 1);
170 c = (c % BN_BITS) - BN_BITS2;
Rich Salz2301d91d2016-08-08 22:12:28 -0400171 BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000172
Matt Caswell0f113f32015-01-22 03:40:55 +0000173 RAND_bytes(&c, 1);
174 c = (c % BN_BITS) - BN_BITS2;
Rich Salz2301d91d2016-08-08 22:12:28 -0400175 BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000176
Matt Caswell0f113f32015-01-22 03:40:55 +0000177 RAND_bytes(&c, 1);
178 c = (c % BN_BITS) - BN_BITS2;
Rich Salz2301d91d2016-08-08 22:12:28 -0400179 BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000180
Matt Caswell0f113f32015-01-22 03:40:55 +0000181 BN_mod(a, a, m, ctx);
182 BN_mod(b, b, m, ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000183
Matt Caswell0f113f32015-01-22 03:40:55 +0000184 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. Engelschalld02b48c1998-12-21 10:52:47 +0000190
Matt Caswell0f113f32015-01-22 03:40:55 +0000191 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öllera79b03c1999-04-29 16:07:56 +0000197
Matt Caswell0f113f32015-01-22 03:40:55 +0000198 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öllera79b03c1999-04-29 16:07:56 +0000204
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 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öller46a64372005-05-16 01:43:31 +0000211
Matt Caswell0f113f32015-01-22 03:40:55 +0000212 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öllera79b03c1999-04-29 16:07:56 +0000224
Matt Caswell0f113f32015-01-22 03:40:55 +0000225 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 Caswell8793f012016-02-08 16:45:35 +0000251
Matt Caswellbfd53c32016-03-09 00:03:50 +0000252 if (test_exp_mod_zero() != 0)
253 goto err;
254
Viktor Dukhovnic2e27312016-01-10 14:42:10 -0500255#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Dr. Stephen Henson541e9562016-01-14 22:00:03 +0000256 if (CRYPTO_mem_leaks(out) <= 0)
257 goto err;
Rich Salz7644a9a2015-12-16 16:12:24 -0500258#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000259 BIO_free(out);
260 printf("\n");
Adam Langley2b0180c2013-04-23 12:13:51 -0400261
Matt Caswell0f113f32015-01-22 03:40:55 +0000262 printf("done\n");
Adam Langley2b0180c2013-04-23 12:13:51 -0400263
Matt Caswell0f113f32015-01-22 03:40:55 +0000264 EXIT(0);
265 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000266 ERR_print_errors(out);
Matt Caswell0f113f32015-01-22 03:40:55 +0000267 EXIT(1);
Matt Caswell0f113f32015-01-22 03:40:55 +0000268}