blake2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. BLAKE2 reference source code package - reference C implementations
  3. Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
  4. To the extent possible under law, the author(s) have dedicated all copyright
  5. and related and neighboring rights to this software to the public domain
  6. worldwide. This software is distributed without any warranty.
  7. You should have received a copy of the CC0 Public Domain Dedication along with
  8. this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. */
  10. /* blake2s.c
  11. *
  12. * Copyright (C) 2006-2023 wolfSSL Inc.
  13. *
  14. * This file is part of wolfSSL.
  15. *
  16. * wolfSSL is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * wolfSSL is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  29. */
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. #include <wolfssl/wolfcrypt/settings.h>
  34. #ifdef HAVE_BLAKE2S
  35. #include <wolfssl/wolfcrypt/blake2.h>
  36. #include <wolfssl/wolfcrypt/blake2-impl.h>
  37. #include <wolfssl/wolfcrypt/error-crypt.h>
  38. static const word32 blake2s_IV[8] =
  39. {
  40. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  41. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  42. };
  43. static const byte blake2s_sigma[10][16] =
  44. {
  45. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
  46. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
  47. { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
  48. { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
  49. { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
  50. { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
  51. { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
  52. { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
  53. { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
  54. { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 }
  55. };
  56. static WC_INLINE int blake2s_set_lastnode( blake2s_state *S )
  57. {
  58. S->f[1] = ~0U;
  59. return 0;
  60. }
  61. /* Some helper functions, not necessarily useful */
  62. static WC_INLINE int blake2s_set_lastblock( blake2s_state *S )
  63. {
  64. if( S->last_node ) blake2s_set_lastnode( S );
  65. S->f[0] = ~0U;
  66. return 0;
  67. }
  68. static WC_INLINE int blake2s_increment_counter( blake2s_state *S, const word32
  69. inc )
  70. {
  71. S->t[0] += inc;
  72. S->t[1] += ( S->t[0] < inc );
  73. return 0;
  74. }
  75. static WC_INLINE int blake2s_init0( blake2s_state *S )
  76. {
  77. int i;
  78. XMEMSET( S, 0, sizeof( blake2s_state ) );
  79. for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
  80. return 0;
  81. }
  82. /* init xors IV with input parameter block */
  83. int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
  84. {
  85. word32 i;
  86. byte *p ;
  87. blake2s_init0( S );
  88. p = ( byte * )( P );
  89. /* IV XOR ParamBlock */
  90. for( i = 0; i < 8; ++i )
  91. S->h[i] ^= load32( p + sizeof( S->h[i] ) * i );
  92. return 0;
  93. }
  94. int blake2s_init( blake2s_state *S, const byte outlen )
  95. {
  96. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  97. blake2s_param P[1];
  98. #else
  99. volatile blake2s_param P[1];
  100. #endif
  101. if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG;
  102. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  103. P->digest_length = outlen;
  104. P->key_length = 0;
  105. P->fanout = 1;
  106. P->depth = 1;
  107. store32( &P->leaf_length, 0 );
  108. store32( &P->node_offset, 0 );
  109. P->node_depth = 0;
  110. P->inner_length = 0;
  111. XMEMSET( P->salt, 0, sizeof( P->salt ) );
  112. XMEMSET( P->personal, 0, sizeof( P->personal ) );
  113. #else
  114. XMEMSET( (blake2s_param *)P, 0, sizeof( *P ) );
  115. P->digest_length = outlen;
  116. P->fanout = 1;
  117. P->depth = 1;
  118. #endif
  119. return blake2s_init_param( S, (blake2s_param *)P );
  120. }
  121. int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key,
  122. const byte keylen )
  123. {
  124. int ret = 0;
  125. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  126. blake2s_param P[1];
  127. #else
  128. volatile blake2s_param P[1];
  129. #endif
  130. if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG;
  131. if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return BAD_FUNC_ARG;
  132. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  133. P->digest_length = outlen;
  134. P->key_length = keylen;
  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. XMEMSET( P->salt, 0, sizeof( P->salt ) );
  142. XMEMSET( P->personal, 0, sizeof( P->personal ) );
  143. #else
  144. XMEMSET( (blake2s_param *)P, 0, sizeof( *P ) );
  145. P->digest_length = outlen;
  146. P->key_length = keylen;
  147. P->fanout = 1;
  148. P->depth = 1;
  149. #endif
  150. ret = blake2s_init_param( S, (blake2s_param *)P );
  151. if (ret < 0)
  152. return ret;
  153. {
  154. #ifdef WOLFSSL_SMALL_STACK
  155. byte* block;
  156. block = (byte*)XMALLOC(BLAKE2S_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  157. if ( block == NULL ) return MEMORY_E;
  158. #else
  159. byte block[BLAKE2S_BLOCKBYTES];
  160. #endif
  161. XMEMSET( block, 0, BLAKE2S_BLOCKBYTES );
  162. XMEMCPY( block, key, keylen );
  163. ret = blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
  164. secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from */
  165. /* memory */
  166. #ifdef WOLFSSL_SMALL_STACK
  167. XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  168. #endif
  169. }
  170. return ret;
  171. }
  172. static WC_INLINE int blake2s_compress(
  173. blake2s_state *S,
  174. const byte block[BLAKE2S_BLOCKBYTES],
  175. word32* m,
  176. word32* v)
  177. {
  178. word32 i;
  179. for( i = 0; i < 16; ++i )
  180. m[i] = load32( block + i * sizeof( m[i] ) );
  181. for( i = 0; i < 8; ++i )
  182. v[i] = S->h[i];
  183. v[ 8] = blake2s_IV[0];
  184. v[ 9] = blake2s_IV[1];
  185. v[10] = blake2s_IV[2];
  186. v[11] = blake2s_IV[3];
  187. v[12] = S->t[0] ^ blake2s_IV[4];
  188. v[13] = S->t[1] ^ blake2s_IV[5];
  189. v[14] = S->f[0] ^ blake2s_IV[6];
  190. v[15] = S->f[1] ^ blake2s_IV[7];
  191. #define G(r,i,a,b,c,d) \
  192. do { \
  193. (a) = (a) + (b) + m[blake2s_sigma[r][2*(i)+0]]; \
  194. (d) = rotr32((d) ^ (a), 16); \
  195. (c) = (c) + (d); \
  196. (b) = rotr32((b) ^ (c), 12); \
  197. (a) = (a) + (b) + m[blake2s_sigma[r][2*(i)+1]]; \
  198. (d) = rotr32((d) ^ (a), 8); \
  199. (c) = (c) + (d); \
  200. (b) = rotr32((b) ^ (c), 7); \
  201. } while(0)
  202. #define ROUND(r) \
  203. do { \
  204. G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
  205. G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
  206. G(r,2,v[ 2],v[ 6],v[10],v[14]); \
  207. G(r,3,v[ 3],v[ 7],v[11],v[15]); \
  208. G(r,4,v[ 0],v[ 5],v[10],v[15]); \
  209. G(r,5,v[ 1],v[ 6],v[11],v[12]); \
  210. G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
  211. G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
  212. } while(0)
  213. ROUND( 0 );
  214. ROUND( 1 );
  215. ROUND( 2 );
  216. ROUND( 3 );
  217. ROUND( 4 );
  218. ROUND( 5 );
  219. ROUND( 6 );
  220. ROUND( 7 );
  221. ROUND( 8 );
  222. ROUND( 9 );
  223. for( i = 0; i < 8; ++i )
  224. S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
  225. #undef G
  226. #undef ROUND
  227. return 0;
  228. }
  229. /* inlen now in bytes */
  230. int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
  231. {
  232. int ret = 0;
  233. #ifdef WOLFSSL_SMALL_STACK
  234. word32* m;
  235. word32* v;
  236. m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  237. if ( m == NULL ) return MEMORY_E;
  238. v = &m[16];
  239. #else
  240. word32 m[16];
  241. word32 v[16];
  242. #endif
  243. while( inlen > 0 )
  244. {
  245. word32 left = S->buflen;
  246. word32 fill = 2 * BLAKE2S_BLOCKBYTES - left;
  247. if( inlen > fill )
  248. {
  249. XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
  250. S->buflen += fill;
  251. blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
  252. {
  253. ret= blake2s_compress( S, S->buf, m, v );
  254. if (ret < 0) break;
  255. }
  256. XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
  257. /* Shift buffer left */
  258. S->buflen -= BLAKE2S_BLOCKBYTES;
  259. in += fill;
  260. inlen -= fill;
  261. }
  262. else /* inlen <= fill */
  263. {
  264. XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
  265. S->buflen += inlen; /* Be lazy, do not compress */
  266. inlen = 0;
  267. }
  268. }
  269. #ifdef WOLFSSL_SMALL_STACK
  270. XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  271. #endif
  272. return ret;
  273. }
  274. /* Is this correct? */
  275. int blake2s_final( blake2s_state *S, byte *out, byte outlen )
  276. {
  277. int ret = 0;
  278. word32 i;
  279. byte buffer[BLAKE2S_BLOCKBYTES];
  280. #ifdef WOLFSSL_SMALL_STACK
  281. word32* m;
  282. word32* v;
  283. m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  284. if ( m == NULL ) return MEMORY_E;
  285. v = &m[16];
  286. #else
  287. word32 m[16];
  288. word32 v[16];
  289. #endif
  290. if( S->buflen > BLAKE2S_BLOCKBYTES )
  291. {
  292. blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
  293. {
  294. ret = blake2s_compress( S, S->buf, m, v );
  295. if (ret < 0) goto out;
  296. }
  297. S->buflen -= BLAKE2S_BLOCKBYTES;
  298. XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, (wolfssl_word)S->buflen );
  299. }
  300. blake2s_increment_counter( S, S->buflen );
  301. blake2s_set_lastblock( S );
  302. XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) );
  303. /* Padding */
  304. {
  305. ret = blake2s_compress( S, S->buf, m, v );
  306. if (ret < 0) goto out;
  307. }
  308. for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
  309. store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
  310. XMEMCPY( out, buffer, outlen );
  311. out:
  312. #ifdef WOLFSSL_SMALL_STACK
  313. XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  314. #endif
  315. return ret;
  316. }
  317. /* inlen, at least, should be word32. Others can be size_t. */
  318. int blake2s( byte *out, const void *in, const void *key, const byte outlen,
  319. const word32 inlen, byte keylen )
  320. {
  321. blake2s_state S[1];
  322. /* Verify parameters */
  323. if ( NULL == in ) return BAD_FUNC_ARG;
  324. if ( NULL == out ) return BAD_FUNC_ARG;
  325. if( NULL == key ) keylen = 0;
  326. if( keylen > 0 )
  327. {
  328. int ret = blake2s_init_key( S, outlen, key, keylen );
  329. if (ret < 0) return ret;
  330. }
  331. else
  332. {
  333. int ret = blake2s_init( S, outlen );
  334. if (ret < 0) return ret;
  335. }
  336. {
  337. int ret = blake2s_update( S, ( byte * )in, inlen );
  338. if (ret < 0) return ret;
  339. }
  340. return blake2s_final( S, out, outlen );
  341. }
  342. #if defined(BLAKE2S_SELFTEST)
  343. #include <string.h>
  344. #include "blake2-kat.h"
  345. int main( int argc, char **argv )
  346. {
  347. byte key[BLAKE2S_KEYBYTES];
  348. byte buf[KAT_LENGTH];
  349. for( word32 i = 0; i < BLAKE2S_KEYBYTES; ++i )
  350. key[i] = ( byte )i;
  351. for( word32 i = 0; i < KAT_LENGTH; ++i )
  352. buf[i] = ( byte )i;
  353. for( word32 i = 0; i < KAT_LENGTH; ++i )
  354. {
  355. byte hash[BLAKE2S_OUTBYTES];
  356. if ( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 )
  357. {
  358. puts( "error" );
  359. return -1;
  360. }
  361. if( 0 != XMEMCMP( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
  362. {
  363. puts( "error" );
  364. return -1;
  365. }
  366. }
  367. puts( "ok" );
  368. return 0;
  369. }
  370. #endif
  371. /* wolfCrypt API */
  372. /* Init Blake2s digest, track size in case final doesn't want to "remember" */
  373. int wc_InitBlake2s(Blake2s* b2s, word32 digestSz)
  374. {
  375. if (b2s == NULL){
  376. return BAD_FUNC_ARG;
  377. }
  378. b2s->digestSz = digestSz;
  379. return blake2s_init(b2s->S, (byte)digestSz);
  380. }
  381. /* Init Blake2s digest with key, track size in case final doesn't want to "remember" */
  382. int wc_InitBlake2s_WithKey(Blake2s* b2s, word32 digestSz, const byte *key, word32 keylen)
  383. {
  384. if (b2s == NULL){
  385. return BAD_FUNC_ARG;
  386. }
  387. b2s->digestSz = digestSz;
  388. if (keylen >= 256)
  389. return BAD_FUNC_ARG;
  390. if (key)
  391. return blake2s_init_key(b2s->S, (byte)digestSz, key, (byte)keylen);
  392. else
  393. return blake2s_init(b2s->S, (byte)digestSz);
  394. }
  395. /* Blake2s Update */
  396. int wc_Blake2sUpdate(Blake2s* b2s, const byte* data, word32 sz)
  397. {
  398. return blake2s_update(b2s->S, data, sz);
  399. }
  400. /* Blake2s Final, if pass in zero size we use init digestSz */
  401. int wc_Blake2sFinal(Blake2s* b2s, byte* final, word32 requestSz)
  402. {
  403. word32 sz = requestSz ? requestSz : b2s->digestSz;
  404. return blake2s_final(b2s->S, final, (byte)sz);
  405. }
  406. /* end CTaoCrypt API */
  407. #endif /* HAVE_BLAKE2S */