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