p256-x86_64.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2014, Intel Corporation. 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. * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
  11. * (1) Intel Corporation, Israel Development Center, Haifa, Israel
  12. * (2) University of Haifa, Israel
  13. *
  14. * Reference:
  15. * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
  16. * 256 Bit Primes"
  17. */
  18. #ifndef OPENSSL_HEADER_EC_P256_X86_64_H
  19. #define OPENSSL_HEADER_EC_P256_X86_64_H
  20. #include <openssl/base.h>
  21. #include <openssl/bn.h>
  22. #if defined(__cplusplus)
  23. extern "C" {
  24. #endif
  25. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  26. !defined(OPENSSL_SMALL)
  27. // P-256 field operations.
  28. //
  29. // An element mod P in P-256 is represented as a little-endian array of
  30. // |P256_LIMBS| |BN_ULONG|s, spanning the full range of values.
  31. //
  32. // The following functions take fully-reduced inputs mod P and give
  33. // fully-reduced outputs. They may be used in-place.
  34. #define P256_LIMBS (256 / BN_BITS2)
  35. // ecp_nistz256_neg sets |res| to -|a| mod P.
  36. void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
  37. // ecp_nistz256_mul_mont sets |res| to |a| * |b| * 2^-256 mod P.
  38. void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
  39. const BN_ULONG a[P256_LIMBS],
  40. const BN_ULONG b[P256_LIMBS]);
  41. // ecp_nistz256_sqr_mont sets |res| to |a| * |a| * 2^-256 mod P.
  42. void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
  43. const BN_ULONG a[P256_LIMBS]);
  44. // ecp_nistz256_from_mont sets |res| to |in|, converted from Montgomery domain
  45. // by multiplying with 1.
  46. static inline void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
  47. const BN_ULONG in[P256_LIMBS]) {
  48. static const BN_ULONG ONE[P256_LIMBS] = { 1 };
  49. ecp_nistz256_mul_mont(res, in, ONE);
  50. }
  51. // P-256 point operations.
  52. //
  53. // The following functions may be used in-place. All coordinates are in the
  54. // Montgomery domain.
  55. // A P256_POINT represents a P-256 point in Jacobian coordinates.
  56. typedef struct {
  57. BN_ULONG X[P256_LIMBS];
  58. BN_ULONG Y[P256_LIMBS];
  59. BN_ULONG Z[P256_LIMBS];
  60. } P256_POINT;
  61. // A P256_POINT_AFFINE represents a P-256 point in affine coordinates. Infinity
  62. // is encoded as (0, 0).
  63. typedef struct {
  64. BN_ULONG X[P256_LIMBS];
  65. BN_ULONG Y[P256_LIMBS];
  66. } P256_POINT_AFFINE;
  67. // ecp_nistz256_select_w5 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 16
  68. // and all zeros (the point at infinity) if |index| is 0. This is done in
  69. // constant time.
  70. void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT in_t[16],
  71. int index);
  72. // ecp_nistz256_select_w7 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 64
  73. // and all zeros (the point at infinity) if |index| is 0. This is done in
  74. // constant time.
  75. void ecp_nistz256_select_w7(P256_POINT_AFFINE *val,
  76. const P256_POINT_AFFINE in_t[64], int index);
  77. // ecp_nistz256_point_double sets |r| to |a| doubled.
  78. void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
  79. // ecp_nistz256_point_add adds |a| to |b| and places the result in |r|.
  80. void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a,
  81. const P256_POINT *b);
  82. // ecp_nistz256_point_add_affine adds |a| to |b| and places the result in
  83. // |r|. |a| and |b| must not represent the same point unless they are both
  84. // infinity.
  85. void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a,
  86. const P256_POINT_AFFINE *b);
  87. #endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  88. !defined(OPENSSL_SMALL) */
  89. #if defined(__cplusplus)
  90. } // extern C++
  91. #endif
  92. #endif // OPENSSL_HEADER_EC_P256_X86_64_H