2 * Based on shasum from http://www.netsw.org/crypto/hash/
3 * Majorly hacked up to use Dr Brian Gladman's sha1 code
5 * Copyright (C) 2003 Glenn L. McGrath
6 * Copyright (C) 2003 Erik Andersen
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
26 ---------------------------------------------------------------------------
27 Begin Dr. Gladman's sha1 code
28 ---------------------------------------------------------------------------
32 ---------------------------------------------------------------------------
33 Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
38 The free distribution and use of this software in both source and binary
39 form is allowed (with or without changes) provided that:
41 1. distributions of this source code include the above copyright
42 notice, this list of conditions and the following disclaimer;
44 2. distributions in binary form include the above copyright
45 notice, this list of conditions and the following disclaimer
46 in the documentation and/or other associated materials;
48 3. the copyright holder's name is not used to endorse products
49 built using this software without specific written permission.
51 ALTERNATIVELY, provided that this notice is retained in full, this product
52 may be distributed under the terms of the GNU General Public License (GPL),
53 in which case the provisions of the GPL apply INSTEAD OF those given above.
57 This software is provided 'as is' with no explicit or implied warranties
58 in respect of its properties, including, but not limited to, correctness
59 and/or fitness for purpose.
60 ---------------------------------------------------------------------------
61 Issue Date: 10/11/2002
63 This is a byte oriented version of SHA1 that operates on arrays of bytes
64 stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
67 # define SHA1_BLOCK_SIZE 64
68 # define SHA1_DIGEST_SIZE 20
69 # define SHA1_HASH_SIZE SHA1_DIGEST_SIZE
73 # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
75 # if __BYTE_ORDER == __BIG_ENDIAN
76 # define swap_b32(x) (x)
77 # elif defined(bswap_32)
78 # define swap_b32(x) bswap_32(x)
80 # define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00))
81 # endif /* __BYTE_ORDER */
83 # define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
85 /* reverse byte order in 32-bit words */
86 #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
87 #define parity(x,y,z) ((x) ^ (y) ^ (z))
88 #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
90 /* A normal version as set out in the FIPS. This version uses */
91 /* partial loop unrolling and is optimised for the Pentium 4 */
93 t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
94 e = d; d = c; c = rotl32(b, 30); b = t
96 /* type to hold the SHA1 context */
103 static void sha1_compile(struct sha1_ctx_t *ctx)
105 uint32_t w[80], i, a, b, c, d, e, t;
107 /* note that words are compiled from the buffer into 32-bit */
108 /* words in big-endian order so an order reversal is needed */
109 /* here on little endian machines */
110 for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
111 w[i] = swap_b32(ctx->wbuf[i]);
113 for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
114 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
122 for (i = 0; i < 20; ++i) {
126 for (i = 20; i < 40; ++i) {
127 rnd(parity, 0x6ed9eba1);
130 for (i = 40; i < 60; ++i) {
131 rnd(maj, 0x8f1bbcdc);
134 for (i = 60; i < 80; ++i) {
135 rnd(parity, 0xca62c1d6);
145 static void sha1_begin(struct sha1_ctx_t *ctx)
147 ctx->count[0] = ctx->count[1] = 0;
148 ctx->hash[0] = 0x67452301;
149 ctx->hash[1] = 0xefcdab89;
150 ctx->hash[2] = 0x98badcfe;
151 ctx->hash[3] = 0x10325476;
152 ctx->hash[4] = 0xc3d2e1f0;
155 /* SHA1 hash data in an array of bytes into hash buffer and call the */
156 /* hash_compile function as required. */
157 static void sha1_hash(const void *data, size_t len, void *ctx_v)
159 struct sha1_ctx_t *ctx = (struct sha1_ctx_t *) ctx_v;
160 uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
161 uint32_t freeb = SHA1_BLOCK_SIZE - pos;
162 const unsigned char *sp = data;
164 if ((ctx->count[0] += len) < len)
167 while (len >= freeb) { /* tranfer whole blocks while possible */
168 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
171 freeb = SHA1_BLOCK_SIZE;
176 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, len);
179 /* SHA1 Final padding and digest calculation */
180 # if __BYTE_ORDER == __LITTLE_ENDIAN
181 static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
182 static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
184 static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
185 static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
186 # endif /* __BYTE_ORDER */
188 static void sha1_end(unsigned char hval[], struct sha1_ctx_t *ctx)
190 uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
192 /* mask out the rest of any partial 32-bit word and then set */
193 /* the next byte to 0x80. On big-endian machines any bytes in */
194 /* the buffer will be at the top end of 32 bit words, on little */
195 /* endian machines they will be at the bottom. Hence the AND */
196 /* and OR masks above are reversed for little endian systems */
197 ctx->wbuf[cnt >> 2] =
198 (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
200 /* we need 9 or more empty positions, one for the padding byte */
201 /* (above) and eight for the length count. If there is not */
202 /* enough space pad and empty the buffer */
203 if (cnt > SHA1_BLOCK_SIZE - 9) {
208 } else /* compute a word index for the empty buffer positions */
209 cnt = (cnt >> 2) + 1;
211 while (cnt < 14) /* and zero pad all but last two positions */
212 ctx->wbuf[cnt++] = 0;
214 /* assemble the eight byte counter in the buffer in big-endian */
217 ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29));
218 ctx->wbuf[15] = swap_b32(ctx->count[0] << 3);
222 /* extract the hash value as bytes in case the hash buffer is */
223 /* misaligned for 32-bit words */
225 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
226 hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
230 ---------------------------------------------------------------------------
231 End of Dr. Gladman's sha1 code
232 ---------------------------------------------------------------------------
234 #endif /* CONFIG_SHA1 */
242 * md5sum.c - Compute MD5 checksum of files or strings according to the
243 * definition of MD5 in RFC 1321 from April 1992.
245 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
246 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
249 * June 29, 2001 Manuel Novoa III
251 * Added MD5SUM_SIZE_VS_SPEED configuration option.
253 * Current valid values, with data from my system for comparison, are:
254 * (using uClibc and running on linux-2.4.4.tar.bz2)
255 * user times (sec) text size (386)
256 * 0 (fastest) 1.1 6144
259 * 3 (smallest) 5.1 4912
262 # if CONFIG_MD5SUM_SIZE_VS_SPEED < 0 || CONFIG_MD5SUM_SIZE_VS_SPEED > 3
263 # define MD5SUM_SIZE_VS_SPEED 2
265 # define MD5SUM_SIZE_VS_SPEED CONFIG_MD5SUM_SIZE_VS_SPEED
268 /* Handle endian-ness */
269 # if __BYTE_ORDER == __LITTLE_ENDIAN
271 # elif defined(bswap_32)
272 # define SWAP(n) bswap_32(n)
274 # define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24))
277 # if MD5SUM_SIZE_VS_SPEED == 0
278 /* This array contains the bytes used to pad the buffer to the next
279 64-byte boundary. (RFC 1321, 3.1: Step 1) */
280 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
281 # endif /* MD5SUM_SIZE_VS_SPEED == 0 */
283 /* Structure to save state of computation between the single steps. */
294 /* Initialize structure containing state of computation.
295 * (RFC 1321, 3.3: Step 3)
297 static void md5_begin(struct md5_ctx_t *ctx)
304 ctx->total[0] = ctx->total[1] = 0;
308 /* These are the four functions used in the four steps of the MD5 algorithm
309 * and defined in the RFC 1321. The first function is a little bit optimized
310 * (as found in Colin Plumbs public domain implementation).
311 * #define FF(b, c, d) ((b & c) | (~b & d))
313 # define FF(b, c, d) (d ^ (b & (c ^ d)))
314 # define FG(b, c, d) FF (d, b, c)
315 # define FH(b, c, d) (b ^ c ^ d)
316 # define FI(b, c, d) (c ^ (b | ~d))
318 /* Starting with the result of former calls of this function (or the
319 * initialization function update the context for the next LEN bytes
320 * starting at BUFFER.
321 * It is necessary that LEN is a multiple of 64!!!
323 static void md5_hash_block(const void *buffer, size_t len, struct md5_ctx_t *ctx)
325 uint32_t correct_words[16];
326 const uint32_t *words = buffer;
327 size_t nwords = len / sizeof(uint32_t);
328 const uint32_t *endp = words + nwords;
330 # if MD5SUM_SIZE_VS_SPEED > 0
331 static const uint32_t C_array[] = {
333 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
334 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
335 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
336 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
338 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
339 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
340 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
341 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
343 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
344 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
345 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
346 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
348 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
349 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
350 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
351 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
354 static const char P_array[] = {
355 # if MD5SUM_SIZE_VS_SPEED > 1
356 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
357 # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
358 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
359 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
360 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
363 # if MD5SUM_SIZE_VS_SPEED > 1
364 static const char S_array[] = {
370 # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
378 /* First increment the byte count. RFC 1321 specifies the possible
379 length of the file up to 2^64 bits. Here we only compute the
380 number of bytes. Do a double word increment. */
381 ctx->total[0] += len;
382 if (ctx->total[0] < len)
385 /* Process all bytes in the buffer with 64 bytes in each round of
387 while (words < endp) {
388 uint32_t *cwp = correct_words;
394 # if MD5SUM_SIZE_VS_SPEED > 1
395 # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
403 for (i = 0; i < 16; i++) {
404 cwp[i] = SWAP(words[i]);
408 # if MD5SUM_SIZE_VS_SPEED > 2
413 for (i = 0; i < 64; i++) {
430 temp += cwp[(int) (*pp++)] + *pc++;
431 CYCLIC(temp, ps[i & 3]);
443 for (i = 0; i < 16; i++) {
444 temp = A + FF(B, C, D) + cwp[(int) (*pp++)] + *pc++;
445 CYCLIC(temp, ps[i & 3]);
454 for (i = 0; i < 16; i++) {
455 temp = A + FG(B, C, D) + cwp[(int) (*pp++)] + *pc++;
456 CYCLIC(temp, ps[i & 3]);
464 for (i = 0; i < 16; i++) {
465 temp = A + FH(B, C, D) + cwp[(int) (*pp++)] + *pc++;
466 CYCLIC(temp, ps[i & 3]);
474 for (i = 0; i < 16; i++) {
475 temp = A + FI(B, C, D) + cwp[(int) (*pp++)] + *pc++;
476 CYCLIC(temp, ps[i & 3]);
484 # endif /* MD5SUM_SIZE_VS_SPEED > 2 */
486 /* First round: using the given function, the context and a constant
487 the next context is computed. Because the algorithms processing
488 unit is a 32-bit word and it is determined to work on words in
489 little endian byte order we perhaps have to change the byte order
490 before the computation. To reduce the work for the next steps
491 we store the swapped words in the array CORRECT_WORDS. */
493 # define OP(a, b, c, d, s, T) \
496 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
503 /* It is unfortunate that C does not provide an operator for
504 cyclic rotation. Hope the C compiler is smart enough. */
505 /* gcc 2.95.4 seems to be --aaronl */
506 # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
508 /* Before we start, one word to the strange constants.
509 They are defined in RFC 1321 as
511 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
514 # if MD5SUM_SIZE_VS_SPEED == 1
518 # endif /* MD5SUM_SIZE_VS_SPEED */
521 # if MD5SUM_SIZE_VS_SPEED == 1
523 for (i = 0; i < 4; i++) {
524 OP(A, B, C, D, 7, *pc++);
525 OP(D, A, B, C, 12, *pc++);
526 OP(C, D, A, B, 17, *pc++);
527 OP(B, C, D, A, 22, *pc++);
530 OP(A, B, C, D, 7, 0xd76aa478);
531 OP(D, A, B, C, 12, 0xe8c7b756);
532 OP(C, D, A, B, 17, 0x242070db);
533 OP(B, C, D, A, 22, 0xc1bdceee);
534 OP(A, B, C, D, 7, 0xf57c0faf);
535 OP(D, A, B, C, 12, 0x4787c62a);
536 OP(C, D, A, B, 17, 0xa8304613);
537 OP(B, C, D, A, 22, 0xfd469501);
538 OP(A, B, C, D, 7, 0x698098d8);
539 OP(D, A, B, C, 12, 0x8b44f7af);
540 OP(C, D, A, B, 17, 0xffff5bb1);
541 OP(B, C, D, A, 22, 0x895cd7be);
542 OP(A, B, C, D, 7, 0x6b901122);
543 OP(D, A, B, C, 12, 0xfd987193);
544 OP(C, D, A, B, 17, 0xa679438e);
545 OP(B, C, D, A, 22, 0x49b40821);
546 # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
548 /* For the second to fourth round we have the possibly swapped words
549 in CORRECT_WORDS. Redefine the macro to take an additional first
550 argument specifying the function to use. */
552 # define OP(f, a, b, c, d, k, s, T) \
555 a += f (b, c, d) + correct_words[k] + T; \
562 # if MD5SUM_SIZE_VS_SPEED == 1
564 for (i = 0; i < 4; i++) {
565 OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++);
566 OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++);
567 OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++);
568 OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++);
571 OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
572 OP(FG, D, A, B, C, 6, 9, 0xc040b340);
573 OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
574 OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
575 OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
576 OP(FG, D, A, B, C, 10, 9, 0x02441453);
577 OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
578 OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
579 OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
580 OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
581 OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
582 OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
583 OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
584 OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
585 OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
586 OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
587 # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
590 # if MD5SUM_SIZE_VS_SPEED == 1
591 for (i = 0; i < 4; i++) {
592 OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++);
593 OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++);
594 OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++);
595 OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++);
598 OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
599 OP(FH, D, A, B, C, 8, 11, 0x8771f681);
600 OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
601 OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
602 OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
603 OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
604 OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
605 OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
606 OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
607 OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
608 OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
609 OP(FH, B, C, D, A, 6, 23, 0x04881d05);
610 OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
611 OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
612 OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
613 OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
614 # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
617 # if MD5SUM_SIZE_VS_SPEED == 1
618 for (i = 0; i < 4; i++) {
619 OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++);
620 OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++);
621 OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++);
622 OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++);
625 OP(FI, A, B, C, D, 0, 6, 0xf4292244);
626 OP(FI, D, A, B, C, 7, 10, 0x432aff97);
627 OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
628 OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
629 OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
630 OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
631 OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
632 OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
633 OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
634 OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
635 OP(FI, C, D, A, B, 6, 15, 0xa3014314);
636 OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
637 OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
638 OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
639 OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
640 OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
641 # endif /* MD5SUM_SIZE_VS_SPEED == 1 */
642 # endif /* MD5SUM_SIZE_VS_SPEED > 1 */
644 /* Add the starting values of the context. */
651 /* Put checksum in context given as argument. */
658 /* Starting with the result of former calls of this function (or the
659 * initialization function update the context for the next LEN bytes
660 * starting at BUFFER.
661 * It is NOT required that LEN is a multiple of 64.
664 static void md5_hash_bytes(const void *buffer, size_t len, struct md5_ctx_t *ctx)
666 /* When we already have some bits in our internal buffer concatenate
667 both inputs first. */
668 if (ctx->buflen != 0) {
669 size_t left_over = ctx->buflen;
670 size_t add = 128 - left_over > len ? len : 128 - left_over;
672 memcpy(&ctx->buffer[left_over], buffer, add);
675 if (left_over + add > 64) {
676 md5_hash_block(ctx->buffer, (left_over + add) & ~63, ctx);
677 /* The regions in the following copy operation cannot overlap. */
678 memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
679 (left_over + add) & 63);
680 ctx->buflen = (left_over + add) & 63;
683 buffer = (const char *) buffer + add;
687 /* Process available complete blocks. */
689 md5_hash_block(buffer, len & ~63, ctx);
690 buffer = (const char *) buffer + (len & ~63);
694 /* Move remaining bytes in internal buffer. */
696 memcpy(ctx->buffer, buffer, len);
701 static void md5_hash(const void *buffer, size_t length, void *md5_ctx)
703 if (length % 64 == 0) {
704 md5_hash_block(buffer, length, md5_ctx);
706 md5_hash_bytes(buffer, length, md5_ctx);
710 /* Process the remaining bytes in the buffer and put result from CTX
711 * in first 16 bytes following RESBUF. The result is always in little
712 * endian byte order, so that a byte-wise output yields to the wanted
713 * ASCII representation of the message digest.
715 * IMPORTANT: On some systems it is required that RESBUF is correctly
716 * aligned for a 32 bits value.
718 static void *md5_end(void *resbuf, struct md5_ctx_t *ctx)
720 /* Take yet unprocessed bytes into account. */
721 uint32_t bytes = ctx->buflen;
724 /* Now count remaining bytes. */
725 ctx->total[0] += bytes;
726 if (ctx->total[0] < bytes)
729 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
730 # if MD5SUM_SIZE_VS_SPEED > 0
731 memset(&ctx->buffer[bytes], 0, pad);
732 ctx->buffer[bytes] = 0x80;
734 memcpy(&ctx->buffer[bytes], fillbuf, pad);
735 # endif /* MD5SUM_SIZE_VS_SPEED > 0 */
737 /* Put the 64-bit file length in *bits* at the end of the buffer. */
738 *(uint32_t *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
739 *(uint32_t *) & ctx->buffer[bytes + pad + 4] =
740 SWAP(((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
742 /* Process last bytes. */
743 md5_hash_block(ctx->buffer, bytes + pad + 8, ctx);
745 /* Put result from CTX in first 16 bytes following RESBUF. The result is
746 * always in little endian byte order, so that a byte-wise output yields
747 * to the wanted ASCII representation of the message digest.
749 * IMPORTANT: On some systems it is required that RESBUF is correctly
750 * aligned for a 32 bits value.
752 ((uint32_t *) resbuf)[0] = SWAP(ctx->A);
753 ((uint32_t *) resbuf)[1] = SWAP(ctx->B);
754 ((uint32_t *) resbuf)[2] = SWAP(ctx->C);
755 ((uint32_t *) resbuf)[3] = SWAP(ctx->D);
759 #endif /* CONFIG_MD5SUM */
764 extern int hash_fd(int src_fd, const size_t size, const uint8_t hash_algo,
767 int result = EXIT_SUCCESS;
768 // size_t hashed_count = 0;
769 size_t blocksize = 0;
770 size_t remaining = size;
771 unsigned char *buffer = NULL;
772 void (*hash_fn_ptr)(const void *, size_t, void *) = NULL;
775 #ifdef CONFIG_SHA1SUM
776 struct sha1_ctx_t sha1_cx;
779 struct md5_ctx_t md5_cx;
783 #ifdef CONFIG_SHA1SUM
784 if (hash_algo == HASH_SHA1) {
785 /* Ensure that BLOCKSIZE is a multiple of 64. */
787 buffer = xmalloc(blocksize);
788 hash_fn_ptr = sha1_hash;
793 if (hash_algo == HASH_MD5) {
795 buffer = xmalloc(blocksize + 72);
796 hash_fn_ptr = md5_hash;
801 /* Initialize the computation context. */
802 #ifdef CONFIG_SHA1SUM
803 if (hash_algo == HASH_SHA1) {
804 sha1_begin(&sha1_cx);
808 if (hash_algo == HASH_MD5) {
812 /* Iterate over full file contents. */
813 while ((remaining == (size_t) -1) || (remaining > 0)) {
817 if (remaining > blocksize) {
818 read_try = blocksize;
820 read_try = remaining;
822 read_got = bb_full_read(src_fd, buffer, read_try);
824 /* count == 0 means short read
825 * count == -1 means read error */
826 result = read_got - 1;
829 if (remaining != (size_t) -1) {
830 remaining -= read_got;
834 hash_fn_ptr(buffer, read_got, cx);
837 /* Finalize and write the hash into our buffer. */
838 #ifdef CONFIG_SHA1SUM
839 if (hash_algo == HASH_SHA1) {
840 sha1_end(hashval, &sha1_cx);
844 if (hash_algo == HASH_MD5) {
845 md5_end(hashval, &md5_cx);