f7f80b3a2bebbabbc93c063b49762a46e8d05ca8
[openssl.git] / crypto / ec / ecdsa_ossl.c
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/bn.h>
14 #include <openssl/rand.h>
15 #include <openssl/ec.h>
16 #include "ec_lcl.h"
17
18 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
19                     unsigned char *sig, unsigned int *siglen,
20                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
21 {
22     ECDSA_SIG *s;
23
24     s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
25     if (s == NULL) {
26         *siglen = 0;
27         return 0;
28     }
29     *siglen = i2d_ECDSA_SIG(s, &sig);
30     ECDSA_SIG_free(s);
31     return 1;
32 }
33
34 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
35                             BIGNUM **kinvp, BIGNUM **rp,
36                             const unsigned char *dgst, int dlen)
37 {
38     BN_CTX *ctx = NULL;
39     BIGNUM *k = NULL, *r = NULL, *X = NULL;
40     const BIGNUM *order;
41     EC_POINT *tmp_point = NULL;
42     const EC_GROUP *group;
43     int ret = 0;
44     int order_bits;
45
46     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
47         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
48         return 0;
49     }
50
51     if (!EC_KEY_can_sign(eckey)) {
52         ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
53         return 0;
54     }
55
56     if (ctx_in == NULL) {
57         if ((ctx = BN_CTX_new()) == NULL) {
58             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
59             return 0;
60         }
61     } else
62         ctx = ctx_in;
63
64     k = BN_new();               /* this value is later returned in *kinvp */
65     r = BN_new();               /* this value is later returned in *rp */
66     X = BN_new();
67     if (k == NULL || r == NULL || X == NULL) {
68         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
69         goto err;
70     }
71     if ((tmp_point = EC_POINT_new(group)) == NULL) {
72         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
73         goto err;
74     }
75     order = EC_GROUP_get0_order(group);
76     if (order == NULL) {
77         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
78         goto err;
79     }
80
81     /* Preallocate space */
82     order_bits = BN_num_bits(order);
83     if (!BN_set_bit(k, order_bits)
84         || !BN_set_bit(r, order_bits)
85         || !BN_set_bit(X, order_bits))
86         goto err;
87
88     do {
89         /* get random k */
90         do
91             if (dgst != NULL) {
92                 if (!BN_generate_dsa_nonce
93                     (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
94                      ctx)) {
95                     ECerr(EC_F_ECDSA_SIGN_SETUP,
96                              EC_R_RANDOM_NUMBER_GENERATION_FAILED);
97                     goto err;
98                 }
99             } else {
100                 if (!BN_priv_rand_range(k, order)) {
101                     ECerr(EC_F_ECDSA_SIGN_SETUP,
102                              EC_R_RANDOM_NUMBER_GENERATION_FAILED);
103                     goto err;
104                 }
105             }
106         while (BN_is_zero(k));
107
108         /* compute r the x-coordinate of generator * k */
109         if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
110             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
111             goto err;
112         }
113         if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
114             NID_X9_62_prime_field) {
115             if (!EC_POINT_get_affine_coordinates_GFp
116                 (group, tmp_point, X, NULL, ctx)) {
117                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
118                 goto err;
119             }
120         }
121 #ifndef OPENSSL_NO_EC2M
122         else {                  /* NID_X9_62_characteristic_two_field */
123
124             if (!EC_POINT_get_affine_coordinates_GF2m(group,
125                                                       tmp_point, X, NULL,
126                                                       ctx)) {
127                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
128                 goto err;
129             }
130         }
131 #endif
132         if (!BN_nnmod(r, X, order, ctx)) {
133             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
134             goto err;
135         }
136     }
137     while (BN_is_zero(r));
138
139     /* compute the inverse of k */
140     if (!EC_GROUP_do_inverse_ord(group, k, k, ctx)) {
141         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
142         goto err;
143     }
144
145     /* clear old values if necessary */
146     BN_clear_free(*rp);
147     BN_clear_free(*kinvp);
148     /* save the pre-computed values  */
149     *rp = r;
150     *kinvp = k;
151     ret = 1;
152  err:
153     if (!ret) {
154         BN_clear_free(k);
155         BN_clear_free(r);
156     }
157     if (ctx != ctx_in)
158         BN_CTX_free(ctx);
159     EC_POINT_free(tmp_point);
160     BN_clear_free(X);
161     return ret;
162 }
163
164 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
165                           BIGNUM **rp)
166 {
167     return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
168 }
169
170 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
171                                const BIGNUM *in_kinv, const BIGNUM *in_r,
172                                EC_KEY *eckey)
173 {
174     int ok = 0, i;
175     BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL;
176     BIGNUM *blindm = NULL;
177     const BIGNUM *order, *ckinv;
178     BN_CTX *ctx = NULL;
179     const EC_GROUP *group;
180     ECDSA_SIG *ret;
181     const BIGNUM *priv_key;
182
183     group = EC_KEY_get0_group(eckey);
184     priv_key = EC_KEY_get0_private_key(eckey);
185
186     if (group == NULL || priv_key == NULL) {
187         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
188         return NULL;
189     }
190
191     if (!EC_KEY_can_sign(eckey)) {
192         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
193         return NULL;
194     }
195
196     ret = ECDSA_SIG_new();
197     if (ret == NULL) {
198         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
199         return NULL;
200     }
201     ret->r = BN_new();
202     ret->s = BN_new();
203     if (ret->r == NULL || ret->s == NULL) {
204         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
205         goto err;
206     }
207     s = ret->s;
208
209     ctx = BN_CTX_secure_new();
210     if (ctx == NULL) {
211         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
212         goto err;
213     }
214
215     BN_CTX_start(ctx);
216     tmp = BN_CTX_get(ctx);
217     m = BN_CTX_get(ctx);
218     blind = BN_CTX_get(ctx);
219     blindm = BN_CTX_get(ctx);
220     if (blindm == NULL) {
221         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
222         goto err;
223     }
224
225     order = EC_GROUP_get0_order(group);
226     if (order == NULL) {
227         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
228         goto err;
229     }
230     i = BN_num_bits(order);
231     /*
232      * Need to truncate digest if it is too long: first truncate whole bytes.
233      */
234     if (8 * dgst_len > i)
235         dgst_len = (i + 7) / 8;
236     if (!BN_bin2bn(dgst, dgst_len, m)) {
237         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
238         goto err;
239     }
240     /* If still too long truncate remaining bits with a shift */
241     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
242         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
243         goto err;
244     }
245     do {
246         if (in_kinv == NULL || in_r == NULL) {
247             if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
248                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
249                 goto err;
250             }
251             ckinv = kinv;
252         } else {
253             ckinv = in_kinv;
254             if (BN_copy(ret->r, in_r) == NULL) {
255                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
256                 goto err;
257             }
258         }
259
260         /*
261          * The normal signature calculation is:
262          *
263          *   s := k^-1 * (m + r * priv_key) mod order
264          *
265          * We will blind this to protect against side channel attacks
266          *
267          *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod order
268          */
269
270         /* Generate a blinding value */
271         do {
272             if (!BN_priv_rand(blind, BN_num_bits(order) - 1,
273                               BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
274                 goto err;
275         } while (BN_is_zero(blind));
276         BN_set_flags(blind, BN_FLG_CONSTTIME);
277         BN_set_flags(blindm, BN_FLG_CONSTTIME);
278         BN_set_flags(tmp, BN_FLG_CONSTTIME);
279
280         /* tmp := blind * priv_key * r mod order */
281         if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
282             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
283             goto err;
284         }
285         if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
286             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
287             goto err;
288         }
289
290         /* blindm := blind * m mod order */
291         if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
292             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
293             goto err;
294         }
295
296         /* s : = (blind * priv_key * r) + (blind * m) mod order */
297         if (!BN_mod_add_quick(s, tmp, blindm, order)) {
298             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
299             goto err;
300         }
301
302         /* s := s * k^-1 mod order */
303         if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
304             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
305             goto err;
306         }
307
308         /* s:= s * blind^-1 mod order */
309         if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
310             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
311             goto err;
312         }
313         if (!BN_mod_mul(s, s, blind, order, ctx)) {
314             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
315             goto err;
316         }
317
318         if (BN_is_zero(s)) {
319             /*
320              * if kinv and r have been supplied by the caller, don't
321              * generate new kinv and r values
322              */
323             if (in_kinv != NULL && in_r != NULL) {
324                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
325                 goto err;
326             }
327         } else
328             /* s != 0 => we have a valid signature */
329             break;
330     }
331     while (1);
332
333     ok = 1;
334  err:
335     if (!ok) {
336         ECDSA_SIG_free(ret);
337         ret = NULL;
338     }
339     BN_CTX_end(ctx);
340     BN_CTX_free(ctx);
341     BN_clear_free(kinv);
342     return ret;
343 }
344
345 /*-
346  * returns
347  *      1: correct signature
348  *      0: incorrect signature
349  *     -1: error
350  */
351 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
352                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
353 {
354     ECDSA_SIG *s;
355     const unsigned char *p = sigbuf;
356     unsigned char *der = NULL;
357     int derlen = -1;
358     int ret = -1;
359
360     s = ECDSA_SIG_new();
361     if (s == NULL)
362         return ret;
363     if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
364         goto err;
365     /* Ensure signature uses DER and doesn't have trailing garbage */
366     derlen = i2d_ECDSA_SIG(s, &der);
367     if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
368         goto err;
369     ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
370  err:
371     OPENSSL_clear_free(der, derlen);
372     ECDSA_SIG_free(s);
373     return ret;
374 }
375
376 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
377                           const ECDSA_SIG *sig, EC_KEY *eckey)
378 {
379     int ret = -1, i;
380     BN_CTX *ctx;
381     const BIGNUM *order;
382     BIGNUM *u1, *u2, *m, *X;
383     EC_POINT *point = NULL;
384     const EC_GROUP *group;
385     const EC_POINT *pub_key;
386
387     /* check input values */
388     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
389         (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
390         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
391         return -1;
392     }
393
394     if (!EC_KEY_can_sign(eckey)) {
395         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
396         return -1;
397     }
398
399     ctx = BN_CTX_new();
400     if (ctx == NULL) {
401         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
402         return -1;
403     }
404     BN_CTX_start(ctx);
405     u1 = BN_CTX_get(ctx);
406     u2 = BN_CTX_get(ctx);
407     m = BN_CTX_get(ctx);
408     X = BN_CTX_get(ctx);
409     if (X == NULL) {
410         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
411         goto err;
412     }
413
414     order = EC_GROUP_get0_order(group);
415     if (order == NULL) {
416         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
417         goto err;
418     }
419
420     if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
421         BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
422         BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
423         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
424         ret = 0;                /* signature is invalid */
425         goto err;
426     }
427     /* calculate tmp1 = inv(S) mod order */
428     if (!EC_GROUP_do_inverse_ord(group, u2, sig->s, ctx)) {
429         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
430         goto err;
431     }
432     /* digest -> m */
433     i = BN_num_bits(order);
434     /*
435      * Need to truncate digest if it is too long: first truncate whole bytes.
436      */
437     if (8 * dgst_len > i)
438         dgst_len = (i + 7) / 8;
439     if (!BN_bin2bn(dgst, dgst_len, m)) {
440         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
441         goto err;
442     }
443     /* If still too long truncate remaining bits with a shift */
444     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
445         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
446         goto err;
447     }
448     /* u1 = m * tmp mod order */
449     if (!BN_mod_mul(u1, m, u2, order, ctx)) {
450         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
451         goto err;
452     }
453     /* u2 = r * w mod q */
454     if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
455         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
456         goto err;
457     }
458
459     if ((point = EC_POINT_new(group)) == NULL) {
460         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
461         goto err;
462     }
463     if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
464         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
465         goto err;
466     }
467     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
468         NID_X9_62_prime_field) {
469         if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
470             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
471             goto err;
472         }
473     }
474 #ifndef OPENSSL_NO_EC2M
475     else {                      /* NID_X9_62_characteristic_two_field */
476
477         if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
478             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
479             goto err;
480         }
481     }
482 #endif
483     if (!BN_nnmod(u1, X, order, ctx)) {
484         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
485         goto err;
486     }
487     /*  if the signature is correct u1 is equal to sig->r */
488     ret = (BN_ucmp(u1, sig->r) == 0);
489  err:
490     BN_CTX_end(ctx);
491     BN_CTX_free(ctx);
492     EC_POINT_free(point);
493     return ret;
494 }