SCA hardening for mod. field inversion in EC_GROUP
[openssl.git] / crypto / ec / ec_cvt.c
1 /*
2  * Copyright 2001-2016 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 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  *
13  * Portions of the attached software ("Contribution") are developed by
14  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15  *
16  * The Contribution is licensed pursuant to the OpenSSL open source
17  * license provided above.
18  *
19  * The elliptic curve binary polynomial software is originally written by
20  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
21  *
22  */
23
24 #include <openssl/err.h>
25 #include "ec_lcl.h"
26
27 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
28                                  const BIGNUM *b, BN_CTX *ctx)
29 {
30     const EC_METHOD *meth;
31     EC_GROUP *ret;
32
33 #if defined(OPENSSL_BN_ASM_MONT)
34     /*
35      * This might appear controversial, but the fact is that generic
36      * prime method was observed to deliver better performance even
37      * for NIST primes on a range of platforms, e.g.: 60%-15%
38      * improvement on IA-64, ~25% on ARM, 30%-90% on P4, 20%-25%
39      * in 32-bit build and 35%--12% in 64-bit build on Core2...
40      * Coefficients are relative to optimized bn_nist.c for most
41      * intensive ECDSA verify and ECDH operations for 192- and 521-
42      * bit keys respectively. Choice of these boundary values is
43      * arguable, because the dependency of improvement coefficient
44      * from key length is not a "monotone" curve. For example while
45      * 571-bit result is 23% on ARM, 384-bit one is -1%. But it's
46      * generally faster, sometimes "respectfully" faster, sometimes
47      * "tolerably" slower... What effectively happens is that loop
48      * with bn_mul_add_words is put against bn_mul_mont, and the
49      * latter "wins" on short vectors. Correct solution should be
50      * implementing dedicated NxN multiplication subroutines for
51      * small N. But till it materializes, let's stick to generic
52      * prime method...
53      *                                              <appro>
54      */
55     meth = EC_GFp_mont_method();
56 #else
57     if (BN_nist_mod_func(p))
58         meth = EC_GFp_nist_method();
59     else
60         meth = EC_GFp_mont_method();
61 #endif
62
63     ret = EC_GROUP_new(meth);
64     if (ret == NULL)
65         return NULL;
66
67     if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx)) {
68         EC_GROUP_clear_free(ret);
69         return NULL;
70     }
71
72     return ret;
73 }
74
75 #ifndef OPENSSL_NO_EC2M
76 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
77                                   const BIGNUM *b, BN_CTX *ctx)
78 {
79     const EC_METHOD *meth;
80     EC_GROUP *ret;
81
82     meth = EC_GF2m_simple_method();
83
84     ret = EC_GROUP_new(meth);
85     if (ret == NULL)
86         return NULL;
87
88     if (!EC_GROUP_set_curve_GF2m(ret, p, a, b, ctx)) {
89         EC_GROUP_clear_free(ret);
90         return NULL;
91     }
92
93     return ret;
94 }
95 #endif