hash_fd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Based on shasum from http://www.netsw.org/crypto/hash/
  3. * Majorly hacked up to use Dr Brian Gladman's sha1 code
  4. *
  5. * Copyright (C) 2003 Glenn L. McGrath
  6. * Copyright (C) 2003 Erik Andersen
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <byteswap.h>
  23. #include <endian.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "busybox.h"
  32. #ifdef CONFIG_SHA1SUM
  33. /*
  34. ---------------------------------------------------------------------------
  35. Begin Dr. Gladman's sha1 code
  36. ---------------------------------------------------------------------------
  37. */
  38. /*
  39. ---------------------------------------------------------------------------
  40. Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  41. All rights reserved.
  42. LICENSE TERMS
  43. The free distribution and use of this software in both source and binary
  44. form is allowed (with or without changes) provided that:
  45. 1. distributions of this source code include the above copyright
  46. notice, this list of conditions and the following disclaimer;
  47. 2. distributions in binary form include the above copyright
  48. notice, this list of conditions and the following disclaimer
  49. in the documentation and/or other associated materials;
  50. 3. the copyright holder's name is not used to endorse products
  51. built using this software without specific written permission.
  52. ALTERNATIVELY, provided that this notice is retained in full, this product
  53. may be distributed under the terms of the GNU General Public License (GPL),
  54. in which case the provisions of the GPL apply INSTEAD OF those given above.
  55. DISCLAIMER
  56. This software is provided 'as is' with no explicit or implied warranties
  57. in respect of its properties, including, but not limited to, correctness
  58. and/or fitness for purpose.
  59. ---------------------------------------------------------------------------
  60. Issue Date: 10/11/2002
  61. This is a byte oriented version of SHA1 that operates on arrays of bytes
  62. stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
  63. */
  64. # define SHA1_BLOCK_SIZE 64
  65. # define SHA1_DIGEST_SIZE 20
  66. # define SHA1_HASH_SIZE SHA1_DIGEST_SIZE
  67. # define SHA2_GOOD 0
  68. # define SHA2_BAD 1
  69. # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  70. # if __BYTE_ORDER == __BIG_ENDIAN
  71. # define swap_b32(x) (x)
  72. # elif defined(bswap_32)
  73. # define swap_b32(x) bswap_32(x)
  74. # else
  75. # define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00))
  76. # endif /* __BYTE_ORDER */
  77. # define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  78. /* reverse byte order in 32-bit words */
  79. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  80. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  81. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  82. /* A normal version as set out in the FIPS. This version uses */
  83. /* partial loop unrolling and is optimised for the Pentium 4 */
  84. # define rnd(f,k) \
  85. t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
  86. e = d; d = c; c = rotl32(b, 30); b = t
  87. /* type to hold the SHA1 context */
  88. struct sha1_ctx_t {
  89. uint32_t count[2];
  90. uint32_t hash[5];
  91. uint32_t wbuf[16];
  92. };
  93. static void sha1_compile(struct sha1_ctx_t *ctx)
  94. {
  95. uint32_t w[80], i, a, b, c, d, e, t;
  96. /* note that words are compiled from the buffer into 32-bit */
  97. /* words in big-endian order so an order reversal is needed */
  98. /* here on little endian machines */
  99. for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
  100. w[i] = swap_b32(ctx->wbuf[i]);
  101. for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
  102. w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  103. a = ctx->hash[0];
  104. b = ctx->hash[1];
  105. c = ctx->hash[2];
  106. d = ctx->hash[3];
  107. e = ctx->hash[4];
  108. for (i = 0; i < 20; ++i) {
  109. rnd(ch, 0x5a827999);
  110. }
  111. for (i = 20; i < 40; ++i) {
  112. rnd(parity, 0x6ed9eba1);
  113. }
  114. for (i = 40; i < 60; ++i) {
  115. rnd(maj, 0x8f1bbcdc);
  116. }
  117. for (i = 60; i < 80; ++i) {
  118. rnd(parity, 0xca62c1d6);
  119. }
  120. ctx->hash[0] += a;
  121. ctx->hash[1] += b;
  122. ctx->hash[2] += c;
  123. ctx->hash[3] += d;
  124. ctx->hash[4] += e;
  125. }
  126. static void sha1_begin(struct sha1_ctx_t *ctx)
  127. {
  128. ctx->count[0] = ctx->count[1] = 0;
  129. ctx->hash[0] = 0x67452301;
  130. ctx->hash[1] = 0xefcdab89;
  131. ctx->hash[2] = 0x98badcfe;
  132. ctx->hash[3] = 0x10325476;
  133. ctx->hash[4] = 0xc3d2e1f0;
  134. }
  135. /* SHA1 hash data in an array of bytes into hash buffer and call the */
  136. /* hash_compile function as required. */
  137. static void sha1_hash(const void *data, size_t len, void *ctx_v)
  138. {
  139. struct sha1_ctx_t *ctx = (struct sha1_ctx_t *) ctx_v;
  140. uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
  141. uint32_t freeb = SHA1_BLOCK_SIZE - pos;
  142. const unsigned char *sp = data;
  143. if ((ctx->count[0] += len) < len)
  144. ++(ctx->count[1]);
  145. while (len >= freeb) { /* tranfer whole blocks while possible */
  146. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
  147. sp += freeb;
  148. len -= freeb;
  149. freeb = SHA1_BLOCK_SIZE;
  150. pos = 0;
  151. sha1_compile(ctx);
  152. }
  153. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, len);
  154. }
  155. /* SHA1 Final padding and digest calculation */
  156. # if __BYTE_ORDER == __LITTLE_ENDIAN
  157. static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
  158. static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
  159. # else
  160. static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
  161. static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
  162. # endif /* __BYTE_ORDER */
  163. static void sha1_end(unsigned char hval[], struct sha1_ctx_t *ctx)
  164. {
  165. uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
  166. /* mask out the rest of any partial 32-bit word and then set */
  167. /* the next byte to 0x80. On big-endian machines any bytes in */
  168. /* the buffer will be at the top end of 32 bit words, on little */
  169. /* endian machines they will be at the bottom. Hence the AND */
  170. /* and OR masks above are reversed for little endian systems */
  171. ctx->wbuf[cnt >> 2] =
  172. (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
  173. /* we need 9 or more empty positions, one for the padding byte */
  174. /* (above) and eight for the length count. If there is not */
  175. /* enough space pad and empty the buffer */
  176. if (cnt > SHA1_BLOCK_SIZE - 9) {
  177. if (cnt < 60)
  178. ctx->wbuf[15] = 0;
  179. sha1_compile(ctx);
  180. cnt = 0;
  181. } else /* compute a word index for the empty buffer positions */
  182. cnt = (cnt >> 2) + 1;
  183. while (cnt < 14) /* and zero pad all but last two positions */
  184. ctx->wbuf[cnt++] = 0;
  185. /* assemble the eight byte counter in the buffer in big-endian */
  186. /* format */
  187. ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29));
  188. ctx->wbuf[15] = swap_b32(ctx->count[0] << 3);
  189. sha1_compile(ctx);
  190. /* extract the hash value as bytes in case the hash buffer is */
  191. /* misaligned for 32-bit words */
  192. for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  193. hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
  194. }
  195. /*
  196. ---------------------------------------------------------------------------
  197. End of Dr. Gladman's sha1 code
  198. ---------------------------------------------------------------------------
  199. */
  200. #endif /* CONFIG_SHA1 */
  201. #ifdef CONFIG_MD5SUM
  202. /*
  203. * md5sum.c - Compute MD5 checksum of files or strings according to the
  204. * definition of MD5 in RFC 1321 from April 1992.
  205. *
  206. * Copyright (C) 1995-1999 Free Software Foundation, Inc.
  207. * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  208. *
  209. *
  210. * June 29, 2001 Manuel Novoa III
  211. *
  212. * Added MD5SUM_SIZE_VS_SPEED configuration option.
  213. *
  214. * Current valid values, with data from my system for comparison, are:
  215. * (using uClibc and running on linux-2.4.4.tar.bz2)
  216. * user times (sec) text size (386)
  217. * 0 (fastest) 1.1 6144
  218. * 1 1.4 5392
  219. * 2 3.0 5088
  220. * 3 (smallest) 5.1 4912
  221. */
  222. # define MD5SUM_SIZE_VS_SPEED 2
  223. /* Handle endian-ness */
  224. # if __BYTE_ORDER == __LITTLE_ENDIAN
  225. # define SWAP(n) (n)
  226. # elif defined(bswap_32)
  227. # define SWAP(n) bswap_32(n)
  228. # else
  229. # define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24))
  230. # endif
  231. # if MD5SUM_SIZE_VS_SPEED == 0
  232. /* This array contains the bytes used to pad the buffer to the next
  233. 64-byte boundary. (RFC 1321, 3.1: Step 1) */
  234. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  235. # endif /* MD5SUM_SIZE_VS_SPEED == 0 */
  236. /* Structure to save state of computation between the single steps. */
  237. struct md5_ctx_t {
  238. uint32_t A;
  239. uint32_t B;
  240. uint32_t C;
  241. uint32_t D;
  242. uint32_t total[2];
  243. uint32_t buflen;
  244. char buffer[128];
  245. };
  246. /* Initialize structure containing state of computation.
  247. * (RFC 1321, 3.3: Step 3)
  248. */
  249. static void md5_begin(struct md5_ctx_t *ctx)
  250. {
  251. ctx->A = 0x67452301;
  252. ctx->B = 0xefcdab89;
  253. ctx->C = 0x98badcfe;
  254. ctx->D = 0x10325476;
  255. ctx->total[0] = ctx->total[1] = 0;
  256. ctx->buflen = 0;
  257. }
  258. /* These are the four functions used in the four steps of the MD5 algorithm
  259. * and defined in the RFC 1321. The first function is a little bit optimized
  260. * (as found in Colin Plumbs public domain implementation).
  261. * #define FF(b, c, d) ((b & c) | (~b & d))
  262. */
  263. # define FF(b, c, d) (d ^ (b & (c ^ d)))
  264. # define FG(b, c, d) FF (d, b, c)
  265. # define FH(b, c, d) (b ^ c ^ d)
  266. # define FI(b, c, d) (c ^ (b | ~d))
  267. /* Starting with the result of former calls of this function (or the
  268. * initialization function update the context for the next LEN bytes
  269. * starting at BUFFER.
  270. * It is necessary that LEN is a multiple of 64!!!
  271. */
  272. static void md5_hash_block(const void *buffer, size_t len, struct md5_ctx_t *ctx)
  273. {
  274. uint32_t correct_words[16];
  275. const uint32_t *words = buffer;
  276. size_t nwords = len / sizeof(uint32_t);
  277. const uint32_t *endp = words + nwords;
  278. # if MD5SUM_SIZE_VS_SPEED > 0
  279. static const uint32_t C_array[] = {
  280. /* round 1 */
  281. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  282. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  283. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  284. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  285. /* round 2 */
  286. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  287. 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
  288. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  289. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  290. /* round 3 */
  291. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  292. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  293. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  294. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  295. /* round 4 */
  296. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  297. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  298. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  299. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  300. };
  301. static const char P_array[] = {
  302. # if MD5SUM_SIZE_VS_SPEED > 1
  303. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
  304. # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
  305. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
  306. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
  307. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
  308. };
  309. # if MD5SUM_SIZE_VS_SPEED > 1
  310. static const char S_array[] = {
  311. 7, 12, 17, 22,
  312. 5, 9, 14, 20,
  313. 4, 11, 16, 23,
  314. 6, 10, 15, 21
  315. };
  316. # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
  317. # endif
  318. uint32_t A = ctx->A;
  319. uint32_t B = ctx->B;
  320. uint32_t C = ctx->C;
  321. uint32_t D = ctx->D;
  322. /* First increment the byte count. RFC 1321 specifies the possible
  323. length of the file up to 2^64 bits. Here we only compute the
  324. number of bytes. Do a double word increment. */
  325. ctx->total[0] += len;
  326. if (ctx->total[0] < len)
  327. ++ctx->total[1];
  328. /* Process all bytes in the buffer with 64 bytes in each round of
  329. the loop. */
  330. while (words < endp) {
  331. uint32_t *cwp = correct_words;
  332. uint32_t A_save = A;
  333. uint32_t B_save = B;
  334. uint32_t C_save = C;
  335. uint32_t D_save = D;
  336. # if MD5SUM_SIZE_VS_SPEED > 1
  337. # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  338. const uint32_t *pc;
  339. const char *pp;
  340. const char *ps;
  341. int i;
  342. uint32_t temp;
  343. for (i = 0; i < 16; i++) {
  344. cwp[i] = SWAP(words[i]);
  345. }
  346. words += 16;
  347. # if MD5SUM_SIZE_VS_SPEED > 2
  348. pc = C_array;
  349. pp = P_array;
  350. ps = S_array - 4;
  351. for (i = 0; i < 64; i++) {
  352. if ((i & 0x0f) == 0)
  353. ps += 4;
  354. temp = A;
  355. switch (i >> 4) {
  356. case 0:
  357. temp += FF(B, C, D);
  358. break;
  359. case 1:
  360. temp += FG(B, C, D);
  361. break;
  362. case 2:
  363. temp += FH(B, C, D);
  364. break;
  365. case 3:
  366. temp += FI(B, C, D);
  367. }
  368. temp += cwp[(int) (*pp++)] + *pc++;
  369. CYCLIC(temp, ps[i & 3]);
  370. temp += B;
  371. A = D;
  372. D = C;
  373. C = B;
  374. B = temp;
  375. }
  376. # else
  377. pc = C_array;
  378. pp = P_array;
  379. ps = S_array;
  380. for (i = 0; i < 16; i++) {
  381. temp = A + FF(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  382. CYCLIC(temp, ps[i & 3]);
  383. temp += B;
  384. A = D;
  385. D = C;
  386. C = B;
  387. B = temp;
  388. }
  389. ps += 4;
  390. for (i = 0; i < 16; i++) {
  391. temp = A + FG(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  392. CYCLIC(temp, ps[i & 3]);
  393. temp += B;
  394. A = D;
  395. D = C;
  396. C = B;
  397. B = temp;
  398. }
  399. ps += 4;
  400. for (i = 0; i < 16; i++) {
  401. temp = A + FH(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  402. CYCLIC(temp, ps[i & 3]);
  403. temp += B;
  404. A = D;
  405. D = C;
  406. C = B;
  407. B = temp;
  408. }
  409. ps += 4;
  410. for (i = 0; i < 16; i++) {
  411. temp = A + FI(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  412. CYCLIC(temp, ps[i & 3]);
  413. temp += B;
  414. A = D;
  415. D = C;
  416. C = B;
  417. B = temp;
  418. }
  419. # endif /* MD5SUM_SIZE_VS_SPEED > 2 */
  420. # else
  421. /* First round: using the given function, the context and a constant
  422. the next context is computed. Because the algorithms processing
  423. unit is a 32-bit word and it is determined to work on words in
  424. little endian byte order we perhaps have to change the byte order
  425. before the computation. To reduce the work for the next steps
  426. we store the swapped words in the array CORRECT_WORDS. */
  427. # define OP(a, b, c, d, s, T) \
  428. do \
  429. { \
  430. a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
  431. ++words; \
  432. CYCLIC (a, s); \
  433. a += b; \
  434. } \
  435. while (0)
  436. /* It is unfortunate that C does not provide an operator for
  437. cyclic rotation. Hope the C compiler is smart enough. */
  438. /* gcc 2.95.4 seems to be --aaronl */
  439. # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  440. /* Before we start, one word to the strange constants.
  441. They are defined in RFC 1321 as
  442. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  443. */
  444. # if MD5SUM_SIZE_VS_SPEED == 1
  445. const uint32_t *pc;
  446. const char *pp;
  447. int i;
  448. # endif /* MD5SUM_SIZE_VS_SPEED */
  449. /* Round 1. */
  450. # if MD5SUM_SIZE_VS_SPEED == 1
  451. pc = C_array;
  452. for (i = 0; i < 4; i++) {
  453. OP(A, B, C, D, 7, *pc++);
  454. OP(D, A, B, C, 12, *pc++);
  455. OP(C, D, A, B, 17, *pc++);
  456. OP(B, C, D, A, 22, *pc++);
  457. }
  458. # else
  459. OP(A, B, C, D, 7, 0xd76aa478);
  460. OP(D, A, B, C, 12, 0xe8c7b756);
  461. OP(C, D, A, B, 17, 0x242070db);
  462. OP(B, C, D, A, 22, 0xc1bdceee);
  463. OP(A, B, C, D, 7, 0xf57c0faf);
  464. OP(D, A, B, C, 12, 0x4787c62a);
  465. OP(C, D, A, B, 17, 0xa8304613);
  466. OP(B, C, D, A, 22, 0xfd469501);
  467. OP(A, B, C, D, 7, 0x698098d8);
  468. OP(D, A, B, C, 12, 0x8b44f7af);
  469. OP(C, D, A, B, 17, 0xffff5bb1);
  470. OP(B, C, D, A, 22, 0x895cd7be);
  471. OP(A, B, C, D, 7, 0x6b901122);
  472. OP(D, A, B, C, 12, 0xfd987193);
  473. OP(C, D, A, B, 17, 0xa679438e);
  474. OP(B, C, D, A, 22, 0x49b40821);
  475. # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
  476. /* For the second to fourth round we have the possibly swapped words
  477. in CORRECT_WORDS. Redefine the macro to take an additional first
  478. argument specifying the function to use. */
  479. # undef OP
  480. # define OP(f, a, b, c, d, k, s, T) \
  481. do \
  482. { \
  483. a += f (b, c, d) + correct_words[k] + T; \
  484. CYCLIC (a, s); \
  485. a += b; \
  486. } \
  487. while (0)
  488. /* Round 2. */
  489. # if MD5SUM_SIZE_VS_SPEED == 1
  490. pp = P_array;
  491. for (i = 0; i < 4; i++) {
  492. OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++);
  493. OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++);
  494. OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++);
  495. OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++);
  496. }
  497. # else
  498. OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
  499. OP(FG, D, A, B, C, 6, 9, 0xc040b340);
  500. OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
  501. OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  502. OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
  503. OP(FG, D, A, B, C, 10, 9, 0x02441453);
  504. OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
  505. OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  506. OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
  507. OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
  508. OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
  509. OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
  510. OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
  511. OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  512. OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
  513. OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  514. # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
  515. /* Round 3. */
  516. # if MD5SUM_SIZE_VS_SPEED == 1
  517. for (i = 0; i < 4; i++) {
  518. OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++);
  519. OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++);
  520. OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++);
  521. OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++);
  522. }
  523. # else
  524. OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
  525. OP(FH, D, A, B, C, 8, 11, 0x8771f681);
  526. OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
  527. OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
  528. OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
  529. OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  530. OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  531. OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
  532. OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
  533. OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
  534. OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
  535. OP(FH, B, C, D, A, 6, 23, 0x04881d05);
  536. OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
  537. OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
  538. OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  539. OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
  540. # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
  541. /* Round 4. */
  542. # if MD5SUM_SIZE_VS_SPEED == 1
  543. for (i = 0; i < 4; i++) {
  544. OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++);
  545. OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++);
  546. OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++);
  547. OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++);
  548. }
  549. # else
  550. OP(FI, A, B, C, D, 0, 6, 0xf4292244);
  551. OP(FI, D, A, B, C, 7, 10, 0x432aff97);
  552. OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
  553. OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
  554. OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
  555. OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  556. OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
  557. OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
  558. OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  559. OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  560. OP(FI, C, D, A, B, 6, 15, 0xa3014314);
  561. OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
  562. OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
  563. OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
  564. OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  565. OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
  566. # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
  567. # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
  568. /* Add the starting values of the context. */
  569. A += A_save;
  570. B += B_save;
  571. C += C_save;
  572. D += D_save;
  573. }
  574. /* Put checksum in context given as argument. */
  575. ctx->A = A;
  576. ctx->B = B;
  577. ctx->C = C;
  578. ctx->D = D;
  579. }
  580. /* Starting with the result of former calls of this function (or the
  581. * initialization function update the context for the next LEN bytes
  582. * starting at BUFFER.
  583. * It is NOT required that LEN is a multiple of 64.
  584. */
  585. static void md5_hash_bytes(const void *buffer, size_t len, struct md5_ctx_t *ctx)
  586. {
  587. /* When we already have some bits in our internal buffer concatenate
  588. both inputs first. */
  589. if (ctx->buflen != 0) {
  590. size_t left_over = ctx->buflen;
  591. size_t add = 128 - left_over > len ? len : 128 - left_over;
  592. memcpy(&ctx->buffer[left_over], buffer, add);
  593. ctx->buflen += add;
  594. if (left_over + add > 64) {
  595. md5_hash_block(ctx->buffer, (left_over + add) & ~63, ctx);
  596. /* The regions in the following copy operation cannot overlap. */
  597. memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
  598. (left_over + add) & 63);
  599. ctx->buflen = (left_over + add) & 63;
  600. }
  601. buffer = (const char *) buffer + add;
  602. len -= add;
  603. }
  604. /* Process available complete blocks. */
  605. if (len > 64) {
  606. md5_hash_block(buffer, len & ~63, ctx);
  607. buffer = (const char *) buffer + (len & ~63);
  608. len &= 63;
  609. }
  610. /* Move remaining bytes in internal buffer. */
  611. if (len > 0) {
  612. memcpy(ctx->buffer, buffer, len);
  613. ctx->buflen = len;
  614. }
  615. }
  616. static void md5_hash(const void *buffer, size_t length, void *md5_ctx)
  617. {
  618. if (length % 64 == 0) {
  619. md5_hash_block(buffer, length, md5_ctx);
  620. } else {
  621. md5_hash_bytes(buffer, length, md5_ctx);
  622. }
  623. }
  624. /* Process the remaining bytes in the buffer and put result from CTX
  625. * in first 16 bytes following RESBUF. The result is always in little
  626. * endian byte order, so that a byte-wise output yields to the wanted
  627. * ASCII representation of the message digest.
  628. *
  629. * IMPORTANT: On some systems it is required that RESBUF is correctly
  630. * aligned for a 32 bits value.
  631. */
  632. static void *md5_end(void *resbuf, struct md5_ctx_t *ctx)
  633. {
  634. /* Take yet unprocessed bytes into account. */
  635. uint32_t bytes = ctx->buflen;
  636. size_t pad;
  637. /* Now count remaining bytes. */
  638. ctx->total[0] += bytes;
  639. if (ctx->total[0] < bytes)
  640. ++ctx->total[1];
  641. pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
  642. # if MD5SUM_SIZE_VS_SPEED > 0
  643. memset(&ctx->buffer[bytes], 0, pad);
  644. ctx->buffer[bytes] = 0x80;
  645. # else
  646. memcpy(&ctx->buffer[bytes], fillbuf, pad);
  647. # endif /* MD5SUM_SIZE_VS_SPEED > 0 */
  648. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  649. *(uint32_t *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
  650. *(uint32_t *) & ctx->buffer[bytes + pad + 4] =
  651. SWAP(((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
  652. /* Process last bytes. */
  653. md5_hash_block(ctx->buffer, bytes + pad + 8, ctx);
  654. /* Put result from CTX in first 16 bytes following RESBUF. The result is
  655. * always in little endian byte order, so that a byte-wise output yields
  656. * to the wanted ASCII representation of the message digest.
  657. *
  658. * IMPORTANT: On some systems it is required that RESBUF is correctly
  659. * aligned for a 32 bits value.
  660. */
  661. ((uint32_t *) resbuf)[0] = SWAP(ctx->A);
  662. ((uint32_t *) resbuf)[1] = SWAP(ctx->B);
  663. ((uint32_t *) resbuf)[2] = SWAP(ctx->C);
  664. ((uint32_t *) resbuf)[3] = SWAP(ctx->D);
  665. return resbuf;
  666. }
  667. #endif /* CONFIG_MD5SUM */
  668. extern int hash_fd(int src_fd, const size_t size, const uint8_t hash_algo,
  669. uint8_t * hashval)
  670. {
  671. int result = EXIT_SUCCESS;
  672. // size_t hashed_count = 0;
  673. size_t blocksize = 0;
  674. size_t remaining = size;
  675. unsigned char *buffer = NULL;
  676. void (*hash_fn_ptr)(const void *, size_t, void *) = NULL;
  677. void *cx = NULL;
  678. #ifdef CONFIG_SHA1SUM
  679. struct sha1_ctx_t sha1_cx;
  680. #endif
  681. #ifdef CONFIG_MD5SUM
  682. struct md5_ctx_t md5_cx;
  683. #endif
  684. #ifdef CONFIG_SHA1SUM
  685. if (hash_algo == HASH_SHA1) {
  686. /* Ensure that BLOCKSIZE is a multiple of 64. */
  687. blocksize = 65536;
  688. buffer = xmalloc(blocksize);
  689. hash_fn_ptr = sha1_hash;
  690. cx = &sha1_cx;
  691. }
  692. #endif
  693. #ifdef CONFIG_MD5SUM
  694. if (hash_algo == HASH_MD5) {
  695. blocksize = 4096;
  696. buffer = xmalloc(blocksize + 72);
  697. hash_fn_ptr = md5_hash;
  698. cx = &md5_cx;
  699. }
  700. #endif
  701. /* Initialize the computation context. */
  702. #ifdef CONFIG_SHA1SUM
  703. if (hash_algo == HASH_SHA1) {
  704. sha1_begin(&sha1_cx);
  705. }
  706. #endif
  707. #ifdef CONFIG_MD5SUM
  708. if (hash_algo == HASH_MD5) {
  709. md5_begin(&md5_cx);
  710. }
  711. #endif
  712. /* Iterate over full file contents. */
  713. while ((remaining == (size_t) -1) || (remaining > 0)) {
  714. size_t read_try;
  715. ssize_t read_got;
  716. if (remaining > blocksize) {
  717. read_try = blocksize;
  718. } else {
  719. read_try = remaining;
  720. }
  721. read_got = bb_full_read(src_fd, buffer, read_try);
  722. if (read_got < 1) {
  723. /* count == 0 means short read
  724. * count == -1 means read error */
  725. result = read_got - 1;
  726. break;
  727. }
  728. if (remaining != (size_t) -1) {
  729. remaining -= read_got;
  730. }
  731. /* Process buffer */
  732. hash_fn_ptr(buffer, read_got, cx);
  733. }
  734. /* Finalize and write the hash into our buffer. */
  735. #ifdef CONFIG_SHA1SUM
  736. if (hash_algo == HASH_SHA1) {
  737. sha1_end(hashval, &sha1_cx);
  738. }
  739. #endif
  740. #ifdef CONFIG_MD5SUM
  741. if (hash_algo == HASH_MD5) {
  742. md5_end(hashval, &md5_cx);
  743. }
  744. #endif
  745. free(buffer);
  746. return result;
  747. }