C25519.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_C25519_HPP
  28. #define _ZT_C25519_HPP
  29. #include "Array.hpp"
  30. namespace ZeroTier {
  31. #define ZT_C25519_PUBLIC_KEY_LEN 64
  32. #define ZT_C25519_PRIVATE_KEY_LEN 64
  33. #define ZT_C25519_SIGNATURE_LEN 96
  34. /**
  35. * C25519 elliptic curve key agreement and signing
  36. */
  37. class C25519
  38. {
  39. public:
  40. /**
  41. * Public key (both crypto and signing)
  42. */
  43. typedef Array<unsigned char,ZT_C25519_PUBLIC_KEY_LEN> Public; // crypto key, signing key (both 32 bytes)
  44. /**
  45. * Private key (both crypto and signing)
  46. */
  47. typedef Array<unsigned char,ZT_C25519_PRIVATE_KEY_LEN> Private; // crypto key, signing key (both 32 bytes)
  48. /**
  49. * Message signature
  50. */
  51. typedef Array<unsigned char,ZT_C25519_SIGNATURE_LEN> Signature;
  52. /**
  53. * Public/private key pair
  54. */
  55. typedef struct {
  56. Public pub;
  57. Private priv;
  58. } Pair;
  59. /**
  60. * Generate a C25519 elliptic curve key pair
  61. */
  62. static Pair generate()
  63. throw();
  64. /**
  65. * Perform C25519 ECC key agreement
  66. *
  67. * Actual key bytes are generated from one or more SHA-512 digests of
  68. * the raw result of key agreement.
  69. *
  70. * @param mine My private key
  71. * @param their Their public key
  72. * @param keybuf Buffer to fill
  73. * @param keylen Number of key bytes to generate
  74. */
  75. static void agree(const Private &mine,const Public &their,void *keybuf,unsigned int keylen)
  76. throw();
  77. static inline void agree(const Pair &mine,const Public &their,void *keybuf,unsigned int keylen)
  78. throw()
  79. {
  80. agree(mine.priv,their,keybuf,keylen);
  81. }
  82. /**
  83. * Sign a message with a sender's key pair
  84. *
  85. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  86. * digest, returning it and the 64-byte ed25519 signature in signature[].
  87. * This results in a signature that verifies both the signer's authenticity
  88. * and the integrity of the message.
  89. *
  90. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  91. * cipher benchmark suite, but with the modification that it always
  92. * produces a signature of fixed 96-byte length based on the hash of an
  93. * arbitrary-length message.
  94. *
  95. * @param myPrivate My private key
  96. * @param myPublic My public key
  97. * @param msg Message to sign
  98. * @param len Length of message in bytes
  99. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  100. */
  101. static void sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len,void *signature)
  102. throw();
  103. static inline void sign(const Pair &mine,const void *msg,unsigned int len,void *signature)
  104. throw()
  105. {
  106. sign(mine.priv,mine.pub,msg,len,signature);
  107. }
  108. /**
  109. * Sign a message with a sender's key pair
  110. *
  111. * @param myPrivate My private key
  112. * @param myPublic My public key
  113. * @param msg Message to sign
  114. * @param len Length of message in bytes
  115. * @return Signature
  116. */
  117. static inline Signature sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len)
  118. throw()
  119. {
  120. Signature sig;
  121. sign(myPrivate,myPublic,msg,len,sig.data);
  122. return sig;
  123. }
  124. static inline Signature sign(const Pair &mine,const void *msg,unsigned int len)
  125. throw()
  126. {
  127. Signature sig;
  128. sign(mine.priv,mine.pub,msg,len,sig.data);
  129. return sig;
  130. }
  131. /**
  132. * Verify a message's signature
  133. *
  134. * @param their Public key to verify against
  135. * @param msg Message to verify signature integrity against
  136. * @param len Length of message in bytes
  137. * @param signature 96-byte signature
  138. * @return True if signature is valid and the message is authentic and unmodified
  139. */
  140. static bool verify(const Public &their,const void *msg,unsigned int len,const void *signature)
  141. throw();
  142. /**
  143. * Verify a message's signature
  144. *
  145. * @param their Public key to verify against
  146. * @param msg Message to verify signature integrity against
  147. * @param len Length of message in bytes
  148. * @param signature 96-byte signature
  149. * @return True if signature is valid and the message is authentic and unmodified
  150. */
  151. static inline bool verify(const Public &their,const void *msg,unsigned int len,const Signature &signature)
  152. throw()
  153. {
  154. return verify(their,msg,len,signature.data);
  155. }
  156. };
  157. } // namespace ZeroTier
  158. #endif