sha1.c 12 KB

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