test/exptest.c: stop marking progress with a period
[openssl.git] / test / exptest.c
1 /*
2  * Copyright 1995-2017 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 #include "testutil.h"
22
23 #define NUM_BITS        (BN_BITS2 * 4)
24
25 #define BN_print_var(v) bn_print_var(#v, v)
26
27 static void bn_print_var(const char *var, const BIGNUM *bn)
28 {
29     fprintf(stderr, "%s (%3d) = ", var, BN_num_bits(bn));
30     BN_print_fp(stderr, bn);
31     fprintf(stderr, "\n");
32 }
33
34 /*
35  * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
36  * returns zero and prints debug output otherwise.
37  */
38 static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
39                              const BIGNUM *a)
40 {
41     if (!BN_is_zero(r)) {
42         fprintf(stderr, "%s failed:\n", method);
43         fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
44         BN_print_var(a);
45         BN_print_var(r);
46         return 0;
47     }
48     return 1;
49 }
50
51 /*
52  * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
53  */
54 static int test_mod_exp_zero()
55 {
56     BIGNUM *a = NULL, *p = NULL, *m = NULL;
57     BIGNUM *r = NULL;
58     BN_ULONG one_word = 1;
59     BN_CTX *ctx = BN_CTX_new();
60     int ret = 1, failed = 0;
61
62     if (!TEST_ptr(m = BN_new())
63         || !TEST_ptr(a = BN_new())
64         || !TEST_ptr(p = BN_new())
65         || !TEST_ptr(r = BN_new()))
66         goto err;
67
68     BN_one(m);
69     BN_one(a);
70     BN_zero(p);
71
72     if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
73         goto err;
74
75     if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
76         goto err;
77
78     if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
79         failed = 1;
80
81     if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
82         goto err;
83
84     if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
85         failed = 1;
86
87     if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
88         goto err;
89
90     if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
91         failed = 1;
92
93     if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
94         goto err;
95
96     if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
97         failed = 1;
98
99     if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
100         goto err;
101
102     if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
103         failed = 1;
104
105     /*
106      * A different codepath exists for single word multiplication
107      * in non-constant-time only.
108      */
109     if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
110         goto err;
111
112     if (!TEST_true(BN_is_zero(r))) {
113         fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
114         fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
115         BN_print_var(r);
116         goto err;
117     }
118
119     ret = !failed;
120  err:
121     BN_free(r);
122     BN_free(a);
123     BN_free(p);
124     BN_free(m);
125     BN_CTX_free(ctx);
126
127     return ret;
128 }
129
130 static int test_mod_exp(int round)
131 {
132     BN_CTX *ctx;
133     unsigned char c;
134     int ret = 0;
135     BIGNUM *r_mont = NULL;
136     BIGNUM *r_mont_const = NULL;
137     BIGNUM *r_recp = NULL;
138     BIGNUM *r_simple = NULL;
139     BIGNUM *a = NULL;
140     BIGNUM *b = NULL;
141     BIGNUM *m = NULL;
142
143     if (!TEST_ptr(ctx = BN_CTX_new()))
144         goto err;
145
146     if (!TEST_ptr(r_mont = BN_new())
147         || !TEST_ptr(r_mont_const = BN_new())
148         || !TEST_ptr(r_recp = BN_new())
149         || !TEST_ptr(r_simple = BN_new())
150         || !TEST_ptr(a = BN_new())
151         || !TEST_ptr(b = BN_new())
152         || !TEST_ptr(m = BN_new()))
153         goto err;
154
155     RAND_bytes(&c, 1);
156     c = (c % BN_BITS) - BN_BITS2;
157     BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
158
159     RAND_bytes(&c, 1);
160     c = (c % BN_BITS) - BN_BITS2;
161     BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
162
163     RAND_bytes(&c, 1);
164     c = (c % BN_BITS) - BN_BITS2;
165     BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
166
167     BN_mod(a, a, m, ctx);
168     BN_mod(b, b, m, ctx);
169
170     if (!TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
171         || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
172         || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
173         || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
174         goto err;
175
176     if (!TEST_int_eq(BN_cmp(r_simple, r_mont), 0)
177         || !TEST_int_eq(BN_cmp(r_simple, r_recp), 0)
178         || !TEST_int_eq(BN_cmp(r_simple, r_mont_const), 0)) {
179         if (BN_cmp(r_simple, r_mont) != 0)
180             fprintf(stderr, "simple and mont results differ\n");
181         if (BN_cmp(r_simple, r_mont_const) != 0)
182             fprintf(stderr, "simple and mont const time results differ\n");
183         if (BN_cmp(r_simple, r_recp) != 0)
184             fprintf(stderr, "simple and recp results differ\n");
185
186         BN_print_var(a);
187         BN_print_var(b);
188         BN_print_var(m);
189         BN_print_var(r_simple);
190         BN_print_var(r_recp);
191         BN_print_var(r_mont);
192         BN_print_var(r_mont_const);
193         goto err;
194     }
195
196     ret = 1;
197  err:
198     BN_free(r_mont);
199     BN_free(r_mont_const);
200     BN_free(r_recp);
201     BN_free(r_simple);
202     BN_free(a);
203     BN_free(b);
204     BN_free(m);
205     BN_CTX_free(ctx);
206
207     return ret;
208 }
209
210 void register_tests(void)
211 {
212     ADD_TEST(test_mod_exp_zero);
213     ADD_ALL_TESTS(test_mod_exp, 200);
214 }