ec_cvt.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/err.h>
  11. #include "ec_local.h"
  12. EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
  13. const BIGNUM *b, BN_CTX *ctx)
  14. {
  15. const EC_METHOD *meth;
  16. EC_GROUP *ret;
  17. #if defined(OPENSSL_BN_ASM_MONT)
  18. /*
  19. * This might appear controversial, but the fact is that generic
  20. * prime method was observed to deliver better performance even
  21. * for NIST primes on a range of platforms, e.g.: 60%-15%
  22. * improvement on IA-64, ~25% on ARM, 30%-90% on P4, 20%-25%
  23. * in 32-bit build and 35%--12% in 64-bit build on Core2...
  24. * Coefficients are relative to optimized bn_nist.c for most
  25. * intensive ECDSA verify and ECDH operations for 192- and 521-
  26. * bit keys respectively. Choice of these boundary values is
  27. * arguable, because the dependency of improvement coefficient
  28. * from key length is not a "monotone" curve. For example while
  29. * 571-bit result is 23% on ARM, 384-bit one is -1%. But it's
  30. * generally faster, sometimes "respectfully" faster, sometimes
  31. * "tolerably" slower... What effectively happens is that loop
  32. * with bn_mul_add_words is put against bn_mul_mont, and the
  33. * latter "wins" on short vectors. Correct solution should be
  34. * implementing dedicated NxN multiplication subroutines for
  35. * small N. But till it materializes, let's stick to generic
  36. * prime method...
  37. * <appro>
  38. */
  39. meth = EC_GFp_mont_method();
  40. #else
  41. if (BN_nist_mod_func(p))
  42. meth = EC_GFp_nist_method();
  43. else
  44. meth = EC_GFp_mont_method();
  45. #endif
  46. ret = EC_GROUP_new(meth);
  47. if (ret == NULL)
  48. return NULL;
  49. if (!EC_GROUP_set_curve(ret, p, a, b, ctx)) {
  50. EC_GROUP_clear_free(ret);
  51. return NULL;
  52. }
  53. return ret;
  54. }
  55. #ifndef OPENSSL_NO_EC2M
  56. EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
  57. const BIGNUM *b, BN_CTX *ctx)
  58. {
  59. const EC_METHOD *meth;
  60. EC_GROUP *ret;
  61. meth = EC_GF2m_simple_method();
  62. ret = EC_GROUP_new(meth);
  63. if (ret == NULL)
  64. return NULL;
  65. if (!EC_GROUP_set_curve(ret, p, a, b, ctx)) {
  66. EC_GROUP_clear_free(ret);
  67. return NULL;
  68. }
  69. return ret;
  70. }
  71. #endif