]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/exptest.c
2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
14 #include "internal/nelem.h"
16 #include <openssl/bio.h>
17 #include <openssl/bn.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
23 #define NUM_BITS (BN_BITS2 * 4)
25 #define BN_print_var(v) test_output_bignum(#v, v)
28 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
29 * returns zero and prints debug output otherwise.
31 static int a_is_zero_mod_one(const char *method
, const BIGNUM
*r
,
35 TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method
);
44 * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
46 static int test_mod_exp_zero(void)
48 BIGNUM
*a
= NULL
, *p
= NULL
, *m
= NULL
;
50 BN_ULONG one_word
= 1;
51 BN_CTX
*ctx
= BN_CTX_new();
52 int ret
= 0, failed
= 0;
53 BN_MONT_CTX
*mont
= NULL
;
55 if (!TEST_ptr(m
= BN_new())
56 || !TEST_ptr(a
= BN_new())
57 || !TEST_ptr(p
= BN_new())
58 || !TEST_ptr(r
= BN_new()))
65 if (!TEST_true(BN_rand(a
, 1024, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
)))
68 if (!TEST_true(BN_mod_exp(r
, a
, p
, m
, ctx
)))
71 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r
, a
)))
74 if (!TEST_true(BN_mod_exp_recp(r
, a
, p
, m
, ctx
)))
77 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r
, a
)))
80 if (!TEST_true(BN_mod_exp_simple(r
, a
, p
, m
, ctx
)))
83 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r
, a
)))
86 if (!TEST_true(BN_mod_exp_mont(r
, a
, p
, m
, ctx
, NULL
)))
89 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r
, a
)))
92 if (!TEST_true(BN_mod_exp_mont_consttime(r
, a
, p
, m
, ctx
, NULL
)))
95 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r
, a
)))
98 if (!TEST_ptr(mont
= BN_MONT_CTX_new()))
102 /* mont is not set but passed in */
103 if (!TEST_false(BN_mod_exp_mont_consttime(r
, p
, a
, m
, ctx
, mont
)))
105 if (!TEST_false(BN_mod_exp_mont(r
, p
, a
, m
, ctx
, mont
)))
109 if (!TEST_true(BN_MONT_CTX_set(mont
, m
, ctx
)))
112 /* we compute 0 ** a mod 1 here, to execute code that uses mont */
113 if (!TEST_true(BN_mod_exp_mont_consttime(r
, p
, a
, m
, ctx
, mont
)))
116 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r
, a
)))
119 if (!TEST_true(BN_mod_exp_mont(r
, p
, a
, m
, ctx
, mont
)))
122 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r
, a
)))
126 * A different codepath exists for single word multiplication
127 * in non-constant-time only.
129 if (!TEST_true(BN_mod_exp_mont_word(r
, one_word
, p
, m
, ctx
, NULL
)))
132 if (!TEST_BN_eq_zero(r
)) {
133 TEST_error("BN_mod_exp_mont_word failed: "
134 "1 ** 0 mod 1 = r (should be 0)");
145 BN_MONT_CTX_free(mont
);
151 static int test_mod_exp(int round
)
156 BIGNUM
*r_mont
= NULL
;
157 BIGNUM
*r_mont_const
= NULL
;
158 BIGNUM
*r_recp
= NULL
;
159 BIGNUM
*r_simple
= NULL
;
164 if (!TEST_ptr(ctx
= BN_CTX_new()))
167 if (!TEST_ptr(r_mont
= BN_new())
168 || !TEST_ptr(r_mont_const
= BN_new())
169 || !TEST_ptr(r_recp
= BN_new())
170 || !TEST_ptr(r_simple
= BN_new())
171 || !TEST_ptr(a
= BN_new())
172 || !TEST_ptr(b
= BN_new())
173 || !TEST_ptr(m
= BN_new()))
176 if (!TEST_int_gt(RAND_bytes(&c
, 1), 0))
178 c
= (c
% BN_BITS
) - BN_BITS2
;
179 if (!TEST_true(BN_rand(a
, NUM_BITS
+ c
, BN_RAND_TOP_ONE
,
180 BN_RAND_BOTTOM_ANY
)))
183 if (!TEST_int_gt(RAND_bytes(&c
, 1), 0))
185 c
= (c
% BN_BITS
) - BN_BITS2
;
186 if (!TEST_true(BN_rand(b
, NUM_BITS
+ c
, BN_RAND_TOP_ONE
,
187 BN_RAND_BOTTOM_ANY
)))
190 if (!TEST_int_gt(RAND_bytes(&c
, 1), 0))
192 c
= (c
% BN_BITS
) - BN_BITS2
;
193 if (!TEST_true(BN_rand(m
, NUM_BITS
+ c
, BN_RAND_TOP_ONE
,
194 BN_RAND_BOTTOM_ODD
)))
197 if (!TEST_true(BN_mod(a
, a
, m
, ctx
))
198 || !TEST_true(BN_mod(b
, b
, m
, ctx
))
199 || !TEST_true(BN_mod_exp_mont(r_mont
, a
, b
, m
, ctx
, NULL
))
200 || !TEST_true(BN_mod_exp_recp(r_recp
, a
, b
, m
, ctx
))
201 || !TEST_true(BN_mod_exp_simple(r_simple
, a
, b
, m
, ctx
))
202 || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const
, a
, b
, m
, ctx
, NULL
)))
205 if (!TEST_BN_eq(r_simple
, r_mont
)
206 || !TEST_BN_eq(r_simple
, r_recp
)
207 || !TEST_BN_eq(r_simple
, r_mont_const
)) {
208 if (BN_cmp(r_simple
, r_mont
) != 0)
209 TEST_info("simple and mont results differ");
210 if (BN_cmp(r_simple
, r_mont_const
) != 0)
211 TEST_info("simple and mont const time results differ");
212 if (BN_cmp(r_simple
, r_recp
) != 0)
213 TEST_info("simple and recp results differ");
218 BN_print_var(r_simple
);
219 BN_print_var(r_recp
);
220 BN_print_var(r_mont
);
221 BN_print_var(r_mont_const
);
228 BN_free(r_mont_const
);
239 static int test_mod_exp_x2(int idx
)
243 BIGNUM
*r_mont_const_x2_1
= NULL
;
244 BIGNUM
*r_mont_const_x2_2
= NULL
;
245 BIGNUM
*r_simple1
= NULL
;
246 BIGNUM
*r_simple2
= NULL
;
262 if (!TEST_ptr(ctx
= BN_CTX_new()))
265 if (!TEST_ptr(r_mont_const_x2_1
= BN_new())
266 || !TEST_ptr(r_mont_const_x2_2
= BN_new())
267 || !TEST_ptr(r_simple1
= BN_new())
268 || !TEST_ptr(r_simple2
= BN_new())
269 || !TEST_ptr(a1
= BN_new())
270 || !TEST_ptr(b1
= BN_new())
271 || !TEST_ptr(m1
= BN_new())
272 || !TEST_ptr(a2
= BN_new())
273 || !TEST_ptr(b2
= BN_new())
274 || !TEST_ptr(m2
= BN_new()))
277 BN_rand(a1
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
);
278 BN_rand(b1
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
);
279 BN_rand(m1
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
);
280 BN_rand(a2
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
);
281 BN_rand(b2
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
);
282 BN_rand(m2
, factor_size
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
);
284 if (!TEST_true(BN_mod(a1
, a1
, m1
, ctx
))
285 || !TEST_true(BN_mod(b1
, b1
, m1
, ctx
))
286 || !TEST_true(BN_mod(a2
, a2
, m2
, ctx
))
287 || !TEST_true(BN_mod(b2
, b2
, m2
, ctx
))
288 || !TEST_true(BN_mod_exp_simple(r_simple1
, a1
, b1
, m1
, ctx
))
289 || !TEST_true(BN_mod_exp_simple(r_simple2
, a2
, b2
, m2
, ctx
))
290 || !TEST_true(BN_mod_exp_mont_consttime_x2(r_mont_const_x2_1
, a1
, b1
, m1
, NULL
,
291 r_mont_const_x2_2
, a2
, b2
, m2
, NULL
,
295 if (!TEST_BN_eq(r_simple1
, r_mont_const_x2_1
)
296 || !TEST_BN_eq(r_simple2
, r_mont_const_x2_2
)) {
297 if (BN_cmp(r_simple1
, r_mont_const_x2_1
) != 0)
298 TEST_info("simple and mont const time x2 (#1) results differ");
299 if (BN_cmp(r_simple2
, r_mont_const_x2_2
) != 0)
300 TEST_info("simple and mont const time x2 (#2) results differ");
308 BN_print_var(r_simple1
);
309 BN_print_var(r_simple2
);
310 BN_print_var(r_mont_const_x2_1
);
311 BN_print_var(r_mont_const_x2_2
);
317 BN_free(r_mont_const_x2_1
);
318 BN_free(r_mont_const_x2_2
);
332 int setup_tests(void)
334 ADD_TEST(test_mod_exp_zero
);
335 ADD_ALL_TESTS(test_mod_exp
, 200);
336 ADD_ALL_TESTS(test_mod_exp_x2
, 300);