hl_sha2ext.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2010 Benjamin Grüdelbach
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1) Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2) Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. //----------------------------------------------------------------------
  29. /*
  30. * The hashlib++ SHA384 and SHA512 implementations are derivative from
  31. * the sourcecode published by Aaron D. Gifford
  32. *
  33. * Copyright (c) 2000-2001, Aaron D. Gifford
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in the
  43. * documentation and/or other materials provided with the distribution.
  44. * 3. Neither the name of the copyright holder nor the names of contributors
  45. * may be used to endorse or promote products derived from this software
  46. * without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  49. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  52. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58. * SUCH DAMAGE.
  59. *
  60. */
  61. //----------------------------------------------------------------------
  62. /**
  63. * @file sha2ext.cpp
  64. * @brief This file contains the implementation of the SHA2Ext class
  65. * @date Mo 12 Nov 2007
  66. */
  67. //----------------------------------------------------------------------
  68. // Standard C includes
  69. #include <string.h>
  70. #include <assert.h>
  71. //----------------------------------------------------------------------
  72. //hashlib++ includes
  73. #include "hl_sha2ext.h"
  74. #include "hl_sha2mac.h"
  75. //----------------------------------------------------------------------
  76. /* Hash constant words K for SHA-384 and SHA-512: */
  77. const static sha2_word64 K512[80] = {
  78. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  79. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  80. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  81. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  82. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  83. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  84. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  85. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  86. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  87. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  88. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  89. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  90. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  91. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  92. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  93. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  94. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  95. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  96. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  97. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  98. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  99. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  100. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  101. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  102. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  103. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  104. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  105. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  106. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  107. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  108. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  109. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  110. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  111. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  112. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  113. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  114. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  115. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  116. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  117. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  118. };
  119. /* Initial hash value H for SHA-384 */
  120. const static sha2_word64 sha384_initial_hash_value[8] = {
  121. 0xcbbb9d5dc1059ed8ULL,
  122. 0x629a292a367cd507ULL,
  123. 0x9159015a3070dd17ULL,
  124. 0x152fecd8f70e5939ULL,
  125. 0x67332667ffc00b31ULL,
  126. 0x8eb44a8768581511ULL,
  127. 0xdb0c2e0d64f98fa7ULL,
  128. 0x47b5481dbefa4fa4ULL
  129. };
  130. /* Initial hash value H for SHA-512 */
  131. const static sha2_word64 sha512_initial_hash_value[8] = {
  132. 0x6a09e667f3bcc908ULL,
  133. 0xbb67ae8584caa73bULL,
  134. 0x3c6ef372fe94f82bULL,
  135. 0xa54ff53a5f1d36f1ULL,
  136. 0x510e527fade682d1ULL,
  137. 0x9b05688c2b3e6c1fULL,
  138. 0x1f83d9abfb41bd6bULL,
  139. 0x5be0cd19137e2179ULL
  140. };
  141. /*
  142. * * Constant used by SHA256/384/512_End() functions for converting the
  143. * * digest to a readable hexadecimal character string:
  144. * */
  145. static const char *sha2_hex_digits = "0123456789abcdef";
  146. //----------------------------------------------------------------------
  147. /**
  148. * @brief Initialize the SHA512 context
  149. * @param context The context to init.
  150. */
  151. void SHA2ext::SHA512_Init(HL_SHA512_CTX* context)
  152. {
  153. if (context == (HL_SHA512_CTX*)0) {
  154. return;
  155. }
  156. MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
  157. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
  158. context->bitcount[0] = context->bitcount[1] = 0;
  159. }
  160. #ifdef SHA2_UNROLL_TRANSFORM
  161. /* Unrolled SHA-512 round macros: */
  162. #if BYTE_ORDER == LITTLE_ENDIAN
  163. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  164. REVERSE64(*data++, W512[j]); \
  165. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  166. K512[j] + W512[j]; \
  167. (d) += T1, \
  168. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
  169. j++
  170. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  171. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  172. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  173. K512[j] + (W512[j] = *data++); \
  174. (d) += T1; \
  175. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  176. j++
  177. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  178. #define ROUND512(a,b,c,d,e,f,g,h) \
  179. s0 = W512[(j+1)&0x0f]; \
  180. s0 = sigma0_512(s0); \
  181. s1 = W512[(j+14)&0x0f]; \
  182. s1 = sigma1_512(s1); \
  183. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  184. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  185. (d) += T1; \
  186. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  187. j++
  188. /**
  189. * @brief Internal data transformation
  190. * @param context The context to use
  191. * @param data The data to transform
  192. */
  193. void SHA2ext::SHA512_Transform(HL_SHA512_CTX* context, const sha2_word64* data) {
  194. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  195. sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
  196. int j;
  197. /* Initialize registers with the prev. intermediate value */
  198. a = context->state[0];
  199. b = context->state[1];
  200. c = context->state[2];
  201. d = context->state[3];
  202. e = context->state[4];
  203. f = context->state[5];
  204. g = context->state[6];
  205. h = context->state[7];
  206. j = 0;
  207. do {
  208. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  209. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  210. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  211. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  212. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  213. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  214. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  215. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  216. } while (j < 16);
  217. /* Now for the remaining rounds up to 79: */
  218. do {
  219. ROUND512(a,b,c,d,e,f,g,h);
  220. ROUND512(h,a,b,c,d,e,f,g);
  221. ROUND512(g,h,a,b,c,d,e,f);
  222. ROUND512(f,g,h,a,b,c,d,e);
  223. ROUND512(e,f,g,h,a,b,c,d);
  224. ROUND512(d,e,f,g,h,a,b,c);
  225. ROUND512(c,d,e,f,g,h,a,b);
  226. ROUND512(b,c,d,e,f,g,h,a);
  227. } while (j < 80);
  228. /* Compute the current intermediate hash value */
  229. context->state[0] += a;
  230. context->state[1] += b;
  231. context->state[2] += c;
  232. context->state[3] += d;
  233. context->state[4] += e;
  234. context->state[5] += f;
  235. context->state[6] += g;
  236. context->state[7] += h;
  237. /* Clean up */
  238. a = b = c = d = e = f = g = h = T1 = 0;
  239. }
  240. #else /* SHA2_UNROLL_TRANSFORM */
  241. /**
  242. * @brief Internal data transformation
  243. * @param context The context to use
  244. * @param data The data to transform
  245. */
  246. void SHA2ext::SHA512_Transform(HL_SHA512_CTX* context, const sha2_word64* data) {
  247. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  248. sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
  249. int j;
  250. /* Initialize registers with the prev. intermediate value */
  251. a = context->state[0];
  252. b = context->state[1];
  253. c = context->state[2];
  254. d = context->state[3];
  255. e = context->state[4];
  256. f = context->state[5];
  257. g = context->state[6];
  258. h = context->state[7];
  259. j = 0;
  260. do {
  261. #if BYTE_ORDER == LITTLE_ENDIAN
  262. /* Convert TO host byte order */
  263. REVERSE64(*data++, W512[j]);
  264. /* Apply the SHA-512 compression function to update a..h */
  265. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  266. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  267. /* Apply the SHA-512 compression function to update a..h with copy */
  268. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  269. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  270. T2 = Sigma0_512(a) + Maj(a, b, c);
  271. h = g;
  272. g = f;
  273. f = e;
  274. e = d + T1;
  275. d = c;
  276. c = b;
  277. b = a;
  278. a = T1 + T2;
  279. j++;
  280. } while (j < 16);
  281. do {
  282. /* Part of the message block expansion: */
  283. s0 = W512[(j+1)&0x0f];
  284. s0 = sigma0_512(s0);
  285. s1 = W512[(j+14)&0x0f];
  286. s1 = sigma1_512(s1);
  287. /* Apply the SHA-512 compression function to update a..h */
  288. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  289. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  290. T2 = Sigma0_512(a) + Maj(a, b, c);
  291. h = g;
  292. g = f;
  293. f = e;
  294. e = d + T1;
  295. d = c;
  296. c = b;
  297. b = a;
  298. a = T1 + T2;
  299. j++;
  300. } while (j < 80);
  301. /* Compute the current intermediate hash value */
  302. context->state[0] += a;
  303. context->state[1] += b;
  304. context->state[2] += c;
  305. context->state[3] += d;
  306. context->state[4] += e;
  307. context->state[5] += f;
  308. context->state[6] += g;
  309. context->state[7] += h;
  310. /* Clean up */
  311. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  312. }
  313. #endif /* SHA2_UNROLL_TRANSFORM */
  314. void SHA2ext::SHA512_Update(HL_SHA512_CTX* context, const sha2_byte *data, unsigned int len) {
  315. unsigned int freespace, usedspace;
  316. if (len == 0) {
  317. /* Calling with no data is valid - we do nothing */
  318. return;
  319. }
  320. /* Sanity check: */
  321. assert(context != (HL_SHA512_CTX*)0 && data != (sha2_byte*)0);
  322. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  323. if (usedspace > 0) {
  324. /* Calculate how much free space is available in the buffer */
  325. freespace = SHA512_BLOCK_LENGTH - usedspace;
  326. if (len >= freespace) {
  327. /* Fill the buffer completely and process it */
  328. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  329. ADDINC128(context->bitcount, freespace << 3);
  330. len -= freespace;
  331. data += freespace;
  332. SHA512_Transform(context, (sha2_word64*)context->buffer);
  333. } else {
  334. /* The buffer is not yet full */
  335. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  336. ADDINC128(context->bitcount, len << 3);
  337. /* Clean up: */
  338. usedspace = freespace = 0;
  339. return;
  340. }
  341. }
  342. while (len >= SHA512_BLOCK_LENGTH) {
  343. /* Process as many complete blocks as we can */
  344. SHA512_Transform(context, (sha2_word64*)data);
  345. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  346. len -= SHA512_BLOCK_LENGTH;
  347. data += SHA512_BLOCK_LENGTH;
  348. }
  349. if (len > 0) {
  350. /* There's left-overs, so save 'em */
  351. MEMCPY_BCOPY(context->buffer, data, len);
  352. ADDINC128(context->bitcount, len << 3);
  353. }
  354. /* Clean up: */
  355. usedspace = freespace = 0;
  356. }
  357. void SHA2ext::SHA512_Last(HL_SHA512_CTX* context)
  358. {
  359. unsigned int usedspace;
  360. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  361. #if BYTE_ORDER == LITTLE_ENDIAN
  362. /* Convert FROM host byte order */
  363. REVERSE64(context->bitcount[0],context->bitcount[0]);
  364. REVERSE64(context->bitcount[1],context->bitcount[1]);
  365. #endif
  366. if (usedspace > 0) {
  367. /* Begin padding with a 1 bit: */
  368. context->buffer[usedspace++] = 0x80;
  369. if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
  370. /* Set-up for the last transform: */
  371. MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
  372. } else {
  373. if (usedspace < SHA512_BLOCK_LENGTH) {
  374. MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
  375. }
  376. /* Do second-to-last transform: */
  377. SHA512_Transform(context, (sha2_word64*)context->buffer);
  378. /* And set-up for the last transform: */
  379. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
  380. }
  381. } else {
  382. /* Prepare for final transform: */
  383. MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
  384. /* Begin padding with a 1 bit: */
  385. *context->buffer = 0x80;
  386. }
  387. /* Store the length of input data (in bits): */
  388. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
  389. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
  390. /* Final transform: */
  391. SHA512_Transform(context, (sha2_word64*)context->buffer);
  392. }
  393. void SHA2ext::SHA512_Final(sha2_byte digest[], HL_SHA512_CTX* context)
  394. {
  395. sha2_word64 *d = (sha2_word64*)digest;
  396. /* Sanity check: */
  397. assert(context != (HL_SHA512_CTX*)0);
  398. /* If no digest buffer is passed, we don't bother doing this: */
  399. if (digest != (sha2_byte*)0) {
  400. SHA512_Last(context);
  401. /* Save the hash data for output: */
  402. #if BYTE_ORDER == LITTLE_ENDIAN
  403. {
  404. /* Convert TO host byte order */
  405. int j;
  406. for (j = 0; j < 8; j++) {
  407. REVERSE64(context->state[j],context->state[j]);
  408. *d++ = context->state[j];
  409. }
  410. }
  411. #else
  412. MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
  413. #endif
  414. }
  415. /* Zero out state data */
  416. MEMSET_BZERO(context, sizeof(context));
  417. }
  418. char* SHA2ext::SHA512_End(HL_SHA512_CTX* context, char buffer[])
  419. {
  420. sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
  421. int i;
  422. /* Sanity check: */
  423. assert(context != (HL_SHA512_CTX*)0);
  424. if (buffer != (char*)0) {
  425. SHA512_Final(digest, context);
  426. for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  427. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  428. *buffer++ = sha2_hex_digits[*d & 0x0f];
  429. d++;
  430. }
  431. *buffer = (char)0;
  432. } else {
  433. MEMSET_BZERO(context, sizeof(context));
  434. }
  435. MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
  436. return buffer;
  437. }
  438. void SHA2ext::SHA384_Init(HL_SHA_384_CTX* context)
  439. {
  440. if (context == (HL_SHA_384_CTX*)0) {
  441. return;
  442. }
  443. MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
  444. MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
  445. context->bitcount[0] = context->bitcount[1] = 0;
  446. }
  447. void SHA2ext::SHA384_Update(HL_SHA_384_CTX* context, const sha2_byte* data, unsigned int len)
  448. {
  449. SHA512_Update((HL_SHA512_CTX*)context, data, len);
  450. }
  451. void SHA2ext::SHA384_Final(sha2_byte digest[], HL_SHA_384_CTX* context)
  452. {
  453. sha2_word64 *d = (sha2_word64*)digest;
  454. /* Sanity check: */
  455. assert(context != (HL_SHA_384_CTX*)0);
  456. /* If no digest buffer is passed, we don't bother doing this: */
  457. if (digest != (sha2_byte*)0) {
  458. SHA512_Last((HL_SHA512_CTX*)context);
  459. /* Save the hash data for output: */
  460. #if BYTE_ORDER == LITTLE_ENDIAN
  461. {
  462. /* Convert TO host byte order */
  463. int j;
  464. for (j = 0; j < 6; j++) {
  465. REVERSE64(context->state[j],context->state[j]);
  466. *d++ = context->state[j];
  467. }
  468. }
  469. #else
  470. MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
  471. #endif
  472. }
  473. /* Zero out state data */
  474. MEMSET_BZERO(context, sizeof(context));
  475. }
  476. char* SHA2ext::SHA384_End(HL_SHA_384_CTX* context, char buffer[])
  477. {
  478. sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
  479. int i;
  480. /* Sanity check: */
  481. assert(context != (HL_SHA_384_CTX*)0);
  482. if (buffer != (char*)0) {
  483. SHA384_Final(digest, context);
  484. for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
  485. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  486. *buffer++ = sha2_hex_digits[*d & 0x0f];
  487. d++;
  488. }
  489. *buffer = (char)0;
  490. } else {
  491. MEMSET_BZERO(context, sizeof(context));
  492. }
  493. MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
  494. return buffer;
  495. }
  496. //----------------------------------------------------------------------
  497. //EOF