siphash.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // siphash.h - written and placed in public domain by Jeffrey Walton.
  2. /// \file siphash.h
  3. /// \brief Classes for SipHash message authentication code
  4. /// \details SipHash computes a 64-bit or 128-bit message authentication code from a variable-length
  5. /// message and 128-bit secret key. It was designed to be efficient even for short inputs, with
  6. /// performance comparable to non-cryptographic hash functions.
  7. /// \details To create a SipHash-2-4 object with a 64-bit MAC use code similar to the following.
  8. /// <pre> SecByteBlock key(16);
  9. /// prng.GenerateBlock(key, key.size());
  10. ///
  11. /// SipHash<2,4,false> hash(key, key.size());
  12. /// hash.Update(...);
  13. /// hash.Final(...);</pre>
  14. /// \details To create a SipHash-2-4 object with a 128-bit MAC use code similar to the following.
  15. /// <pre> SecByteBlock key(16);
  16. /// prng.GenerateBlock(key, key.size());
  17. ///
  18. /// SipHash<2,4,true> hash(key, key.size());
  19. /// hash.Update(...);
  20. /// hash.Final(...);</pre>
  21. /// \sa Jean-Philippe Aumasson and Daniel J. Bernstein <A HREF="http://131002.net/siphash/siphash.pdf">SipHash:
  22. /// a fast short-input PRF</A>
  23. /// \since Crypto++ 6.0
  24. #ifndef CRYPTOPP_SIPHASH_H
  25. #define CRYPTOPP_SIPHASH_H
  26. #include "cryptlib.h"
  27. #include "secblock.h"
  28. #include "seckey.h"
  29. #include "misc.h"
  30. NAMESPACE_BEGIN(CryptoPP)
  31. /// \brief SipHash message authentication code information
  32. /// \tparam T_128bit flag indicating 128-bit (true) versus 64-bit (false) digest size
  33. template <bool T_128bit>
  34. class SipHash_Info : public FixedKeyLength<16>
  35. {
  36. public:
  37. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "SipHash";}
  38. CRYPTOPP_CONSTANT(DIGESTSIZE = (T_128bit ? 16 : 8));
  39. };
  40. /// \brief SipHash message authentication code base class
  41. /// \tparam C the number of compression rounds
  42. /// \tparam D the number of finalization rounds
  43. /// \tparam T_128bit flag indicating 128-bit (true) versus 64-bit (false) digest size
  44. template <unsigned int C, unsigned int D, bool T_128bit>
  45. class SipHash_Base : public MessageAuthenticationCode, public SipHash_Info<T_128bit>
  46. {
  47. public:
  48. static std::string StaticAlgorithmName() {
  49. return std::string(SipHash_Info<T_128bit>::StaticAlgorithmName())+"-"+IntToString(C)+"-"+IntToString(D);
  50. }
  51. virtual ~SipHash_Base() {}
  52. SipHash_Base() : m_idx(0) {}
  53. virtual unsigned int DigestSize() const
  54. {return SipHash_Info<T_128bit>::DIGESTSIZE;}
  55. virtual size_t MinKeyLength() const
  56. {return SipHash_Info<T_128bit>::MIN_KEYLENGTH;}
  57. virtual size_t MaxKeyLength() const
  58. {return SipHash_Info<T_128bit>::MAX_KEYLENGTH;}
  59. virtual size_t DefaultKeyLength() const
  60. {return SipHash_Info<T_128bit>::DEFAULT_KEYLENGTH;}
  61. virtual size_t GetValidKeyLength(size_t keylength) const
  62. {CRYPTOPP_UNUSED(keylength); return SipHash_Info<T_128bit>::DEFAULT_KEYLENGTH;}
  63. virtual IV_Requirement IVRequirement() const
  64. {return SimpleKeyingInterface::NOT_RESYNCHRONIZABLE;}
  65. virtual unsigned int IVSize() const
  66. {return 0;}
  67. virtual unsigned int OptimalBlockSize() const
  68. {return sizeof(word64);}
  69. virtual unsigned int OptimalDataAlignment () const
  70. {return GetAlignmentOf<word64>();}
  71. virtual void Update(const byte *input, size_t length);
  72. virtual void TruncatedFinal(byte *digest, size_t digestSize);
  73. protected:
  74. virtual void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  75. virtual void Restart();
  76. inline void SIPROUND()
  77. {
  78. m_v[0] += m_v[1];
  79. m_v[1] = rotlConstant<13>(m_v[1]);
  80. m_v[1] ^= m_v[0];
  81. m_v[0] = rotlConstant<32>(m_v[0]);
  82. m_v[2] += m_v[3];
  83. m_v[3] = rotlConstant<16>(m_v[3]);
  84. m_v[3] ^= m_v[2];
  85. m_v[0] += m_v[3];
  86. m_v[3] = rotlConstant<21>(m_v[3]);
  87. m_v[3] ^= m_v[0];
  88. m_v[2] += m_v[1];
  89. m_v[1] = rotlConstant<17>(m_v[1]);
  90. m_v[1] ^= m_v[2];
  91. m_v[2] = rotlConstant<32>(m_v[2]);
  92. }
  93. private:
  94. FixedSizeSecBlock<word64, 4> m_v;
  95. FixedSizeSecBlock<word64, 2> m_k;
  96. FixedSizeSecBlock<word64, 2> m_b;
  97. // Tail bytes
  98. FixedSizeSecBlock<byte, 8> m_acc;
  99. size_t m_idx;
  100. };
  101. /// \brief SipHash message authentication code
  102. /// \tparam C the number of compression rounds
  103. /// \tparam D the number of finalization rounds
  104. /// \tparam T_128bit flag indicating 128-bit (true) versus 64-bit (false) digest size
  105. /// \details SipHash computes a 64-bit or 128-bit message authentication code from a variable-length
  106. /// message and 128-bit secret key. It was designed to be efficient even for short inputs, with
  107. /// performance comparable to non-cryptographic hash functions.
  108. /// \details To create a SipHash-2-4 object with a 64-bit MAC use code similar to the following.
  109. /// <pre> SecByteBlock key(16);
  110. /// prng.GenerateBlock(key, key.size());
  111. ///
  112. /// SipHash<2,4,false> hash(key, key.size());
  113. /// hash.Update(...);
  114. /// hash.Final(...);</pre>
  115. /// \details To create a SipHash-2-4 object with a 128-bit MAC use code similar to the following.
  116. /// <pre> SecByteBlock key(16);
  117. /// prng.GenerateBlock(key, key.size());
  118. ///
  119. /// SipHash<2,4,true> hash(key, key.size());
  120. /// hash.Update(...);
  121. /// hash.Final(...);</pre>
  122. /// \sa Jean-Philippe Aumasson and Daniel J. Bernstein <A HREF="http://131002.net/siphash/siphash.pdf">SipHash:
  123. /// a fast short-input PRF</A>
  124. /// \since Crypto++ 6.0
  125. template <unsigned int C=2, unsigned int D=4, bool T_128bit=false>
  126. class SipHash : public SipHash_Base<C, D, T_128bit>
  127. {
  128. public:
  129. /// \brief Create a SipHash
  130. SipHash()
  131. {this->UncheckedSetKey(NULLPTR, 0, g_nullNameValuePairs);}
  132. /// \brief Create a SipHash
  133. /// \param key a byte array used to key the cipher
  134. /// \param length the size of the byte array, in bytes
  135. SipHash(const byte *key, unsigned int length)
  136. {this->ThrowIfInvalidKeyLength(length);
  137. this->UncheckedSetKey(key, length, g_nullNameValuePairs);}
  138. };
  139. template <unsigned int C, unsigned int D, bool T_128bit>
  140. void SipHash_Base<C,D,T_128bit>::Update(const byte *input, size_t length)
  141. {
  142. CRYPTOPP_ASSERT((input && length) || !length);
  143. if (!length) return;
  144. if (m_idx)
  145. {
  146. size_t head = STDMIN(size_t(8U-m_idx), length);
  147. memcpy(m_acc+m_idx, input, head);
  148. m_idx += head; input += head; length -= head;
  149. if (m_idx == 8)
  150. {
  151. word64 m = GetWord<word64>(true, LITTLE_ENDIAN_ORDER, m_acc);
  152. m_v[3] ^= m;
  153. for (unsigned int i = 0; i < C; ++i)
  154. SIPROUND();
  155. m_v[0] ^= m;
  156. m_b[0] += 8;
  157. m_idx = 0;
  158. }
  159. }
  160. while (length >= 8)
  161. {
  162. word64 m = GetWord<word64>(false, LITTLE_ENDIAN_ORDER, input);
  163. m_v[3] ^= m;
  164. for (unsigned int i = 0; i < C; ++i)
  165. SIPROUND();
  166. m_v[0] ^= m;
  167. m_b[0] += 8;
  168. input += 8;
  169. length -= 8;
  170. }
  171. CRYPTOPP_ASSERT(length < 8);
  172. size_t tail = length % 8;
  173. if (tail)
  174. {
  175. memcpy(m_acc+m_idx, input, tail);
  176. m_idx += tail;
  177. }
  178. }
  179. template <unsigned int C, unsigned int D, bool T_128bit>
  180. void SipHash_Base<C,D,T_128bit>::TruncatedFinal(byte *digest, size_t digestSize)
  181. {
  182. CRYPTOPP_ASSERT(digest); // Pointer is valid
  183. ThrowIfInvalidTruncatedSize(digestSize);
  184. // The high octet holds length and is digested mod 256
  185. m_b[0] += m_idx; m_b[0] <<= 56U;
  186. switch (m_idx)
  187. {
  188. case 7:
  189. m_b[0] |= ((word64)m_acc[6]) << 48;
  190. // fall through
  191. case 6:
  192. m_b[0] |= ((word64)m_acc[5]) << 40;
  193. // fall through
  194. case 5:
  195. m_b[0] |= ((word64)m_acc[4]) << 32;
  196. // fall through
  197. case 4:
  198. m_b[0] |= ((word64)m_acc[3]) << 24;
  199. // fall through
  200. case 3:
  201. m_b[0] |= ((word64)m_acc[2]) << 16;
  202. // fall through
  203. case 2:
  204. m_b[0] |= ((word64)m_acc[1]) << 8;
  205. // fall through
  206. case 1:
  207. m_b[0] |= ((word64)m_acc[0]);
  208. // fall through
  209. case 0:
  210. break;
  211. }
  212. m_v[3] ^= m_b[0];
  213. for (unsigned int i=0; i<C; i++)
  214. SIPROUND();
  215. m_v[0] ^= m_b[0];
  216. if (T_128bit)
  217. m_v[2] ^= 0xee;
  218. else
  219. m_v[2] ^= 0xff;
  220. for (unsigned int i=0; i<D; i++)
  221. SIPROUND();
  222. m_b[0] = m_v[0] ^ m_v[1] ^ m_v[2] ^ m_v[3];
  223. m_b[0] = ConditionalByteReverse(LITTLE_ENDIAN_ORDER, m_b[0]);
  224. if (T_128bit)
  225. {
  226. m_v[1] ^= 0xdd;
  227. for (unsigned int i = 0; i<D; ++i)
  228. SIPROUND();
  229. m_b[1] = m_v[0] ^ m_v[1] ^ m_v[2] ^ m_v[3];
  230. m_b[1] = ConditionalByteReverse(LITTLE_ENDIAN_ORDER, m_b[1]);
  231. }
  232. memcpy_s(digest, digestSize, m_b.begin(), STDMIN(digestSize, (size_t)SipHash_Info<T_128bit>::DIGESTSIZE));
  233. Restart();
  234. }
  235. template <unsigned int C, unsigned int D, bool T_128bit>
  236. void SipHash_Base<C,D,T_128bit>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  237. {
  238. CRYPTOPP_UNUSED(params);
  239. if (key && length)
  240. {
  241. m_k[0] = GetWord<word64>(false, LITTLE_ENDIAN_ORDER, key);
  242. m_k[1] = GetWord<word64>(false, LITTLE_ENDIAN_ORDER, key+8);
  243. }
  244. else
  245. {
  246. // Avoid Coverity finding
  247. m_k[0] = m_k[1] = 0;
  248. }
  249. Restart();
  250. }
  251. template <unsigned int C, unsigned int D, bool T_128bit>
  252. void SipHash_Base<C,D,T_128bit>::Restart ()
  253. {
  254. m_v[0] = W64LIT(0x736f6d6570736575);
  255. m_v[1] = W64LIT(0x646f72616e646f6d);
  256. m_v[2] = W64LIT(0x6c7967656e657261);
  257. m_v[3] = W64LIT(0x7465646279746573);
  258. m_v[3] ^= m_k[1];
  259. m_v[2] ^= m_k[0];
  260. m_v[1] ^= m_k[1];
  261. m_v[0] ^= m_k[0];
  262. if (T_128bit)
  263. {
  264. m_v[1] ^= 0xee;
  265. }
  266. m_idx = 0;
  267. m_b[0] = 0;
  268. }
  269. NAMESPACE_END
  270. #endif // CRYPTOPP_SIPHASH_H