blake2b.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "blake2/blake2.h"
  21. #include "blake2/blake2-impl.h"
  22. static const uint64_t blake2b_IV[8] = {
  23. UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
  24. UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
  25. UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
  26. UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)};
  27. static const unsigned int blake2b_sigma[12][16] = {
  28. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  29. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  30. {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  31. {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  32. {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  33. {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  34. {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
  35. {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
  36. {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
  37. {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
  38. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  39. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  40. };
  41. static BLAKE2_INLINE void blake2b_set_lastnode(blake2b_state *S) {
  42. S->f[1] = (uint64_t)-1;
  43. }
  44. static BLAKE2_INLINE void blake2b_set_lastblock(blake2b_state *S) {
  45. if (S->last_node) {
  46. blake2b_set_lastnode(S);
  47. }
  48. S->f[0] = (uint64_t)-1;
  49. }
  50. static BLAKE2_INLINE void blake2b_increment_counter(blake2b_state *S,
  51. uint64_t inc) {
  52. S->t[0] += inc;
  53. S->t[1] += (S->t[0] < inc);
  54. }
  55. static BLAKE2_INLINE void blake2b_invalidate_state(blake2b_state *S) {
  56. clear_internal_memory(S, sizeof(*S)); /* wipe */
  57. blake2b_set_lastblock(S); /* invalidate for further use */
  58. }
  59. static BLAKE2_INLINE void blake2b_init0(blake2b_state *S) {
  60. memset(S, 0, sizeof(*S));
  61. memcpy(S->h, blake2b_IV, sizeof(S->h));
  62. }
  63. int kp_blake2b_init_param(blake2b_state *S, const blake2b_param *P) {
  64. const unsigned char *p = (const unsigned char *)P;
  65. unsigned int i;
  66. if (NULL == P || NULL == S) {
  67. return -1;
  68. }
  69. blake2b_init0(S);
  70. /* IV XOR Parameter Block */
  71. for (i = 0; i < 8; ++i) {
  72. S->h[i] ^= load64(&p[i * sizeof(S->h[i])]);
  73. }
  74. S->outlen = P->digest_length;
  75. return 0;
  76. }
  77. /* Sequential blake2b initialization */
  78. int kp_blake2b_init(blake2b_state *S, size_t outlen) {
  79. blake2b_param P;
  80. if (S == NULL) {
  81. return -1;
  82. }
  83. if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
  84. blake2b_invalidate_state(S);
  85. return -1;
  86. }
  87. /* Setup Parameter Block for unkeyed BLAKE2 */
  88. P.digest_length = (uint8_t)outlen;
  89. P.key_length = 0;
  90. P.fanout = 1;
  91. P.depth = 1;
  92. P.leaf_length = 0;
  93. P.node_offset = 0;
  94. P.node_depth = 0;
  95. P.inner_length = 0;
  96. memset(P.reserved, 0, sizeof(P.reserved));
  97. memset(P.salt, 0, sizeof(P.salt));
  98. memset(P.personal, 0, sizeof(P.personal));
  99. return kp_blake2b_init_param(S, &P);
  100. }
  101. int kp_blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
  102. size_t keylen) {
  103. blake2b_param P;
  104. if (S == NULL) {
  105. return -1;
  106. }
  107. if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
  108. blake2b_invalidate_state(S);
  109. return -1;
  110. }
  111. if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) {
  112. blake2b_invalidate_state(S);
  113. return -1;
  114. }
  115. /* Setup Parameter Block for keyed BLAKE2 */
  116. P.digest_length = (uint8_t)outlen;
  117. P.key_length = (uint8_t)keylen;
  118. P.fanout = 1;
  119. P.depth = 1;
  120. P.leaf_length = 0;
  121. P.node_offset = 0;
  122. P.node_depth = 0;
  123. P.inner_length = 0;
  124. memset(P.reserved, 0, sizeof(P.reserved));
  125. memset(P.salt, 0, sizeof(P.salt));
  126. memset(P.personal, 0, sizeof(P.personal));
  127. if (kp_blake2b_init_param(S, &P) < 0) {
  128. blake2b_invalidate_state(S);
  129. return -1;
  130. }
  131. {
  132. uint8_t block[BLAKE2B_BLOCKBYTES];
  133. memset(block, 0, BLAKE2B_BLOCKBYTES);
  134. memcpy(block, key, keylen);
  135. kp_blake2b_update(S, block, BLAKE2B_BLOCKBYTES);
  136. /* Burn the key from stack */
  137. clear_internal_memory(block, BLAKE2B_BLOCKBYTES);
  138. }
  139. return 0;
  140. }
  141. static void blake2b_compress(blake2b_state *S, const uint8_t *block) {
  142. uint64_t m[16];
  143. uint64_t v[16];
  144. unsigned int i, r;
  145. for (i = 0; i < 16; ++i) {
  146. m[i] = load64(block + i * sizeof(m[i]));
  147. }
  148. for (i = 0; i < 8; ++i) {
  149. v[i] = S->h[i];
  150. }
  151. v[8] = blake2b_IV[0];
  152. v[9] = blake2b_IV[1];
  153. v[10] = blake2b_IV[2];
  154. v[11] = blake2b_IV[3];
  155. v[12] = blake2b_IV[4] ^ S->t[0];
  156. v[13] = blake2b_IV[5] ^ S->t[1];
  157. v[14] = blake2b_IV[6] ^ S->f[0];
  158. v[15] = blake2b_IV[7] ^ S->f[1];
  159. #define G(r, i, a, b, c, d) \
  160. do { \
  161. a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \
  162. d = rotr64(d ^ a, 32); \
  163. c = c + d; \
  164. b = rotr64(b ^ c, 24); \
  165. a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \
  166. d = rotr64(d ^ a, 16); \
  167. c = c + d; \
  168. b = rotr64(b ^ c, 63); \
  169. } while ((void)0, 0)
  170. #define ROUND(r) \
  171. do { \
  172. G(r, 0, v[0], v[4], v[8], v[12]); \
  173. G(r, 1, v[1], v[5], v[9], v[13]); \
  174. G(r, 2, v[2], v[6], v[10], v[14]); \
  175. G(r, 3, v[3], v[7], v[11], v[15]); \
  176. G(r, 4, v[0], v[5], v[10], v[15]); \
  177. G(r, 5, v[1], v[6], v[11], v[12]); \
  178. G(r, 6, v[2], v[7], v[8], v[13]); \
  179. G(r, 7, v[3], v[4], v[9], v[14]); \
  180. } while ((void)0, 0)
  181. for (r = 0; r < 12; ++r) {
  182. ROUND(r);
  183. }
  184. for (i = 0; i < 8; ++i) {
  185. S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
  186. }
  187. #undef G
  188. #undef ROUND
  189. }
  190. int kp_blake2b_update(blake2b_state *S, const void *in, size_t inlen) {
  191. const uint8_t *pin = (const uint8_t *)in;
  192. if (inlen == 0) {
  193. return 0;
  194. }
  195. /* Sanity check */
  196. if (S == NULL || in == NULL) {
  197. return -1;
  198. }
  199. /* Is this a reused state? */
  200. if (S->f[0] != 0) {
  201. return -1;
  202. }
  203. if (S->buflen + inlen > BLAKE2B_BLOCKBYTES) {
  204. /* Complete current block */
  205. size_t left = S->buflen;
  206. size_t fill = BLAKE2B_BLOCKBYTES - left;
  207. memcpy(&S->buf[left], pin, fill);
  208. blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
  209. blake2b_compress(S, S->buf);
  210. S->buflen = 0;
  211. inlen -= fill;
  212. pin += fill;
  213. /* Avoid buffer copies when possible */
  214. while (inlen > BLAKE2B_BLOCKBYTES) {
  215. blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
  216. blake2b_compress(S, pin);
  217. inlen -= BLAKE2B_BLOCKBYTES;
  218. pin += BLAKE2B_BLOCKBYTES;
  219. }
  220. }
  221. memcpy(&S->buf[S->buflen], pin, inlen);
  222. S->buflen += (unsigned int)inlen;
  223. return 0;
  224. }
  225. int kp_blake2b_final(blake2b_state *S, void *out, size_t outlen) {
  226. uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
  227. unsigned int i;
  228. /* Sanity checks */
  229. if (S == NULL || out == NULL || outlen < S->outlen) {
  230. return -1;
  231. }
  232. /* Is this a reused state? */
  233. if (S->f[0] != 0) {
  234. return -1;
  235. }
  236. blake2b_increment_counter(S, S->buflen);
  237. blake2b_set_lastblock(S);
  238. memset(&S->buf[S->buflen], 0, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
  239. blake2b_compress(S, S->buf);
  240. for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
  241. store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
  242. }
  243. memcpy(out, buffer, S->outlen);
  244. clear_internal_memory(buffer, sizeof(buffer));
  245. clear_internal_memory(S->buf, sizeof(S->buf));
  246. clear_internal_memory(S->h, sizeof(S->h));
  247. return 0;
  248. }
  249. int kp_blake2b(void *out, size_t outlen, const void *in, size_t inlen,
  250. const void *key, size_t keylen) {
  251. blake2b_state S;
  252. int ret = -1;
  253. /* Verify parameters */
  254. if (NULL == in && inlen > 0) {
  255. goto fail;
  256. }
  257. if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) {
  258. goto fail;
  259. }
  260. if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) {
  261. goto fail;
  262. }
  263. if (keylen > 0) {
  264. if (kp_blake2b_init_key(&S, outlen, key, keylen) < 0) {
  265. goto fail;
  266. }
  267. } else {
  268. if (kp_blake2b_init(&S, outlen) < 0) {
  269. goto fail;
  270. }
  271. }
  272. if (kp_blake2b_update(&S, in, inlen) < 0) {
  273. goto fail;
  274. }
  275. ret = kp_blake2b_final(&S, out, outlen);
  276. fail:
  277. clear_internal_memory(&S, sizeof(S));
  278. return ret;
  279. }
  280. /* Argon2 Team - Begin Code */
  281. int kp_blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) {
  282. uint8_t *out = (uint8_t *)pout;
  283. blake2b_state blake_state;
  284. uint8_t outlen_bytes[sizeof(uint32_t)] = {0};
  285. int ret = -1;
  286. if (outlen > UINT32_MAX) {
  287. goto fail;
  288. }
  289. /* Ensure little-endian byte order! */
  290. store32(outlen_bytes, (uint32_t)outlen);
  291. #define TRY(statement) \
  292. do { \
  293. ret = statement; \
  294. if (ret < 0) { \
  295. goto fail; \
  296. } \
  297. } while ((void)0, 0)
  298. if (outlen <= BLAKE2B_OUTBYTES) {
  299. TRY(kp_blake2b_init(&blake_state, outlen));
  300. TRY(kp_blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
  301. TRY(kp_blake2b_update(&blake_state, in, inlen));
  302. TRY(kp_blake2b_final(&blake_state, out, outlen));
  303. } else {
  304. uint32_t toproduce;
  305. uint8_t out_buffer[BLAKE2B_OUTBYTES];
  306. uint8_t in_buffer[BLAKE2B_OUTBYTES];
  307. TRY(kp_blake2b_init(&blake_state, BLAKE2B_OUTBYTES));
  308. TRY(kp_blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
  309. TRY(kp_blake2b_update(&blake_state, in, inlen));
  310. TRY(kp_blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES));
  311. memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
  312. out += BLAKE2B_OUTBYTES / 2;
  313. toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2;
  314. while (toproduce > BLAKE2B_OUTBYTES) {
  315. memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
  316. TRY(kp_blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer,
  317. BLAKE2B_OUTBYTES, NULL, 0));
  318. memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
  319. out += BLAKE2B_OUTBYTES / 2;
  320. toproduce -= BLAKE2B_OUTBYTES / 2;
  321. }
  322. memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
  323. TRY(kp_blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL,
  324. 0));
  325. memcpy(out, out_buffer, toproduce);
  326. }
  327. fail:
  328. clear_internal_memory(&blake_state, sizeof(blake_state));
  329. return ret;
  330. #undef TRY
  331. }
  332. /* Argon2 Team - End Code */