sha256.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Functions to compute SHA256 message digest of files or memory blocks.
  2. according to the definition of SHA256 in FIPS 180-2.
  3. Copyright (C) 2007 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* Written by Ulrich Drepper <drepper@redhat.com>, 2007. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <endian.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include "sha256.h"
  26. #if __BYTE_ORDER == __LITTLE_ENDIAN
  27. # ifdef _LIBC
  28. # include <byteswap.h>
  29. # define SWAP(n) bswap_32 (n)
  30. # else
  31. # define SWAP(n) \
  32. (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
  33. # endif
  34. #else
  35. # define SWAP(n) (n)
  36. #endif
  37. /* This array contains the bytes used to pad the buffer to the next
  38. 64-byte boundary. (FIPS 180-2:5.1.1) */
  39. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  40. /* Constants for SHA256 from FIPS 180-2:4.2.2. */
  41. static const uint32_t K[64] =
  42. {
  43. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  44. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  45. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  46. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  47. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  48. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  49. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  50. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  51. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  52. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  53. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  54. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  55. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  56. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  57. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  58. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  59. };
  60. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  61. It is assumed that LEN % 64 == 0. */
  62. static void
  63. sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
  64. {
  65. const uint32_t *words = buffer;
  66. size_t nwords = len / sizeof (uint32_t);
  67. uint32_t a = ctx->H[0];
  68. uint32_t b = ctx->H[1];
  69. uint32_t c = ctx->H[2];
  70. uint32_t d = ctx->H[3];
  71. uint32_t e = ctx->H[4];
  72. uint32_t f = ctx->H[5];
  73. uint32_t g = ctx->H[6];
  74. uint32_t h = ctx->H[7];
  75. /* First increment the byte count. FIPS 180-2 specifies the possible
  76. length of the file up to 2^64 bits. Here we only compute the
  77. number of bytes. Do a double word increment. */
  78. ctx->total[0] += len;
  79. if (ctx->total[0] < len)
  80. ++ctx->total[1];
  81. /* Process all bytes in the buffer with 64 bytes in each round of
  82. the loop. */
  83. while (nwords > 0)
  84. {
  85. uint32_t W[64];
  86. uint32_t a_save = a;
  87. uint32_t b_save = b;
  88. uint32_t c_save = c;
  89. uint32_t d_save = d;
  90. uint32_t e_save = e;
  91. uint32_t f_save = f;
  92. uint32_t g_save = g;
  93. uint32_t h_save = h;
  94. /* Operators defined in FIPS 180-2:4.1.2. */
  95. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  96. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  97. #define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
  98. #define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
  99. #define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
  100. #define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
  101. /* It is unfortunate that C does not provide an operator for
  102. cyclic rotation. Hope the C compiler is smart enough. */
  103. #define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
  104. /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
  105. for (unsigned int t = 0; t < 16; ++t)
  106. {
  107. W[t] = SWAP (*words);
  108. ++words;
  109. }
  110. for (unsigned int t = 16; t < 64; ++t)
  111. W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
  112. /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
  113. for (unsigned int t = 0; t < 64; ++t)
  114. {
  115. uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
  116. uint32_t T2 = S0 (a) + Maj (a, b, c);
  117. h = g;
  118. g = f;
  119. f = e;
  120. e = d + T1;
  121. d = c;
  122. c = b;
  123. b = a;
  124. a = T1 + T2;
  125. }
  126. /* Add the starting values of the context according to FIPS 180-2:6.2.2
  127. step 4. */
  128. a += a_save;
  129. b += b_save;
  130. c += c_save;
  131. d += d_save;
  132. e += e_save;
  133. f += f_save;
  134. g += g_save;
  135. h += h_save;
  136. /* Prepare for the next round. */
  137. nwords -= 16;
  138. }
  139. /* Put checksum in context given as argument. */
  140. ctx->H[0] = a;
  141. ctx->H[1] = b;
  142. ctx->H[2] = c;
  143. ctx->H[3] = d;
  144. ctx->H[4] = e;
  145. ctx->H[5] = f;
  146. ctx->H[6] = g;
  147. ctx->H[7] = h;
  148. }
  149. /* Initialize structure containing state of computation.
  150. (FIPS 180-2:5.3.2) */
  151. void
  152. __sha256_init_ctx (struct sha256_ctx *ctx)
  153. {
  154. ctx->H[0] = 0x6a09e667;
  155. ctx->H[1] = 0xbb67ae85;
  156. ctx->H[2] = 0x3c6ef372;
  157. ctx->H[3] = 0xa54ff53a;
  158. ctx->H[4] = 0x510e527f;
  159. ctx->H[5] = 0x9b05688c;
  160. ctx->H[6] = 0x1f83d9ab;
  161. ctx->H[7] = 0x5be0cd19;
  162. ctx->total[0] = ctx->total[1] = 0;
  163. ctx->buflen = 0;
  164. }
  165. /* Process the remaining bytes in the internal buffer and the usual
  166. prolog according to the standard and write the result to RESBUF.
  167. IMPORTANT: On some systems it is required that RESBUF is correctly
  168. aligned for a 32 bits value. */
  169. void *
  170. __sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
  171. {
  172. /* Take yet unprocessed bytes into account. */
  173. uint32_t bytes = ctx->buflen;
  174. size_t pad;
  175. /* Now count remaining bytes. */
  176. ctx->total[0] += bytes;
  177. if (ctx->total[0] < bytes)
  178. ++ctx->total[1];
  179. pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
  180. memcpy (&ctx->buffer[bytes], fillbuf, pad);
  181. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  182. *(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
  183. *(uint32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
  184. (ctx->total[0] >> 29));
  185. /* Process last bytes. */
  186. sha256_process_block (ctx->buffer, bytes + pad + 8, ctx);
  187. /* Put result from CTX in first 32 bytes following RESBUF. */
  188. for (unsigned int i = 0; i < 8; ++i)
  189. ((uint32_t *) resbuf)[i] = SWAP (ctx->H[i]);
  190. return resbuf;
  191. }
  192. void
  193. __sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
  194. {
  195. /* When we already have some bits in our internal buffer concatenate
  196. both inputs first. */
  197. if (ctx->buflen != 0)
  198. {
  199. size_t left_over = ctx->buflen;
  200. size_t add = 128 - left_over > len ? len : 128 - left_over;
  201. memcpy (&ctx->buffer[left_over], buffer, add);
  202. ctx->buflen += add;
  203. if (ctx->buflen > 64)
  204. {
  205. sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
  206. ctx->buflen &= 63;
  207. /* The regions in the following copy operation cannot overlap. */
  208. memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
  209. ctx->buflen);
  210. }
  211. buffer = (const char *) buffer + add;
  212. len -= add;
  213. }
  214. /* Process available complete blocks. */
  215. if (len >= 64)
  216. {
  217. #if __GNUC__ >= 2
  218. # define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
  219. #else
  220. # define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
  221. #endif
  222. if (UNALIGNED_P (buffer))
  223. while (len > 64)
  224. {
  225. sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
  226. buffer = (const char *) buffer + 64;
  227. len -= 64;
  228. }
  229. else
  230. {
  231. sha256_process_block (buffer, len & ~63, ctx);
  232. buffer = (const char *) buffer + (len & ~63);
  233. len &= 63;
  234. }
  235. }
  236. /* Move remaining bytes into internal buffer. */
  237. if (len > 0)
  238. {
  239. size_t left_over = ctx->buflen;
  240. memcpy (&ctx->buffer[left_over], buffer, len);
  241. left_over += len;
  242. if (left_over >= 64)
  243. {
  244. sha256_process_block (ctx->buffer, 64, ctx);
  245. left_over -= 64;
  246. memcpy (ctx->buffer, &ctx->buffer[64], left_over);
  247. }
  248. ctx->buflen = left_over;
  249. }
  250. }