关键词搜索

源码搜索 ×
×

tars源码分析之21

发布2022-07-17浏览738次

详情内容

sha到底是怎么实现的,来看下:

  1. #include "util/tc_sha.h"
  2. #include "util/tc_common.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <endian.h>
  6. #include <limits.h>
  7. namespace tars
  8. {
  9. /* Defines for suffixes to 32 and 64 bit unsigned numeric values */
  10. #define sfx_lo(x,y) x##y
  11. #define sfx_hi(x,y) sfx_lo(x,y)
  12. #define n_u32(p) sfx_hi(0x##p, s_u32)
  13. #define n_u64(p) sfx_hi(0x##p, s_u64)
  14. #if UINT_MAX == 0xffffffff
  15. #define s_u32 u
  16. #elif ULONG_MAX == 0xffffffff
  17. #define s_u32 ul
  18. #else
  19. #error Please define sha2_32t as an unsigned 32 bit type in sha2.h
  20. #endif
  21. #if ULONG_MAX == 0xffffffffffffffff
  22. #define s_u64 ul
  23. #elif ULONG_MAX == 0xffffffff
  24. #define s_u64 ull
  25. #else
  26. #error Please define sha2_64t as an unsigned 64 bit type in sha2.h
  27. #endif
  28. #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  29. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  30. #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  31. namespace detail_sha1
  32. {
  33. #define SHA1_BLOCK_SIZE 64
  34. #define SHA1_DIGEST_SIZE 20
  35. typedef struct
  36. {
  37. uint32_t count[2];
  38. uint32_t hash[5];
  39. uint32_t wbuf[16];
  40. } sha1_ctx;
  41. #define rotr32_sha1(x,n) (((x) << n) | ((x) >> (32 - n)))
  42. #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  43. #if(__BYTE_ORDER == __BIG_ENDIAN)
  44. #define swap_b32_sha1(x) (x)
  45. #elif defined(bswap_32_sha1)
  46. #define swap_b32_sha1(x) bswap_32_sha1(x)
  47. #else
  48. #define swap_b32_sha1(x) ((rotr32_sha1((x), 8) & 0x00ff00ff) | (rotr32_sha1((x), 24) & 0xff00ff00))
  49. #endif
  50. /* A normal version as set out in the FIPS. This version uses */
  51. /* partial loop unrolling and is optimised for the Pentium 4 */
  52. #define rnd(f,k) \
  53. t = a; a = rotr32_sha1(a,5) + f(b,c,d) + e + k + w[i]; \
  54. e = d; d = c; c = rotr32_sha1(b, 30); b = t
  55. void sha1_compile(sha1_ctx ctx[1])
  56. { uint32_t w[80], i, a, b, c, d, e, t;
  57. /* note that words are compiled from the buffer into 32-bit */
  58. /* words in big-endian order so an order reversal is needed */
  59. /* here on little endian machines */
  60. for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i) w[i] = swap_b32_sha1(ctx->wbuf[i]);
  61. for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i) w[i] = rotr32_sha1(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  62. a = ctx->hash[0];
  63. b = ctx->hash[1];
  64. c = ctx->hash[2];
  65. d = ctx->hash[3];
  66. e = ctx->hash[4];
  67. for (i = 0; i < 20; ++i)
  68. {
  69. rnd(ch, 0x5a827999);
  70. }
  71. for (i = 20; i < 40; ++i)
  72. {
  73. rnd(parity, 0x6ed9eba1);
  74. }
  75. for (i = 40; i < 60; ++i)
  76. {
  77. rnd(maj, 0x8f1bbcdc);
  78. }
  79. for (i = 60; i < 80; ++i)
  80. {
  81. rnd(parity, 0xca62c1d6);
  82. }
  83. ctx->hash[0] += a;
  84. ctx->hash[1] += b;
  85. ctx->hash[2] += c;
  86. ctx->hash[3] += d;
  87. ctx->hash[4] += e;
  88. }
  89. void sha1_begin(sha1_ctx ctx[1])
  90. {
  91. ctx->count[0] = ctx->count[1] = 0;
  92. ctx->hash[0] = 0x67452301;
  93. ctx->hash[1] = 0xefcdab89;
  94. ctx->hash[2] = 0x98badcfe;
  95. ctx->hash[3] = 0x10325476;
  96. ctx->hash[4] = 0xc3d2e1f0;
  97. }
  98. /* SHA1 hash data in an array of bytes into hash buffer and call the */
  99. /* hash_compile function as required. */
  100. void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx ctx[1])
  101. {
  102. uint32_t pos = (uint32_t)(ctx->count[0] & SHA1_MASK),
  103. space = SHA1_BLOCK_SIZE - pos;
  104. const unsigned char *sp = data;
  105. if ((ctx->count[0] += len) < len) ++(ctx->count[1]);
  106. while (len >= space) /* tranfer whole blocks while possible */
  107. {
  108. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, space);
  109. sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
  110. sha1_compile(ctx);
  111. }
  112. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, len);
  113. }
  114. /* SHA1 final padding and digest calculation */
  115. #if(__BYTE_ORDER == __LITTLE_ENDIAN)
  116. static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
  117. static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
  118. #else
  119. static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
  120. static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
  121. #endif
  122. void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
  123. {
  124. uint32_t i = (uint32_t)(ctx->count[0] & SHA1_MASK);
  125. /* mask out the rest of any partial 32-bit word and then set */
  126. /* the next byte to 0x80. On big-endian machines any bytes in */
  127. /* the buffer will be at the top end of 32 bit words, on little */
  128. /* endian machines they will be at the bottom. Hence the AND */
  129. /* and OR masks above are reversed for little endian systems */
  130. /* Note that we can always add the first padding byte at this */
  131. /* because the buffer always contains at least one empty slot */
  132. ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & mask[i & 3]) | bits[i & 3];
  133. /* we need 9 or more empty positions, one for the padding byte */
  134. /* (above) and eight for the length count. If there is not */
  135. /* enough space pad and empty the buffer */
  136. if (i > SHA1_BLOCK_SIZE - 9)
  137. {
  138. if (i < 60) ctx->wbuf[15] = 0;
  139. sha1_compile(ctx);
  140. i = 0;
  141. }
  142. else /* compute a word index for the empty buffer positions */
  143. i = (i >> 2) + 1;
  144. while (i < 14) /* and zero pad all but last two positions */
  145. ctx->wbuf[i++] = 0;
  146. /* assemble the eight byte counter in in big-endian format */
  147. ctx->wbuf[14] = swap_b32_sha1((ctx->count[1] << 3) | (ctx->count[0] >> 29));
  148. ctx->wbuf[15] = swap_b32_sha1(ctx->count[0] << 3);
  149. sha1_compile(ctx);
  150. /* extract the hash value as bytes in case the hash buffer is */
  151. /* misaligned for 32-bit words */
  152. for (i = 0; i < SHA1_DIGEST_SIZE; ++i) hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));
  153. }
  154. }
  155. //
  156. namespace detail_sha2
  157. {
  158. #define SHA256_DIGEST_SIZE 32
  159. #define SHA384_DIGEST_SIZE 48
  160. #define SHA512_DIGEST_SIZE 64
  161. #define SHA256_BLOCK_SIZE 64
  162. #define SHA384_BLOCK_SIZE 128
  163. #define SHA512_BLOCK_SIZE 128
  164. /* type to hold the SHA256 context */
  165. typedef struct
  166. {uint32_t count[2];
  167. uint32_t hash[8];
  168. uint32_t wbuf[16];
  169. } sha256_ctx;
  170. /* type to hold the SHA384/512 context */
  171. typedef struct
  172. {uint64_t count[2];
  173. uint64_t hash[8];
  174. uint64_t wbuf[16];
  175. } sha512_ctx;
  176. typedef sha512_ctx sha384_ctx;
  177. #define rotr32_sha2(x,n) (((x) >> n) | ((x) << (32 - n)))
  178. #define rotr64_sha2(x,n) (((x) >> n) | ((x) << (64 - n)))
  179. #define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
  180. #define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
  181. #if !defined(bswap_32_sha2)
  182. #define bswap_32_sha2(x) ((rotr32_sha2((x), 24) & 0x00ff00ff) | (rotr32_sha2((x), 8) & 0xff00ff00))
  183. #endif
  184. #if(__BYTE_ORDER == __LITTLE_ENDIAN)
  185. #define bsw_32_sha2(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32_sha2(p[_i]); }
  186. #else
  187. #define bsw_32_sha2(p,n)
  188. #endif
  189. #if(__BYTE_ORDER == __LITTLE_ENDIAN)
  190. #if !defined(bswap_64_sha2)
  191. #define bswap_64_sha2(x) (((uint64_t)(bswap_32_sha2((uint32_t)(x)))) << 32 | bswap_32_sha2((uint32_t)((x) >> 32)))
  192. #endif
  193. #define bsw_64_sha2(p,n) { int _i = (n); while(_i--) p[_i] = bswap_64_sha2(p[_i]); }
  194. #else
  195. #define bsw_64_sha2(p,n)
  196. #endif
  197. /* SHA256 mixing function definitions */
  198. #define s256_0(x) (rotr32_sha2((x), 2) ^ rotr32_sha2((x), 13) ^ rotr32_sha2((x), 22))
  199. #define s256_1(x) (rotr32_sha2((x), 6) ^ rotr32_sha2((x), 11) ^ rotr32_sha2((x), 25))
  200. #define g256_0(x) (rotr32_sha2((x), 7) ^ rotr32_sha2((x), 18) ^ ((x) >> 3))
  201. #define g256_1(x) (rotr32_sha2((x), 17) ^ rotr32_sha2((x), 19) ^ ((x) >> 10))
  202. /* rotated SHA256 round definition. Rather than swapping variables as in */
  203. /* FIPS-180, different variables are 'rotated' on each round, returning */
  204. /* to their starting positions every eight rounds */
  205. #define h2(i) ctx->wbuf[i & 15] += \
  206. g256_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g256_0(ctx->wbuf[(i + 1) & 15])
  207. #define h2_cycle(i,j) \
  208. v[(7 - i) & 7] += (j ? h2(i) : ctx->wbuf[i & 15]) + k256[i + j] \
  209. + s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \
  210. v[(3 - i) & 7] += v[(7 - i) & 7]; \
  211. v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])
  212. /* SHA256 mixing data */
  213. const uint32_t k256[64] =
  214. { n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5),
  215. n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5),
  216. n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3),
  217. n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174),
  218. n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc),
  219. n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da),
  220. n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7),
  221. n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967),
  222. n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13),
  223. n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),
  224. n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3),
  225. n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070),
  226. n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5),
  227. n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3),
  228. n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208),
  229. n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),
  230. };
  231. /* SHA256 initialisation data */
  232. const uint32_t i256[8] =
  233. {
  234. n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),
  235. n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)
  236. };
  237. void sha256_begin(sha256_ctx ctx[1])
  238. {
  239. ctx->count[0] = ctx->count[1] = 0;
  240. memcpy(ctx->hash, i256, 8 * sizeof(uint32_t));
  241. }
  242. /* Compile 64 bytes of hash data into SHA256 digest value */
  243. /* NOTE: this routine assumes that the byte order in the */
  244. /* ctx->wbuf[] at this point is in such an order that low */
  245. /* address bytes in the ORIGINAL byte stream placed in this */
  246. /* buffer will now go to the high end of words on BOTH big */
  247. /* and little endian systems */
  248. void sha256_compile(sha256_ctx ctx[1])
  249. { uint32_t v[8], j;
  250. memcpy(v, ctx->hash, 8 * sizeof(uint32_t));
  251. for (j = 0; j < 64; j += 16)
  252. {
  253. h2_cycle(0, j); h2_cycle(1, j); h2_cycle(2, j); h2_cycle(3, j);
  254. h2_cycle(4, j); h2_cycle(5, j); h2_cycle(6, j); h2_cycle(7, j);
  255. h2_cycle(8, j); h2_cycle(9, j); h2_cycle(10, j); h2_cycle(11, j);
  256. h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);
  257. }
  258. ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  259. ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  260. }
  261. /* SHA256 hash data in an array of bytes into hash buffer */
  262. /* and call the hash_compile function as required. */
  263. void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
  264. { uint32_t pos = (uint32_t)(ctx->count[0] & SHA256_MASK),
  265. space = SHA256_BLOCK_SIZE - pos;
  266. const unsigned char *sp = data;
  267. if ((ctx->count[0] += len) < len) ++(ctx->count[1]);
  268. while (len >= space) /* tranfer whole blocks while possible */
  269. {
  270. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, space);
  271. sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
  272. bsw_32_sha2(ctx->wbuf, SHA256_BLOCK_SIZE >> 2);
  273. sha256_compile(ctx);
  274. }
  275. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, len);
  276. }
  277. /* SHA256 Final padding and digest calculation */
  278. static uint32_t m1[4] =
  279. {
  280. n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)
  281. };
  282. static uint32_t b1[4] =
  283. {
  284. n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)
  285. };
  286. void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
  287. {
  288. uint32_t i = (uint32_t)(ctx->count[0] & SHA256_MASK);
  289. bsw_32_sha2(ctx->wbuf, (i + 3) >> 2);
  290. /* bytes in the buffer are now in an order in which references */
  291. /* to 32-bit words will put bytes with lower addresses into the */
  292. /* top of 32 bit words on BOTH big and little endian machines */
  293. /* we now need to mask valid bytes and add the padding which is */
  294. /* a single 1 bit and as many zero bits as necessary. */
  295. ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];
  296. /* we need 9 or more empty positions, one for the padding byte */
  297. /* (above) and eight for the length count. If there is not */
  298. /* enough space pad and empty the buffer */
  299. if (i > SHA256_BLOCK_SIZE - 9)
  300. {
  301. if (i < 60) ctx->wbuf[15] = 0;
  302. sha256_compile(ctx);
  303. i = 0;
  304. }
  305. else /* compute a word index for the empty buffer positions */
  306. i = (i >> 2) + 1;
  307. while (i < 14) /* and zero pad all but last two positions */
  308. ctx->wbuf[i++] = 0;
  309. /* the following 32-bit length fields are assembled in the */
  310. /* wrong byte order on little endian machines but this is */
  311. /* corrected later since they are only ever used as 32-bit */
  312. /* word values. */
  313. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
  314. ctx->wbuf[15] = ctx->count[0] << 3;
  315. sha256_compile(ctx);
  316. /* extract the hash value as bytes in case the hash buffer is */
  317. /* mislaigned for 32-bit words */
  318. for (i = 0; i < SHA256_DIGEST_SIZE; ++i) hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));
  319. }
  320. /* SHA512 mixing function definitions */
  321. #define s512_0(x) (rotr64_sha2((x), 28) ^ rotr64_sha2((x), 34) ^ rotr64_sha2((x), 39))
  322. #define s512_1(x) (rotr64_sha2((x), 14) ^ rotr64_sha2((x), 18) ^ rotr64_sha2((x), 41))
  323. #define g512_0(x) (rotr64_sha2((x), 1) ^ rotr64_sha2((x), 8) ^ ((x) >> 7))
  324. #define g512_1(x) (rotr64_sha2((x), 19) ^ rotr64_sha2((x), 61) ^ ((x) >> 6))
  325. /* rotated SHA512 round definition. Rather than swapping variables as in */
  326. /* FIPS-180, different variables are 'rotated' on each round, returning */
  327. /* to their starting positions every eight rounds */
  328. #define h5(i) ctx->wbuf[i & 15] += \
  329. g512_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g512_0(ctx->wbuf[(i + 1) & 15])
  330. #define h5_cycle(i,j) \
  331. v[(7 - i) & 7] += (j ? h5(i) : ctx->wbuf[i & 15]) + k512[i + j] \
  332. + s512_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \
  333. v[(3 - i) & 7] += v[(7 - i) & 7]; \
  334. v[(7 - i) & 7] += s512_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])
  335. /* SHA384/SHA512 mixing data */
  336. const uint64_t k512[80] =
  337. {
  338. n_u64(428a2f98d728ae22), n_u64(7137449123ef65cd),
  339. n_u64(b5c0fbcfec4d3b2f), n_u64(e9b5dba58189dbbc),
  340. n_u64(3956c25bf348b538), n_u64(59f111f1b605d019),
  341. n_u64(923f82a4af194f9b), n_u64(ab1c5ed5da6d8118),
  342. n_u64(d807aa98a3030242), n_u64(12835b0145706fbe),
  343. n_u64(243185be4ee4b28c), n_u64(550c7dc3d5ffb4e2),
  344. n_u64(72be5d74f27b896f), n_u64(80deb1fe3b1696b1),
  345. n_u64(9bdc06a725c71235), n_u64(c19bf174cf692694),
  346. n_u64(e49b69c19ef14ad2), n_u64(efbe4786384f25e3),
  347. n_u64(0fc19dc68b8cd5b5), n_u64(240ca1cc77ac9c65),
  348. n_u64(2de92c6f592b0275), n_u64(4a7484aa6ea6e483),
  349. n_u64(5cb0a9dcbd41fbd4), n_u64(76f988da831153b5),
  350. n_u64(983e5152ee66dfab), n_u64(a831c66d2db43210),
  351. n_u64(b00327c898fb213f), n_u64(bf597fc7beef0ee4),
  352. n_u64(c6e00bf33da88fc2), n_u64(d5a79147930aa725),
  353. n_u64(06ca6351e003826f), n_u64(142929670a0e6e70),
  354. n_u64(27b70a8546d22ffc), n_u64(2e1b21385c26c926),
  355. n_u64(4d2c6dfc5ac42aed), n_u64(53380d139d95b3df),
  356. n_u64(650a73548baf63de), n_u64(766a0abb3c77b2a8),
  357. n_u64(81c2c92e47edaee6), n_u64(92722c851482353b),
  358. n_u64(a2bfe8a14cf10364), n_u64(a81a664bbc423001),
  359. n_u64(c24b8b70d0f89791), n_u64(c76c51a30654be30),
  360. n_u64(d192e819d6ef5218), n_u64(d69906245565a910),
  361. n_u64(f40e35855771202a), n_u64(106aa07032bbd1b8),
  362. n_u64(19a4c116b8d2d0c8), n_u64(1e376c085141ab53),
  363. n_u64(2748774cdf8eeb99), n_u64(34b0bcb5e19b48a8),
  364. n_u64(391c0cb3c5c95a63), n_u64(4ed8aa4ae3418acb),
  365. n_u64(5b9cca4f7763e373), n_u64(682e6ff3d6b2b8a3),
  366. n_u64(748f82ee5defb2fc), n_u64(78a5636f43172f60),
  367. n_u64(84c87814a1f0ab72), n_u64(8cc702081a6439ec),
  368. n_u64(90befffa23631e28), n_u64(a4506cebde82bde9),
  369. n_u64(bef9a3f7b2c67915), n_u64(c67178f2e372532b),
  370. n_u64(ca273eceea26619c), n_u64(d186b8c721c0c207),
  371. n_u64(eada7dd6cde0eb1e), n_u64(f57d4f7fee6ed178),
  372. n_u64(06f067aa72176fba), n_u64(0a637dc5a2c898a6),
  373. n_u64(113f9804bef90dae), n_u64(1b710b35131c471b),
  374. n_u64(28db77f523047d84), n_u64(32caab7b40c72493),
  375. n_u64(3c9ebe0a15c9bebc), n_u64(431d67c49c100d4c),
  376. n_u64(4cc5d4becb3e42b6), n_u64(597f299cfc657e2a),
  377. n_u64(5fcb6fab3ad6faec), n_u64(6c44198c4a475817)
  378. };
  379. /* Compile 64 bytes of hash data into SHA384/SHA512 digest value */
  380. void sha512_compile(sha512_ctx ctx[1])
  381. { uint64_t v[8];
  382. uint32_t j;
  383. memcpy(v, ctx->hash, 8 * sizeof(uint64_t));
  384. for (j = 0; j < 80; j += 16)
  385. {
  386. h5_cycle(0, j); h5_cycle(1, j); h5_cycle(2, j); h5_cycle(3, j);
  387. h5_cycle(4, j); h5_cycle(5, j); h5_cycle(6, j); h5_cycle(7, j);
  388. h5_cycle(8, j); h5_cycle(9, j); h5_cycle(10, j); h5_cycle(11, j);
  389. h5_cycle(12, j); h5_cycle(13, j); h5_cycle(14, j); h5_cycle(15, j);
  390. }
  391. ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  392. ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
  393. }
  394. /* Compile 128 bytes of hash data into SHA256 digest value */
  395. /* NOTE: this routine assumes that the byte order in the */
  396. /* ctx->wbuf[] at this point is in such an order that low */
  397. /* address bytes in the ORIGINAL byte stream placed in this */
  398. /* buffer will now go to the high end of words on BOTH big */
  399. /* and little endian systems */
  400. void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
  401. {
  402. uint32_t pos = (uint32_t)(ctx->count[0] & SHA512_MASK),
  403. space = SHA512_BLOCK_SIZE - pos;
  404. const unsigned char *sp = data;
  405. if ((ctx->count[0] += len) < len) ++(ctx->count[1]);
  406. while (len >= space) /* tranfer whole blocks while possible */
  407. {
  408. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, space);
  409. sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;
  410. bsw_64_sha2(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);
  411. sha512_compile(ctx);
  412. }
  413. memcpy(((unsigned char *)ctx->wbuf) + pos, sp, len);
  414. }
  415. /* SHA384/512 Final padding and digest calculation */
  416. static uint64_t m2[8] =
  417. {
  418. n_u64(0000000000000000), n_u64(ff00000000000000),
  419. n_u64(ffff000000000000), n_u64(ffffff0000000000),
  420. n_u64(ffffffff00000000), n_u64(ffffffffff000000),
  421. n_u64(ffffffffffff0000), n_u64(ffffffffffffff00)
  422. };
  423. static uint64_t b2[8] =
  424. {
  425. n_u64(8000000000000000), n_u64(0080000000000000),
  426. n_u64(0000800000000000), n_u64(0000008000000000),
  427. n_u64(0000000080000000), n_u64(0000000000800000),
  428. n_u64(0000000000008000), n_u64(0000000000000080)
  429. };
  430. static void sha_end(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
  431. {
  432. uint32_t i = (uint32_t)(ctx->count[0] & SHA512_MASK);
  433. bsw_64_sha2(ctx->wbuf, (i + 7) >> 3);
  434. /* bytes in the buffer are now in an order in which references */
  435. /* to 64-bit words will put bytes with lower addresses into the */
  436. /* top of 64 bit words on BOTH big and little endian machines */
  437. /* we now need to mask valid bytes and add the padding which is */
  438. /* a single 1 bit and as many zero bits as necessary. */
  439. ctx->wbuf[i >> 3] = (ctx->wbuf[i >> 3] & m2[i & 7]) | b2[i & 7];
  440. /* we need 17 or more empty byte positions, one for the padding */
  441. /* byte (above) and sixteen for the length count. If there is */
  442. /* not enough space pad and empty the buffer */
  443. if (i > SHA512_BLOCK_SIZE - 17)
  444. {
  445. if (i < 120) ctx->wbuf[15] = 0;
  446. sha512_compile(ctx);
  447. i = 0;
  448. }
  449. else i = (i >> 3) + 1;
  450. while (i < 14) ctx->wbuf[i++] = 0;
  451. /* the following 64-bit length fields are assembled in the */
  452. /* wrong byte order on little endian machines but this is */
  453. /* corrected later since they are only ever used as 64-bit */
  454. /* word values. */
  455. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);
  456. ctx->wbuf[15] = ctx->count[0] << 3;
  457. sha512_compile(ctx);
  458. /* extract the hash value as bytes in case the hash buffer is */
  459. /* misaligned for 32-bit words */
  460. for (i = 0; i < hlen; ++i) hval[i] = (unsigned char)(ctx->hash[i >> 3] >> 8 * (~i & 7));
  461. }
  462. ///
  463. /* SHA384 initialisation data */
  464. const uint64_t i384[80] =
  465. {
  466. n_u64(cbbb9d5dc1059ed8), n_u64(629a292a367cd507),
  467. n_u64(9159015a3070dd17), n_u64(152fecd8f70e5939),
  468. n_u64(67332667ffc00b31), n_u64(8eb44a8768581511),
  469. n_u64(db0c2e0d64f98fa7), n_u64(47b5481dbefa4fa4)
  470. };
  471. void sha384_begin(sha384_ctx ctx[1])
  472. {
  473. ctx->count[0] = ctx->count[1] = 0;
  474. memcpy(ctx->hash, i384, 8 * sizeof(uint64_t));
  475. }
  476. void sha384_end(unsigned char hval[], sha384_ctx ctx[1])
  477. {
  478. sha_end(hval, ctx, SHA384_DIGEST_SIZE);
  479. }
  480. ///
  481. /* SHA512 initialisation data */
  482. const uint64_t i512[80] =
  483. {
  484. n_u64(6a09e667f3bcc908), n_u64(bb67ae8584caa73b),
  485. n_u64(3c6ef372fe94f82b), n_u64(a54ff53a5f1d36f1),
  486. n_u64(510e527fade682d1), n_u64(9b05688c2b3e6c1f),
  487. n_u64(1f83d9abfb41bd6b), n_u64(5be0cd19137e2179)
  488. };
  489. void sha512_begin(sha512_ctx ctx[1])
  490. {
  491. ctx->count[0] = ctx->count[1] = 0;
  492. memcpy(ctx->hash, i512, 8 * sizeof(uint64_t));
  493. }
  494. void sha512_end(unsigned char hval[], sha512_ctx ctx[1])
  495. {
  496. sha_end(hval, ctx, SHA512_DIGEST_SIZE);
  497. }
  498. }//detail_sha2
  499. //
  500. vector<char> TC_SHA::sha1bin(const char *data, size_t len)
  501. {
  502. vector<char> digest;
  503. digest.resize(SHA1_DIGEST_SIZE);
  504. detail_sha1::sha1_ctx cx[1];
  505. detail_sha1::sha1_begin(cx);
  506. detail_sha1::sha1_hash((unsigned char *)data, len, cx);
  507. detail_sha1::sha1_end((unsigned char *)&digest[0], cx);
  508. return digest;
  509. }
  510. string TC_SHA::sha1str(const char *buffer, size_t length)
  511. {
  512. vector<char> digest = sha1bin(buffer, length);
  513. return TC_Common::bin2str(&digest[0], digest.size());
  514. }
  515. string TC_SHA::sha1file(const string &fileName)
  516. {
  517. unsigned char sOutBuffer[SHA1_DIGEST_SIZE];
  518. unsigned char buf[16*1024];
  519. FILE *f;
  520. size_t n;
  521. detail_sha1::sha1_ctx cx[1];
  522. if(( f = fopen( fileName.c_str(), "rb" )) == NULL )
  523. throw TC_SHA_Exception("[TC_SHA::sha1file] fopen '" + fileName + "', error.", errno);
  524. detail_sha1::sha1_begin(cx);
  525. while((n = fread( buf, 1, sizeof( buf ),f)) > 0 )
  526. detail_sha1::sha1_hash(buf, n, cx);
  527. detail_sha1::sha1_end (sOutBuffer, cx);
  528. fclose(f);
  529. return TC_Common::bin2str(sOutBuffer, sizeof(sOutBuffer));
  530. }
  531. vector<char> TC_SHA::sha256bin(const char *data, size_t len)
  532. {
  533. vector<char> digest;
  534. digest.resize(SHA256_DIGEST_SIZE);
  535. detail_sha2::sha256_ctx cx[1];
  536. detail_sha2::sha256_begin(cx);
  537. detail_sha2::sha256_hash((unsigned char *)data, len, cx);
  538. detail_sha2::sha256_end((unsigned char *)&digest[0], cx);
  539. return digest;
  540. }
  541. string TC_SHA::sha256str(const char *buffer, size_t length)
  542. {
  543. vector<char> digest = sha256bin(buffer, length);
  544. return TC_Common::bin2str(&digest[0], digest.size());
  545. }
  546. string TC_SHA::sha256file(const string &fileName)
  547. {
  548. unsigned char sOutBuffer[SHA256_DIGEST_SIZE];
  549. unsigned char buf[16*1024];
  550. FILE *f;
  551. size_t n;
  552. detail_sha2::sha256_ctx cx[1];
  553. if(( f = fopen( fileName.c_str(), "rb" )) == NULL )
  554. throw TC_SHA_Exception("[TC_SHA::sha256file] fopen '" + fileName + "', error.", errno);
  555. detail_sha2::sha256_begin(cx);
  556. while((n = fread( buf, 1, sizeof( buf ),f)) > 0 )
  557. detail_sha2::sha256_hash(buf, n, cx);
  558. detail_sha2::sha256_end(sOutBuffer, cx);
  559. fclose(f);
  560. return TC_Common::bin2str(sOutBuffer, sizeof(sOutBuffer));
  561. }
  562. vector<char> TC_SHA::sha384bin(const char *data, size_t len)
  563. {
  564. vector<char> digest;
  565. digest.resize(SHA384_DIGEST_SIZE);
  566. detail_sha2::sha384_ctx cx[1];
  567. detail_sha2::sha384_begin(cx);
  568. detail_sha2::sha512_hash((unsigned char *)data, len, cx);
  569. detail_sha2::sha384_end((unsigned char *)&digest[0], cx);
  570. return digest;
  571. }
  572. string TC_SHA::sha384str(const char *buffer, size_t length)
  573. {
  574. vector<char> digest = sha384bin(buffer, length);
  575. return TC_Common::bin2str(&digest[0], digest.size());
  576. }
  577. string TC_SHA::sha384file(const string &fileName)
  578. {
  579. unsigned char sOutBuffer[SHA384_DIGEST_SIZE];
  580. unsigned char buf[16*1024];
  581. FILE *f;
  582. size_t n;
  583. detail_sha2::sha384_ctx cx[1];
  584. if(( f = fopen( fileName.c_str(), "rb" )) == NULL )
  585. throw TC_SHA_Exception("[TC_SHA::sha384file] fopen '" + fileName + "', error.", errno);
  586. detail_sha2::sha384_begin(cx);
  587. while((n = fread( buf, 1, sizeof( buf ),f)) > 0 )
  588. detail_sha2::sha512_hash(buf, n, cx);
  589. detail_sha2::sha384_end(sOutBuffer, cx);
  590. fclose(f);
  591. return TC_Common::bin2str(sOutBuffer, sizeof(sOutBuffer));
  592. }
  593. vector<char> TC_SHA::sha512bin(const char *data, size_t len)
  594. {
  595. vector<char> digest;
  596. digest.resize(SHA512_DIGEST_SIZE);
  597. detail_sha2::sha512_ctx cx[1];
  598. detail_sha2::sha512_begin(cx);
  599. detail_sha2::sha512_hash((unsigned char *)data, len, cx);
  600. detail_sha2::sha512_end((unsigned char *)&digest[0], cx);
  601. return digest;
  602. }
  603. string TC_SHA::sha512str(const char *buffer, size_t length)
  604. {
  605. vector<char> digest = sha512bin(buffer, length);
  606. return TC_Common::bin2str(&digest[0], digest.size());
  607. }
  608. string TC_SHA::sha512file(const string &fileName)
  609. {
  610. unsigned char sOutBuffer[SHA512_DIGEST_SIZE];
  611. unsigned char buf[16*1024];
  612. FILE *f;
  613. size_t n;
  614. detail_sha2::sha512_ctx cx[1];
  615. if(( f = fopen( fileName.c_str(), "rb" )) == NULL )
  616. throw TC_SHA_Exception("[TC_SHA::sha512file] fopen '" + fileName + "', error.", errno);
  617. detail_sha2::sha512_begin(cx);
  618. while((n = fread( buf, 1, sizeof( buf ),f)) > 0 )
  619. detail_sha2::sha512_hash(buf, n, cx);
  620. detail_sha2::sha512_end(sOutBuffer, cx);
  621. fclose(f);
  622. return TC_Common::bin2str(sOutBuffer, sizeof(sOutBuffer));
  623. }
  624. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载