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