ed25519-donna.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. Modified from the amd64-51-30k implementation by
  4. Daniel J. Bernstein
  5. Niels Duif
  6. Tanja Lange
  7. Peter Schwabe
  8. Bo-Yin Yang
  9. */
  10. #include "ed25519-donna-portable.h"
  11. #include "orconfig.h"
  12. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  13. /* Some of the ASM here is very long strings. */
  14. #ifdef __clang__
  15. #pragma clang diagnostic ignored "-Woverlength-strings"
  16. #else
  17. #pragma GCC diagnostic ignored "-Woverlength-strings"
  18. #endif
  19. #endif
  20. #if defined(ED25519_SSE2)
  21. #else
  22. #if defined(HAVE_UINT128) && !defined(ED25519_FORCE_32BIT)
  23. #define ED25519_64BIT
  24. #else
  25. #define ED25519_32BIT
  26. #endif
  27. #endif
  28. #if !defined(ED25519_NO_INLINE_ASM)
  29. /* detect extra features first so un-needed functions can be disabled throughout */
  30. #if defined(ED25519_SSE2)
  31. #if defined(COMPILER_GCC) && defined(CPU_X86)
  32. #define ED25519_GCC_32BIT_SSE_CHOOSE
  33. #elif defined(COMPILER_GCC) && defined(CPU_X86_64)
  34. #define ED25519_GCC_64BIT_SSE_CHOOSE
  35. #endif
  36. #else
  37. #if defined(CPU_X86_64)
  38. #if defined(COMPILER_GCC)
  39. #if defined(ED25519_64BIT)
  40. #define ED25519_GCC_64BIT_X86_CHOOSE
  41. #else
  42. #define ED25519_GCC_64BIT_32BIT_CHOOSE
  43. #endif
  44. #endif
  45. #endif
  46. #endif
  47. #endif
  48. #if defined(ED25519_SSE2)
  49. #include "curve25519-donna-sse2.h"
  50. #elif defined(ED25519_64BIT)
  51. #include "curve25519-donna-64bit.h"
  52. #else
  53. #include "curve25519-donna-32bit.h"
  54. #endif
  55. #include "curve25519-donna-helpers.h"
  56. /* separate uint128 check for 64 bit sse2 */
  57. #if defined(HAVE_UINT128) && !defined(ED25519_FORCE_32BIT)
  58. #include "modm-donna-64bit.h"
  59. #else
  60. #include "modm-donna-32bit.h"
  61. #endif
  62. typedef unsigned char hash_512bits[64];
  63. /*
  64. Timing safe memory compare
  65. */
  66. static int
  67. ed25519_verify(const unsigned char *x, const unsigned char *y, size_t len) {
  68. size_t differentbits = 0;
  69. while (len--)
  70. differentbits |= (*x++ ^ *y++);
  71. /*coverity[overflow]*/
  72. return (int) (1 & ((differentbits - 1) >> 8));
  73. }
  74. /*
  75. * Arithmetic on the twisted Edwards curve -x^2 + y^2 = 1 + dx^2y^2
  76. * with d = -(121665/121666) = 37095705934669439343138083508754565189542113879843219016388785533085940283555
  77. * Base point: (15112221349535400772501151409588531511454012693041857206046113283949847762202,46316835694926478169428394003475163141307993866256225615783033603165251855960);
  78. */
  79. typedef struct ge25519_t {
  80. bignum25519 x, y, z, t;
  81. } ge25519;
  82. typedef struct ge25519_p1p1_t {
  83. bignum25519 x, y, z, t;
  84. } ge25519_p1p1;
  85. typedef struct ge25519_niels_t {
  86. bignum25519 ysubx, xaddy, t2d;
  87. } ge25519_niels;
  88. typedef struct ge25519_pniels_t {
  89. bignum25519 ysubx, xaddy, z, t2d;
  90. } ge25519_pniels;
  91. #include "ed25519-donna-basepoint-table.h"
  92. #if defined(ED25519_64BIT)
  93. #include "ed25519-donna-64bit-tables.h"
  94. #include "ed25519-donna-64bit-x86.h"
  95. #else
  96. #include "ed25519-donna-32bit-tables.h"
  97. #include "ed25519-donna-64bit-x86-32bit.h"
  98. #endif
  99. #if defined(ED25519_SSE2)
  100. #include "ed25519-donna-32bit-sse2.h"
  101. #include "ed25519-donna-64bit-sse2.h"
  102. #include "ed25519-donna-impl-sse2.h"
  103. #else
  104. #include "ed25519-donna-impl-base.h"
  105. #endif