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