blake2.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // blake2.h - written and placed in the public domain by Jeffrey Walton
  2. // and Zooko Wilcox-O'Hearn. Based on Aumasson, Neves,
  3. // Wilcox-O'Hearn and Winnerlein's reference BLAKE2
  4. // implementation at http://github.com/BLAKE2/BLAKE2.
  5. /// \file blake2.h
  6. /// \brief Classes for BLAKE2b and BLAKE2s message digests and keyed message digests
  7. /// \details This implementation follows Aumasson, Neves, Wilcox-O'Hearn and Winnerlein's
  8. /// <A HREF="http://blake2.net/blake2.pdf">BLAKE2: simpler, smaller, fast as MD5</A> (2013.01.29).
  9. /// Static algorithm name return either "BLAKE2b" or "BLAKE2s". An object algorithm name follows
  10. /// the naming described in <A HREF="http://tools.ietf.org/html/rfc7693#section-4">RFC 7693, The
  11. /// BLAKE2 Cryptographic Hash and Message Authentication Code (MAC)</A>.
  12. /// \since C++ since Crypto++ 5.6.4, SSE since Crypto++ 5.6.4, NEON since Crypto++ 6.0,
  13. /// Power8 since Crypto++ 8.0
  14. #ifndef CRYPTOPP_BLAKE2_H
  15. #define CRYPTOPP_BLAKE2_H
  16. #include "cryptlib.h"
  17. #include "secblock.h"
  18. #include "seckey.h"
  19. NAMESPACE_BEGIN(CryptoPP)
  20. /// \brief BLAKE2s hash information
  21. /// \since Crypto++ 5.6.4
  22. struct BLAKE2s_Info : public VariableKeyLength<32,0,32,1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
  23. {
  24. typedef VariableKeyLength<32,0,32,1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE> KeyBase;
  25. CRYPTOPP_CONSTANT(MIN_KEYLENGTH = KeyBase::MIN_KEYLENGTH);
  26. CRYPTOPP_CONSTANT(MAX_KEYLENGTH = KeyBase::MAX_KEYLENGTH);
  27. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = KeyBase::DEFAULT_KEYLENGTH);
  28. CRYPTOPP_CONSTANT(BLOCKSIZE = 64);
  29. CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
  30. CRYPTOPP_CONSTANT(SALTSIZE = 8);
  31. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = 8);
  32. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BLAKE2s";}
  33. };
  34. /// \brief BLAKE2b hash information
  35. /// \since Crypto++ 5.6.4
  36. struct BLAKE2b_Info : public VariableKeyLength<64,0,64,1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
  37. {
  38. typedef VariableKeyLength<64,0,64,1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE> KeyBase;
  39. CRYPTOPP_CONSTANT(MIN_KEYLENGTH = KeyBase::MIN_KEYLENGTH);
  40. CRYPTOPP_CONSTANT(MAX_KEYLENGTH = KeyBase::MAX_KEYLENGTH);
  41. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = KeyBase::DEFAULT_KEYLENGTH);
  42. CRYPTOPP_CONSTANT(BLOCKSIZE = 128);
  43. CRYPTOPP_CONSTANT(DIGESTSIZE = 64);
  44. CRYPTOPP_CONSTANT(SALTSIZE = 16);
  45. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = 16);
  46. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BLAKE2b";}
  47. };
  48. /// \brief BLAKE2s parameter block
  49. struct CRYPTOPP_NO_VTABLE BLAKE2s_ParameterBlock
  50. {
  51. CRYPTOPP_CONSTANT(SALTSIZE = BLAKE2s_Info::SALTSIZE);
  52. CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2s_Info::DIGESTSIZE);
  53. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = BLAKE2s_Info::PERSONALIZATIONSIZE);
  54. BLAKE2s_ParameterBlock()
  55. {
  56. Reset();
  57. }
  58. BLAKE2s_ParameterBlock(size_t digestSize)
  59. {
  60. Reset(digestSize);
  61. }
  62. BLAKE2s_ParameterBlock(size_t digestSize, size_t keyLength, const byte* salt, size_t saltLength,
  63. const byte* personalization, size_t personalizationLength);
  64. void Reset(size_t digestLength=DIGESTSIZE, size_t keyLength=0);
  65. byte* data() {
  66. return m_data.data();
  67. }
  68. const byte* data() const {
  69. return m_data.data();
  70. }
  71. size_t size() const {
  72. return m_data.size();
  73. }
  74. byte* salt() {
  75. return m_data + SaltOff;
  76. }
  77. byte* personalization() {
  78. return m_data + PersonalizationOff;
  79. }
  80. // Offsets into the byte array
  81. enum {
  82. DigestOff = 0, KeyOff = 1, FanoutOff = 2, DepthOff = 3, LeafOff = 4, NodeOff = 8,
  83. NodeDepthOff = 14, InnerOff = 15, SaltOff = 16, PersonalizationOff = 24
  84. };
  85. FixedSizeAlignedSecBlock<byte, 32, true> m_data;
  86. };
  87. /// \brief BLAKE2b parameter block
  88. struct CRYPTOPP_NO_VTABLE BLAKE2b_ParameterBlock
  89. {
  90. CRYPTOPP_CONSTANT(SALTSIZE = BLAKE2b_Info::SALTSIZE);
  91. CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2b_Info::DIGESTSIZE);
  92. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = BLAKE2b_Info::PERSONALIZATIONSIZE);
  93. BLAKE2b_ParameterBlock()
  94. {
  95. Reset();
  96. }
  97. BLAKE2b_ParameterBlock(size_t digestSize)
  98. {
  99. Reset(digestSize);
  100. }
  101. BLAKE2b_ParameterBlock(size_t digestSize, size_t keyLength, const byte* salt, size_t saltLength,
  102. const byte* personalization, size_t personalizationLength);
  103. void Reset(size_t digestLength=DIGESTSIZE, size_t keyLength=0);
  104. byte* data() {
  105. return m_data.data();
  106. }
  107. const byte* data() const {
  108. return m_data.data();
  109. }
  110. size_t size() const {
  111. return m_data.size();
  112. }
  113. byte* salt() {
  114. return m_data + SaltOff;
  115. }
  116. byte* personalization() {
  117. return m_data + PersonalizationOff;
  118. }
  119. // Offsets into the byte array
  120. enum {
  121. DigestOff = 0, KeyOff = 1, FanoutOff = 2, DepthOff = 3, LeafOff = 4, NodeOff = 8,
  122. NodeDepthOff = 16, InnerOff = 17, RfuOff = 18, SaltOff = 32, PersonalizationOff = 48
  123. };
  124. FixedSizeAlignedSecBlock<byte, 64, true> m_data;
  125. };
  126. /// \brief BLAKE2s state information
  127. /// \since Crypto++ 5.6.4
  128. struct CRYPTOPP_NO_VTABLE BLAKE2s_State
  129. {
  130. BLAKE2s_State() {
  131. Reset();
  132. }
  133. void Reset();
  134. inline word32* h() {
  135. return m_hft.data();
  136. }
  137. inline word32* t() {
  138. return m_hft.data() + 8;
  139. }
  140. inline word32* f() {
  141. return m_hft.data() + 10;
  142. }
  143. inline byte* data() {
  144. return m_buf.data();
  145. }
  146. // SSE4, Power7 and NEON depend upon t[] and f[] being side-by-side
  147. CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2s_Info::BLOCKSIZE);
  148. FixedSizeAlignedSecBlock<word32, 8+2+2, true> m_hft;
  149. FixedSizeAlignedSecBlock<byte, BLOCKSIZE, true> m_buf;
  150. size_t m_len;
  151. };
  152. /// \brief BLAKE2b state information
  153. /// \since Crypto++ 5.6.4
  154. struct CRYPTOPP_NO_VTABLE BLAKE2b_State
  155. {
  156. BLAKE2b_State() {
  157. Reset();
  158. }
  159. void Reset();
  160. inline word64* h() {
  161. return m_hft.data();
  162. }
  163. inline word64* t() {
  164. return m_hft.data() + 8;
  165. }
  166. inline word64* f() {
  167. return m_hft.data() + 10;
  168. }
  169. inline byte* data() {
  170. return m_buf.data();
  171. }
  172. // SSE4, Power8 and NEON depend upon t[] and f[] being side-by-side
  173. CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2b_Info::BLOCKSIZE);
  174. FixedSizeAlignedSecBlock<word64, 8+2+2, true> m_hft;
  175. FixedSizeAlignedSecBlock<byte, BLOCKSIZE, true> m_buf;
  176. size_t m_len;
  177. };
  178. /// \brief The BLAKE2s cryptographic hash function
  179. /// \details BLAKE2s can function as both a hash and keyed hash. If you want only the hash,
  180. /// then use the BLAKE2s constructor that accepts no parameters or digest size. If you
  181. /// want a keyed hash, then use the constructor that accpts the key as a parameter.
  182. /// Once a key and digest size are selected, its effectively immutable. The Restart()
  183. /// method that accepts a ParameterBlock does not allow you to change it.
  184. /// \sa Aumasson, Neves, Wilcox-O'Hearn and Winnerlein's
  185. /// <A HREF="http://blake2.net/blake2.pdf">BLAKE2: simpler, smaller, fast as MD5</A> (2013.01.29).
  186. /// \since C++ since Crypto++ 5.6.4, SSE since Crypto++ 5.6.4, NEON since Crypto++ 6.0,
  187. /// Power8 since Crypto++ 8.0
  188. class BLAKE2s : public SimpleKeyingInterfaceImpl<MessageAuthenticationCode, BLAKE2s_Info>
  189. {
  190. public:
  191. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = BLAKE2s_Info::DEFAULT_KEYLENGTH);
  192. CRYPTOPP_CONSTANT(MIN_KEYLENGTH = BLAKE2s_Info::MIN_KEYLENGTH);
  193. CRYPTOPP_CONSTANT(MAX_KEYLENGTH = BLAKE2s_Info::MAX_KEYLENGTH);
  194. CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2s_Info::DIGESTSIZE);
  195. CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2s_Info::BLOCKSIZE);
  196. CRYPTOPP_CONSTANT(SALTSIZE = BLAKE2s_Info::SALTSIZE);
  197. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = BLAKE2s_Info::PERSONALIZATIONSIZE);
  198. typedef BLAKE2s_State State;
  199. typedef BLAKE2s_ParameterBlock ParameterBlock;
  200. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BLAKE2s";}
  201. virtual ~BLAKE2s() {}
  202. /// \brief Construct a BLAKE2s hash
  203. /// \param digestSize the digest size, in bytes
  204. /// \param treeMode flag indicating tree mode
  205. /// \since Crypto++ 5.6.4
  206. BLAKE2s(bool treeMode=false, unsigned int digestSize = DIGESTSIZE);
  207. /// \brief Construct a BLAKE2s hash
  208. /// \param digestSize the digest size, in bytes
  209. /// \details treeMode flag is set to false
  210. /// \since Crypto++ 8.2
  211. BLAKE2s(unsigned int digestSize);
  212. /// \brief Construct a BLAKE2s hash
  213. /// \param key a byte array used to key the cipher
  214. /// \param keyLength the size of the byte array
  215. /// \param salt a byte array used as salt
  216. /// \param saltLength the size of the byte array
  217. /// \param personalization a byte array used as personalization string
  218. /// \param personalizationLength the size of the byte array
  219. /// \param treeMode flag indicating tree mode
  220. /// \param digestSize the digest size, in bytes
  221. /// \since Crypto++ 5.6.4
  222. BLAKE2s(const byte *key, size_t keyLength, const byte* salt = NULLPTR, size_t saltLength = 0,
  223. const byte* personalization = NULLPTR, size_t personalizationLength = 0,
  224. bool treeMode=false, unsigned int digestSize = DIGESTSIZE);
  225. /// \brief Retrieve the object's name
  226. /// \return the object's algorithm name following RFC 7693
  227. /// \details Object algorithm name follows the naming described in
  228. /// <A HREF="http://tools.ietf.org/html/rfc7693#section-4">RFC 7693, The BLAKE2 Cryptographic Hash and
  229. /// Message Authentication Code (MAC)</A>. For example, "BLAKE2b-512" and "BLAKE2s-256".
  230. std::string AlgorithmName() const {return std::string(BLAKE2s_Info::StaticAlgorithmName()) + "-" + IntToString(DigestSize()*8);}
  231. unsigned int BlockSize() const {return BLOCKSIZE;}
  232. unsigned int DigestSize() const {return m_digestSize;}
  233. unsigned int OptimalDataAlignment() const;
  234. void Update(const byte *input, size_t length);
  235. void Restart();
  236. /// \brief Restart a hash with parameter block and counter
  237. /// \param block parameter block
  238. /// \param counter counter array
  239. /// \details Parameter block is persisted across calls to Restart().
  240. void Restart(const BLAKE2s_ParameterBlock& block, const word32 counter[2]);
  241. /// \brief Set tree mode
  242. /// \param mode the new tree mode
  243. /// \details BLAKE2 has two finalization flags, called State::f[0] and State::f[1].
  244. /// If <tt>treeMode=false</tt> (default), then State::f[1] is never set. If
  245. /// <tt>treeMode=true</tt>, then State::f[1] is set when State::f[0] is set.
  246. /// Tree mode is persisted across calls to Restart().
  247. void SetTreeMode(bool mode) {m_treeMode=mode;}
  248. /// \brief Get tree mode
  249. /// \return the current tree mode
  250. /// \details Tree mode is persisted across calls to Restart().
  251. bool GetTreeMode() const {return m_treeMode;}
  252. void TruncatedFinal(byte *hash, size_t size);
  253. std::string AlgorithmProvider() const;
  254. protected:
  255. // Operates on state buffer and/or input. Must be BLOCKSIZE, final block will pad with 0's.
  256. void Compress(const byte *input);
  257. inline void IncrementCounter(size_t count=BLOCKSIZE);
  258. void UncheckedSetKey(const byte* key, unsigned int length, const CryptoPP::NameValuePairs& params);
  259. private:
  260. State m_state;
  261. ParameterBlock m_block;
  262. AlignedSecByteBlock m_key;
  263. word32 m_digestSize, m_keyLength;
  264. bool m_treeMode;
  265. };
  266. /// \brief The BLAKE2b cryptographic hash function
  267. /// \details BLAKE2b can function as both a hash and keyed hash. If you want only the hash,
  268. /// then use the BLAKE2b constructor that accepts no parameters or digest size. If you
  269. /// want a keyed hash, then use the constructor that accpts the key as a parameter.
  270. /// Once a key and digest size are selected, its effectively immutable. The Restart()
  271. /// method that accepts a ParameterBlock does not allow you to change it.
  272. /// \sa Aumasson, Neves, Wilcox-O'Hearn and Winnerlein's
  273. /// <A HREF="http://blake2.net/blake2.pdf">BLAKE2: simpler, smaller, fast as MD5</A> (2013.01.29).
  274. /// \since C++ since Crypto++ 5.6.4, SSE since Crypto++ 5.6.4, NEON since Crypto++ 6.0,
  275. /// Power8 since Crypto++ 8.0
  276. class BLAKE2b : public SimpleKeyingInterfaceImpl<MessageAuthenticationCode, BLAKE2b_Info>
  277. {
  278. public:
  279. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = BLAKE2b_Info::DEFAULT_KEYLENGTH);
  280. CRYPTOPP_CONSTANT(MIN_KEYLENGTH = BLAKE2b_Info::MIN_KEYLENGTH);
  281. CRYPTOPP_CONSTANT(MAX_KEYLENGTH = BLAKE2b_Info::MAX_KEYLENGTH);
  282. CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2b_Info::DIGESTSIZE);
  283. CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2b_Info::BLOCKSIZE);
  284. CRYPTOPP_CONSTANT(SALTSIZE = BLAKE2b_Info::SALTSIZE);
  285. CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = BLAKE2b_Info::PERSONALIZATIONSIZE);
  286. typedef BLAKE2b_State State;
  287. typedef BLAKE2b_ParameterBlock ParameterBlock;
  288. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BLAKE2b";}
  289. virtual ~BLAKE2b() {}
  290. /// \brief Construct a BLAKE2b hash
  291. /// \param digestSize the digest size, in bytes
  292. /// \param treeMode flag indicating tree mode
  293. /// \since Crypto++ 5.6.4
  294. BLAKE2b(bool treeMode=false, unsigned int digestSize = DIGESTSIZE);
  295. /// \brief Construct a BLAKE2s hash
  296. /// \param digestSize the digest size, in bytes
  297. /// \details treeMode flag is set to false
  298. /// \since Crypto++ 8.2
  299. BLAKE2b(unsigned int digestSize);
  300. /// \brief Construct a BLAKE2b hash
  301. /// \param key a byte array used to key the cipher
  302. /// \param keyLength the size of the byte array
  303. /// \param salt a byte array used as salt
  304. /// \param saltLength the size of the byte array
  305. /// \param personalization a byte array used as personalization string
  306. /// \param personalizationLength the size of the byte array
  307. /// \param treeMode flag indicating tree mode
  308. /// \param digestSize the digest size, in bytes
  309. /// \since Crypto++ 5.6.4
  310. BLAKE2b(const byte *key, size_t keyLength, const byte* salt = NULLPTR, size_t saltLength = 0,
  311. const byte* personalization = NULLPTR, size_t personalizationLength = 0,
  312. bool treeMode=false, unsigned int digestSize = DIGESTSIZE);
  313. /// \brief Retrieve the object's name
  314. /// \return the object's algorithm name following RFC 7693
  315. /// \details Object algorithm name follows the naming described in
  316. /// <A HREF="http://tools.ietf.org/html/rfc7693#section-4">RFC 7693, The BLAKE2 Cryptographic Hash and
  317. /// Message Authentication Code (MAC)</A>. For example, "BLAKE2b-512" and "BLAKE2s-256".
  318. std::string AlgorithmName() const {return std::string(BLAKE2b_Info::StaticAlgorithmName()) + "-" + IntToString(DigestSize()*8);}
  319. unsigned int BlockSize() const {return BLOCKSIZE;}
  320. unsigned int DigestSize() const {return m_digestSize;}
  321. unsigned int OptimalDataAlignment() const;
  322. void Update(const byte *input, size_t length);
  323. void Restart();
  324. /// \brief Restart a hash with parameter block and counter
  325. /// \param block parameter block
  326. /// \param counter counter array
  327. /// \details Parameter block is persisted across calls to Restart().
  328. void Restart(const BLAKE2b_ParameterBlock& block, const word64 counter[2]);
  329. /// \brief Set tree mode
  330. /// \param mode the new tree mode
  331. /// \details BLAKE2 has two finalization flags, called State::f[0] and State::f[1].
  332. /// If <tt>treeMode=false</tt> (default), then State::f[1] is never set. If
  333. /// <tt>treeMode=true</tt>, then State::f[1] is set when State::f[0] is set.
  334. /// Tree mode is persisted across calls to Restart().
  335. void SetTreeMode(bool mode) {m_treeMode=mode;}
  336. /// \brief Get tree mode
  337. /// \return the current tree mode
  338. /// \details Tree mode is persisted across calls to Restart().
  339. bool GetTreeMode() const {return m_treeMode;}
  340. void TruncatedFinal(byte *hash, size_t size);
  341. std::string AlgorithmProvider() const;
  342. protected:
  343. // Operates on state buffer and/or input. Must be BLOCKSIZE, final block will pad with 0's.
  344. void Compress(const byte *input);
  345. inline void IncrementCounter(size_t count=BLOCKSIZE);
  346. void UncheckedSetKey(const byte* key, unsigned int length, const CryptoPP::NameValuePairs& params);
  347. private:
  348. State m_state;
  349. ParameterBlock m_block;
  350. AlignedSecByteBlock m_key;
  351. word32 m_digestSize, m_keyLength;
  352. bool m_treeMode;
  353. };
  354. NAMESPACE_END
  355. #endif