1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/* $OpenBSD: bn_mod_exp_zero.c,v 1.2 2023/03/15 00:41:04 tb Exp $ */
/*
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <err.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include "bn_local.h"
#define INIT_MOD_EXP_FN(f) { .name = #f, .mod_exp_fn = (f), }
#define INIT_MOD_EXP_MONT_FN(f) { .name = #f, .mod_exp_mont_fn = (f), }
static const struct mod_exp_zero_test {
const char *name;
int (*mod_exp_fn)(BIGNUM *,const BIGNUM *, const BIGNUM *,
const BIGNUM *, BN_CTX *);
int (*mod_exp_mont_fn)(BIGNUM *,const BIGNUM *, const BIGNUM *,
const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
} mod_exp_zero_test_data[] = {
INIT_MOD_EXP_FN(BN_mod_exp),
INIT_MOD_EXP_FN(BN_mod_exp_ct),
INIT_MOD_EXP_FN(BN_mod_exp_nonct),
INIT_MOD_EXP_FN(BN_mod_exp_recp),
INIT_MOD_EXP_FN(BN_mod_exp_simple),
INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont),
INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_ct),
INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_consttime),
INIT_MOD_EXP_MONT_FN(BN_mod_exp_mont_nonct),
};
#define N_MOD_EXP_ZERO_TESTS \
(sizeof(mod_exp_zero_test_data) / sizeof(mod_exp_zero_test_data[0]))
static void
print_failure(const BIGNUM *got, const BIGNUM *a, const char *name)
{
fprintf(stderr, "%s test failed for a = ", name);
BN_print_fp(stderr, a);
fprintf(stderr, "\nwant 0, got ");
BN_print_fp(stderr, got);
fprintf(stderr, "\n");
}
static int
bn_mod_exp_zero_test(const struct mod_exp_zero_test *test, BN_CTX *ctx,
int use_random)
{
const BIGNUM *one;
BIGNUM *a, *p, *got;
int failed = 1;
BN_CTX_start(ctx);
if ((a = BN_CTX_get(ctx)) == NULL)
errx(1, "BN_CTX_get");
if ((p = BN_CTX_get(ctx)) == NULL)
errx(1, "BN_CTX_get");
if ((got = BN_CTX_get(ctx)) == NULL)
errx(1, "BN_CTX_get");
one = BN_value_one();
BN_zero(a);
BN_zero(p);
if (use_random) {
if (!BN_rand(a, 1024, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
errx(1, "BN_rand");
}
if (test->mod_exp_fn != NULL) {
if (!test->mod_exp_fn(got, a, p, one, ctx)) {
fprintf(stderr, "%s failed\n", test->name);
ERR_print_errors_fp(stderr);
goto err;
}
} else {
if (!test->mod_exp_mont_fn(got, a, p, one, ctx, NULL)) {
fprintf(stderr, "%s failed\n", test->name);
ERR_print_errors_fp(stderr);
goto err;
}
}
if (!BN_is_zero(got)) {
print_failure(got, a, test->name);
goto err;
}
failed = 0;
err:
BN_CTX_end(ctx);
return failed;
}
static int
bn_mod_exp_zero_word_test(BN_CTX *ctx)
{
const char *name = "BN_mod_exp_mont_word";
const BIGNUM *one;
BIGNUM *p, *got;
int failed = 1;
BN_CTX_start(ctx);
if ((p = BN_CTX_get(ctx)) == NULL)
errx(1, "BN_CTX_get");
if ((got = BN_CTX_get(ctx)) == NULL)
errx(1, "BN_CTX_get");
one = BN_value_one();
BN_zero(p);
if (!BN_mod_exp_mont_word(got, 1, p, one, ctx, NULL)) {
fprintf(stderr, "%s failed\n", name);
ERR_print_errors_fp(stderr);
goto err;
}
if (!BN_is_zero(got)) {
print_failure(got, one, name);
goto err;
}
failed = 0;
err:
BN_CTX_end(ctx);
return failed;
}
static int
run_bn_mod_exp_zero_tests(void)
{
BN_CTX *ctx;
size_t i;
int use_random;
int failed = 0;
if ((ctx = BN_CTX_new()) == NULL)
errx(1, "BN_CTX_new");
use_random = 1;
for (i = 0; i < N_MOD_EXP_ZERO_TESTS; i++)
failed |= bn_mod_exp_zero_test(&mod_exp_zero_test_data[i], ctx,
use_random);
use_random = 0;
for (i = 0; i < N_MOD_EXP_ZERO_TESTS; i++)
failed |= bn_mod_exp_zero_test(&mod_exp_zero_test_data[i], ctx,
use_random);
failed |= bn_mod_exp_zero_word_test(ctx);
BN_CTX_free(ctx);
return failed;
}
int
main(void)
{
int failed = 0;
failed |= run_bn_mod_exp_zero_tests();
return failed;
}
|