sha256.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
  2. memory blocks according to the NIST specification FIPS-180-2.
  3. Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by David Madore, considerably copypasting from
  15. Scott G. Miller's sha1.c
  16. */
  17. /* Specification. */
  18. #if HAVE_OPENSSL_SHA256
  19. # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
  20. #endif
  21. #include "sha256.h"
  22. #include <stdint.h>
  23. #include <string.h>
  24. #ifdef WORDS_BIGENDIAN
  25. # define SWAP(n) (n)
  26. #else
  27. #include "byteswap.h"
  28. # define SWAP(n) swap_uint32 (n)
  29. #endif
  30. #if ! HAVE_OPENSSL_SHA256
  31. /* This array contains the bytes used to pad the buffer to the next
  32. 64-byte boundary. */
  33. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  34. /*
  35. Takes a pointer to a 256 bit block of data (eight 32 bit ints) and
  36. initializes it to the start constants of the SHA256 algorithm. This
  37. must be called before using hash in the call to sha256_hash
  38. */
  39. void
  40. sha256_init_ctx (struct sha256_ctx *ctx)
  41. {
  42. ctx->state[0] = 0x6a09e667UL;
  43. ctx->state[1] = 0xbb67ae85UL;
  44. ctx->state[2] = 0x3c6ef372UL;
  45. ctx->state[3] = 0xa54ff53aUL;
  46. ctx->state[4] = 0x510e527fUL;
  47. ctx->state[5] = 0x9b05688cUL;
  48. ctx->state[6] = 0x1f83d9abUL;
  49. ctx->state[7] = 0x5be0cd19UL;
  50. ctx->total[0] = ctx->total[1] = 0;
  51. ctx->buflen = 0;
  52. }
  53. void
  54. sha224_init_ctx (struct sha256_ctx *ctx)
  55. {
  56. ctx->state[0] = 0xc1059ed8UL;
  57. ctx->state[1] = 0x367cd507UL;
  58. ctx->state[2] = 0x3070dd17UL;
  59. ctx->state[3] = 0xf70e5939UL;
  60. ctx->state[4] = 0xffc00b31UL;
  61. ctx->state[5] = 0x68581511UL;
  62. ctx->state[6] = 0x64f98fa7UL;
  63. ctx->state[7] = 0xbefa4fa4UL;
  64. ctx->total[0] = ctx->total[1] = 0;
  65. ctx->buflen = 0;
  66. }
  67. /* Copy the value from v into the memory location pointed to by *CP,
  68. If your architecture allows unaligned access, this is equivalent to
  69. * (__typeof__ (v) *) cp = v */
  70. static void
  71. set_uint32 (char *cp, uint32_t v)
  72. {
  73. memcpy (cp, &v, sizeof v);
  74. }
  75. /* Put result from CTX in first 32 bytes following RESBUF.
  76. The result must be in little endian byte order. */
  77. void *
  78. sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
  79. {
  80. int i;
  81. char *r = resbuf;
  82. for (i = 0; i < 8; i++)
  83. set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
  84. return resbuf;
  85. }
  86. void *
  87. sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
  88. {
  89. int i;
  90. char *r = resbuf;
  91. for (i = 0; i < 7; i++)
  92. set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
  93. return resbuf;
  94. }
  95. /* Process the remaining bytes in the internal buffer and the usual
  96. prolog according to the standard and write the result to RESBUF. */
  97. static void
  98. sha256_conclude_ctx (struct sha256_ctx *ctx)
  99. {
  100. /* Take yet unprocessed bytes into account. */
  101. size_t bytes = ctx->buflen;
  102. size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
  103. /* Now count remaining bytes. */
  104. ctx->total[0] += bytes;
  105. if (ctx->total[0] < bytes)
  106. ++ctx->total[1];
  107. /* Put the 64-bit file length in *bits* at the end of the buffer.
  108. Use set_uint32 rather than a simple assignment, to avoid risk of
  109. unaligned access. */
  110. set_uint32 ((char *) &ctx->buffer[size - 2],
  111. SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
  112. set_uint32 ((char *) &ctx->buffer[size - 1],
  113. SWAP (ctx->total[0] << 3));
  114. memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
  115. /* Process last bytes. */
  116. sha256_process_block (ctx->buffer, size * 4, ctx);
  117. }
  118. void *
  119. sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
  120. {
  121. sha256_conclude_ctx (ctx);
  122. return sha256_read_ctx (ctx, resbuf);
  123. }
  124. void *
  125. sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
  126. {
  127. sha256_conclude_ctx (ctx);
  128. return sha224_read_ctx (ctx, resbuf);
  129. }
  130. /* Compute SHA256 message digest for LEN bytes beginning at BUFFER. The
  131. result is always in little endian byte order, so that a byte-wise
  132. output yields to the wanted ASCII representation of the message
  133. digest. */
  134. void *
  135. sha256_buffer (const char *buffer, size_t len, void *resblock)
  136. {
  137. struct sha256_ctx ctx;
  138. /* Initialize the computation context. */
  139. sha256_init_ctx (&ctx);
  140. /* Process whole buffer but last len % 64 bytes. */
  141. sha256_process_bytes (buffer, len, &ctx);
  142. /* Put result in desired memory area. */
  143. return sha256_finish_ctx (&ctx, resblock);
  144. }
  145. void *
  146. sha224_buffer (const char *buffer, size_t len, void *resblock)
  147. {
  148. struct sha256_ctx ctx;
  149. /* Initialize the computation context. */
  150. sha224_init_ctx (&ctx);
  151. /* Process whole buffer but last len % 64 bytes. */
  152. sha256_process_bytes (buffer, len, &ctx);
  153. /* Put result in desired memory area. */
  154. return sha224_finish_ctx (&ctx, resblock);
  155. }
  156. void
  157. sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
  158. {
  159. /* When we already have some bits in our internal buffer concatenate
  160. both inputs first. */
  161. if (ctx->buflen != 0)
  162. {
  163. size_t left_over = ctx->buflen;
  164. size_t add = 128 - left_over > len ? len : 128 - left_over;
  165. memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
  166. ctx->buflen += add;
  167. if (ctx->buflen > 64)
  168. {
  169. sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
  170. ctx->buflen &= 63;
  171. /* The regions in the following copy operation cannot overlap,
  172. because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
  173. memcpy (ctx->buffer,
  174. &((char *) ctx->buffer)[(left_over + add) & ~63],
  175. ctx->buflen);
  176. }
  177. buffer = (const char *) buffer + add;
  178. len -= add;
  179. }
  180. /* Process available complete blocks. */
  181. if (len >= 64)
  182. {
  183. #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
  184. # define UNALIGNED_P(p) ((uintptr_t) (p) % sizeof (uint32_t) != 0)
  185. if (UNALIGNED_P (buffer))
  186. while (len > 64)
  187. {
  188. sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
  189. buffer = (const char *) buffer + 64;
  190. len -= 64;
  191. }
  192. else
  193. #endif
  194. {
  195. sha256_process_block (buffer, len & ~63, ctx);
  196. buffer = (const char *) buffer + (len & ~63);
  197. len &= 63;
  198. }
  199. }
  200. /* Move remaining bytes in internal buffer. */
  201. if (len > 0)
  202. {
  203. size_t left_over = ctx->buflen;
  204. memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
  205. left_over += len;
  206. if (left_over >= 64)
  207. {
  208. sha256_process_block (ctx->buffer, 64, ctx);
  209. left_over -= 64;
  210. /* The regions in the following copy operation cannot overlap,
  211. because left_over ≤ 64. */
  212. memcpy (ctx->buffer, &ctx->buffer[16], left_over);
  213. }
  214. ctx->buflen = left_over;
  215. }
  216. }
  217. /* --- Code below is the primary difference between sha1.c and sha256.c --- */
  218. /* SHA256 round constants */
  219. #define K(I) sha256_round_constants[I]
  220. static const uint32_t sha256_round_constants[64] = {
  221. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  222. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  223. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  224. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  225. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  226. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  227. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  228. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  229. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  230. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  231. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  232. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  233. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  234. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  235. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  236. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
  237. };
  238. /* Round functions. */
  239. #define F2(A,B,C) ( ( A & B ) | ( C & ( A | B ) ) )
  240. #define F1(E,F,G) ( G ^ ( E & ( F ^ G ) ) )
  241. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  242. It is assumed that LEN % 64 == 0.
  243. Most of this code comes from GnuPG's cipher/sha1.c. */
  244. void
  245. sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
  246. {
  247. const uint32_t *words = buffer;
  248. size_t nwords = len / sizeof (uint32_t);
  249. const uint32_t *endp = words + nwords;
  250. uint32_t x[16];
  251. uint32_t a = ctx->state[0];
  252. uint32_t b = ctx->state[1];
  253. uint32_t c = ctx->state[2];
  254. uint32_t d = ctx->state[3];
  255. uint32_t e = ctx->state[4];
  256. uint32_t f = ctx->state[5];
  257. uint32_t g = ctx->state[6];
  258. uint32_t h = ctx->state[7];
  259. uint32_t lolen = len;
  260. /* First increment the byte count. FIPS PUB 180-2 specifies the possible
  261. length of the file up to 2^64 bits. Here we only compute the
  262. number of bytes. Do a double word increment. */
  263. ctx->total[0] += lolen;
  264. ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
  265. #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  266. #define S0(x) (rol(x,25)^rol(x,14)^(x>>3))
  267. #define S1(x) (rol(x,15)^rol(x,13)^(x>>10))
  268. #define SS0(x) (rol(x,30)^rol(x,19)^rol(x,10))
  269. #define SS1(x) (rol(x,26)^rol(x,21)^rol(x,7))
  270. #define M(I) ( tm = S1(x[(I-2)&0x0f]) + x[(I-7)&0x0f] \
  271. + S0(x[(I-15)&0x0f]) + x[I&0x0f] \
  272. , x[I&0x0f] = tm )
  273. #define R(A,B,C,D,E,F,G,H,K,M) do { t0 = SS0(A) + F2(A,B,C); \
  274. t1 = H + SS1(E) \
  275. + F1(E,F,G) \
  276. + K \
  277. + M; \
  278. D += t1; H = t0 + t1; \
  279. } while(0)
  280. while (words < endp)
  281. {
  282. uint32_t tm;
  283. uint32_t t0, t1;
  284. int t;
  285. /* FIXME: see sha1.c for a better implementation. */
  286. for (t = 0; t < 16; t++)
  287. {
  288. x[t] = SWAP (*words);
  289. words++;
  290. }
  291. R( a, b, c, d, e, f, g, h, K( 0), x[ 0] );
  292. R( h, a, b, c, d, e, f, g, K( 1), x[ 1] );
  293. R( g, h, a, b, c, d, e, f, K( 2), x[ 2] );
  294. R( f, g, h, a, b, c, d, e, K( 3), x[ 3] );
  295. R( e, f, g, h, a, b, c, d, K( 4), x[ 4] );
  296. R( d, e, f, g, h, a, b, c, K( 5), x[ 5] );
  297. R( c, d, e, f, g, h, a, b, K( 6), x[ 6] );
  298. R( b, c, d, e, f, g, h, a, K( 7), x[ 7] );
  299. R( a, b, c, d, e, f, g, h, K( 8), x[ 8] );
  300. R( h, a, b, c, d, e, f, g, K( 9), x[ 9] );
  301. R( g, h, a, b, c, d, e, f, K(10), x[10] );
  302. R( f, g, h, a, b, c, d, e, K(11), x[11] );
  303. R( e, f, g, h, a, b, c, d, K(12), x[12] );
  304. R( d, e, f, g, h, a, b, c, K(13), x[13] );
  305. R( c, d, e, f, g, h, a, b, K(14), x[14] );
  306. R( b, c, d, e, f, g, h, a, K(15), x[15] );
  307. R( a, b, c, d, e, f, g, h, K(16), M(16) );
  308. R( h, a, b, c, d, e, f, g, K(17), M(17) );
  309. R( g, h, a, b, c, d, e, f, K(18), M(18) );
  310. R( f, g, h, a, b, c, d, e, K(19), M(19) );
  311. R( e, f, g, h, a, b, c, d, K(20), M(20) );
  312. R( d, e, f, g, h, a, b, c, K(21), M(21) );
  313. R( c, d, e, f, g, h, a, b, K(22), M(22) );
  314. R( b, c, d, e, f, g, h, a, K(23), M(23) );
  315. R( a, b, c, d, e, f, g, h, K(24), M(24) );
  316. R( h, a, b, c, d, e, f, g, K(25), M(25) );
  317. R( g, h, a, b, c, d, e, f, K(26), M(26) );
  318. R( f, g, h, a, b, c, d, e, K(27), M(27) );
  319. R( e, f, g, h, a, b, c, d, K(28), M(28) );
  320. R( d, e, f, g, h, a, b, c, K(29), M(29) );
  321. R( c, d, e, f, g, h, a, b, K(30), M(30) );
  322. R( b, c, d, e, f, g, h, a, K(31), M(31) );
  323. R( a, b, c, d, e, f, g, h, K(32), M(32) );
  324. R( h, a, b, c, d, e, f, g, K(33), M(33) );
  325. R( g, h, a, b, c, d, e, f, K(34), M(34) );
  326. R( f, g, h, a, b, c, d, e, K(35), M(35) );
  327. R( e, f, g, h, a, b, c, d, K(36), M(36) );
  328. R( d, e, f, g, h, a, b, c, K(37), M(37) );
  329. R( c, d, e, f, g, h, a, b, K(38), M(38) );
  330. R( b, c, d, e, f, g, h, a, K(39), M(39) );
  331. R( a, b, c, d, e, f, g, h, K(40), M(40) );
  332. R( h, a, b, c, d, e, f, g, K(41), M(41) );
  333. R( g, h, a, b, c, d, e, f, K(42), M(42) );
  334. R( f, g, h, a, b, c, d, e, K(43), M(43) );
  335. R( e, f, g, h, a, b, c, d, K(44), M(44) );
  336. R( d, e, f, g, h, a, b, c, K(45), M(45) );
  337. R( c, d, e, f, g, h, a, b, K(46), M(46) );
  338. R( b, c, d, e, f, g, h, a, K(47), M(47) );
  339. R( a, b, c, d, e, f, g, h, K(48), M(48) );
  340. R( h, a, b, c, d, e, f, g, K(49), M(49) );
  341. R( g, h, a, b, c, d, e, f, K(50), M(50) );
  342. R( f, g, h, a, b, c, d, e, K(51), M(51) );
  343. R( e, f, g, h, a, b, c, d, K(52), M(52) );
  344. R( d, e, f, g, h, a, b, c, K(53), M(53) );
  345. R( c, d, e, f, g, h, a, b, K(54), M(54) );
  346. R( b, c, d, e, f, g, h, a, K(55), M(55) );
  347. R( a, b, c, d, e, f, g, h, K(56), M(56) );
  348. R( h, a, b, c, d, e, f, g, K(57), M(57) );
  349. R( g, h, a, b, c, d, e, f, K(58), M(58) );
  350. R( f, g, h, a, b, c, d, e, K(59), M(59) );
  351. R( e, f, g, h, a, b, c, d, K(60), M(60) );
  352. R( d, e, f, g, h, a, b, c, K(61), M(61) );
  353. R( c, d, e, f, g, h, a, b, K(62), M(62) );
  354. R( b, c, d, e, f, g, h, a, K(63), M(63) );
  355. a = ctx->state[0] += a;
  356. b = ctx->state[1] += b;
  357. c = ctx->state[2] += c;
  358. d = ctx->state[3] += d;
  359. e = ctx->state[4] += e;
  360. f = ctx->state[5] += f;
  361. g = ctx->state[6] += g;
  362. h = ctx->state[7] += h;
  363. }
  364. }
  365. #endif
  366. /*
  367. * Hey Emacs!
  368. * Local Variables:
  369. * coding: utf-8
  370. * End:
  371. */