sha2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* $OpenBSD: sha2.c,v 1.18 2015/03/14 03:38:46 jsg Exp $ */
  2. /*
  3. * FILE: sha2.c
  4. * AUTHOR: Aaron D. Gifford <me@aarongifford.com>
  5. *
  6. * Copyright (c) 2000-2001, Aaron D. Gifford
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
  34. */
  35. #include <sys/time.h>
  36. #include <sys/systm.h>
  37. #include <crypto/sha2.h>
  38. /*
  39. * UNROLLED TRANSFORM LOOP NOTE:
  40. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  41. * loop version for the hash transform rounds (defined using macros
  42. * later in this file). Either define on the command line, for example:
  43. *
  44. * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  45. *
  46. * or define below:
  47. *
  48. * #define SHA2_UNROLL_TRANSFORM
  49. *
  50. */
  51. #ifndef SMALL_KERNEL
  52. #if defined(__amd64__) || defined(__i386__)
  53. #define SHA2_UNROLL_TRANSFORM
  54. #endif
  55. #endif
  56. /*** SHA-256/384/512 Machine Architecture Definitions *****************/
  57. /*
  58. * BYTE_ORDER NOTE:
  59. *
  60. * Please make sure that your system defines BYTE_ORDER. If your
  61. * architecture is little-endian, make sure it also defines
  62. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  63. * equivilent.
  64. *
  65. * If your system does not define the above, then you can do so by
  66. * hand like this:
  67. *
  68. * #define LITTLE_ENDIAN 1234
  69. * #define BIG_ENDIAN 4321
  70. *
  71. * And for little-endian machines, add:
  72. *
  73. * #define BYTE_ORDER LITTLE_ENDIAN
  74. *
  75. * Or for big-endian machines:
  76. *
  77. * #define BYTE_ORDER BIG_ENDIAN
  78. *
  79. * The FreeBSD machine this was written on defines BYTE_ORDER
  80. * appropriately by including <sys/types.h> (which in turn includes
  81. * <machine/endian.h> where the appropriate definitions are actually
  82. * made).
  83. */
  84. #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
  85. #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
  86. #endif
  87. /*** SHA-256/384/512 Various Length Definitions ***********************/
  88. /* NOTE: Most of these are in sha2.h */
  89. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  90. #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
  91. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  92. /*
  93. * Macro for incrementally adding the unsigned 64-bit integer n to the
  94. * unsigned 128-bit integer (represented using a two-element array of
  95. * 64-bit words):
  96. */
  97. #define ADDINC128(w,n) { \
  98. (w)[0] += (u_int64_t)(n); \
  99. if ((w)[0] < (n)) { \
  100. (w)[1]++; \
  101. } \
  102. }
  103. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  104. /*
  105. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  106. *
  107. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  108. * S is a ROTATION) because the SHA-256/384/512 description document
  109. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  110. * same "backwards" definition.
  111. */
  112. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  113. #define R(b,x) ((x) >> (b))
  114. /* 32-bit Rotate-right (used in SHA-256): */
  115. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  116. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  117. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  118. /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
  119. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  120. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  121. /* Four of six logical functions used in SHA-256: */
  122. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  123. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  124. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  125. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  126. /* Four of six logical functions used in SHA-384 and SHA-512: */
  127. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  128. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  129. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  130. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
  131. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  132. /* NOTE: These should not be accessed directly from outside this
  133. * library -- they are intended for private internal visibility/use
  134. * only.
  135. */
  136. void SHA512Last(SHA2_CTX *);
  137. void SHA256Transform(u_int32_t *, const u_int8_t *);
  138. void SHA512Transform(u_int64_t *, const u_int8_t *);
  139. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  140. /* Hash constant words K for SHA-256: */
  141. const static u_int32_t K256[64] = {
  142. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  143. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  144. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  145. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  146. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  147. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  148. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  149. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  150. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  151. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  152. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  153. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  154. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  155. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  156. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  157. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  158. };
  159. /* Initial hash value H for SHA-256: */
  160. const static u_int32_t sha256_initial_hash_value[8] = {
  161. 0x6a09e667UL,
  162. 0xbb67ae85UL,
  163. 0x3c6ef372UL,
  164. 0xa54ff53aUL,
  165. 0x510e527fUL,
  166. 0x9b05688cUL,
  167. 0x1f83d9abUL,
  168. 0x5be0cd19UL
  169. };
  170. /* Hash constant words K for SHA-384 and SHA-512: */
  171. const static u_int64_t K512[80] = {
  172. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  173. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  174. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  175. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  176. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  177. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  178. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  179. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  180. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  181. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  182. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  183. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  184. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  185. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  186. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  187. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  188. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  189. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  190. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  191. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  192. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  193. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  194. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  195. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  196. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  197. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  198. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  199. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  200. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  201. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  202. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  203. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  204. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  205. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  206. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  207. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  208. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  209. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  210. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  211. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  212. };
  213. /* Initial hash value H for SHA-384 */
  214. const static u_int64_t sha384_initial_hash_value[8] = {
  215. 0xcbbb9d5dc1059ed8ULL,
  216. 0x629a292a367cd507ULL,
  217. 0x9159015a3070dd17ULL,
  218. 0x152fecd8f70e5939ULL,
  219. 0x67332667ffc00b31ULL,
  220. 0x8eb44a8768581511ULL,
  221. 0xdb0c2e0d64f98fa7ULL,
  222. 0x47b5481dbefa4fa4ULL
  223. };
  224. /* Initial hash value H for SHA-512 */
  225. const static u_int64_t sha512_initial_hash_value[8] = {
  226. 0x6a09e667f3bcc908ULL,
  227. 0xbb67ae8584caa73bULL,
  228. 0x3c6ef372fe94f82bULL,
  229. 0xa54ff53a5f1d36f1ULL,
  230. 0x510e527fade682d1ULL,
  231. 0x9b05688c2b3e6c1fULL,
  232. 0x1f83d9abfb41bd6bULL,
  233. 0x5be0cd19137e2179ULL
  234. };
  235. /*** SHA-256: *********************************************************/
  236. void
  237. SHA256Init(SHA2_CTX *context)
  238. {
  239. memcpy(context->state.st32, sha256_initial_hash_value,
  240. SHA256_DIGEST_LENGTH);
  241. memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
  242. context->bitcount[0] = 0;
  243. }
  244. #ifdef SHA2_UNROLL_TRANSFORM
  245. /* Unrolled SHA-256 round macros: */
  246. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \
  247. W256[j] = (u_int32_t)data[3] | ((u_int32_t)data[2] << 8) | \
  248. ((u_int32_t)data[1] << 16) | ((u_int32_t)data[0] << 24); \
  249. data += 4; \
  250. T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
  251. (d) += T1; \
  252. (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
  253. j++; \
  254. } while(0)
  255. #define ROUND256(a,b,c,d,e,f,g,h) do { \
  256. s0 = W256[(j+1)&0x0f]; \
  257. s0 = sigma0_256(s0); \
  258. s1 = W256[(j+14)&0x0f]; \
  259. s1 = sigma1_256(s1); \
  260. T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \
  261. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
  262. (d) += T1; \
  263. (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \
  264. j++; \
  265. } while(0)
  266. void
  267. SHA256Transform(u_int32_t *state, const u_int8_t *data)
  268. {
  269. u_int32_t a, b, c, d, e, f, g, h, s0, s1;
  270. u_int32_t T1, W256[16];
  271. int j;
  272. /* Initialize registers with the prev. intermediate value */
  273. a = state[0];
  274. b = state[1];
  275. c = state[2];
  276. d = state[3];
  277. e = state[4];
  278. f = state[5];
  279. g = state[6];
  280. h = state[7];
  281. j = 0;
  282. do {
  283. /* Rounds 0 to 15 (unrolled): */
  284. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  285. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  286. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  287. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  288. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  289. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  290. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  291. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  292. } while (j < 16);
  293. /* Now for the remaining rounds to 64: */
  294. do {
  295. ROUND256(a,b,c,d,e,f,g,h);
  296. ROUND256(h,a,b,c,d,e,f,g);
  297. ROUND256(g,h,a,b,c,d,e,f);
  298. ROUND256(f,g,h,a,b,c,d,e);
  299. ROUND256(e,f,g,h,a,b,c,d);
  300. ROUND256(d,e,f,g,h,a,b,c);
  301. ROUND256(c,d,e,f,g,h,a,b);
  302. ROUND256(b,c,d,e,f,g,h,a);
  303. } while (j < 64);
  304. /* Compute the current intermediate hash value */
  305. state[0] += a;
  306. state[1] += b;
  307. state[2] += c;
  308. state[3] += d;
  309. state[4] += e;
  310. state[5] += f;
  311. state[6] += g;
  312. state[7] += h;
  313. /* Clean up */
  314. a = b = c = d = e = f = g = h = T1 = 0;
  315. }
  316. #else /* SHA2_UNROLL_TRANSFORM */
  317. void
  318. SHA256Transform(u_int32_t *state, const u_int8_t *data)
  319. {
  320. u_int32_t a, b, c, d, e, f, g, h, s0, s1;
  321. u_int32_t T1, T2, W256[16];
  322. int j;
  323. /* Initialize registers with the prev. intermediate value */
  324. a = state[0];
  325. b = state[1];
  326. c = state[2];
  327. d = state[3];
  328. e = state[4];
  329. f = state[5];
  330. g = state[6];
  331. h = state[7];
  332. j = 0;
  333. do {
  334. W256[j] = (u_int32_t)data[3] | ((u_int32_t)data[2] << 8) |
  335. ((u_int32_t)data[1] << 16) | ((u_int32_t)data[0] << 24);
  336. data += 4;
  337. /* Apply the SHA-256 compression function to update a..h */
  338. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
  339. T2 = Sigma0_256(a) + Maj(a, b, c);
  340. h = g;
  341. g = f;
  342. f = e;
  343. e = d + T1;
  344. d = c;
  345. c = b;
  346. b = a;
  347. a = T1 + T2;
  348. j++;
  349. } while (j < 16);
  350. do {
  351. /* Part of the message block expansion: */
  352. s0 = W256[(j+1)&0x0f];
  353. s0 = sigma0_256(s0);
  354. s1 = W256[(j+14)&0x0f];
  355. s1 = sigma1_256(s1);
  356. /* Apply the SHA-256 compression function to update a..h */
  357. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  358. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  359. T2 = Sigma0_256(a) + Maj(a, b, c);
  360. h = g;
  361. g = f;
  362. f = e;
  363. e = d + T1;
  364. d = c;
  365. c = b;
  366. b = a;
  367. a = T1 + T2;
  368. j++;
  369. } while (j < 64);
  370. /* Compute the current intermediate hash value */
  371. state[0] += a;
  372. state[1] += b;
  373. state[2] += c;
  374. state[3] += d;
  375. state[4] += e;
  376. state[5] += f;
  377. state[6] += g;
  378. state[7] += h;
  379. /* Clean up */
  380. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  381. }
  382. #endif /* SHA2_UNROLL_TRANSFORM */
  383. void
  384. SHA256Update(SHA2_CTX *context, const void *dataptr, size_t len)
  385. {
  386. const uint8_t *data = dataptr;
  387. size_t freespace, usedspace;
  388. /* Calling with no data is valid (we do nothing) */
  389. if (len == 0)
  390. return;
  391. usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
  392. if (usedspace > 0) {
  393. /* Calculate how much free space is available in the buffer */
  394. freespace = SHA256_BLOCK_LENGTH - usedspace;
  395. if (len >= freespace) {
  396. /* Fill the buffer completely and process it */
  397. memcpy(&context->buffer[usedspace], data, freespace);
  398. context->bitcount[0] += freespace << 3;
  399. len -= freespace;
  400. data += freespace;
  401. SHA256Transform(context->state.st32, context->buffer);
  402. } else {
  403. /* The buffer is not yet full */
  404. memcpy(&context->buffer[usedspace], data, len);
  405. context->bitcount[0] += len << 3;
  406. /* Clean up: */
  407. usedspace = freespace = 0;
  408. return;
  409. }
  410. }
  411. while (len >= SHA256_BLOCK_LENGTH) {
  412. /* Process as many complete blocks as we can */
  413. SHA256Transform(context->state.st32, data);
  414. context->bitcount[0] += SHA256_BLOCK_LENGTH << 3;
  415. len -= SHA256_BLOCK_LENGTH;
  416. data += SHA256_BLOCK_LENGTH;
  417. }
  418. if (len > 0) {
  419. /* There's left-overs, so save 'em */
  420. memcpy(context->buffer, data, len);
  421. context->bitcount[0] += len << 3;
  422. }
  423. /* Clean up: */
  424. usedspace = freespace = 0;
  425. }
  426. void
  427. SHA256Final(u_int8_t digest[], SHA2_CTX *context)
  428. {
  429. unsigned int usedspace;
  430. usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
  431. #if BYTE_ORDER == LITTLE_ENDIAN
  432. /* Convert FROM host byte order */
  433. context->bitcount[0] = swap64(context->bitcount[0]);
  434. #endif
  435. if (usedspace > 0) {
  436. /* Begin padding with a 1 bit: */
  437. context->buffer[usedspace++] = 0x80;
  438. if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
  439. /* Set-up for the last transform: */
  440. memset(&context->buffer[usedspace], 0,
  441. SHA256_SHORT_BLOCK_LENGTH - usedspace);
  442. } else {
  443. if (usedspace < SHA256_BLOCK_LENGTH) {
  444. memset(&context->buffer[usedspace], 0,
  445. SHA256_BLOCK_LENGTH - usedspace);
  446. }
  447. /* Do second-to-last transform: */
  448. SHA256Transform(context->state.st32, context->buffer);
  449. /* And set-up for the last transform: */
  450. memset(context->buffer, 0,
  451. SHA256_SHORT_BLOCK_LENGTH);
  452. }
  453. } else {
  454. /* Set-up for the last transform: */
  455. memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
  456. /* Begin padding with a 1 bit: */
  457. *context->buffer = 0x80;
  458. }
  459. /* Set the bit count: */
  460. *(u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount[0];
  461. /* Final transform: */
  462. SHA256Transform(context->state.st32, context->buffer);
  463. #if BYTE_ORDER == LITTLE_ENDIAN
  464. {
  465. /* Convert TO host byte order */
  466. int j;
  467. for (j = 0; j < 8; j++) {
  468. context->state.st32[j] = swap32(context->state.st32[j]);
  469. }
  470. }
  471. #endif
  472. memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
  473. /* Clean up state data: */
  474. explicit_bzero(context, sizeof(*context));
  475. usedspace = 0;
  476. }
  477. /*** SHA-512: *********************************************************/
  478. void
  479. SHA512Init(SHA2_CTX *context)
  480. {
  481. memcpy(context->state.st64, sha512_initial_hash_value,
  482. SHA512_DIGEST_LENGTH);
  483. memset(context->buffer, 0, SHA512_BLOCK_LENGTH);
  484. context->bitcount[0] = context->bitcount[1] = 0;
  485. }
  486. #ifdef SHA2_UNROLL_TRANSFORM
  487. /* Unrolled SHA-512 round macros: */
  488. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do { \
  489. W512[j] = (u_int64_t)data[7] | ((u_int64_t)data[6] << 8) | \
  490. ((u_int64_t)data[5] << 16) | ((u_int64_t)data[4] << 24) | \
  491. ((u_int64_t)data[3] << 32) | ((u_int64_t)data[2] << 40) | \
  492. ((u_int64_t)data[1] << 48) | ((u_int64_t)data[0] << 56); \
  493. data += 8; \
  494. T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
  495. (d) += T1; \
  496. (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
  497. j++; \
  498. } while(0)
  499. #define ROUND512(a,b,c,d,e,f,g,h) do { \
  500. s0 = W512[(j+1)&0x0f]; \
  501. s0 = sigma0_512(s0); \
  502. s1 = W512[(j+14)&0x0f]; \
  503. s1 = sigma1_512(s1); \
  504. T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + \
  505. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  506. (d) += T1; \
  507. (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c)); \
  508. j++; \
  509. } while(0)
  510. void
  511. SHA512Transform(u_int64_t *state, const u_int8_t *data)
  512. {
  513. u_int64_t a, b, c, d, e, f, g, h, s0, s1;
  514. u_int64_t T1, W512[16];
  515. int j;
  516. /* Initialize registers with the prev. intermediate value */
  517. a = state[0];
  518. b = state[1];
  519. c = state[2];
  520. d = state[3];
  521. e = state[4];
  522. f = state[5];
  523. g = state[6];
  524. h = state[7];
  525. j = 0;
  526. do {
  527. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  528. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  529. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  530. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  531. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  532. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  533. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  534. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  535. } while (j < 16);
  536. /* Now for the remaining rounds up to 79: */
  537. do {
  538. ROUND512(a,b,c,d,e,f,g,h);
  539. ROUND512(h,a,b,c,d,e,f,g);
  540. ROUND512(g,h,a,b,c,d,e,f);
  541. ROUND512(f,g,h,a,b,c,d,e);
  542. ROUND512(e,f,g,h,a,b,c,d);
  543. ROUND512(d,e,f,g,h,a,b,c);
  544. ROUND512(c,d,e,f,g,h,a,b);
  545. ROUND512(b,c,d,e,f,g,h,a);
  546. } while (j < 80);
  547. /* Compute the current intermediate hash value */
  548. state[0] += a;
  549. state[1] += b;
  550. state[2] += c;
  551. state[3] += d;
  552. state[4] += e;
  553. state[5] += f;
  554. state[6] += g;
  555. state[7] += h;
  556. /* Clean up */
  557. a = b = c = d = e = f = g = h = T1 = 0;
  558. }
  559. #else /* SHA2_UNROLL_TRANSFORM */
  560. void
  561. SHA512Transform(u_int64_t *state, const u_int8_t *data)
  562. {
  563. u_int64_t a, b, c, d, e, f, g, h, s0, s1;
  564. u_int64_t T1, T2, W512[16];
  565. int j;
  566. /* Initialize registers with the prev. intermediate value */
  567. a = state[0];
  568. b = state[1];
  569. c = state[2];
  570. d = state[3];
  571. e = state[4];
  572. f = state[5];
  573. g = state[6];
  574. h = state[7];
  575. j = 0;
  576. do {
  577. W512[j] = (u_int64_t)data[7] | ((u_int64_t)data[6] << 8) |
  578. ((u_int64_t)data[5] << 16) | ((u_int64_t)data[4] << 24) |
  579. ((u_int64_t)data[3] << 32) | ((u_int64_t)data[2] << 40) |
  580. ((u_int64_t)data[1] << 48) | ((u_int64_t)data[0] << 56);
  581. data += 8;
  582. /* Apply the SHA-512 compression function to update a..h */
  583. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  584. T2 = Sigma0_512(a) + Maj(a, b, c);
  585. h = g;
  586. g = f;
  587. f = e;
  588. e = d + T1;
  589. d = c;
  590. c = b;
  591. b = a;
  592. a = T1 + T2;
  593. j++;
  594. } while (j < 16);
  595. do {
  596. /* Part of the message block expansion: */
  597. s0 = W512[(j+1)&0x0f];
  598. s0 = sigma0_512(s0);
  599. s1 = W512[(j+14)&0x0f];
  600. s1 = sigma1_512(s1);
  601. /* Apply the SHA-512 compression function to update a..h */
  602. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  603. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  604. T2 = Sigma0_512(a) + Maj(a, b, c);
  605. h = g;
  606. g = f;
  607. f = e;
  608. e = d + T1;
  609. d = c;
  610. c = b;
  611. b = a;
  612. a = T1 + T2;
  613. j++;
  614. } while (j < 80);
  615. /* Compute the current intermediate hash value */
  616. state[0] += a;
  617. state[1] += b;
  618. state[2] += c;
  619. state[3] += d;
  620. state[4] += e;
  621. state[5] += f;
  622. state[6] += g;
  623. state[7] += h;
  624. /* Clean up */
  625. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  626. }
  627. #endif /* SHA2_UNROLL_TRANSFORM */
  628. void
  629. SHA512Update(SHA2_CTX *context, const void *dataptr, size_t len)
  630. {
  631. const uint8_t *data = dataptr;
  632. size_t freespace, usedspace;
  633. /* Calling with no data is valid (we do nothing) */
  634. if (len == 0)
  635. return;
  636. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  637. if (usedspace > 0) {
  638. /* Calculate how much free space is available in the buffer */
  639. freespace = SHA512_BLOCK_LENGTH - usedspace;
  640. if (len >= freespace) {
  641. /* Fill the buffer completely and process it */
  642. memcpy(&context->buffer[usedspace], data, freespace);
  643. ADDINC128(context->bitcount, freespace << 3);
  644. len -= freespace;
  645. data += freespace;
  646. SHA512Transform(context->state.st64, context->buffer);
  647. } else {
  648. /* The buffer is not yet full */
  649. memcpy(&context->buffer[usedspace], data, len);
  650. ADDINC128(context->bitcount, len << 3);
  651. /* Clean up: */
  652. usedspace = freespace = 0;
  653. return;
  654. }
  655. }
  656. while (len >= SHA512_BLOCK_LENGTH) {
  657. /* Process as many complete blocks as we can */
  658. SHA512Transform(context->state.st64, data);
  659. ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
  660. len -= SHA512_BLOCK_LENGTH;
  661. data += SHA512_BLOCK_LENGTH;
  662. }
  663. if (len > 0) {
  664. /* There's left-overs, so save 'em */
  665. memcpy(context->buffer, data, len);
  666. ADDINC128(context->bitcount, len << 3);
  667. }
  668. /* Clean up: */
  669. usedspace = freespace = 0;
  670. }
  671. void
  672. SHA512Last(SHA2_CTX *context)
  673. {
  674. unsigned int usedspace;
  675. usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
  676. #if BYTE_ORDER == LITTLE_ENDIAN
  677. /* Convert FROM host byte order */
  678. context->bitcount[0] = swap64(context->bitcount[0]);
  679. context->bitcount[1] = swap64(context->bitcount[1]);
  680. #endif
  681. if (usedspace > 0) {
  682. /* Begin padding with a 1 bit: */
  683. context->buffer[usedspace++] = 0x80;
  684. if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
  685. /* Set-up for the last transform: */
  686. memset(&context->buffer[usedspace], 0,
  687. SHA512_SHORT_BLOCK_LENGTH - usedspace);
  688. } else {
  689. if (usedspace < SHA512_BLOCK_LENGTH) {
  690. memset(&context->buffer[usedspace], 0,
  691. SHA512_BLOCK_LENGTH - usedspace);
  692. }
  693. /* Do second-to-last transform: */
  694. SHA512Transform(context->state.st64, context->buffer);
  695. /* And set-up for the last transform: */
  696. memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
  697. }
  698. } else {
  699. /* Prepare for final transform: */
  700. memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
  701. /* Begin padding with a 1 bit: */
  702. *context->buffer = 0x80;
  703. }
  704. /* Store the length of input data (in bits): */
  705. *(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
  706. *(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
  707. /* Final transform: */
  708. SHA512Transform(context->state.st64, context->buffer);
  709. }
  710. void
  711. SHA512Final(u_int8_t digest[], SHA2_CTX *context)
  712. {
  713. SHA512Last(context);
  714. /* Save the hash data for output: */
  715. #if BYTE_ORDER == LITTLE_ENDIAN
  716. {
  717. /* Convert TO host byte order */
  718. int j;
  719. for (j = 0; j < 8; j++) {
  720. context->state.st64[j] = swap64(context->state.st64[j]);
  721. }
  722. }
  723. #endif
  724. memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
  725. /* Zero out state data */
  726. explicit_bzero(context, sizeof(*context));
  727. }
  728. /*** SHA-384: *********************************************************/
  729. void
  730. SHA384Init(SHA2_CTX *context)
  731. {
  732. memcpy(context->state.st64, sha384_initial_hash_value,
  733. SHA512_DIGEST_LENGTH);
  734. memset(context->buffer, 0, SHA384_BLOCK_LENGTH);
  735. context->bitcount[0] = context->bitcount[1] = 0;
  736. }
  737. void
  738. SHA384Update(SHA2_CTX *context, const void *data, size_t len)
  739. {
  740. SHA512Update(context, data, len);
  741. }
  742. void
  743. SHA384Final(u_int8_t digest[], SHA2_CTX *context)
  744. {
  745. SHA512Last(context);
  746. /* Save the hash data for output: */
  747. #if BYTE_ORDER == LITTLE_ENDIAN
  748. {
  749. /* Convert TO host byte order */
  750. int j;
  751. for (j = 0; j < 6; j++) {
  752. context->state.st64[j] = swap64(context->state.st64[j]);
  753. }
  754. }
  755. #endif
  756. memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
  757. /* Zero out state data */
  758. explicit_bzero(context, sizeof(*context));
  759. }