sha256.c 8.3 KB

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