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