sha1.c 12 KB

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