donna.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // donna.h - written and placed in public domain by Jeffrey Walton
  2. // Crypto++ specific implementation wrapped around Andrew
  3. // Moon's public domain curve25519-donna and ed25519-donna,
  4. // https://github.com/floodyberry/curve25519-donna and
  5. // https://github.com/floodyberry/ed25519-donna.
  6. // The curve25519 and ed25519 source files multiplex different repos and
  7. // architectures using namespaces. The repos are Andrew Moon's
  8. // curve25519-donna and ed25519-donna. The architectures are 32-bit, 64-bit
  9. // and SSE. For example, 32-bit x25519 uses symbols from Donna::X25519 and
  10. // Donna::Arch32.
  11. // If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
  12. // https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
  13. /// \file donna.h
  14. /// \details Functions for curve25519 and ed25519 operations
  15. /// \details This header provides the entry points into Andrew Moon's
  16. /// curve25519 and ed25519 curve functions. The Crypto++ classes x25519
  17. /// and ed25519 use the functions. The functions are in the <tt>Donna</tt>
  18. /// namespace and are curve25519_mult(), ed25519_publickey(),
  19. /// ed25519_sign() and ed25519_sign_open().
  20. /// \details At the moment the hash function for signing is fixed at
  21. /// SHA512.
  22. #ifndef CRYPTOPP_DONNA_H
  23. #define CRYPTOPP_DONNA_H
  24. #include "cryptlib.h"
  25. #include "stdcpp.h"
  26. NAMESPACE_BEGIN(CryptoPP)
  27. NAMESPACE_BEGIN(Donna)
  28. //***************************** curve25519 *****************************//
  29. /// \brief Generate a public key
  30. /// \param publicKey byte array for the public key
  31. /// \param secretKey byte array with the private key
  32. /// \return 0 on success, non-0 otherwise
  33. /// \details curve25519_mult() generates a public key from an existing
  34. /// secret key. Internally curve25519_mult() performs a scalar
  35. /// multiplication using the base point and writes the result to
  36. /// <tt>pubkey</tt>.
  37. int curve25519_mult(byte publicKey[32], const byte secretKey[32]);
  38. /// \brief Generate a shared key
  39. /// \param sharedKey byte array for the shared secret
  40. /// \param secretKey byte array with the private key
  41. /// \param othersKey byte array with the peer's public key
  42. /// \return 0 on success, non-0 otherwise
  43. /// \details curve25519_mult() generates a shared key from an existing
  44. /// secret key and the other party's public key. Internally
  45. /// curve25519_mult() performs a scalar multiplication using the two keys
  46. /// and writes the result to <tt>sharedKey</tt>.
  47. int curve25519_mult(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32]);
  48. //******************************* ed25519 *******************************//
  49. /// \brief Creates a public key from a secret key
  50. /// \param publicKey byte array for the public key
  51. /// \param secretKey byte array with the private key
  52. /// \return 0 on success, non-0 otherwise
  53. /// \details ed25519_publickey() generates a public key from a secret key.
  54. /// Internally ed25519_publickey() performs a scalar multiplication
  55. /// using the secret key and then writes the result to <tt>publicKey</tt>.
  56. int ed25519_publickey(byte publicKey[32], const byte secretKey[32]);
  57. /// \brief Creates a signature on a message
  58. /// \param message byte array with the message
  59. /// \param messageLength size of the message, in bytes
  60. /// \param publicKey byte array with the public key
  61. /// \param secretKey byte array with the private key
  62. /// \param signature byte array for the signature
  63. /// \return 0 on success, non-0 otherwise
  64. /// \details ed25519_sign() generates a signature on a message using
  65. /// the public and private keys. The various buffers can be exact
  66. /// sizes, and do not require extra space like when using the
  67. /// NaCl library functions.
  68. /// \details At the moment the hash function for signing is fixed at
  69. /// SHA512.
  70. int ed25519_sign(const byte* message, size_t messageLength, const byte secretKey[32], const byte publicKey[32], byte signature[64]);
  71. /// \brief Creates a signature on a message
  72. /// \param stream std::istream derived class
  73. /// \param publicKey byte array with the public key
  74. /// \param secretKey byte array with the private key
  75. /// \param signature byte array for the signature
  76. /// \return 0 on success, non-0 otherwise
  77. /// \details ed25519_sign() generates a signature on a message using
  78. /// the public and private keys. The various buffers can be exact
  79. /// sizes, and do not require extra space like when using the
  80. /// NaCl library functions.
  81. /// \details This ed25519_sign() overload handles large streams. It
  82. /// was added for signing and verifying files that are too large
  83. /// for a memory allocation.
  84. /// \details At the moment the hash function for signing is fixed at
  85. /// SHA512.
  86. int ed25519_sign(std::istream& stream, const byte secretKey[32], const byte publicKey[32], byte signature[64]);
  87. /// \brief Verifies a signature on a message
  88. /// \param message byte array with the message
  89. /// \param messageLength size of the message, in bytes
  90. /// \param publicKey byte array with the public key
  91. /// \param signature byte array with the signature
  92. /// \return 0 on success, non-0 otherwise
  93. /// \details ed25519_sign_open() verifies a signature on a message using
  94. /// the public key. The various buffers can be exact sizes, and do not
  95. /// require extra space like when using the NaCl library functions.
  96. /// \details At the moment the hash function for signing is fixed at
  97. /// SHA512.
  98. int
  99. ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64]);
  100. /// \brief Verifies a signature on a message
  101. /// \param stream std::istream derived class
  102. /// \param publicKey byte array with the public key
  103. /// \param signature byte array with the signature
  104. /// \return 0 on success, non-0 otherwise
  105. /// \details ed25519_sign_open() verifies a signature on a message using
  106. /// the public key. The various buffers can be exact sizes, and do not
  107. /// require extra space like when using the NaCl library functions.
  108. /// \details This ed25519_sign_open() overload handles large streams. It
  109. /// was added for signing and verifying files that are too large
  110. /// for a memory allocation.
  111. /// \details At the moment the hash function for signing is fixed at
  112. /// SHA512.
  113. int
  114. ed25519_sign_open(std::istream& stream, const byte publicKey[32], const byte signature[64]);
  115. //****************************** Internal ******************************//
  116. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  117. // CRYPTOPP_WORD128_AVAILABLE mostly depends upon GCC support for
  118. // __SIZEOF_INT128__. If __SIZEOF_INT128__ is not available then Moon
  119. // provides routines for MSC and GCC. It should cover most platforms,
  120. // but there are gaps like MS ARM64 and XLC. We tried to enable the
  121. // 64-bit path for SunCC from 12.5 but we got the dreaded compile
  122. // error "The operand ___LCM cannot be assigned to".
  123. #if defined(CRYPTOPP_WORD128_AVAILABLE) || \
  124. (defined(_MSC_VER) && defined(_M_X64))
  125. # define CRYPTOPP_CURVE25519_64BIT 1
  126. #else
  127. # define CRYPTOPP_CURVE25519_32BIT 1
  128. #endif
  129. // Benchmarking on a modern 64-bit Core i5-6400 @2.7 GHz shows SSE2 on Linux
  130. // is not profitable. Here are the numbers in milliseconds/operation:
  131. //
  132. // * Langley, C++, 0.050
  133. // * Moon, C++: 0.040
  134. // * Moon, SSE2: 0.061
  135. // * Moon, native: 0.045
  136. //
  137. // However, a modern 64-bit Core i5-3200 @2.5 GHz shows SSE2 is profitable
  138. // for MS compilers. Here are the numbers in milliseconds/operation:
  139. //
  140. // * x86, no SSE2, 0.294
  141. // * x86, SSE2, 0.097
  142. // * x64, no SSE2, 0.081
  143. // * x64, SSE2, 0.071
  144. #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE) && defined(_MSC_VER)
  145. # define CRYPTOPP_CURVE25519_SSE2 1
  146. #endif
  147. #if (CRYPTOPP_CURVE25519_SSE2)
  148. extern int curve25519_mult_SSE2(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32]);
  149. #endif
  150. #endif // CRYPTOPP_DOXYGEN_PROCESSING
  151. NAMESPACE_END // Donna
  152. NAMESPACE_END // CryptoPP
  153. #endif // CRYPTOPP_DONNA_H