1*4724848cSchristos /*
2*4724848cSchristos * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include <stdio.h>
11*4724848cSchristos #include <stdlib.h>
12*4724848cSchristos #include <string.h>
13*4724848cSchristos
14*4724848cSchristos #include "internal/nelem.h"
15*4724848cSchristos
16*4724848cSchristos #include <openssl/bio.h>
17*4724848cSchristos #include <openssl/bn.h>
18*4724848cSchristos #include <openssl/rand.h>
19*4724848cSchristos #include <openssl/err.h>
20*4724848cSchristos
21*4724848cSchristos #include "testutil.h"
22*4724848cSchristos
23*4724848cSchristos #define NUM_BITS (BN_BITS2 * 4)
24*4724848cSchristos
25*4724848cSchristos #define BN_print_var(v) test_output_bignum(#v, v)
26*4724848cSchristos
27*4724848cSchristos /*
28*4724848cSchristos * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
29*4724848cSchristos * returns zero and prints debug output otherwise.
30*4724848cSchristos */
a_is_zero_mod_one(const char * method,const BIGNUM * r,const BIGNUM * a)31*4724848cSchristos static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
32*4724848cSchristos const BIGNUM *a)
33*4724848cSchristos {
34*4724848cSchristos if (!BN_is_zero(r)) {
35*4724848cSchristos TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method);
36*4724848cSchristos BN_print_var(a);
37*4724848cSchristos BN_print_var(r);
38*4724848cSchristos return 0;
39*4724848cSchristos }
40*4724848cSchristos return 1;
41*4724848cSchristos }
42*4724848cSchristos
43*4724848cSchristos /*
44*4724848cSchristos * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
45*4724848cSchristos */
test_mod_exp_zero(void)46*4724848cSchristos static int test_mod_exp_zero(void)
47*4724848cSchristos {
48*4724848cSchristos BIGNUM *a = NULL, *p = NULL, *m = NULL;
49*4724848cSchristos BIGNUM *r = NULL;
50*4724848cSchristos BN_ULONG one_word = 1;
51*4724848cSchristos BN_CTX *ctx = BN_CTX_new();
52*4724848cSchristos int ret = 0, failed = 0;
53*4724848cSchristos BN_MONT_CTX *mont = NULL;
54*4724848cSchristos
55*4724848cSchristos if (!TEST_ptr(m = BN_new())
56*4724848cSchristos || !TEST_ptr(a = BN_new())
57*4724848cSchristos || !TEST_ptr(p = BN_new())
58*4724848cSchristos || !TEST_ptr(r = BN_new()))
59*4724848cSchristos goto err;
60*4724848cSchristos
61*4724848cSchristos BN_one(m);
62*4724848cSchristos BN_one(a);
63*4724848cSchristos BN_zero(p);
64*4724848cSchristos
65*4724848cSchristos if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
66*4724848cSchristos goto err;
67*4724848cSchristos
68*4724848cSchristos if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
69*4724848cSchristos goto err;
70*4724848cSchristos
71*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
72*4724848cSchristos failed = 1;
73*4724848cSchristos
74*4724848cSchristos if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
75*4724848cSchristos goto err;
76*4724848cSchristos
77*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
78*4724848cSchristos failed = 1;
79*4724848cSchristos
80*4724848cSchristos if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
81*4724848cSchristos goto err;
82*4724848cSchristos
83*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
84*4724848cSchristos failed = 1;
85*4724848cSchristos
86*4724848cSchristos if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
87*4724848cSchristos goto err;
88*4724848cSchristos
89*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
90*4724848cSchristos failed = 1;
91*4724848cSchristos
92*4724848cSchristos if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
93*4724848cSchristos goto err;
94*4724848cSchristos
95*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
96*4724848cSchristos failed = 1;
97*4724848cSchristos
98*4724848cSchristos if (!TEST_ptr(mont = BN_MONT_CTX_new()))
99*4724848cSchristos goto err;
100*4724848cSchristos
101*4724848cSchristos ERR_set_mark();
102*4724848cSchristos /* mont is not set but passed in */
103*4724848cSchristos if (!TEST_false(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont)))
104*4724848cSchristos goto err;
105*4724848cSchristos if (!TEST_false(BN_mod_exp_mont(r, p, a, m, ctx, mont)))
106*4724848cSchristos goto err;
107*4724848cSchristos ERR_pop_to_mark();
108*4724848cSchristos
109*4724848cSchristos if (!TEST_true(BN_MONT_CTX_set(mont, m, ctx)))
110*4724848cSchristos goto err;
111*4724848cSchristos
112*4724848cSchristos /* we compute 0 ** a mod 1 here, to execute code that uses mont */
113*4724848cSchristos if (!TEST_true(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont)))
114*4724848cSchristos goto err;
115*4724848cSchristos
116*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
117*4724848cSchristos failed = 1;
118*4724848cSchristos
119*4724848cSchristos if (!TEST_true(BN_mod_exp_mont(r, p, a, m, ctx, mont)))
120*4724848cSchristos goto err;
121*4724848cSchristos
122*4724848cSchristos if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
123*4724848cSchristos failed = 1;
124*4724848cSchristos
125*4724848cSchristos /*
126*4724848cSchristos * A different codepath exists for single word multiplication
127*4724848cSchristos * in non-constant-time only.
128*4724848cSchristos */
129*4724848cSchristos if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
130*4724848cSchristos goto err;
131*4724848cSchristos
132*4724848cSchristos if (!TEST_BN_eq_zero(r)) {
133*4724848cSchristos TEST_error("BN_mod_exp_mont_word failed: "
134*4724848cSchristos "1 ** 0 mod 1 = r (should be 0)");
135*4724848cSchristos BN_print_var(r);
136*4724848cSchristos goto err;
137*4724848cSchristos }
138*4724848cSchristos
139*4724848cSchristos ret = !failed;
140*4724848cSchristos err:
141*4724848cSchristos BN_free(r);
142*4724848cSchristos BN_free(a);
143*4724848cSchristos BN_free(p);
144*4724848cSchristos BN_free(m);
145*4724848cSchristos BN_MONT_CTX_free(mont);
146*4724848cSchristos BN_CTX_free(ctx);
147*4724848cSchristos
148*4724848cSchristos return ret;
149*4724848cSchristos }
150*4724848cSchristos
test_mod_exp(int round)151*4724848cSchristos static int test_mod_exp(int round)
152*4724848cSchristos {
153*4724848cSchristos BN_CTX *ctx;
154*4724848cSchristos unsigned char c;
155*4724848cSchristos int ret = 0;
156*4724848cSchristos BIGNUM *r_mont = NULL;
157*4724848cSchristos BIGNUM *r_mont_const = NULL;
158*4724848cSchristos BIGNUM *r_recp = NULL;
159*4724848cSchristos BIGNUM *r_simple = NULL;
160*4724848cSchristos BIGNUM *a = NULL;
161*4724848cSchristos BIGNUM *b = NULL;
162*4724848cSchristos BIGNUM *m = NULL;
163*4724848cSchristos
164*4724848cSchristos if (!TEST_ptr(ctx = BN_CTX_new()))
165*4724848cSchristos goto err;
166*4724848cSchristos
167*4724848cSchristos if (!TEST_ptr(r_mont = BN_new())
168*4724848cSchristos || !TEST_ptr(r_mont_const = BN_new())
169*4724848cSchristos || !TEST_ptr(r_recp = BN_new())
170*4724848cSchristos || !TEST_ptr(r_simple = BN_new())
171*4724848cSchristos || !TEST_ptr(a = BN_new())
172*4724848cSchristos || !TEST_ptr(b = BN_new())
173*4724848cSchristos || !TEST_ptr(m = BN_new()))
174*4724848cSchristos goto err;
175*4724848cSchristos
176*4724848cSchristos RAND_bytes(&c, 1);
177*4724848cSchristos c = (c % BN_BITS) - BN_BITS2;
178*4724848cSchristos BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
179*4724848cSchristos
180*4724848cSchristos RAND_bytes(&c, 1);
181*4724848cSchristos c = (c % BN_BITS) - BN_BITS2;
182*4724848cSchristos BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
183*4724848cSchristos
184*4724848cSchristos RAND_bytes(&c, 1);
185*4724848cSchristos c = (c % BN_BITS) - BN_BITS2;
186*4724848cSchristos BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
187*4724848cSchristos
188*4724848cSchristos if (!TEST_true(BN_mod(a, a, m, ctx))
189*4724848cSchristos || !TEST_true(BN_mod(b, b, m, ctx))
190*4724848cSchristos || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
191*4724848cSchristos || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
192*4724848cSchristos || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
193*4724848cSchristos || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
194*4724848cSchristos goto err;
195*4724848cSchristos
196*4724848cSchristos if (!TEST_BN_eq(r_simple, r_mont)
197*4724848cSchristos || !TEST_BN_eq(r_simple, r_recp)
198*4724848cSchristos || !TEST_BN_eq(r_simple, r_mont_const)) {
199*4724848cSchristos if (BN_cmp(r_simple, r_mont) != 0)
200*4724848cSchristos TEST_info("simple and mont results differ");
201*4724848cSchristos if (BN_cmp(r_simple, r_mont_const) != 0)
202*4724848cSchristos TEST_info("simple and mont const time results differ");
203*4724848cSchristos if (BN_cmp(r_simple, r_recp) != 0)
204*4724848cSchristos TEST_info("simple and recp results differ");
205*4724848cSchristos
206*4724848cSchristos BN_print_var(a);
207*4724848cSchristos BN_print_var(b);
208*4724848cSchristos BN_print_var(m);
209*4724848cSchristos BN_print_var(r_simple);
210*4724848cSchristos BN_print_var(r_recp);
211*4724848cSchristos BN_print_var(r_mont);
212*4724848cSchristos BN_print_var(r_mont_const);
213*4724848cSchristos goto err;
214*4724848cSchristos }
215*4724848cSchristos
216*4724848cSchristos ret = 1;
217*4724848cSchristos err:
218*4724848cSchristos BN_free(r_mont);
219*4724848cSchristos BN_free(r_mont_const);
220*4724848cSchristos BN_free(r_recp);
221*4724848cSchristos BN_free(r_simple);
222*4724848cSchristos BN_free(a);
223*4724848cSchristos BN_free(b);
224*4724848cSchristos BN_free(m);
225*4724848cSchristos BN_CTX_free(ctx);
226*4724848cSchristos
227*4724848cSchristos return ret;
228*4724848cSchristos }
229*4724848cSchristos
setup_tests(void)230*4724848cSchristos int setup_tests(void)
231*4724848cSchristos {
232*4724848cSchristos ADD_TEST(test_mod_exp_zero);
233*4724848cSchristos ADD_ALL_TESTS(test_mod_exp, 200);
234*4724848cSchristos return 1;
235*4724848cSchristos }
236