2
0

sha2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The free distribution and use of this software in both source and binary
  6. form is allowed (with or without changes) provided that:
  7. 1. distributions of this source code include the above copyright
  8. notice, this list of conditions and the following disclaimer;
  9. 2. distributions in binary form include the above copyright
  10. notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other associated materials;
  12. 3. the copyright holder's name is not used to endorse products
  13. built using this software without specific written permission.
  14. ALTERNATIVELY, provided that this notice is retained in full, this product
  15. may be distributed under the terms of the GNU General Public License (GPL),
  16. in which case the provisions of the GPL apply INSTEAD OF those given above.
  17. DISCLAIMER
  18. This software is provided 'as is' with no explicit or implied warranties
  19. in respect of its properties, including, but not limited to, correctness
  20. and/or fitness for purpose.
  21. ---------------------------------------------------------------------------
  22. Issue Date: 01/08/2005
  23. This is a byte oriented version of SHA2 that operates on arrays of bytes
  24. stored in memory. This code implements sha256, sha384 and sha512 but the
  25. latter two functions rely on efficient 64-bit integer operations that
  26. may not be very efficient on 32-bit machines
  27. The sha256 functions use a type 'sha256_ctx' to hold details of the
  28. current hash state and uses the following three calls:
  29. void sha256_begin(sha256_ctx ctx[1])
  30. void sha256_hash(const unsigned char data[],
  31. unsigned long len, sha256_ctx ctx[1])
  32. void sha_end1(unsigned char hval[], sha256_ctx ctx[1])
  33. The first subroutine initialises a hash computation by setting up the
  34. context in the sha256_ctx context. The second subroutine hashes 8-bit
  35. bytes from array data[] into the hash state withinh sha256_ctx context,
  36. the number of bytes to be hashed being given by the the unsigned long
  37. integer len. The third subroutine completes the hash calculation and
  38. places the resulting digest value in the array of 8-bit bytes hval[].
  39. The sha384 and sha512 functions are similar and use the interfaces:
  40. void sha384_begin(sha384_ctx ctx[1]);
  41. void sha384_hash(const unsigned char data[],
  42. unsigned long len, sha384_ctx ctx[1]);
  43. void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
  44. void sha512_begin(sha512_ctx ctx[1]);
  45. void sha512_hash(const unsigned char data[],
  46. unsigned long len, sha512_ctx ctx[1]);
  47. void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
  48. In addition there is a function sha2 that can be used to call all these
  49. functions using a call with a hash length parameter as follows:
  50. int sha2_begin(unsigned long len, sha2_ctx ctx[1]);
  51. void sha2_hash(const unsigned char data[],
  52. unsigned long len, sha2_ctx ctx[1]);
  53. void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
  54. My thanks to Erik Andersen <andersen@codepoet.org> for testing this code
  55. on big-endian systems and for his assistance with corrections
  56. */
  57. #if 0
  58. #define UNROLL_SHA2 /* for SHA2 loop unroll */
  59. #endif
  60. #include <string.h> /* for memcpy() etc. */
  61. #include "sha2.h"
  62. //#include "brg_endian.h"
  63. #include "bg2zrtp.h"
  64. #if defined(__cplusplus)
  65. extern "C"
  66. {
  67. #endif
  68. #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
  69. #pragma intrinsic(memcpy)
  70. #endif
  71. #if 0 && defined(_MSC_VER)
  72. #define rotl32 _lrotl
  73. #define rotr32 _lrotr
  74. #else
  75. #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  76. #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
  77. #endif
  78. #if !defined(bswap_32)
  79. #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
  80. #endif
  81. #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
  82. #define SWAP_BYTES
  83. #else
  84. #undef SWAP_BYTES
  85. #endif
  86. #if 0
  87. #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  88. #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  89. #else /* Thanks to Rich Schroeppel and Colin Plumb for the following */
  90. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  91. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
  92. #endif
  93. /* round transforms for SHA256 and SHA512 compression functions */
  94. #define vf(n,i) v[(n - i) & 7]
  95. #define hf(i) (p[i & 15] += \
  96. g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))
  97. #define v_cycle(i,j) \
  98. vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j] \
  99. + s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i)); \
  100. vf(3,i) += vf(7,i); \
  101. vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))
  102. #if defined(SHA_224) || defined(SHA_256)
  103. #define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
  104. #if defined(SWAP_BYTES)
  105. #define bsw_32(p,n) \
  106. { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); }
  107. #else
  108. #define bsw_32(p,n)
  109. #endif
  110. #define s_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
  111. #define s_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
  112. #define g_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
  113. #define g_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
  114. #define k_0 k256
  115. /* rotated SHA256 round definition. Rather than swapping variables as in */
  116. /* FIPS-180, different variables are 'rotated' on each round, returning */
  117. /* to their starting positions every eight rounds */
  118. #define q(n) v##n
  119. #define one_cycle(a,b,c,d,e,f,g,h,k,w) \
  120. q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
  121. q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))
  122. /* SHA256 mixing data */
  123. const uint_32t k256[64] =
  124. { 0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul,
  125. 0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,
  126. 0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul,
  127. 0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,
  128. 0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul,
  129. 0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,
  130. 0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul,
  131. 0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,
  132. 0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul,
  133. 0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,
  134. 0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul,
  135. 0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul,
  136. 0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul,
  137. 0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul,
  138. 0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul,
  139. 0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul,
  140. };
  141. /* Compile 64 bytes of hash data into SHA256 digest value */
  142. /* NOTE: this routine assumes that the byte order in the */
  143. /* ctx->wbuf[] at this point is such that low address bytes */
  144. /* in the ORIGINAL byte stream will go into the high end of */
  145. /* words on BOTH big and little endian systems */
  146. VOID_RETURN sha256_compile(sha256_ctx ctx[1])
  147. {
  148. #if !defined(UNROLL_SHA2)
  149. uint_32t j, *p = ctx->wbuf, v[8];
  150. memcpy(v, ctx->hash, 8 * sizeof(uint_32t));
  151. for(j = 0; j < 64; j += 16)
  152. {
  153. v_cycle( 0, j); v_cycle( 1, j);
  154. v_cycle( 2, j); v_cycle( 3, j);
  155. v_cycle( 4, j); v_cycle( 5, j);
  156. v_cycle( 6, j); v_cycle( 7, j);
  157. v_cycle( 8, j); v_cycle( 9, j);
  158. v_cycle(10, j); v_cycle(11, j);
  159. v_cycle(12, j); v_cycle(13, j);
  160. v_cycle(14, j); v_cycle(15, j);
  161. }
  162. ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
  163. ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  164. ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
  165. ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  166. #else
  167. uint_32t *p = ctx->wbuf,v0,v1,v2,v3,v4,v5,v6,v7;
  168. v0 = ctx->hash[0]; v1 = ctx->hash[1];
  169. v2 = ctx->hash[2]; v3 = ctx->hash[3];
  170. v4 = ctx->hash[4]; v5 = ctx->hash[5];
  171. v6 = ctx->hash[6]; v7 = ctx->hash[7];
  172. one_cycle(0,1,2,3,4,5,6,7,k256[ 0],p[ 0]);
  173. one_cycle(7,0,1,2,3,4,5,6,k256[ 1],p[ 1]);
  174. one_cycle(6,7,0,1,2,3,4,5,k256[ 2],p[ 2]);
  175. one_cycle(5,6,7,0,1,2,3,4,k256[ 3],p[ 3]);
  176. one_cycle(4,5,6,7,0,1,2,3,k256[ 4],p[ 4]);
  177. one_cycle(3,4,5,6,7,0,1,2,k256[ 5],p[ 5]);
  178. one_cycle(2,3,4,5,6,7,0,1,k256[ 6],p[ 6]);
  179. one_cycle(1,2,3,4,5,6,7,0,k256[ 7],p[ 7]);
  180. one_cycle(0,1,2,3,4,5,6,7,k256[ 8],p[ 8]);
  181. one_cycle(7,0,1,2,3,4,5,6,k256[ 9],p[ 9]);
  182. one_cycle(6,7,0,1,2,3,4,5,k256[10],p[10]);
  183. one_cycle(5,6,7,0,1,2,3,4,k256[11],p[11]);
  184. one_cycle(4,5,6,7,0,1,2,3,k256[12],p[12]);
  185. one_cycle(3,4,5,6,7,0,1,2,k256[13],p[13]);
  186. one_cycle(2,3,4,5,6,7,0,1,k256[14],p[14]);
  187. one_cycle(1,2,3,4,5,6,7,0,k256[15],p[15]);
  188. one_cycle(0,1,2,3,4,5,6,7,k256[16],hf( 0));
  189. one_cycle(7,0,1,2,3,4,5,6,k256[17],hf( 1));
  190. one_cycle(6,7,0,1,2,3,4,5,k256[18],hf( 2));
  191. one_cycle(5,6,7,0,1,2,3,4,k256[19],hf( 3));
  192. one_cycle(4,5,6,7,0,1,2,3,k256[20],hf( 4));
  193. one_cycle(3,4,5,6,7,0,1,2,k256[21],hf( 5));
  194. one_cycle(2,3,4,5,6,7,0,1,k256[22],hf( 6));
  195. one_cycle(1,2,3,4,5,6,7,0,k256[23],hf( 7));
  196. one_cycle(0,1,2,3,4,5,6,7,k256[24],hf( 8));
  197. one_cycle(7,0,1,2,3,4,5,6,k256[25],hf( 9));
  198. one_cycle(6,7,0,1,2,3,4,5,k256[26],hf(10));
  199. one_cycle(5,6,7,0,1,2,3,4,k256[27],hf(11));
  200. one_cycle(4,5,6,7,0,1,2,3,k256[28],hf(12));
  201. one_cycle(3,4,5,6,7,0,1,2,k256[29],hf(13));
  202. one_cycle(2,3,4,5,6,7,0,1,k256[30],hf(14));
  203. one_cycle(1,2,3,4,5,6,7,0,k256[31],hf(15));
  204. one_cycle(0,1,2,3,4,5,6,7,k256[32],hf( 0));
  205. one_cycle(7,0,1,2,3,4,5,6,k256[33],hf( 1));
  206. one_cycle(6,7,0,1,2,3,4,5,k256[34],hf( 2));
  207. one_cycle(5,6,7,0,1,2,3,4,k256[35],hf( 3));
  208. one_cycle(4,5,6,7,0,1,2,3,k256[36],hf( 4));
  209. one_cycle(3,4,5,6,7,0,1,2,k256[37],hf( 5));
  210. one_cycle(2,3,4,5,6,7,0,1,k256[38],hf( 6));
  211. one_cycle(1,2,3,4,5,6,7,0,k256[39],hf( 7));
  212. one_cycle(0,1,2,3,4,5,6,7,k256[40],hf( 8));
  213. one_cycle(7,0,1,2,3,4,5,6,k256[41],hf( 9));
  214. one_cycle(6,7,0,1,2,3,4,5,k256[42],hf(10));
  215. one_cycle(5,6,7,0,1,2,3,4,k256[43],hf(11));
  216. one_cycle(4,5,6,7,0,1,2,3,k256[44],hf(12));
  217. one_cycle(3,4,5,6,7,0,1,2,k256[45],hf(13));
  218. one_cycle(2,3,4,5,6,7,0,1,k256[46],hf(14));
  219. one_cycle(1,2,3,4,5,6,7,0,k256[47],hf(15));
  220. one_cycle(0,1,2,3,4,5,6,7,k256[48],hf( 0));
  221. one_cycle(7,0,1,2,3,4,5,6,k256[49],hf( 1));
  222. one_cycle(6,7,0,1,2,3,4,5,k256[50],hf( 2));
  223. one_cycle(5,6,7,0,1,2,3,4,k256[51],hf( 3));
  224. one_cycle(4,5,6,7,0,1,2,3,k256[52],hf( 4));
  225. one_cycle(3,4,5,6,7,0,1,2,k256[53],hf( 5));
  226. one_cycle(2,3,4,5,6,7,0,1,k256[54],hf( 6));
  227. one_cycle(1,2,3,4,5,6,7,0,k256[55],hf( 7));
  228. one_cycle(0,1,2,3,4,5,6,7,k256[56],hf( 8));
  229. one_cycle(7,0,1,2,3,4,5,6,k256[57],hf( 9));
  230. one_cycle(6,7,0,1,2,3,4,5,k256[58],hf(10));
  231. one_cycle(5,6,7,0,1,2,3,4,k256[59],hf(11));
  232. one_cycle(4,5,6,7,0,1,2,3,k256[60],hf(12));
  233. one_cycle(3,4,5,6,7,0,1,2,k256[61],hf(13));
  234. one_cycle(2,3,4,5,6,7,0,1,k256[62],hf(14));
  235. one_cycle(1,2,3,4,5,6,7,0,k256[63],hf(15));
  236. ctx->hash[0] += v0; ctx->hash[1] += v1;
  237. ctx->hash[2] += v2; ctx->hash[3] += v3;
  238. ctx->hash[4] += v4; ctx->hash[5] += v5;
  239. ctx->hash[6] += v6; ctx->hash[7] += v7;
  240. #endif
  241. }
  242. /* SHA256 hash data in an array of bytes into hash buffer */
  243. /* and call the hash_compile function as required. */
  244. VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
  245. { uint_32t pos = (uint_32t)(ctx->count[0] & SHA256_MASK),
  246. space = SHA256_BLOCK_SIZE - pos;
  247. const unsigned char *sp = data;
  248. if((ctx->count[0] += len) < len)
  249. ++(ctx->count[1]);
  250. while(len >= space) /* tranfer whole blocks while possible */
  251. {
  252. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
  253. sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
  254. bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)
  255. sha256_compile(ctx);
  256. }
  257. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
  258. }
  259. /* SHA256 Final padding and digest calculation */
  260. static void sha_end1(unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen)
  261. { uint_32t i = (uint_32t)(ctx->count[0] & SHA256_MASK);
  262. /* put bytes in the buffer in an order in which references to */
  263. /* 32-bit words will put bytes with lower addresses into the */
  264. /* top of 32 bit words on BOTH big and little endian machines */
  265. bsw_32(ctx->wbuf, (i + 3) >> 2)
  266. /* we now need to mask valid bytes and add the padding which is */
  267. /* a single 1 bit and as many zero bits as necessary. Note that */
  268. /* we can always add the first padding byte here because the */
  269. /* buffer always has at least one empty slot */
  270. ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
  271. ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
  272. /* we need 9 or more empty positions, one for the padding byte */
  273. /* (above) and eight for the length count. If there is not */
  274. /* enough space pad and empty the buffer */
  275. if(i > SHA256_BLOCK_SIZE - 9)
  276. {
  277. if(i < 60) ctx->wbuf[15] = 0;
  278. sha256_compile(ctx);
  279. i = 0;
  280. }
  281. else /* compute a word index for the empty buffer positions */
  282. i = (i >> 2) + 1;
  283. while(i < 14) /* and zero pad all but last two positions */
  284. ctx->wbuf[i++] = 0;
  285. /* the following 32-bit length fields are assembled in the */
  286. /* wrong byte order on little endian machines but this is */
  287. /* corrected later since they are only ever used as 32-bit */
  288. /* word values. */
  289. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
  290. ctx->wbuf[15] = ctx->count[0] << 3;
  291. sha256_compile(ctx);
  292. /* extract the hash value as bytes in case the hash buffer is */
  293. /* mislaigned for 32-bit words */
  294. for(i = 0; i < hlen; ++i)
  295. hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
  296. }
  297. #endif
  298. #if defined(SHA_224)
  299. const uint_32t i224[8] =
  300. {
  301. 0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul,
  302. 0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul
  303. };
  304. VOID_RETURN sha224_begin(sha224_ctx ctx[1])
  305. {
  306. ctx->count[0] = ctx->count[1] = 0;
  307. memcpy(ctx->hash, i224, 8 * sizeof(uint_32t));
  308. }
  309. VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1])
  310. {
  311. sha_end1(hval, ctx, SHA224_DIGEST_SIZE);
  312. }
  313. VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len)
  314. { sha224_ctx cx[1];
  315. sha224_begin(cx);
  316. sha224_hash(data, len, cx);
  317. sha_end1(hval, cx, SHA224_DIGEST_SIZE);
  318. }
  319. #endif
  320. #if defined(SHA_256)
  321. const uint_32t i256[8] =
  322. {
  323. 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,
  324. 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul
  325. };
  326. VOID_RETURN sha256_begin(sha256_ctx ctx[1])
  327. {
  328. ctx->count[0] = ctx->count[1] = 0;
  329. memcpy(ctx->hash, i256, 8 * sizeof(uint_32t));
  330. }
  331. VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1])
  332. {
  333. sha_end1(hval, ctx, SHA256_DIGEST_SIZE);
  334. }
  335. VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
  336. { sha256_ctx cx[1];
  337. sha256_begin(cx);
  338. sha256_hash(data, len, cx);
  339. sha_end1(hval, cx, SHA256_DIGEST_SIZE);
  340. }
  341. #endif
  342. #if defined(SHA_384) || defined(SHA_512)
  343. #define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
  344. #define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
  345. #if !defined(bswap_64)
  346. #define bswap_64(x) (((uint_64t)(bswap_32((uint_32t)(x)))) << 32 | bswap_32((uint_32t)((x) >> 32)))
  347. #endif
  348. #if defined(SWAP_BYTES)
  349. #define bsw_64(p,n) \
  350. { int _i = (n); while(_i--) ((uint_64t*)p)[_i] = bswap_64(((uint_64t*)p)[_i]); }
  351. #else
  352. #define bsw_64(p,n)
  353. #endif
  354. /* SHA512 mixing function definitions */
  355. #ifdef s_0
  356. # undef s_0
  357. # undef s_1
  358. # undef g_0
  359. # undef g_1
  360. # undef k_0
  361. #endif
  362. #define s_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
  363. #define s_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
  364. #define g_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
  365. #define g_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
  366. #define k_0 k512
  367. /* SHA384/SHA512 mixing data */
  368. const uint_64t k512[80] =
  369. {
  370. li_64(428a2f98d728ae22), li_64(7137449123ef65cd),
  371. li_64(b5c0fbcfec4d3b2f), li_64(e9b5dba58189dbbc),
  372. li_64(3956c25bf348b538), li_64(59f111f1b605d019),
  373. li_64(923f82a4af194f9b), li_64(ab1c5ed5da6d8118),
  374. li_64(d807aa98a3030242), li_64(12835b0145706fbe),
  375. li_64(243185be4ee4b28c), li_64(550c7dc3d5ffb4e2),
  376. li_64(72be5d74f27b896f), li_64(80deb1fe3b1696b1),
  377. li_64(9bdc06a725c71235), li_64(c19bf174cf692694),
  378. li_64(e49b69c19ef14ad2), li_64(efbe4786384f25e3),
  379. li_64(0fc19dc68b8cd5b5), li_64(240ca1cc77ac9c65),
  380. li_64(2de92c6f592b0275), li_64(4a7484aa6ea6e483),
  381. li_64(5cb0a9dcbd41fbd4), li_64(76f988da831153b5),
  382. li_64(983e5152ee66dfab), li_64(a831c66d2db43210),
  383. li_64(b00327c898fb213f), li_64(bf597fc7beef0ee4),
  384. li_64(c6e00bf33da88fc2), li_64(d5a79147930aa725),
  385. li_64(06ca6351e003826f), li_64(142929670a0e6e70),
  386. li_64(27b70a8546d22ffc), li_64(2e1b21385c26c926),
  387. li_64(4d2c6dfc5ac42aed), li_64(53380d139d95b3df),
  388. li_64(650a73548baf63de), li_64(766a0abb3c77b2a8),
  389. li_64(81c2c92e47edaee6), li_64(92722c851482353b),
  390. li_64(a2bfe8a14cf10364), li_64(a81a664bbc423001),
  391. li_64(c24b8b70d0f89791), li_64(c76c51a30654be30),
  392. li_64(d192e819d6ef5218), li_64(d69906245565a910),
  393. li_64(f40e35855771202a), li_64(106aa07032bbd1b8),
  394. li_64(19a4c116b8d2d0c8), li_64(1e376c085141ab53),
  395. li_64(2748774cdf8eeb99), li_64(34b0bcb5e19b48a8),
  396. li_64(391c0cb3c5c95a63), li_64(4ed8aa4ae3418acb),
  397. li_64(5b9cca4f7763e373), li_64(682e6ff3d6b2b8a3),
  398. li_64(748f82ee5defb2fc), li_64(78a5636f43172f60),
  399. li_64(84c87814a1f0ab72), li_64(8cc702081a6439ec),
  400. li_64(90befffa23631e28), li_64(a4506cebde82bde9),
  401. li_64(bef9a3f7b2c67915), li_64(c67178f2e372532b),
  402. li_64(ca273eceea26619c), li_64(d186b8c721c0c207),
  403. li_64(eada7dd6cde0eb1e), li_64(f57d4f7fee6ed178),
  404. li_64(06f067aa72176fba), li_64(0a637dc5a2c898a6),
  405. li_64(113f9804bef90dae), li_64(1b710b35131c471b),
  406. li_64(28db77f523047d84), li_64(32caab7b40c72493),
  407. li_64(3c9ebe0a15c9bebc), li_64(431d67c49c100d4c),
  408. li_64(4cc5d4becb3e42b6), li_64(597f299cfc657e2a),
  409. li_64(5fcb6fab3ad6faec), li_64(6c44198c4a475817)
  410. };
  411. /* Compile 128 bytes of hash data into SHA384/512 digest */
  412. /* NOTE: this routine assumes that the byte order in the */
  413. /* ctx->wbuf[] at this point is such that low address bytes */
  414. /* in the ORIGINAL byte stream will go into the high end of */
  415. /* words on BOTH big and little endian systems */
  416. VOID_RETURN sha512_compile(sha512_ctx ctx[1])
  417. { uint_64t v[8], *p = ctx->wbuf;
  418. uint_32t j;
  419. memcpy(v, ctx->hash, 8 * sizeof(uint_64t));
  420. for(j = 0; j < 80; j += 16)
  421. {
  422. v_cycle( 0, j); v_cycle( 1, j);
  423. v_cycle( 2, j); v_cycle( 3, j);
  424. v_cycle( 4, j); v_cycle( 5, j);
  425. v_cycle( 6, j); v_cycle( 7, j);
  426. v_cycle( 8, j); v_cycle( 9, j);
  427. v_cycle(10, j); v_cycle(11, j);
  428. v_cycle(12, j); v_cycle(13, j);
  429. v_cycle(14, j); v_cycle(15, j);
  430. }
  431. ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
  432. ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  433. ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
  434. ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  435. }
  436. /* Compile 128 bytes of hash data into SHA256 digest value */
  437. /* NOTE: this routine assumes that the byte order in the */
  438. /* ctx->wbuf[] at this point is in such an order that low */
  439. /* address bytes in the ORIGINAL byte stream placed in this */
  440. /* buffer will now go to the high end of words on BOTH big */
  441. /* and little endian systems */
  442. VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
  443. { uint_32t pos = (uint_32t)(ctx->count[0] & SHA512_MASK),
  444. space = SHA512_BLOCK_SIZE - pos;
  445. const unsigned char *sp = data;
  446. if((ctx->count[0] += len) < len)
  447. ++(ctx->count[1]);
  448. while(len >= space) /* tranfer whole blocks while possible */
  449. {
  450. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
  451. sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;
  452. bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);
  453. sha512_compile(ctx);
  454. }
  455. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
  456. }
  457. /* SHA384/512 Final padding and digest calculation */
  458. static void sha_end2(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
  459. { uint_32t i = (uint_32t)(ctx->count[0] & SHA512_MASK);
  460. /* put bytes in the buffer in an order in which references to */
  461. /* 32-bit words will put bytes with lower addresses into the */
  462. /* top of 32 bit words on BOTH big and little endian machines */
  463. bsw_64(ctx->wbuf, (i + 7) >> 3);
  464. /* we now need to mask valid bytes and add the padding which is */
  465. /* a single 1 bit and as many zero bits as necessary. Note that */
  466. /* we can always add the first padding byte here because the */
  467. /* buffer always has at least one empty slot */
  468. ctx->wbuf[i >> 3] &= li_64(ffffffffffffff00) << 8 * (~i & 7);
  469. ctx->wbuf[i >> 3] |= li_64(0000000000000080) << 8 * (~i & 7);
  470. /* we need 17 or more empty byte positions, one for the padding */
  471. /* byte (above) and sixteen for the length count. If there is */
  472. /* not enough space pad and empty the buffer */
  473. if(i > SHA512_BLOCK_SIZE - 17)
  474. {
  475. if(i < 120) ctx->wbuf[15] = 0;
  476. sha512_compile(ctx);
  477. i = 0;
  478. }
  479. else
  480. i = (i >> 3) + 1;
  481. while(i < 14)
  482. ctx->wbuf[i++] = 0;
  483. /* the following 64-bit length fields are assembled in the */
  484. /* wrong byte order on little endian machines but this is */
  485. /* corrected later since they are only ever used as 64-bit */
  486. /* word values. */
  487. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);
  488. ctx->wbuf[15] = ctx->count[0] << 3;
  489. sha512_compile(ctx);
  490. /* extract the hash value as bytes in case the hash buffer is */
  491. /* misaligned for 32-bit words */
  492. for(i = 0; i < hlen; ++i)
  493. hval[i] = (unsigned char)(ctx->hash[i >> 3] >> (8 * (~i & 7)));
  494. }
  495. #endif
  496. #if defined(SHA_384)
  497. /* SHA384 initialisation data */
  498. const uint_64t i384[80] =
  499. {
  500. li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),
  501. li_64(9159015a3070dd17), li_64(152fecd8f70e5939),
  502. li_64(67332667ffc00b31), li_64(8eb44a8768581511),
  503. li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)
  504. };
  505. VOID_RETURN sha384_begin(sha384_ctx ctx[1])
  506. {
  507. ctx->count[0] = ctx->count[1] = 0;
  508. memcpy(ctx->hash, i384, 8 * sizeof(uint_64t));
  509. }
  510. VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1])
  511. {
  512. sha_end2(hval, ctx, SHA384_DIGEST_SIZE);
  513. }
  514. VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
  515. { sha384_ctx cx[1];
  516. sha384_begin(cx);
  517. sha384_hash(data, len, cx);
  518. sha_end2(hval, cx, SHA384_DIGEST_SIZE);
  519. }
  520. #endif
  521. #if defined(SHA_512)
  522. /* SHA512 initialisation data */
  523. const uint_64t i512[80] =
  524. {
  525. li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),
  526. li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),
  527. li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),
  528. li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)
  529. };
  530. VOID_RETURN sha512_begin(sha512_ctx ctx[1])
  531. {
  532. ctx->count[0] = ctx->count[1] = 0;
  533. memcpy(ctx->hash, i512, 8 * sizeof(uint_64t));
  534. }
  535. VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1])
  536. {
  537. sha_end2(hval, ctx, SHA512_DIGEST_SIZE);
  538. }
  539. VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
  540. { sha512_ctx cx[1];
  541. sha512_begin(cx);
  542. sha512_hash(data, len, cx);
  543. sha_end2(hval, cx, SHA512_DIGEST_SIZE);
  544. }
  545. #endif
  546. #if defined(SHA_2)
  547. #define CTX_224(x) ((x)->uu->ctx256)
  548. #define CTX_256(x) ((x)->uu->ctx256)
  549. #define CTX_384(x) ((x)->uu->ctx512)
  550. #define CTX_512(x) ((x)->uu->ctx512)
  551. /* SHA2 initialisation */
  552. INT_RETURN sha2_begin(unsigned long len, sha2_ctx ctx[1])
  553. {
  554. switch(len)
  555. {
  556. #if defined(SHA_224)
  557. case 224:
  558. case 28: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
  559. memcpy(CTX_256(ctx)->hash, i224, 32);
  560. ctx->sha2_len = 28; return EXIT_SUCCESS;
  561. #endif
  562. #if defined(SHA_256)
  563. case 256:
  564. case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
  565. memcpy(CTX_256(ctx)->hash, i256, 32);
  566. ctx->sha2_len = 32; return EXIT_SUCCESS;
  567. #endif
  568. #if defined(SHA_384)
  569. case 384:
  570. case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
  571. memcpy(CTX_384(ctx)->hash, i384, 64);
  572. ctx->sha2_len = 48; return EXIT_SUCCESS;
  573. #endif
  574. #if defined(SHA_512)
  575. case 512:
  576. case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
  577. memcpy(CTX_512(ctx)->hash, i512, 64);
  578. ctx->sha2_len = 64; return EXIT_SUCCESS;
  579. #endif
  580. default: return EXIT_FAILURE;
  581. }
  582. }
  583. VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
  584. {
  585. switch(ctx->sha2_len)
  586. {
  587. #if defined(SHA_224)
  588. case 28: sha224_hash(data, len, CTX_224(ctx)); return;
  589. #endif
  590. #if defined(SHA_256)
  591. case 32: sha256_hash(data, len, CTX_256(ctx)); return;
  592. #endif
  593. #if defined(SHA_384)
  594. case 48: sha384_hash(data, len, CTX_384(ctx)); return;
  595. #endif
  596. #if defined(SHA_512)
  597. case 64: sha512_hash(data, len, CTX_512(ctx)); return;
  598. #endif
  599. }
  600. }
  601. VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1])
  602. {
  603. switch(ctx->sha2_len)
  604. {
  605. #if defined(SHA_224)
  606. case 28: sha_end1(hval, CTX_224(ctx), SHA224_DIGEST_SIZE); return;
  607. #endif
  608. #if defined(SHA_256)
  609. case 32: sha_end1(hval, CTX_256(ctx), SHA256_DIGEST_SIZE); return;
  610. #endif
  611. #if defined(SHA_384)
  612. case 48: sha_end2(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
  613. #endif
  614. #if defined(SHA_512)
  615. case 64: sha_end2(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
  616. #endif
  617. }
  618. }
  619. INT_RETURN sha2(unsigned char hval[], unsigned long size,
  620. const unsigned char data[], unsigned long len)
  621. { sha2_ctx cx[1];
  622. if(sha2_begin(size, cx) == EXIT_SUCCESS)
  623. {
  624. sha2_hash(data, len, cx); sha2_end(hval, cx); return EXIT_SUCCESS;
  625. }
  626. else
  627. return EXIT_FAILURE;
  628. }
  629. #endif
  630. #if defined(__cplusplus)
  631. }
  632. #endif