Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 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 |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | 37dec0e | 2006-01-17 12:07:03 +0000 | [diff] [blame^] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <byteswap.h> |
| 12 | #include <endian.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <limits.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include "busybox.h" |
| 22 | |
| 23 | |
| 24 | #ifdef CONFIG_SHA1SUM |
| 25 | /* |
| 26 | --------------------------------------------------------------------------- |
| 27 | Begin Dr. Gladman's sha1 code |
| 28 | --------------------------------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | --------------------------------------------------------------------------- |
| 33 | Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. |
| 34 | All rights reserved. |
| 35 | |
| 36 | LICENSE TERMS |
| 37 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 38 | The free distribution and use of this software in both source and binary |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 39 | form is allowed (with or without changes) provided that: |
| 40 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 41 | 1. distributions of this source code include the above copyright |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 42 | notice, this list of conditions and the following disclaimer; |
| 43 | |
| 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; |
| 47 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 48 | 3. the copyright holder's name is not used to endorse products |
| 49 | built using this software without specific written permission. |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 50 | |
| 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. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 55 | DISCLAIMER |
| 56 | |
| 57 | This software is provided 'as is' with no explicit or implied warranties |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 58 | in respect of its properties, including, but not limited to, correctness |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 59 | and/or fitness for purpose. |
| 60 | --------------------------------------------------------------------------- |
| 61 | Issue Date: 10/11/2002 |
| 62 | |
| 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 |
| 65 | */ |
| 66 | |
| 67 | # define SHA1_BLOCK_SIZE 64 |
| 68 | # define SHA1_DIGEST_SIZE 20 |
| 69 | # define SHA1_HASH_SIZE SHA1_DIGEST_SIZE |
| 70 | # define SHA2_GOOD 0 |
| 71 | # define SHA2_BAD 1 |
| 72 | |
| 73 | # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) |
| 74 | |
| 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) |
| 79 | # else |
| 80 | # define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00)) |
| 81 | # endif /* __BYTE_ORDER */ |
| 82 | |
| 83 | # define SHA1_MASK (SHA1_BLOCK_SIZE - 1) |
| 84 | |
| 85 | /* reverse byte order in 32-bit words */ |
Eric Andersen | f88bb72 | 2004-01-29 22:33:28 +0000 | [diff] [blame] | 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)))) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 89 | |
| 90 | /* A normal version as set out in the FIPS. This version uses */ |
| 91 | /* partial loop unrolling and is optimised for the Pentium 4 */ |
| 92 | # define rnd(f,k) \ |
| 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 |
| 95 | |
| 96 | /* type to hold the SHA1 context */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 97 | struct sha1_ctx_t { |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 98 | uint32_t count[2]; |
| 99 | uint32_t hash[5]; |
| 100 | uint32_t wbuf[16]; |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 101 | }; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 102 | |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 103 | static void sha1_compile(struct sha1_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 104 | { |
| 105 | uint32_t w[80], i, a, b, c, d, e, t; |
| 106 | |
| 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]); |
| 112 | |
| 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); |
| 115 | |
| 116 | a = ctx->hash[0]; |
| 117 | b = ctx->hash[1]; |
| 118 | c = ctx->hash[2]; |
| 119 | d = ctx->hash[3]; |
| 120 | e = ctx->hash[4]; |
| 121 | |
| 122 | for (i = 0; i < 20; ++i) { |
| 123 | rnd(ch, 0x5a827999); |
| 124 | } |
| 125 | |
| 126 | for (i = 20; i < 40; ++i) { |
| 127 | rnd(parity, 0x6ed9eba1); |
| 128 | } |
| 129 | |
| 130 | for (i = 40; i < 60; ++i) { |
| 131 | rnd(maj, 0x8f1bbcdc); |
| 132 | } |
| 133 | |
| 134 | for (i = 60; i < 80; ++i) { |
| 135 | rnd(parity, 0xca62c1d6); |
| 136 | } |
| 137 | |
| 138 | ctx->hash[0] += a; |
| 139 | ctx->hash[1] += b; |
| 140 | ctx->hash[2] += c; |
| 141 | ctx->hash[3] += d; |
| 142 | ctx->hash[4] += e; |
| 143 | } |
| 144 | |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 145 | static void sha1_begin(struct sha1_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 146 | { |
| 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; |
| 153 | } |
| 154 | |
| 155 | /* SHA1 hash data in an array of bytes into hash buffer and call the */ |
| 156 | /* hash_compile function as required. */ |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 157 | static void sha1_hash(const void *data, size_t len, void *ctx_v) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 158 | { |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 159 | struct sha1_ctx_t *ctx = (struct sha1_ctx_t *) ctx_v; |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 160 | uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK); |
| 161 | uint32_t freeb = SHA1_BLOCK_SIZE - pos; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 162 | const unsigned char *sp = data; |
| 163 | |
| 164 | if ((ctx->count[0] += len) < len) |
| 165 | ++(ctx->count[1]); |
| 166 | |
| 167 | while (len >= freeb) { /* tranfer whole blocks while possible */ |
| 168 | memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb); |
| 169 | sp += freeb; |
| 170 | len -= freeb; |
| 171 | freeb = SHA1_BLOCK_SIZE; |
| 172 | pos = 0; |
| 173 | sha1_compile(ctx); |
| 174 | } |
| 175 | |
| 176 | memcpy(((unsigned char *) ctx->wbuf) + pos, sp, len); |
| 177 | } |
| 178 | |
| 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 }; |
| 183 | # else |
| 184 | static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 }; |
| 185 | static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 }; |
| 186 | # endif /* __BYTE_ORDER */ |
| 187 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 188 | static void sha1_end(unsigned char hval[], struct sha1_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 189 | { |
| 190 | uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK); |
| 191 | |
| 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]; |
| 199 | |
| 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) { |
| 204 | if (cnt < 60) |
| 205 | ctx->wbuf[15] = 0; |
| 206 | sha1_compile(ctx); |
| 207 | cnt = 0; |
| 208 | } else /* compute a word index for the empty buffer positions */ |
| 209 | cnt = (cnt >> 2) + 1; |
| 210 | |
| 211 | while (cnt < 14) /* and zero pad all but last two positions */ |
| 212 | ctx->wbuf[cnt++] = 0; |
| 213 | |
| 214 | /* assemble the eight byte counter in the buffer in big-endian */ |
| 215 | /* format */ |
| 216 | |
| 217 | ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29)); |
| 218 | ctx->wbuf[15] = swap_b32(ctx->count[0] << 3); |
| 219 | |
| 220 | sha1_compile(ctx); |
| 221 | |
| 222 | /* extract the hash value as bytes in case the hash buffer is */ |
| 223 | /* misaligned for 32-bit words */ |
| 224 | |
| 225 | for (i = 0; i < SHA1_DIGEST_SIZE; ++i) |
| 226 | hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3)); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | --------------------------------------------------------------------------- |
| 231 | End of Dr. Gladman's sha1 code |
| 232 | --------------------------------------------------------------------------- |
| 233 | */ |
| 234 | #endif /* CONFIG_SHA1 */ |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | #ifdef CONFIG_MD5SUM |
| 241 | /* |
| 242 | * md5sum.c - Compute MD5 checksum of files or strings according to the |
| 243 | * definition of MD5 in RFC 1321 from April 1992. |
| 244 | * |
| 245 | * Copyright (C) 1995-1999 Free Software Foundation, Inc. |
| 246 | * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. |
| 247 | * |
| 248 | * |
| 249 | * June 29, 2001 Manuel Novoa III |
| 250 | * |
| 251 | * Added MD5SUM_SIZE_VS_SPEED configuration option. |
| 252 | * |
| 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 |
| 257 | * 1 1.4 5392 |
| 258 | * 2 3.0 5088 |
| 259 | * 3 (smallest) 5.1 4912 |
| 260 | */ |
| 261 | |
Bernhard Reutner-Fischer | 37dec0e | 2006-01-17 12:07:03 +0000 | [diff] [blame^] | 262 | # if CONFIG_MD5SUM_SIZE_VS_SPEED < 0 || CONFIG_MD5SUM_SIZE_VS_SPEED > 3 |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 263 | # define MD5SUM_SIZE_VS_SPEED 2 |
Bernhard Reutner-Fischer | 37dec0e | 2006-01-17 12:07:03 +0000 | [diff] [blame^] | 264 | # else |
| 265 | # define MD5SUM_SIZE_VS_SPEED CONFIG_MD5SUM_SIZE_VS_SPEED |
| 266 | # endif |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 267 | |
| 268 | /* Handle endian-ness */ |
| 269 | # if __BYTE_ORDER == __LITTLE_ENDIAN |
| 270 | # define SWAP(n) (n) |
Eric Andersen | f88bb72 | 2004-01-29 22:33:28 +0000 | [diff] [blame] | 271 | # elif defined(bswap_32) |
| 272 | # define SWAP(n) bswap_32(n) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 273 | # else |
| 274 | # define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24)) |
| 275 | # endif |
| 276 | |
| 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 */ |
| 282 | |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 283 | /* Structure to save state of computation between the single steps. */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 284 | struct md5_ctx_t { |
| 285 | uint32_t A; |
| 286 | uint32_t B; |
| 287 | uint32_t C; |
| 288 | uint32_t D; |
| 289 | uint32_t total[2]; |
| 290 | uint32_t buflen; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 291 | char buffer[128]; |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 292 | }; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 293 | |
| 294 | /* Initialize structure containing state of computation. |
| 295 | * (RFC 1321, 3.3: Step 3) |
| 296 | */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 297 | static void md5_begin(struct md5_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 298 | { |
| 299 | ctx->A = 0x67452301; |
| 300 | ctx->B = 0xefcdab89; |
| 301 | ctx->C = 0x98badcfe; |
| 302 | ctx->D = 0x10325476; |
| 303 | |
| 304 | ctx->total[0] = ctx->total[1] = 0; |
| 305 | ctx->buflen = 0; |
| 306 | } |
| 307 | |
| 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)) |
| 312 | */ |
| 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)) |
| 317 | |
| 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!!! |
| 322 | */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 323 | static void md5_hash_block(const void *buffer, size_t len, struct md5_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 324 | { |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 329 | |
| 330 | # if MD5SUM_SIZE_VS_SPEED > 0 |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 331 | static const uint32_t C_array[] = { |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 332 | /* round 1 */ |
| 333 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, |
| 334 | 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, |
| 335 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 336 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, |
| 337 | /* round 2 */ |
| 338 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, |
| 339 | 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, |
| 340 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, |
| 341 | 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, |
| 342 | /* round 3 */ |
| 343 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
| 344 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, |
| 345 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, |
| 346 | 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
| 347 | /* round 4 */ |
| 348 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, |
| 349 | 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, |
| 350 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
| 351 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
| 352 | }; |
| 353 | |
| 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 */ |
| 361 | }; |
| 362 | |
| 363 | # if MD5SUM_SIZE_VS_SPEED > 1 |
| 364 | static const char S_array[] = { |
| 365 | 7, 12, 17, 22, |
| 366 | 5, 9, 14, 20, |
| 367 | 4, 11, 16, 23, |
| 368 | 6, 10, 15, 21 |
| 369 | }; |
| 370 | # endif /* MD5SUM_SIZE_VS_SPEED > 1 */ |
| 371 | # endif |
| 372 | |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 373 | uint32_t A = ctx->A; |
| 374 | uint32_t B = ctx->B; |
| 375 | uint32_t C = ctx->C; |
| 376 | uint32_t D = ctx->D; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 377 | |
| 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) |
| 383 | ++ctx->total[1]; |
| 384 | |
| 385 | /* Process all bytes in the buffer with 64 bytes in each round of |
| 386 | the loop. */ |
| 387 | while (words < endp) { |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 388 | uint32_t *cwp = correct_words; |
| 389 | uint32_t A_save = A; |
| 390 | uint32_t B_save = B; |
| 391 | uint32_t C_save = C; |
| 392 | uint32_t D_save = D; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 393 | |
| 394 | # if MD5SUM_SIZE_VS_SPEED > 1 |
| 395 | # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) |
| 396 | |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 397 | const uint32_t *pc; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 398 | const char *pp; |
| 399 | const char *ps; |
| 400 | int i; |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 401 | uint32_t temp; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 402 | |
| 403 | for (i = 0; i < 16; i++) { |
| 404 | cwp[i] = SWAP(words[i]); |
| 405 | } |
| 406 | words += 16; |
| 407 | |
| 408 | # if MD5SUM_SIZE_VS_SPEED > 2 |
| 409 | pc = C_array; |
| 410 | pp = P_array; |
| 411 | ps = S_array - 4; |
| 412 | |
| 413 | for (i = 0; i < 64; i++) { |
| 414 | if ((i & 0x0f) == 0) |
| 415 | ps += 4; |
| 416 | temp = A; |
| 417 | switch (i >> 4) { |
| 418 | case 0: |
| 419 | temp += FF(B, C, D); |
| 420 | break; |
| 421 | case 1: |
| 422 | temp += FG(B, C, D); |
| 423 | break; |
| 424 | case 2: |
| 425 | temp += FH(B, C, D); |
| 426 | break; |
| 427 | case 3: |
| 428 | temp += FI(B, C, D); |
| 429 | } |
| 430 | temp += cwp[(int) (*pp++)] + *pc++; |
| 431 | CYCLIC(temp, ps[i & 3]); |
| 432 | temp += B; |
| 433 | A = D; |
| 434 | D = C; |
| 435 | C = B; |
| 436 | B = temp; |
| 437 | } |
| 438 | # else |
| 439 | pc = C_array; |
| 440 | pp = P_array; |
| 441 | ps = S_array; |
| 442 | |
| 443 | for (i = 0; i < 16; i++) { |
| 444 | temp = A + FF(B, C, D) + cwp[(int) (*pp++)] + *pc++; |
| 445 | CYCLIC(temp, ps[i & 3]); |
| 446 | temp += B; |
| 447 | A = D; |
| 448 | D = C; |
| 449 | C = B; |
| 450 | B = temp; |
| 451 | } |
| 452 | |
| 453 | ps += 4; |
| 454 | for (i = 0; i < 16; i++) { |
| 455 | temp = A + FG(B, C, D) + cwp[(int) (*pp++)] + *pc++; |
| 456 | CYCLIC(temp, ps[i & 3]); |
| 457 | temp += B; |
| 458 | A = D; |
| 459 | D = C; |
| 460 | C = B; |
| 461 | B = temp; |
| 462 | } |
| 463 | ps += 4; |
| 464 | for (i = 0; i < 16; i++) { |
| 465 | temp = A + FH(B, C, D) + cwp[(int) (*pp++)] + *pc++; |
| 466 | CYCLIC(temp, ps[i & 3]); |
| 467 | temp += B; |
| 468 | A = D; |
| 469 | D = C; |
| 470 | C = B; |
| 471 | B = temp; |
| 472 | } |
| 473 | ps += 4; |
| 474 | for (i = 0; i < 16; i++) { |
| 475 | temp = A + FI(B, C, D) + cwp[(int) (*pp++)] + *pc++; |
| 476 | CYCLIC(temp, ps[i & 3]); |
| 477 | temp += B; |
| 478 | A = D; |
| 479 | D = C; |
| 480 | C = B; |
| 481 | B = temp; |
| 482 | } |
| 483 | |
| 484 | # endif /* MD5SUM_SIZE_VS_SPEED > 2 */ |
| 485 | # else |
| 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. */ |
| 492 | |
| 493 | # define OP(a, b, c, d, s, T) \ |
| 494 | do \ |
| 495 | { \ |
| 496 | a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ |
| 497 | ++words; \ |
| 498 | CYCLIC (a, s); \ |
| 499 | a += b; \ |
| 500 | } \ |
| 501 | while (0) |
| 502 | |
| 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))) |
| 507 | |
| 508 | /* Before we start, one word to the strange constants. |
| 509 | They are defined in RFC 1321 as |
| 510 | |
| 511 | T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 |
| 512 | */ |
| 513 | |
| 514 | # if MD5SUM_SIZE_VS_SPEED == 1 |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 515 | const uint32_t *pc; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 516 | const char *pp; |
| 517 | int i; |
| 518 | # endif /* MD5SUM_SIZE_VS_SPEED */ |
| 519 | |
| 520 | /* Round 1. */ |
| 521 | # if MD5SUM_SIZE_VS_SPEED == 1 |
| 522 | pc = C_array; |
| 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++); |
| 528 | } |
| 529 | # else |
| 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 */ |
| 547 | |
| 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. */ |
| 551 | # undef OP |
| 552 | # define OP(f, a, b, c, d, k, s, T) \ |
| 553 | do \ |
| 554 | { \ |
| 555 | a += f (b, c, d) + correct_words[k] + T; \ |
| 556 | CYCLIC (a, s); \ |
| 557 | a += b; \ |
| 558 | } \ |
| 559 | while (0) |
| 560 | |
| 561 | /* Round 2. */ |
| 562 | # if MD5SUM_SIZE_VS_SPEED == 1 |
| 563 | pp = P_array; |
| 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++); |
| 569 | } |
| 570 | # else |
| 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 */ |
| 588 | |
| 589 | /* Round 3. */ |
| 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++); |
| 596 | } |
| 597 | # else |
| 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 */ |
| 615 | |
| 616 | /* Round 4. */ |
| 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++); |
| 623 | } |
| 624 | # else |
| 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 */ |
| 643 | |
| 644 | /* Add the starting values of the context. */ |
| 645 | A += A_save; |
| 646 | B += B_save; |
| 647 | C += C_save; |
| 648 | D += D_save; |
| 649 | } |
| 650 | |
| 651 | /* Put checksum in context given as argument. */ |
| 652 | ctx->A = A; |
| 653 | ctx->B = B; |
| 654 | ctx->C = C; |
| 655 | ctx->D = D; |
| 656 | } |
| 657 | |
| 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. |
| 662 | */ |
| 663 | |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 664 | static void md5_hash_bytes(const void *buffer, size_t len, struct md5_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 665 | { |
| 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; |
| 671 | |
| 672 | memcpy(&ctx->buffer[left_over], buffer, add); |
| 673 | ctx->buflen += add; |
| 674 | |
| 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; |
| 681 | } |
| 682 | |
| 683 | buffer = (const char *) buffer + add; |
| 684 | len -= add; |
| 685 | } |
| 686 | |
| 687 | /* Process available complete blocks. */ |
| 688 | if (len > 64) { |
| 689 | md5_hash_block(buffer, len & ~63, ctx); |
| 690 | buffer = (const char *) buffer + (len & ~63); |
| 691 | len &= 63; |
| 692 | } |
| 693 | |
| 694 | /* Move remaining bytes in internal buffer. */ |
| 695 | if (len > 0) { |
| 696 | memcpy(ctx->buffer, buffer, len); |
| 697 | ctx->buflen = len; |
| 698 | } |
| 699 | } |
| 700 | |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 701 | static void md5_hash(const void *buffer, size_t length, void *md5_ctx) |
| 702 | { |
| 703 | if (length % 64 == 0) { |
| 704 | md5_hash_block(buffer, length, md5_ctx); |
| 705 | } else { |
| 706 | md5_hash_bytes(buffer, length, md5_ctx); |
| 707 | } |
| 708 | } |
| 709 | |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 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. |
| 714 | * |
| 715 | * IMPORTANT: On some systems it is required that RESBUF is correctly |
| 716 | * aligned for a 32 bits value. |
| 717 | */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 718 | static void *md5_end(void *resbuf, struct md5_ctx_t *ctx) |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 719 | { |
| 720 | /* Take yet unprocessed bytes into account. */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 721 | uint32_t bytes = ctx->buflen; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 722 | size_t pad; |
| 723 | |
| 724 | /* Now count remaining bytes. */ |
| 725 | ctx->total[0] += bytes; |
| 726 | if (ctx->total[0] < bytes) |
| 727 | ++ctx->total[1]; |
| 728 | |
| 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; |
| 733 | # else |
| 734 | memcpy(&ctx->buffer[bytes], fillbuf, pad); |
| 735 | # endif /* MD5SUM_SIZE_VS_SPEED > 0 */ |
| 736 | |
| 737 | /* Put the 64-bit file length in *bits* at the end of the buffer. */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 738 | *(uint32_t *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3); |
| 739 | *(uint32_t *) & ctx->buffer[bytes + pad + 4] = |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 740 | SWAP(((ctx->total[1] << 3) | (ctx->total[0] >> 29))); |
| 741 | |
| 742 | /* Process last bytes. */ |
| 743 | md5_hash_block(ctx->buffer, bytes + pad + 8, ctx); |
| 744 | |
| 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. |
| 748 | * |
| 749 | * IMPORTANT: On some systems it is required that RESBUF is correctly |
| 750 | * aligned for a 32 bits value. |
| 751 | */ |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 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); |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 756 | |
| 757 | return resbuf; |
| 758 | } |
| 759 | #endif /* CONFIG_MD5SUM */ |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 764 | extern int hash_fd(int src_fd, const size_t size, const uint8_t hash_algo, |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 765 | uint8_t * hashval) |
| 766 | { |
| 767 | int result = EXIT_SUCCESS; |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 768 | // size_t hashed_count = 0; |
| 769 | size_t blocksize = 0; |
| 770 | size_t remaining = size; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 771 | unsigned char *buffer = NULL; |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 772 | void (*hash_fn_ptr)(const void *, size_t, void *) = NULL; |
| 773 | void *cx = NULL; |
| 774 | |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 775 | #ifdef CONFIG_SHA1SUM |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 776 | struct sha1_ctx_t sha1_cx; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 777 | #endif |
| 778 | #ifdef CONFIG_MD5SUM |
Eric Andersen | ad84a51 | 2004-01-30 22:31:58 +0000 | [diff] [blame] | 779 | struct md5_ctx_t md5_cx; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 780 | #endif |
| 781 | |
| 782 | |
| 783 | #ifdef CONFIG_SHA1SUM |
| 784 | if (hash_algo == HASH_SHA1) { |
| 785 | /* Ensure that BLOCKSIZE is a multiple of 64. */ |
| 786 | blocksize = 65536; |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 787 | buffer = xmalloc(blocksize); |
| 788 | hash_fn_ptr = sha1_hash; |
| 789 | cx = &sha1_cx; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 790 | } |
| 791 | #endif |
| 792 | #ifdef CONFIG_MD5SUM |
| 793 | if (hash_algo == HASH_MD5) { |
| 794 | blocksize = 4096; |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 795 | buffer = xmalloc(blocksize + 72); |
| 796 | hash_fn_ptr = md5_hash; |
| 797 | cx = &md5_cx; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 798 | } |
| 799 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 800 | |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 801 | /* Initialize the computation context. */ |
| 802 | #ifdef CONFIG_SHA1SUM |
| 803 | if (hash_algo == HASH_SHA1) { |
Glenn L McGrath | 0413af0 | 2003-11-20 06:27:33 +0000 | [diff] [blame] | 804 | sha1_begin(&sha1_cx); |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 805 | } |
| 806 | #endif |
| 807 | #ifdef CONFIG_MD5SUM |
| 808 | if (hash_algo == HASH_MD5) { |
| 809 | md5_begin(&md5_cx); |
| 810 | } |
| 811 | #endif |
| 812 | /* Iterate over full file contents. */ |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 813 | while ((remaining == (size_t) -1) || (remaining > 0)) { |
| 814 | size_t read_try; |
| 815 | ssize_t read_got; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 816 | |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 817 | if (remaining > blocksize) { |
| 818 | read_try = blocksize; |
| 819 | } else { |
| 820 | read_try = remaining; |
| 821 | } |
| 822 | read_got = bb_full_read(src_fd, buffer, read_try); |
| 823 | if (read_got < 1) { |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 824 | /* count == 0 means short read |
| 825 | * count == -1 means read error */ |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 826 | result = read_got - 1; |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 827 | break; |
| 828 | } |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 829 | if (remaining != (size_t) -1) { |
| 830 | remaining -= read_got; |
| 831 | } |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 832 | |
| 833 | /* Process buffer */ |
Glenn L McGrath | 78cd84d | 2003-11-20 21:30:58 +0000 | [diff] [blame] | 834 | hash_fn_ptr(buffer, read_got, cx); |
| 835 | } |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 836 | |
| 837 | /* Finalize and write the hash into our buffer. */ |
| 838 | #ifdef CONFIG_SHA1SUM |
| 839 | if (hash_algo == HASH_SHA1) { |
Glenn L McGrath | 0413af0 | 2003-11-20 06:27:33 +0000 | [diff] [blame] | 840 | sha1_end(hashval, &sha1_cx); |
Glenn L McGrath | d2a897a | 2003-11-10 04:33:55 +0000 | [diff] [blame] | 841 | } |
| 842 | #endif |
| 843 | #ifdef CONFIG_MD5SUM |
| 844 | if (hash_algo == HASH_MD5) { |
| 845 | md5_end(hashval, &md5_cx); |
| 846 | } |
| 847 | #endif |
| 848 | |
| 849 | free(buffer); |
| 850 | return result; |
| 851 | } |