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