blake2b_prov.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Derived from the BLAKE2 reference implementation written by Samuel Neves.
  11. * Copyright 2012, Samuel Neves <[email protected]>
  12. * More information about the BLAKE2 hash function and its implementations
  13. * can be found at https://blake2.net.
  14. */
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/core_names.h>
  19. #include <openssl/proverr.h>
  20. #include <openssl/err.h>
  21. #include "internal/numbers.h"
  22. #include "blake2_impl.h"
  23. #include "prov/blake2.h"
  24. static const OSSL_PARAM known_blake2b_ctx_params[] = {
  25. {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
  26. OSSL_PARAM_END
  27. };
  28. const OSSL_PARAM *ossl_blake2b_gettable_ctx_params(ossl_unused void *ctx,
  29. ossl_unused void *pctx)
  30. {
  31. return known_blake2b_ctx_params;
  32. }
  33. const OSSL_PARAM *ossl_blake2b_settable_ctx_params(ossl_unused void *ctx,
  34. ossl_unused void *pctx)
  35. {
  36. return known_blake2b_ctx_params;
  37. }
  38. int ossl_blake2b_get_ctx_params(void *vctx, OSSL_PARAM params[])
  39. {
  40. struct blake2b_md_data_st *mdctx = vctx;
  41. OSSL_PARAM *p;
  42. BLAKE2B_CTX *ctx = &mdctx->ctx;
  43. if (ctx == NULL)
  44. return 0;
  45. if (params == NULL)
  46. return 1;
  47. p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
  48. if (p != NULL
  49. && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) {
  50. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. int ossl_blake2b_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  56. {
  57. size_t size;
  58. struct blake2b_md_data_st *mdctx = vctx;
  59. const OSSL_PARAM *p;
  60. BLAKE2B_CTX *ctx = &mdctx->ctx;
  61. if (ctx == NULL)
  62. return 0;
  63. if (params == NULL)
  64. return 1;
  65. p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE);
  66. if (p != NULL) {
  67. if (!OSSL_PARAM_get_size_t(p, &size)) {
  68. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  69. return 0;
  70. }
  71. if (size < 1 || size > BLAKE2B_OUTBYTES) {
  72. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);
  73. return 0;
  74. }
  75. ossl_blake2b_param_set_digest_length(&mdctx->params, (uint8_t)size);
  76. }
  77. return 1;
  78. }
  79. static const uint64_t blake2b_IV[8] =
  80. {
  81. 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
  82. 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
  83. 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
  84. 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
  85. };
  86. static const uint8_t blake2b_sigma[12][16] =
  87. {
  88. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
  89. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
  90. { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
  91. { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
  92. { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
  93. { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
  94. { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
  95. { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
  96. { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
  97. { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
  98. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
  99. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
  100. };
  101. /* Set that it's the last block we'll compress */
  102. static ossl_inline void blake2b_set_lastblock(BLAKE2B_CTX *S)
  103. {
  104. S->f[0] = -1;
  105. }
  106. /* Initialize the hashing state. */
  107. static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
  108. {
  109. int i;
  110. memset(S, 0, sizeof(BLAKE2B_CTX));
  111. for (i = 0; i < 8; ++i) {
  112. S->h[i] = blake2b_IV[i];
  113. }
  114. }
  115. /* init xors IV with input parameter block and sets the output length */
  116. static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
  117. {
  118. size_t i;
  119. const uint8_t *p = (const uint8_t *)(P);
  120. blake2b_init0(S);
  121. S->outlen = P->digest_length;
  122. /* The param struct is carefully hand packed, and should be 64 bytes on
  123. * every platform. */
  124. assert(sizeof(BLAKE2B_PARAM) == 64);
  125. /* IV XOR ParamBlock */
  126. for (i = 0; i < 8; ++i) {
  127. S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
  128. }
  129. }
  130. /* Initialize the parameter block with default values */
  131. void ossl_blake2b_param_init(BLAKE2B_PARAM *P)
  132. {
  133. P->digest_length = BLAKE2B_DIGEST_LENGTH;
  134. P->key_length = 0;
  135. P->fanout = 1;
  136. P->depth = 1;
  137. store32(P->leaf_length, 0);
  138. store64(P->node_offset, 0);
  139. P->node_depth = 0;
  140. P->inner_length = 0;
  141. memset(P->reserved, 0, sizeof(P->reserved));
  142. memset(P->salt, 0, sizeof(P->salt));
  143. memset(P->personal, 0, sizeof(P->personal));
  144. }
  145. void ossl_blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen)
  146. {
  147. P->digest_length = outlen;
  148. }
  149. void ossl_blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen)
  150. {
  151. P->key_length = keylen;
  152. }
  153. void ossl_blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal,
  154. size_t len)
  155. {
  156. memcpy(P->personal, personal, len);
  157. memset(P->personal + len, 0, BLAKE2B_PERSONALBYTES - len);
  158. }
  159. void ossl_blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt,
  160. size_t len)
  161. {
  162. memcpy(P->salt, salt, len);
  163. memset(P->salt + len, 0, BLAKE2B_SALTBYTES - len);
  164. }
  165. /*
  166. * Initialize the hashing context with the given parameter block.
  167. * Always returns 1.
  168. */
  169. int ossl_blake2b_init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P)
  170. {
  171. blake2b_init_param(c, P);
  172. return 1;
  173. }
  174. /*
  175. * Initialize the hashing context with the given parameter block and key.
  176. * Always returns 1.
  177. */
  178. int ossl_blake2b_init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P,
  179. const void *key)
  180. {
  181. blake2b_init_param(c, P);
  182. /* Pad the key to form first data block */
  183. {
  184. uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
  185. memcpy(block, key, P->key_length);
  186. ossl_blake2b_update(c, block, BLAKE2B_BLOCKBYTES);
  187. OPENSSL_cleanse(block, BLAKE2B_BLOCKBYTES);
  188. }
  189. return 1;
  190. }
  191. /* Permute the state while xoring in the block of data. */
  192. static void blake2b_compress(BLAKE2B_CTX *S,
  193. const uint8_t *blocks,
  194. size_t len)
  195. {
  196. uint64_t m[16];
  197. uint64_t v[16];
  198. int i;
  199. size_t increment;
  200. /*
  201. * There are two distinct usage vectors for this function:
  202. *
  203. * a) BLAKE2b_Update uses it to process complete blocks,
  204. * possibly more than one at a time;
  205. *
  206. * b) BLAK2b_Final uses it to process last block, always
  207. * single but possibly incomplete, in which case caller
  208. * pads input with zeros.
  209. */
  210. assert(len < BLAKE2B_BLOCKBYTES || len % BLAKE2B_BLOCKBYTES == 0);
  211. /*
  212. * Since last block is always processed with separate call,
  213. * |len| not being multiple of complete blocks can be observed
  214. * only with |len| being less than BLAKE2B_BLOCKBYTES ("less"
  215. * including even zero), which is why following assignment doesn't
  216. * have to reside inside the main loop below.
  217. */
  218. increment = len < BLAKE2B_BLOCKBYTES ? len : BLAKE2B_BLOCKBYTES;
  219. for (i = 0; i < 8; ++i) {
  220. v[i] = S->h[i];
  221. }
  222. do {
  223. for (i = 0; i < 16; ++i) {
  224. m[i] = load64(blocks + i * sizeof(m[i]));
  225. }
  226. /* blake2b_increment_counter */
  227. S->t[0] += increment;
  228. S->t[1] += (S->t[0] < increment);
  229. v[8] = blake2b_IV[0];
  230. v[9] = blake2b_IV[1];
  231. v[10] = blake2b_IV[2];
  232. v[11] = blake2b_IV[3];
  233. v[12] = S->t[0] ^ blake2b_IV[4];
  234. v[13] = S->t[1] ^ blake2b_IV[5];
  235. v[14] = S->f[0] ^ blake2b_IV[6];
  236. v[15] = S->f[1] ^ blake2b_IV[7];
  237. #define G(r,i,a,b,c,d) \
  238. do { \
  239. a = a + b + m[blake2b_sigma[r][2*i+0]]; \
  240. d = rotr64(d ^ a, 32); \
  241. c = c + d; \
  242. b = rotr64(b ^ c, 24); \
  243. a = a + b + m[blake2b_sigma[r][2*i+1]]; \
  244. d = rotr64(d ^ a, 16); \
  245. c = c + d; \
  246. b = rotr64(b ^ c, 63); \
  247. } while (0)
  248. #define ROUND(r) \
  249. do { \
  250. G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
  251. G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
  252. G(r,2,v[ 2],v[ 6],v[10],v[14]); \
  253. G(r,3,v[ 3],v[ 7],v[11],v[15]); \
  254. G(r,4,v[ 0],v[ 5],v[10],v[15]); \
  255. G(r,5,v[ 1],v[ 6],v[11],v[12]); \
  256. G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
  257. G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
  258. } while (0)
  259. #if defined(OPENSSL_SMALL_FOOTPRINT)
  260. /* 3x size reduction on x86_64, almost 7x on ARMv8, 9x on ARMv4 */
  261. for (i = 0; i < 12; i++) {
  262. ROUND(i);
  263. }
  264. #else
  265. ROUND(0);
  266. ROUND(1);
  267. ROUND(2);
  268. ROUND(3);
  269. ROUND(4);
  270. ROUND(5);
  271. ROUND(6);
  272. ROUND(7);
  273. ROUND(8);
  274. ROUND(9);
  275. ROUND(10);
  276. ROUND(11);
  277. #endif
  278. for (i = 0; i < 8; ++i) {
  279. S->h[i] = v[i] ^= v[i + 8] ^ S->h[i];
  280. }
  281. #undef G
  282. #undef ROUND
  283. blocks += increment;
  284. len -= increment;
  285. } while (len);
  286. }
  287. /* Absorb the input data into the hash state. Always returns 1. */
  288. int ossl_blake2b_update(BLAKE2B_CTX *c, const void *data, size_t datalen)
  289. {
  290. const uint8_t *in = data;
  291. size_t fill;
  292. /*
  293. * Intuitively one would expect intermediate buffer, c->buf, to
  294. * store incomplete blocks. But in this case we are interested to
  295. * temporarily stash even complete blocks, because last one in the
  296. * stream has to be treated in special way, and at this point we
  297. * don't know if last block in *this* call is last one "ever". This
  298. * is the reason for why |datalen| is compared as >, and not >=.
  299. */
  300. fill = sizeof(c->buf) - c->buflen;
  301. if (datalen > fill) {
  302. if (c->buflen) {
  303. memcpy(c->buf + c->buflen, in, fill); /* Fill buffer */
  304. blake2b_compress(c, c->buf, BLAKE2B_BLOCKBYTES);
  305. c->buflen = 0;
  306. in += fill;
  307. datalen -= fill;
  308. }
  309. if (datalen > BLAKE2B_BLOCKBYTES) {
  310. size_t stashlen = datalen % BLAKE2B_BLOCKBYTES;
  311. /*
  312. * If |datalen| is a multiple of the blocksize, stash
  313. * last complete block, it can be final one...
  314. */
  315. stashlen = stashlen ? stashlen : BLAKE2B_BLOCKBYTES;
  316. datalen -= stashlen;
  317. blake2b_compress(c, in, datalen);
  318. in += datalen;
  319. datalen = stashlen;
  320. }
  321. }
  322. assert(datalen <= BLAKE2B_BLOCKBYTES);
  323. memcpy(c->buf + c->buflen, in, datalen);
  324. c->buflen += datalen; /* Be lazy, do not compress */
  325. return 1;
  326. }
  327. /*
  328. * Calculate the final hash and save it in md.
  329. * Always returns 1.
  330. */
  331. int ossl_blake2b_final(unsigned char *md, BLAKE2B_CTX *c)
  332. {
  333. uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
  334. uint8_t *target = outbuffer;
  335. int iter = (c->outlen + 7) / 8;
  336. int i;
  337. /* Avoid writing to the temporary buffer if possible */
  338. if ((c->outlen % sizeof(c->h[0])) == 0)
  339. target = md;
  340. blake2b_set_lastblock(c);
  341. /* Padding */
  342. memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
  343. blake2b_compress(c, c->buf, c->buflen);
  344. /* Output full hash to buffer */
  345. for (i = 0; i < iter; ++i)
  346. store64(target + sizeof(c->h[i]) * i, c->h[i]);
  347. if (target != md)
  348. memcpy(md, target, c->outlen);
  349. OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
  350. return 1;
  351. }