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