[crypto/ec] don't assume points are of order group->order
[openssl.git] / crypto / ec / ecdsa_ossl.c
1 /*
2  * Copyright 2002-2017 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     RAND_seed(dgst, dlen);
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_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         /*
109          * We do not want timing information to leak the length of k, so we
110          * compute G*k using an equivalent scalar of fixed bit-length.
111          *
112          * We unconditionally perform both of these additions to prevent a
113          * small timing information leakage.  We then choose the sum that is
114          * one bit longer than the order.  This guarantees the code
115          * path used in the constant time implementations elsewhere.
116          *
117          * TODO: revisit the BN_copy aiming for a memory access agnostic
118          * conditional copy.
119          */
120         if (!BN_add(r, k, order)
121             || !BN_add(X, r, order)
122             || !BN_copy(k, BN_num_bits(r) > order_bits ? r : X))
123             goto err;
124
125         /* compute r the x-coordinate of generator * k */
126         if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
127             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
128             goto err;
129         }
130         if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
131             NID_X9_62_prime_field) {
132             if (!EC_POINT_get_affine_coordinates_GFp
133                 (group, tmp_point, X, NULL, ctx)) {
134                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
135                 goto err;
136             }
137         }
138 #ifndef OPENSSL_NO_EC2M
139         else {                  /* NID_X9_62_characteristic_two_field */
140
141             if (!EC_POINT_get_affine_coordinates_GF2m(group,
142                                                       tmp_point, X, NULL,
143                                                       ctx)) {
144                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
145                 goto err;
146             }
147         }
148 #endif
149         if (!BN_nnmod(r, X, order, ctx)) {
150             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
151             goto err;
152         }
153     }
154     while (BN_is_zero(r));
155
156     /* compute the inverse of k */
157     if (EC_GROUP_get_mont_data(group) != NULL) {
158         /*
159          * We want inverse in constant time, therefore we utilize the fact
160          * order must be prime and use Fermats Little Theorem instead.
161          */
162         if (!BN_set_word(X, 2)) {
163             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
164             goto err;
165         }
166         if (!BN_mod_sub(X, order, X, order, ctx)) {
167             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
168             goto err;
169         }
170         BN_set_flags(X, BN_FLG_CONSTTIME);
171         if (!BN_mod_exp_mont_consttime
172             (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
173             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
174             goto err;
175         }
176     } else {
177         if (!BN_mod_inverse(k, k, order, ctx)) {
178             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
179             goto err;
180         }
181     }
182
183     /* clear old values if necessary */
184     BN_clear_free(*rp);
185     BN_clear_free(*kinvp);
186     /* save the pre-computed values  */
187     *rp = r;
188     *kinvp = k;
189     ret = 1;
190  err:
191     if (!ret) {
192         BN_clear_free(k);
193         BN_clear_free(r);
194     }
195     if (ctx != ctx_in)
196         BN_CTX_free(ctx);
197     EC_POINT_free(tmp_point);
198     BN_clear_free(X);
199     return (ret);
200 }
201
202 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
203                           BIGNUM **rp)
204 {
205     return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
206 }
207
208 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
209                                const BIGNUM *in_kinv, const BIGNUM *in_r,
210                                EC_KEY *eckey)
211 {
212     int ok = 0, i;
213     BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL;
214     BIGNUM *blindm = NULL;
215     const BIGNUM *order, *ckinv;
216     BN_CTX *ctx = NULL;
217     const EC_GROUP *group;
218     ECDSA_SIG *ret;
219     const BIGNUM *priv_key;
220
221     group = EC_KEY_get0_group(eckey);
222     priv_key = EC_KEY_get0_private_key(eckey);
223
224     if (group == NULL || priv_key == NULL) {
225         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
226         return NULL;
227     }
228
229     if (!EC_KEY_can_sign(eckey)) {
230         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
231         return NULL;
232     }
233
234     ret = ECDSA_SIG_new();
235     if (ret == NULL) {
236         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
237         return NULL;
238     }
239     ret->r = BN_new();
240     ret->s = BN_new();
241     if (ret->r == NULL || ret->s == NULL) {
242         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
243         goto err;
244     }
245     s = ret->s;
246
247     ctx = BN_CTX_secure_new();
248     if (ctx == NULL) {
249         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
250         goto err;
251     }
252
253     BN_CTX_start(ctx);
254     tmp = BN_CTX_get(ctx);
255     m = BN_CTX_get(ctx);
256     blind = BN_CTX_get(ctx);
257     blindm = BN_CTX_get(ctx);
258     if (blindm == NULL) {
259         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
260         goto err;
261     }
262
263     order = EC_GROUP_get0_order(group);
264     if (order == NULL) {
265         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
266         goto err;
267     }
268     i = BN_num_bits(order);
269     /*
270      * Need to truncate digest if it is too long: first truncate whole bytes.
271      */
272     if (8 * dgst_len > i)
273         dgst_len = (i + 7) / 8;
274     if (!BN_bin2bn(dgst, dgst_len, m)) {
275         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
276         goto err;
277     }
278     /* If still too long truncate remaining bits with a shift */
279     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
280         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
281         goto err;
282     }
283     do {
284         if (in_kinv == NULL || in_r == NULL) {
285             if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
286                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
287                 goto err;
288             }
289             ckinv = kinv;
290         } else {
291             ckinv = in_kinv;
292             if (BN_copy(ret->r, in_r) == NULL) {
293                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
294                 goto err;
295             }
296         }
297
298         /*
299          * The normal signature calculation is:
300          *
301          *   s := k^-1 * (m + r * priv_key) mod order
302          *
303          * We will blind this to protect against side channel attacks
304          *
305          *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod order
306          */
307
308         /* Generate a blinding value */
309         do {
310             if (!BN_rand(blind, BN_num_bits(order) - 1, BN_RAND_TOP_ANY,
311                          BN_RAND_BOTTOM_ANY))
312                 goto err;
313         } while (BN_is_zero(blind));
314         BN_set_flags(blind, BN_FLG_CONSTTIME);
315         BN_set_flags(blindm, BN_FLG_CONSTTIME);
316         BN_set_flags(tmp, BN_FLG_CONSTTIME);
317
318         /* tmp := blind * priv_key * r mod order */
319         if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
320             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
321             goto err;
322         }
323         if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
324             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
325             goto err;
326         }
327
328         /* blindm := blind * m mod order */
329         if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
330             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
331             goto err;
332         }
333
334         /* s : = (blind * priv_key * r) + (blind * m) mod order */
335         if (!BN_mod_add_quick(s, tmp, blindm, order)) {
336             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
337             goto err;
338         }
339
340         /* s := s * k^-1 mod order */
341         if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
342             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
343             goto err;
344         }
345
346         /* s:= s * blind^-1 mod order */
347         if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
348             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
349             goto err;
350         }
351         if (!BN_mod_mul(s, s, blind, order, ctx)) {
352             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
353             goto err;
354         }
355
356         if (BN_is_zero(s)) {
357             /*
358              * if kinv and r have been supplied by the caller don't to
359              * generate new kinv and r values
360              */
361             if (in_kinv != NULL && in_r != NULL) {
362                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
363                 goto err;
364             }
365         } else
366             /* s != 0 => we have a valid signature */
367             break;
368     }
369     while (1);
370
371     ok = 1;
372  err:
373     if (!ok) {
374         ECDSA_SIG_free(ret);
375         ret = NULL;
376     }
377     BN_CTX_end(ctx);
378     BN_CTX_free(ctx);
379     BN_clear_free(kinv);
380     return ret;
381 }
382
383 /*-
384  * returns
385  *      1: correct signature
386  *      0: incorrect signature
387  *     -1: error
388  */
389 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
390                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
391 {
392     ECDSA_SIG *s;
393     const unsigned char *p = sigbuf;
394     unsigned char *der = NULL;
395     int derlen = -1;
396     int ret = -1;
397
398     s = ECDSA_SIG_new();
399     if (s == NULL)
400         return (ret);
401     if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
402         goto err;
403     /* Ensure signature uses DER and doesn't have trailing garbage */
404     derlen = i2d_ECDSA_SIG(s, &der);
405     if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
406         goto err;
407     ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
408  err:
409     OPENSSL_clear_free(der, derlen);
410     ECDSA_SIG_free(s);
411     return (ret);
412 }
413
414 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
415                           const ECDSA_SIG *sig, EC_KEY *eckey)
416 {
417     int ret = -1, i;
418     BN_CTX *ctx;
419     const BIGNUM *order;
420     BIGNUM *u1, *u2, *m, *X;
421     EC_POINT *point = NULL;
422     const EC_GROUP *group;
423     const EC_POINT *pub_key;
424
425     /* check input values */
426     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
427         (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
428         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
429         return -1;
430     }
431
432     if (!EC_KEY_can_sign(eckey)) {
433         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
434         return -1;
435     }
436
437     ctx = BN_CTX_new();
438     if (ctx == NULL) {
439         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
440         return -1;
441     }
442     BN_CTX_start(ctx);
443     u1 = BN_CTX_get(ctx);
444     u2 = BN_CTX_get(ctx);
445     m = BN_CTX_get(ctx);
446     X = BN_CTX_get(ctx);
447     if (X == NULL) {
448         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
449         goto err;
450     }
451
452     order = EC_GROUP_get0_order(group);
453     if (order == NULL) {
454         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
455         goto err;
456     }
457
458     if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
459         BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
460         BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
461         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
462         ret = 0;                /* signature is invalid */
463         goto err;
464     }
465     /* calculate tmp1 = inv(S) mod order */
466     if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
467         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
468         goto err;
469     }
470     /* digest -> m */
471     i = BN_num_bits(order);
472     /*
473      * Need to truncate digest if it is too long: first truncate whole bytes.
474      */
475     if (8 * dgst_len > i)
476         dgst_len = (i + 7) / 8;
477     if (!BN_bin2bn(dgst, dgst_len, m)) {
478         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
479         goto err;
480     }
481     /* If still too long truncate remaining bits with a shift */
482     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
483         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
484         goto err;
485     }
486     /* u1 = m * tmp mod order */
487     if (!BN_mod_mul(u1, m, u2, order, ctx)) {
488         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
489         goto err;
490     }
491     /* u2 = r * w mod q */
492     if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
493         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
494         goto err;
495     }
496
497     if ((point = EC_POINT_new(group)) == NULL) {
498         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
499         goto err;
500     }
501     if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
502         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
503         goto err;
504     }
505     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
506         NID_X9_62_prime_field) {
507         if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
508             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
509             goto err;
510         }
511     }
512 #ifndef OPENSSL_NO_EC2M
513     else {                      /* NID_X9_62_characteristic_two_field */
514
515         if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
516             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
517             goto err;
518         }
519     }
520 #endif
521     if (!BN_nnmod(u1, X, order, ctx)) {
522         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
523         goto err;
524     }
525     /*  if the signature is correct u1 is equal to sig->r */
526     ret = (BN_ucmp(u1, sig->r) == 0);
527  err:
528     BN_CTX_end(ctx);
529     BN_CTX_free(ctx);
530     EC_POINT_free(point);
531     return ret;
532 }