/contrib/bind9/lib/isc/sha2.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1449 lines · 1023 code · 182 blank · 244 comment · 137 complexity · 87f485a0daedd79db17ac4bc30a4839b MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2007, 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id$ */
  17. /* $FreeBSD$ */
  18. /* $KAME: sha2.c,v 1.8 2001/11/08 01:07:52 itojun Exp $ */
  19. /*
  20. * sha2.c
  21. *
  22. * Version 1.0.0beta1
  23. *
  24. * Written by Aaron D. Gifford <me@aarongifford.com>
  25. *
  26. * Copyright 2000 Aaron D. Gifford. All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. * 3. Neither the name of the copyright holder nor the names of contributors
  37. * may be used to endorse or promote products derived from this software
  38. * without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. */
  53. #include <config.h>
  54. #include <isc/assertions.h>
  55. #include <isc/platform.h>
  56. #include <isc/sha2.h>
  57. #include <isc/string.h>
  58. #include <isc/util.h>
  59. #ifdef ISC_PLATFORM_OPENSSLHASH
  60. void
  61. isc_sha224_init(isc_sha224_t *context) {
  62. if (context == (isc_sha224_t *)0) {
  63. return;
  64. }
  65. EVP_DigestInit(context, EVP_sha224());
  66. }
  67. void
  68. isc_sha224_invalidate(isc_sha224_t *context) {
  69. EVP_MD_CTX_cleanup(context);
  70. }
  71. void
  72. isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
  73. if (len == 0U) {
  74. /* Calling with no data is valid - we do nothing */
  75. return;
  76. }
  77. /* Sanity check: */
  78. REQUIRE(context != (isc_sha224_t *)0 && data != (isc_uint8_t*)0);
  79. EVP_DigestUpdate(context, (const void *) data, len);
  80. }
  81. void
  82. isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {
  83. /* Sanity check: */
  84. REQUIRE(context != (isc_sha224_t *)0);
  85. /* If no digest buffer is passed, we don't bother doing this: */
  86. if (digest != (isc_uint8_t*)0) {
  87. EVP_DigestFinal(context, digest, NULL);
  88. } else {
  89. EVP_MD_CTX_cleanup(context);
  90. }
  91. }
  92. void
  93. isc_sha256_init(isc_sha256_t *context) {
  94. if (context == (isc_sha256_t *)0) {
  95. return;
  96. }
  97. EVP_DigestInit(context, EVP_sha256());
  98. }
  99. void
  100. isc_sha256_invalidate(isc_sha256_t *context) {
  101. EVP_MD_CTX_cleanup(context);
  102. }
  103. void
  104. isc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {
  105. if (len == 0U) {
  106. /* Calling with no data is valid - we do nothing */
  107. return;
  108. }
  109. /* Sanity check: */
  110. REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
  111. EVP_DigestUpdate(context, (const void *) data, len);
  112. }
  113. void
  114. isc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
  115. /* Sanity check: */
  116. REQUIRE(context != (isc_sha256_t *)0);
  117. /* If no digest buffer is passed, we don't bother doing this: */
  118. if (digest != (isc_uint8_t*)0) {
  119. EVP_DigestFinal(context, digest, NULL);
  120. } else {
  121. EVP_MD_CTX_cleanup(context);
  122. }
  123. }
  124. void
  125. isc_sha512_init(isc_sha512_t *context) {
  126. if (context == (isc_sha512_t *)0) {
  127. return;
  128. }
  129. EVP_DigestInit(context, EVP_sha512());
  130. }
  131. void
  132. isc_sha512_invalidate(isc_sha512_t *context) {
  133. EVP_MD_CTX_cleanup(context);
  134. }
  135. void isc_sha512_update(isc_sha512_t *context, const isc_uint8_t *data, size_t len) {
  136. if (len == 0U) {
  137. /* Calling with no data is valid - we do nothing */
  138. return;
  139. }
  140. /* Sanity check: */
  141. REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
  142. EVP_DigestUpdate(context, (const void *) data, len);
  143. }
  144. void isc_sha512_final(isc_uint8_t digest[], isc_sha512_t *context) {
  145. /* Sanity check: */
  146. REQUIRE(context != (isc_sha512_t *)0);
  147. /* If no digest buffer is passed, we don't bother doing this: */
  148. if (digest != (isc_uint8_t*)0) {
  149. EVP_DigestFinal(context, digest, NULL);
  150. } else {
  151. EVP_MD_CTX_cleanup(context);
  152. }
  153. }
  154. void
  155. isc_sha384_init(isc_sha384_t *context) {
  156. if (context == (isc_sha384_t *)0) {
  157. return;
  158. }
  159. EVP_DigestInit(context, EVP_sha384());
  160. }
  161. void
  162. isc_sha384_invalidate(isc_sha384_t *context) {
  163. EVP_MD_CTX_cleanup(context);
  164. }
  165. void
  166. isc_sha384_update(isc_sha384_t *context, const isc_uint8_t* data, size_t len) {
  167. if (len == 0U) {
  168. /* Calling with no data is valid - we do nothing */
  169. return;
  170. }
  171. /* Sanity check: */
  172. REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
  173. EVP_DigestUpdate(context, (const void *) data, len);
  174. }
  175. void
  176. isc_sha384_final(isc_uint8_t digest[], isc_sha384_t *context) {
  177. /* Sanity check: */
  178. REQUIRE(context != (isc_sha384_t *)0);
  179. /* If no digest buffer is passed, we don't bother doing this: */
  180. if (digest != (isc_uint8_t*)0) {
  181. EVP_DigestFinal(context, digest, NULL);
  182. } else {
  183. EVP_MD_CTX_cleanup(context);
  184. }
  185. }
  186. #else
  187. /*
  188. * UNROLLED TRANSFORM LOOP NOTE:
  189. * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
  190. * loop version for the hash transform rounds (defined using macros
  191. * later in this file). Either define on the command line, for example:
  192. *
  193. * cc -DISC_SHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
  194. *
  195. * or define below:
  196. *
  197. * \#define ISC_SHA2_UNROLL_TRANSFORM
  198. *
  199. */
  200. /*** SHA-256/384/512 Machine Architecture Definitions *****************/
  201. /*
  202. * BYTE_ORDER NOTE:
  203. *
  204. * Please make sure that your system defines BYTE_ORDER. If your
  205. * architecture is little-endian, make sure it also defines
  206. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  207. * equivalent.
  208. *
  209. * If your system does not define the above, then you can do so by
  210. * hand like this:
  211. *
  212. * \#define LITTLE_ENDIAN 1234
  213. * \#define BIG_ENDIAN 4321
  214. *
  215. * And for little-endian machines, add:
  216. *
  217. * \#define BYTE_ORDER LITTLE_ENDIAN
  218. *
  219. * Or for big-endian machines:
  220. *
  221. * \#define BYTE_ORDER BIG_ENDIAN
  222. *
  223. * The FreeBSD machine this was written on defines BYTE_ORDER
  224. * appropriately by including <sys/types.h> (which in turn includes
  225. * <machine/endian.h> where the appropriate definitions are actually
  226. * made).
  227. */
  228. #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
  229. #ifndef BYTE_ORDER
  230. #ifndef BIG_ENDIAN
  231. #define BIG_ENDIAN 4321
  232. #endif
  233. #ifndef LITTLE_ENDIAN
  234. #define LITTLE_ENDIAN 1234
  235. #endif
  236. #ifdef WORDS_BIGENDIAN
  237. #define BYTE_ORDER BIG_ENDIAN
  238. #else
  239. #define BYTE_ORDER LITTLE_ENDIAN
  240. #endif
  241. #else
  242. #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
  243. #endif
  244. #endif
  245. /*** SHA-256/384/512 Various Length Definitions ***********************/
  246. /* NOTE: Most of these are in sha2.h */
  247. #define ISC_SHA256_SHORT_BLOCK_LENGTH (ISC_SHA256_BLOCK_LENGTH - 8)
  248. #define ISC_SHA384_SHORT_BLOCK_LENGTH (ISC_SHA384_BLOCK_LENGTH - 16)
  249. #define ISC_SHA512_SHORT_BLOCK_LENGTH (ISC_SHA512_BLOCK_LENGTH - 16)
  250. /*** ENDIAN REVERSAL MACROS *******************************************/
  251. #if BYTE_ORDER == LITTLE_ENDIAN
  252. #define REVERSE32(w,x) { \
  253. isc_uint32_t tmp = (w); \
  254. tmp = (tmp >> 16) | (tmp << 16); \
  255. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  256. }
  257. #ifdef WIN32
  258. #define REVERSE64(w,x) { \
  259. isc_uint64_t tmp = (w); \
  260. tmp = (tmp >> 32) | (tmp << 32); \
  261. tmp = ((tmp & 0xff00ff00ff00ff00UL) >> 8) | \
  262. ((tmp & 0x00ff00ff00ff00ffUL) << 8); \
  263. (x) = ((tmp & 0xffff0000ffff0000UL) >> 16) | \
  264. ((tmp & 0x0000ffff0000ffffUL) << 16); \
  265. }
  266. #else
  267. #define REVERSE64(w,x) { \
  268. isc_uint64_t tmp = (w); \
  269. tmp = (tmp >> 32) | (tmp << 32); \
  270. tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
  271. ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
  272. (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
  273. ((tmp & 0x0000ffff0000ffffULL) << 16); \
  274. }
  275. #endif
  276. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  277. /*
  278. * Macro for incrementally adding the unsigned 64-bit integer n to the
  279. * unsigned 128-bit integer (represented using a two-element array of
  280. * 64-bit words):
  281. */
  282. #define ADDINC128(w,n) { \
  283. (w)[0] += (isc_uint64_t)(n); \
  284. if ((w)[0] < (n)) { \
  285. (w)[1]++; \
  286. } \
  287. }
  288. /*** THE SIX LOGICAL FUNCTIONS ****************************************/
  289. /*
  290. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  291. *
  292. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  293. * S is a ROTATION) because the SHA-256/384/512 description document
  294. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  295. * same "backwards" definition.
  296. */
  297. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  298. #define R(b,x) ((x) >> (b))
  299. /* 32-bit Rotate-right (used in SHA-256): */
  300. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  301. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  302. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  303. /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
  304. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  305. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  306. /* Four of six logical functions used in SHA-256: */
  307. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  308. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  309. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  310. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  311. /* Four of six logical functions used in SHA-384 and SHA-512: */
  312. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  313. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  314. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  315. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
  316. /*** INTERNAL FUNCTION PROTOTYPES *************************************/
  317. /* NOTE: These should not be accessed directly from outside this
  318. * library -- they are intended for private internal visibility/use
  319. * only.
  320. */
  321. void isc_sha512_last(isc_sha512_t *);
  322. void isc_sha256_transform(isc_sha256_t *, const isc_uint32_t*);
  323. void isc_sha512_transform(isc_sha512_t *, const isc_uint64_t*);
  324. /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
  325. /* Hash constant words K for SHA-224 and SHA-256: */
  326. static const isc_uint32_t K256[64] = {
  327. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  328. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  329. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  330. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  331. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  332. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  333. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  334. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  335. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  336. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  337. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  338. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  339. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  340. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  341. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  342. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  343. };
  344. /* Initial hash value H for SHA-224: */
  345. static const isc_uint32_t sha224_initial_hash_value[8] = {
  346. 0xc1059ed8UL,
  347. 0x367cd507UL,
  348. 0x3070dd17UL,
  349. 0xf70e5939UL,
  350. 0xffc00b31UL,
  351. 0x68581511UL,
  352. 0x64f98fa7UL,
  353. 0xbefa4fa4UL
  354. };
  355. /* Initial hash value H for SHA-256: */
  356. static const isc_uint32_t sha256_initial_hash_value[8] = {
  357. 0x6a09e667UL,
  358. 0xbb67ae85UL,
  359. 0x3c6ef372UL,
  360. 0xa54ff53aUL,
  361. 0x510e527fUL,
  362. 0x9b05688cUL,
  363. 0x1f83d9abUL,
  364. 0x5be0cd19UL
  365. };
  366. #ifdef WIN32
  367. /* Hash constant words K for SHA-384 and SHA-512: */
  368. static const isc_uint64_t K512[80] = {
  369. 0x428a2f98d728ae22UL, 0x7137449123ef65cdUL,
  370. 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL,
  371. 0x3956c25bf348b538UL, 0x59f111f1b605d019UL,
  372. 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL,
  373. 0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
  374. 0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL,
  375. 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL,
  376. 0x9bdc06a725c71235UL, 0xc19bf174cf692694UL,
  377. 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL,
  378. 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
  379. 0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL,
  380. 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL,
  381. 0x983e5152ee66dfabUL, 0xa831c66d2db43210UL,
  382. 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL,
  383. 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
  384. 0x06ca6351e003826fUL, 0x142929670a0e6e70UL,
  385. 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL,
  386. 0x4d2c6dfc5ac42aedUL, 0x53380d139d95b3dfUL,
  387. 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL,
  388. 0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
  389. 0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL,
  390. 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL,
  391. 0xd192e819d6ef5218UL, 0xd69906245565a910UL,
  392. 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL,
  393. 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
  394. 0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL,
  395. 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL,
  396. 0x5b9cca4f7763e373UL, 0x682e6ff3d6b2b8a3UL,
  397. 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL,
  398. 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
  399. 0x90befffa23631e28UL, 0xa4506cebde82bde9UL,
  400. 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL,
  401. 0xca273eceea26619cUL, 0xd186b8c721c0c207UL,
  402. 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL,
  403. 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
  404. 0x113f9804bef90daeUL, 0x1b710b35131c471bUL,
  405. 0x28db77f523047d84UL, 0x32caab7b40c72493UL,
  406. 0x3c9ebe0a15c9bebcUL, 0x431d67c49c100d4cUL,
  407. 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL,
  408. 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL
  409. };
  410. /* Initial hash value H for SHA-384: */
  411. static const isc_uint64_t sha384_initial_hash_value[8] = {
  412. 0xcbbb9d5dc1059ed8UL,
  413. 0x629a292a367cd507UL,
  414. 0x9159015a3070dd17UL,
  415. 0x152fecd8f70e5939UL,
  416. 0x67332667ffc00b31UL,
  417. 0x8eb44a8768581511UL,
  418. 0xdb0c2e0d64f98fa7UL,
  419. 0x47b5481dbefa4fa4UL
  420. };
  421. /* Initial hash value H for SHA-512: */
  422. static const isc_uint64_t sha512_initial_hash_value[8] = {
  423. 0x6a09e667f3bcc908U,
  424. 0xbb67ae8584caa73bUL,
  425. 0x3c6ef372fe94f82bUL,
  426. 0xa54ff53a5f1d36f1UL,
  427. 0x510e527fade682d1UL,
  428. 0x9b05688c2b3e6c1fUL,
  429. 0x1f83d9abfb41bd6bUL,
  430. 0x5be0cd19137e2179UL
  431. };
  432. #else
  433. /* Hash constant words K for SHA-384 and SHA-512: */
  434. static const isc_uint64_t K512[80] = {
  435. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  436. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  437. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  438. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  439. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  440. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  441. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  442. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  443. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  444. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  445. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  446. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  447. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  448. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  449. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  450. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  451. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  452. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  453. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  454. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  455. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  456. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  457. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  458. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  459. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  460. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  461. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  462. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  463. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  464. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  465. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  466. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  467. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  468. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  469. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  470. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  471. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  472. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  473. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  474. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  475. };
  476. /* Initial hash value H for SHA-384: */
  477. static const isc_uint64_t sha384_initial_hash_value[8] = {
  478. 0xcbbb9d5dc1059ed8ULL,
  479. 0x629a292a367cd507ULL,
  480. 0x9159015a3070dd17ULL,
  481. 0x152fecd8f70e5939ULL,
  482. 0x67332667ffc00b31ULL,
  483. 0x8eb44a8768581511ULL,
  484. 0xdb0c2e0d64f98fa7ULL,
  485. 0x47b5481dbefa4fa4ULL
  486. };
  487. /* Initial hash value H for SHA-512: */
  488. static const isc_uint64_t sha512_initial_hash_value[8] = {
  489. 0x6a09e667f3bcc908ULL,
  490. 0xbb67ae8584caa73bULL,
  491. 0x3c6ef372fe94f82bULL,
  492. 0xa54ff53a5f1d36f1ULL,
  493. 0x510e527fade682d1ULL,
  494. 0x9b05688c2b3e6c1fULL,
  495. 0x1f83d9abfb41bd6bULL,
  496. 0x5be0cd19137e2179ULL
  497. };
  498. #endif
  499. /*** SHA-224: *********************************************************/
  500. void
  501. isc_sha224_init(isc_sha224_t *context) {
  502. if (context == (isc_sha256_t *)0) {
  503. return;
  504. }
  505. memcpy(context->state, sha224_initial_hash_value,
  506. ISC_SHA256_DIGESTLENGTH);
  507. memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
  508. context->bitcount = 0;
  509. }
  510. void
  511. isc_sha224_invalidate(isc_sha224_t *context) {
  512. memset(context, 0, sizeof(isc_sha224_t));
  513. }
  514. void
  515. isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
  516. isc_sha256_update((isc_sha256_t *)context, data, len);
  517. }
  518. void
  519. isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {
  520. isc_uint8_t sha256_digest[ISC_SHA256_DIGESTLENGTH];
  521. isc_sha256_final(sha256_digest, (isc_sha256_t *)context);
  522. memcpy(digest, sha256_digest, ISC_SHA224_DIGESTLENGTH);
  523. memset(sha256_digest, 0, ISC_SHA256_DIGESTLENGTH);
  524. }
  525. /*** SHA-256: *********************************************************/
  526. void
  527. isc_sha256_init(isc_sha256_t *context) {
  528. if (context == (isc_sha256_t *)0) {
  529. return;
  530. }
  531. memcpy(context->state, sha256_initial_hash_value,
  532. ISC_SHA256_DIGESTLENGTH);
  533. memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
  534. context->bitcount = 0;
  535. }
  536. void
  537. isc_sha256_invalidate(isc_sha256_t *context) {
  538. memset(context, 0, sizeof(isc_sha256_t));
  539. }
  540. #ifdef ISC_SHA2_UNROLL_TRANSFORM
  541. /* Unrolled SHA-256 round macros: */
  542. #if BYTE_ORDER == LITTLE_ENDIAN
  543. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  544. REVERSE32(*data++, W256[j]); \
  545. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  546. K256[j] + W256[j]; \
  547. (d) += T1; \
  548. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  549. j++
  550. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  551. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
  552. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  553. K256[j] + (W256[j] = *data++); \
  554. (d) += T1; \
  555. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  556. j++
  557. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  558. #define ROUND256(a,b,c,d,e,f,g,h) \
  559. s0 = W256[(j+1)&0x0f]; \
  560. s0 = sigma0_256(s0); \
  561. s1 = W256[(j+14)&0x0f]; \
  562. s1 = sigma1_256(s1); \
  563. T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
  564. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
  565. (d) += T1; \
  566. (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
  567. j++
  568. void isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
  569. isc_uint32_t a, b, c, d, e, f, g, h, s0, s1;
  570. isc_uint32_t T1, *W256;
  571. int j;
  572. W256 = (isc_uint32_t*)context->buffer;
  573. /* Initialize registers with the prev. intermediate value */
  574. a = context->state[0];
  575. b = context->state[1];
  576. c = context->state[2];
  577. d = context->state[3];
  578. e = context->state[4];
  579. f = context->state[5];
  580. g = context->state[6];
  581. h = context->state[7];
  582. j = 0;
  583. do {
  584. /* Rounds 0 to 15 (unrolled): */
  585. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  586. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  587. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  588. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  589. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  590. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  591. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  592. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  593. } while (j < 16);
  594. /* Now for the remaining rounds to 64: */
  595. do {
  596. ROUND256(a,b,c,d,e,f,g,h);
  597. ROUND256(h,a,b,c,d,e,f,g);
  598. ROUND256(g,h,a,b,c,d,e,f);
  599. ROUND256(f,g,h,a,b,c,d,e);
  600. ROUND256(e,f,g,h,a,b,c,d);
  601. ROUND256(d,e,f,g,h,a,b,c);
  602. ROUND256(c,d,e,f,g,h,a,b);
  603. ROUND256(b,c,d,e,f,g,h,a);
  604. } while (j < 64);
  605. /* Compute the current intermediate hash value */
  606. context->state[0] += a;
  607. context->state[1] += b;
  608. context->state[2] += c;
  609. context->state[3] += d;
  610. context->state[4] += e;
  611. context->state[5] += f;
  612. context->state[6] += g;
  613. context->state[7] += h;
  614. /* Clean up */
  615. a = b = c = d = e = f = g = h = T1 = 0;
  616. /* Avoid compiler warnings */
  617. POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
  618. POST(g); POST(h); POST(T1);
  619. }
  620. #else /* ISC_SHA2_UNROLL_TRANSFORM */
  621. void
  622. isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
  623. isc_uint32_t a, b, c, d, e, f, g, h, s0, s1;
  624. isc_uint32_t T1, T2, *W256;
  625. int j;
  626. W256 = (isc_uint32_t*)context->buffer;
  627. /* Initialize registers with the prev. intermediate value */
  628. a = context->state[0];
  629. b = context->state[1];
  630. c = context->state[2];
  631. d = context->state[3];
  632. e = context->state[4];
  633. f = context->state[5];
  634. g = context->state[6];
  635. h = context->state[7];
  636. j = 0;
  637. do {
  638. #if BYTE_ORDER == LITTLE_ENDIAN
  639. /* Copy data while converting to host byte order */
  640. REVERSE32(*data++,W256[j]);
  641. /* Apply the SHA-256 compression function to update a..h */
  642. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
  643. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  644. /* Apply the SHA-256 compression function to update a..h with copy */
  645. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
  646. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  647. T2 = Sigma0_256(a) + Maj(a, b, c);
  648. h = g;
  649. g = f;
  650. f = e;
  651. e = d + T1;
  652. d = c;
  653. c = b;
  654. b = a;
  655. a = T1 + T2;
  656. j++;
  657. } while (j < 16);
  658. do {
  659. /* Part of the message block expansion: */
  660. s0 = W256[(j+1)&0x0f];
  661. s0 = sigma0_256(s0);
  662. s1 = W256[(j+14)&0x0f];
  663. s1 = sigma1_256(s1);
  664. /* Apply the SHA-256 compression function to update a..h */
  665. T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
  666. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  667. T2 = Sigma0_256(a) + Maj(a, b, c);
  668. h = g;
  669. g = f;
  670. f = e;
  671. e = d + T1;
  672. d = c;
  673. c = b;
  674. b = a;
  675. a = T1 + T2;
  676. j++;
  677. } while (j < 64);
  678. /* Compute the current intermediate hash value */
  679. context->state[0] += a;
  680. context->state[1] += b;
  681. context->state[2] += c;
  682. context->state[3] += d;
  683. context->state[4] += e;
  684. context->state[5] += f;
  685. context->state[6] += g;
  686. context->state[7] += h;
  687. /* Clean up */
  688. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  689. /* Avoid compiler warnings */
  690. POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
  691. POST(g); POST(h); POST(T1); POST(T2);
  692. }
  693. #endif /* ISC_SHA2_UNROLL_TRANSFORM */
  694. void
  695. isc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {
  696. unsigned int freespace, usedspace;
  697. if (len == 0U) {
  698. /* Calling with no data is valid - we do nothing */
  699. return;
  700. }
  701. /* Sanity check: */
  702. REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
  703. usedspace = (unsigned int)((context->bitcount >> 3) %
  704. ISC_SHA256_BLOCK_LENGTH);
  705. if (usedspace > 0) {
  706. /* Calculate how much free space is available in the buffer */
  707. freespace = ISC_SHA256_BLOCK_LENGTH - usedspace;
  708. if (len >= freespace) {
  709. /* Fill the buffer completely and process it */
  710. memcpy(&context->buffer[usedspace], data, freespace);
  711. context->bitcount += freespace << 3;
  712. len -= freespace;
  713. data += freespace;
  714. isc_sha256_transform(context,
  715. (isc_uint32_t*)context->buffer);
  716. } else {
  717. /* The buffer is not yet full */
  718. memcpy(&context->buffer[usedspace], data, len);
  719. context->bitcount += len << 3;
  720. /* Clean up: */
  721. usedspace = freespace = 0;
  722. /* Avoid compiler warnings: */
  723. POST(usedspace); POST(freespace);
  724. return;
  725. }
  726. }
  727. while (len >= ISC_SHA256_BLOCK_LENGTH) {
  728. /* Process as many complete blocks as we can */
  729. memcpy(context->buffer, data, ISC_SHA256_BLOCK_LENGTH);
  730. isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
  731. context->bitcount += ISC_SHA256_BLOCK_LENGTH << 3;
  732. len -= ISC_SHA256_BLOCK_LENGTH;
  733. data += ISC_SHA256_BLOCK_LENGTH;
  734. }
  735. if (len > 0U) {
  736. /* There's left-overs, so save 'em */
  737. memcpy(context->buffer, data, len);
  738. context->bitcount += len << 3;
  739. }
  740. /* Clean up: */
  741. usedspace = freespace = 0;
  742. /* Avoid compiler warnings: */
  743. POST(usedspace); POST(freespace);
  744. }
  745. void
  746. isc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
  747. isc_uint32_t *d = (isc_uint32_t*)digest;
  748. unsigned int usedspace;
  749. /* Sanity check: */
  750. REQUIRE(context != (isc_sha256_t *)0);
  751. /* If no digest buffer is passed, we don't bother doing this: */
  752. if (digest != (isc_uint8_t*)0) {
  753. usedspace = (unsigned int)((context->bitcount >> 3) %
  754. ISC_SHA256_BLOCK_LENGTH);
  755. #if BYTE_ORDER == LITTLE_ENDIAN
  756. /* Convert FROM host byte order */
  757. REVERSE64(context->bitcount,context->bitcount);
  758. #endif
  759. if (usedspace > 0) {
  760. /* Begin padding with a 1 bit: */
  761. context->buffer[usedspace++] = 0x80;
  762. if (usedspace <= ISC_SHA256_SHORT_BLOCK_LENGTH) {
  763. /* Set-up for the last transform: */
  764. memset(&context->buffer[usedspace], 0,
  765. ISC_SHA256_SHORT_BLOCK_LENGTH - usedspace);
  766. } else {
  767. if (usedspace < ISC_SHA256_BLOCK_LENGTH) {
  768. memset(&context->buffer[usedspace], 0,
  769. ISC_SHA256_BLOCK_LENGTH -
  770. usedspace);
  771. }
  772. /* Do second-to-last transform: */
  773. isc_sha256_transform(context,
  774. (isc_uint32_t*)context->buffer);
  775. /* And set-up for the last transform: */
  776. memset(context->buffer, 0,
  777. ISC_SHA256_SHORT_BLOCK_LENGTH);
  778. }
  779. } else {
  780. /* Set-up for the last transform: */
  781. memset(context->buffer, 0, ISC_SHA256_SHORT_BLOCK_LENGTH);
  782. /* Begin padding with a 1 bit: */
  783. *context->buffer = 0x80;
  784. }
  785. /* Set the bit count: */
  786. *(isc_uint64_t*)&context->buffer[ISC_SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
  787. /* Final transform: */
  788. isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
  789. #if BYTE_ORDER == LITTLE_ENDIAN
  790. {
  791. /* Convert TO host byte order */
  792. int j;
  793. for (j = 0; j < 8; j++) {
  794. REVERSE32(context->state[j],context->state[j]);
  795. *d++ = context->state[j];
  796. }
  797. }
  798. #else
  799. memcpy(d, context->state, ISC_SHA256_DIGESTLENGTH);
  800. #endif
  801. }
  802. /* Clean up state data: */
  803. memset(context, 0, sizeof(*context));
  804. usedspace = 0;
  805. POST(usedspace);
  806. }
  807. /*** SHA-512: *********************************************************/
  808. void
  809. isc_sha512_init(isc_sha512_t *context) {
  810. if (context == (isc_sha512_t *)0) {
  811. return;
  812. }
  813. memcpy(context->state, sha512_initial_hash_value,
  814. ISC_SHA512_DIGESTLENGTH);
  815. memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH);
  816. context->bitcount[0] = context->bitcount[1] = 0;
  817. }
  818. void
  819. isc_sha512_invalidate(isc_sha512_t *context) {
  820. memset(context, 0, sizeof(isc_sha512_t));
  821. }
  822. #ifdef ISC_SHA2_UNROLL_TRANSFORM
  823. /* Unrolled SHA-512 round macros: */
  824. #if BYTE_ORDER == LITTLE_ENDIAN
  825. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  826. REVERSE64(*data++, W512[j]); \
  827. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  828. K512[j] + W512[j]; \
  829. (d) += T1, \
  830. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
  831. j++
  832. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  833. #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
  834. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  835. K512[j] + (W512[j] = *data++); \
  836. (d) += T1; \
  837. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  838. j++
  839. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  840. #define ROUND512(a,b,c,d,e,f,g,h) \
  841. s0 = W512[(j+1)&0x0f]; \
  842. s0 = sigma0_512(s0); \
  843. s1 = W512[(j+14)&0x0f]; \
  844. s1 = sigma1_512(s1); \
  845. T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
  846. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
  847. (d) += T1; \
  848. (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
  849. j++
  850. void isc_sha512_transform(isc_sha512_t *context, const isc_uint64_t* data) {
  851. isc_uint64_t a, b, c, d, e, f, g, h, s0, s1;
  852. isc_uint64_t T1, *W512 = (isc_uint64_t*)context->buffer;
  853. int j;
  854. /* Initialize registers with the prev. intermediate value */
  855. a = context->state[0];
  856. b = context->state[1];
  857. c = context->state[2];
  858. d = context->state[3];
  859. e = context->state[4];
  860. f = context->state[5];
  861. g = context->state[6];
  862. h = context->state[7];
  863. j = 0;
  864. do {
  865. ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
  866. ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
  867. ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
  868. ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
  869. ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
  870. ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
  871. ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
  872. ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
  873. } while (j < 16);
  874. /* Now for the remaining rounds up to 79: */
  875. do {
  876. ROUND512(a,b,c,d,e,f,g,h);
  877. ROUND512(h,a,b,c,d,e,f,g);
  878. ROUND512(g,h,a,b,c,d,e,f);
  879. ROUND512(f,g,h,a,b,c,d,e);
  880. ROUND512(e,f,g,h,a,b,c,d);
  881. ROUND512(d,e,f,g,h,a,b,c);
  882. ROUND512(c,d,e,f,g,h,a,b);
  883. ROUND512(b,c,d,e,f,g,h,a);
  884. } while (j < 80);
  885. /* Compute the current intermediate hash value */
  886. context->state[0] += a;
  887. context->state[1] += b;
  888. context->state[2] += c;
  889. context->state[3] += d;
  890. context->state[4] += e;
  891. context->state[5] += f;
  892. context->state[6] += g;
  893. context->state[7] += h;
  894. /* Clean up */
  895. a = b = c = d = e = f = g = h = T1 = 0;
  896. /* Avoid compiler warnings */
  897. POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
  898. POST(g); POST(h); POST(T1);
  899. }
  900. #else /* ISC_SHA2_UNROLL_TRANSFORM */
  901. void
  902. isc_sha512_transform(isc_sha512_t *context, const isc_uint64_t* data) {
  903. isc_uint64_t a, b, c, d, e, f, g, h, s0, s1;
  904. isc_uint64_t T1, T2, *W512 = (isc_uint64_t*)context->buffer;
  905. int j;
  906. /* Initialize registers with the prev. intermediate value */
  907. a = context->state[0];
  908. b = context->state[1];
  909. c = context->state[2];
  910. d = context->state[3];
  911. e = context->state[4];
  912. f = context->state[5];
  913. g = context->state[6];
  914. h = context->state[7];
  915. j = 0;
  916. do {
  917. #if BYTE_ORDER == LITTLE_ENDIAN
  918. /* Convert TO host byte order */
  919. REVERSE64(*data++, W512[j]);
  920. /* Apply the SHA-512 compression function to update a..h */
  921. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
  922. #else /* BYTE_ORDER == LITTLE_ENDIAN */
  923. /* Apply the SHA-512 compression function to update a..h with copy */
  924. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
  925. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  926. T2 = Sigma0_512(a) + Maj(a, b, c);
  927. h = g;
  928. g = f;
  929. f = e;
  930. e = d + T1;
  931. d = c;
  932. c = b;
  933. b = a;
  934. a = T1 + T2;
  935. j++;
  936. } while (j < 16);
  937. do {
  938. /* Part of the message block expansion: */
  939. s0 = W512[(j+1)&0x0f];
  940. s0 = sigma0_512(s0);
  941. s1 = W512[(j+14)&0x0f];
  942. s1 = sigma1_512(s1);
  943. /* Apply the SHA-512 compression function to update a..h */
  944. T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
  945. (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
  946. T2 = Sigma0_512(a) + Maj(a, b, c);
  947. h = g;
  948. g = f;
  949. f = e;
  950. e = d + T1;
  951. d = c;
  952. c = b;
  953. b = a;
  954. a = T1 + T2;
  955. j++;
  956. } while (j < 80);
  957. /* Compute the current intermediate hash value */
  958. context->state[0] += a;
  959. context->state[1] += b;
  960. context->state[2] += c;
  961. context->state[3] += d;
  962. context->state[4] += e;
  963. context->state[5] += f;
  964. context->state[6] += g;
  965. context->state[7] += h;
  966. /* Clean up */
  967. a = b = c = d = e = f = g = h = T1 = T2 = 0;
  968. /* Avoid compiler warnings */
  969. POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
  970. POST(g); POST(h); POST(T1); POST(T2);
  971. }
  972. #endif /* ISC_SHA2_UNROLL_TRANSFORM */
  973. void isc_sha512_update(isc_sha512_t *context, const isc_uint8_t *data, size_t len) {
  974. unsigned int freespace, usedspace;
  975. if (len == 0U) {
  976. /* Calling with no data is valid - we do nothing */
  977. return;
  978. }
  979. /* Sanity check: */
  980. REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
  981. usedspace = (unsigned int)((context->bitcount[0] >> 3) %
  982. ISC_SHA512_BLOCK_LENGTH);
  983. if (usedspace > 0) {
  984. /* Calculate how much free space is available in the buffer */
  985. freespace = ISC_SHA512_BLOCK_LENGTH - usedspace;
  986. if (len >= freespace) {
  987. /* Fill the buffer completely and process it */
  988. memcpy(&context->buffer[usedspace], data, freespace);
  989. ADDINC128(context->bitcount, freespace << 3);
  990. len -= freespace;
  991. data += freespace;
  992. isc_sha512_transform(context,
  993. (isc_uint64_t*)context->buffer);
  994. } else {
  995. /* The buffer is not yet full */
  996. memcpy(&context->buffer[usedspace], data, len);
  997. ADDINC128(context->bitcount, len << 3);
  998. /* Clean up: */
  999. usedspace = freespace = 0;
  1000. /* Avoid compiler warnings: */
  1001. POST(usedspace); POST(freespace);
  1002. return;
  1003. }
  1004. }
  1005. while (len >= ISC_SHA512_BLOCK_LENGTH) {
  1006. /* Process as many complete blocks as we can */
  1007. memcpy(context->buffer, data, ISC_SHA512_BLOCK_LENGTH);
  1008. isc_sha512_transform(context, (isc_uint64_t*)context->buffer);
  1009. ADDINC128(context->bitcount, ISC_SHA512_BLOCK_LENGTH << 3);
  1010. len -= ISC_SHA512_BLOCK_LENGTH;
  1011. data += ISC_SHA512_BLOCK_LENGTH;
  1012. }
  1013. if (len > 0U) {
  1014. /* There's left-overs, so save 'em */
  1015. memcpy(context->buffer, data, len);
  1016. ADDINC128(context->bitcount, len << 3);
  1017. }
  1018. /* Clean up: */
  1019. usedspace = freespace = 0;
  1020. /* Avoid compiler warnings: */
  1021. POST(usedspace); POST(freespace);
  1022. }
  1023. void isc_sha512_last(isc_sha512_t *context) {
  1024. unsigned int usedspace;
  1025. usedspace = (unsigned int)((context->bitcount[0] >> 3) %
  1026. ISC_SHA512_BLOCK_LENGTH);
  1027. #if BYTE_ORDER == LITTLE_ENDIAN
  1028. /* Convert FROM host byte order */
  1029. REVERSE64(context->bitcount[0],context->bitcount[0]);
  1030. REVERSE64(context->bitcount[1],context->bitcount[1]);
  1031. #endif
  1032. if (usedspace > 0) {
  1033. /* Begin padding with a 1 bit: */
  1034. context->buffer[usedspace++] = 0x80;
  1035. if (usedspace <= ISC_SHA512_SHORT_BLOCK_LENGTH) {
  1036. /* Set-up for the last transform: */
  1037. memset(&context->buffer[usedspace], 0,
  1038. ISC_SHA512_SHORT_BLOCK_LENGTH - usedspace);
  1039. } else {
  1040. if (usedspace < ISC_SHA512_BLOCK_LENGTH) {
  1041. memset(&context->buffer[usedspace], 0,
  1042. ISC_SHA512_BLOCK_LENGTH - usedspace);
  1043. }
  1044. /* Do second-to-last transform: */
  1045. isc_sha512_transform(context,
  1046. (isc_uint64_t*)context->buffer);
  1047. /* And set-up for the last transform: */
  1048. memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH - 2);
  1049. }
  1050. } else {
  1051. /* Prepare for final transform: */
  1052. memset(context->buffer, 0, ISC_SHA512_SHORT_BLOCK_LENGTH);
  1053. /* Begin padding with a 1 bit: */
  1054. *context->buffer = 0x80;
  1055. }
  1056. /* Store the length of input data (in bits): */
  1057. *(isc_uint64_t*)&context->buffer[ISC_SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
  1058. *(isc_uint64_t*)&context->buffer[ISC_SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
  1059. /* Final transform: */
  1060. isc_sha512_transform(context, (isc_uint64_t*)context->buffer);
  1061. }
  1062. void isc_sha512_final(isc_uint8_t digest[], isc_sha512_t *context) {
  1063. isc_uint64_t *d = (isc_uint64_t*)digest;
  1064. /* Sanity check: */
  1065. REQUIRE(context != (isc_sha512_t *)0);
  1066. /* If no digest buffer is passed, we don't bother doing this: */
  1067. if (digest != (isc_uint8_t*)0) {
  1068. isc_sha512_last(context);
  1069. /* Save the hash data for output: */
  1070. #if BYTE_ORDER == LITTLE_ENDIAN
  1071. {
  1072. /* Convert TO host byte order */
  1073. int j;
  1074. for (j = 0; j < 8; j++) {
  1075. REVERSE64(context->state[j],context->state[j]);
  1076. *d++ = context->state[j];
  1077. }
  1078. }
  1079. #else
  1080. memcpy(d, context->state, ISC_SHA512_DIGESTLENGTH);
  1081. #endif
  1082. }
  1083. /* Zero out state data */
  1084. memset(context, 0, sizeof(*context));
  1085. }
  1086. /*** SHA-384: *********************************************************/
  1087. void
  1088. isc_sha384_init(isc_sha384_t *context) {
  1089. if (context == (isc_sha384_t *)0) {
  1090. return;
  1091. }
  1092. memcpy(context->state, sha384_initial_hash_value,
  1093. ISC_SHA512_DIGESTLENGTH);
  1094. memset(context->buffer, 0, ISC_SHA384_BLOCK_LENGTH);
  1095. context->bitcount[0] = context->bitcount[1] = 0;
  1096. }
  1097. void
  1098. isc_sha384_invalidate(isc_sha384_t *context) {
  1099. memset(context, 0, sizeof(isc_sha384_t));
  1100. }
  1101. void
  1102. isc_sha384_update(isc_sha384_t *context, const isc_uint8_t* data, size_t len) {
  1103. isc_sha512_update((isc_sha512_t *)context, data, len);
  1104. }
  1105. void
  1106. isc_sha384_final(isc_uint8_t digest[], isc_sha384_t *context) {
  1107. isc_uint64_t *d = (isc_uint64_t*)digest;
  1108. /* Sanity check: */
  1109. REQUIRE(context != (isc_sha384_t *)0);
  1110. /* If no digest buffer is passed, we don't bother doing this: */
  1111. if (digest != (isc_uint8_t*)0) {
  1112. isc_sha512_last((isc_sha512_t *)context);
  1113. /* Save the hash data for output: */
  1114. #if BYTE_ORDER == LITTLE_ENDIAN
  1115. {
  1116. /* Convert TO host byte order */
  1117. int j;
  1118. for (j = 0; j < 6; j++) {
  1119. REVERSE64(context->state[j],context->state[j]);
  1120. *d++ = context->state[j];
  1121. }
  1122. }
  1123. #else
  1124. memcpy(d, context->state, ISC_SHA384_DIGESTLENGTH);
  1125. #endif
  1126. }
  1127. /* Zero out state data */
  1128. memset(context, 0, sizeof(*context));
  1129. }
  1130. #endif /* !ISC_PLATFORM_OPENSSLHASH */
  1131. /*
  1132. * Constant used by SHA256/384/512_End() functions for converting the
  1133. * digest to a readable hexadecimal character string:
  1134. */
  1135. static const char *sha2_hex_digits = "0123456789abcdef";
  1136. char *
  1137. isc_sha224_end(isc_sha224_t *context, char buffer[]) {
  1138. isc_uint8_t digest[ISC_SHA224_DIGESTLENGTH], *d = digest;
  1139. unsigned int i;
  1140. /* Sanity check: */
  1141. REQUIRE(context != (isc_sha224_t *)0);
  1142. if (buffer != (char*)0) {
  1143. isc_sha224_final(digest, context);
  1144. for (i = 0; i < ISC_SHA224_DIGESTLENGTH; i++) {
  1145. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  1146. *buffer++ = sha2_hex_digits[*d & 0x0f];
  1147. d++;
  1148. }
  1149. *buffer = (char)0;
  1150. } else {
  1151. #ifdef ISC_PLATFORM_OPENSSLHASH
  1152. EVP_MD_CTX_cleanup(context);
  1153. #else
  1154. memset(context, 0, sizeof(*context));
  1155. #endif
  1156. }
  1157. memset(digest, 0, ISC_SHA224_DIGESTLENGTH);
  1158. return buffer;
  1159. }
  1160. char *
  1161. isc_sha224_data(const isc_uint8_t *data, size_t len,
  1162. char digest[ISC_SHA224_DIGESTSTRINGLENGTH])
  1163. {
  1164. isc_sha224_t context;
  1165. isc_sha224_init(&context);
  1166. isc_sha224_update(&context, data, len);
  1167. return (isc_sha224_end(&context, digest));
  1168. }
  1169. char *
  1170. isc_sha256_end(isc_sha256_t *context, char buffer[]) {
  1171. isc_uint8_t digest[ISC_SHA256_DIGESTLENGTH], *d = digest;
  1172. unsigned int i;
  1173. /* Sanity check: */
  1174. REQUIRE(context != (isc_sha256_t *)0);
  1175. if (buffer != (char*)0) {
  1176. isc_sha256_final(digest, context);
  1177. for (i = 0; i < ISC_SHA256_DIGESTLENGTH; i++) {
  1178. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  1179. *buffer++ = sha2_hex_digits[*d & 0x0f];
  1180. d++;
  1181. }
  1182. *buffer = (char)0;
  1183. } else {
  1184. #ifdef ISC_PLATFORM_OPENSSLHASH
  1185. EVP_MD_CTX_cleanup(context);
  1186. #else
  1187. memset(context, 0, sizeof(*context));
  1188. #endif
  1189. }
  1190. memset(digest, 0, ISC_SHA256_DIGESTLENGTH);
  1191. return buffer;
  1192. }
  1193. char *
  1194. isc_sha256_data(const isc_uint8_t* data, size_t len,
  1195. char digest[ISC_SHA256_DIGESTSTRINGLENGTH])
  1196. {
  1197. isc_sha256_t context;
  1198. isc_sha256_init(&context);
  1199. isc_sha256_update(&context, data, len);
  1200. return (isc_sha256_end(&context, digest));
  1201. }
  1202. char *
  1203. isc_sha512_end(isc_sha512_t *context, char buffer[]) {
  1204. isc_uint8_t digest[ISC_SHA512_DIGESTLENGTH], *d = digest;
  1205. unsigned int i;
  1206. /* Sanity check: */
  1207. REQUIRE(context != (isc_sha512_t *)0);
  1208. if (buffer != (char*)0) {
  1209. isc_sha512_final(digest, context);
  1210. for (i = 0; i < ISC_SHA512_DIGESTLENGTH; i++) {
  1211. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  1212. *buffer++ = sha2_hex_digits[*d & 0x0f];
  1213. d++;
  1214. }
  1215. *buffer = (char)0;
  1216. } else {
  1217. #ifdef ISC_PLATFORM_OPENSSLHASH
  1218. EVP_MD_CTX_cleanup(context);
  1219. #else
  1220. memset(context, 0, sizeof(*context));
  1221. #endif
  1222. }
  1223. memset(digest, 0, ISC_SHA512_DIGESTLENGTH);
  1224. return buffer;
  1225. }
  1226. char *
  1227. isc_sha512_data(const isc_uint8_t *data, size_t len,
  1228. char digest[ISC_SHA512_DIGESTSTRINGLENGTH])
  1229. {
  1230. isc_sha512_t context;
  1231. isc_sha512_init(&context);
  1232. isc_sha512_update(&context, data, len);
  1233. return (isc_sha512_end(&context, digest));
  1234. }
  1235. char *
  1236. isc_sha384_end(isc_sha384_t *context, char buffer[]) {
  1237. isc_uint8_t digest[ISC_SHA384_DIGESTLENGTH], *d = digest;
  1238. unsigned int i;
  1239. /* Sanity check: */
  1240. REQUIRE(context != (isc_sha384_t *)0);
  1241. if (buffer != (char*)0) {
  1242. isc_sha384_final(digest, context);
  1243. for (i = 0; i < ISC_SHA384_DIGESTLENGTH; i++) {
  1244. *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
  1245. *buffer++ = sha2_hex_digits[*d & 0x0f];
  1246. d++;
  1247. }
  1248. *buffer = (char)0;
  1249. } else {
  1250. #ifdef ISC_PLATFORM_OPENSSLHASH
  1251. EVP_MD_CTX_cleanup(context);
  1252. #else
  1253. memset(context, 0, sizeof(*context));
  1254. #endif
  1255. }
  1256. memset(digest, 0, ISC_SHA384_DIGESTLENGTH);
  1257. return buffer;
  1258. }
  1259. char *
  1260. isc_sha384_data(const isc_uint8_t *data, size_t len,
  1261. char digest[ISC_SHA384_DIGESTSTRINGLENGTH])
  1262. {
  1263. isc_sha384_t context;
  1264. isc_sha384_init(&context);
  1265. isc_sha384_update(&context, data, len);
  1266. return (isc_sha384_end(&context, digest));
  1267. }