Remove OPENSSL_FIPSCANISTER code.
[openssl.git] / crypto / ecdsa / ecs_ossl.c
1 /* crypto/ecdsa/ecs_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 #define OPENSSL_FIPSAPI
60
61 #include "ecs_locl.h"
62 #include <openssl/err.h>
63 #include <openssl/obj_mac.h>
64 #include <openssl/bn.h>
65 #include <openssl/rand.h>
66
67 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 
68                 const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
69 static int ecdsa_sign_setup_no_digest(EC_KEY *eckey,
70                 BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
71 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
72                                         BIGNUM **kinvp, BIGNUM **rp,
73                                         const unsigned char *dgst, int dlen);
74 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, 
75                 const ECDSA_SIG *sig, EC_KEY *eckey);
76
77 static ECDSA_METHOD openssl_ecdsa_meth = {
78         "OpenSSL ECDSA method",
79         ecdsa_do_sign,
80         ecdsa_sign_setup_no_digest,
81         ecdsa_do_verify,
82 #if 0
83         NULL, /* init     */
84         NULL, /* finish   */
85 #endif
86         ECDSA_FLAG_FIPS_METHOD,    /* flags    */
87         NULL  /* app_data */
88 };
89
90 const ECDSA_METHOD *ECDSA_OpenSSL(void)
91 {
92         return &openssl_ecdsa_meth;
93 }
94
95 static int ecdsa_sign_setup_no_digest(EC_KEY *eckey,
96                 BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) {
97         return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
98 }
99
100 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
101                                         BIGNUM **kinvp, BIGNUM **rp,
102                                         const unsigned char *dgst, int dlen)
103 {
104         BN_CTX   *ctx = NULL;
105         BIGNUM   *k = NULL, *r = NULL, *order = NULL, *X = NULL;
106         EC_POINT *tmp_point=NULL;
107         const EC_GROUP *group;
108         int      ret = 0;
109
110         if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
111         {
112                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
113                 return 0;
114         }
115
116         if (ctx_in == NULL) 
117         {
118                 if ((ctx = BN_CTX_new()) == NULL)
119                 {
120                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
121                         return 0;
122                 }
123         }
124         else
125                 ctx = ctx_in;
126
127         k     = BN_new();       /* this value is later returned in *kinvp */
128         r     = BN_new();       /* this value is later returned in *rp    */
129         order = BN_new();
130         X     = BN_new();
131         if (!k || !r || !order || !X)
132         {
133                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
134                 goto err;
135         }
136         if ((tmp_point = EC_POINT_new(group)) == NULL)
137         {
138                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
139                 goto err;
140         }
141         if (!EC_GROUP_get_order(group, order, ctx))
142         {
143                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
144                 goto err;
145         }
146
147 #ifdef OPENSSL_FIPS
148         if (!fips_check_ec_prng(eckey))
149                 goto err;
150 #endif
151         
152         do
153         {
154                 /* get random k */      
155                 do
156 #ifndef OPENSSL_NO_SHA512
157                         if (dgst != NULL)
158                         {
159                                 if (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
160                                                            dgst, dlen, ctx))
161                                         {
162                                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
163                                                  ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
164                                         goto err;
165                                         }
166                         }
167                         else
168 #endif
169                         {
170                                 if (!BN_rand_range(k, order))
171                                 {
172                                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
173                                                  ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
174                                         goto err;
175                                 }
176                         }
177                 while (BN_is_zero(k));
178
179                 /* We do not want timing information to leak the length of k,
180                  * so we compute G*k using an equivalent scalar of fixed
181                  * bit-length. */
182
183                 if (!BN_add(k, k, order)) goto err;
184                 if (BN_num_bits(k) <= BN_num_bits(order))
185                         if (!BN_add(k, k, order)) goto err;
186
187                 /* compute r the x-coordinate of generator * k */
188                 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))
189                 {
190                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
191                         goto err;
192                 }
193                 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
194                 {
195                         if (!EC_POINT_get_affine_coordinates_GFp(group,
196                                 tmp_point, X, NULL, ctx))
197                         {
198                                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
199                                 goto err;
200                         }
201                 }
202 #ifndef OPENSSL_NO_EC2M
203                 else /* NID_X9_62_characteristic_two_field */
204                 {
205                         if (!EC_POINT_get_affine_coordinates_GF2m(group,
206                                 tmp_point, X, NULL, ctx))
207                         {
208                                 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
209                                 goto err;
210                         }
211                 }
212 #endif
213                 if (!BN_nnmod(r, X, order, ctx))
214                 {
215                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
216                         goto err;
217                 }
218         }
219         while (BN_is_zero(r));
220
221         /* compute the inverse of k */
222         if (EC_GROUP_get_mont_data(group) != NULL)
223                 {
224                 /* We want inverse in constant time, therefore we utilize the
225                  * fact order must be prime and use Fermats Little Theorem
226                  * instead. */
227                 if (!BN_set_word(X, 2) )
228                         {
229                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
230                         goto err;
231                         }
232                 if (!BN_mod_sub(X, order, X, order, ctx))
233                         {
234                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
235                         goto err;
236                         }
237                 BN_set_flags(X, BN_FLG_CONSTTIME);
238                 if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx, EC_GROUP_get_mont_data(group)))
239                         {
240                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
241                         goto err;
242                         }
243                 }
244         else
245                 {
246                 if (!BN_mod_inverse(k, k, order, ctx))
247                         {
248                         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
249                         goto err;       
250                         }
251                 }
252
253         /* clear old values if necessary */
254         if (*rp != NULL)
255                 BN_clear_free(*rp);
256         if (*kinvp != NULL) 
257                 BN_clear_free(*kinvp);
258         /* save the pre-computed values  */
259         *rp    = r;
260         *kinvp = k;
261         ret = 1;
262 err:
263         if (!ret)
264         {
265                 if (k != NULL) BN_clear_free(k);
266                 if (r != NULL) BN_clear_free(r);
267         }
268         if (ctx_in == NULL) 
269                 BN_CTX_free(ctx);
270         if (order != NULL)
271                 BN_free(order);
272         if (tmp_point != NULL) 
273                 EC_POINT_free(tmp_point);
274         if (X)
275                 BN_clear_free(X);
276         return(ret);
277 }
278
279
280 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len, 
281                 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
282 {
283         int     ok = 0, i;
284         BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
285         const BIGNUM *ckinv;
286         BN_CTX     *ctx = NULL;
287         const EC_GROUP   *group;
288         ECDSA_SIG  *ret;
289         ECDSA_DATA *ecdsa;
290         const BIGNUM *priv_key;
291
292 #ifdef OPENSSL_FIPS
293         if(FIPS_selftest_failed())
294                 {
295                 FIPSerr(FIPS_F_ECDSA_DO_SIGN,FIPS_R_FIPS_SELFTEST_FAILED);
296                 return NULL;
297                 }
298 #endif
299
300         ecdsa    = ecdsa_check(eckey);
301         group    = EC_KEY_get0_group(eckey);
302         priv_key = EC_KEY_get0_private_key(eckey);
303         
304         if (group == NULL || priv_key == NULL || ecdsa == NULL)
305         {
306                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
307                 return NULL;
308         }
309
310 #ifdef OPENSSL_FIPS
311         if (!fips_check_ec_prng(eckey))
312                 return NULL;
313 #endif
314
315         ret = ECDSA_SIG_new();
316         if (!ret)
317         {
318                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
319                 return NULL;
320         }
321         s = ret->s;
322
323         if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
324                 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL)
325         {
326                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
327                 goto err;
328         }
329
330         if (!EC_GROUP_get_order(group, order, ctx))
331         {
332                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
333                 goto err;
334         }
335         i = BN_num_bits(order);
336         /* Need to truncate digest if it is too long: first truncate whole
337          * bytes.
338          */
339         if (8 * dgst_len > i)
340                 dgst_len = (i + 7)/8;
341         if (!BN_bin2bn(dgst, dgst_len, m))
342         {
343                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
344                 goto err;
345         }
346         /* If still too long truncate remaining bits with a shift */
347         if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
348         {
349                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
350                 goto err;
351         }
352         do
353         {
354                 if (in_kinv == NULL || in_r == NULL)
355                 {
356                         if (!ecdsa_sign_setup(
357                                 eckey, ctx, &kinv, &ret->r, dgst, dgst_len))
358                         {
359                                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
360                                 goto err;
361                         }
362                         ckinv = kinv;
363                 }
364                 else
365                 {
366                         ckinv  = in_kinv;
367                         if (BN_copy(ret->r, in_r) == NULL)
368                         {
369                                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
370                                 goto err;
371                         }
372                 }
373
374                 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
375                 {
376                         ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
377                         goto err;
378                 }
379                 if (!BN_mod_add_quick(s, tmp, m, order))
380                 {
381                         ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
382                         goto err;
383                 }
384                 if (!BN_mod_mul(s, s, ckinv, order, ctx))
385                 {
386                         ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
387                         goto err;
388                 }
389                 if (BN_is_zero(s))
390                 {
391                         /* if kinv and r have been supplied by the caller
392                          * don't to generate new kinv and r values */
393                         if (in_kinv != NULL && in_r != NULL)
394                         {
395                                 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
396                                 goto err;
397                         }
398                 }
399                 else
400                         /* s != 0 => we have a valid signature */
401                         break;
402         }
403         while (1);
404
405         ok = 1;
406 err:
407         if (!ok)
408         {
409                 ECDSA_SIG_free(ret);
410                 ret = NULL;
411         }
412         if (ctx)
413                 BN_CTX_free(ctx);
414         if (m)
415                 BN_clear_free(m);
416         if (tmp)
417                 BN_clear_free(tmp);
418         if (order)
419                 BN_free(order);
420         if (kinv)
421                 BN_clear_free(kinv);
422         return ret;
423 }
424
425 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
426                 const ECDSA_SIG *sig, EC_KEY *eckey)
427 {
428         int ret = -1, i;
429         BN_CTX   *ctx;
430         BIGNUM   *order, *u1, *u2, *m, *X;
431         EC_POINT *point = NULL;
432         const EC_GROUP *group;
433         const EC_POINT *pub_key;
434
435 #ifdef OPENSSL_FIPS
436         if(FIPS_selftest_failed())
437                 {
438                 FIPSerr(FIPS_F_ECDSA_DO_VERIFY,FIPS_R_FIPS_SELFTEST_FAILED);
439                 return -1;
440                 }
441 #endif
442
443         /* check input values */
444         if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
445             (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
446         {
447                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
448                 return -1;
449         }
450
451         ctx = BN_CTX_new();
452         if (!ctx)
453         {
454                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
455                 return -1;
456         }
457         BN_CTX_start(ctx);
458         order = BN_CTX_get(ctx);        
459         u1    = BN_CTX_get(ctx);
460         u2    = BN_CTX_get(ctx);
461         m     = BN_CTX_get(ctx);
462         X     = BN_CTX_get(ctx);
463         if (!X)
464         {
465                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
466                 goto err;
467         }
468         
469         if (!EC_GROUP_get_order(group, order, ctx))
470         {
471                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
472                 goto err;
473         }
474
475         if (BN_is_zero(sig->r)          || BN_is_negative(sig->r) || 
476             BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s)  ||
477             BN_is_negative(sig->s)      || BN_ucmp(sig->s, order) >= 0)
478         {
479                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
480                 ret = 0;        /* signature is invalid */
481                 goto err;
482         }
483         /* calculate tmp1 = inv(S) mod order */
484         if (!BN_mod_inverse(u2, sig->s, order, ctx))
485         {
486                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
487                 goto err;
488         }
489         /* digest -> m */
490         i = BN_num_bits(order);
491         /* Need to truncate digest if it is too long: first truncate whole
492          * bytes.
493          */
494         if (8 * dgst_len > i)
495                 dgst_len = (i + 7)/8;
496         if (!BN_bin2bn(dgst, dgst_len, m))
497         {
498                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
499                 goto err;
500         }
501         /* If still too long truncate remaining bits with a shift */
502         if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
503         {
504                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
505                 goto err;
506         }
507         /* u1 = m * tmp mod order */
508         if (!BN_mod_mul(u1, m, u2, order, ctx))
509         {
510                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
511                 goto err;
512         }
513         /* u2 = r * w mod q */
514         if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
515         {
516                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
517                 goto err;
518         }
519
520         if ((point = EC_POINT_new(group)) == NULL)
521         {
522                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
523                 goto err;
524         }
525         if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))
526         {
527                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
528                 goto err;
529         }
530         if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
531         {
532                 if (!EC_POINT_get_affine_coordinates_GFp(group,
533                         point, X, NULL, ctx))
534                 {
535                         ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
536                         goto err;
537                 }
538         }
539 #ifndef OPENSSL_NO_EC2M
540         else /* NID_X9_62_characteristic_two_field */
541         {
542                 if (!EC_POINT_get_affine_coordinates_GF2m(group,
543                         point, X, NULL, ctx))
544                 {
545                         ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
546                         goto err;
547                 }
548         }
549 #endif  
550         if (!BN_nnmod(u1, X, order, ctx))
551         {
552                 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
553                 goto err;
554         }
555         /*  if the signature is correct u1 is equal to sig->r */
556         ret = (BN_ucmp(u1, sig->r) == 0);
557 err:
558         BN_CTX_end(ctx);
559         BN_CTX_free(ctx);
560         if (point)
561                 EC_POINT_free(point);
562         return ret;
563 }