home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 1300s / rfc1320.txt < prev    next >
Text File  |  1992-04-14  |  32KB  |  1,123 lines

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