ec/ecdsa_ossl.c: revert blinding in ECDSA signature.
[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;
176     const BIGNUM *order, *ckinv;
177     BN_CTX *ctx = NULL;
178     const EC_GROUP *group;
179     ECDSA_SIG *ret;
180     const BIGNUM *priv_key;
181
182     group = EC_KEY_get0_group(eckey);
183     priv_key = EC_KEY_get0_private_key(eckey);
184
185     if (group == NULL || priv_key == NULL) {
186         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
187         return NULL;
188     }
189
190     if (!EC_KEY_can_sign(eckey)) {
191         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
192         return NULL;
193     }
194
195     ret = ECDSA_SIG_new();
196     if (ret == NULL) {
197         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
198         return NULL;
199     }
200     ret->r = BN_new();
201     ret->s = BN_new();
202     if (ret->r == NULL || ret->s == NULL) {
203         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
204         goto err;
205     }
206     s = ret->s;
207
208     if ((ctx = BN_CTX_new()) == NULL ||
209         (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
210         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
211         goto err;
212     }
213
214     order = EC_GROUP_get0_order(group);
215     if (order == NULL) {
216         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
217         goto err;
218     }
219     i = BN_num_bits(order);
220     /*
221      * Need to truncate digest if it is too long: first truncate whole bytes.
222      */
223     if (8 * dgst_len > i)
224         dgst_len = (i + 7) / 8;
225     if (!BN_bin2bn(dgst, dgst_len, m)) {
226         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
227         goto err;
228     }
229     /* If still too long truncate remaining bits with a shift */
230     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
231         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
232         goto err;
233     }
234     do {
235         if (in_kinv == NULL || in_r == NULL) {
236             if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
237                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
238                 goto err;
239             }
240             ckinv = kinv;
241         } else {
242             ckinv = in_kinv;
243             if (BN_copy(ret->r, in_r) == NULL) {
244                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
245                 goto err;
246             }
247         }
248
249         if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
250             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
251             goto err;
252         }
253         if (!BN_mod_add_quick(s, tmp, m, order)) {
254             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
255             goto err;
256         }
257         if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
258             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
259             goto err;
260         }
261         if (BN_is_zero(s)) {
262             /*
263              * if kinv and r have been supplied by the caller, don't
264              * generate new kinv and r values
265              */
266             if (in_kinv != NULL && in_r != NULL) {
267                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
268                 goto err;
269             }
270         } else
271             /* s != 0 => we have a valid signature */
272             break;
273     }
274     while (1);
275
276     ok = 1;
277  err:
278     if (!ok) {
279         ECDSA_SIG_free(ret);
280         ret = NULL;
281     }
282     BN_CTX_free(ctx);
283     BN_clear_free(m);
284     BN_clear_free(tmp);
285     BN_clear_free(kinv);
286     return ret;
287 }
288
289 /*-
290  * returns
291  *      1: correct signature
292  *      0: incorrect signature
293  *     -1: error
294  */
295 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
296                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
297 {
298     ECDSA_SIG *s;
299     const unsigned char *p = sigbuf;
300     unsigned char *der = NULL;
301     int derlen = -1;
302     int ret = -1;
303
304     s = ECDSA_SIG_new();
305     if (s == NULL)
306         return ret;
307     if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
308         goto err;
309     /* Ensure signature uses DER and doesn't have trailing garbage */
310     derlen = i2d_ECDSA_SIG(s, &der);
311     if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
312         goto err;
313     ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
314  err:
315     OPENSSL_clear_free(der, derlen);
316     ECDSA_SIG_free(s);
317     return ret;
318 }
319
320 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
321                           const ECDSA_SIG *sig, EC_KEY *eckey)
322 {
323     int ret = -1, i;
324     BN_CTX *ctx;
325     const BIGNUM *order;
326     BIGNUM *u1, *u2, *m, *X;
327     EC_POINT *point = NULL;
328     const EC_GROUP *group;
329     const EC_POINT *pub_key;
330
331     /* check input values */
332     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
333         (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
334         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
335         return -1;
336     }
337
338     if (!EC_KEY_can_sign(eckey)) {
339         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
340         return -1;
341     }
342
343     ctx = BN_CTX_new();
344     if (ctx == NULL) {
345         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
346         return -1;
347     }
348     BN_CTX_start(ctx);
349     u1 = BN_CTX_get(ctx);
350     u2 = BN_CTX_get(ctx);
351     m = BN_CTX_get(ctx);
352     X = BN_CTX_get(ctx);
353     if (X == NULL) {
354         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
355         goto err;
356     }
357
358     order = EC_GROUP_get0_order(group);
359     if (order == NULL) {
360         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
361         goto err;
362     }
363
364     if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
365         BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
366         BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
367         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
368         ret = 0;                /* signature is invalid */
369         goto err;
370     }
371     /* calculate tmp1 = inv(S) mod order */
372     if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
373         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
374         goto err;
375     }
376     /* digest -> m */
377     i = BN_num_bits(order);
378     /*
379      * Need to truncate digest if it is too long: first truncate whole bytes.
380      */
381     if (8 * dgst_len > i)
382         dgst_len = (i + 7) / 8;
383     if (!BN_bin2bn(dgst, dgst_len, m)) {
384         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
385         goto err;
386     }
387     /* If still too long truncate remaining bits with a shift */
388     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
389         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
390         goto err;
391     }
392     /* u1 = m * tmp mod order */
393     if (!BN_mod_mul(u1, m, u2, order, ctx)) {
394         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
395         goto err;
396     }
397     /* u2 = r * w mod q */
398     if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
399         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
400         goto err;
401     }
402
403     if ((point = EC_POINT_new(group)) == NULL) {
404         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
405         goto err;
406     }
407     if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
408         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
409         goto err;
410     }
411     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
412         NID_X9_62_prime_field) {
413         if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
414             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
415             goto err;
416         }
417     }
418 #ifndef OPENSSL_NO_EC2M
419     else {                      /* NID_X9_62_characteristic_two_field */
420
421         if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
422             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
423             goto err;
424         }
425     }
426 #endif
427     if (!BN_nnmod(u1, X, order, ctx)) {
428         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
429         goto err;
430     }
431     /*  if the signature is correct u1 is equal to sig->r */
432     ret = (BN_ucmp(u1, sig->r) == 0);
433  err:
434     BN_CTX_end(ctx);
435     BN_CTX_free(ctx);
436     EC_POINT_free(point);
437     return ret;
438 }