Update test counting in checkhandshake.pm
[openssl.git] / test / exptest.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "../e_os.h"
15
16 #include <openssl/bio.h>
17 #include <openssl/bn.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
20
21 #define NUM_BITS        (BN_BITS2 * 4)
22
23 static const char rnd_seed[] =
24     "string to make the random number generator think it has entropy";
25
26 /*
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 /*
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;
52     BN_ULONG one_word = 1;
53     BN_CTX *ctx = BN_CTX_new();
54     int ret = 1, failed = 0;
55
56     m = BN_new();
57     if (!m)
58         goto err;
59     BN_one(m);
60
61     a = BN_new();
62     if (!a)
63         goto err;
64     BN_one(a);
65
66     p = BN_new();
67     if (!p)
68         goto err;
69     BN_zero(p);
70
71     r = BN_new();
72     if (!r)
73         goto err;
74
75     if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
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;
104     }
105
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
127  err:
128     BN_free(r);
129     BN_free(a);
130     BN_free(p);
131     BN_free(m);
132     BN_CTX_free(ctx);
133
134     return ret;
135 }
136
137 int main(int argc, char *argv[])
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;
144
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) */
148
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;
161
162     out = BIO_new(BIO_s_file());
163
164     if (out == NULL)
165         EXIT(1);
166     BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
167
168     for (i = 0; i < 200; i++) {
169         RAND_bytes(&c, 1);
170         c = (c % BN_BITS) - BN_BITS2;
171         BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
172
173         RAND_bytes(&c, 1);
174         c = (c % BN_BITS) - BN_BITS2;
175         BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
176
177         RAND_bytes(&c, 1);
178         c = (c % BN_BITS) - BN_BITS2;
179         BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
180
181         BN_mod(a, a, m, ctx);
182         BN_mod(b, b, m, ctx);
183
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         }
190
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         }
197
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         }
204
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         }
211
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");
224
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);
251
252     if (test_exp_mod_zero() != 0)
253         goto err;
254
255 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
256     if (CRYPTO_mem_leaks(out) <= 0)
257         goto err;
258 #endif
259     BIO_free(out);
260     printf("\n");
261
262     printf("done\n");
263
264     EXIT(0);
265  err:
266     ERR_print_errors(out);
267     EXIT(1);
268 }