sha1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* sha1.c - Functions to compute SHA1 message digest of files or
  2. memory blocks according to the NIST specification FIPS-180-1.
  3. Copyright (C) 2000-2001, 2003-2006, 2008-2011 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This program 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* Written by Scott G. Miller
  16. Credits:
  17. Robert Klep <robert@ilse.nl> -- Expansion function fix
  18. */
  19. #include <config.h>
  20. #include "sha1.h"
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #if USE_UNLOCKED_IO
  25. # include "unlocked-io.h"
  26. #endif
  27. #ifdef WORDS_BIGENDIAN
  28. # define SWAP(n) (n)
  29. #else
  30. # define SWAP(n) \
  31. (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
  32. #endif
  33. #define BLOCKSIZE 32768
  34. #if BLOCKSIZE % 64 != 0
  35. # error "invalid BLOCKSIZE"
  36. #endif
  37. /* This array contains the bytes used to pad the buffer to the next
  38. 64-byte boundary. (RFC 1321, 3.1: Step 1) */
  39. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  40. /* Take a pointer to a 160 bit block of data (five 32 bit ints) and
  41. initialize it to the start constants of the SHA1 algorithm. This
  42. must be called before using hash in the call to sha1_hash. */
  43. void
  44. sha1_init_ctx (struct sha1_ctx *ctx)
  45. {
  46. ctx->A = 0x67452301;
  47. ctx->B = 0xefcdab89;
  48. ctx->C = 0x98badcfe;
  49. ctx->D = 0x10325476;
  50. ctx->E = 0xc3d2e1f0;
  51. ctx->total[0] = ctx->total[1] = 0;
  52. ctx->buflen = 0;
  53. }
  54. /* Copy the 4 byte value from v into the memory location pointed to by *cp,
  55. If your architecture allows unaligned access this is equivalent to
  56. * (uint32_t *) cp = v */
  57. static inline void
  58. set_uint32 (char *cp, uint32_t v)
  59. {
  60. memcpy (cp, &v, sizeof v);
  61. }
  62. /* Put result from CTX in first 20 bytes following RESBUF. The result
  63. must be in little endian byte order. */
  64. void *
  65. sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
  66. {
  67. char *r = resbuf;
  68. set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
  69. set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
  70. set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
  71. set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
  72. set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
  73. return resbuf;
  74. }
  75. /* Process the remaining bytes in the internal buffer and the usual
  76. prolog according to the standard and write the result to RESBUF. */
  77. void *
  78. sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
  79. {
  80. /* Take yet unprocessed bytes into account. */
  81. uint32_t bytes = ctx->buflen;
  82. size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
  83. /* Now count remaining bytes. */
  84. ctx->total[0] += bytes;
  85. if (ctx->total[0] < bytes)
  86. ++ctx->total[1];
  87. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  88. ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
  89. ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
  90. memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
  91. /* Process last bytes. */
  92. sha1_process_block (ctx->buffer, size * 4, ctx);
  93. return sha1_read_ctx (ctx, resbuf);
  94. }
  95. /* Compute SHA1 message digest for bytes read from STREAM. The
  96. resulting message digest number will be written into the 16 bytes
  97. beginning at RESBLOCK. */
  98. int
  99. sha1_stream (FILE *stream, void *resblock)
  100. {
  101. struct sha1_ctx ctx;
  102. size_t sum;
  103. char *buffer = malloc (BLOCKSIZE + 72);
  104. if (!buffer)
  105. return 1;
  106. /* Initialize the computation context. */
  107. sha1_init_ctx (&ctx);
  108. /* Iterate over full file contents. */
  109. while (1)
  110. {
  111. /* We read the file in blocks of BLOCKSIZE bytes. One call of the
  112. computation function processes the whole buffer so that with the
  113. next round of the loop another block can be read. */
  114. size_t n;
  115. sum = 0;
  116. /* Read block. Take care for partial reads. */
  117. while (1)
  118. {
  119. n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
  120. sum += n;
  121. if (sum == BLOCKSIZE)
  122. break;
  123. if (n == 0)
  124. {
  125. /* Check for the error flag IFF N == 0, so that we don't
  126. exit the loop after a partial read due to e.g., EAGAIN
  127. or EWOULDBLOCK. */
  128. if (ferror (stream))
  129. {
  130. free (buffer);
  131. return 1;
  132. }
  133. goto process_partial_block;
  134. }
  135. /* We've read at least one byte, so ignore errors. But always
  136. check for EOF, since feof may be true even though N > 0.
  137. Otherwise, we could end up calling fread after EOF. */
  138. if (feof (stream))
  139. goto process_partial_block;
  140. }
  141. /* Process buffer with BLOCKSIZE bytes. Note that
  142. BLOCKSIZE % 64 == 0
  143. */
  144. sha1_process_block (buffer, BLOCKSIZE, &ctx);
  145. }
  146. process_partial_block:;
  147. /* Process any remaining bytes. */
  148. if (sum > 0)
  149. sha1_process_bytes (buffer, sum, &ctx);
  150. /* Construct result in desired memory. */
  151. sha1_finish_ctx (&ctx, resblock);
  152. free (buffer);
  153. return 0;
  154. }
  155. /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
  156. result is always in little endian byte order, so that a byte-wise
  157. output yields to the wanted ASCII representation of the message
  158. digest. */
  159. void *
  160. sha1_buffer (const char *buffer, size_t len, void *resblock)
  161. {
  162. struct sha1_ctx ctx;
  163. /* Initialize the computation context. */
  164. sha1_init_ctx (&ctx);
  165. /* Process whole buffer but last len % 64 bytes. */
  166. sha1_process_bytes (buffer, len, &ctx);
  167. /* Put result in desired memory area. */
  168. return sha1_finish_ctx (&ctx, resblock);
  169. }
  170. void
  171. sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
  172. {
  173. /* When we already have some bits in our internal buffer concatenate
  174. both inputs first. */
  175. if (ctx->buflen != 0)
  176. {
  177. size_t left_over = ctx->buflen;
  178. size_t add = 128 - left_over > len ? len : 128 - left_over;
  179. memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
  180. ctx->buflen += add;
  181. if (ctx->buflen > 64)
  182. {
  183. sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
  184. ctx->buflen &= 63;
  185. /* The regions in the following copy operation cannot overlap. */
  186. memcpy (ctx->buffer,
  187. &((char *) ctx->buffer)[(left_over + add) & ~63],
  188. ctx->buflen);
  189. }
  190. buffer = (const char *) buffer + add;
  191. len -= add;
  192. }
  193. /* Process available complete blocks. */
  194. if (len >= 64)
  195. {
  196. #if !_STRING_ARCH_unaligned
  197. # define alignof(type) offsetof (struct { char c; type x; }, x)
  198. # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
  199. if (UNALIGNED_P (buffer))
  200. while (len > 64)
  201. {
  202. sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
  203. buffer = (const char *) buffer + 64;
  204. len -= 64;
  205. }
  206. else
  207. #endif
  208. {
  209. sha1_process_block (buffer, len & ~63, ctx);
  210. buffer = (const char *) buffer + (len & ~63);
  211. len &= 63;
  212. }
  213. }
  214. /* Move remaining bytes in internal buffer. */
  215. if (len > 0)
  216. {
  217. size_t left_over = ctx->buflen;
  218. memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
  219. left_over += len;
  220. if (left_over >= 64)
  221. {
  222. sha1_process_block (ctx->buffer, 64, ctx);
  223. left_over -= 64;
  224. memcpy (ctx->buffer, &ctx->buffer[16], left_over);
  225. }
  226. ctx->buflen = left_over;
  227. }
  228. }
  229. /* --- Code below is the primary difference between md5.c and sha1.c --- */
  230. /* SHA1 round constants */
  231. #define K1 0x5a827999
  232. #define K2 0x6ed9eba1
  233. #define K3 0x8f1bbcdc
  234. #define K4 0xca62c1d6
  235. /* Round functions. Note that F2 is the same as F4. */
  236. #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
  237. #define F2(B,C,D) (B ^ C ^ D)
  238. #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
  239. #define F4(B,C,D) (B ^ C ^ D)
  240. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  241. It is assumed that LEN % 64 == 0.
  242. Most of this code comes from GnuPG's cipher/sha1.c. */
  243. void
  244. sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
  245. {
  246. const uint32_t *words = buffer;
  247. size_t nwords = len / sizeof (uint32_t);
  248. const uint32_t *endp = words + nwords;
  249. uint32_t x[16];
  250. uint32_t a = ctx->A;
  251. uint32_t b = ctx->B;
  252. uint32_t c = ctx->C;
  253. uint32_t d = ctx->D;
  254. uint32_t e = ctx->E;
  255. /* First increment the byte count. RFC 1321 specifies the possible
  256. length of the file up to 2^64 bits. Here we only compute the
  257. number of bytes. Do a double word increment. */
  258. ctx->total[0] += len;
  259. if (ctx->total[0] < len)
  260. ++ctx->total[1];
  261. #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
  262. #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
  263. ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
  264. , (x[I&0x0f] = rol(tm, 1)) )
  265. #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
  266. + F( B, C, D ) \
  267. + K \
  268. + M; \
  269. B = rol( B, 30 ); \
  270. } while(0)
  271. while (words < endp)
  272. {
  273. uint32_t tm;
  274. int t;
  275. for (t = 0; t < 16; t++)
  276. {
  277. x[t] = SWAP (*words);
  278. words++;
  279. }
  280. R( a, b, c, d, e, F1, K1, x[ 0] );
  281. R( e, a, b, c, d, F1, K1, x[ 1] );
  282. R( d, e, a, b, c, F1, K1, x[ 2] );
  283. R( c, d, e, a, b, F1, K1, x[ 3] );
  284. R( b, c, d, e, a, F1, K1, x[ 4] );
  285. R( a, b, c, d, e, F1, K1, x[ 5] );
  286. R( e, a, b, c, d, F1, K1, x[ 6] );
  287. R( d, e, a, b, c, F1, K1, x[ 7] );
  288. R( c, d, e, a, b, F1, K1, x[ 8] );
  289. R( b, c, d, e, a, F1, K1, x[ 9] );
  290. R( a, b, c, d, e, F1, K1, x[10] );
  291. R( e, a, b, c, d, F1, K1, x[11] );
  292. R( d, e, a, b, c, F1, K1, x[12] );
  293. R( c, d, e, a, b, F1, K1, x[13] );
  294. R( b, c, d, e, a, F1, K1, x[14] );
  295. R( a, b, c, d, e, F1, K1, x[15] );
  296. R( e, a, b, c, d, F1, K1, M(16) );
  297. R( d, e, a, b, c, F1, K1, M(17) );
  298. R( c, d, e, a, b, F1, K1, M(18) );
  299. R( b, c, d, e, a, F1, K1, M(19) );
  300. R( a, b, c, d, e, F2, K2, M(20) );
  301. R( e, a, b, c, d, F2, K2, M(21) );
  302. R( d, e, a, b, c, F2, K2, M(22) );
  303. R( c, d, e, a, b, F2, K2, M(23) );
  304. R( b, c, d, e, a, F2, K2, M(24) );
  305. R( a, b, c, d, e, F2, K2, M(25) );
  306. R( e, a, b, c, d, F2, K2, M(26) );
  307. R( d, e, a, b, c, F2, K2, M(27) );
  308. R( c, d, e, a, b, F2, K2, M(28) );
  309. R( b, c, d, e, a, F2, K2, M(29) );
  310. R( a, b, c, d, e, F2, K2, M(30) );
  311. R( e, a, b, c, d, F2, K2, M(31) );
  312. R( d, e, a, b, c, F2, K2, M(32) );
  313. R( c, d, e, a, b, F2, K2, M(33) );
  314. R( b, c, d, e, a, F2, K2, M(34) );
  315. R( a, b, c, d, e, F2, K2, M(35) );
  316. R( e, a, b, c, d, F2, K2, M(36) );
  317. R( d, e, a, b, c, F2, K2, M(37) );
  318. R( c, d, e, a, b, F2, K2, M(38) );
  319. R( b, c, d, e, a, F2, K2, M(39) );
  320. R( a, b, c, d, e, F3, K3, M(40) );
  321. R( e, a, b, c, d, F3, K3, M(41) );
  322. R( d, e, a, b, c, F3, K3, M(42) );
  323. R( c, d, e, a, b, F3, K3, M(43) );
  324. R( b, c, d, e, a, F3, K3, M(44) );
  325. R( a, b, c, d, e, F3, K3, M(45) );
  326. R( e, a, b, c, d, F3, K3, M(46) );
  327. R( d, e, a, b, c, F3, K3, M(47) );
  328. R( c, d, e, a, b, F3, K3, M(48) );
  329. R( b, c, d, e, a, F3, K3, M(49) );
  330. R( a, b, c, d, e, F3, K3, M(50) );
  331. R( e, a, b, c, d, F3, K3, M(51) );
  332. R( d, e, a, b, c, F3, K3, M(52) );
  333. R( c, d, e, a, b, F3, K3, M(53) );
  334. R( b, c, d, e, a, F3, K3, M(54) );
  335. R( a, b, c, d, e, F3, K3, M(55) );
  336. R( e, a, b, c, d, F3, K3, M(56) );
  337. R( d, e, a, b, c, F3, K3, M(57) );
  338. R( c, d, e, a, b, F3, K3, M(58) );
  339. R( b, c, d, e, a, F3, K3, M(59) );
  340. R( a, b, c, d, e, F4, K4, M(60) );
  341. R( e, a, b, c, d, F4, K4, M(61) );
  342. R( d, e, a, b, c, F4, K4, M(62) );
  343. R( c, d, e, a, b, F4, K4, M(63) );
  344. R( b, c, d, e, a, F4, K4, M(64) );
  345. R( a, b, c, d, e, F4, K4, M(65) );
  346. R( e, a, b, c, d, F4, K4, M(66) );
  347. R( d, e, a, b, c, F4, K4, M(67) );
  348. R( c, d, e, a, b, F4, K4, M(68) );
  349. R( b, c, d, e, a, F4, K4, M(69) );
  350. R( a, b, c, d, e, F4, K4, M(70) );
  351. R( e, a, b, c, d, F4, K4, M(71) );
  352. R( d, e, a, b, c, F4, K4, M(72) );
  353. R( c, d, e, a, b, F4, K4, M(73) );
  354. R( b, c, d, e, a, F4, K4, M(74) );
  355. R( a, b, c, d, e, F4, K4, M(75) );
  356. R( e, a, b, c, d, F4, K4, M(76) );
  357. R( d, e, a, b, c, F4, K4, M(77) );
  358. R( c, d, e, a, b, F4, K4, M(78) );
  359. R( b, c, d, e, a, F4, K4, M(79) );
  360. a = ctx->A += a;
  361. b = ctx->B += b;
  362. c = ctx->C += c;
  363. d = ctx->D += d;
  364. e = ctx->E += e;
  365. }
  366. }