2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * The elliptic curve binary polynomial software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
24 #include <openssl/err.h>
27 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
28 const BIGNUM *b, BN_CTX *ctx)
30 const EC_METHOD *meth;
33 #if defined(OPENSSL_BN_ASM_MONT)
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
55 meth = EC_GFp_mont_method();
57 if (BN_nist_mod_func(p))
58 meth = EC_GFp_nist_method();
60 meth = EC_GFp_mont_method();
63 ret = EC_GROUP_new(meth);
67 if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx)) {
68 EC_GROUP_clear_free(ret);
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)
79 const EC_METHOD *meth;
82 meth = EC_GF2m_simple_method();
84 ret = EC_GROUP_new(meth);
88 if (!EC_GROUP_set_curve_GF2m(ret, p, a, b, ctx)) {
89 EC_GROUP_clear_free(ret);