From 886d64e0e0c4ada69e68c5a6207caf03e8e06d44 Mon Sep 17 00:00:00 2001 From: ewt Date: Sat, 31 Aug 1996 18:35:54 +0000 Subject: [PATCH] works with old, broken MD5 sums RPM used to generate on big endian machines CVS patchset: 1007 CVS date: 1996/08/31 18:35:54 --- lib/md5.c | 38 +++++++++------- lib/md5.h | 8 +++- lib/md5sum.c | 146 +++++++++++++++++++++++++++++------------------------------ 3 files changed, 100 insertions(+), 92 deletions(-) diff --git a/lib/md5.c b/lib/md5.c index 005568b..8d5eaab 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -15,14 +15,11 @@ * will fill a supplied 16-byte array with the digest. */ #include /* for memcpy() */ +#include #include "md5.h" -#ifndef HIGHFIRST -#define byteReverse(buf, len) /* Nothing */ -#else void byteReverse(unsigned char *buf, unsigned longs); -#ifndef ASM_MD5 /* * Note: this code is harmless on little-endian machines. */ @@ -36,14 +33,12 @@ void byteReverse(unsigned char *buf, unsigned longs) buf += 4; } while (--longs); } -#endif -#endif /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ -void MD5Init(struct MD5Context *ctx) +void MD5Init(struct MD5Context *ctx, int brokenEndian) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -52,6 +47,16 @@ void MD5Init(struct MD5Context *ctx) ctx->bits[0] = 0; ctx->bits[1] = 0; + + #if BYTE_ORDER == BIG_ENDIAN + if (brokenEndian) { + ctx->doByteReverse = 0; + } else { + ctx->doByteReverse = 1; + } + #else + ctx->doByteReverse = 0; + #endif } /* @@ -82,7 +87,8 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) return; } memcpy(p, buf, t); - byteReverse(ctx->in, 16); + if (ctx->doByteReverse) + byteReverse(ctx->in, 16); MD5Transform(ctx->buf, (uint32 *) ctx->in); buf += t; len -= t; @@ -91,7 +97,8 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) while (len >= 64) { memcpy(ctx->in, buf, 64); - byteReverse(ctx->in, 16); + if (ctx->doByteReverse) + byteReverse(ctx->in, 16); MD5Transform(ctx->buf, (uint32 *) ctx->in); buf += 64; len -= 64; @@ -126,7 +133,8 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) if (count < 8) { /* Two lots of padding: Pad the first block to 64 bytes */ memset(p, 0, count); - byteReverse(ctx->in, 16); + if (ctx->doByteReverse) + byteReverse(ctx->in, 16); MD5Transform(ctx->buf, (uint32 *) ctx->in); /* Now fill the next block with 56 bytes */ @@ -135,20 +143,20 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) /* Pad block to 56 bytes */ memset(p, 0, count - 8); } - byteReverse(ctx->in, 14); + if (ctx->doByteReverse) + byteReverse(ctx->in, 14); /* Append length in bits and transform */ ((uint32 *) ctx->in)[14] = ctx->bits[0]; ((uint32 *) ctx->in)[15] = ctx->bits[1]; MD5Transform(ctx->buf, (uint32 *) ctx->in); - byteReverse((unsigned char *) ctx->buf, 4); + if (ctx->doByteReverse) + byteReverse((unsigned char *) ctx->buf, 4); memcpy(digest, ctx->buf, 16); memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ } -#ifndef ASM_MD5 - /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ @@ -249,5 +257,3 @@ void MD5Transform(uint32 buf[4], uint32 const in[16]) buf[3] += d; } -#endif - diff --git a/lib/md5.h b/lib/md5.h index 2b4b2d9..52b367e 100644 --- a/lib/md5.h +++ b/lib/md5.h @@ -11,9 +11,10 @@ struct MD5Context { uint32 buf[4]; uint32 bits[2]; unsigned char in[64]; + int doByteReverse; }; -void MD5Init(struct MD5Context *context); +void MD5Init(struct MD5Context *context, int brokenEndian); void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len); void MD5Final(unsigned char digest[16], struct MD5Context *context); @@ -22,6 +23,11 @@ void MD5Transform(uint32 buf[4], uint32 const in[16]); int mdfile(char *fn, unsigned char *digest); int mdbinfile(char *fn, unsigned char *bindigest); +/* These assume a little endian machine and return incorrect results! + They are here for compatibility with old (broken) versions of RPM */ +int mdfileBroken(char *fn, unsigned char *digest); +int mdbinfileBroken(char *fn, unsigned char *bindigest); + /* * This is needed to make RSAREF happy on some MS-DOS compilers. */ diff --git a/lib/md5sum.c b/lib/md5sum.c index dd20b70..9bb6183 100644 --- a/lib/md5sum.c +++ b/lib/md5sum.c @@ -1,76 +1,72 @@ -/* - * md5sum.c - Generate/check MD5 Message Digests - * - * Compile and link with md5.c. If you don't have getopt() in your library - * also include getopt.c. For MSDOS you can also link with the wildcard - * initialization function (wildargs.obj for Turbo C and setargv.obj for MSC) - * so that you can use wildcards on the commandline. - * - * Written March 1993 by Branko Lankester - * Modified June 1993 by Colin Plumb for altered md5.c. - * Modified October 1995 by Erik Troan for RPM - */ -#include -#include -#include - -#include "md5.h" -#include "messages.h" - -int mdfile(char *fn, unsigned char *digest) { - unsigned char buf[1024]; - unsigned char bindigest[16]; - FILE * fp; - MD5_CTX ctx; - int n; - - fp = fopen(fn, "r"); - if (!fp) { - return 1; - } - - MD5Init(&ctx); - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) - MD5Update(&ctx, buf, n); - MD5Final(bindigest, &ctx); - if (ferror(fp)) { - fclose(fp); - return 1; - } - - sprintf(digest, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" - "%02x%02x%02x%02x%02x", - bindigest[0], bindigest[1], bindigest[2], bindigest[3], - bindigest[4], bindigest[5], bindigest[6], bindigest[7], - bindigest[8], bindigest[9], bindigest[10], bindigest[11], - bindigest[12], bindigest[13], bindigest[14], bindigest[15]); - - fclose(fp); - - return 0; -} +/* + * md5sum.c - Generate/check MD5 Message Digests + * + * Compile and link with md5.c. If you don't have getopt() in your library + * also include getopt.c. For MSDOS you can also link with the wildcard + * initialization function (wildargs.obj for Turbo C and setargv.obj for MSC) + * so that you can use wildcards on the commandline. + * + * Written March 1993 by Branko Lankester + * Modified June 1993 by Colin Plumb for altered md5.c. + * Modified October 1995 by Erik Troan for RPM + */ +#include +#include +#include -int mdbinfile(char *fn, unsigned char *bindigest) { - unsigned char buf[1024]; - FILE * fp; - MD5_CTX ctx; - int n; - - fp = fopen(fn, "r"); - if (!fp) { - return 1; - } - - MD5Init(&ctx); - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) - MD5Update(&ctx, buf, n); - MD5Final(bindigest, &ctx); - if (ferror(fp)) { - fclose(fp); - return 1; - } - - fclose(fp); - - return 0; -} +#include "md5.h" +#include "messages.h" + +static int domd5(char * fn, unsigned char * digest, int asAscii, + int brokenEndian) { + unsigned char buf[1024]; + unsigned char bindigest[16]; + FILE * fp; + MD5_CTX ctx; + int n; + + fp = fopen(fn, "r"); + if (!fp) { + return 1; + } + + MD5Init(&ctx, brokenEndian); + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) + MD5Update(&ctx, buf, n); + MD5Final(bindigest, &ctx); + if (ferror(fp)) { + fclose(fp); + return 1; + } + + if (!asAscii) { + memcpy(digest, bindigest, 16); + } else { + sprintf(digest, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x", + bindigest[0], bindigest[1], bindigest[2], bindigest[3], + bindigest[4], bindigest[5], bindigest[6], bindigest[7], + bindigest[8], bindigest[9], bindigest[10], bindigest[11], + bindigest[12], bindigest[13], bindigest[14], bindigest[15]); + + fclose(fp); + } + + return 0; +} + +int mdbinfile(char *fn, unsigned char *bindigest) { + return domd5(fn, bindigest, 0, 0); +} + +int mdbinfileBroken(char *fn, unsigned char *bindigest) { + return domd5(fn, bindigest, 0, 1); +} + +int mdfile(char *fn, unsigned char *digest) { + return domd5(fn, digest, 1, 0); +} + +int mdfileBroken(char *fn, unsigned char *digest) { + return domd5(fn, digest, 1, 1); +} -- 2.7.4