rfc1320.txt 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. Network Working Group R. Rivest
  2. Request for Comments: 1320 MIT Laboratory for Computer Science
  3. Obsoletes: RFC 1186 and RSA Data Security, Inc.
  4. April 1992
  5. The MD4 Message-Digest Algorithm
  6. Status of thie Memo
  7. This memo provides information for the Internet community. It does
  8. not specify an Internet standard. Distribution of this memo is
  9. unlimited.
  10. Acknowlegements
  11. We would like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle,
  12. and Noam Nisan for numerous helpful comments and suggestions.
  13. Table of Contents
  14. 1. Executive Summary 1
  15. 2. Terminology and Notation 2
  16. 3. MD4 Algorithm Description 2
  17. 4. Summary 6
  18. References 6
  19. APPENDIX A - Reference Implementation 6
  20. Security Considerations 20
  21. Author's Address 20
  22. 1. Executive Summary
  23. This document describes the MD4 message-digest algorithm [1]. The
  24. algorithm takes as input a message of arbitrary length and produces
  25. as output a 128-bit "fingerprint" or "message digest" of the input.
  26. It is conjectured that it is computationally infeasible to produce
  27. two messages having the same message digest, or to produce any
  28. message having a given prespecified target message digest. The MD4
  29. algorithm is intended for digital signature applications, where a
  30. large file must be "compressed" in a secure manner before being
  31. encrypted with a private (secret) key under a public-key cryptosystem
  32. such as RSA.
  33. The MD4 algorithm is designed to be quite fast on 32-bit machines. In
  34. addition, the MD4 algorithm does not require any large substitution
  35. tables; the algorithm can be coded quite compactly.
  36. Rivest [Page 1]
  37. RFC 1320 MD4 Message-Digest Algorithm April 1992
  38. The MD4 algorithm is being placed in the public domain for review and
  39. possible adoption as a standard.
  40. This document replaces the October 1990 RFC 1186 [2]. The main
  41. difference is that the reference implementation of MD4 in the
  42. appendix is more portable.
  43. For OSI-based applications, MD4's object identifier is
  44. md4 OBJECT IDENTIFIER ::=
  45. {iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 4}
  46. In the X.509 type AlgorithmIdentifier [3], the parameters for MD4
  47. should have type NULL.
  48. 2. Terminology and Notation
  49. In this document a "word" is a 32-bit quantity and a "byte" is an
  50. eight-bit quantity. A sequence of bits can be interpreted in a
  51. natural manner as a sequence of bytes, where each consecutive group
  52. of eight bits is interpreted as a byte with the high-order (most
  53. significant) bit of each byte listed first. Similarly, a sequence of
  54. bytes can be interpreted as a sequence of 32-bit words, where each
  55. consecutive group of four bytes is interpreted as a word with the
  56. low-order (least significant) byte given first.
  57. Let x_i denote "x sub i". If the subscript is an expression, we
  58. surround it in braces, as in x_{i+1}. Similarly, we use ^ for
  59. superscripts (exponentiation), so that x^i denotes x to the i-th
  60. power.
  61. Let the symbol "+" denote addition of words (i.e., modulo-2^32
  62. addition). Let X <<< s denote the 32-bit value obtained by circularly
  63. shifting (rotating) X left by s bit positions. Let not(X) denote the
  64. bit-wise complement of X, and let X v Y denote the bit-wise OR of X
  65. and Y. Let X xor Y denote the bit-wise XOR of X and Y, and let XY
  66. denote the bit-wise AND of X and Y.
  67. 3. MD4 Algorithm Description
  68. We begin by supposing that we have a b-bit message as input, and that
  69. we wish to find its message digest. Here b is an arbitrary
  70. nonnegative integer; b may be zero, it need not be a multiple of
  71. eight, and it may be arbitrarily large. We imagine the bits of the
  72. message written down as follows:
  73. m_0 m_1 ... m_{b-1}
  74. Rivest [Page 2]
  75. RFC 1320 MD4 Message-Digest Algorithm April 1992
  76. The following five steps are performed to compute the message digest
  77. of the message.
  78. 3.1 Step 1. Append Padding Bits
  79. The message is "padded" (extended) so that its length (in bits) is
  80. congruent to 448, modulo 512. That is, the message is extended so
  81. that it is just 64 bits shy of being a multiple of 512 bits long.
  82. Padding is always performed, even if the length of the message is
  83. already congruent to 448, modulo 512.
  84. Padding is performed as follows: a single "1" bit is appended to the
  85. message, and then "0" bits are appended so that the length in bits of
  86. the padded message becomes congruent to 448, modulo 512. In all, at
  87. least one bit and at most 512 bits are appended.
  88. 3.2 Step 2. Append Length
  89. A 64-bit representation of b (the length of the message before the
  90. padding bits were added) is appended to the result of the previous
  91. step. In the unlikely event that b is greater than 2^64, then only
  92. the low-order 64 bits of b are used. (These bits are appended as two
  93. 32-bit words and appended low-order word first in accordance with the
  94. previous conventions.)
  95. At this point the resulting message (after padding with bits and with
  96. b) has a length that is an exact multiple of 512 bits. Equivalently,
  97. this message has a length that is an exact multiple of 16 (32-bit)
  98. words. Let M[0 ... N-1] denote the words of the resulting message,
  99. where N is a multiple of 16.
  100. 3.3 Step 3. Initialize MD Buffer
  101. A four-word buffer (A,B,C,D) is used to compute the message digest.
  102. Here each of A, B, C, D is a 32-bit register. These registers are
  103. initialized to the following values in hexadecimal, low-order bytes
  104. first):
  105. word A: 01 23 45 67
  106. word B: 89 ab cd ef
  107. word C: fe dc ba 98
  108. word D: 76 54 32 10
  109. Rivest [Page 3]
  110. RFC 1320 MD4 Message-Digest Algorithm April 1992
  111. 3.4 Step 4. Process Message in 16-Word Blocks
  112. We first define three auxiliary functions that each take as input
  113. three 32-bit words and produce as output one 32-bit word.
  114. F(X,Y,Z) = XY v not(X) Z
  115. G(X,Y,Z) = XY v XZ v YZ
  116. H(X,Y,Z) = X xor Y xor Z
  117. In each bit position F acts as a conditional: if X then Y else Z.
  118. The function F could have been defined using + instead of v since XY
  119. and not(X)Z will never have "1" bits in the same bit position.) In
  120. each bit position G acts as a majority function: if at least two of
  121. X, Y, Z are on, then G has a "1" bit in that bit position, else G has
  122. a "0" bit. It is interesting to note that if the bits of X, Y, and Z
  123. are independent and unbiased, the each bit of f(X,Y,Z) will be
  124. independent and unbiased, and similarly each bit of g(X,Y,Z) will be
  125. independent and unbiased. The function H is the bit-wise XOR or
  126. parity" function; it has properties similar to those of F and G.
  127. Do the following:
  128. Process each 16-word block. */
  129. For i = 0 to N/16-1 do
  130. /* Copy block i into X. */
  131. For j = 0 to 15 do
  132. Set X[j] to M[i*16+j].
  133. end /* of loop on j */
  134. /* Save A as AA, B as BB, C as CC, and D as DD. */
  135. AA = A
  136. BB = B
  137. CC = C
  138. DD = D
  139. /* Round 1. */
  140. /* Let [abcd k s] denote the operation
  141. a = (a + F(b,c,d) + X[k]) <<< s. */
  142. /* Do the following 16 operations. */
  143. [ABCD 0 3] [DABC 1 7] [CDAB 2 11] [BCDA 3 19]
  144. [ABCD 4 3] [DABC 5 7] [CDAB 6 11] [BCDA 7 19]
  145. [ABCD 8 3] [DABC 9 7] [CDAB 10 11] [BCDA 11 19]
  146. [ABCD 12 3] [DABC 13 7] [CDAB 14 11] [BCDA 15 19]
  147. /* Round 2. */
  148. /* Let [abcd k s] denote the operation
  149. a = (a + G(b,c,d) + X[k] + 5A827999) <<< s. */
  150. Rivest [Page 4]
  151. RFC 1320 MD4 Message-Digest Algorithm April 1992
  152. /* Do the following 16 operations. */
  153. [ABCD 0 3] [DABC 4 5] [CDAB 8 9] [BCDA 12 13]
  154. [ABCD 1 3] [DABC 5 5] [CDAB 9 9] [BCDA 13 13]
  155. [ABCD 2 3] [DABC 6 5] [CDAB 10 9] [BCDA 14 13]
  156. [ABCD 3 3] [DABC 7 5] [CDAB 11 9] [BCDA 15 13]
  157. /* Round 3. */
  158. /* Let [abcd k s] denote the operation
  159. a = (a + H(b,c,d) + X[k] + 6ED9EBA1) <<< s. */
  160. /* Do the following 16 operations. */
  161. [ABCD 0 3] [DABC 8 9] [CDAB 4 11] [BCDA 12 15]
  162. [ABCD 2 3] [DABC 10 9] [CDAB 6 11] [BCDA 14 15]
  163. [ABCD 1 3] [DABC 9 9] [CDAB 5 11] [BCDA 13 15]
  164. [ABCD 3 3] [DABC 11 9] [CDAB 7 11] [BCDA 15 15]
  165. /* Then perform the following additions. (That is, increment each
  166. of the four registers by the value it had before this block
  167. was started.) */
  168. A = A + AA
  169. B = B + BB
  170. C = C + CC
  171. D = D + DD
  172. end /* of loop on i */
  173. Note. The value 5A..99 is a hexadecimal 32-bit constant, written with
  174. the high-order digit first. This constant represents the square root
  175. of 2. The octal value of this constant is 013240474631.
  176. The value 6E..A1 is a hexadecimal 32-bit constant, written with the
  177. high-order digit first. This constant represents the square root of
  178. 3. The octal value of this constant is 015666365641.
  179. See Knuth, The Art of Programming, Volume 2 (Seminumerical
  180. Algorithms), Second Edition (1981), Addison-Wesley. Table 2, page
  181. 660.
  182. 3.5 Step 5. Output
  183. The message digest produced as output is A, B, C, D. That is, we
  184. begin with the low-order byte of A, and end with the high-order byte
  185. of D.
  186. This completes the description of MD4. A reference implementation in
  187. C is given in the appendix.
  188. Rivest [Page 5]
  189. RFC 1320 MD4 Message-Digest Algorithm April 1992
  190. 4. Summary
  191. The MD4 message-digest algorithm is simple to implement, and provides
  192. a "fingerprint" or message digest of a message of arbitrary length.
  193. It is conjectured that the difficulty of coming up with two messages
  194. having the same message digest is on the order of 2^64 operations,
  195. and that the difficulty of coming up with any message having a given
  196. message digest is on the order of 2^128 operations. The MD4 algorithm
  197. has been carefully scrutinized for weaknesses. It is, however, a
  198. relatively new algorithm and further security analysis is of course
  199. justified, as is the case with any new proposal of this sort.
  200. References
  201. [1] Rivest, R., "The MD4 message digest algorithm", in A.J. Menezes
  202. and S.A. Vanstone, editors, Advances in Cryptology - CRYPTO '90
  203. Proceedings, pages 303-311, Springer-Verlag, 1991.
  204. [2] Rivest, R., "The MD4 Message Digest Algorithm", RFC 1186, MIT,
  205. October 1990.
  206. [3] CCITT Recommendation X.509 (1988), "The Directory -
  207. Authentication Framework".
  208. [4] Rivest, R., "The MD5 Message-Digest Algorithm", RFC 1321, MIT and
  209. RSA Data Security, Inc, April 1992.
  210. APPENDIX A - Reference Implementation
  211. This appendix contains the following files:
  212. global.h -- global header file
  213. md4.h -- header file for MD4
  214. md4c.c -- source code for MD4
  215. mddriver.c -- test driver for MD2, MD4 and MD5
  216. The driver compiles for MD5 by default but can compile for MD2 or MD4
  217. if the symbol MD is defined on the C compiler command line as 2 or 4.
  218. The implementation is portable and should work on many different
  219. plaforms. However, it is not difficult to optimize the implementation
  220. on particular platforms, an exercise left to the reader. For example,
  221. on "little-endian" platforms where the lowest-addressed byte in a 32-
  222. bit word is the least significant and there are no alignment
  223. restrictions, the call to Decode in MD4Transform can be replaced with
  224. Rivest [Page 6]
  225. RFC 1320 MD4 Message-Digest Algorithm April 1992
  226. a typecast.
  227. A.1 global.h
  228. /* GLOBAL.H - RSAREF types and constants
  229. */
  230. /* PROTOTYPES should be set to one if and only if the compiler supports
  231. function argument prototyping.
  232. The following makes PROTOTYPES default to 0 if it has not already
  233. been defined with C compiler flags.
  234. */
  235. #ifndef PROTOTYPES
  236. #define PROTOTYPES 0
  237. #endif
  238. /* POINTER defines a generic pointer type */
  239. typedef unsigned char *POINTER;
  240. /* UINT2 defines a two byte word */
  241. typedef unsigned short int UINT2;
  242. /* UINT4 defines a four byte word */
  243. typedef unsigned long int UINT4;
  244. /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  245. If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  246. returns an empty list.
  247. */
  248. #if PROTOTYPES
  249. #define PROTO_LIST(list) list
  250. #else
  251. #define PROTO_LIST(list) ()
  252. #endif
  253. A.2 md4.h
  254. /* MD4.H - header file for MD4C.C
  255. */
  256. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  257. rights reserved.
  258. License to copy and use this software is granted provided that it
  259. is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  260. Algorithm" in all material mentioning or referencing this software
  261. or this function.
  262. Rivest [Page 7]
  263. RFC 1320 MD4 Message-Digest Algorithm April 1992
  264. License is also granted to make and use derivative works provided
  265. that such works are identified as "derived from the RSA Data
  266. Security, Inc. MD4 Message-Digest Algorithm" in all material
  267. mentioning or referencing the derived work.
  268. RSA Data Security, Inc. makes no representations concerning either
  269. the merchantability of this software or the suitability of this
  270. software for any particular purpose. It is provided "as is"
  271. without express or implied warranty of any kind.
  272. These notices must be retained in any copies of any part of this
  273. documentation and/or software.
  274. */
  275. /* MD4 context. */
  276. typedef struct {
  277. UINT4 state[4]; /* state (ABCD) */
  278. UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  279. unsigned char buffer[64]; /* input buffer */
  280. } MD4_CTX;
  281. void MD4Init PROTO_LIST ((MD4_CTX *));
  282. void MD4Update PROTO_LIST
  283. ((MD4_CTX *, unsigned char *, unsigned int));
  284. void MD4Final PROTO_LIST ((unsigned char [16], MD4_CTX *));
  285. A.3 md4c.c
  286. /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
  287. */
  288. /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  289. License to copy and use this software is granted provided that it
  290. is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  291. Algorithm" in all material mentioning or referencing this software
  292. or this function.
  293. License is also granted to make and use derivative works provided
  294. that such works are identified as "derived from the RSA Data
  295. Security, Inc. MD4 Message-Digest Algorithm" in all material
  296. mentioning or referencing the derived work.
  297. RSA Data Security, Inc. makes no representations concerning either
  298. the merchantability of this software or the suitability of this
  299. software for any particular purpose. It is provided "as is"
  300. without express or implied warranty of any kind.
  301. Rivest [Page 8]
  302. RFC 1320 MD4 Message-Digest Algorithm April 1992
  303. These notices must be retained in any copies of any part of this
  304. documentation and/or software.
  305. */
  306. #include "global.h"
  307. #include "md4.h"
  308. /* Constants for MD4Transform routine.
  309. */
  310. #define S11 3
  311. #define S12 7
  312. #define S13 11
  313. #define S14 19
  314. #define S21 3
  315. #define S22 5
  316. #define S23 9
  317. #define S24 13
  318. #define S31 3
  319. #define S32 9
  320. #define S33 11
  321. #define S34 15
  322. static void MD4Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  323. static void Encode PROTO_LIST
  324. ((unsigned char *, UINT4 *, unsigned int));
  325. static void Decode PROTO_LIST
  326. ((UINT4 *, unsigned char *, unsigned int));
  327. static void MD4_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  328. static void MD4_memset PROTO_LIST ((POINTER, int, unsigned int));
  329. static unsigned char PADDING[64] = {
  330. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  331. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  332. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  333. };
  334. /* F, G and H are basic MD4 functions.
  335. */
  336. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  337. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  338. #define H(x, y, z) ((x) ^ (y) ^ (z))
  339. /* ROTATE_LEFT rotates x left n bits.
  340. */
  341. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  342. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  343. /* Rotation is separate from addition to prevent recomputation */
  344. Rivest [Page 9]
  345. RFC 1320 MD4 Message-Digest Algorithm April 1992
  346. #define FF(a, b, c, d, x, s) { \
  347. (a) += F ((b), (c), (d)) + (x); \
  348. (a) = ROTATE_LEFT ((a), (s)); \
  349. }
  350. #define GG(a, b, c, d, x, s) { \
  351. (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  352. (a) = ROTATE_LEFT ((a), (s)); \
  353. }
  354. #define HH(a, b, c, d, x, s) { \
  355. (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  356. (a) = ROTATE_LEFT ((a), (s)); \
  357. }
  358. /* MD4 initialization. Begins an MD4 operation, writing a new context.
  359. */
  360. void MD4Init (context)
  361. MD4_CTX *context; /* context */
  362. {
  363. context->count[0] = context->count[1] = 0;
  364. /* Load magic initialization constants.
  365. */
  366. context->state[0] = 0x67452301;
  367. context->state[1] = 0xefcdab89;
  368. context->state[2] = 0x98badcfe;
  369. context->state[3] = 0x10325476;
  370. }
  371. /* MD4 block update operation. Continues an MD4 message-digest
  372. operation, processing another message block, and updating the
  373. context.
  374. */
  375. void MD4Update (context, input, inputLen)
  376. MD4_CTX *context; /* context */
  377. unsigned char *input; /* input block */
  378. unsigned int inputLen; /* length of input block */
  379. {
  380. unsigned int i, index, partLen;
  381. /* Compute number of bytes mod 64 */
  382. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  383. /* Update number of bits */
  384. if ((context->count[0] += ((UINT4)inputLen << 3))
  385. < ((UINT4)inputLen << 3))
  386. context->count[1]++;
  387. context->count[1] += ((UINT4)inputLen >> 29);
  388. partLen = 64 - index;
  389. Rivest [Page 10]
  390. RFC 1320 MD4 Message-Digest Algorithm April 1992
  391. /* Transform as many times as possible.
  392. */
  393. if (inputLen >= partLen) {
  394. MD4_memcpy
  395. ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  396. MD4Transform (context->state, context->buffer);
  397. for (i = partLen; i + 63 < inputLen; i += 64)
  398. MD4Transform (context->state, &input[i]);
  399. index = 0;
  400. }
  401. else
  402. i = 0;
  403. /* Buffer remaining input */
  404. MD4_memcpy
  405. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  406. inputLen-i);
  407. }
  408. /* MD4 finalization. Ends an MD4 message-digest operation, writing the
  409. the message digest and zeroizing the context.
  410. */
  411. void MD4Final (digest, context)
  412. unsigned char digest[16]; /* message digest */
  413. MD4_CTX *context; /* context */
  414. {
  415. unsigned char bits[8];
  416. unsigned int index, padLen;
  417. /* Save number of bits */
  418. Encode (bits, context->count, 8);
  419. /* Pad out to 56 mod 64.
  420. */
  421. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  422. padLen = (index < 56) ? (56 - index) : (120 - index);
  423. MD4Update (context, PADDING, padLen);
  424. /* Append length (before padding) */
  425. MD4Update (context, bits, 8);
  426. /* Store state in digest */
  427. Encode (digest, context->state, 16);
  428. /* Zeroize sensitive information.
  429. */
  430. MD4_memset ((POINTER)context, 0, sizeof (*context));
  431. Rivest [Page 11]
  432. RFC 1320 MD4 Message-Digest Algorithm April 1992
  433. }
  434. /* MD4 basic transformation. Transforms state based on block.
  435. */
  436. static void MD4Transform (state, block)
  437. UINT4 state[4];
  438. unsigned char block[64];
  439. {
  440. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  441. Decode (x, block, 64);
  442. /* Round 1 */
  443. FF (a, b, c, d, x[ 0], S11); /* 1 */
  444. FF (d, a, b, c, x[ 1], S12); /* 2 */
  445. FF (c, d, a, b, x[ 2], S13); /* 3 */
  446. FF (b, c, d, a, x[ 3], S14); /* 4 */
  447. FF (a, b, c, d, x[ 4], S11); /* 5 */
  448. FF (d, a, b, c, x[ 5], S12); /* 6 */
  449. FF (c, d, a, b, x[ 6], S13); /* 7 */
  450. FF (b, c, d, a, x[ 7], S14); /* 8 */
  451. FF (a, b, c, d, x[ 8], S11); /* 9 */
  452. FF (d, a, b, c, x[ 9], S12); /* 10 */
  453. FF (c, d, a, b, x[10], S13); /* 11 */
  454. FF (b, c, d, a, x[11], S14); /* 12 */
  455. FF (a, b, c, d, x[12], S11); /* 13 */
  456. FF (d, a, b, c, x[13], S12); /* 14 */
  457. FF (c, d, a, b, x[14], S13); /* 15 */
  458. FF (b, c, d, a, x[15], S14); /* 16 */
  459. /* Round 2 */
  460. GG (a, b, c, d, x[ 0], S21); /* 17 */
  461. GG (d, a, b, c, x[ 4], S22); /* 18 */
  462. GG (c, d, a, b, x[ 8], S23); /* 19 */
  463. GG (b, c, d, a, x[12], S24); /* 20 */
  464. GG (a, b, c, d, x[ 1], S21); /* 21 */
  465. GG (d, a, b, c, x[ 5], S22); /* 22 */
  466. GG (c, d, a, b, x[ 9], S23); /* 23 */
  467. GG (b, c, d, a, x[13], S24); /* 24 */
  468. GG (a, b, c, d, x[ 2], S21); /* 25 */
  469. GG (d, a, b, c, x[ 6], S22); /* 26 */
  470. GG (c, d, a, b, x[10], S23); /* 27 */
  471. GG (b, c, d, a, x[14], S24); /* 28 */
  472. GG (a, b, c, d, x[ 3], S21); /* 29 */
  473. GG (d, a, b, c, x[ 7], S22); /* 30 */
  474. GG (c, d, a, b, x[11], S23); /* 31 */
  475. GG (b, c, d, a, x[15], S24); /* 32 */
  476. Rivest [Page 12]
  477. RFC 1320 MD4 Message-Digest Algorithm April 1992
  478. /* Round 3 */
  479. HH (a, b, c, d, x[ 0], S31); /* 33 */
  480. HH (d, a, b, c, x[ 8], S32); /* 34 */
  481. HH (c, d, a, b, x[ 4], S33); /* 35 */
  482. HH (b, c, d, a, x[12], S34); /* 36 */
  483. HH (a, b, c, d, x[ 2], S31); /* 37 */
  484. HH (d, a, b, c, x[10], S32); /* 38 */
  485. HH (c, d, a, b, x[ 6], S33); /* 39 */
  486. HH (b, c, d, a, x[14], S34); /* 40 */
  487. HH (a, b, c, d, x[ 1], S31); /* 41 */
  488. HH (d, a, b, c, x[ 9], S32); /* 42 */
  489. HH (c, d, a, b, x[ 5], S33); /* 43 */
  490. HH (b, c, d, a, x[13], S34); /* 44 */
  491. HH (a, b, c, d, x[ 3], S31); /* 45 */
  492. HH (d, a, b, c, x[11], S32); /* 46 */
  493. HH (c, d, a, b, x[ 7], S33); /* 47 */
  494. HH (b, c, d, a, x[15], S34); /* 48 */
  495. state[0] += a;
  496. state[1] += b;
  497. state[2] += c;
  498. state[3] += d;
  499. /* Zeroize sensitive information.
  500. */
  501. MD4_memset ((POINTER)x, 0, sizeof (x));
  502. }
  503. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  504. a multiple of 4.
  505. */
  506. static void Encode (output, input, len)
  507. unsigned char *output;
  508. UINT4 *input;
  509. unsigned int len;
  510. {
  511. unsigned int i, j;
  512. for (i = 0, j = 0; j < len; i++, j += 4) {
  513. output[j] = (unsigned char)(input[i] & 0xff);
  514. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  515. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  516. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  517. }
  518. }
  519. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  520. a multiple of 4.
  521. Rivest [Page 13]
  522. RFC 1320 MD4 Message-Digest Algorithm April 1992
  523. */
  524. static void Decode (output, input, len)
  525. UINT4 *output;
  526. unsigned char *input;
  527. unsigned int len;
  528. {
  529. unsigned int i, j;
  530. for (i = 0, j = 0; j < len; i++, j += 4)
  531. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  532. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  533. }
  534. /* Note: Replace "for loop" with standard memcpy if possible.
  535. */
  536. static void MD4_memcpy (output, input, len)
  537. POINTER output;
  538. POINTER input;
  539. unsigned int len;
  540. {
  541. unsigned int i;
  542. for (i = 0; i < len; i++)
  543. output[i] = input[i];
  544. }
  545. /* Note: Replace "for loop" with standard memset if possible.
  546. */
  547. static void MD4_memset (output, value, len)
  548. POINTER output;
  549. int value;
  550. unsigned int len;
  551. {
  552. unsigned int i;
  553. for (i = 0; i < len; i++)
  554. ((char *)output)[i] = (char)value;
  555. }
  556. A.4 mddriver.c
  557. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  558. */
  559. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  560. rights reserved.
  561. Rivest [Page 14]
  562. RFC 1320 MD4 Message-Digest Algorithm April 1992
  563. RSA Data Security, Inc. makes no representations concerning either
  564. the merchantability of this software or the suitability of this
  565. software for any particular purpose. It is provided "as is"
  566. without express or implied warranty of any kind.
  567. These notices must be retained in any copies of any part of this
  568. documentation and/or software.
  569. */
  570. /* The following makes MD default to MD5 if it has not already been
  571. defined with C compiler flags.
  572. */
  573. #ifndef MD
  574. #define MD MD5
  575. #endif
  576. #include <stdio.h>
  577. #include <time.h>
  578. #include <string.h>
  579. #include "global.h"
  580. #if MD == 2
  581. #include "md2.h"
  582. #endif
  583. #if MD == 4
  584. #include "md4.h"
  585. #endif
  586. #if MD == 5
  587. #include "md5.h"
  588. #endif
  589. /* Length of test block, number of test blocks.
  590. */
  591. #define TEST_BLOCK_LEN 1000
  592. #define TEST_BLOCK_COUNT 1000
  593. static void MDString PROTO_LIST ((char *));
  594. static void MDTimeTrial PROTO_LIST ((void));
  595. static void MDTestSuite PROTO_LIST ((void));
  596. static void MDFile PROTO_LIST ((char *));
  597. static void MDFilter PROTO_LIST ((void));
  598. static void MDPrint PROTO_LIST ((unsigned char [16]));
  599. #if MD == 2
  600. #define MD_CTX MD2_CTX
  601. #define MDInit MD2Init
  602. #define MDUpdate MD2Update
  603. #define MDFinal MD2Final
  604. Rivest [Page 15]
  605. RFC 1320 MD4 Message-Digest Algorithm April 1992
  606. #endif
  607. #if MD == 4
  608. #define MD_CTX MD4_CTX
  609. #define MDInit MD4Init
  610. #define MDUpdate MD4Update
  611. #define MDFinal MD4Final
  612. #endif
  613. #if MD == 5
  614. #define MD_CTX MD5_CTX
  615. #define MDInit MD5Init
  616. #define MDUpdate MD5Update
  617. #define MDFinal MD5Final
  618. #endif
  619. /* Main driver.
  620. Arguments (may be any combination):
  621. -sstring - digests string
  622. -t - runs time trial
  623. -x - runs test script
  624. filename - digests file
  625. (none) - digests standard input
  626. */
  627. int main (argc, argv)
  628. int argc;
  629. char *argv[];
  630. {
  631. int i;
  632. if (argc > 1)
  633. for (i = 1; i < argc; i++)
  634. if (argv[i][0] == '-' && argv[i][1] == 's')
  635. MDString (argv[i] + 2);
  636. else if (strcmp (argv[i], "-t") == 0)
  637. MDTimeTrial ();
  638. else if (strcmp (argv[i], "-x") == 0)
  639. MDTestSuite ();
  640. else
  641. MDFile (argv[i]);
  642. else
  643. MDFilter ();
  644. return (0);
  645. }
  646. /* Digests a string and prints the result.
  647. */
  648. static void MDString (string)
  649. Rivest [Page 16]
  650. RFC 1320 MD4 Message-Digest Algorithm April 1992
  651. char *string;
  652. {
  653. MD_CTX context;
  654. unsigned char digest[16];
  655. unsigned int len = strlen (string);
  656. MDInit (&context);
  657. MDUpdate (&context, string, len);
  658. MDFinal (digest, &context);
  659. printf ("MD%d (\"%s\") = ", MD, string);
  660. MDPrint (digest);
  661. printf ("\n");
  662. }
  663. /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
  664. blocks.
  665. */
  666. static void MDTimeTrial ()
  667. {
  668. MD_CTX context;
  669. time_t endTime, startTime;
  670. unsigned char block[TEST_BLOCK_LEN], digest[16];
  671. unsigned int i;
  672. printf
  673. ("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
  674. TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
  675. /* Initialize block */
  676. for (i = 0; i < TEST_BLOCK_LEN; i++)
  677. block[i] = (unsigned char)(i & 0xff);
  678. /* Start timer */
  679. time (&startTime);
  680. /* Digest blocks */
  681. MDInit (&context);
  682. for (i = 0; i < TEST_BLOCK_COUNT; i++)
  683. MDUpdate (&context, block, TEST_BLOCK_LEN);
  684. MDFinal (digest, &context);
  685. /* Stop timer */
  686. time (&endTime);
  687. printf (" done\n");
  688. printf ("Digest = ");
  689. MDPrint (digest);
  690. Rivest [Page 17]
  691. RFC 1320 MD4 Message-Digest Algorithm April 1992
  692. printf ("\nTime = %ld seconds\n", (long)(endTime-startTime));
  693. printf
  694. ("Speed = %ld bytes/second\n",
  695. (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime));
  696. }
  697. /* Digests a reference suite of strings and prints the results.
  698. */
  699. static void MDTestSuite ()
  700. {
  701. printf ("MD%d test suite:\n", MD);
  702. MDString ("");
  703. MDString ("a");
  704. MDString ("abc");
  705. MDString ("message digest");
  706. MDString ("abcdefghijklmnopqrstuvwxyz");
  707. MDString
  708. ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  709. MDString
  710. ("1234567890123456789012345678901234567890\
  711. 1234567890123456789012345678901234567890");
  712. }
  713. /* Digests a file and prints the result.
  714. */
  715. static void MDFile (filename)
  716. char *filename;
  717. {
  718. FILE *file;
  719. MD_CTX context;
  720. int len;
  721. unsigned char buffer[1024], digest[16];
  722. if ((file = fopen (filename, "rb")) == NULL)
  723. printf ("%s can't be opened\n", filename);
  724. else {
  725. MDInit (&context);
  726. while (len = fread (buffer, 1, 1024, file))
  727. MDUpdate (&context, buffer, len);
  728. MDFinal (digest, &context);
  729. fclose (file);
  730. printf ("MD%d (%s) = ", MD, filename);
  731. MDPrint (digest);
  732. Rivest [Page 18]
  733. RFC 1320 MD4 Message-Digest Algorithm April 1992
  734. printf ("\n");
  735. }
  736. }
  737. /* Digests the standard input and prints the result.
  738. */
  739. static void MDFilter ()
  740. {
  741. MD_CTX context;
  742. int len;
  743. unsigned char buffer[16], digest[16];
  744. MDInit (&context);
  745. while (len = fread (buffer, 1, 16, stdin))
  746. MDUpdate (&context, buffer, len);
  747. MDFinal (digest, &context);
  748. MDPrint (digest);
  749. printf ("\n");
  750. }
  751. /* Prints a message digest in hexadecimal.
  752. */
  753. static void MDPrint (digest)
  754. unsigned char digest[16];
  755. {
  756. unsigned int i;
  757. for (i = 0; i < 16; i++)
  758. printf ("%02x", digest[i]);
  759. }
  760. A.5 Test suite
  761. The MD4 test suite (driver option "-x") should print the following
  762. results:
  763. MD4 test suite:
  764. MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
  765. MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
  766. MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
  767. MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
  768. MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
  769. MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
  770. 043f8582f241db351ce627e153e7f0e4
  771. MD4 ("123456789012345678901234567890123456789012345678901234567890123456
  772. 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
  773. Rivest [Page 19]
  774. RFC 1320 MD4 Message-Digest Algorithm April 1992
  775. Security Considerations
  776. The level of security discussed in this memo is considered to be
  777. sufficient for implementing moderate security hybrid digital-
  778. signature schemes based on MD4 and a public-key cryptosystem. We do
  779. not know of any reason that MD4 would not be sufficient for
  780. implementing very high security digital-signature schemes, but
  781. because MD4 was designed to be exceptionally fast, it is "at the
  782. edge" in terms of risking successful cryptanalytic attack. After
  783. further critical review, it may be appropriate to consider MD4 for
  784. very high security applications. For very high security applications
  785. before the completion of that review, the MD5 algorithm [4] is
  786. recommended.
  787. Author's Address
  788. Ronald L. Rivest
  789. Massachusetts Institute of Technology
  790. Laboratory for Computer Science
  791. NE43-324
  792. 545 Technology Square
  793. Cambridge, MA 02139-1986
  794. Phone: (617) 253-5880
  795. EMail: rivest@theory.lcs.mit.edu
  796. Rivest [Page 20]