sha1.c 13 KB

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