blake2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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-2020 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] = ~0;
  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] = ~0;
  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. blake2s_param P[1];
  97. if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG;
  98. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  99. P->digest_length = outlen;
  100. P->key_length = 0;
  101. P->fanout = 1;
  102. P->depth = 1;
  103. store32( &P->leaf_length, 0 );
  104. store32( &P->node_offset, 0 );
  105. P->node_depth = 0;
  106. P->inner_length = 0;
  107. XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
  108. XMEMSET( P->salt, 0, sizeof( P->salt ) );
  109. XMEMSET( P->personal, 0, sizeof( P->personal ) );
  110. #else
  111. XMEMSET( P, 0, sizeof( *P ) );
  112. P->digest_length = outlen;
  113. P->fanout = 1;
  114. P->depth = 1;
  115. #endif
  116. return blake2s_init_param( S, P );
  117. }
  118. int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key,
  119. const byte keylen )
  120. {
  121. blake2s_param P[1];
  122. if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG;
  123. if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return BAD_FUNC_ARG;
  124. #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
  125. P->digest_length = outlen;
  126. P->key_length = keylen;
  127. P->fanout = 1;
  128. P->depth = 1;
  129. store32( &P->leaf_length, 0 );
  130. store64( &P->node_offset, 0 );
  131. P->node_depth = 0;
  132. P->inner_length = 0;
  133. XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
  134. XMEMSET( P->salt, 0, sizeof( P->salt ) );
  135. XMEMSET( P->personal, 0, sizeof( P->personal ) );
  136. #else
  137. XMEMSET( P, 0, sizeof( *P ) );
  138. P->digest_length = outlen;
  139. P->key_length = keylen;
  140. P->fanout = 1;
  141. P->depth = 1;
  142. #endif
  143. {
  144. int ret = blake2s_init_param( S, P );
  145. if (ret < 0)
  146. return ret;
  147. }
  148. {
  149. #ifdef WOLFSSL_SMALL_STACK
  150. byte* block;
  151. block = (byte*)XMALLOC(BLAKE2S_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  152. if ( block == NULL ) return MEMORY_E;
  153. #else
  154. byte block[BLAKE2S_BLOCKBYTES];
  155. #endif
  156. XMEMSET( block, 0, BLAKE2S_BLOCKBYTES );
  157. XMEMCPY( block, key, keylen );
  158. blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
  159. secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from */
  160. /* memory */
  161. #ifdef WOLFSSL_SMALL_STACK
  162. XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  163. #endif
  164. }
  165. return 0;
  166. }
  167. static WC_INLINE int blake2s_compress(
  168. blake2s_state *S,
  169. const byte block[BLAKE2S_BLOCKBYTES],
  170. word32* m,
  171. word32* v)
  172. {
  173. int i;
  174. for( i = 0; i < 16; ++i )
  175. m[i] = load32( block + i * sizeof( m[i] ) );
  176. for( i = 0; i < 8; ++i )
  177. v[i] = S->h[i];
  178. v[ 8] = blake2s_IV[0];
  179. v[ 9] = blake2s_IV[1];
  180. v[10] = blake2s_IV[2];
  181. v[11] = blake2s_IV[3];
  182. v[12] = S->t[0] ^ blake2s_IV[4];
  183. v[13] = S->t[1] ^ blake2s_IV[5];
  184. v[14] = S->f[0] ^ blake2s_IV[6];
  185. v[15] = S->f[1] ^ blake2s_IV[7];
  186. #define G(r,i,a,b,c,d) \
  187. do { \
  188. a = a + b + m[blake2s_sigma[r][2*i+0]]; \
  189. d = rotr32(d ^ a, 16); \
  190. c = c + d; \
  191. b = rotr32(b ^ c, 12); \
  192. a = a + b + m[blake2s_sigma[r][2*i+1]]; \
  193. d = rotr32(d ^ a, 8); \
  194. c = c + d; \
  195. b = rotr32(b ^ c, 7); \
  196. } while(0)
  197. #define ROUND(r) \
  198. do { \
  199. G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
  200. G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
  201. G(r,2,v[ 2],v[ 6],v[10],v[14]); \
  202. G(r,3,v[ 3],v[ 7],v[11],v[15]); \
  203. G(r,4,v[ 0],v[ 5],v[10],v[15]); \
  204. G(r,5,v[ 1],v[ 6],v[11],v[12]); \
  205. G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
  206. G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
  207. } while(0)
  208. ROUND( 0 );
  209. ROUND( 1 );
  210. ROUND( 2 );
  211. ROUND( 3 );
  212. ROUND( 4 );
  213. ROUND( 5 );
  214. ROUND( 6 );
  215. ROUND( 7 );
  216. ROUND( 8 );
  217. ROUND( 9 );
  218. for( i = 0; i < 8; ++i )
  219. S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
  220. #undef G
  221. #undef ROUND
  222. return 0;
  223. }
  224. /* inlen now in bytes */
  225. int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
  226. {
  227. int ret = 0;
  228. #ifdef WOLFSSL_SMALL_STACK
  229. word32* m;
  230. word32* v;
  231. m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  232. if ( m == NULL ) return MEMORY_E;
  233. v = &m[16];
  234. #else
  235. word32 m[16];
  236. word32 v[16];
  237. #endif
  238. while( inlen > 0 )
  239. {
  240. word32 left = S->buflen;
  241. word32 fill = 2 * BLAKE2S_BLOCKBYTES - left;
  242. if( inlen > fill )
  243. {
  244. XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
  245. S->buflen += fill;
  246. blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
  247. {
  248. ret= blake2s_compress( S, S->buf, m, v );
  249. if (ret < 0) break;
  250. }
  251. XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
  252. /* Shift buffer left */
  253. S->buflen -= BLAKE2S_BLOCKBYTES;
  254. in += fill;
  255. inlen -= fill;
  256. }
  257. else /* inlen <= fill */
  258. {
  259. XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
  260. S->buflen += inlen; /* Be lazy, do not compress */
  261. inlen = 0;
  262. }
  263. }
  264. #ifdef WOLFSSL_SMALL_STACK
  265. XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  266. #endif
  267. return ret;
  268. }
  269. /* Is this correct? */
  270. int blake2s_final( blake2s_state *S, byte *out, byte outlen )
  271. {
  272. int ret = 0;
  273. int i;
  274. byte buffer[BLAKE2S_BLOCKBYTES];
  275. #ifdef WOLFSSL_SMALL_STACK
  276. word32* m;
  277. word32* v;
  278. m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  279. if ( m == NULL ) return MEMORY_E;
  280. v = &m[16];
  281. #else
  282. word32 m[16];
  283. word32 v[16];
  284. #endif
  285. if( S->buflen > BLAKE2S_BLOCKBYTES )
  286. {
  287. blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
  288. {
  289. ret = blake2s_compress( S, S->buf, m, v );
  290. if (ret < 0) goto out;
  291. }
  292. S->buflen -= BLAKE2S_BLOCKBYTES;
  293. XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, (wolfssl_word)S->buflen );
  294. }
  295. blake2s_increment_counter( S, S->buflen );
  296. blake2s_set_lastblock( S );
  297. XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) );
  298. /* Padding */
  299. {
  300. ret = blake2s_compress( S, S->buf, m, v );
  301. if (ret < 0) goto out;
  302. }
  303. for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
  304. store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
  305. XMEMCPY( out, buffer, outlen );
  306. out:
  307. #ifdef WOLFSSL_SMALL_STACK
  308. XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  309. #endif
  310. return ret;
  311. }
  312. /* inlen, at least, should be word32. Others can be size_t. */
  313. int blake2s( byte *out, const void *in, const void *key, const byte outlen,
  314. const word32 inlen, byte keylen )
  315. {
  316. blake2s_state S[1];
  317. /* Verify parameters */
  318. if ( NULL == in ) return BAD_FUNC_ARG;
  319. if ( NULL == out ) return BAD_FUNC_ARG;
  320. if( NULL == key ) keylen = 0;
  321. if( keylen > 0 )
  322. {
  323. int ret = blake2s_init_key( S, outlen, key, keylen );
  324. if (ret < 0) return ret;
  325. }
  326. else
  327. {
  328. int ret = blake2s_init( S, outlen );
  329. if (ret < 0) return ret;
  330. }
  331. {
  332. int ret = blake2s_update( S, ( byte * )in, inlen );
  333. if (ret < 0) return ret;
  334. }
  335. return blake2s_final( S, out, outlen );
  336. }
  337. #if defined(BLAKE2S_SELFTEST)
  338. #include <string.h>
  339. #include "blake2-kat.h"
  340. int main( int argc, char **argv )
  341. {
  342. byte key[BLAKE2S_KEYBYTES];
  343. byte buf[KAT_LENGTH];
  344. for( word32 i = 0; i < BLAKE2S_KEYBYTES; ++i )
  345. key[i] = ( byte )i;
  346. for( word32 i = 0; i < KAT_LENGTH; ++i )
  347. buf[i] = ( byte )i;
  348. for( word32 i = 0; i < KAT_LENGTH; ++i )
  349. {
  350. byte hash[BLAKE2S_OUTBYTES];
  351. if ( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 )
  352. {
  353. puts( "error" );
  354. return -1;
  355. }
  356. if( 0 != XMEMCMP( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
  357. {
  358. puts( "error" );
  359. return -1;
  360. }
  361. }
  362. puts( "ok" );
  363. return 0;
  364. }
  365. #endif
  366. /* wolfCrypt API */
  367. /* Init Blake2s digest, track size in case final doesn't want to "remember" */
  368. int wc_InitBlake2s(Blake2s* b2s, word32 digestSz)
  369. {
  370. if (b2s == NULL){
  371. return BAD_FUNC_ARG;
  372. }
  373. b2s->digestSz = digestSz;
  374. return blake2s_init(b2s->S, (byte)digestSz);
  375. }
  376. /* Init Blake2s digest with key, track size in case final doesn't want to "remember" */
  377. int wc_InitBlake2s_WithKey(Blake2s* b2s, word32 digestSz, const byte *key, word32 keylen)
  378. {
  379. if (b2s == NULL){
  380. return BAD_FUNC_ARG;
  381. }
  382. b2s->digestSz = digestSz;
  383. if (keylen >= 256)
  384. return BAD_FUNC_ARG;
  385. if (key)
  386. return blake2s_init_key(b2s->S, (byte)digestSz, key, (byte)keylen);
  387. else
  388. return blake2s_init(b2s->S, (byte)digestSz);
  389. }
  390. /* Blake2s Update */
  391. int wc_Blake2sUpdate(Blake2s* b2s, const byte* data, word32 sz)
  392. {
  393. return blake2s_update(b2s->S, data, sz);
  394. }
  395. /* Blake2s Final, if pass in zero size we use init digestSz */
  396. int wc_Blake2sFinal(Blake2s* b2s, byte* final, word32 requestSz)
  397. {
  398. word32 sz = requestSz ? requestSz : b2s->digestSz;
  399. return blake2s_final(b2s->S, final, (byte)sz);
  400. }
  401. /* end CTaoCrypt API */
  402. #endif /* HAVE_BLAKE2S */