sha2_internal.cc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * FILE: sha2.c
  3. * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
  4. *
  5. * Copyright (c) 2000-2001, Aaron D. Gifford
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holder nor the names of contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
  33. */
  34. #include <config.h>
  35. #include <endian.h>
  36. #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
  37. #include <assert.h> /* assert() */
  38. #include "sha2_internal.h"
  39. /*
  40. * ASSERT NOTE:
  41. * Some sanity checking code is included using assert(). On my FreeBSD
  42. * system, this additional code can be removed by compiling with NDEBUG
  43. * defined. Check your own systems manpage on assert() to see how to
  44. * compile WITHOUT the sanity checking code on your system.
  45. *
  46. * UNROLLED TRANSFORM LOOP NOTE:
  47. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  48. * loop version for the hash transform rounds (defined using macros
  49. * later in this file). Either define on the command line, for example:
  50. *
  51. * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  52. *
  53. * or define below:
  54. *
  55. * #define SHA2_UNROLL_TRANSFORM
  56. *
  57. */
  58. /*** SHA-256/384/512 Machine Architecture Definitions *****************/
  59. /*
  60. * BYTE_ORDER NOTE:
  61. *
  62. * Please make sure that your system defines BYTE_ORDER. If your
  63. * architecture is little-endian, make sure it also defines
  64. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  65. * equivalent.
  66. *
  67. * If your system does not define the above, then you can do so by
  68. * hand like this:
  69. *
  70. * #define LITTLE_ENDIAN 1234
  71. * #define BIG_ENDIAN 4321
  72. *
  73. * And for little-endian machines, add:
  74. *
  75. * #define BYTE_ORDER LITTLE_ENDIAN
  76. *
  77. * Or for big-endian machines:
  78. *
  79. * #define BYTE_ORDER BIG_ENDIAN
  80. *
  81. * The FreeBSD machine this was written on defines BYTE_ORDER
  82. * appropriately by including <sys/types.h> (which in turn includes
  83. * <machine/endian.h> where the appropriate definitions are actually
  84. * made).
  85. */
  86. #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
  87. #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
  88. #endif
  89. /*
  90. * Define the followingsha2_* types to types of the correct length on
  91. * the native archtecture. Most BSD systems and Linux define u_intXX_t
  92. * types. Machines with very recent ANSI C headers, can use the
  93. * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
  94. * during compile or in the sha.h header file.
  95. *
  96. * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
  97. * will need to define these three typedefs below (and the appropriate
  98. * ones in sha.h too) by hand according to their system architecture.
  99. *
  100. * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
  101. * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
  102. */
  103. #ifdef SHA2_USE_INTTYPES_H
  104. typedef uint8_t sha2_byte; /* Exactly 1 byte */
  105. typedef uint32_t sha2_word32; /* Exactly 4 bytes */
  106. typedef uint64_t sha2_word64; /* Exactly 8 bytes */
  107. #else /* SHA2_USE_INTTYPES_H */
  108. typedef u_int8_t sha2_byte; /* Exactly 1 byte */
  109. typedef u_int32_t sha2_word32; /* Exactly 4 bytes */
  110. typedef u_int64_t sha2_word64; /* Exactly 8 bytes */
  111. #endif /* SHA2_USE_INTTYPES_H */
  112. /*** SHA-256/384/512 Various Length Definitions ***********************/
  113. /* NOTE: Most of these are in sha2.h */
  114. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  115. #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
  116. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  117. /*** ENDIAN REVERSAL MACROS *******************************************/
  118. #if BYTE_ORDER == LITTLE_ENDIAN
  119. #define REVERSE32(w,x) { \
  120. sha2_word32 tmp = (w); \
  121. tmp = (tmp >> 16) | (tmp << 16); \
  122. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  123. }
  124. #define REVERSE64(w,x) { \
  125. sha2_word64 tmp = (w); \
  126. tmp = (tmp >> 32) | (tmp << 32); \
  127. tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
  128. ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
  129. (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
  130. ((tmp & 0x0000ffff0000ffffULL) << 16); \
  131. }
  132. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  133. /*
  134. * Macro for incrementally adding the unsigned 64-bit integer n to the
  135. * unsigned 128-bit integer (represented using a two-element array of
  136. * 64-bit words):
  137. */
  138. #define ADDINC128(w,n) { \
  139. (w)[0] += (sha2_word64)(n); \
  140. if ((w)[0] < (n)) { \
  141. (w)[1]++; \
  142. } \
  143. }
  144. /*
  145. * Macros for copying blocks of memory and for zeroing out ranges
  146. * of memory. Using these macros makes it easy to switch from
  147. * using memset()/memcpy() and using bzero()/bcopy().
  148. *
  149. * Please define either SHA2_USE_MEMSET_MEMCPY or define
  150. * SHA2_USE_BZERO_BCOPY depending on which function set you
  151. * choose to use:
  152. */
  153. #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
  154. /* Default to memset()/memcpy() if no option is specified */
  155. #define SHA2_USE_MEMSET_MEMCPY 1
  156. #endif
  157. #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
  158. /* Abort with an error if BOTH options are defined */
  159. #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
  160. #endif
  161. #ifdef SHA2_USE_MEMSET_MEMCPY
  162. #define MEMSET_BZERO(p,l) memset((p), 0, (l))
  163. #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
  164. #endif
  165. #ifdef SHA2_USE_BZERO_BCOPY
  166. #define MEMSET_BZERO(p,l) bzero((p), (l))
  167. #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
  168. #endif
  169. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  170. /*
  171. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  172. *
  173. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  174. * S is a ROTATION) because the SHA-256/384/512 description document
  175. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  176. * same "backwards" definition.
  177. */
  178. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  179. #define R(b,x) ((x) >> (b))
  180. /* 32-bit Rotate-right (used in SHA-256): */
  181. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  182. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  183. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  184. /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
  185. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  186. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  187. /* Four of six logical functions used in SHA-256: */
  188. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  189. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  190. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  191. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  192. /* Four of six logical functions used in SHA-384 and SHA-512: */
  193. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  194. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  195. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  196. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
  197. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  198. /* NOTE: These should not be accessed directly from outside this
  199. * library -- they are intended for private internal visibility/use
  200. * only.
  201. */
  202. static void SHA512_Last(SHA512_CTX*);
  203. static void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
  204. static void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
  205. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  206. /* Hash constant words K for SHA-256: */
  207. const static sha2_word32 K256[64] = {
  208. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  209. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  210. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  211. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  212. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  213. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  214. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  215. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  216. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  217. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  218. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  219. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  220. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  221. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  222. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  223. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  224. };
  225. /* Initial hash value H for SHA-256: */
  226. const static sha2_word32 sha256_initial_hash_value[8] = {
  227. 0x6a09e667UL,
  228. 0xbb67ae85UL,
  229. 0x3c6ef372UL,
  230. 0xa54ff53aUL,
  231. 0x510e527fUL,
  232. 0x9b05688cUL,
  233. 0x1f83d9abUL,
  234. 0x5be0cd19UL
  235. };
  236. /* Hash constant words K for SHA-384 and SHA-512: */
  237. const static sha2_word64 K512[80] = {
  238. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  239. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  240. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  241. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  242. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  243. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  244. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  245. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  246. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  247. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  248. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  249. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  250. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  251. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  252. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  253. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  254. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  255. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  256. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  257. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  258. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  259. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  260. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  261. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  262. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  263. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  264. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  265. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  266. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  267. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  268. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  269. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  270. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  271. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  272. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  273. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  274. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  275. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  276. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  277. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  278. };
  279. /* Initial hash value H for SHA-384 */
  280. const static sha2_word64 sha384_initial_hash_value[8] = {
  281. 0xcbbb9d5dc1059ed8ULL,
  282. 0x629a292a367cd507ULL,
  283. 0x9159015a3070dd17ULL,
  284. 0x152fecd8f70e5939ULL,
  285. 0x67332667ffc00b31ULL,
  286. 0x8eb44a8768581511ULL,
  287. 0xdb0c2e0d64f98fa7ULL,
  288. 0x47b5481dbefa4fa4ULL
  289. };
  290. /* Initial hash value H for SHA-512 */
  291. const static sha2_word64 sha512_initial_hash_value[8] = {
  292. 0x6a09e667f3bcc908ULL,
  293. 0xbb67ae8584caa73bULL,
  294. 0x3c6ef372fe94f82bULL,
  295. 0xa54ff53a5f1d36f1ULL,
  296. 0x510e527fade682d1ULL,
  297. 0x9b05688c2b3e6c1fULL,
  298. 0x1f83d9abfb41bd6bULL,
  299. 0x5be0cd19137e2179ULL
  300. };
  301. /*
  302. * Constant used by SHA256/384/512_End() functions for converting the
  303. * digest to a readable hexadecimal character string:
  304. */
  305. static const char *sha2_hex_digits = "0123456789abcdef";
  306. /*** SHA-256: *********************************************************/
  307. void SHA256_Init(SHA256_CTX* context) {
  308. if (context == (SHA256_CTX*)0) {
  309. return;
  310. }
  311. MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
  312. MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
  313. context->bitcount = 0;
  314. }
  315. #ifdef SHA2_UNROLL_TRANSFORM
  316. /* Unrolled SHA-256 round macros: */
  317. #if BYTE_ORDER == LITTLE_ENDIAN
  318. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  319. REVERSE32(*data++, W256[j]); \
  320. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  321. K256[j] + W256[j]; \
  322. (d) += T1; \
  323. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  324. j++
  325. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  326. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  327. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  328. K256[j] + (W256[j] = *data++); \
  329. (d) += T1; \
  330. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  331. j++
  332. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  333. #define ROUND256(a,b,c,d,e,f,g,h) \
  334. s0 = W256[(j+1)&0x0f]; \
  335. s0 = sigma0_256(s0); \
  336. s1 = W256[(j+14)&0x0f]; \
  337. s1 = sigma1_256(s1); \
  338. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
  339. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
  340. (d) += T1; \
  341. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  342. j++
  343. static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  344. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  345. sha2_word32 T1, *W256;
  346. int j;
  347. W256 = (sha2_word32*)context->buffer;
  348. /* Initialize registers with the prev. intermediate value */
  349. a = context->state[0];
  350. b = context->state[1];
  351. c = context->state[2];
  352. d = context->state[3];
  353. e = context->state[4];
  354. f = context->state[5];
  355. g = context->state[6];
  356. h = context->state[7];
  357. j = 0;
  358. do {
  359. /* Rounds 0 to 15 (unrolled): */
  360. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  361. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  362. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  363. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  364. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  365. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  366. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  367. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  368. } while (j < 16);
  369. /* Now for the remaining rounds to 64: */
  370. do {
  371. ROUND256(a,b,c,d,e,f,g,h);
  372. ROUND256(h,a,b,c,d,e,f,g);
  373. ROUND256(g,h,a,b,c,d,e,f);
  374. ROUND256(f,g,h,a,b,c,d,e);
  375. ROUND256(e,f,g,h,a,b,c,d);
  376. ROUND256(d,e,f,g,h,a,b,c);
  377. ROUND256(c,d,e,f,g,h,a,b);
  378. ROUND256(b,c,d,e,f,g,h,a);
  379. } while (j < 64);
  380. /* Compute the current intermediate hash value */
  381. context->state[0] += a;
  382. context->state[1] += b;
  383. context->state[2] += c;
  384. context->state[3] += d;
  385. context->state[4] += e;
  386. context->state[5] += f;
  387. context->state[6] += g;
  388. context->state[7] += h;
  389. /* Clean up */
  390. a = b = c = d = e = f = g = h = T1 = 0;
  391. }
  392. #else /* SHA2_UNROLL_TRANSFORM */
  393. static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  394. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  395. sha2_word32 T1, T2, *W256;
  396. int j;
  397. W256 = (sha2_word32*)context->buffer;
  398. /* Initialize registers with the prev. intermediate value */
  399. a = context->state[0];
  400. b = context->state[1];
  401. c = context->state[2];
  402. d = context->state[3];
  403. e = context->state[4];
  404. f = context->state[5];
  405. g = context->state[6];
  406. h = context->state[7];
  407. j = 0;
  408. do {
  409. #if BYTE_ORDER == LITTLE_ENDIAN
  410. /* Copy data while converting to host byte order */
  411. REVERSE32(*data++,W256[j]);
  412. /* Apply the SHA-256 compression function to update a..h */
  413. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
  414. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  415. /* Apply the SHA-256 compression function to update a..h with copy */
  416. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
  417. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  418. T2 = Sigma0_256(a) + Maj(a, b, c);
  419. h = g;
  420. g = f;
  421. f = e;
  422. e = d + T1;
  423. d = c;
  424. c = b;
  425. b = a;
  426. a = T1 + T2;
  427. j++;
  428. } while (j < 16);
  429. do {
  430. /* Part of the message block expansion: */
  431. s0 = W256[(j+1)&0x0f];
  432. s0 = sigma0_256(s0);
  433. s1 = W256[(j+14)&0x0f];
  434. s1 = sigma1_256(s1);
  435. /* Apply the SHA-256 compression function to update a..h */
  436. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  437. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  438. T2 = Sigma0_256(a) + Maj(a, b, c);
  439. h = g;
  440. g = f;
  441. f = e;
  442. e = d + T1;
  443. d = c;
  444. c = b;
  445. b = a;
  446. a = T1 + T2;
  447. j++;
  448. } while (j < 64);
  449. /* Compute the current intermediate hash value */
  450. context->state[0] += a;
  451. context->state[1] += b;
  452. context->state[2] += c;
  453. context->state[3] += d;
  454. context->state[4] += e;
  455. context->state[5] += f;
  456. context->state[6] += g;
  457. context->state[7] += h;
  458. /* Clean up */
  459. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  460. }
  461. #endif /* SHA2_UNROLL_TRANSFORM */
  462. void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
  463. unsigned int freespace, usedspace;
  464. if (len == 0) {
  465. /* Calling with no data is valid - we do nothing */
  466. return;
  467. }
  468. /* Sanity check: */
  469. assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
  470. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  471. if (usedspace > 0) {
  472. /* Calculate how much free space is available in the buffer */
  473. freespace = SHA256_BLOCK_LENGTH - usedspace;
  474. if (len >= freespace) {
  475. /* Fill the buffer completely and process it */
  476. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  477. context->bitcount += freespace << 3;
  478. len -= freespace;
  479. data += freespace;
  480. SHA256_Transform(context, (sha2_word32*)context->buffer);
  481. } else {
  482. /* The buffer is not yet full */
  483. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  484. context->bitcount += len << 3;
  485. /* Clean up: */
  486. usedspace = freespace = 0;
  487. return;
  488. }
  489. }
  490. while (len >= SHA256_BLOCK_LENGTH) {
  491. /* Process as many complete blocks as we can */
  492. sha2_byte buffer[SHA256_BLOCK_LENGTH];
  493. MEMCPY_BCOPY(buffer, data, SHA256_BLOCK_LENGTH);
  494. SHA256_Transform(context, (sha2_word32*)buffer);
  495. context->bitcount += SHA256_BLOCK_LENGTH << 3;
  496. len -= SHA256_BLOCK_LENGTH;
  497. data += SHA256_BLOCK_LENGTH;
  498. }
  499. if (len > 0) {
  500. /* There's left-overs, so save 'em */
  501. MEMCPY_BCOPY(context->buffer, data, len);
  502. context->bitcount += len << 3;
  503. }
  504. /* Clean up: */
  505. usedspace = freespace = 0;
  506. }
  507. void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
  508. sha2_word32 *d = (sha2_word32*)digest;
  509. unsigned int usedspace;
  510. /* Sanity check: */
  511. assert(context != (SHA256_CTX*)0);
  512. /* If no digest buffer is passed, we don't bother doing this: */
  513. if (digest != (sha2_byte*)0) {
  514. usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
  515. #if BYTE_ORDER == LITTLE_ENDIAN
  516. /* Convert FROM host byte order */
  517. REVERSE64(context->bitcount,context->bitcount);
  518. #endif
  519. if (usedspace > 0) {
  520. /* Begin padding with a 1 bit: */
  521. context->buffer[usedspace++] = 0x80;
  522. if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
  523. /* Set-up for the last transform: */
  524. MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
  525. } else {
  526. if (usedspace < SHA256_BLOCK_LENGTH) {
  527. MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
  528. }
  529. /* Do second-to-last transform: */
  530. SHA256_Transform(context, (sha2_word32*)context->buffer);
  531. /* And set-up for the last transform: */
  532. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  533. }
  534. } else {
  535. /* Set-up for the last transform: */
  536. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  537. /* Begin padding with a 1 bit: */
  538. *context->buffer = 0x80;
  539. }
  540. /* Set the bit count: */
  541. union {
  542. sha2_byte* c;
  543. sha2_word64* l;
  544. } bitcount;
  545. bitcount.c = &context->buffer[SHA256_SHORT_BLOCK_LENGTH];
  546. *(bitcount.l) = context->bitcount;
  547. /* Final transform: */
  548. SHA256_Transform(context, (sha2_word32*)context->buffer);
  549. #if BYTE_ORDER == LITTLE_ENDIAN
  550. {
  551. /* Convert TO host byte order */
  552. int j;
  553. for (j = 0; j < 8; j++) {
  554. REVERSE32(context->state[j],context->state[j]);
  555. *d++ = context->state[j];
  556. }
  557. }
  558. #else
  559. MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
  560. #endif
  561. }
  562. /* Clean up state data: */
  563. MEMSET_BZERO(context, sizeof(*context));
  564. usedspace = 0;
  565. }
  566. char *SHA256_End(SHA256_CTX* context, char buffer[]) {
  567. sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
  568. int i;
  569. /* Sanity check: */
  570. assert(context != (SHA256_CTX*)0);
  571. if (buffer != (char*)0) {
  572. SHA256_Final(digest, context);
  573. for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  574. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  575. *buffer++ = sha2_hex_digits[*d & 0x0f];
  576. d++;
  577. }
  578. *buffer = (char)0;
  579. } else {
  580. MEMSET_BZERO(context, sizeof(*context));
  581. }
  582. MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
  583. return buffer;
  584. }
  585. char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
  586. SHA256_CTX context;
  587. SHA256_Init(&context);
  588. SHA256_Update(&context, data, len);
  589. return SHA256_End(&context, digest);
  590. }
  591. /*** SHA-512: *********************************************************/
  592. void SHA512_Init(SHA512_CTX* context) {
  593. if (context == (SHA512_CTX*)0) {
  594. return;
  595. }
  596. MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
  597. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
  598. context->bitcount[0] = context->bitcount[1] = 0;
  599. }
  600. #ifdef SHA2_UNROLL_TRANSFORM
  601. /* Unrolled SHA-512 round macros: */
  602. #if BYTE_ORDER == LITTLE_ENDIAN
  603. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  604. REVERSE64(*data++, W512[j]); \
  605. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  606. K512[j] + W512[j]; \
  607. (d) += T1, \
  608. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
  609. j++
  610. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  611. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  612. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  613. K512[j] + (W512[j] = *data++); \
  614. (d) += T1; \
  615. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  616. j++
  617. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  618. #define ROUND512(a,b,c,d,e,f,g,h) \
  619. s0 = W512[(j+1)&0x0f]; \
  620. s0 = sigma0_512(s0); \
  621. s1 = W512[(j+14)&0x0f]; \
  622. s1 = sigma1_512(s1); \
  623. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  624. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  625. (d) += T1; \
  626. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  627. j++
  628. static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  629. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  630. sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
  631. int j;
  632. /* Initialize registers with the prev. intermediate value */
  633. a = context->state[0];
  634. b = context->state[1];
  635. c = context->state[2];
  636. d = context->state[3];
  637. e = context->state[4];
  638. f = context->state[5];
  639. g = context->state[6];
  640. h = context->state[7];
  641. j = 0;
  642. do {
  643. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  644. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  645. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  646. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  647. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  648. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  649. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  650. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  651. } while (j < 16);
  652. /* Now for the remaining rounds up to 79: */
  653. do {
  654. ROUND512(a,b,c,d,e,f,g,h);
  655. ROUND512(h,a,b,c,d,e,f,g);
  656. ROUND512(g,h,a,b,c,d,e,f);
  657. ROUND512(f,g,h,a,b,c,d,e);
  658. ROUND512(e,f,g,h,a,b,c,d);
  659. ROUND512(d,e,f,g,h,a,b,c);
  660. ROUND512(c,d,e,f,g,h,a,b);
  661. ROUND512(b,c,d,e,f,g,h,a);
  662. } while (j < 80);
  663. /* Compute the current intermediate hash value */
  664. context->state[0] += a;
  665. context->state[1] += b;
  666. context->state[2] += c;
  667. context->state[3] += d;
  668. context->state[4] += e;
  669. context->state[5] += f;
  670. context->state[6] += g;
  671. context->state[7] += h;
  672. /* Clean up */
  673. a = b = c = d = e = f = g = h = T1 = 0;
  674. }
  675. #else /* SHA2_UNROLL_TRANSFORM */
  676. static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  677. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  678. sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
  679. int j;
  680. /* Initialize registers with the prev. intermediate value */
  681. a = context->state[0];
  682. b = context->state[1];
  683. c = context->state[2];
  684. d = context->state[3];
  685. e = context->state[4];
  686. f = context->state[5];
  687. g = context->state[6];
  688. h = context->state[7];
  689. j = 0;
  690. do {
  691. #if BYTE_ORDER == LITTLE_ENDIAN
  692. /* Convert TO host byte order */
  693. REVERSE64(*data++, W512[j]);
  694. /* Apply the SHA-512 compression function to update a..h */
  695. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  696. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  697. /* Apply the SHA-512 compression function to update a..h with copy */
  698. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  699. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  700. T2 = Sigma0_512(a) + Maj(a, b, c);
  701. h = g;
  702. g = f;
  703. f = e;
  704. e = d + T1;
  705. d = c;
  706. c = b;
  707. b = a;
  708. a = T1 + T2;
  709. j++;
  710. } while (j < 16);
  711. do {
  712. /* Part of the message block expansion: */
  713. s0 = W512[(j+1)&0x0f];
  714. s0 = sigma0_512(s0);
  715. s1 = W512[(j+14)&0x0f];
  716. s1 = sigma1_512(s1);
  717. /* Apply the SHA-512 compression function to update a..h */
  718. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  719. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  720. T2 = Sigma0_512(a) + Maj(a, b, c);
  721. h = g;
  722. g = f;
  723. f = e;
  724. e = d + T1;
  725. d = c;
  726. c = b;
  727. b = a;
  728. a = T1 + T2;
  729. j++;
  730. } while (j < 80);
  731. /* Compute the current intermediate hash value */
  732. context->state[0] += a;
  733. context->state[1] += b;
  734. context->state[2] += c;
  735. context->state[3] += d;
  736. context->state[4] += e;
  737. context->state[5] += f;
  738. context->state[6] += g;
  739. context->state[7] += h;
  740. /* Clean up */
  741. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  742. }
  743. #endif /* SHA2_UNROLL_TRANSFORM */
  744. void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
  745. unsigned int freespace, usedspace;
  746. if (len == 0) {
  747. /* Calling with no data is valid - we do nothing */
  748. return;
  749. }
  750. /* Sanity check: */
  751. assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
  752. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  753. if (usedspace > 0) {
  754. /* Calculate how much free space is available in the buffer */
  755. freespace = SHA512_BLOCK_LENGTH - usedspace;
  756. if (len >= freespace) {
  757. /* Fill the buffer completely and process it */
  758. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  759. ADDINC128(context->bitcount, freespace << 3);
  760. len -= freespace;
  761. data += freespace;
  762. SHA512_Transform(context, (sha2_word64*)context->buffer);
  763. } else {
  764. /* The buffer is not yet full */
  765. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  766. ADDINC128(context->bitcount, len << 3);
  767. /* Clean up: */
  768. usedspace = freespace = 0;
  769. return;
  770. }
  771. }
  772. while (len >= SHA512_BLOCK_LENGTH) {
  773. /* Process as many complete blocks as we can */
  774. sha2_byte buffer[SHA512_BLOCK_LENGTH];
  775. MEMCPY_BCOPY(buffer, data, SHA512_BLOCK_LENGTH);
  776. SHA512_Transform(context, (sha2_word64*)buffer);
  777. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  778. len -= SHA512_BLOCK_LENGTH;
  779. data += SHA512_BLOCK_LENGTH;
  780. }
  781. if (len > 0) {
  782. /* There's left-overs, so save 'em */
  783. MEMCPY_BCOPY(context->buffer, data, len);
  784. ADDINC128(context->bitcount, len << 3);
  785. }
  786. /* Clean up: */
  787. usedspace = freespace = 0;
  788. }
  789. static void SHA512_Last(SHA512_CTX* context) {
  790. unsigned int usedspace;
  791. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  792. #if BYTE_ORDER == LITTLE_ENDIAN
  793. /* Convert FROM host byte order */
  794. REVERSE64(context->bitcount[0],context->bitcount[0]);
  795. REVERSE64(context->bitcount[1],context->bitcount[1]);
  796. #endif
  797. if (usedspace > 0) {
  798. /* Begin padding with a 1 bit: */
  799. context->buffer[usedspace++] = 0x80;
  800. if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
  801. /* Set-up for the last transform: */
  802. MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
  803. } else {
  804. if (usedspace < SHA512_BLOCK_LENGTH) {
  805. MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
  806. }
  807. /* Do second-to-last transform: */
  808. SHA512_Transform(context, (sha2_word64*)context->buffer);
  809. /* And set-up for the last transform: */
  810. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
  811. }
  812. } else {
  813. /* Prepare for final transform: */
  814. MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
  815. /* Begin padding with a 1 bit: */
  816. *context->buffer = 0x80;
  817. }
  818. /* Store the length of input data (in bits): */
  819. union {
  820. sha2_byte* c;
  821. sha2_word64* l;
  822. } bitcount;
  823. bitcount.c = &context->buffer[SHA512_SHORT_BLOCK_LENGTH];
  824. bitcount.l[0] = context->bitcount[1];
  825. bitcount.l[1] = context->bitcount[0];
  826. /* Final transform: */
  827. SHA512_Transform(context, (sha2_word64*)context->buffer);
  828. }
  829. void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
  830. sha2_word64 *d = (sha2_word64*)digest;
  831. /* Sanity check: */
  832. assert(context != (SHA512_CTX*)0);
  833. /* If no digest buffer is passed, we don't bother doing this: */
  834. if (digest != (sha2_byte*)0) {
  835. SHA512_Last(context);
  836. /* Save the hash data for output: */
  837. #if BYTE_ORDER == LITTLE_ENDIAN
  838. {
  839. /* Convert TO host byte order */
  840. int j;
  841. for (j = 0; j < 8; j++) {
  842. REVERSE64(context->state[j],context->state[j]);
  843. *d++ = context->state[j];
  844. }
  845. }
  846. #else
  847. MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
  848. #endif
  849. }
  850. /* Zero out state data */
  851. MEMSET_BZERO(context, sizeof(*context));
  852. }
  853. char *SHA512_End(SHA512_CTX* context, char buffer[]) {
  854. sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
  855. int i;
  856. /* Sanity check: */
  857. assert(context != (SHA512_CTX*)0);
  858. if (buffer != (char*)0) {
  859. SHA512_Final(digest, context);
  860. for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  861. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  862. *buffer++ = sha2_hex_digits[*d & 0x0f];
  863. d++;
  864. }
  865. *buffer = (char)0;
  866. } else {
  867. MEMSET_BZERO(context, sizeof(*context));
  868. }
  869. MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
  870. return buffer;
  871. }
  872. char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
  873. SHA512_CTX context;
  874. SHA512_Init(&context);
  875. SHA512_Update(&context, data, len);
  876. return SHA512_End(&context, digest);
  877. }
  878. /*** SHA-384: *********************************************************/
  879. void SHA384_Init(SHA384_CTX* context) {
  880. if (context == (SHA384_CTX*)0) {
  881. return;
  882. }
  883. MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
  884. MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
  885. context->bitcount[0] = context->bitcount[1] = 0;
  886. }
  887. void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
  888. SHA512_Update((SHA512_CTX*)context, data, len);
  889. }
  890. void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
  891. sha2_word64 *d = (sha2_word64*)digest;
  892. /* Sanity check: */
  893. assert(context != (SHA384_CTX*)0);
  894. /* If no digest buffer is passed, we don't bother doing this: */
  895. if (digest != (sha2_byte*)0) {
  896. SHA512_Last((SHA512_CTX*)context);
  897. /* Save the hash data for output: */
  898. #if BYTE_ORDER == LITTLE_ENDIAN
  899. {
  900. /* Convert TO host byte order */
  901. int j;
  902. for (j = 0; j < 6; j++) {
  903. REVERSE64(context->state[j],context->state[j]);
  904. *d++ = context->state[j];
  905. }
  906. }
  907. #else
  908. MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
  909. #endif
  910. }
  911. /* Zero out state data */
  912. MEMSET_BZERO(context, sizeof(*context));
  913. }
  914. char *SHA384_End(SHA384_CTX* context, char buffer[]) {
  915. sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
  916. int i;
  917. /* Sanity check: */
  918. assert(context != (SHA384_CTX*)0);
  919. if (buffer != (char*)0) {
  920. SHA384_Final(digest, context);
  921. for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
  922. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  923. *buffer++ = sha2_hex_digits[*d & 0x0f];
  924. d++;
  925. }
  926. *buffer = (char)0;
  927. } else {
  928. MEMSET_BZERO(context, sizeof(*context));
  929. }
  930. MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
  931. return buffer;
  932. }
  933. char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
  934. SHA384_CTX context;
  935. SHA384_Init(&context);
  936. SHA384_Update(&context, data, len);
  937. return SHA384_End(&context, digest);
  938. }