sha2.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // Modified version of Dr Brian Gladmans implementation of the SHA-2
  2. // algorithms.
  3. //
  4. // Changes:
  5. // - Added #include <stdafx.h> to compile with MFC projects
  6. // - Added _WIN32_WCE define condition
  7. /*
  8. ---------------------------------------------------------------------------
  9. Copyright (c) 2002, Dr Brian Gladman <[email protected]>, Worcester, UK.
  10. All rights reserved.
  11. LICENSE TERMS
  12. The free distribution and use of this software in both source and binary
  13. form is allowed (with or without changes) provided that:
  14. 1. distributions of this source code include the above copyright
  15. notice, this list of conditions and the following disclaimer;
  16. 2. distributions in binary form include the above copyright
  17. notice, this list of conditions and the following disclaimer
  18. in the documentation and/or other associated materials;
  19. 3. the copyright holder's name is not used to endorse products
  20. built using this software without specific written permission.
  21. ALTERNATIVELY, provided that this notice is retained in full, this product
  22. may be distributed under the terms of the GNU General Public License (GPL),
  23. in which case the provisions of the GPL apply INSTEAD OF those given above.
  24. DISCLAIMER
  25. This software is provided 'as is' with no explicit or implied warranties
  26. in respect of its properties, including, but not limited to, correctness
  27. and/or fitness for purpose.
  28. ---------------------------------------------------------------------------
  29. Issue Date: 26/08/2003
  30. This is a byte oriented version of SHA2 that operates on arrays of bytes
  31. stored in memory. This code implements sha256, sha384 and sha512 but the
  32. latter two functions rely on efficient 64-bit integer operations that
  33. may not be very efficient on 32-bit machines
  34. The sha256 functions use a type 'sha256_ctx' to hold details of the
  35. current hash state and uses the following three calls:
  36. void sha256_begin(sha256_ctx ctx[1])
  37. void sha256_hash(const unsigned char data[],
  38. unsigned long len, sha256_ctx ctx[1])
  39. void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
  40. The first subroutine initialises a hash computation by setting up the
  41. context in the sha256_ctx context. The second subroutine hashes 8-bit
  42. bytes from array data[] into the hash state withinh sha256_ctx context,
  43. the number of bytes to be hashed being given by the the unsigned long
  44. integer len. The third subroutine completes the hash calculation and
  45. places the resulting digest value in the array of 8-bit bytes hval[].
  46. The sha384 and sha512 functions are similar and use the interfaces:
  47. void sha384_begin(sha384_ctx ctx[1]);
  48. void sha384_hash(const unsigned char data[],
  49. unsigned long len, sha384_ctx ctx[1]);
  50. void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
  51. void sha512_begin(sha512_ctx ctx[1]);
  52. void sha512_hash(const unsigned char data[],
  53. unsigned long len, sha512_ctx ctx[1]);
  54. void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
  55. In addition there is a function sha2 that can be used to call all these
  56. functions using a call with a hash length parameter as follows:
  57. int sha2_begin(unsigned long len, sha2_ctx ctx[1]);
  58. void sha2_hash(const unsigned char data[],
  59. unsigned long len, sha2_ctx ctx[1]);
  60. void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
  61. My thanks to Erik Andersen <[email protected]> for testing this code
  62. on big-endian systems and for his assistance with corrections
  63. */
  64. // Dominik Reichl: Added to compile with MFC
  65. #include "StdAfx.h"
  66. /* define the hash functions that you need */
  67. #define SHA_2 /* for dynamic hash length */
  68. #define SHA_256
  69. #define SHA_384
  70. #define SHA_512
  71. #include <string.h> /* for memcpy() etc. */
  72. #include <stdlib.h> /* for _lrotr with VC++ */
  73. #include "sha2.h"
  74. #if defined(__cplusplus)
  75. extern "C"
  76. {
  77. #endif
  78. /* PLATFORM SPECIFIC INCLUDES */
  79. #if defined( __FreeBSD__ ) || defined( __OpenBSD__ )
  80. # include <sys/endian.h>
  81. #elif defined( BSD ) && ( BSD >= 199103 )
  82. # include <machine/endian.h>
  83. #elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
  84. # include <endian.h>
  85. # include <byteswap.h>
  86. #elif defined( linux )
  87. # include <endian.h>
  88. #endif
  89. /* BYTE ORDER IN 32-BIT WORDS
  90. To obtain the highest speed on processors with 32-bit words, this code
  91. needs to determine the byte order of the target machine. The following
  92. block of code is an attempt to capture the most obvious ways in which
  93. various environemnts define byte order. It may well fail, in which case
  94. the definitions will need to be set by editing at the points marked
  95. **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for
  96. some of these defines (from cryptlib).
  97. */
  98. #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
  99. #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
  100. //#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
  101. // defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
  102. // defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
  103. // defined( vax ) || defined( vms ) || defined( VMS ) || \
  104. // defined( __VMS ) || defined( _WIN32_WCE )
  105. #define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  106. //#endif
  107. //
  108. //#if defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
  109. // defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
  110. // defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
  111. // defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
  112. // defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
  113. // defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )
  114. //
  115. //#define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  116. //
  117. //#endif
  118. /* if the platform is still not known, try to find its byte order */
  119. /* from commonly used definitions in the headers included earlier */
  120. #if !defined(PLATFORM_BYTE_ORDER)
  121. #if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
  122. # if defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
  123. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  124. # elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
  125. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  126. # elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
  127. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  128. # elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
  129. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  130. # endif
  131. #elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
  132. # if defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
  133. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  134. # elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
  135. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  136. # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
  137. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  138. # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
  139. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  140. # endif
  141. #elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)
  142. # if defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  143. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  144. # elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
  145. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  146. # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
  147. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  148. # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
  149. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  150. # endif
  151. #elif 0 /* **** EDIT HERE IF NECESSARY **** */
  152. #define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  153. #elif 0 /* **** EDIT HERE IF NECESSARY **** */
  154. #define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  155. #else
  156. #error Please edit sha2.c (line 180 or 183) to set the platform byte order
  157. #endif
  158. #endif
  159. #ifdef _MSC_VER
  160. #pragma intrinsic(memcpy)
  161. #endif
  162. #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
  163. #if !defined(bswap_32)
  164. #define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)
  165. #endif
  166. #if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
  167. #define SWAP_BYTES
  168. #else
  169. #undef SWAP_BYTES
  170. #endif
  171. #if defined(SHA_2) || defined(SHA_256)
  172. #define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
  173. #if defined(SWAP_BYTES)
  174. #define bsw_32(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32(p[_i]); }
  175. #else
  176. #define bsw_32(p,n)
  177. #endif
  178. /* SHA256 mixing function definitions */
  179. #if 0
  180. #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  181. #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  182. #else /* Thanks to Rich Schroeppel and Colin Plumb for the following */
  183. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  184. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
  185. #endif
  186. #define s256_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
  187. #define s256_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
  188. #define g256_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
  189. #define g256_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
  190. /* rotated SHA256 round definition. Rather than swapping variables as in */
  191. /* FIPS-180, different variables are 'rotated' on each round, returning */
  192. /* to their starting positions every eight rounds */
  193. #define h2(index) p[index & 15] += \
  194. g256_1(p[(index + 14) & 15]) + p[(index + 9) & 15] + g256_0(p[(index + 1) & 15])
  195. #define h2_cycle(index,j) \
  196. v[(7 - index) & 7] += (j ? h2(index) : p[index & 15]) + k256[index + j] \
  197. + s256_1(v[(4 - index) & 7]) + ch(v[(4 - index) & 7], v[(5 - index) & 7], v[(6 - index) & 7]); \
  198. v[(3 - index) & 7] += v[(7 - index) & 7]; \
  199. v[(7 - index) & 7] += s256_0(v[(0 - index) & 7]) + maj(v[(0 - index) & 7], v[(1 - index) & 7], v[(2 - index) & 7])
  200. /* SHA256 mixing data */
  201. const sha2_32t k256[64] =
  202. { n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5),
  203. n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5),
  204. n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3),
  205. n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174),
  206. n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc),
  207. n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da),
  208. n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7),
  209. n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967),
  210. n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13),
  211. n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),
  212. n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3),
  213. n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070),
  214. n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5),
  215. n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3),
  216. n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208),
  217. n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),
  218. };
  219. /* SHA256 initialisation data */
  220. const sha2_32t i256[8] =
  221. {
  222. n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),
  223. n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)
  224. };
  225. sha2_void sha256_begin(sha256_ctx ctx[1])
  226. {
  227. ctx->count[0] = ctx->count[1] = 0;
  228. memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));
  229. }
  230. /* Compile 64 bytes of hash data into SHA256 digest value */
  231. /* NOTE: this routine assumes that the byte order in the */
  232. /* ctx->wbuf[] at this point is in such an order that low */
  233. /* address bytes in the ORIGINAL byte stream placed in this */
  234. /* buffer will now go to the high end of words on BOTH big */
  235. /* and little endian systems */
  236. sha2_void sha256_compile(sha256_ctx ctx[1])
  237. { sha2_32t v[8], j, *p = ctx->wbuf;
  238. memcpy(v, ctx->hash, 8 * sizeof(sha2_32t));
  239. for(j = 0; j < 64; j += 16)
  240. {
  241. h2_cycle( 0, j); h2_cycle( 1, j); h2_cycle( 2, j); h2_cycle( 3, j);
  242. h2_cycle( 4, j); h2_cycle( 5, j); h2_cycle( 6, j); h2_cycle( 7, j);
  243. h2_cycle( 8, j); h2_cycle( 9, j); h2_cycle(10, j); h2_cycle(11, j);
  244. h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);
  245. }
  246. ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  247. ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  248. }
  249. /* SHA256 hash data in an array of bytes into hash buffer */
  250. /* and call the hash_compile function as required. */
  251. sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
  252. { sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK),
  253. space = SHA256_BLOCK_SIZE - pos;
  254. const unsigned char *sp = data;
  255. if((ctx->count[0] += len) < len)
  256. ++(ctx->count[1]);
  257. while(len >= space) /* tranfer whole blocks while possible */
  258. {
  259. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
  260. sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
  261. bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)
  262. sha256_compile(ctx);
  263. }
  264. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
  265. }
  266. /* SHA256 Final padding and digest calculation */
  267. static sha2_32t m1[4] =
  268. {
  269. n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)
  270. };
  271. static sha2_32t b1[4] =
  272. {
  273. n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)
  274. };
  275. sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
  276. { sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK);
  277. bsw_32(ctx->wbuf, (i + 3) >> 2)
  278. /* bytes in the buffer are now in an order in which references */
  279. /* to 32-bit words will put bytes with lower addresses into the */
  280. /* top of 32 bit words on BOTH big and little endian machines */
  281. /* we now need to mask valid bytes and add the padding which is */
  282. /* a single 1 bit and as many zero bits as necessary. */
  283. ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];
  284. /* we need 9 or more empty positions, one for the padding byte */
  285. /* (above) and eight for the length count. If there is not */
  286. /* enough space pad and empty the buffer */
  287. if(i > SHA256_BLOCK_SIZE - 9)
  288. {
  289. if(i < 60) ctx->wbuf[15] = 0;
  290. sha256_compile(ctx);
  291. i = 0;
  292. }
  293. else /* compute a word index for the empty buffer positions */
  294. i = (i >> 2) + 1;
  295. while(i < 14) /* and zero pad all but last two positions */
  296. ctx->wbuf[i++] = 0;
  297. /* the following 32-bit length fields are assembled in the */
  298. /* wrong byte order on little endian machines but this is */
  299. /* corrected later since they are only ever used as 32-bit */
  300. /* word values. */
  301. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
  302. ctx->wbuf[15] = ctx->count[0] << 3;
  303. sha256_compile(ctx);
  304. /* extract the hash value as bytes in case the hash buffer is */
  305. /* mislaigned for 32-bit words */
  306. for(i = 0; i < SHA256_DIGEST_SIZE; ++i)
  307. hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
  308. }
  309. sha2_void sha256Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
  310. { sha256_ctx cx[1];
  311. sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);
  312. }
  313. #endif
  314. #if defined(SHA_2) || defined(SHA_384) || defined(SHA_512)
  315. #define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
  316. #define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
  317. #if !defined(bswap_64)
  318. #define bswap_64(x) (((sha2_64t)(bswap_32((sha2_32t)(x)))) << 32 | bswap_32((sha2_32t)((x) >> 32)))
  319. #endif
  320. #if defined(SWAP_BYTES)
  321. #define bsw_64(p,n) { int _i = (n); while(_i--) p[_i] = bswap_64(p[_i]); }
  322. #else
  323. #define bsw_64(p,n)
  324. #endif
  325. /* SHA512 mixing function definitions */
  326. #define s512_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
  327. #define s512_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
  328. #define g512_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
  329. #define g512_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
  330. /* rotated SHA512 round definition. Rather than swapping variables as in */
  331. /* FIPS-180, different variables are 'rotated' on each round, returning */
  332. /* to their starting positions every eight rounds */
  333. #define h5(index) ctx->wbuf[index & 15] += \
  334. g512_1(ctx->wbuf[(index + 14) & 15]) + ctx->wbuf[(index + 9) & 15] + g512_0(ctx->wbuf[(index + 1) & 15])
  335. #define h5_cycle(index,j) \
  336. v[(7 - index) & 7] += (j ? h5(index) : ctx->wbuf[index & 15]) + k512[index + j] \
  337. + s512_1(v[(4 - index) & 7]) + ch(v[(4 - index) & 7], v[(5 - index) & 7], v[(6 - index) & 7]); \
  338. v[(3 - index) & 7] += v[(7 - index) & 7]; \
  339. v[(7 - index) & 7] += s512_0(v[(0 - index) & 7]) + maj(v[(0 - index) & 7], v[(1 - index) & 7], v[(2 - index) & 7])
  340. /* SHA384/SHA512 mixing data */
  341. const sha2_64t k512[80] =
  342. {
  343. n_u64(428a2f98d728ae22), n_u64(7137449123ef65cd),
  344. n_u64(b5c0fbcfec4d3b2f), n_u64(e9b5dba58189dbbc),
  345. n_u64(3956c25bf348b538), n_u64(59f111f1b605d019),
  346. n_u64(923f82a4af194f9b), n_u64(ab1c5ed5da6d8118),
  347. n_u64(d807aa98a3030242), n_u64(12835b0145706fbe),
  348. n_u64(243185be4ee4b28c), n_u64(550c7dc3d5ffb4e2),
  349. n_u64(72be5d74f27b896f), n_u64(80deb1fe3b1696b1),
  350. n_u64(9bdc06a725c71235), n_u64(c19bf174cf692694),
  351. n_u64(e49b69c19ef14ad2), n_u64(efbe4786384f25e3),
  352. n_u64(0fc19dc68b8cd5b5), n_u64(240ca1cc77ac9c65),
  353. n_u64(2de92c6f592b0275), n_u64(4a7484aa6ea6e483),
  354. n_u64(5cb0a9dcbd41fbd4), n_u64(76f988da831153b5),
  355. n_u64(983e5152ee66dfab), n_u64(a831c66d2db43210),
  356. n_u64(b00327c898fb213f), n_u64(bf597fc7beef0ee4),
  357. n_u64(c6e00bf33da88fc2), n_u64(d5a79147930aa725),
  358. n_u64(06ca6351e003826f), n_u64(142929670a0e6e70),
  359. n_u64(27b70a8546d22ffc), n_u64(2e1b21385c26c926),
  360. n_u64(4d2c6dfc5ac42aed), n_u64(53380d139d95b3df),
  361. n_u64(650a73548baf63de), n_u64(766a0abb3c77b2a8),
  362. n_u64(81c2c92e47edaee6), n_u64(92722c851482353b),
  363. n_u64(a2bfe8a14cf10364), n_u64(a81a664bbc423001),
  364. n_u64(c24b8b70d0f89791), n_u64(c76c51a30654be30),
  365. n_u64(d192e819d6ef5218), n_u64(d69906245565a910),
  366. n_u64(f40e35855771202a), n_u64(106aa07032bbd1b8),
  367. n_u64(19a4c116b8d2d0c8), n_u64(1e376c085141ab53),
  368. n_u64(2748774cdf8eeb99), n_u64(34b0bcb5e19b48a8),
  369. n_u64(391c0cb3c5c95a63), n_u64(4ed8aa4ae3418acb),
  370. n_u64(5b9cca4f7763e373), n_u64(682e6ff3d6b2b8a3),
  371. n_u64(748f82ee5defb2fc), n_u64(78a5636f43172f60),
  372. n_u64(84c87814a1f0ab72), n_u64(8cc702081a6439ec),
  373. n_u64(90befffa23631e28), n_u64(a4506cebde82bde9),
  374. n_u64(bef9a3f7b2c67915), n_u64(c67178f2e372532b),
  375. n_u64(ca273eceea26619c), n_u64(d186b8c721c0c207),
  376. n_u64(eada7dd6cde0eb1e), n_u64(f57d4f7fee6ed178),
  377. n_u64(06f067aa72176fba), n_u64(0a637dc5a2c898a6),
  378. n_u64(113f9804bef90dae), n_u64(1b710b35131c471b),
  379. n_u64(28db77f523047d84), n_u64(32caab7b40c72493),
  380. n_u64(3c9ebe0a15c9bebc), n_u64(431d67c49c100d4c),
  381. n_u64(4cc5d4becb3e42b6), n_u64(597f299cfc657e2a),
  382. n_u64(5fcb6fab3ad6faec), n_u64(6c44198c4a475817)
  383. };
  384. /* Compile 64 bytes of hash data into SHA384/SHA512 digest value */
  385. sha2_void sha512_compile(sha512_ctx ctx[1])
  386. { sha2_64t v[8];
  387. sha2_32t j;
  388. memcpy(v, ctx->hash, 8 * sizeof(sha2_64t));
  389. for(j = 0; j < 80; j += 16)
  390. {
  391. h5_cycle( 0, j); h5_cycle( 1, j); h5_cycle( 2, j); h5_cycle( 3, j);
  392. h5_cycle( 4, j); h5_cycle( 5, j); h5_cycle( 6, j); h5_cycle( 7, j);
  393. h5_cycle( 8, j); h5_cycle( 9, j); h5_cycle(10, j); h5_cycle(11, j);
  394. h5_cycle(12, j); h5_cycle(13, j); h5_cycle(14, j); h5_cycle(15, j);
  395. }
  396. ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  397. ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  398. }
  399. /* Compile 128 bytes of hash data into SHA256 digest value */
  400. /* NOTE: this routine assumes that the byte order in the */
  401. /* ctx->wbuf[] at this point is in such an order that low */
  402. /* address bytes in the ORIGINAL byte stream placed in this */
  403. /* buffer will now go to the high end of words on BOTH big */
  404. /* and little endian systems */
  405. sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
  406. { sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA512_MASK),
  407. space = SHA512_BLOCK_SIZE - pos;
  408. const unsigned char *sp = data;
  409. if((ctx->count[0] += len) < len)
  410. ++(ctx->count[1]);
  411. while(len >= space) /* tranfer whole blocks while possible */
  412. {
  413. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
  414. sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;
  415. bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);
  416. sha512_compile(ctx);
  417. }
  418. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
  419. }
  420. /* SHA384/512 Final padding and digest calculation */
  421. static sha2_64t m2[8] =
  422. {
  423. n_u64(0000000000000000), n_u64(ff00000000000000),
  424. n_u64(ffff000000000000), n_u64(ffffff0000000000),
  425. n_u64(ffffffff00000000), n_u64(ffffffffff000000),
  426. n_u64(ffffffffffff0000), n_u64(ffffffffffffff00)
  427. };
  428. static sha2_64t b2[8] =
  429. {
  430. n_u64(8000000000000000), n_u64(0080000000000000),
  431. n_u64(0000800000000000), n_u64(0000008000000000),
  432. n_u64(0000000080000000), n_u64(0000000000800000),
  433. n_u64(0000000000008000), n_u64(0000000000000080)
  434. };
  435. static void sha_end(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
  436. { sha2_32t i = (sha2_32t)(ctx->count[0] & SHA512_MASK);
  437. bsw_64(ctx->wbuf, (i + 7) >> 3);
  438. /* bytes in the buffer are now in an order in which references */
  439. /* to 64-bit words will put bytes with lower addresses into the */
  440. /* top of 64 bit words on BOTH big and little endian machines */
  441. /* we now need to mask valid bytes and add the padding which is */
  442. /* a single 1 bit and as many zero bits as necessary. */
  443. ctx->wbuf[i >> 3] = (ctx->wbuf[i >> 3] & m2[i & 7]) | b2[i & 7];
  444. /* we need 17 or more empty byte positions, one for the padding */
  445. /* byte (above) and sixteen for the length count. If there is */
  446. /* not enough space pad and empty the buffer */
  447. if(i > SHA512_BLOCK_SIZE - 17)
  448. {
  449. if(i < 120) ctx->wbuf[15] = 0;
  450. sha512_compile(ctx);
  451. i = 0;
  452. }
  453. else
  454. i = (i >> 3) + 1;
  455. while(i < 14)
  456. ctx->wbuf[i++] = 0;
  457. /* the following 64-bit length fields are assembled in the */
  458. /* wrong byte order on little endian machines but this is */
  459. /* corrected later since they are only ever used as 64-bit */
  460. /* word values. */
  461. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);
  462. ctx->wbuf[15] = ctx->count[0] << 3;
  463. sha512_compile(ctx);
  464. /* extract the hash value as bytes in case the hash buffer is */
  465. /* misaligned for 32-bit words */
  466. for(i = 0; i < hlen; ++i)
  467. hval[i] = (unsigned char)(ctx->hash[i >> 3] >> (8 * (~i & 7)));
  468. }
  469. #endif
  470. #if defined(SHA_2) || defined(SHA_384)
  471. /* SHA384 initialisation data */
  472. const sha2_64t i384[80] =
  473. {
  474. n_u64(cbbb9d5dc1059ed8), n_u64(629a292a367cd507),
  475. n_u64(9159015a3070dd17), n_u64(152fecd8f70e5939),
  476. n_u64(67332667ffc00b31), n_u64(8eb44a8768581511),
  477. n_u64(db0c2e0d64f98fa7), n_u64(47b5481dbefa4fa4)
  478. };
  479. sha2_void sha384_begin(sha384_ctx ctx[1])
  480. {
  481. ctx->count[0] = ctx->count[1] = 0;
  482. memcpy(ctx->hash, i384, 8 * sizeof(sha2_64t));
  483. }
  484. sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1])
  485. {
  486. sha_end(hval, ctx, SHA384_DIGEST_SIZE);
  487. }
  488. sha2_void sha384Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
  489. { sha384_ctx cx[1];
  490. sha384_begin(cx); sha384_hash(data, len, cx); sha384_end(hval, cx);
  491. }
  492. #endif
  493. #if defined(SHA_2) || defined(SHA_512)
  494. /* SHA512 initialisation data */
  495. const sha2_64t i512[80] =
  496. {
  497. n_u64(6a09e667f3bcc908), n_u64(bb67ae8584caa73b),
  498. n_u64(3c6ef372fe94f82b), n_u64(a54ff53a5f1d36f1),
  499. n_u64(510e527fade682d1), n_u64(9b05688c2b3e6c1f),
  500. n_u64(1f83d9abfb41bd6b), n_u64(5be0cd19137e2179)
  501. };
  502. sha2_void sha512_begin(sha512_ctx ctx[1])
  503. {
  504. ctx->count[0] = ctx->count[1] = 0;
  505. memcpy(ctx->hash, i512, 8 * sizeof(sha2_64t));
  506. }
  507. sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1])
  508. {
  509. sha_end(hval, ctx, SHA512_DIGEST_SIZE);
  510. }
  511. sha2_void sha512Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
  512. { sha512_ctx cx[1];
  513. sha512_begin(cx); sha512_hash(data, len, cx); sha512_end(hval, cx);
  514. }
  515. #endif
  516. #if defined(SHA_2)
  517. #define CTX_256(x) ((x)->uu->ctx256)
  518. #define CTX_384(x) ((x)->uu->ctx512)
  519. #define CTX_512(x) ((x)->uu->ctx512)
  520. /* SHA2 initialisation */
  521. sha2_int sha2_begin(unsigned long len, sha2_ctx ctx[1])
  522. { unsigned long l = len;
  523. switch(len)
  524. {
  525. case 256: l = len >> 3;
  526. case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
  527. memcpy(CTX_256(ctx)->hash, i256, 32); break;
  528. case 384: l = len >> 3;
  529. case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
  530. memcpy(CTX_384(ctx)->hash, i384, 64); break;
  531. case 512: l = len >> 3;
  532. case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
  533. memcpy(CTX_512(ctx)->hash, i512, 64); break;
  534. default: return SHA2_BAD;
  535. }
  536. ctx->sha2_len = l; return SHA2_GOOD;
  537. }
  538. sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
  539. {
  540. switch(ctx->sha2_len)
  541. {
  542. case 32: sha256_hash(data, len, CTX_256(ctx)); return;
  543. case 48: sha384_hash(data, len, CTX_384(ctx)); return;
  544. case 64: sha512_hash(data, len, CTX_512(ctx)); return;
  545. }
  546. }
  547. sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1])
  548. {
  549. switch(ctx->sha2_len)
  550. {
  551. case 32: sha256_end(hval, CTX_256(ctx)); return;
  552. case 48: sha_end(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
  553. case 64: sha_end(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
  554. }
  555. }
  556. sha2_int sha2Lib(unsigned char hval[], unsigned long size,
  557. const unsigned char data[], unsigned long len)
  558. { sha2_ctx cx[1];
  559. if(sha2_begin(size, cx) == SHA2_GOOD)
  560. {
  561. sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;
  562. }
  563. else
  564. return SHA2_BAD;
  565. }
  566. #endif
  567. #if defined(__cplusplus)
  568. }
  569. #endif