/src/openssl/crypto/ec/ecdsa_ossl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2002-2018 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 <string.h> |
11 | | #include <openssl/err.h> |
12 | | #include <openssl/obj_mac.h> |
13 | | #include <openssl/rand.h> |
14 | | #include "internal/bn_int.h" |
15 | | #include "ec_lcl.h" |
16 | | |
17 | | int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, |
18 | | unsigned char *sig, unsigned int *siglen, |
19 | | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) |
20 | 0 | { |
21 | 0 | ECDSA_SIG *s; |
22 | 0 |
|
23 | 0 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); |
24 | 0 | if (s == NULL) { |
25 | 0 | *siglen = 0; |
26 | 0 | return 0; |
27 | 0 | } |
28 | 0 | *siglen = i2d_ECDSA_SIG(s, &sig); |
29 | 0 | ECDSA_SIG_free(s); |
30 | 0 | return 1; |
31 | 0 | } |
32 | | |
33 | | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, |
34 | | BIGNUM **kinvp, BIGNUM **rp, |
35 | | const unsigned char *dgst, int dlen) |
36 | 0 | { |
37 | 0 | BN_CTX *ctx = NULL; |
38 | 0 | BIGNUM *k = NULL, *r = NULL, *X = NULL; |
39 | 0 | const BIGNUM *order; |
40 | 0 | EC_POINT *tmp_point = NULL; |
41 | 0 | const EC_GROUP *group; |
42 | 0 | int ret = 0; |
43 | 0 | int order_bits; |
44 | 0 |
|
45 | 0 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
46 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER); |
47 | 0 | return 0; |
48 | 0 | } |
49 | 0 |
|
50 | 0 | if (!EC_KEY_can_sign(eckey)) { |
51 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); |
52 | 0 | return 0; |
53 | 0 | } |
54 | 0 |
|
55 | 0 | if ((ctx = ctx_in) == NULL) { |
56 | 0 | if ((ctx = BN_CTX_new()) == NULL) { |
57 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
58 | 0 | return 0; |
59 | 0 | } |
60 | 0 | } |
61 | 0 |
|
62 | 0 | k = BN_new(); /* this value is later returned in *kinvp */ |
63 | 0 | r = BN_new(); /* this value is later returned in *rp */ |
64 | 0 | X = BN_new(); |
65 | 0 | if (k == NULL || r == NULL || X == NULL) { |
66 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
67 | 0 | goto err; |
68 | 0 | } |
69 | 0 | if ((tmp_point = EC_POINT_new(group)) == NULL) { |
70 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
71 | 0 | goto err; |
72 | 0 | } |
73 | 0 | order = EC_GROUP_get0_order(group); |
74 | 0 |
|
75 | 0 | /* Preallocate space */ |
76 | 0 | order_bits = BN_num_bits(order); |
77 | 0 | if (!BN_set_bit(k, order_bits) |
78 | 0 | || !BN_set_bit(r, order_bits) |
79 | 0 | || !BN_set_bit(X, order_bits)) |
80 | 0 | goto err; |
81 | 0 | |
82 | 0 | do { |
83 | 0 | /* get random k */ |
84 | 0 | do { |
85 | 0 | if (dgst != NULL) { |
86 | 0 | if (!BN_generate_dsa_nonce(k, order, |
87 | 0 | EC_KEY_get0_private_key(eckey), |
88 | 0 | dgst, dlen, ctx)) { |
89 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
90 | 0 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); |
91 | 0 | goto err; |
92 | 0 | } |
93 | 0 | } else { |
94 | 0 | if (!BN_priv_rand_range(k, order)) { |
95 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
96 | 0 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); |
97 | 0 | goto err; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } while (BN_is_zero(k)); |
101 | 0 |
|
102 | 0 | /* compute r the x-coordinate of generator * k */ |
103 | 0 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) { |
104 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
105 | 0 | goto err; |
106 | 0 | } |
107 | 0 |
|
108 | 0 | if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) { |
109 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
110 | 0 | goto err; |
111 | 0 | } |
112 | 0 |
|
113 | 0 | if (!BN_nnmod(r, X, order, ctx)) { |
114 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
115 | 0 | goto err; |
116 | 0 | } |
117 | 0 | } while (BN_is_zero(r)); |
118 | 0 |
|
119 | 0 | /* compute the inverse of k */ |
120 | 0 | if (!ec_group_do_inverse_ord(group, k, k, ctx)) { |
121 | 0 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
122 | 0 | goto err; |
123 | 0 | } |
124 | 0 |
|
125 | 0 | /* clear old values if necessary */ |
126 | 0 | BN_clear_free(*rp); |
127 | 0 | BN_clear_free(*kinvp); |
128 | 0 | /* save the pre-computed values */ |
129 | 0 | *rp = r; |
130 | 0 | *kinvp = k; |
131 | 0 | ret = 1; |
132 | 0 | err: |
133 | 0 | if (!ret) { |
134 | 0 | BN_clear_free(k); |
135 | 0 | BN_clear_free(r); |
136 | 0 | } |
137 | 0 | if (ctx != ctx_in) |
138 | 0 | BN_CTX_free(ctx); |
139 | 0 | EC_POINT_free(tmp_point); |
140 | 0 | BN_clear_free(X); |
141 | 0 | return ret; |
142 | 0 | } |
143 | | |
144 | | int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
145 | | BIGNUM **rp) |
146 | 0 | { |
147 | 0 | return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0); |
148 | 0 | } |
149 | | |
150 | | ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, |
151 | | const BIGNUM *in_kinv, const BIGNUM *in_r, |
152 | | EC_KEY *eckey) |
153 | 0 | { |
154 | 0 | int ok = 0, i; |
155 | 0 | BIGNUM *kinv = NULL, *s, *m = NULL; |
156 | 0 | const BIGNUM *order, *ckinv; |
157 | 0 | BN_CTX *ctx = NULL; |
158 | 0 | const EC_GROUP *group; |
159 | 0 | ECDSA_SIG *ret; |
160 | 0 | const BIGNUM *priv_key; |
161 | 0 |
|
162 | 0 | group = EC_KEY_get0_group(eckey); |
163 | 0 | priv_key = EC_KEY_get0_private_key(eckey); |
164 | 0 |
|
165 | 0 | if (group == NULL || priv_key == NULL) { |
166 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER); |
167 | 0 | return NULL; |
168 | 0 | } |
169 | 0 |
|
170 | 0 | if (!EC_KEY_can_sign(eckey)) { |
171 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); |
172 | 0 | return NULL; |
173 | 0 | } |
174 | 0 |
|
175 | 0 | ret = ECDSA_SIG_new(); |
176 | 0 | if (ret == NULL) { |
177 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
178 | 0 | return NULL; |
179 | 0 | } |
180 | 0 | ret->r = BN_new(); |
181 | 0 | ret->s = BN_new(); |
182 | 0 | if (ret->r == NULL || ret->s == NULL) { |
183 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
184 | 0 | goto err; |
185 | 0 | } |
186 | 0 | s = ret->s; |
187 | 0 |
|
188 | 0 | if ((ctx = BN_CTX_new()) == NULL |
189 | 0 | || (m = BN_new()) == NULL) { |
190 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
191 | 0 | goto err; |
192 | 0 | } |
193 | 0 |
|
194 | 0 | order = EC_GROUP_get0_order(group); |
195 | 0 | i = BN_num_bits(order); |
196 | 0 | /* |
197 | 0 | * Need to truncate digest if it is too long: first truncate whole bytes. |
198 | 0 | */ |
199 | 0 | if (8 * dgst_len > i) |
200 | 0 | dgst_len = (i + 7) / 8; |
201 | 0 | if (!BN_bin2bn(dgst, dgst_len, m)) { |
202 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
203 | 0 | goto err; |
204 | 0 | } |
205 | 0 | /* If still too long, truncate remaining bits with a shift */ |
206 | 0 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { |
207 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
208 | 0 | goto err; |
209 | 0 | } |
210 | 0 | do { |
211 | 0 | if (in_kinv == NULL || in_r == NULL) { |
212 | 0 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) { |
213 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB); |
214 | 0 | goto err; |
215 | 0 | } |
216 | 0 | ckinv = kinv; |
217 | 0 | } else { |
218 | 0 | ckinv = in_kinv; |
219 | 0 | if (BN_copy(ret->r, in_r) == NULL) { |
220 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
221 | 0 | goto err; |
222 | 0 | } |
223 | 0 | } |
224 | 0 |
|
225 | 0 | /* |
226 | 0 | * With only one multiplicant being in Montgomery domain |
227 | 0 | * multiplication yields real result without post-conversion. |
228 | 0 | * Also note that all operations but last are performed with |
229 | 0 | * zero-padded vectors. Last operation, BN_mod_mul_montgomery |
230 | 0 | * below, returns user-visible value with removed zero padding. |
231 | 0 | */ |
232 | 0 | if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx) |
233 | 0 | || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) { |
234 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
235 | 0 | goto err; |
236 | 0 | } |
237 | 0 | if (!bn_mod_add_fixed_top(s, s, m, order)) { |
238 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
239 | 0 | goto err; |
240 | 0 | } |
241 | 0 | /* |
242 | 0 | * |s| can still be larger than modulus, because |m| can be. In |
243 | 0 | * such case we count on Montgomery reduction to tie it up. |
244 | 0 | */ |
245 | 0 | if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx) |
246 | 0 | || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) { |
247 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
248 | 0 | goto err; |
249 | 0 | } |
250 | 0 |
|
251 | 0 | if (BN_is_zero(s)) { |
252 | 0 | /* |
253 | 0 | * if kinv and r have been supplied by the caller, don't |
254 | 0 | * generate new kinv and r values |
255 | 0 | */ |
256 | 0 | if (in_kinv != NULL && in_r != NULL) { |
257 | 0 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES); |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | } else { |
261 | 0 | /* s != 0 => we have a valid signature */ |
262 | 0 | break; |
263 | 0 | } |
264 | 0 | } while (1); |
265 | 0 |
|
266 | 0 | ok = 1; |
267 | 0 | err: |
268 | 0 | if (!ok) { |
269 | 0 | ECDSA_SIG_free(ret); |
270 | 0 | ret = NULL; |
271 | 0 | } |
272 | 0 | BN_CTX_free(ctx); |
273 | 0 | BN_clear_free(m); |
274 | 0 | BN_clear_free(kinv); |
275 | 0 | return ret; |
276 | 0 | } |
277 | | |
278 | | /*- |
279 | | * returns |
280 | | * 1: correct signature |
281 | | * 0: incorrect signature |
282 | | * -1: error |
283 | | */ |
284 | | int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len, |
285 | | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) |
286 | 0 | { |
287 | 0 | ECDSA_SIG *s; |
288 | 0 | const unsigned char *p = sigbuf; |
289 | 0 | unsigned char *der = NULL; |
290 | 0 | int derlen = -1; |
291 | 0 | int ret = -1; |
292 | 0 |
|
293 | 0 | s = ECDSA_SIG_new(); |
294 | 0 | if (s == NULL) |
295 | 0 | return ret; |
296 | 0 | if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) |
297 | 0 | goto err; |
298 | 0 | /* Ensure signature uses DER and doesn't have trailing garbage */ |
299 | 0 | derlen = i2d_ECDSA_SIG(s, &der); |
300 | 0 | if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0) |
301 | 0 | goto err; |
302 | 0 | ret = ECDSA_do_verify(dgst, dgst_len, s, eckey); |
303 | 0 | err: |
304 | 0 | OPENSSL_clear_free(der, derlen); |
305 | 0 | ECDSA_SIG_free(s); |
306 | 0 | return ret; |
307 | 0 | } |
308 | | |
309 | | int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len, |
310 | | const ECDSA_SIG *sig, EC_KEY *eckey) |
311 | 0 | { |
312 | 0 | int ret = -1, i; |
313 | 0 | BN_CTX *ctx; |
314 | 0 | const BIGNUM *order; |
315 | 0 | BIGNUM *u1, *u2, *m, *X; |
316 | 0 | EC_POINT *point = NULL; |
317 | 0 | const EC_GROUP *group; |
318 | 0 | const EC_POINT *pub_key; |
319 | 0 |
|
320 | 0 | /* check input values */ |
321 | 0 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || |
322 | 0 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { |
323 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS); |
324 | 0 | return -1; |
325 | 0 | } |
326 | 0 |
|
327 | 0 | if (!EC_KEY_can_sign(eckey)) { |
328 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); |
329 | 0 | return -1; |
330 | 0 | } |
331 | 0 |
|
332 | 0 | ctx = BN_CTX_new(); |
333 | 0 | if (ctx == NULL) { |
334 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
335 | 0 | return -1; |
336 | 0 | } |
337 | 0 | BN_CTX_start(ctx); |
338 | 0 | u1 = BN_CTX_get(ctx); |
339 | 0 | u2 = BN_CTX_get(ctx); |
340 | 0 | m = BN_CTX_get(ctx); |
341 | 0 | X = BN_CTX_get(ctx); |
342 | 0 | if (X == NULL) { |
343 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
344 | 0 | goto err; |
345 | 0 | } |
346 | 0 |
|
347 | 0 | order = EC_GROUP_get0_order(group); |
348 | 0 | if (order == NULL) { |
349 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
350 | 0 | goto err; |
351 | 0 | } |
352 | 0 |
|
353 | 0 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
354 | 0 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || |
355 | 0 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { |
356 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE); |
357 | 0 | ret = 0; /* signature is invalid */ |
358 | 0 | goto err; |
359 | 0 | } |
360 | 0 | /* calculate tmp1 = inv(S) mod order */ |
361 | 0 | if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) { |
362 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
363 | 0 | goto err; |
364 | 0 | } |
365 | 0 | /* digest -> m */ |
366 | 0 | i = BN_num_bits(order); |
367 | 0 | /* |
368 | 0 | * Need to truncate digest if it is too long: first truncate whole bytes. |
369 | 0 | */ |
370 | 0 | if (8 * dgst_len > i) |
371 | 0 | dgst_len = (i + 7) / 8; |
372 | 0 | if (!BN_bin2bn(dgst, dgst_len, m)) { |
373 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
374 | 0 | goto err; |
375 | 0 | } |
376 | 0 | /* If still too long truncate remaining bits with a shift */ |
377 | 0 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { |
378 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
379 | 0 | goto err; |
380 | 0 | } |
381 | 0 | /* u1 = m * tmp mod order */ |
382 | 0 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { |
383 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
384 | 0 | goto err; |
385 | 0 | } |
386 | 0 | /* u2 = r * w mod q */ |
387 | 0 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { |
388 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
389 | 0 | goto err; |
390 | 0 | } |
391 | 0 |
|
392 | 0 | if ((point = EC_POINT_new(group)) == NULL) { |
393 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
394 | 0 | goto err; |
395 | 0 | } |
396 | 0 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { |
397 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
398 | 0 | goto err; |
399 | 0 | } |
400 | 0 |
|
401 | 0 | if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) { |
402 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
403 | 0 | goto err; |
404 | 0 | } |
405 | 0 |
|
406 | 0 | if (!BN_nnmod(u1, X, order, ctx)) { |
407 | 0 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
408 | 0 | goto err; |
409 | 0 | } |
410 | 0 | /* if the signature is correct u1 is equal to sig->r */ |
411 | 0 | ret = (BN_ucmp(u1, sig->r) == 0); |
412 | 0 | err: |
413 | 0 | BN_CTX_end(ctx); |
414 | 0 | BN_CTX_free(ctx); |
415 | 0 | EC_POINT_free(point); |
416 | 0 | return ret; |
417 | 0 | } |