2
0

sha256.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* libcrypto/sha/sha256.c */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. */
  54. #include <stdint.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include "my_sha256.h"
  58. #if defined(_MSC_VER) && !defined(__clang__) && !defined(__attribute__)
  59. #define __attribute__(a)
  60. #endif
  61. #include "cmake_config.h" /* HAVE_ENDIAN_H */
  62. /** endian.h **/
  63. /*
  64. * Public domain
  65. * endian.h compatibility shim
  66. */
  67. #if defined(_WIN32)
  68. #define LITTLE_ENDIAN 1234
  69. #define BIG_ENDIAN 4321
  70. #define PDP_ENDIAN 3412
  71. /*
  72. * Use GCC and Visual Studio compiler defines to determine endian.
  73. */
  74. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  75. #define BYTE_ORDER LITTLE_ENDIAN
  76. #else
  77. #define BYTE_ORDER BIG_ENDIAN
  78. #endif
  79. #elif defined(HAVE_ENDIAN_H)
  80. #include <endian.h>
  81. #elif defined(__MACH__) && defined(__APPLE__)
  82. #include <machine/endian.h>
  83. #elif defined(__sun) || defined(_AIX) || defined(__hpux)
  84. #include <arpa/nameser_compat.h>
  85. #include <sys/types.h>
  86. #elif defined(__sgi)
  87. #include <standards.h>
  88. #include <sys/endian.h>
  89. #endif
  90. #ifndef __STRICT_ALIGNMENT
  91. #define __STRICT_ALIGNMENT
  92. #if defined(__i386) || defined(__i386__) || defined(__x86_64) || \
  93. defined(__x86_64__) || defined(__s390__) || defined(__s390x__) || \
  94. defined(__aarch64__) || \
  95. ((defined(__arm__) || defined(__arm)) && __ARM_ARCH >= 6)
  96. #undef __STRICT_ALIGNMENT
  97. #endif
  98. #endif
  99. #if defined(__APPLE__) && !defined(HAVE_ENDIAN_H)
  100. #include <libkern/OSByteOrder.h>
  101. #define be16toh(x) OSSwapBigToHostInt16((x))
  102. #define htobe16(x) OSSwapHostToBigInt16((x))
  103. #define le32toh(x) OSSwapLittleToHostInt32((x))
  104. #define be32toh(x) OSSwapBigToHostInt32((x))
  105. #define htole32(x) OSSwapHostToLittleInt32(x)
  106. #define htobe32(x) OSSwapHostToBigInt32(x)
  107. #endif /* __APPLE__ && !HAVE_ENDIAN_H */
  108. #if defined(_WIN32) && !defined(HAVE_ENDIAN_H)
  109. #include <winsock2.h>
  110. #define be16toh(x) ntohs((x))
  111. #define htobe16(x) htons((x))
  112. #define le32toh(x) (x)
  113. #define be32toh(x) ntohl((x))
  114. #define htole32(x) (x)
  115. #define htobe32(x) ntohl((x))
  116. #endif /* _WIN32 && !HAVE_ENDIAN_H */
  117. #ifdef __linux__
  118. #if !defined(betoh16)
  119. #define betoh16(x) be16toh(x)
  120. #endif
  121. #if !defined(betoh32)
  122. #define betoh32(x) be32toh(x)
  123. #endif
  124. #endif /* __linux__ */
  125. #if defined(__FreeBSD__)
  126. #if !defined(HAVE_ENDIAN_H)
  127. #include <sys/endian.h>
  128. #endif
  129. #if !defined(betoh16)
  130. #define betoh16(x) be16toh(x)
  131. #endif
  132. #if !defined(betoh32)
  133. #define betoh32(x) be32toh(x)
  134. #endif
  135. #endif
  136. #if defined(__NetBSD__)
  137. #if !defined(betoh16)
  138. #define betoh16(x) be16toh(x)
  139. #endif
  140. #if !defined(betoh32)
  141. #define betoh32(x) be32toh(x)
  142. #endif
  143. #endif
  144. #if defined(__sun)
  145. #include <sys/byteorder.h>
  146. #define be16toh(x) BE_16(x)
  147. #define htobe16(x) BE_16(x)
  148. #define le32toh(x) LE_32(x)
  149. #define be32toh(x) BE_32(x)
  150. #define htole32(x) LE_32(x)
  151. #define htobe32(x) BE_32(x)
  152. #endif
  153. /** **/
  154. /** libcrypto/crypto_internal.h **/
  155. #define CTASSERT(x) \
  156. extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__))
  157. static inline uint32_t
  158. crypto_load_be32toh(const uint8_t *src)
  159. {
  160. uint32_t v;
  161. memcpy(&v, src, sizeof(v));
  162. return be32toh(v);
  163. }
  164. static inline void
  165. crypto_store_htobe32(uint8_t *dst, uint32_t v)
  166. {
  167. v = htobe32(v);
  168. memcpy(dst, &v, sizeof(v));
  169. }
  170. static inline uint32_t
  171. crypto_ror_u32(uint32_t v, size_t shift)
  172. {
  173. return (v << (32 - shift)) | (v >> shift);
  174. }
  175. /** **/
  176. /** libcrypto/hidden/crypto_namespace.h **/
  177. # define LCRYPTO_UNUSED(x)
  178. # define LCRYPTO_USED(x)
  179. # define LCRYPTO_ALIAS1(pre,x)
  180. # define LCRYPTO_ALIAS(x)
  181. /** **/
  182. /* Ensure that SHA_LONG and uint32_t are equivalent. */
  183. CTASSERT(sizeof(SHA_LONG) == sizeof(uint32_t));
  184. static void sha256_block_data_order(SHA256_CTX *ctx, const void *_in, size_t num);
  185. static const SHA_LONG K256[64] = {
  186. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  187. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  188. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  189. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  190. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  191. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  192. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  193. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  194. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  195. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  196. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  197. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  198. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  199. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  200. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  201. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
  202. };
  203. static inline SHA_LONG
  204. Sigma0(SHA_LONG x)
  205. {
  206. return crypto_ror_u32(x, 2) ^ crypto_ror_u32(x, 13) ^
  207. crypto_ror_u32(x, 22);
  208. }
  209. static inline SHA_LONG
  210. Sigma1(SHA_LONG x)
  211. {
  212. return crypto_ror_u32(x, 6) ^ crypto_ror_u32(x, 11) ^
  213. crypto_ror_u32(x, 25);
  214. }
  215. static inline SHA_LONG
  216. sigma0(SHA_LONG x)
  217. {
  218. return crypto_ror_u32(x, 7) ^ crypto_ror_u32(x, 18) ^ (x >> 3);
  219. }
  220. static inline SHA_LONG
  221. sigma1(SHA_LONG x)
  222. {
  223. return crypto_ror_u32(x, 17) ^ crypto_ror_u32(x, 19) ^ (x >> 10);
  224. }
  225. static inline SHA_LONG
  226. Ch(SHA_LONG x, SHA_LONG y, SHA_LONG z)
  227. {
  228. return (x & y) ^ (~x & z);
  229. }
  230. static inline SHA_LONG
  231. Maj(SHA_LONG x, SHA_LONG y, SHA_LONG z)
  232. {
  233. return (x & y) ^ (x & z) ^ (y & z);
  234. }
  235. static inline void
  236. sha256_msg_schedule_update(SHA_LONG *W0, SHA_LONG W1,
  237. SHA_LONG W9, SHA_LONG W14)
  238. {
  239. *W0 = sigma1(W14) + W9 + sigma0(W1) + *W0;
  240. }
  241. static inline void
  242. sha256_round(SHA_LONG *a, SHA_LONG *b, SHA_LONG *c, SHA_LONG *d,
  243. SHA_LONG *e, SHA_LONG *f, SHA_LONG *g, SHA_LONG *h,
  244. SHA_LONG Kt, SHA_LONG Wt)
  245. {
  246. SHA_LONG T1, T2;
  247. T1 = *h + Sigma1(*e) + Ch(*e, *f, *g) + Kt + Wt;
  248. T2 = Sigma0(*a) + Maj(*a, *b, *c);
  249. *h = *g;
  250. *g = *f;
  251. *f = *e;
  252. *e = *d + T1;
  253. *d = *c;
  254. *c = *b;
  255. *b = *a;
  256. *a = T1 + T2;
  257. }
  258. static void
  259. sha256_block_data_order(SHA256_CTX *ctx, const void *_in, size_t num)
  260. {
  261. const uint8_t *in = _in;
  262. const SHA_LONG *in32;
  263. SHA_LONG a, b, c, d, e, f, g, h;
  264. SHA_LONG X[16];
  265. int i;
  266. while (num--) {
  267. a = ctx->h[0];
  268. b = ctx->h[1];
  269. c = ctx->h[2];
  270. d = ctx->h[3];
  271. e = ctx->h[4];
  272. f = ctx->h[5];
  273. g = ctx->h[6];
  274. h = ctx->h[7];
  275. if ((size_t)in % 4 == 0) {
  276. /* Input is 32 bit aligned. */
  277. in32 = (const SHA_LONG *)in;
  278. X[0] = be32toh(in32[0]);
  279. X[1] = be32toh(in32[1]);
  280. X[2] = be32toh(in32[2]);
  281. X[3] = be32toh(in32[3]);
  282. X[4] = be32toh(in32[4]);
  283. X[5] = be32toh(in32[5]);
  284. X[6] = be32toh(in32[6]);
  285. X[7] = be32toh(in32[7]);
  286. X[8] = be32toh(in32[8]);
  287. X[9] = be32toh(in32[9]);
  288. X[10] = be32toh(in32[10]);
  289. X[11] = be32toh(in32[11]);
  290. X[12] = be32toh(in32[12]);
  291. X[13] = be32toh(in32[13]);
  292. X[14] = be32toh(in32[14]);
  293. X[15] = be32toh(in32[15]);
  294. } else {
  295. /* Input is not 32 bit aligned. */
  296. X[0] = crypto_load_be32toh(&in[0 * 4]);
  297. X[1] = crypto_load_be32toh(&in[1 * 4]);
  298. X[2] = crypto_load_be32toh(&in[2 * 4]);
  299. X[3] = crypto_load_be32toh(&in[3 * 4]);
  300. X[4] = crypto_load_be32toh(&in[4 * 4]);
  301. X[5] = crypto_load_be32toh(&in[5 * 4]);
  302. X[6] = crypto_load_be32toh(&in[6 * 4]);
  303. X[7] = crypto_load_be32toh(&in[7 * 4]);
  304. X[8] = crypto_load_be32toh(&in[8 * 4]);
  305. X[9] = crypto_load_be32toh(&in[9 * 4]);
  306. X[10] = crypto_load_be32toh(&in[10 * 4]);
  307. X[11] = crypto_load_be32toh(&in[11 * 4]);
  308. X[12] = crypto_load_be32toh(&in[12 * 4]);
  309. X[13] = crypto_load_be32toh(&in[13 * 4]);
  310. X[14] = crypto_load_be32toh(&in[14 * 4]);
  311. X[15] = crypto_load_be32toh(&in[15 * 4]);
  312. }
  313. in += SHA256_CBLOCK;
  314. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[0], X[0]);
  315. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[1], X[1]);
  316. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[2], X[2]);
  317. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[3], X[3]);
  318. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[4], X[4]);
  319. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[5], X[5]);
  320. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[6], X[6]);
  321. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[7], X[7]);
  322. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[8], X[8]);
  323. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[9], X[9]);
  324. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[10], X[10]);
  325. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[11], X[11]);
  326. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[12], X[12]);
  327. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[13], X[13]);
  328. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[14], X[14]);
  329. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[15], X[15]);
  330. for (i = 16; i < 64; i += 16) {
  331. sha256_msg_schedule_update(&X[0], X[1], X[9], X[14]);
  332. sha256_msg_schedule_update(&X[1], X[2], X[10], X[15]);
  333. sha256_msg_schedule_update(&X[2], X[3], X[11], X[0]);
  334. sha256_msg_schedule_update(&X[3], X[4], X[12], X[1]);
  335. sha256_msg_schedule_update(&X[4], X[5], X[13], X[2]);
  336. sha256_msg_schedule_update(&X[5], X[6], X[14], X[3]);
  337. sha256_msg_schedule_update(&X[6], X[7], X[15], X[4]);
  338. sha256_msg_schedule_update(&X[7], X[8], X[0], X[5]);
  339. sha256_msg_schedule_update(&X[8], X[9], X[1], X[6]);
  340. sha256_msg_schedule_update(&X[9], X[10], X[2], X[7]);
  341. sha256_msg_schedule_update(&X[10], X[11], X[3], X[8]);
  342. sha256_msg_schedule_update(&X[11], X[12], X[4], X[9]);
  343. sha256_msg_schedule_update(&X[12], X[13], X[5], X[10]);
  344. sha256_msg_schedule_update(&X[13], X[14], X[6], X[11]);
  345. sha256_msg_schedule_update(&X[14], X[15], X[7], X[12]);
  346. sha256_msg_schedule_update(&X[15], X[0], X[8], X[13]);
  347. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 0], X[0]);
  348. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 1], X[1]);
  349. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 2], X[2]);
  350. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 3], X[3]);
  351. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 4], X[4]);
  352. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 5], X[5]);
  353. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 6], X[6]);
  354. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 7], X[7]);
  355. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 8], X[8]);
  356. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 9], X[9]);
  357. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 10], X[10]);
  358. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 11], X[11]);
  359. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 12], X[12]);
  360. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 13], X[13]);
  361. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 14], X[14]);
  362. sha256_round(&a, &b, &c, &d, &e, &f, &g, &h, K256[i + 15], X[15]);
  363. }
  364. ctx->h[0] += a;
  365. ctx->h[1] += b;
  366. ctx->h[2] += c;
  367. ctx->h[3] += d;
  368. ctx->h[4] += e;
  369. ctx->h[5] += f;
  370. ctx->h[6] += g;
  371. ctx->h[7] += h;
  372. }
  373. }
  374. int
  375. SHA256_Init(SHA256_CTX *c)
  376. {
  377. memset(c, 0, sizeof(*c));
  378. c->h[0] = 0x6a09e667UL;
  379. c->h[1] = 0xbb67ae85UL;
  380. c->h[2] = 0x3c6ef372UL;
  381. c->h[3] = 0xa54ff53aUL;
  382. c->h[4] = 0x510e527fUL;
  383. c->h[5] = 0x9b05688cUL;
  384. c->h[6] = 0x1f83d9abUL;
  385. c->h[7] = 0x5be0cd19UL;
  386. c->md_len = SHA256_DIGEST_LENGTH;
  387. return 1;
  388. }
  389. LCRYPTO_ALIAS(SHA256_Init);
  390. int
  391. SHA256_Update(SHA256_CTX *c, const void *data_, size_t len)
  392. {
  393. const unsigned char *data = data_;
  394. unsigned char *p;
  395. SHA_LONG l;
  396. size_t n;
  397. if (len == 0)
  398. return 1;
  399. l = (c->Nl + (((SHA_LONG)len) << 3)) & 0xffffffffUL;
  400. /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
  401. * Wei Dai <weidai@eskimo.com> for pointing it out. */
  402. if (l < c->Nl) /* overflow */
  403. c->Nh++;
  404. c->Nh += (SHA_LONG)(len >> 29); /* might cause compiler warning on 16-bit */
  405. c->Nl = l;
  406. n = c->num;
  407. if (n != 0) {
  408. p = (unsigned char *)c->data;
  409. if (len >= SHA_CBLOCK || len + n >= SHA_CBLOCK) {
  410. memcpy(p + n, data, SHA_CBLOCK - n);
  411. sha256_block_data_order(c, p, 1);
  412. n = SHA_CBLOCK - n;
  413. data += n;
  414. len -= n;
  415. c->num = 0;
  416. memset(p, 0, SHA_CBLOCK); /* keep it zeroed */
  417. } else {
  418. memcpy(p + n, data, len);
  419. c->num += (unsigned int)len;
  420. return 1;
  421. }
  422. }
  423. n = len/SHA_CBLOCK;
  424. if (n > 0) {
  425. sha256_block_data_order(c, data, n);
  426. n *= SHA_CBLOCK;
  427. data += n;
  428. len -= n;
  429. }
  430. if (len != 0) {
  431. p = (unsigned char *)c->data;
  432. c->num = (unsigned int)len;
  433. memcpy(p, data, len);
  434. }
  435. return 1;
  436. }
  437. LCRYPTO_ALIAS(SHA256_Update);
  438. void
  439. SHA256_Transform(SHA256_CTX *c, const unsigned char *data)
  440. {
  441. sha256_block_data_order(c, data, 1);
  442. }
  443. LCRYPTO_ALIAS(SHA256_Transform);
  444. int
  445. SHA256_Final(unsigned char *md, SHA256_CTX *c)
  446. {
  447. unsigned char *p = (unsigned char *)c->data;
  448. size_t n = c->num;
  449. unsigned int nn;
  450. p[n] = 0x80; /* there is always room for one */
  451. n++;
  452. if (n > (SHA_CBLOCK - 8)) {
  453. memset(p + n, 0, SHA_CBLOCK - n);
  454. n = 0;
  455. sha256_block_data_order(c, p, 1);
  456. }
  457. memset(p + n, 0, SHA_CBLOCK - 8 - n);
  458. c->data[SHA_LBLOCK - 2] = htobe32(c->Nh);
  459. c->data[SHA_LBLOCK - 1] = htobe32(c->Nl);
  460. sha256_block_data_order(c, p, 1);
  461. c->num = 0;
  462. memset(p, 0, SHA_CBLOCK);
  463. /*
  464. * Note that FIPS180-2 discusses "Truncation of the Hash Function Output."
  465. * default: case below covers for it. It's not clear however if it's
  466. * permitted to truncate to amount of bytes not divisible by 4. I bet not,
  467. * but if it is, then default: case shall be extended. For reference.
  468. * Idea behind separate cases for pre-defined lengths is to let the
  469. * compiler decide if it's appropriate to unroll small loops.
  470. */
  471. switch (c->md_len) {
  472. case SHA256_DIGEST_LENGTH:
  473. for (nn = 0; nn < SHA256_DIGEST_LENGTH / 4; nn++) {
  474. crypto_store_htobe32(md, c->h[nn]);
  475. md += 4;
  476. }
  477. break;
  478. default:
  479. if (c->md_len > SHA256_DIGEST_LENGTH)
  480. return 0;
  481. for (nn = 0; nn < c->md_len / 4; nn++) {
  482. crypto_store_htobe32(md, c->h[nn]);
  483. md += 4;
  484. }
  485. break;
  486. }
  487. return 1;
  488. }
  489. LCRYPTO_ALIAS(SHA256_Final);
  490. unsigned char *
  491. SHA256(const unsigned char *d, size_t n, unsigned char *md)
  492. {
  493. SHA256_CTX c;
  494. static unsigned char m[SHA256_DIGEST_LENGTH];
  495. if (md == NULL)
  496. md = m;
  497. SHA256_Init(&c);
  498. SHA256_Update(&c, d, n);
  499. SHA256_Final(md, &c);
  500. memset(&c, 0, sizeof(c));
  501. return (md);
  502. }
  503. LCRYPTO_ALIAS(SHA256);