[PATCH] Message digest test suite and incomplete MD4 support

Simon Josefsson jas@extundo.com
Sat, 23 Nov 2002 03:40:16 +0100


Simon Josefsson <jas@extundo.com> writes:

> Werner Koch <wk@gnupg.org> writes:
>
>> On Thu, 21 Nov 2002 17:34:47 +0100, Simon Josefsson said:
>>
>>> algo 301, grcy_md_open failed: invalid hash algorithm
>>
>>> Does it work for you?  Note that my original patch didn't work for me
>>
>> No. I had some hope that someone else debugs this ;-). Should be not
>> that complicated.  I am currently hacking on a Mutt version and the
>> time is pressing.  I'll fix it asap and make a new release.
>
> Thanks.  I'll try to debug it too.

Now it works.

Index: cipher/md.c
===================================================================
RCS file: /cvs/gnupg/libgcrypt/cipher/md.c,v
retrieving revision 1.56
diff -u -p -r1.56 md.c
--- cipher/md.c	10 Nov 2002 18:03:28 -0000	1.56
+++ cipher/md.c	23 Nov 2002 02:37:08 -0000
@@ -145,7 +145,7 @@ static int
 load_digest_module( int req_algo )
 {
     static int initialized = 0;
-    static u32 checked_algos[256/32];
+    static u32 checked_algos[512/32];
     static int checked_all = 0;
     struct md_digest_list_s *r;
     void *context = NULL;
@@ -161,7 +161,7 @@ load_digest_module( int req_algo )
 	initialized = 1;
     }
     algo = req_algo;
-    if( algo > 255 || !algo )
+    if( algo > 512 || !algo )
 	return 0; /* algorithm number too high (does not fit into out bitmap)*/
     if( checked_all )
 	return 0; /* already called with -1 */
Index: cipher/md4.c
===================================================================
RCS file: /cvs/gnupg/libgcrypt/cipher/md4.c,v
retrieving revision 1.2
diff -u -p -r1.2 md4.c
--- cipher/md4.c	10 Nov 2002 19:04:54 -0000	1.2
+++ cipher/md4.c	23 Nov 2002 02:37:08 -0000
@@ -90,11 +90,11 @@ burn_stack (int bytes)
         burn_stack (bytes);
 }
 
+// #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
 #define H(x, y, z) ((x) ^ (y) ^ (z))
 
-
 /****************
  * transform n*64 bytes
  */
@@ -102,29 +102,14 @@ static void
 /*transform( MD4_CONTEXT *ctx, const void *buffer, size_t len )*/
 transform( MD4_CONTEXT *ctx, byte *data )
 {
-    u32 correct_words[16];
     register u32 A = ctx->A;
     register u32 B = ctx->B;
     register u32 C = ctx->C;
     register u32 D = ctx->D;
-    u32 *cwp = correct_words;
-
-#ifdef BIG_ENDIAN_HOST
-    { int i;
-      byte *p2, *p1;
-      for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 ) {
-	p2[3] = *p1++;
-	p2[2] = *p1++;
-	p2[1] = *p1++;
-	p2[0] = *p1++;
-      }
-    }
-#else
-    memcpy( correct_words, data, 64 );
-#endif
+    u32 *in = (u32*)data;
 
     /* Round 1.  */
-#define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+data[k],s);
+#define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+in[k],s);
           function(A,B,C,D, 0, 3);
           function(D,A,B,C, 1, 7);
           function(C,D,A,B, 2,11);
@@ -145,7 +130,7 @@ transform( MD4_CONTEXT *ctx, byte *data 
 #undef function
 
     /* Round 2.  */
-#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+data[k]+0x5a827999,s);
+#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
 
           function(A,B,C,D, 0, 3);
           function(D,A,B,C, 4, 5);
@@ -167,7 +152,7 @@ transform( MD4_CONTEXT *ctx, byte *data 
 #undef function
 
     /* Round 3.  */
-#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+data[k]+0x6ed9eba1,s);
+#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
 
           function(A,B,C,D, 0, 3);
           function(D,A,B,C, 8, 9);
@@ -354,7 +339,7 @@ static struct {
     void (*func)(void);
 } func_table[] = {
     { 10, 1, 0, (void(*)(void))md4_get_info },
-    { 11, 1, 1 },
+    { 11, 1, 301 },
 };
 
 
Index: tests/basic.c
===================================================================
RCS file: /cvs/gnupg/libgcrypt/tests/basic.c,v
retrieving revision 1.5
diff -u -p -r1.5 basic.c
--- tests/basic.c	10 Nov 2002 18:01:50 -0000	1.5
+++ tests/basic.c	23 Nov 2002 02:37:09 -0000
@@ -287,7 +287,9 @@ check_digests ()
     { GCRY_MD_MD4, "",
       "\x31\xD6\xCF\xE0\xD1\x6A\xE9\x31\xB7\x3C\x59\xD7\xE0\xC0\x89\xC0" },
     { GCRY_MD_MD4, "a",
-      "\xBD\xE5\x2C\xB3\x1D\xE3\x3E\x46\x24\x5E\x05\xFB\xDB\xD6\xFB\x24" },
+      "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24" },
+    { GCRY_MD_MD4, "message digest",
+      "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b" },
     { GCRY_MD_MD5, "",
       "\xD4\x1D\x8C\xD9\x8F\x00\xB2\x04\xE9\x80\x09\x98\xEC\xF8\x42\x7E" },
     { GCRY_MD_MD5, "a",