adapt ossl_ecdsa.c to crypto/ec
[openssl.git] / crypto / ec / ecdsa_ossl.c
1 /* crypto/ec/ecdsa_ossl.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <openssl/err.h>
60 #include <openssl/obj_mac.h>
61 #include <openssl/bn.h>
62 #include <openssl/rand.h>
63 #include <openssl/ec.h>
64 #include "ec_lcl.h"
65
66 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
67                             BIGNUM **kinvp, BIGNUM **rp,
68                             const unsigned char *dgst, int dlen)
69 {
70     BN_CTX *ctx = NULL;
71     BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
72     EC_POINT *tmp_point = NULL;
73     const EC_GROUP *group;
74     int ret = 0;
75
76     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
77         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
78         return 0;
79     }
80
81     if (ctx_in == NULL) {
82         if ((ctx = BN_CTX_new()) == NULL) {
83             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
84             return 0;
85         }
86     } else
87         ctx = ctx_in;
88
89     k = BN_new();               /* this value is later returned in *kinvp */
90     r = BN_new();               /* this value is later returned in *rp */
91     order = BN_new();
92     X = BN_new();
93     if (k == NULL || r == NULL || order == NULL || X == NULL) {
94         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
95         goto err;
96     }
97     if ((tmp_point = EC_POINT_new(group)) == NULL) {
98         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
99         goto err;
100     }
101     if (!EC_GROUP_get_order(group, order, ctx)) {
102         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
103         goto err;
104     }
105
106     do {
107         /* get random k */
108         do
109             if (dgst != NULL) {
110                 if (!BN_generate_dsa_nonce
111                     (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
112                      ctx)) {
113                     ECerr(EC_F_ECDSA_SIGN_SETUP,
114                              EC_R_RANDOM_NUMBER_GENERATION_FAILED);
115                     goto err;
116                 }
117             } else {
118                 if (!BN_rand_range(k, order)) {
119                     ECerr(EC_F_ECDSA_SIGN_SETUP,
120                              EC_R_RANDOM_NUMBER_GENERATION_FAILED);
121                     goto err;
122                 }
123             }
124         while (BN_is_zero(k));
125
126         /*
127          * We do not want timing information to leak the length of k, so we
128          * compute G*k using an equivalent scalar of fixed bit-length.
129          */
130
131         if (!BN_add(k, k, order))
132             goto err;
133         if (BN_num_bits(k) <= BN_num_bits(order))
134             if (!BN_add(k, k, order))
135                 goto err;
136
137         /* compute r the x-coordinate of generator * k */
138         if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
139             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
140             goto err;
141         }
142         if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
143             NID_X9_62_prime_field) {
144             if (!EC_POINT_get_affine_coordinates_GFp
145                 (group, tmp_point, X, NULL, ctx)) {
146                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
147                 goto err;
148             }
149         }
150 #ifndef OPENSSL_NO_EC2M
151         else {                  /* NID_X9_62_characteristic_two_field */
152
153             if (!EC_POINT_get_affine_coordinates_GF2m(group,
154                                                       tmp_point, X, NULL,
155                                                       ctx)) {
156                 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
157                 goto err;
158             }
159         }
160 #endif
161         if (!BN_nnmod(r, X, order, ctx)) {
162             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
163             goto err;
164         }
165     }
166     while (BN_is_zero(r));
167
168     /* compute the inverse of k */
169     if (EC_GROUP_get_mont_data(group) != NULL) {
170         /*
171          * We want inverse in constant time, therefore we utilize the fact
172          * order must be prime and use Fermats Little Theorem instead.
173          */
174         if (!BN_set_word(X, 2)) {
175             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
176             goto err;
177         }
178         if (!BN_mod_sub(X, order, X, order, ctx)) {
179             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
180             goto err;
181         }
182         BN_set_flags(X, BN_FLG_CONSTTIME);
183         if (!BN_mod_exp_mont_consttime
184             (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
185             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
186             goto err;
187         }
188     } else {
189         if (!BN_mod_inverse(k, k, order, ctx)) {
190             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
191             goto err;
192         }
193     }
194
195     /* clear old values if necessary */
196     BN_clear_free(*rp);
197     BN_clear_free(*kinvp);
198     /* save the pre-computed values  */
199     *rp = r;
200     *kinvp = k;
201     ret = 1;
202  err:
203     if (!ret) {
204         BN_clear_free(k);
205         BN_clear_free(r);
206     }
207     if (ctx != ctx_in)
208         BN_CTX_free(ctx);
209     BN_free(order);
210     EC_POINT_free(tmp_point);
211     BN_clear_free(X);
212     return (ret);
213 }
214
215 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
216                           BIGNUM **rp)
217 {
218     return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
219 }
220
221 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
222                                const BIGNUM *in_kinv, const BIGNUM *in_r,
223                                EC_KEY *eckey)
224 {
225     int ok = 0, i;
226     BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
227     const BIGNUM *ckinv;
228     BN_CTX *ctx = NULL;
229     const EC_GROUP *group;
230     ECDSA_SIG *ret;
231     const BIGNUM *priv_key;
232
233     group = EC_KEY_get0_group(eckey);
234     priv_key = EC_KEY_get0_private_key(eckey);
235
236     if (group == NULL || priv_key == NULL) {
237         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
238         return NULL;
239     }
240
241     ret = ECDSA_SIG_new();
242     if (ret == NULL) {
243         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
244         return NULL;
245     }
246     s = ret->s;
247
248     if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
249         (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
250         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
251         goto err;
252     }
253
254     if (!EC_GROUP_get_order(group, order, ctx)) {
255         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
256         goto err;
257     }
258     i = BN_num_bits(order);
259     /*
260      * Need to truncate digest if it is too long: first truncate whole bytes.
261      */
262     if (8 * dgst_len > i)
263         dgst_len = (i + 7) / 8;
264     if (!BN_bin2bn(dgst, dgst_len, m)) {
265         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
266         goto err;
267     }
268     /* If still too long truncate remaining bits with a shift */
269     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
270         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
271         goto err;
272     }
273     do {
274         if (in_kinv == NULL || in_r == NULL) {
275             if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
276                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
277                 goto err;
278             }
279             ckinv = kinv;
280         } else {
281             ckinv = in_kinv;
282             if (BN_copy(ret->r, in_r) == NULL) {
283                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
284                 goto err;
285             }
286         }
287
288         if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
289             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
290             goto err;
291         }
292         if (!BN_mod_add_quick(s, tmp, m, order)) {
293             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
294             goto err;
295         }
296         if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
297             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
298             goto err;
299         }
300         if (BN_is_zero(s)) {
301             /*
302              * if kinv and r have been supplied by the caller don't to
303              * generate new kinv and r values
304              */
305             if (in_kinv != NULL && in_r != NULL) {
306                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
307                 goto err;
308             }
309         } else
310             /* s != 0 => we have a valid signature */
311             break;
312     }
313     while (1);
314
315     ok = 1;
316  err:
317     if (!ok) {
318         ECDSA_SIG_free(ret);
319         ret = NULL;
320     }
321     BN_CTX_free(ctx);
322     BN_clear_free(m);
323     BN_clear_free(tmp);
324     BN_free(order);
325     BN_clear_free(kinv);
326     return ret;
327 }
328
329 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
330                           const ECDSA_SIG *sig, EC_KEY *eckey)
331 {
332     int ret = -1, i;
333     BN_CTX *ctx;
334     BIGNUM *order, *u1, *u2, *m, *X;
335     EC_POINT *point = NULL;
336     const EC_GROUP *group;
337     const EC_POINT *pub_key;
338
339     /* check input values */
340     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
341         (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
342         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
343         return -1;
344     }
345
346     ctx = BN_CTX_new();
347     if (ctx == NULL) {
348         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
349         return -1;
350     }
351     BN_CTX_start(ctx);
352     order = BN_CTX_get(ctx);
353     u1 = BN_CTX_get(ctx);
354     u2 = BN_CTX_get(ctx);
355     m = BN_CTX_get(ctx);
356     X = BN_CTX_get(ctx);
357     if (!X) {
358         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
359         goto err;
360     }
361
362     if (!EC_GROUP_get_order(group, order, ctx)) {
363         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
364         goto err;
365     }
366
367     if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
368         BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
369         BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
370         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
371         ret = 0;                /* signature is invalid */
372         goto err;
373     }
374     /* calculate tmp1 = inv(S) mod order */
375     if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
376         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
377         goto err;
378     }
379     /* digest -> m */
380     i = BN_num_bits(order);
381     /*
382      * Need to truncate digest if it is too long: first truncate whole bytes.
383      */
384     if (8 * dgst_len > i)
385         dgst_len = (i + 7) / 8;
386     if (!BN_bin2bn(dgst, dgst_len, m)) {
387         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
388         goto err;
389     }
390     /* If still too long truncate remaining bits with a shift */
391     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
392         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
393         goto err;
394     }
395     /* u1 = m * tmp mod order */
396     if (!BN_mod_mul(u1, m, u2, order, ctx)) {
397         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
398         goto err;
399     }
400     /* u2 = r * w mod q */
401     if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
402         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
403         goto err;
404     }
405
406     if ((point = EC_POINT_new(group)) == NULL) {
407         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
408         goto err;
409     }
410     if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
411         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
412         goto err;
413     }
414     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
415         NID_X9_62_prime_field) {
416         if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
417             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
418             goto err;
419         }
420     }
421 #ifndef OPENSSL_NO_EC2M
422     else {                      /* NID_X9_62_characteristic_two_field */
423
424         if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
425             ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
426             goto err;
427         }
428     }
429 #endif
430     if (!BN_nnmod(u1, X, order, ctx)) {
431         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
432         goto err;
433     }
434     /*  if the signature is correct u1 is equal to sig->r */
435     ret = (BN_ucmp(u1, sig->r) == 0);
436  err:
437     BN_CTX_end(ctx);
438     BN_CTX_free(ctx);
439     EC_POINT_free(point);
440     return ret;
441 }