poly1305.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // poly1305.h - written and placed in the public domain by Jeffrey Walton and Jean-Pierre Munch
  2. // Based on Andy Polyakov's Base-2^26 scalar multiplication implementation.
  3. // For more information, see https://www.openssl.org/~appro/cryptogams/.
  4. // The library added Bernstein's Poly1305 classes at Crypto++ 6.0. The IETF
  5. // uses a slightly different implementation than Bernstein, and the IETF
  6. // classes were added at Crypto++ 8.1. We wanted to maintain ABI compatibility
  7. // at the 8.1 release so the original Poly1305 classes were not disturbed.
  8. // Instead new classes were added for IETF Poly1305. The back-end implementation
  9. // shares code as expected, however.
  10. /// \file poly1305.h
  11. /// \brief Classes for Poly1305 message authentication code
  12. /// \details Poly1305-AES is a state-of-the-art message-authentication code suitable for a wide
  13. /// variety of applications. Poly1305-AES computes a 16-byte authenticator of a variable-length
  14. /// message, using a 16-byte AES key, a 16-byte additional key, and a 16-byte nonce.
  15. /// \details Crypto++ also supplies the IETF's version of Poly1305. It is a slightly different
  16. /// algorithm than Bernstein's version.
  17. /// \sa Daniel J. Bernstein <A HREF="http://cr.yp.to/mac/poly1305-20050329.pdf">The Poly1305-AES
  18. /// Message-Authentication Code (20050329)</A>, <a href="http://tools.ietf.org/html/rfc8439">RFC
  19. /// 8439, ChaCha20 and Poly1305 for IETF Protocols</a> and Andy Polyakov <A
  20. /// HREF="http://www.openssl.org/blog/blog/2016/02/15/poly1305-revised/">Poly1305 Revised</A>
  21. /// \since Poly1305 since Crypto++ 6.0, Poly1305TLS since Crypto++ 8.1
  22. #ifndef CRYPTOPP_POLY1305_H
  23. #define CRYPTOPP_POLY1305_H
  24. #include "cryptlib.h"
  25. #include "seckey.h"
  26. #include "secblock.h"
  27. #include "argnames.h"
  28. #include "algparam.h"
  29. NAMESPACE_BEGIN(CryptoPP)
  30. ////////////////////////////// Bernstein Poly1305 //////////////////////////////
  31. /// \brief Poly1305 message authentication code base class
  32. /// \tparam T BlockCipherDocumentation derived class with 16-byte key and 16-byte blocksize
  33. /// \details Poly1305_Base is the base class of Bernstein's Poly1305 algorithm.
  34. /// \since Crypto++ 6.0
  35. template <class T>
  36. class CRYPTOPP_NO_VTABLE Poly1305_Base : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 16>, public MessageAuthenticationCode
  37. {
  38. CRYPTOPP_COMPILE_ASSERT(T::DEFAULT_KEYLENGTH == 16);
  39. CRYPTOPP_COMPILE_ASSERT(T::BLOCKSIZE == 16);
  40. public:
  41. static std::string StaticAlgorithmName() {return std::string("Poly1305(") + T::StaticAlgorithmName() + ")";}
  42. CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE);
  43. CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE);
  44. virtual ~Poly1305_Base() {}
  45. Poly1305_Base() : m_idx(0), m_used(true) {}
  46. void Resynchronize (const byte *iv, int ivLength=-1);
  47. void GetNextIV (RandomNumberGenerator &rng, byte *iv);
  48. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  49. void Update(const byte *input, size_t length);
  50. void TruncatedFinal(byte *mac, size_t size);
  51. void Restart();
  52. unsigned int BlockSize() const {return BLOCKSIZE;}
  53. unsigned int DigestSize() const {return DIGESTSIZE;}
  54. std::string AlgorithmProvider() const;
  55. protected:
  56. // TODO: No longer needed. Remove at next major version bump
  57. void HashBlocks(const byte *input, size_t length, word32 padbit);
  58. void HashFinal(byte *mac, size_t length);
  59. typename T::Encryption m_cipher;
  60. // Accumulated hash, clamped r-key, and encrypted nonce
  61. FixedSizeAlignedSecBlock<word32, 5> m_h;
  62. FixedSizeAlignedSecBlock<word32, 4> m_r;
  63. FixedSizeAlignedSecBlock<word32, 4> m_n;
  64. // Accumulated message bytes and index
  65. FixedSizeAlignedSecBlock<byte, BLOCKSIZE> m_acc, m_nk;
  66. size_t m_idx;
  67. // Track nonce reuse; assert in debug but continue
  68. bool m_used;
  69. };
  70. /// \brief Poly1305 message authentication code
  71. /// \tparam T class derived from BlockCipherDocumentation with 16-byte key and 16-byte blocksize
  72. /// \details Poly1305-AES is a state-of-the-art message-authentication code suitable for a wide
  73. /// variety of applications. Poly1305-AES computes a 16-byte authenticator of a variable-length
  74. /// message, using a 16-byte AES key, a 16-byte additional key, and a 16-byte nonce.
  75. /// \details The key is 32 bytes and a concatenation <tt>key = {k,s}</tt>, where
  76. /// <tt>k</tt> is the AES key and <tt>r</tt> is additional key that gets clamped.
  77. /// The key is clamped internally so there is no need to perform the operation
  78. /// before setting the key.
  79. /// \details Each message must have a unique security context, which means either the key or nonce
  80. /// must be changed after each message. It can be accomplished in one of two ways. First, you
  81. /// can create a new Poly1305 object each time its needed.
  82. /// <pre> SecByteBlock key(32), nonce(16);
  83. /// prng.GenerateBlock(key, key.size());
  84. /// prng.GenerateBlock(nonce, nonce.size());
  85. ///
  86. /// Poly1305<AES> poly1305(key, key.size(), nonce, nonce.size());
  87. /// poly1305.Update(...);
  88. /// poly1305.Final(...);</pre>
  89. ///
  90. /// \details Second, you can create a Poly1305 object, reuse the key, and set a fresh nonce
  91. /// for each message. The second and subsequent nonces can be generated using GetNextIV().
  92. /// <pre> SecByteBlock key(32), nonce(16);
  93. /// prng.GenerateBlock(key, key.size());
  94. /// prng.GenerateBlock(nonce, nonce.size());
  95. ///
  96. /// // First message
  97. /// Poly1305<AES> poly1305(key, key.size());
  98. /// poly1305.Resynchronize(nonce);
  99. /// poly1305.Update(...);
  100. /// poly1305.Final(...);
  101. ///
  102. /// // Second message
  103. /// poly1305.GetNextIV(prng, nonce);
  104. /// poly1305.Resynchronize(nonce);
  105. /// poly1305.Update(...);
  106. /// poly1305.Final(...);
  107. /// ...</pre>
  108. /// \warning Each message must have a unique security context. The Poly1305 class does not
  109. /// enforce a fresh key or nonce for each message. The source code will assert in debug
  110. /// builds to alert of nonce reuse. No action is taken in release builds.
  111. /// \sa Daniel J. Bernstein <A HREF="http://cr.yp.to/mac/poly1305-20050329.pdf">The Poly1305-AES
  112. /// Message-Authentication Code (20050329)</A> and Andy Polyakov <A
  113. /// HREF="http://www.openssl.org/blog/blog/2016/02/15/poly1305-revised/">Poly1305 Revised</A>
  114. /// \since Crypto++ 6.0
  115. template <class T>
  116. class Poly1305 : public MessageAuthenticationCodeFinal<Poly1305_Base<T> >
  117. {
  118. public:
  119. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=Poly1305_Base<T>::DEFAULT_KEYLENGTH);
  120. /// \brief Construct a Poly1305
  121. Poly1305() {}
  122. /// \brief Construct a Poly1305
  123. /// \param key a byte array used to key the cipher
  124. /// \param keyLength the size of the byte array, in bytes
  125. /// \param nonce a byte array used to key the cipher
  126. /// \param nonceLength the size of the byte array, in bytes
  127. /// \details The key is 32 bytes and a concatenation <tt>key = {k,s}</tt>, where
  128. /// <tt>k</tt> is the AES key and <tt>r</tt> is additional key that gets clamped.
  129. /// The key is clamped internally so there is no need to perform the operation
  130. /// before setting the key.
  131. /// \details Each message requires a unique security context. You can use GetNextIV()
  132. /// and Resynchronize() to set a new nonce under a key for a message.
  133. Poly1305(const byte *key, size_t keyLength=DEFAULT_KEYLENGTH, const byte *nonce=NULLPTR, size_t nonceLength=0)
  134. {this->SetKey(key, keyLength, MakeParameters(Name::IV(), ConstByteArrayParameter(nonce, nonceLength)));}
  135. };
  136. ////////////////////////////// IETF Poly1305 //////////////////////////////
  137. /// \brief Poly1305-TLS message authentication code base class
  138. /// \details Poly1305TLS_Base is the base class of the IETF's Poly1305 algorithm.
  139. /// \since Crypto++ 8.1
  140. class Poly1305TLS_Base : public FixedKeyLength<32>, public MessageAuthenticationCode
  141. {
  142. public:
  143. static std::string StaticAlgorithmName() {return std::string("Poly1305TLS");}
  144. CRYPTOPP_CONSTANT(DIGESTSIZE=16);
  145. CRYPTOPP_CONSTANT(BLOCKSIZE=16);
  146. virtual ~Poly1305TLS_Base() {}
  147. Poly1305TLS_Base() {}
  148. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  149. void Update(const byte *input, size_t length);
  150. void TruncatedFinal(byte *mac, size_t size);
  151. void Restart();
  152. unsigned int BlockSize() const {return BLOCKSIZE;}
  153. unsigned int DigestSize() const {return DIGESTSIZE;}
  154. protected:
  155. // Accumulated hash, clamped r-key, and encrypted nonce
  156. FixedSizeAlignedSecBlock<word32, 5> m_h;
  157. FixedSizeAlignedSecBlock<word32, 4> m_r;
  158. FixedSizeAlignedSecBlock<word32, 4> m_n;
  159. // Accumulated message bytes and index
  160. FixedSizeAlignedSecBlock<byte, BLOCKSIZE> m_acc;
  161. size_t m_idx;
  162. };
  163. /// \brief Poly1305-TLS message authentication code
  164. /// \details This is the IETF's variant of Bernstein's Poly1305 from RFC 8439.
  165. /// IETF Poly1305 is called Poly1305TLS in the Crypto++ library. It is
  166. /// _slightly_ different from the Bernstein implementation. Poly1305-TLS
  167. /// can be used for cipher suites
  168. /// <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
  169. /// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and
  170. /// <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
  171. /// \details The key is 32 bytes and a concatenation <tt>key = {r,s}</tt>, where
  172. /// <tt>r</tt> is additional key that gets clamped and <tt>s</tt> is the nonce.
  173. /// The key is clamped internally so there is no need to perform the operation
  174. /// before setting the key.
  175. /// \details Each message must have a unique security context, which means the key
  176. /// must be changed after each message. It can be accomplished in one of two ways.
  177. /// First, you can create a new Poly1305 object with a new key each time its needed.
  178. /// <pre> SecByteBlock key(32);
  179. /// prng.GenerateBlock(key, key.size());
  180. ///
  181. /// Poly1305TLS poly1305(key, key.size());
  182. /// poly1305.Update(...);
  183. /// poly1305.Final(...);</pre>
  184. ///
  185. /// \details Second, you can create a Poly1305 object, and use a new key for each
  186. /// message. The keys can be generated directly using a RandomNumberGenerator()
  187. /// derived class.
  188. /// <pre> SecByteBlock key(32);
  189. /// prng.GenerateBlock(key, key.size());
  190. ///
  191. /// // First message
  192. /// Poly1305TLS poly1305(key, key.size());
  193. /// poly1305.Update(...);
  194. /// poly1305.Final(...);
  195. ///
  196. /// // Second message
  197. /// prng.GenerateBlock(key, key.size());
  198. /// poly1305.SetKey(key, key.size());
  199. /// poly1305.Update(...);
  200. /// poly1305.Final(...);
  201. /// ...</pre>
  202. /// \warning Each message must have a unique security context. The Poly1305-TLS class
  203. /// does not enforce a fresh key or nonce for each message.
  204. /// \since Crypto++ 8.1
  205. /// \sa MessageAuthenticationCode(), <a href="http://tools.ietf.org/html/rfc8439">RFC
  206. /// 8439, ChaCha20 and Poly1305 for IETF Protocols</a>
  207. DOCUMENTED_TYPEDEF(MessageAuthenticationCodeFinal<Poly1305TLS_Base>, Poly1305TLS);
  208. NAMESPACE_END
  209. #endif // CRYPTOPP_POLY1305_H