/src/openssl111/crypto/ec/ecp_mont.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the OpenSSL license (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <openssl/err.h> |
12 | | |
13 | | #include "ec_local.h" |
14 | | |
15 | | const EC_METHOD *EC_GFp_mont_method(void) |
16 | 7.90k | { |
17 | 7.90k | static const EC_METHOD ret = { |
18 | 7.90k | EC_FLAGS_DEFAULT_OCT, |
19 | 7.90k | NID_X9_62_prime_field, |
20 | 7.90k | ec_GFp_mont_group_init, |
21 | 7.90k | ec_GFp_mont_group_finish, |
22 | 7.90k | ec_GFp_mont_group_clear_finish, |
23 | 7.90k | ec_GFp_mont_group_copy, |
24 | 7.90k | ec_GFp_mont_group_set_curve, |
25 | 7.90k | ec_GFp_simple_group_get_curve, |
26 | 7.90k | ec_GFp_simple_group_get_degree, |
27 | 7.90k | ec_group_simple_order_bits, |
28 | 7.90k | ec_GFp_simple_group_check_discriminant, |
29 | 7.90k | ec_GFp_simple_point_init, |
30 | 7.90k | ec_GFp_simple_point_finish, |
31 | 7.90k | ec_GFp_simple_point_clear_finish, |
32 | 7.90k | ec_GFp_simple_point_copy, |
33 | 7.90k | ec_GFp_simple_point_set_to_infinity, |
34 | 7.90k | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
35 | 7.90k | ec_GFp_simple_get_Jprojective_coordinates_GFp, |
36 | 7.90k | ec_GFp_simple_point_set_affine_coordinates, |
37 | 7.90k | ec_GFp_simple_point_get_affine_coordinates, |
38 | 7.90k | 0, 0, 0, |
39 | 7.90k | ec_GFp_simple_add, |
40 | 7.90k | ec_GFp_simple_dbl, |
41 | 7.90k | ec_GFp_simple_invert, |
42 | 7.90k | ec_GFp_simple_is_at_infinity, |
43 | 7.90k | ec_GFp_simple_is_on_curve, |
44 | 7.90k | ec_GFp_simple_cmp, |
45 | 7.90k | ec_GFp_simple_make_affine, |
46 | 7.90k | ec_GFp_simple_points_make_affine, |
47 | 7.90k | 0 /* mul */ , |
48 | 7.90k | 0 /* precompute_mult */ , |
49 | 7.90k | 0 /* have_precompute_mult */ , |
50 | 7.90k | ec_GFp_mont_field_mul, |
51 | 7.90k | ec_GFp_mont_field_sqr, |
52 | 7.90k | 0 /* field_div */ , |
53 | 7.90k | ec_GFp_mont_field_inv, |
54 | 7.90k | ec_GFp_mont_field_encode, |
55 | 7.90k | ec_GFp_mont_field_decode, |
56 | 7.90k | ec_GFp_mont_field_set_to_one, |
57 | 7.90k | ec_key_simple_priv2oct, |
58 | 7.90k | ec_key_simple_oct2priv, |
59 | 7.90k | 0, /* set private */ |
60 | 7.90k | ec_key_simple_generate_key, |
61 | 7.90k | ec_key_simple_check_key, |
62 | 7.90k | ec_key_simple_generate_public_key, |
63 | 7.90k | 0, /* keycopy */ |
64 | 7.90k | 0, /* keyfinish */ |
65 | 7.90k | ecdh_simple_compute_key, |
66 | 7.90k | 0, /* field_inverse_mod_ord */ |
67 | 7.90k | ec_GFp_simple_blind_coordinates, |
68 | 7.90k | ec_GFp_simple_ladder_pre, |
69 | 7.90k | ec_GFp_simple_ladder_step, |
70 | 7.90k | ec_GFp_simple_ladder_post |
71 | 7.90k | }; |
72 | | |
73 | 7.90k | return &ret; |
74 | 7.90k | } |
75 | | |
76 | | int ec_GFp_mont_group_init(EC_GROUP *group) |
77 | 14.6k | { |
78 | 14.6k | int ok; |
79 | | |
80 | 14.6k | ok = ec_GFp_simple_group_init(group); |
81 | 14.6k | group->field_data1 = NULL; |
82 | 14.6k | group->field_data2 = NULL; |
83 | 14.6k | return ok; |
84 | 14.6k | } |
85 | | |
86 | | void ec_GFp_mont_group_finish(EC_GROUP *group) |
87 | 13.9k | { |
88 | 13.9k | BN_MONT_CTX_free(group->field_data1); |
89 | 13.9k | group->field_data1 = NULL; |
90 | 13.9k | BN_free(group->field_data2); |
91 | 13.9k | group->field_data2 = NULL; |
92 | 13.9k | ec_GFp_simple_group_finish(group); |
93 | 13.9k | } |
94 | | |
95 | | void ec_GFp_mont_group_clear_finish(EC_GROUP *group) |
96 | 630 | { |
97 | 630 | BN_MONT_CTX_free(group->field_data1); |
98 | 630 | group->field_data1 = NULL; |
99 | 630 | BN_clear_free(group->field_data2); |
100 | 630 | group->field_data2 = NULL; |
101 | 630 | ec_GFp_simple_group_clear_finish(group); |
102 | 630 | } |
103 | | |
104 | | int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
105 | 6.50k | { |
106 | 6.50k | BN_MONT_CTX_free(dest->field_data1); |
107 | 6.50k | dest->field_data1 = NULL; |
108 | 6.50k | BN_clear_free(dest->field_data2); |
109 | 6.50k | dest->field_data2 = NULL; |
110 | | |
111 | 6.50k | if (!ec_GFp_simple_group_copy(dest, src)) |
112 | 0 | return 0; |
113 | | |
114 | 6.50k | if (src->field_data1 != NULL) { |
115 | 6.50k | dest->field_data1 = BN_MONT_CTX_new(); |
116 | 6.50k | if (dest->field_data1 == NULL) |
117 | 0 | return 0; |
118 | 6.50k | if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) |
119 | 0 | goto err; |
120 | 6.50k | } |
121 | 6.50k | if (src->field_data2 != NULL) { |
122 | 6.50k | dest->field_data2 = BN_dup(src->field_data2); |
123 | 6.50k | if (dest->field_data2 == NULL) |
124 | 0 | goto err; |
125 | 6.50k | } |
126 | | |
127 | 6.50k | return 1; |
128 | | |
129 | 0 | err: |
130 | 0 | BN_MONT_CTX_free(dest->field_data1); |
131 | 0 | dest->field_data1 = NULL; |
132 | 0 | return 0; |
133 | 6.50k | } |
134 | | |
135 | | int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, |
136 | | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
137 | 8.10k | { |
138 | 8.10k | BN_CTX *new_ctx = NULL; |
139 | 8.10k | BN_MONT_CTX *mont = NULL; |
140 | 8.10k | BIGNUM *one = NULL; |
141 | 8.10k | int ret = 0; |
142 | | |
143 | 8.10k | BN_MONT_CTX_free(group->field_data1); |
144 | 8.10k | group->field_data1 = NULL; |
145 | 8.10k | BN_free(group->field_data2); |
146 | 8.10k | group->field_data2 = NULL; |
147 | | |
148 | 8.10k | if (ctx == NULL) { |
149 | 3.32k | ctx = new_ctx = BN_CTX_new(); |
150 | 3.32k | if (ctx == NULL) |
151 | 0 | return 0; |
152 | 3.32k | } |
153 | | |
154 | 8.10k | mont = BN_MONT_CTX_new(); |
155 | 8.10k | if (mont == NULL) |
156 | 0 | goto err; |
157 | 8.10k | if (!BN_MONT_CTX_set(mont, p, ctx)) { |
158 | 630 | ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB); |
159 | 630 | goto err; |
160 | 630 | } |
161 | 7.47k | one = BN_new(); |
162 | 7.47k | if (one == NULL) |
163 | 0 | goto err; |
164 | 7.47k | if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) |
165 | 0 | goto err; |
166 | | |
167 | 7.47k | group->field_data1 = mont; |
168 | 7.47k | mont = NULL; |
169 | 7.47k | group->field_data2 = one; |
170 | 7.47k | one = NULL; |
171 | | |
172 | 7.47k | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); |
173 | | |
174 | 7.47k | if (!ret) { |
175 | 0 | BN_MONT_CTX_free(group->field_data1); |
176 | 0 | group->field_data1 = NULL; |
177 | 0 | BN_free(group->field_data2); |
178 | 0 | group->field_data2 = NULL; |
179 | 0 | } |
180 | | |
181 | 8.10k | err: |
182 | 8.10k | BN_free(one); |
183 | 8.10k | BN_CTX_free(new_ctx); |
184 | 8.10k | BN_MONT_CTX_free(mont); |
185 | 8.10k | return ret; |
186 | 7.47k | } |
187 | | |
188 | | int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
189 | | const BIGNUM *b, BN_CTX *ctx) |
190 | 763k | { |
191 | 763k | if (group->field_data1 == NULL) { |
192 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED); |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | 763k | return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx); |
197 | 763k | } |
198 | | |
199 | | int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
200 | | BN_CTX *ctx) |
201 | 459k | { |
202 | 459k | if (group->field_data1 == NULL) { |
203 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED); |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 459k | return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx); |
208 | 459k | } |
209 | | |
210 | | /*- |
211 | | * Computes the multiplicative inverse of a in GF(p), storing the result in r. |
212 | | * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error. |
213 | | * We have a Mont structure, so SCA hardening is FLT inversion. |
214 | | */ |
215 | | int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
216 | | BN_CTX *ctx) |
217 | 348 | { |
218 | 348 | BIGNUM *e = NULL; |
219 | 348 | BN_CTX *new_ctx = NULL; |
220 | 348 | int ret = 0; |
221 | | |
222 | 348 | if (group->field_data1 == NULL) |
223 | 0 | return 0; |
224 | | |
225 | 348 | if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) |
226 | 0 | return 0; |
227 | | |
228 | 348 | BN_CTX_start(ctx); |
229 | 348 | if ((e = BN_CTX_get(ctx)) == NULL) |
230 | 0 | goto err; |
231 | | |
232 | | /* Inverse in constant time with Fermats Little Theorem */ |
233 | 348 | if (!BN_set_word(e, 2)) |
234 | 0 | goto err; |
235 | 348 | if (!BN_sub(e, group->field, e)) |
236 | 0 | goto err; |
237 | | /*- |
238 | | * Exponent e is public. |
239 | | * No need for scatter-gather or BN_FLG_CONSTTIME. |
240 | | */ |
241 | 348 | if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1)) |
242 | 0 | goto err; |
243 | | |
244 | | /* throw an error on zero */ |
245 | 348 | if (BN_is_zero(r)) { |
246 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT); |
247 | 0 | goto err; |
248 | 0 | } |
249 | | |
250 | 348 | ret = 1; |
251 | | |
252 | 348 | err: |
253 | 348 | BN_CTX_end(ctx); |
254 | 348 | BN_CTX_free(new_ctx); |
255 | 348 | return ret; |
256 | 348 | } |
257 | | |
258 | | int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, |
259 | | const BIGNUM *a, BN_CTX *ctx) |
260 | 37.1k | { |
261 | 37.1k | if (group->field_data1 == NULL) { |
262 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED); |
263 | 0 | return 0; |
264 | 0 | } |
265 | | |
266 | 37.1k | return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx); |
267 | 37.1k | } |
268 | | |
269 | | int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, |
270 | | const BIGNUM *a, BN_CTX *ctx) |
271 | 17.4k | { |
272 | 17.4k | if (group->field_data1 == NULL) { |
273 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED); |
274 | 0 | return 0; |
275 | 0 | } |
276 | | |
277 | 17.4k | return BN_from_montgomery(r, a, group->field_data1, ctx); |
278 | 17.4k | } |
279 | | |
280 | | int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, |
281 | | BN_CTX *ctx) |
282 | 11.1k | { |
283 | 11.1k | if (group->field_data2 == NULL) { |
284 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED); |
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | 11.1k | if (!BN_copy(r, group->field_data2)) |
289 | 0 | return 0; |
290 | 11.1k | return 1; |
291 | 11.1k | } |