sha2.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * FILE: sha2.c
  18. * AUTHOR: Aaron D. Gifford <me@aarongifford.com>
  19. *
  20. * A licence was granted to the ASF by Aaron on 4 November 2003.
  21. */
  22. #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
  23. #include <assert.h> /* assert() */
  24. #include "sha2.h"
  25. /*
  26. * ASSERT NOTE:
  27. * Some sanity checking code is included using assert(). On my FreeBSD
  28. * system, this additional code can be removed by compiling with NDEBUG
  29. * defined. Check your own systems manpage on assert() to see how to
  30. * compile WITHOUT the sanity checking code on your system.
  31. *
  32. * UNROLLED TRANSFORM LOOP NOTE:
  33. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  34. * loop version for the hash transform rounds (defined using macros
  35. * later in this file). Either define on the command line, for example:
  36. *
  37. * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  38. *
  39. * or define below:
  40. *
  41. * #define SHA2_UNROLL_TRANSFORM
  42. *
  43. */
  44. /*** SHA-256/384/512 Machine Architecture Definitions *****************/
  45. typedef fspr_byte_t sha2_byte; /* Exactly 1 byte */
  46. typedef fspr_uint32_t sha2_word32; /* Exactly 4 bytes */
  47. typedef fspr_uint64_t sha2_word64; /* Exactly 8 bytes */
  48. /*** SHA-256/384/512 Various Length Definitions ***********************/
  49. /* NOTE: Most of these are in sha2.h */
  50. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  51. #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
  52. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  53. /*** ENDIAN REVERSAL MACROS *******************************************/
  54. #if !APR_IS_BIGENDIAN
  55. #define REVERSE32(w,x) { \
  56. sha2_word32 tmp = (w); \
  57. tmp = (tmp >> 16) | (tmp << 16); \
  58. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  59. }
  60. #define REVERSE64(w,x) { \
  61. sha2_word64 tmp = (w); \
  62. tmp = (tmp >> 32) | (tmp << 32); \
  63. tmp = ((tmp & APR_UINT64_C(0xff00ff00ff00ff00)) >> 8) | \
  64. ((tmp & APR_UINT64_C(0x00ff00ff00ff00ff)) << 8); \
  65. (x) = ((tmp & APR_UINT64_C(0xffff0000ffff0000)) >> 16) | \
  66. ((tmp & APR_UINT64_C(0x0000ffff0000ffff)) << 16); \
  67. }
  68. #endif /* !APR_IS_BIGENDIAN */
  69. /*
  70. * Macro for incrementally adding the unsigned 64-bit integer n to the
  71. * unsigned 128-bit integer (represented using a two-element array of
  72. * 64-bit words):
  73. */
  74. #define ADDINC128(w,n) { \
  75. (w)[0] += (sha2_word64)(n); \
  76. if ((w)[0] < (n)) { \
  77. (w)[1]++; \
  78. } \
  79. }
  80. /*
  81. * Macros for copying blocks of memory and for zeroing out ranges
  82. * of memory. Using these macros makes it easy to switch from
  83. * using memset()/memcpy() and using bzero()/bcopy().
  84. *
  85. * Please define either SHA2_USE_MEMSET_MEMCPY or define
  86. * SHA2_USE_BZERO_BCOPY depending on which function set you
  87. * choose to use:
  88. */
  89. #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
  90. /* Default to memset()/memcpy() if no option is specified */
  91. #define SHA2_USE_MEMSET_MEMCPY 1
  92. #endif
  93. #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
  94. /* Abort with an error if BOTH options are defined */
  95. #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
  96. #endif
  97. #ifdef SHA2_USE_MEMSET_MEMCPY
  98. #define MEMSET_BZERO(p,l) memset((p), 0, (l))
  99. #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
  100. #endif
  101. #ifdef SHA2_USE_BZERO_BCOPY
  102. #define MEMSET_BZERO(p,l) bzero((p), (l))
  103. #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
  104. #endif
  105. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  106. /*
  107. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  108. *
  109. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  110. * S is a ROTATION) because the SHA-256/384/512 description document
  111. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  112. * same "backwards" definition.
  113. */
  114. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  115. #define R(b,x) ((x) >> (b))
  116. /* 32-bit Rotate-right (used in SHA-256): */
  117. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  118. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  119. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  120. /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
  121. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  122. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  123. /* Four of six logical functions used in SHA-256: */
  124. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  125. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  126. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  127. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  128. /* Four of six logical functions used in SHA-384 and SHA-512: */
  129. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  130. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  131. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  132. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
  133. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  134. /* NOTE: These should not be accessed directly from outside this
  135. * library -- they are intended for private internal visibility/use
  136. * only.
  137. */
  138. void fspr__SHA512_Last(SHA512_CTX*);
  139. void fspr__SHA256_Transform(SHA256_CTX*, const sha2_word32*);
  140. void fspr__SHA512_Transform(SHA512_CTX*, const sha2_word64*);
  141. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  142. /* Hash constant words K for SHA-256: */
  143. const static sha2_word32 K256[64] = {
  144. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  145. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  146. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  147. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  148. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  149. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  150. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  151. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  152. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  153. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  154. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  155. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  156. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  157. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  158. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  159. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  160. };
  161. /* Initial hash value H for SHA-256: */
  162. const static sha2_word32 sha256_initial_hash_value[8] = {
  163. 0x6a09e667UL,
  164. 0xbb67ae85UL,
  165. 0x3c6ef372UL,
  166. 0xa54ff53aUL,
  167. 0x510e527fUL,
  168. 0x9b05688cUL,
  169. 0x1f83d9abUL,
  170. 0x5be0cd19UL
  171. };
  172. /* Hash constant words K for SHA-384 and SHA-512: */
  173. const static sha2_word64 K512[80] = {
  174. APR_UINT64_C(0x428a2f98d728ae22), APR_UINT64_C(0x7137449123ef65cd),
  175. APR_UINT64_C(0xb5c0fbcfec4d3b2f), APR_UINT64_C(0xe9b5dba58189dbbc),
  176. APR_UINT64_C(0x3956c25bf348b538), APR_UINT64_C(0x59f111f1b605d019),
  177. APR_UINT64_C(0x923f82a4af194f9b), APR_UINT64_C(0xab1c5ed5da6d8118),
  178. APR_UINT64_C(0xd807aa98a3030242), APR_UINT64_C(0x12835b0145706fbe),
  179. APR_UINT64_C(0x243185be4ee4b28c), APR_UINT64_C(0x550c7dc3d5ffb4e2),
  180. APR_UINT64_C(0x72be5d74f27b896f), APR_UINT64_C(0x80deb1fe3b1696b1),
  181. APR_UINT64_C(0x9bdc06a725c71235), APR_UINT64_C(0xc19bf174cf692694),
  182. APR_UINT64_C(0xe49b69c19ef14ad2), APR_UINT64_C(0xefbe4786384f25e3),
  183. APR_UINT64_C(0x0fc19dc68b8cd5b5), APR_UINT64_C(0x240ca1cc77ac9c65),
  184. APR_UINT64_C(0x2de92c6f592b0275), APR_UINT64_C(0x4a7484aa6ea6e483),
  185. APR_UINT64_C(0x5cb0a9dcbd41fbd4), APR_UINT64_C(0x76f988da831153b5),
  186. APR_UINT64_C(0x983e5152ee66dfab), APR_UINT64_C(0xa831c66d2db43210),
  187. APR_UINT64_C(0xb00327c898fb213f), APR_UINT64_C(0xbf597fc7beef0ee4),
  188. APR_UINT64_C(0xc6e00bf33da88fc2), APR_UINT64_C(0xd5a79147930aa725),
  189. APR_UINT64_C(0x06ca6351e003826f), APR_UINT64_C(0x142929670a0e6e70),
  190. APR_UINT64_C(0x27b70a8546d22ffc), APR_UINT64_C(0x2e1b21385c26c926),
  191. APR_UINT64_C(0x4d2c6dfc5ac42aed), APR_UINT64_C(0x53380d139d95b3df),
  192. APR_UINT64_C(0x650a73548baf63de), APR_UINT64_C(0x766a0abb3c77b2a8),
  193. APR_UINT64_C(0x81c2c92e47edaee6), APR_UINT64_C(0x92722c851482353b),
  194. APR_UINT64_C(0xa2bfe8a14cf10364), APR_UINT64_C(0xa81a664bbc423001),
  195. APR_UINT64_C(0xc24b8b70d0f89791), APR_UINT64_C(0xc76c51a30654be30),
  196. APR_UINT64_C(0xd192e819d6ef5218), APR_UINT64_C(0xd69906245565a910),
  197. APR_UINT64_C(0xf40e35855771202a), APR_UINT64_C(0x106aa07032bbd1b8),
  198. APR_UINT64_C(0x19a4c116b8d2d0c8), APR_UINT64_C(0x1e376c085141ab53),
  199. APR_UINT64_C(0x2748774cdf8eeb99), APR_UINT64_C(0x34b0bcb5e19b48a8),
  200. APR_UINT64_C(0x391c0cb3c5c95a63), APR_UINT64_C(0x4ed8aa4ae3418acb),
  201. APR_UINT64_C(0x5b9cca4f7763e373), APR_UINT64_C(0x682e6ff3d6b2b8a3),
  202. APR_UINT64_C(0x748f82ee5defb2fc), APR_UINT64_C(0x78a5636f43172f60),
  203. APR_UINT64_C(0x84c87814a1f0ab72), APR_UINT64_C(0x8cc702081a6439ec),
  204. APR_UINT64_C(0x90befffa23631e28), APR_UINT64_C(0xa4506cebde82bde9),
  205. APR_UINT64_C(0xbef9a3f7b2c67915), APR_UINT64_C(0xc67178f2e372532b),
  206. APR_UINT64_C(0xca273eceea26619c), APR_UINT64_C(0xd186b8c721c0c207),
  207. APR_UINT64_C(0xeada7dd6cde0eb1e), APR_UINT64_C(0xf57d4f7fee6ed178),
  208. APR_UINT64_C(0x06f067aa72176fba), APR_UINT64_C(0x0a637dc5a2c898a6),
  209. APR_UINT64_C(0x113f9804bef90dae), APR_UINT64_C(0x1b710b35131c471b),
  210. APR_UINT64_C(0x28db77f523047d84), APR_UINT64_C(0x32caab7b40c72493),
  211. APR_UINT64_C(0x3c9ebe0a15c9bebc), APR_UINT64_C(0x431d67c49c100d4c),
  212. APR_UINT64_C(0x4cc5d4becb3e42b6), APR_UINT64_C(0x597f299cfc657e2a),
  213. APR_UINT64_C(0x5fcb6fab3ad6faec), APR_UINT64_C(0x6c44198c4a475817)
  214. };
  215. /* Initial hash value H for SHA-384 */
  216. const static sha2_word64 sha384_initial_hash_value[8] = {
  217. APR_UINT64_C(0xcbbb9d5dc1059ed8),
  218. APR_UINT64_C(0x629a292a367cd507),
  219. APR_UINT64_C(0x9159015a3070dd17),
  220. APR_UINT64_C(0x152fecd8f70e5939),
  221. APR_UINT64_C(0x67332667ffc00b31),
  222. APR_UINT64_C(0x8eb44a8768581511),
  223. APR_UINT64_C(0xdb0c2e0d64f98fa7),
  224. APR_UINT64_C(0x47b5481dbefa4fa4)
  225. };
  226. /* Initial hash value H for SHA-512 */
  227. const static sha2_word64 sha512_initial_hash_value[8] = {
  228. APR_UINT64_C(0x6a09e667f3bcc908),
  229. APR_UINT64_C(0xbb67ae8584caa73b),
  230. APR_UINT64_C(0x3c6ef372fe94f82b),
  231. APR_UINT64_C(0xa54ff53a5f1d36f1),
  232. APR_UINT64_C(0x510e527fade682d1),
  233. APR_UINT64_C(0x9b05688c2b3e6c1f),
  234. APR_UINT64_C(0x1f83d9abfb41bd6b),
  235. APR_UINT64_C(0x5be0cd19137e2179)
  236. };
  237. /*
  238. * Constant used by SHA256/384/512_End() functions for converting the
  239. * digest to a readable hexadecimal character string:
  240. */
  241. static const char *sha2_hex_digits = "0123456789abcdef";
  242. /*** SHA-256: *********************************************************/
  243. void fspr__SHA256_Init(SHA256_CTX* context) {
  244. if (context == (SHA256_CTX*)0) {
  245. return;
  246. }
  247. MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
  248. MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
  249. context->bitcount = 0;
  250. }
  251. #ifdef SHA2_UNROLL_TRANSFORM
  252. /* Unrolled SHA-256 round macros: */
  253. #if !APR_IS_BIGENDIAN
  254. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  255. REVERSE32(*data++, W256[j]); \
  256. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  257. K256[j] + W256[j]; \
  258. (d) += T1; \
  259. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  260. j++
  261. #else /* APR_IS_BIGENDIAN */
  262. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  263. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  264. K256[j] + (W256[j] = *data++); \
  265. (d) += T1; \
  266. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  267. j++
  268. #endif /* APR_IS_BIGENDIAN */
  269. #define ROUND256(a,b,c,d,e,f,g,h) \
  270. s0 = W256[(j+1)&0x0f]; \
  271. s0 = sigma0_256(s0); \
  272. s1 = W256[(j+14)&0x0f]; \
  273. s1 = sigma1_256(s1); \
  274. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
  275. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
  276. (d) += T1; \
  277. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  278. j++
  279. void fspr__SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  280. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  281. sha2_word32 T1, *W256;
  282. int j;
  283. W256 = (sha2_word32*)context->buffer;
  284. /* Initialize registers with the prev. intermediate value */
  285. a = context->state[0];
  286. b = context->state[1];
  287. c = context->state[2];
  288. d = context->state[3];
  289. e = context->state[4];
  290. f = context->state[5];
  291. g = context->state[6];
  292. h = context->state[7];
  293. j = 0;
  294. do {
  295. /* Rounds 0 to 15 (unrolled): */
  296. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  297. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  298. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  299. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  300. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  301. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  302. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  303. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  304. } while (j < 16);
  305. /* Now for the remaining rounds to 64: */
  306. do {
  307. ROUND256(a,b,c,d,e,f,g,h);
  308. ROUND256(h,a,b,c,d,e,f,g);
  309. ROUND256(g,h,a,b,c,d,e,f);
  310. ROUND256(f,g,h,a,b,c,d,e);
  311. ROUND256(e,f,g,h,a,b,c,d);
  312. ROUND256(d,e,f,g,h,a,b,c);
  313. ROUND256(c,d,e,f,g,h,a,b);
  314. ROUND256(b,c,d,e,f,g,h,a);
  315. } while (j < 64);
  316. /* Compute the current intermediate hash value */
  317. context->state[0] += a;
  318. context->state[1] += b;
  319. context->state[2] += c;
  320. context->state[3] += d;
  321. context->state[4] += e;
  322. context->state[5] += f;
  323. context->state[6] += g;
  324. context->state[7] += h;
  325. /* Clean up */
  326. a = b = c = d = e = f = g = h = T1 = 0;
  327. }
  328. #else /* SHA2_UNROLL_TRANSFORM */
  329. void fspr__SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
  330. sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
  331. sha2_word32 T1, T2, *W256;
  332. int j;
  333. W256 = (sha2_word32*)context->buffer;
  334. /* Initialize registers with the prev. intermediate value */
  335. a = context->state[0];
  336. b = context->state[1];
  337. c = context->state[2];
  338. d = context->state[3];
  339. e = context->state[4];
  340. f = context->state[5];
  341. g = context->state[6];
  342. h = context->state[7];
  343. j = 0;
  344. do {
  345. #if !APR_IS_BIGENDIAN
  346. /* Copy data while converting to host byte order */
  347. REVERSE32(*data++,W256[j]);
  348. /* Apply the SHA-256 compression function to update a..h */
  349. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
  350. #else /* APR_IS_BIGENDIAN */
  351. /* Apply the SHA-256 compression function to update a..h with copy */
  352. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
  353. #endif /* APR_IS_BIGENDIAN */
  354. T2 = Sigma0_256(a) + Maj(a, b, c);
  355. h = g;
  356. g = f;
  357. f = e;
  358. e = d + T1;
  359. d = c;
  360. c = b;
  361. b = a;
  362. a = T1 + T2;
  363. j++;
  364. } while (j < 16);
  365. do {
  366. /* Part of the message block expansion: */
  367. s0 = W256[(j+1)&0x0f];
  368. s0 = sigma0_256(s0);
  369. s1 = W256[(j+14)&0x0f];
  370. s1 = sigma1_256(s1);
  371. /* Apply the SHA-256 compression function to update a..h */
  372. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  373. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  374. T2 = Sigma0_256(a) + Maj(a, b, c);
  375. h = g;
  376. g = f;
  377. f = e;
  378. e = d + T1;
  379. d = c;
  380. c = b;
  381. b = a;
  382. a = T1 + T2;
  383. j++;
  384. } while (j < 64);
  385. /* Compute the current intermediate hash value */
  386. context->state[0] += a;
  387. context->state[1] += b;
  388. context->state[2] += c;
  389. context->state[3] += d;
  390. context->state[4] += e;
  391. context->state[5] += f;
  392. context->state[6] += g;
  393. context->state[7] += h;
  394. /* Clean up */
  395. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  396. assert(a==0);
  397. assert(b==0);
  398. assert(c==0);
  399. assert(d==0);
  400. assert(e==0);
  401. assert(f==0);
  402. assert(g==0);
  403. assert(h==0);
  404. assert(T1==0);
  405. assert(T2==0);
  406. }
  407. #endif /* SHA2_UNROLL_TRANSFORM */
  408. void fspr__SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
  409. unsigned int freespace, usedspace;
  410. if (len == 0) {
  411. /* Calling with no data is valid - we do nothing */
  412. return;
  413. }
  414. /* Sanity check: */
  415. assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
  416. usedspace = (unsigned int)((context->bitcount >> 3)
  417. % SHA256_BLOCK_LENGTH);
  418. if (usedspace > 0) {
  419. /* Calculate how much free space is available in the buffer */
  420. freespace = SHA256_BLOCK_LENGTH - usedspace;
  421. if (len >= freespace) {
  422. /* Fill the buffer completely and process it */
  423. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  424. context->bitcount += freespace << 3;
  425. len -= freespace;
  426. data += freespace;
  427. fspr__SHA256_Transform(context, (sha2_word32*)context->buffer);
  428. } else {
  429. /* The buffer is not yet full */
  430. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  431. context->bitcount += len << 3;
  432. /* Clean up: */
  433. usedspace = freespace = 0;
  434. assert(usedspace==0);
  435. assert(freespace==0);
  436. return;
  437. }
  438. }
  439. while (len >= SHA256_BLOCK_LENGTH) {
  440. /* Process as many complete blocks as we can */
  441. fspr__SHA256_Transform(context, (sha2_word32*)data);
  442. context->bitcount += SHA256_BLOCK_LENGTH << 3;
  443. len -= SHA256_BLOCK_LENGTH;
  444. data += SHA256_BLOCK_LENGTH;
  445. }
  446. if (len > 0) {
  447. /* There's left-overs, so save 'em */
  448. MEMCPY_BCOPY(context->buffer, data, len);
  449. context->bitcount += len << 3;
  450. }
  451. /* Clean up: */
  452. usedspace = freespace = 0;
  453. assert(usedspace==0);
  454. assert(freespace==0);
  455. }
  456. void fspr__SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
  457. sha2_word32 *d = (sha2_word32*)digest;
  458. unsigned int usedspace;
  459. /* Sanity check: */
  460. assert(context != (SHA256_CTX*)0);
  461. /* If no digest buffer is passed, we don't bother doing this: */
  462. if (digest != (sha2_byte*)0) {
  463. usedspace = (unsigned int)((context->bitcount >> 3)
  464. % SHA256_BLOCK_LENGTH);
  465. #if !APR_IS_BIGENDIAN
  466. /* Convert FROM host byte order */
  467. REVERSE64(context->bitcount,context->bitcount);
  468. #endif
  469. if (usedspace > 0) {
  470. /* Begin padding with a 1 bit: */
  471. context->buffer[usedspace++] = 0x80;
  472. if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
  473. /* Set-up for the last transform: */
  474. MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
  475. } else {
  476. if (usedspace < SHA256_BLOCK_LENGTH) {
  477. MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
  478. }
  479. /* Do second-to-last transform: */
  480. fspr__SHA256_Transform(context, (sha2_word32*)context->buffer);
  481. /* And set-up for the last transform: */
  482. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  483. }
  484. } else {
  485. /* Set-up for the last transform: */
  486. MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
  487. /* Begin padding with a 1 bit: */
  488. *context->buffer = 0x80;
  489. }
  490. /* Set the bit count: */
  491. *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
  492. /* Final transform: */
  493. fspr__SHA256_Transform(context, (sha2_word32*)context->buffer);
  494. #if !APR_IS_BIGENDIAN
  495. {
  496. /* Convert TO host byte order */
  497. int j;
  498. for (j = 0; j < 8; j++) {
  499. REVERSE32(context->state[j],context->state[j]);
  500. *d++ = context->state[j];
  501. }
  502. }
  503. #else
  504. MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
  505. #endif
  506. }
  507. /* Clean up state data: */
  508. MEMSET_BZERO(context, sizeof(*context));
  509. usedspace = 0;
  510. assert(usedspace==0);
  511. }
  512. char *fspr__SHA256_End(SHA256_CTX* context, char buffer[]) {
  513. sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
  514. int i;
  515. /* Sanity check: */
  516. assert(context != (SHA256_CTX*)0);
  517. if (buffer != (char*)0) {
  518. fspr__SHA256_Final(digest, context);
  519. for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  520. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  521. *buffer++ = sha2_hex_digits[*d & 0x0f];
  522. d++;
  523. }
  524. *buffer = (char)0;
  525. } else {
  526. MEMSET_BZERO(context, sizeof(*context));
  527. }
  528. MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
  529. return buffer;
  530. }
  531. char* fspr__SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
  532. SHA256_CTX context;
  533. fspr__SHA256_Init(&context);
  534. fspr__SHA256_Update(&context, data, len);
  535. return fspr__SHA256_End(&context, digest);
  536. }
  537. /*** SHA-512: *********************************************************/
  538. void fspr__SHA512_Init(SHA512_CTX* context) {
  539. if (context == (SHA512_CTX*)0) {
  540. return;
  541. }
  542. MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
  543. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
  544. context->bitcount[0] = context->bitcount[1] = 0;
  545. }
  546. #ifdef SHA2_UNROLL_TRANSFORM
  547. /* Unrolled SHA-512 round macros: */
  548. #if !APR_IS_BIGENDIAN
  549. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  550. REVERSE64(*data++, W512[j]); \
  551. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  552. K512[j] + W512[j]; \
  553. (d) += T1, \
  554. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
  555. j++
  556. #else /* APR_IS_BIGENDIAN */
  557. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  558. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  559. K512[j] + (W512[j] = *data++); \
  560. (d) += T1; \
  561. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  562. j++
  563. #endif /* APR_IS_BIGENDIAN */
  564. #define ROUND512(a,b,c,d,e,f,g,h) \
  565. s0 = W512[(j+1)&0x0f]; \
  566. s0 = sigma0_512(s0); \
  567. s1 = W512[(j+14)&0x0f]; \
  568. s1 = sigma1_512(s1); \
  569. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  570. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  571. (d) += T1; \
  572. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  573. j++
  574. void fspr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  575. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  576. sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
  577. int j;
  578. /* Initialize registers with the prev. intermediate value */
  579. a = context->state[0];
  580. b = context->state[1];
  581. c = context->state[2];
  582. d = context->state[3];
  583. e = context->state[4];
  584. f = context->state[5];
  585. g = context->state[6];
  586. h = context->state[7];
  587. j = 0;
  588. do {
  589. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  590. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  591. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  592. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  593. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  594. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  595. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  596. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  597. } while (j < 16);
  598. /* Now for the remaining rounds up to 79: */
  599. do {
  600. ROUND512(a,b,c,d,e,f,g,h);
  601. ROUND512(h,a,b,c,d,e,f,g);
  602. ROUND512(g,h,a,b,c,d,e,f);
  603. ROUND512(f,g,h,a,b,c,d,e);
  604. ROUND512(e,f,g,h,a,b,c,d);
  605. ROUND512(d,e,f,g,h,a,b,c);
  606. ROUND512(c,d,e,f,g,h,a,b);
  607. ROUND512(b,c,d,e,f,g,h,a);
  608. } while (j < 80);
  609. /* Compute the current intermediate hash value */
  610. context->state[0] += a;
  611. context->state[1] += b;
  612. context->state[2] += c;
  613. context->state[3] += d;
  614. context->state[4] += e;
  615. context->state[5] += f;
  616. context->state[6] += g;
  617. context->state[7] += h;
  618. /* Clean up */
  619. a = b = c = d = e = f = g = h = T1 = 0;
  620. }
  621. #else /* SHA2_UNROLL_TRANSFORM */
  622. void fspr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
  623. sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
  624. sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
  625. int j;
  626. /* Initialize registers with the prev. intermediate value */
  627. a = context->state[0];
  628. b = context->state[1];
  629. c = context->state[2];
  630. d = context->state[3];
  631. e = context->state[4];
  632. f = context->state[5];
  633. g = context->state[6];
  634. h = context->state[7];
  635. j = 0;
  636. do {
  637. #if !APR_IS_BIGENDIAN
  638. /* Convert TO host byte order */
  639. REVERSE64(*data++, W512[j]);
  640. /* Apply the SHA-512 compression function to update a..h */
  641. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  642. #else /* APR_IS_BIGENDIAN */
  643. /* Apply the SHA-512 compression function to update a..h with copy */
  644. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  645. #endif /* APR_IS_BIGENDIAN */
  646. T2 = Sigma0_512(a) + Maj(a, b, c);
  647. h = g;
  648. g = f;
  649. f = e;
  650. e = d + T1;
  651. d = c;
  652. c = b;
  653. b = a;
  654. a = T1 + T2;
  655. j++;
  656. } while (j < 16);
  657. do {
  658. /* Part of the message block expansion: */
  659. s0 = W512[(j+1)&0x0f];
  660. s0 = sigma0_512(s0);
  661. s1 = W512[(j+14)&0x0f];
  662. s1 = sigma1_512(s1);
  663. /* Apply the SHA-512 compression function to update a..h */
  664. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  665. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  666. T2 = Sigma0_512(a) + Maj(a, b, c);
  667. h = g;
  668. g = f;
  669. f = e;
  670. e = d + T1;
  671. d = c;
  672. c = b;
  673. b = a;
  674. a = T1 + T2;
  675. j++;
  676. } while (j < 80);
  677. /* Compute the current intermediate hash value */
  678. context->state[0] += a;
  679. context->state[1] += b;
  680. context->state[2] += c;
  681. context->state[3] += d;
  682. context->state[4] += e;
  683. context->state[5] += f;
  684. context->state[6] += g;
  685. context->state[7] += h;
  686. /* Clean up */
  687. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  688. assert(a==0);
  689. assert(b==0);
  690. assert(c==0);
  691. assert(d==0);
  692. assert(e==0);
  693. assert(f==0);
  694. assert(g==0);
  695. assert(h==0);
  696. assert(T1==0);
  697. assert(T2==0);
  698. }
  699. #endif /* SHA2_UNROLL_TRANSFORM */
  700. void fspr__SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
  701. unsigned int freespace, usedspace;
  702. if (len == 0) {
  703. /* Calling with no data is valid - we do nothing */
  704. return;
  705. }
  706. /* Sanity check: */
  707. assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
  708. usedspace = (unsigned int)((context->bitcount[0] >> 3)
  709. % SHA512_BLOCK_LENGTH);
  710. if (usedspace > 0) {
  711. /* Calculate how much free space is available in the buffer */
  712. freespace = SHA512_BLOCK_LENGTH - usedspace;
  713. if (len >= freespace) {
  714. /* Fill the buffer completely and process it */
  715. MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
  716. ADDINC128(context->bitcount, freespace << 3);
  717. len -= freespace;
  718. data += freespace;
  719. fspr__SHA512_Transform(context, (sha2_word64*)context->buffer);
  720. } else {
  721. /* The buffer is not yet full */
  722. MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
  723. ADDINC128(context->bitcount, len << 3);
  724. /* Clean up: */
  725. usedspace = freespace = 0;
  726. assert(usedspace==0);
  727. assert(freespace==0);
  728. return;
  729. }
  730. }
  731. while (len >= SHA512_BLOCK_LENGTH) {
  732. /* Process as many complete blocks as we can */
  733. fspr__SHA512_Transform(context, (sha2_word64*)data);
  734. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  735. len -= SHA512_BLOCK_LENGTH;
  736. data += SHA512_BLOCK_LENGTH;
  737. }
  738. if (len > 0) {
  739. /* There's left-overs, so save 'em */
  740. MEMCPY_BCOPY(context->buffer, data, len);
  741. ADDINC128(context->bitcount, len << 3);
  742. }
  743. /* Clean up: */
  744. usedspace = freespace = 0;
  745. assert(usedspace==0);
  746. assert(freespace==0);
  747. }
  748. void fspr__SHA512_Last(SHA512_CTX* context) {
  749. unsigned int usedspace;
  750. usedspace = (unsigned int)((context->bitcount[0] >> 3)
  751. % SHA512_BLOCK_LENGTH);
  752. #if !APR_IS_BIGENDIAN
  753. /* Convert FROM host byte order */
  754. REVERSE64(context->bitcount[0],context->bitcount[0]);
  755. REVERSE64(context->bitcount[1],context->bitcount[1]);
  756. #endif
  757. if (usedspace > 0) {
  758. /* Begin padding with a 1 bit: */
  759. context->buffer[usedspace++] = 0x80;
  760. if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
  761. /* Set-up for the last transform: */
  762. MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
  763. } else {
  764. if (usedspace < SHA512_BLOCK_LENGTH) {
  765. MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
  766. }
  767. /* Do second-to-last transform: */
  768. fspr__SHA512_Transform(context, (sha2_word64*)context->buffer);
  769. /* And set-up for the last transform: */
  770. MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
  771. }
  772. } else {
  773. /* Prepare for final transform: */
  774. MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
  775. /* Begin padding with a 1 bit: */
  776. *context->buffer = 0x80;
  777. }
  778. /* Store the length of input data (in bits): */
  779. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
  780. *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
  781. /* Final transform: */
  782. fspr__SHA512_Transform(context, (sha2_word64*)context->buffer);
  783. }
  784. void fspr__SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
  785. sha2_word64 *d = (sha2_word64*)digest;
  786. /* Sanity check: */
  787. assert(context != (SHA512_CTX*)0);
  788. /* If no digest buffer is passed, we don't bother doing this: */
  789. if (digest != (sha2_byte*)0) {
  790. fspr__SHA512_Last(context);
  791. /* Save the hash data for output: */
  792. #if !APR_IS_BIGENDIAN
  793. {
  794. /* Convert TO host byte order */
  795. int j;
  796. for (j = 0; j < 8; j++) {
  797. REVERSE64(context->state[j],context->state[j]);
  798. *d++ = context->state[j];
  799. }
  800. }
  801. #else /* APR_IS_BIGENDIAN */
  802. MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
  803. #endif /* APR_IS_BIGENDIAN */
  804. }
  805. /* Zero out state data */
  806. MEMSET_BZERO(context, sizeof(*context));
  807. }
  808. char *fspr__SHA512_End(SHA512_CTX* context, char buffer[]) {
  809. sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
  810. int i;
  811. /* Sanity check: */
  812. assert(context != (SHA512_CTX*)0);
  813. if (buffer != (char*)0) {
  814. fspr__SHA512_Final(digest, context);
  815. for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  816. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  817. *buffer++ = sha2_hex_digits[*d & 0x0f];
  818. d++;
  819. }
  820. *buffer = (char)0;
  821. } else {
  822. MEMSET_BZERO(context, sizeof(*context));
  823. }
  824. MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
  825. return buffer;
  826. }
  827. char* fspr__SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
  828. SHA512_CTX context;
  829. fspr__SHA512_Init(&context);
  830. fspr__SHA512_Update(&context, data, len);
  831. return fspr__SHA512_End(&context, digest);
  832. }
  833. /*** SHA-384: *********************************************************/
  834. void fspr__SHA384_Init(SHA384_CTX* context) {
  835. if (context == (SHA384_CTX*)0) {
  836. return;
  837. }
  838. MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
  839. MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
  840. context->bitcount[0] = context->bitcount[1] = 0;
  841. }
  842. void fspr__SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
  843. fspr__SHA512_Update((SHA512_CTX*)context, data, len);
  844. }
  845. void fspr__SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
  846. sha2_word64 *d = (sha2_word64*)digest;
  847. /* Sanity check: */
  848. assert(context != (SHA384_CTX*)0);
  849. /* If no digest buffer is passed, we don't bother doing this: */
  850. if (digest != (sha2_byte*)0) {
  851. fspr__SHA512_Last((SHA512_CTX*)context);
  852. /* Save the hash data for output: */
  853. #if !APR_IS_BIGENDIAN
  854. {
  855. /* Convert TO host byte order */
  856. int j;
  857. for (j = 0; j < 6; j++) {
  858. REVERSE64(context->state[j],context->state[j]);
  859. *d++ = context->state[j];
  860. }
  861. }
  862. #else /* APR_IS_BIGENDIAN */
  863. MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
  864. #endif /* APR_IS_BIGENDIAN */
  865. }
  866. /* Zero out state data */
  867. MEMSET_BZERO(context, sizeof(*context));
  868. }
  869. char *fspr__SHA384_End(SHA384_CTX* context, char buffer[]) {
  870. sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
  871. int i;
  872. /* Sanity check: */
  873. assert(context != (SHA384_CTX*)0);
  874. if (buffer != (char*)0) {
  875. fspr__SHA384_Final(digest, context);
  876. for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
  877. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  878. *buffer++ = sha2_hex_digits[*d & 0x0f];
  879. d++;
  880. }
  881. *buffer = (char)0;
  882. } else {
  883. MEMSET_BZERO(context, sizeof(*context));
  884. }
  885. MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
  886. return buffer;
  887. }
  888. char* fspr__SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
  889. SHA384_CTX context;
  890. fspr__SHA384_Init(&context);
  891. fspr__SHA384_Update(&context, data, len);
  892. return fspr__SHA384_End(&context, digest);
  893. }