summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/exp
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/exp')
-rw-r--r--src/regress/lib/libcrypto/exp/Makefile9
-rw-r--r--src/regress/lib/libcrypto/exp/exptest.c354
2 files changed, 0 insertions, 363 deletions
diff --git a/src/regress/lib/libcrypto/exp/Makefile b/src/regress/lib/libcrypto/exp/Makefile
deleted file mode 100644
index 890b38e9fe..0000000000
--- a/src/regress/lib/libcrypto/exp/Makefile
+++ /dev/null
@@ -1,9 +0,0 @@
1# $OpenBSD: Makefile,v 1.4 2017/01/21 09:38:58 beck Exp $
2
3PROG= exptest
4LDADD= ${CRYPTO_INT}
5DPADD= ${LIBCRYPTO}
6WARNINGS= Yes
7CFLAGS+= -Werror
8
9.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/exp/exptest.c b/src/regress/lib/libcrypto/exp/exptest.c
deleted file mode 100644
index e7f5848528..0000000000
--- a/src/regress/lib/libcrypto/exp/exptest.c
+++ /dev/null
@@ -1,354 +0,0 @@
1/* $OpenBSD: exptest.c,v 1.7 2018/11/08 22:20:25 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#include <string.h>
62
63#include <openssl/bio.h>
64#include <openssl/bn.h>
65#include <openssl/err.h>
66
67int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
68 const BIGNUM *m, BN_CTX *ctx);
69int BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
70 const BIGNUM *m, BN_CTX *ctx);
71int 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);
73int 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 */
82static 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 */
100static 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
205int main(int argc, char *argv[])
206{
207 BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple;
208 BIGNUM *r_mont_ct, *r_mont_nonct, *a, *b, *m;
209 BN_CTX *ctx;
210 BIO *out = NULL;
211 unsigned char c;
212 int i, ret;
213
214 ERR_load_BN_strings();
215
216 if ((ctx = BN_CTX_new()) == NULL)
217 goto err;
218 if ((r_mont = BN_new()) == NULL)
219 goto err;
220 if ((r_mont_const = BN_new()) == NULL)
221 goto err;
222 if ((r_mont_ct = BN_new()) == NULL)
223 goto err;
224 if ((r_mont_nonct = BN_new()) == NULL)
225 goto err;
226 if ((r_recp = BN_new()) == NULL)
227 goto err;
228 if ((r_simple = BN_new()) == NULL)
229 goto err;
230 if ((a = BN_new()) == NULL)
231 goto err;
232 if ((b = BN_new()) == NULL)
233 goto err;
234 if ((m = BN_new()) == NULL)
235 goto err;
236
237 if ((out = BIO_new(BIO_s_file())) == NULL)
238 exit(1);
239 BIO_set_fp(out, stdout, BIO_NOCLOSE);
240
241 for (i = 0; i < 200; i++) {
242 arc4random_buf(&c, 1);
243 c = (c % BN_BITS) - BN_BITS2;
244 BN_rand(a, NUM_BITS + c, 0, 0);
245
246 arc4random_buf(&c, 1);
247 c = (c % BN_BITS) - BN_BITS2;
248 BN_rand(b, NUM_BITS + c, 0, 0);
249
250 arc4random_buf(&c, 1);
251 c = (c % BN_BITS) - BN_BITS2;
252 BN_rand(m, NUM_BITS + c, 0, 1);
253
254 BN_mod(a, a, m, ctx);
255 BN_mod(b, b, m, ctx);
256
257 ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
258 if (ret <= 0) {
259 printf("BN_mod_exp_mont() problems\n");
260 goto err;
261 }
262
263 ret = BN_mod_exp_mont_ct(r_mont_ct, a, b, m, ctx, NULL);
264 if (ret <= 0) {
265 printf("BN_mod_exp_mont_ct() problems\n");
266 goto err;
267 }
268
269 ret = BN_mod_exp_mont_nonct(r_mont_nonct, a, b, m, ctx, NULL);
270 if (ret <= 0) {
271 printf("BN_mod_exp_mont_nonct() problems\n");
272 goto err;
273 }
274
275 ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
276 if (ret <= 0) {
277 printf("BN_mod_exp_recp() problems\n");
278 goto err;
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 goto err;
285 }
286
287 ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
288 if (ret <= 0) {
289 printf("BN_mod_exp_mont_consttime() problems\n");
290 goto err;
291 }
292
293 if (BN_cmp(r_simple, r_mont) == 0 &&
294 BN_cmp(r_simple, r_recp) == 0 &&
295 BN_cmp(r_simple, r_mont_const) == 0) {
296 printf(".");
297 fflush(stdout);
298 } else {
299 if (BN_cmp(r_simple, r_mont) != 0)
300 printf("\nsimple and mont results differ\n");
301 if (BN_cmp(r_simple, r_mont_const) != 0)
302 printf("\nsimple and mont const time results differ\n");
303 if (BN_cmp(r_simple, r_recp) != 0)
304 printf("\nsimple and recp results differ\n");
305 if (BN_cmp(r_mont, r_mont_ct) != 0)
306 printf("\nmont_ct and mont results differ\n");
307 if (BN_cmp(r_mont_ct, r_mont_nonct) != 0)
308 printf("\nmont_ct and mont_nonct results differ\n");
309
310 printf("a (%3d) = ", BN_num_bits(a));
311 BN_print(out, a);
312 printf("\nb (%3d) = ", BN_num_bits(b));
313 BN_print(out, b);
314 printf("\nm (%3d) = ", BN_num_bits(m));
315 BN_print(out, m);
316 printf("\nsimple =");
317 BN_print(out, r_simple);
318 printf("\nrecp =");
319 BN_print(out, r_recp);
320 printf("\nmont =");
321 BN_print(out, r_mont);
322 printf("\nmont_ct =");
323 BN_print(out, r_mont_const);
324 printf("\n");
325 exit(1);
326 }
327 }
328 BN_free(r_mont);
329 BN_free(r_mont_const);
330 BN_free(r_mont_ct);
331 BN_free(r_mont_nonct);
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
350 err:
351 ERR_load_crypto_strings();
352 ERR_print_errors(out);
353 return (1);
354}