📄 rfc1186
字号:
RFC 1186 MD4 Message Digest Algorithm October 1990
}
/* MDupdate(MDp,X,count)
** Input: MDp -- an MDptr
** X -- a pointer to an array of unsigned characters.
** count -- the number of bits of X to use.
** (if not a multiple of 8, uses high bits of last byte.)
** Update MDp using the number of bits of X given by count.
** This is the basic input routine for an MD4 user.
** The routine completes the MD computation when count < 512, so
** every MD computation should end with one call to MDupdate with a
** count less than 512. A call with count 0 will be ignored if the
** MD has already been terminated (done != 0), so an extra call with
** count 0 can be given as a "courtesy close" to force termination
** if desired.
*/
void
MDupdate(MDp,X,count)
MDptr MDp;
unsigned char *X;
unsigned int count;
{ unsigned int i, tmp, bit, byte, mask;
unsigned char XX[64];
unsigned char *p;
/* return with no error if this is a courtesy close with count
** zero and MDp->done is true.
*/
if (count == 0 && MDp->done) return;
/* check to see if MD is already done and report error */
if (MDp->done)
{ printf("\nError: MDupdate MD already done."); return; }
/* Add count to MDp->count */
tmp = count;
p = MDp->count;
while (tmp)
{ tmp += *p;
*p++ = tmp;
tmp = tmp >> 8;
}
/* Process data */
if (count == 512)
{ /* Full block of data to handle */
MDblock(MDp,(unsigned int *)X);
}
else if (count > 512) /* Check for count too large */
{ printf("\nError: MDupdate called with illegal count value %d."
,count);
return;
Rivest [Page 13]
RFC 1186 MD4 Message Digest Algorithm October 1990
}
else /* partial block -- must be last block so finish up */
{ /* Find out how many bytes and residual bits there are */
byte = count >> 3;
bit = count & 7;
/* Copy X into XX since we need to modify it */
for (i=0;i<=byte;i++) XX[i] = X[i];
for (i=byte+1;i<64;i++) XX[i] = 0;
/* Add padding '1' bit and low-order zeros in last byte */
mask = 1 << (7 - bit);
XX[byte] = (XX[byte] | mask) & ~( mask - 1);
/* If room for bit count, finish up with this block */
if (byte <= 55)
{ for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
MDblock(MDp,(unsigned int *)XX);
}
else /* need to do two blocks to finish up */
{ MDblock(MDp,(unsigned int *)XX);
for (i=0;i<56;i++) XX[i] = 0;
for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
MDblock(MDp,(unsigned int *)XX);
}
/* Set flag saying we're done with MD computation */
MDp->done = 1;
}
}
/*
** End of md4.c
****************************(cut)***********************************/
/*
** ********************************************************************
** md4driver.c -- sample routines to test **
** MD4 message digest algorithm. **
** Updated: 2/16/90 by Ronald L. Rivest **
** (C) 1990 RSA Data Security, Inc. **
** ********************************************************************
*/
#include <stdio.h>
#include "md4.h"
/* MDtimetrial()
** A time trial routine, to measure the speed of MD4.
** Measures speed for 1M blocks = 64M bytes.
*/
MDtimetrial()
Rivest [Page 14]
RFC 1186 MD4 Message Digest Algorithm October 1990
{ unsigned int X[16];
MDstruct MD;
int i;
double t;
for (i=0;i<16;i++) X[i] = 0x01234567 + i;
printf
("MD4 time trial. Processing 1 million 64-character blocks...\n");
clock();
MDbegin(&MD);
for (i=0;i<1000000;i++) MDupdate(&MD,X,512);
MDupdate(&MD,X,0);
t = (double) clock(); /* in microseconds */
MDprint(&MD); printf(" is digest of 64M byte test input.\n");
printf("Seconds to process test input: %g\n,t/1e6);
printf("Characters processed per second: %ld.\n,(int)(64e12/t));
}
/* MDstring(s)
** Computes the message digest for string s.
** Prints out message digest, a space, the string (in quotes) and a
** carriage return.
*/
MDstring(s)
unsigned char *s;
{ unsigned int i, len = strlen(s);
MDstruct MD;
MDbegin(&MD);
for (i=0;i+64<=len;i=i+64) MDupdate(&MD,s+i,512);
MDupdate(&MD,s+i,(len-i)*8);
MDprint(&MD);
printf(" \"%s\"\n",s);
}
/* MDfile(filename)
** Computes the message digest for a specified file.
** Prints out message digest, a space, the file name, and a
** carriage return.
*/
MDfile(filename)
char *filename;
{ FILE *f = fopen(filename,"rb");
unsigned char X[64];
MDstruct MD;
int b;
if (f == NULL)
{ printf("%s can't be opened.\n",filename); return; }
MDbegin(&MD);
while ((b=fread(X,1,64,f))!=0) MDupdate(&MD,X,b*8);
Rivest [Page 15]
RFC 1186 MD4 Message Digest Algorithm October 1990
MDupdate(&MD,X,0);
MDprint(&MD);
printf(" %s\n",filename);
fclose(f);
}
/* MDfilter()
** Writes the message digest of the data from stdin onto stdout,
** followed by a carriage return.
*/
MDfilter()
{ unsigned char X[64];
MDstruct MD;
int b;
MDbegin(&MD);
while ((b=fread(X,1,64,stdin))!=0) MDupdate(&MD,X,b*8);
MDupdate(&MD,X,0);
MDprint(&MD);
printf("\n");
}
/* MDtestsuite()
** Run a standard suite of test data.
*/
MDtestsuite()
{
printf("MD4 test suite results:\n");
MDstring("");
MDstring("a");
MDstring("abc");
MDstring("message digest");
MDstring("abcdefghijklmnopqrstuvwxyz");
MDstring
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
MDfile("foo"); /* Contents of file foo are "abc" */
}
main(argc,argv)
int argc;
char *argv[];
{ int i;
/* For each command line argument in turn:
** filename -- prints message digest and name of file
** -sstring -- prints message digest and contents of string
** -t -- prints time trial statistics for 64M bytes
** -x -- execute a standard suite of test data
** (no args) -- writes messages digest of stdin onto stdout
*/
Rivest [Page 16]
RFC 1186 MD4 Message Digest Algorithm October 1990
if (argc==1) MDfilter();
else
for (i=1;i<argc;i++)
if (argv[i][0]=='-' && argv[i][1]=='s') MDstring(argv[i]+2);
else if (strcmp(argv[i],"-t")==0) MDtimetrial();
else if (strcmp(argv[i],"-x")==0) MDtestsuite();
else MDfile(argv[i]);
}
/*
** end of md4driver.c
****************************(cut)***********************************/
--------------------------------------------------------------------
--- Sample session. Compiling and using MD4 on SUN Sparcstation ---
--------------------------------------------------------------------
>ls
total 66
-rw-rw-r-- 1 rivest 3 Feb 14 17:40 abcfile
-rwxrwxr-x 1 rivest 24576 Feb 17 12:28 md4
-rw-rw-r-- 1 rivest 9347 Feb 17 00:37 md4.c
-rw-rw-r-- 1 rivest 25150 Feb 17 12:25 md4.doc
-rw-rw-r-- 1 rivest 1844 Feb 16 21:21 md4.h
-rw-rw-r-- 1 rivest 3497 Feb 17 12:27 md4driver.c
>
>cc -o md4 -O4 md4.c md4driver.c
md4.c:
md4driver.c:
Linking:
>
>md4 -x
MD4 test suite results:
31d6cfe0d16ae931b73c59d7e0c089c0 ""
bde52cb31de33e46245e05fbdbd6fb24 "a"
a448017aaf21d8525fc10ae87aa6729d "abc"
d9130a8164549fe818874806e1c7014b "message digest"
d79e1c308aa5bbcdeea8ed63df412da9 "abcdefghijklmnopqrstuvwxyz"
043f8582f241db351ce627e153e7f0e4
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
a448017aaf21d8525fc10ae87aa6729d abcfile
>
>md4 -sabc -shi
a448017aaf21d8525fc10ae87aa6729d "abc"
cfaee2512bd25eb033236f0cd054e308 "hi"
>
>md4 *
a448017aaf21d8525fc10ae87aa6729d abcfile
Rivest [Page 17]
RFC 1186 MD4 Message Digest Algorithm October 1990
d316f994da0e951cf9502928a1f73300 md4
379adb39eada0dfdbbdfdcd0d9def8c4 md4.c
9a3f73327c65954198b1f45a3aa12665 md4.doc
37fe165ac177b461ff78b86d10e4ff33 md4.h
7dcba2e2dc4d8f1408d08beb17dabb2a md4.o
08790161bfddc6f5788b4353875cb1c3 md4driver.c
1f84a7f690b0545d2d0480d5d3c26eea md4driver.o
>
>cat abcfile | md4
a448017aaf21d8525fc10ae87aa6729d
>
>md4 -t
MD4 time trial. Processing 1 million 64-character blocks...
6325bf77e5891c7c0d8104b64cc6e9ef is digest of 64M byte test input.
Seconds to process test input: 44.0982
Characters processed per second: 1451305.
>
>
------------------------ end of sample session --------------------
Note: A version of this document including the C source code is
available for FTP from THEORY.LSC.MIT.EDU in the file "md4.doc".
Security Considerations
The level of security discussed in this memo by MD4 is considered to
be sufficient for implementing very high security hybrid digital
signature schemes based on MD4 and the RSA public-key cryptosystem.
Author's Address
Ronald L. Rivest
Massachusetts Institute of Technology
Laboratory for Computer Science
NE43-324
545 Technology Square
Cambridge, MA 02139-1986
Phone: (617) 253-5880
EMail: rivest@theory.lcs.mit.edu
Rivest [Page 18]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -