]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecdsa_ossl.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / ec / ecdsa_ossl.c
1 /*
2 * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
11 * ECDSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17 #include <openssl/err.h>
18 #include <openssl/obj_mac.h>
19 #include <openssl/rand.h>
20 #include "crypto/bn.h"
21 #include "ec_local.h"
22 #include "internal/deterministic_nonce.h"
23
24 #define MIN_ECDSA_SIGN_ORDERBITS 64
25 /*
26 * It is highly unlikely that a retry will happen,
27 * Multiple retries would indicate that something is wrong
28 * with the group parameters (which would normally only happen
29 * with a bad custom group).
30 */
31 #define MAX_ECDSA_SIGN_RETRIES 8
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 unsigned int nonce_type, const char *digestname,
37 OSSL_LIB_CTX *libctx, const char *propq);
38
39 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
40 BIGNUM **rp)
41 {
42 if (eckey->group->meth->ecdsa_sign_setup == NULL) {
43 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
44 return 0;
45 }
46
47 return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
48 }
49
50 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
51 const BIGNUM *in_kinv, const BIGNUM *in_r,
52 EC_KEY *eckey)
53 {
54 if (eckey->group->meth->ecdsa_sign_sig == NULL) {
55 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
56 return NULL;
57 }
58
59 return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
60 in_kinv, in_r, eckey);
61 }
62
63 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
64 const ECDSA_SIG *sig, EC_KEY *eckey)
65 {
66 if (eckey->group->meth->ecdsa_verify_sig == NULL) {
67 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
68 return 0;
69 }
70
71 return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
72 }
73
74 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
75 unsigned char *sig, unsigned int *siglen,
76 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
77 {
78 ECDSA_SIG *s;
79
80 if (sig == NULL && (kinv == NULL || r == NULL)) {
81 *siglen = ECDSA_size(eckey);
82 return 1;
83 }
84
85 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
86 if (s == NULL) {
87 *siglen = 0;
88 return 0;
89 }
90 *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
91 ECDSA_SIG_free(s);
92 return 1;
93 }
94
95 int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
96 unsigned char *sig, unsigned int *siglen,
97 EC_KEY *eckey, unsigned int nonce_type,
98 const char *digestname,
99 OSSL_LIB_CTX *libctx, const char *propq)
100 {
101 ECDSA_SIG *s;
102 BIGNUM *kinv = NULL, *r = NULL;
103 int ret = 0;
104
105 if (sig == NULL) {
106 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
107 return 0;
108 }
109
110 *siglen = 0;
111 if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
112 nonce_type, digestname, libctx, propq))
113 return 0;
114
115 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
116 if (s == NULL)
117 goto end;
118
119 *siglen = i2d_ECDSA_SIG(s, &sig);
120 ECDSA_SIG_free(s);
121 ret = 1;
122 end:
123 BN_clear_free(kinv);
124 BN_clear_free(r);
125 return ret;
126 }
127
128 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
129 BIGNUM **kinvp, BIGNUM **rp,
130 const unsigned char *dgst, int dlen,
131 unsigned int nonce_type, const char *digestname,
132 OSSL_LIB_CTX *libctx, const char *propq)
133 {
134 BN_CTX *ctx = NULL;
135 BIGNUM *k = NULL, *r = NULL, *X = NULL;
136 const BIGNUM *order;
137 EC_POINT *tmp_point = NULL;
138 const EC_GROUP *group;
139 int ret = 0;
140 int order_bits;
141 const BIGNUM *priv_key;
142
143 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
144 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
145 return 0;
146 }
147 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
148 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
149 return 0;
150 }
151
152 if (!EC_KEY_can_sign(eckey)) {
153 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
154 return 0;
155 }
156
157 if ((ctx = ctx_in) == NULL) {
158 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
159 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
160 return 0;
161 }
162 }
163
164 k = BN_secure_new(); /* this value is later returned in *kinvp */
165 r = BN_new(); /* this value is later returned in *rp */
166 X = BN_new();
167 if (k == NULL || r == NULL || X == NULL) {
168 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
169 goto err;
170 }
171 if ((tmp_point = EC_POINT_new(group)) == NULL) {
172 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
173 goto err;
174 }
175 order = EC_GROUP_get0_order(group);
176
177 /* Preallocate space */
178 order_bits = BN_num_bits(order);
179 /* Check the number of bits here so that an infinite loop is not possible */
180 if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
181 || !BN_set_bit(k, order_bits)
182 || !BN_set_bit(r, order_bits)
183 || !BN_set_bit(X, order_bits))
184 goto err;
185
186 do {
187 /* get random or deterministic value of k */
188 do {
189 int res = 0;
190
191 if (dgst != NULL) {
192 if (nonce_type == 1) {
193 #ifndef FIPS_MODULE
194 res = ossl_gen_deterministic_nonce_rfc6979(k, order,
195 priv_key,
196 dgst, dlen,
197 digestname,
198 libctx, propq);
199 #endif
200 } else {
201 res = ossl_bn_gen_dsa_nonce_fixed_top(k, order, priv_key,
202 dgst, dlen, ctx);
203 }
204 } else {
205 res = ossl_bn_priv_rand_range_fixed_top(k, order, 0, ctx);
206 }
207 if (!res) {
208 ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
209 goto err;
210 }
211 } while (ossl_bn_is_word_fixed_top(k, 0));
212
213 /* compute r the x-coordinate of generator * k */
214 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
215 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
216 goto err;
217 }
218
219 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
220 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
221 goto err;
222 }
223
224 if (!BN_nnmod(r, X, order, ctx)) {
225 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
226 goto err;
227 }
228 } while (BN_is_zero(r));
229
230 /* compute the inverse of k */
231 if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
232 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
233 goto err;
234 }
235
236 /* clear old values if necessary */
237 BN_clear_free(*rp);
238 BN_clear_free(*kinvp);
239 /* save the pre-computed values */
240 *rp = r;
241 *kinvp = k;
242 ret = 1;
243 err:
244 if (!ret) {
245 BN_clear_free(k);
246 BN_clear_free(r);
247 }
248 if (ctx != ctx_in)
249 BN_CTX_free(ctx);
250 EC_POINT_free(tmp_point);
251 BN_clear_free(X);
252 return ret;
253 }
254
255 int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
256 BIGNUM **rp)
257 {
258 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
259 0, NULL, NULL, NULL);
260 }
261
262 ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
263 const BIGNUM *in_kinv, const BIGNUM *in_r,
264 EC_KEY *eckey)
265 {
266 int ok = 0, i;
267 int retries = 0;
268 BIGNUM *kinv = NULL, *s, *m = NULL;
269 const BIGNUM *order, *ckinv;
270 BN_CTX *ctx = NULL;
271 const EC_GROUP *group;
272 ECDSA_SIG *ret;
273 const BIGNUM *priv_key;
274
275 group = EC_KEY_get0_group(eckey);
276 priv_key = EC_KEY_get0_private_key(eckey);
277
278 if (group == NULL) {
279 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
280 return NULL;
281 }
282 if (priv_key == NULL) {
283 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
284 return NULL;
285 }
286
287 if (!EC_KEY_can_sign(eckey)) {
288 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
289 return NULL;
290 }
291
292 ret = ECDSA_SIG_new();
293 if (ret == NULL) {
294 ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
295 return NULL;
296 }
297 ret->r = BN_new();
298 ret->s = BN_new();
299 if (ret->r == NULL || ret->s == NULL) {
300 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
301 goto err;
302 }
303 s = ret->s;
304
305 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
306 || (m = BN_new()) == NULL) {
307 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
308 goto err;
309 }
310
311 order = EC_GROUP_get0_order(group);
312 i = BN_num_bits(order);
313 /*
314 * Need to truncate digest if it is too long: first truncate whole bytes.
315 */
316 if (8 * dgst_len > i)
317 dgst_len = (i + 7) / 8;
318 if (!BN_bin2bn(dgst, dgst_len, m)) {
319 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
320 goto err;
321 }
322 /* If still too long, truncate remaining bits with a shift */
323 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
324 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
325 goto err;
326 }
327 do {
328 if (in_kinv == NULL || in_r == NULL) {
329 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
330 0, NULL, NULL, NULL)) {
331 ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
332 goto err;
333 }
334 ckinv = kinv;
335 } else {
336 ckinv = in_kinv;
337 if (BN_copy(ret->r, in_r) == NULL) {
338 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
339 goto err;
340 }
341 }
342
343 /*
344 * With only one multiplicant being in Montgomery domain
345 * multiplication yields real result without post-conversion.
346 * Also note that all operations but last are performed with
347 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
348 * below, returns user-visible value with removed zero padding.
349 */
350 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
351 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
352 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
353 goto err;
354 }
355 if (!bn_mod_add_fixed_top(s, s, m, order)) {
356 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
357 goto err;
358 }
359 /*
360 * |s| can still be larger than modulus, because |m| can be. In
361 * such case we count on Montgomery reduction to tie it up.
362 */
363 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
364 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
365 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
366 goto err;
367 }
368
369 if (BN_is_zero(s)) {
370 /*
371 * if kinv and r have been supplied by the caller, don't
372 * generate new kinv and r values
373 */
374 if (in_kinv != NULL && in_r != NULL) {
375 ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
376 goto err;
377 }
378 /* Avoid infinite loops cause by invalid group parameters */
379 if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
380 ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
381 goto err;
382 }
383 } else {
384 /* s != 0 => we have a valid signature */
385 break;
386 }
387 } while (1);
388
389 ok = 1;
390 err:
391 if (!ok) {
392 ECDSA_SIG_free(ret);
393 ret = NULL;
394 }
395 BN_CTX_free(ctx);
396 BN_clear_free(m);
397 BN_clear_free(kinv);
398 return ret;
399 }
400
401 /*-
402 * returns
403 * 1: correct signature
404 * 0: incorrect signature
405 * -1: error
406 */
407 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
408 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
409 {
410 ECDSA_SIG *s;
411 const unsigned char *p = sigbuf;
412 unsigned char *der = NULL;
413 int derlen = -1;
414 int ret = -1;
415
416 s = ECDSA_SIG_new();
417 if (s == NULL)
418 return ret;
419 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
420 goto err;
421 /* Ensure signature uses DER and doesn't have trailing garbage */
422 derlen = i2d_ECDSA_SIG(s, &der);
423 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
424 goto err;
425 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
426 err:
427 OPENSSL_free(der);
428 ECDSA_SIG_free(s);
429 return ret;
430 }
431
432 int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
433 const ECDSA_SIG *sig, EC_KEY *eckey)
434 {
435 int ret = -1, i;
436 BN_CTX *ctx;
437 const BIGNUM *order;
438 BIGNUM *u1, *u2, *m, *X;
439 EC_POINT *point = NULL;
440 const EC_GROUP *group;
441 const EC_POINT *pub_key;
442
443 /* check input values */
444 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
445 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
446 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
447 return -1;
448 }
449
450 if (!EC_KEY_can_sign(eckey)) {
451 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
452 return -1;
453 }
454
455 ctx = BN_CTX_new_ex(eckey->libctx);
456 if (ctx == NULL) {
457 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
458 return -1;
459 }
460 BN_CTX_start(ctx);
461 u1 = BN_CTX_get(ctx);
462 u2 = BN_CTX_get(ctx);
463 m = BN_CTX_get(ctx);
464 X = BN_CTX_get(ctx);
465 if (X == NULL) {
466 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
467 goto err;
468 }
469
470 order = EC_GROUP_get0_order(group);
471 if (order == NULL) {
472 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
473 goto err;
474 }
475
476 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
477 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
478 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
479 ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
480 ret = 0; /* signature is invalid */
481 goto err;
482 }
483 /* calculate tmp1 = inv(S) mod order */
484 if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
485 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
486 goto err;
487 }
488 /* digest -> m */
489 i = BN_num_bits(order);
490 /*
491 * Need to truncate digest if it is too long: first truncate whole bytes.
492 */
493 if (8 * dgst_len > i)
494 dgst_len = (i + 7) / 8;
495 if (!BN_bin2bn(dgst, dgst_len, m)) {
496 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
497 goto err;
498 }
499 /* If still too long truncate remaining bits with a shift */
500 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
501 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
502 goto err;
503 }
504 /* u1 = m * tmp mod order */
505 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
506 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
507 goto err;
508 }
509 /* u2 = r * w mod q */
510 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
511 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
512 goto err;
513 }
514
515 if ((point = EC_POINT_new(group)) == NULL) {
516 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
517 goto err;
518 }
519 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
520 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
521 goto err;
522 }
523
524 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
525 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
526 goto err;
527 }
528
529 if (!BN_nnmod(u1, X, order, ctx)) {
530 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
531 goto err;
532 }
533 /* if the signature is correct u1 is equal to sig->r */
534 ret = (BN_ucmp(u1, sig->r) == 0);
535 err:
536 BN_CTX_end(ctx);
537 BN_CTX_free(ctx);
538 EC_POINT_free(point);
539 return ret;
540 }