Test an overlong ChaCha20-Poly1305 nonce
[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     /*
146      * Seed or BN_rand may fail, and we don't even check its return
147      * value (which we should)
148      */
149     RAND_seed(rnd_seed, sizeof(rnd_seed));
150
151     ctx = BN_CTX_new();
152     if (ctx == NULL)
153         EXIT(1);
154     r_mont = BN_new();
155     r_mont_const = BN_new();
156     r_recp = BN_new();
157     r_simple = BN_new();
158     a = BN_new();
159     b = BN_new();
160     m = BN_new();
161     if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
162         goto err;
163
164     out = BIO_new(BIO_s_file());
165
166     if (out == NULL)
167         EXIT(1);
168     BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
169
170     for (i = 0; i < 200; i++) {
171         RAND_bytes(&c, 1);
172         c = (c % BN_BITS) - BN_BITS2;
173         BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
174
175         RAND_bytes(&c, 1);
176         c = (c % BN_BITS) - BN_BITS2;
177         BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
178
179         RAND_bytes(&c, 1);
180         c = (c % BN_BITS) - BN_BITS2;
181         BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
182
183         BN_mod(a, a, m, ctx);
184         BN_mod(b, b, m, ctx);
185
186         ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
187         if (ret <= 0) {
188             printf("BN_mod_exp_mont() problems\n");
189             ERR_print_errors(out);
190             EXIT(1);
191         }
192
193         ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
194         if (ret <= 0) {
195             printf("BN_mod_exp_recp() problems\n");
196             ERR_print_errors(out);
197             EXIT(1);
198         }
199
200         ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
201         if (ret <= 0) {
202             printf("BN_mod_exp_simple() problems\n");
203             ERR_print_errors(out);
204             EXIT(1);
205         }
206
207         ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
208         if (ret <= 0) {
209             printf("BN_mod_exp_mont_consttime() problems\n");
210             ERR_print_errors(out);
211             EXIT(1);
212         }
213
214         if (BN_cmp(r_simple, r_mont) == 0
215             && BN_cmp(r_simple, r_recp) == 0
216             && BN_cmp(r_simple, r_mont_const) == 0) {
217             printf(".");
218             fflush(stdout);
219         } else {
220             if (BN_cmp(r_simple, r_mont) != 0)
221                 printf("\nsimple and mont results differ\n");
222             if (BN_cmp(r_simple, r_mont_const) != 0)
223                 printf("\nsimple and mont const time results differ\n");
224             if (BN_cmp(r_simple, r_recp) != 0)
225                 printf("\nsimple and recp results differ\n");
226
227             printf("a (%3d) = ", BN_num_bits(a));
228             BN_print(out, a);
229             printf("\nb (%3d) = ", BN_num_bits(b));
230             BN_print(out, b);
231             printf("\nm (%3d) = ", BN_num_bits(m));
232             BN_print(out, m);
233             printf("\nsimple   =");
234             BN_print(out, r_simple);
235             printf("\nrecp     =");
236             BN_print(out, r_recp);
237             printf("\nmont     =");
238             BN_print(out, r_mont);
239             printf("\nmont_ct  =");
240             BN_print(out, r_mont_const);
241             printf("\n");
242             EXIT(1);
243         }
244     }
245     BN_free(r_mont);
246     BN_free(r_mont_const);
247     BN_free(r_recp);
248     BN_free(r_simple);
249     BN_free(a);
250     BN_free(b);
251     BN_free(m);
252     BN_CTX_free(ctx);
253
254     if (test_exp_mod_zero() != 0)
255         goto err;
256
257 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
258     if (CRYPTO_mem_leaks(out) <= 0)
259         goto err;
260 #endif
261     BIO_free(out);
262     printf("\n");
263
264     printf("done\n");
265
266     EXIT(0);
267  err:
268     ERR_print_errors(out);
269     EXIT(1);
270 }