works with old, broken MD5 sums RPM used to generate on big endian
authorewt <devnull@localhost>
Sat, 31 Aug 1996 18:35:54 +0000 (18:35 +0000)
committerewt <devnull@localhost>
Sat, 31 Aug 1996 18:35:54 +0000 (18:35 +0000)
machines

CVS patchset: 1007
CVS date: 1996/08/31 18:35:54

lib/md5.c
lib/md5.h
lib/md5sum.c

index 005568b..8d5eaab 100644 (file)
--- a/lib/md5.c
+++ b/lib/md5.c
  * will fill a supplied 16-byte array with the digest.
  */
 #include <string.h>            /* for memcpy() */
+#include <endian.h>
 #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
-
index 2b4b2d9..52b367e 100644 (file)
--- 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.
  */
index dd20b70..9bb6183 100644 (file)
@@ -1,76 +1,72 @@
-/*\r
- * md5sum.c    - Generate/check MD5 Message Digests\r
- *\r
- * Compile and link with md5.c.  If you don't have getopt() in your library\r
- * also include getopt.c.  For MSDOS you can also link with the wildcard\r
- * initialization function (wildargs.obj for Turbo C and setargv.obj for MSC)\r
- * so that you can use wildcards on the commandline.\r
- *\r
- * Written March 1993 by Branko Lankester\r
- * Modified June 1993 by Colin Plumb for altered md5.c.\r
- * Modified October 1995 by Erik Troan for RPM\r
- */\r
-#include <errno.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
-\r
-#include "md5.h"\r
-#include "messages.h"\r
-\r
-int mdfile(char *fn, unsigned char *digest) {\r
-    unsigned char buf[1024];\r
-    unsigned char bindigest[16];\r
-    FILE * fp;\r
-    MD5_CTX ctx;\r
-    int n;\r
-\r
-    fp = fopen(fn, "r");\r
-    if (!fp) {\r
-       return 1;\r
-    }\r
-\r
-    MD5Init(&ctx);\r
-    while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)\r
-           MD5Update(&ctx, buf, n);\r
-    MD5Final(bindigest, &ctx);\r
-    if (ferror(fp)) {\r
-       fclose(fp);\r
-       return 1;\r
-    }\r
-\r
-    sprintf(digest, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"\r
-                    "%02x%02x%02x%02x%02x",\r
-           bindigest[0],  bindigest[1],  bindigest[2],  bindigest[3],\r
-           bindigest[4],  bindigest[5],  bindigest[6],  bindigest[7],\r
-           bindigest[8],  bindigest[9],  bindigest[10], bindigest[11],\r
-           bindigest[12], bindigest[13], bindigest[14], bindigest[15]);\r
-\r
-    fclose(fp);\r
-\r
-    return 0;\r
-}\r
+/*
+ * 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 <errno.h>
+#include <stdio.h>
+#include <string.h>
 
-int mdbinfile(char *fn, unsigned char *bindigest) {\r
-    unsigned char buf[1024];\r
-    FILE * fp;\r
-    MD5_CTX ctx;\r
-    int n;\r
-\r
-    fp = fopen(fn, "r");\r
-    if (!fp) {\r
-       return 1;\r
-    }\r
-\r
-    MD5Init(&ctx);\r
-    while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)\r
-           MD5Update(&ctx, buf, n);\r
-    MD5Final(bindigest, &ctx);\r
-    if (ferror(fp)) {\r
-       fclose(fp);\r
-       return 1;\r
-    }\r
-\r
-    fclose(fp);\r
-\r
-    return 0;\r
-}\r
+#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);
+}