home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 1100s / rfc1186.txt < prev    next >
Text File  |  1990-10-17  |  34KB  |  1,011 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                         R. Rivest
  8. Request for Comments: 1186          MIT Laboratory for Computer Science
  9.                                                            October 1990
  10.  
  11.  
  12.                     The MD4 Message Digest Algorithm
  13.  
  14. Status of this Memo
  15.  
  16.    This RFC is the specification of the MD4 Digest Algorithm.  If you
  17.    are going to implement MD4, it is suggested you do it this way.  This
  18.    memo is for informational use and does not constitute a standard.
  19.    Distribution of this memo is unlimited.
  20.  
  21. Table of Contents
  22.  
  23.    1.  Abstract ....................................................    1
  24.    2.  Terminology and Notation ....................................    2
  25.    3.  MD4 Algorithm Description ...................................    2
  26.    4.  Extensions ..................................................    6
  27.    5.  Summary .....................................................    7
  28.    6.  Acknowledgements ............................................    7
  29.    APPENDIX - Reference Implementation .............................    7
  30.    Security Considerations..........................................   18
  31.    Author's Address.................................................   18
  32.  
  33. 1. Abstract
  34.  
  35.    This note describes the MD4 message digest algorithm.  The algorithm
  36.    takes as input an input message of arbitrary length and produces as
  37.    output a 128-bit "fingerprint" or "message digest" of the input.  It
  38.    is conjectured that it is computationally infeasible to produce two
  39.    messages having the same message digest, or to produce any message
  40.    having a given prespecified target message digest.  The MD4 algorithm
  41.    is thus ideal for digital signature applications, where a large file
  42.    must be "compressed" in a secure manner before being signed with the
  43.    RSA public-key cryptosystem.
  44.  
  45.    The MD4 algorithm is designed to be quite fast on 32-bit machines.
  46.    On a SUN Sparc station, MD4 runs at 1,450,000 bytes/second.  On a DEC
  47.    MicroVax II, MD4 runs at approximately 70,000 bytes/second.  On a
  48.    20MHz 80286, MD4 runs at approximately 32,000 bytes/second.  In
  49.    addition, the MD4 algorithm does not require any large substitution
  50.    tables; the algorithm can be coded quite compactly.
  51.  
  52.    The MD4 algorithm is being placed in the public domain for review and
  53.    possible adoption as a standard.
  54.  
  55.  
  56.  
  57.  
  58. Rivest                                                          [Page 1]
  59.  
  60. RFC 1186              MD4 Message Digest Algorithm          October 1990
  61.  
  62.  
  63.    (Note: The document supersedes an earlier draft.  The algorithm
  64.    described here is a slight modification of the one described in the
  65.    draft.)
  66.  
  67. 2.  Terminology and Notation
  68.  
  69.    In this note a "word" is a 32-bit quantity and a byte is an 8-bit
  70.    quantity.  A sequence of bits can be interpreted in a natural manner
  71.    as a sequence of bytes, where each consecutive group of 8 bits is
  72.    interpreted as a byte with the high-order (most significant) bit of
  73.    each byte listed first.  Similarly, a sequence of bytes can be
  74.    interpreted as a sequence of 32-bit words, where each consecutive
  75.    group of 4 bytes is interpreted as a word with the low-order (least
  76.    significant) byte given first.
  77.  
  78.    Let x_i denote "x sub i".  If the subscript is an expression, we
  79.    surround it in braces, as in x_{i+1}.  Similarly, we use ^ for
  80.    superscripts (exponentiation), so that x^i denotes x to the i-th
  81.    power.
  82.  
  83.    Let the symbol "+" denote addition of words (i.e., modulo- 2^32
  84.    addition). Let X <<< s denote the 32-bit value obtained by circularly
  85.    shifting (rotating) X left by s bit positions.  Let not(X) denote the
  86.    bit-wise complement of X, and let X v Y denote the bit-wise OR of X
  87.    and Y.  Let X xor Y denote the bit-wise XOR of X and Y, and let XY
  88.    denote the bit-wise AND of X and Y.
  89.  
  90. 3.  MD4 Algorithm Description
  91.  
  92.    We begin by supposing that we have a b-bit message as input, and that
  93.    we wish to find its message digest.  Here b is an arbitrary
  94.    nonnegative integer; b may be zero, it need not be a multiple of 8,
  95.    and it may be arbitrarily large.  We imagine the bits of the message
  96.    written down as follows:
  97.  
  98.                 m_0 m_1 ... m_{b-1} .
  99.  
  100.    The following five steps are performed to compute the message digest
  101.    of the message.
  102.  
  103.       Step 1. Append padding bits
  104.  
  105.          The message is "padded" (extended) so that its length (in bits)
  106.          is congruent to 448, modulo 512.  That is, the message is
  107.          extended so that it is just 64 bits shy of being a multiple of
  108.          512 bits long.  Padding is always performed, even if the length
  109.          of the message is already congruent to 448, modulo 512 (in
  110.          which case 512 bits of padding are added).
  111.  
  112.  
  113.  
  114. Rivest                                                          [Page 2]
  115.  
  116. RFC 1186              MD4 Message Digest Algorithm          October 1990
  117.  
  118.  
  119.          Padding is performed as follows: a single "1" bit is appended
  120.          to the message, and then enough zero bits are appended so that
  121.          the length in bits of the padded message becomes congruent to
  122.          448, modulo 512.
  123.  
  124.       Step 2. Append length
  125.  
  126.          A 64-bit representation of b (the length of the message before
  127.          the padding bits were added) is appended to the result of the
  128.          previous step.  In the unlikely event that b is greater than
  129.          2^64, then only the low-order 64 bits of b are used.  (These
  130.          bits are appended as two 32-bit words and appended low-order
  131.          word first in accordance with the previous conventions.)
  132.  
  133.          At this point the resulting message (after padding with bits
  134.          and with b) has a length that is an exact multiple of 512 bits.
  135.          Equivalently, this message has a length that is an exact
  136.          multiple of 16 (32-bit) words.  Let M[0 ... N-1] denote the
  137.          words of the resulting message, where N is a multiple of 16.
  138.  
  139.       Step 3. Initialize MD buffer
  140.  
  141.          A 4-word buffer (A,B,C,D) is used to compute the message
  142.          digest.  Here each of A,B,C,D are 32-bit registers.  These
  143.          registers are initialized to the following values in
  144.          hexadecimal, low-order bytes first):
  145.  
  146.             word A:    01 23 45 67
  147.             word B:    89 ab cd ef
  148.             word C:    fe dc ba 98
  149.             word D:    76 54 32 10
  150.  
  151.       Step 4. Process message in 16-word blocks
  152.  
  153.          We first define three auxiliary functions that each take
  154.          as input three 32-bit words and produce as output one
  155.          32-bit word.
  156.  
  157.             f(X,Y,Z)  =  XY v not(X)Z
  158.             g(X,Y,Z)  =  XY v XZ v YZ
  159.             h(X,Y,Z)  =  X xor Y xor Z
  160.  
  161.          In each bit position f acts as a conditional: if x then y else
  162.          z.  (The function f could have been defined using + instead of
  163.          v since XY and not(X)Z will never have 1's in the same bit
  164.          position.)  In each bit position g acts as a majority function:
  165.          if at least two of x, y, z are on, then g has a one in that bit
  166.          position, else g has a zero. It is interesting to note that if
  167.  
  168.  
  169.  
  170. Rivest                                                          [Page 3]
  171.  
  172. RFC 1186              MD4 Message Digest Algorithm          October 1990
  173.  
  174.  
  175.          the bits of X, Y, and Z are independent and unbiased, the each
  176.          bit of f(X,Y,Z) will be independent and unbiased, and similarly
  177.          each bit of g(X,Y,Z) will be independent and unbiased.  The
  178.          function h is the bit-wise "xor" or "parity" function; it has
  179.          properties similar to those of f and g.
  180.  
  181.          Do the following:
  182.  
  183.          For i = 0 to N/16-1 do  /* process each 16-word block */
  184.                  For j = 0 to 15 do: /* copy block i into X */
  185.                    Set X[j] to M[i*16+j].
  186.                  end /* of loop on j */
  187.                  Save A as AA, B as BB, C as CC, and D as DD.
  188.  
  189.                  [Round 1]
  190.                    Let [A B C D i s] denote the operation
  191.                          A = (A + f(B,C,D) + X[i]) <<< s  .
  192.                    Do the following 16 operations:
  193.                          [A B C D 0 3]
  194.                          [D A B C 1 7]
  195.                          [C D A B 2 11]
  196.                          [B C D A 3 19]
  197.                          [A B C D 4 3]
  198.                          [D A B C 5 7]
  199.                          [C D A B 6 11]
  200.                          [B C D A 7 19]
  201.                          [A B C D 8 3]
  202.                          [D A B C 9 7]
  203.                          [C D A B 10 11]
  204.                          [B C D A 11 19]
  205.                          [A B C D 12 3]
  206.                          [D A B C 13 7]
  207.                          [C D A B 14 11]
  208.                          [B C D A 15 19]
  209.  
  210.                  [Round 2]
  211.                    Let [A B C D i s] denote the operation
  212.                          A = (A + g(B,C,D) + X[i] + 5A827999) <<< s .
  213.                    (The value 5A..99 is a hexadecimal 32-bit
  214.                    constant, written with the high-order digit
  215.                    first. This constant represents the square
  216.                    root of 2.  The octal value of this constant
  217.                    is 013240474631.  See Knuth, The Art of
  218.                    Programming, Volume 2 (Seminumerical
  219.                    Algorithms), Second Edition (1981),
  220.                    Addison-Wesley.  Table 2, page 660.)
  221.                    Do the following 16 operations:
  222.                          [A B C D 0  3]
  223.  
  224.  
  225.  
  226. Rivest                                                          [Page 4]
  227.  
  228. RFC 1186              MD4 Message Digest Algorithm          October 1990
  229.  
  230.  
  231.                          [D A B C 4  5]
  232.                          [C D A B 8  9]
  233.                          [B C D A 12 13]
  234.                          [A B C D 1  3]
  235.                          [D A B C 5  5]
  236.                          [C D A B 9  9]
  237.                          [B C D A 13 13]
  238.                          [A B C D 2  3]
  239.                          [D A B C 6  5]
  240.                          [C D A B 10 9]
  241.                          [B C D A 14 13]
  242.                          [A B C D 3  3]
  243.                          [D A B C 7  5]
  244.                          [C D A B 11 9]
  245.                          [B C D A 15 13]
  246.  
  247.                  [Round 3]
  248.                    Let [A B C D i s] denote the operation
  249.                          A = (A + h(B,C,D) + X[i] + 6ED9EBA1) <<< s .
  250.                    (The value 6E..A1 is a hexadecimal 32-bit
  251.                    constant, written with the high-order digit
  252.                    first.  This constant represents the square
  253.                    root of 3.  The octal value of this constant
  254.                    is 015666365641.  See Knuth, The Art of
  255.                    Programming, Volume 2 (Seminumerical
  256.                    Algorithms), Second Edition (1981),
  257.                    Addison-Wesley.  Table 2, page 660.)
  258.                    Do the following 16 operations:
  259.                          [A B C D 0  3]
  260.                          [D A B C 8  9]
  261.                          [C D A B 4  11]
  262.                          [B C D A 12 15]
  263.                          [A B C D 2  3]
  264.                          [D A B C 10 9]
  265.                          [C D A B 6  11]
  266.                          [B C D A 14 15]
  267.                          [A B C D 1  3]
  268.                          [D A B C 9  9]
  269.                          [C D A B 5  11]
  270.                          [B C D A 13 15]
  271.                          [A B C D 3  3]
  272.                          [D A B C 11 9]
  273.                          [C D A B 7  11]
  274.                          [B C D A 15 15]
  275.  
  276.          Then perform the following additions:
  277.                          A = A + AA
  278.                          B = B + BB
  279.  
  280.  
  281.  
  282. Rivest                                                          [Page 5]
  283.  
  284. RFC 1186              MD4 Message Digest Algorithm          October 1990
  285.  
  286.  
  287.                          C = C + CC
  288.                          D = D + DD
  289.          (That is, each of the four registers is incremented by
  290.          the value it had before this block was started.)
  291.  
  292.          end /* of loop on i */
  293.  
  294.       Step 5. Output
  295.  
  296.          The message digest produced as output is A,B,C,D.  That is, we
  297.          begin with the low-order byte of A, and end with the high-order
  298.          byte of D.
  299.  
  300.          This completes the description of MD4.  A reference
  301.          implementation in C is given in the Appendix.
  302.  
  303. 4.  Extensions
  304.  
  305.    If more than 128 bits of output are required, then the following
  306.    procedure is recommended to obtain a 256-bit output.  (There is no
  307.    provision made for obtaining more than 256 bits.)
  308.  
  309.    Two copies of MD4 are run in parallel over the input.  The first copy
  310.    is standard as described above.  The second copy is modified as
  311.    follows.
  312.  
  313.    The initial state of the second copy is:
  314.                     word A:    00 11 22 33
  315.                     word B:    44 55 66 77
  316.                     word C:    88 99 aa bb
  317.                     word D:    cc dd ee ff
  318.  
  319.    The magic constants in rounds 2 and 3 for the second copy of MD4 are
  320.    changed from sqrt(2) and sqrt(3) to cuberoot(2) and cuberoot(3):
  321.  
  322.                                     Octal           Hex
  323.             Round 2 constant        012050505746    50a28be6
  324.             Round 3 constant        013423350444    5c4dd124
  325.  
  326.    Finally, after every 16-word block is processed (including the last
  327.    block), the values of the A registers in the two copies are
  328.    exchanged.
  329.  
  330.    The final message digest is obtaining by appending the result of the
  331.    second copy of MD4 to the end of the result of the first copy of MD4.
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Rivest                                                          [Page 6]
  339.  
  340. RFC 1186              MD4 Message Digest Algorithm          October 1990
  341.  
  342.  
  343. 5.  Summary
  344.  
  345.    The MD4 message digest algorithm is simple to implement, and provides
  346.    a "fingerprint" or message digest of a message of arbitrary length.
  347.  
  348.    It is conjectured that the difficulty of coming up with two messages
  349.    having the same message digest is on the order of 2^64 operations,
  350.    and that the difficulty of coming up with any message having a given
  351.    message digest is on the order of 2^128 operations.  The MD4
  352.    algorithm has been carefully scrutinized for weaknesses.  It is,
  353.    however, a relatively new algorithm and further security analysis is
  354.    of course justified, as is the case with any new proposal of this
  355.    sort.  The level of security provided by MD4 should be sufficient for
  356.    implementing very high security hybrid digital signature schemes
  357.    based on MD4 and the RSA public-key cryptosystem.
  358.  
  359. 6.  Acknowledgements
  360.  
  361.    I'd like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle, and
  362.    Noam Nisan for numerous helpful comments and suggestions.
  363.  
  364. APPENDIX - Reference Implementation
  365.  
  366. This appendix contains the following files:
  367.  
  368.          md4.h        -- header file for using MD4 implementation
  369.          md4.c        -- the source code for MD4 routines
  370.          md4driver.c  -- a sample "user" routine
  371.          session      -- sample results of running md4driver
  372.  
  373.  /*
  374.  ** ********************************************************************
  375.  ** md4.h -- Header file for implementation of                        **
  376.  ** MD4 Message Digest Algorithm                                      **
  377.  ** Updated: 2/13/90 by Ronald L. Rivest                              **
  378.  ** (C) 1990 RSA Data Security, Inc.                                  **
  379.  ** ********************************************************************
  380.  */
  381.  
  382.  /* MDstruct is the data structure for a message digest computation.
  383.  */
  384.  typedef struct {
  385.    unsigned int buffer[4]; /* Holds 4-word result of MD computation */
  386.    unsigned char count[8]; /* Number of bits processed so far */
  387.    unsigned int done;      /* Nonzero means MD computation finished */
  388.  } MDstruct, *MDptr;
  389.  
  390.  /* MDbegin(MD)
  391.  
  392.  
  393.  
  394. Rivest                                                          [Page 7]
  395.  
  396. RFC 1186              MD4 Message Digest Algorithm          October 1990
  397.  
  398.  
  399.  ** Input: MD -- an MDptr
  400.  ** Initialize the MDstruct prepatory to doing a message digest
  401.  ** computation.
  402.  */
  403.  extern void MDbegin();
  404.  
  405.  /* MDupdate(MD,X,count)
  406.  ** Input: MD -- an MDptr
  407.  **        X -- a pointer to an array of unsigned characters.
  408.  **        count -- the number of bits of X to use (an unsigned int).
  409.  ** Updates MD using the first "count" bits of X.
  410.  ** The array pointed to by X is not modified.
  411.  ** If count is not a multiple of 8, MDupdate uses high bits of
  412.  ** last byte.
  413.  ** This is the basic input routine for a user.
  414.  ** The routine terminates the MD computation when count < 512, so
  415.  ** every MD computation should end with one call to MDupdate with a
  416.  ** count less than 512.  Zero is OK for a count.
  417.  */
  418.  extern void MDupdate();
  419.  
  420.  /* MDprint(MD)
  421.  ** Input: MD -- an MDptr
  422.  ** Prints message digest buffer MD as 32 hexadecimal digits.
  423.  ** Order is from low-order byte of buffer[0] to high-order byte
  424.  ** of buffer[3].
  425.  ** Each byte is printed with high-order hexadecimal digit first.
  426.  */
  427.  extern void MDprint();
  428.  
  429.  /*
  430.  ** End of md4.h
  431.  ****************************(cut)***********************************/
  432.  
  433.  /*
  434.  ** ********************************************************************
  435.  ** md4.c -- Implementation of MD4 Message Digest Algorithm           **
  436.  ** Updated: 2/16/90 by Ronald L. Rivest                              **
  437.  ** (C) 1990 RSA Data Security, Inc.                                  **
  438.  ** ********************************************************************
  439.  */
  440.  
  441.  /*
  442.  ** To use MD4:
  443.  **   -- Include md4.h in your program
  444.  **   -- Declare an MDstruct MD to hold the state of the digest
  445.  **          computation.
  446.  **   -- Initialize MD using MDbegin(&MD)
  447.  
  448.  
  449.  
  450. Rivest                                                          [Page 8]
  451.  
  452. RFC 1186              MD4 Message Digest Algorithm          October 1990
  453.  
  454.  
  455.  **   -- For each full block (64 bytes) X you wish to process, call
  456.  **          MDupdate(&MD,X,512)
  457.  **      (512 is the number of bits in a full block.)
  458.  **   -- For the last block (less than 64 bytes) you wish to process,
  459.  **          MDupdate(&MD,X,n)
  460.  **      where n is the number of bits in the partial block. A partial
  461.  **      block terminates the computation, so every MD computation
  462.  **      should terminate by processing a partial block, even if it
  463.  **      has n = 0.
  464.  **   -- The message digest is available in MD.buffer[0] ...
  465.  **      MD.buffer[3].  (Least-significant byte of each word
  466.  **      should be output first.)
  467.  **   -- You can print out the digest using MDprint(&MD)
  468.  */
  469.  
  470.  /* Implementation notes:
  471.  ** This implementation assumes that ints are 32-bit quantities.
  472.  ** If the machine stores the least-significant byte of an int in the
  473.  ** least-addressed byte (e.g., VAX and 8086), then LOWBYTEFIRST
  474.  ** should be set to TRUE.  Otherwise (e.g., SUNS), LOWBYTEFIRST
  475.  ** should be set to FALSE.  Note that on machines with LOWBYTEFIRST
  476.  ** FALSE the routine MDupdate modifies has a side-effect on its input
  477.  ** array (the order of bytes in each word are reversed).  If this is
  478.  ** undesired a call to MDreverse(X) can reverse the bytes of X back
  479.  ** into order after each call to MDupdate.
  480.  
  481.  */
  482.  #define TRUE  1
  483.  #define FALSE 0
  484.  #define LOWBYTEFIRST FALSE
  485.  
  486.  /* Compile-time includes
  487.  */
  488.  #include <stdio.h>
  489.  #include "md4.h"
  490.  
  491.  /* Compile-time declarations of MD4 "magic constants".
  492.  */
  493.  #define I0  0x67452301       /* Initial values for MD buffer */
  494.  #define I1  0xefcdab89
  495.  #define I2  0x98badcfe
  496.  #define I3  0x10325476
  497.  #define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  498.  #define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  499.  /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  500.  ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  501.  ** Table 2, page 660.
  502.  */
  503.  
  504.  
  505.  
  506. Rivest                                                          [Page 9]
  507.  
  508. RFC 1186              MD4 Message Digest Algorithm          October 1990
  509.  
  510.  
  511.  #define fs1  3               /* round 1 shift amounts */
  512.  #define fs2  7
  513.  #define fs3 11
  514.  #define fs4 19
  515.  #define gs1  3               /* round 2 shift amounts */
  516.  #define gs2  5
  517.  #define gs3  9
  518.  #define gs4 13
  519.  #define hs1  3               /* round 3 shift amounts */
  520.  #define hs2  9
  521.  #define hs3 11
  522.  #define hs4 15
  523.  
  524.  /* Compile-time macro declarations for MD4.
  525.  ** Note: The "rot" operator uses the variable "tmp".
  526.  ** It assumes tmp is declared as unsigned int, so that the >>
  527.  ** operator will shift in zeros rather than extending the sign bit.
  528.  */
  529.  #define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  530.  #define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  531.  #define h(X,Y,Z)             (X^Y^Z)
  532.  #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  533.  #define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  534.  #define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  535.  #define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  536.  
  537.  /* MDprint(MDp)
  538.  ** Print message digest buffer MDp as 32 hexadecimal digits.
  539.  ** Order is from low-order byte of buffer[0] to high-order byte of
  540.  ** buffer[3].
  541.  ** Each byte is printed with high-order hexadecimal digit first.
  542.  ** This is a user-callable routine.
  543.  */
  544.  void
  545.  MDprint(MDp)
  546.  MDptr MDp;
  547.  { int i,j;
  548.    for (i=0;i<4;i++)
  549.      for (j=0;j<32;j=j+8)
  550.        printf("%02x",(MDp->buffer[i]>>j) & 0xFF);
  551.  }
  552.  
  553.  /* MDbegin(MDp)
  554.  ** Initialize message digest buffer MDp.
  555.  ** This is a user-callable routine.
  556.  */
  557.  void
  558.  MDbegin(MDp)
  559.  
  560.  
  561.  
  562. Rivest                                                         [Page 10]
  563.  
  564. RFC 1186              MD4 Message Digest Algorithm          October 1990
  565.  
  566.  
  567.  MDptr MDp;
  568.  { int i;
  569.    MDp->buffer[0] = I0;
  570.    MDp->buffer[1] = I1;
  571.    MDp->buffer[2] = I2;
  572.    MDp->buffer[3] = I3;
  573.    for (i=0;i<8;i++) MDp->count[i] = 0;
  574.    MDp->done = 0;
  575.  }
  576.  
  577.  /* MDreverse(X)
  578.  ** Reverse the byte-ordering of every int in X.
  579.  ** Assumes X is an array of 16 ints.
  580.  ** The macro revx reverses the byte-ordering of the next word of X.
  581.  */
  582.  #define revx { t = (*X << 16) | (*X >> 16); \
  583.       *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
  584.  MDreverse(X)
  585.  unsigned int *X;
  586.  { register unsigned int t;
  587.    revx; revx; revx; revx; revx; revx; revx; revx;
  588.    revx; revx; revx; revx; revx; revx; revx; revx;
  589.  }
  590.  
  591.  /* MDblock(MDp,X)
  592.  ** Update message digest buffer MDp->buffer using 16-word data block X.
  593.  ** Assumes all 16 words of X are full of data.
  594.  ** Does not update MDp->count.
  595.  ** This routine is not user-callable.
  596.  */
  597.  static void
  598.  MDblock(MDp,X)
  599.  MDptr MDp;
  600.  unsigned int *X;
  601.  {
  602.    register unsigned int tmp, A, B, C, D;
  603.  #if LOWBYTEFIRST == FALSE
  604.    MDreverse(X);
  605.  #endif
  606.    A = MDp->buffer[0];
  607.    B = MDp->buffer[1];
  608.    C = MDp->buffer[2];
  609.    D = MDp->buffer[3];
  610.    /* Update the message digest buffer */
  611.    ff(A , B , C , D ,  0 , fs1); /* Round 1 */
  612.    ff(D , A , B , C ,  1 , fs2);
  613.    ff(C , D , A , B ,  2 , fs3);
  614.    ff(B , C , D , A ,  3 , fs4);
  615.  
  616.  
  617.  
  618. Rivest                                                         [Page 11]
  619.  
  620. RFC 1186              MD4 Message Digest Algorithm          October 1990
  621.  
  622.  
  623.    ff(A , B , C , D ,  4 , fs1);
  624.    ff(D , A , B , C ,  5 , fs2);
  625.    ff(C , D , A , B ,  6 , fs3);
  626.    ff(B , C , D , A ,  7 , fs4);
  627.    ff(A , B , C , D ,  8 , fs1);
  628.    ff(D , A , B , C ,  9 , fs2);
  629.    ff(C , D , A , B , 10 , fs3);
  630.    ff(B , C , D , A , 11 , fs4);
  631.    ff(A , B , C , D , 12 , fs1);
  632.    ff(D , A , B , C , 13 , fs2);
  633.    ff(C , D , A , B , 14 , fs3);
  634.    ff(B , C , D , A , 15 , fs4);
  635.    gg(A , B , C , D ,  0 , gs1); /* Round 2 */
  636.    gg(D , A , B , C ,  4 , gs2);
  637.    gg(C , D , A , B ,  8 , gs3);
  638.    gg(B , C , D , A , 12 , gs4);
  639.    gg(A , B , C , D ,  1 , gs1);
  640.    gg(D , A , B , C ,  5 , gs2);
  641.    gg(C , D , A , B ,  9 , gs3);
  642.    gg(B , C , D , A , 13 , gs4);
  643.    gg(A , B , C , D ,  2 , gs1);
  644.    gg(D , A , B , C ,  6 , gs2);
  645.    gg(C , D , A , B , 10 , gs3);
  646.    gg(B , C , D , A , 14 , gs4);
  647.    gg(A , B , C , D ,  3 , gs1);
  648.    gg(D , A , B , C ,  7 , gs2);
  649.    gg(C , D , A , B , 11 , gs3);
  650.    gg(B , C , D , A , 15 , gs4);
  651.    hh(A , B , C , D ,  0 , hs1); /* Round 3 */
  652.    hh(D , A , B , C ,  8 , hs2);
  653.    hh(C , D , A , B ,  4 , hs3);
  654.    hh(B , C , D , A , 12 , hs4);
  655.    hh(A , B , C , D ,  2 , hs1);
  656.    hh(D , A , B , C , 10 , hs2);
  657.    hh(C , D , A , B ,  6 , hs3);
  658.    hh(B , C , D , A , 14 , hs4);
  659.    hh(A , B , C , D ,  1 , hs1);
  660.    hh(D , A , B , C ,  9 , hs2);
  661.    hh(C , D , A , B ,  5 , hs3);
  662.    hh(B , C , D , A , 13 , hs4);
  663.    hh(A , B , C , D ,  3 , hs1);
  664.    hh(D , A , B , C , 11 , hs2);
  665.    hh(C , D , A , B ,  7 , hs3);
  666.    hh(B , C , D , A , 15 , hs4);
  667.    MDp->buffer[0] += A;
  668.    MDp->buffer[1] += B;
  669.    MDp->buffer[2] += C;
  670.    MDp->buffer[3] += D;
  671.  
  672.  
  673.  
  674. Rivest                                                         [Page 12]
  675.  
  676. RFC 1186              MD4 Message Digest Algorithm          October 1990
  677.  
  678.  
  679.  }
  680.  
  681.  /* MDupdate(MDp,X,count)
  682.  ** Input: MDp -- an MDptr
  683.  **        X -- a pointer to an array of unsigned characters.
  684.  **        count -- the number of bits of X to use.
  685.  **          (if not a multiple of 8, uses high bits of last byte.)
  686.  ** Update MDp using the number of bits of X given by count.
  687.  ** This is the basic input routine for an MD4 user.
  688.  ** The routine completes the MD computation when count < 512, so
  689.  ** every MD computation should end with one call to MDupdate with a
  690.  ** count less than 512.  A call with count 0 will be ignored if the
  691.  ** MD has already been terminated (done != 0), so an extra call with
  692.  ** count 0 can be given as a "courtesy close" to force termination
  693.  ** if desired.
  694.  */
  695.  void
  696.  MDupdate(MDp,X,count)
  697.  MDptr MDp;
  698.  unsigned char *X;
  699.  unsigned int count;
  700.  { unsigned int i, tmp, bit, byte, mask;
  701.    unsigned char XX[64];
  702.    unsigned char *p;
  703.    /* return with no error if this is a courtesy close with count
  704.    ** zero and MDp->done is true.
  705.    */
  706.    if (count == 0 && MDp->done) return;
  707.    /* check to see if MD is already done and report error */
  708.    if (MDp->done)
  709.           { printf("\nError: MDupdate MD already done."); return; }
  710.    /* Add count to MDp->count */
  711.    tmp = count;
  712.    p = MDp->count;
  713.    while (tmp)
  714.      { tmp += *p;
  715.        *p++ = tmp;
  716.        tmp = tmp >> 8;
  717.      }
  718.    /* Process data */
  719.    if (count == 512)
  720.      { /* Full block of data to handle */
  721.        MDblock(MDp,(unsigned int *)X);
  722.      }
  723.    else if (count > 512) /* Check for count too large */
  724.      { printf("\nError: MDupdate called with illegal count value %d."
  725.               ,count);
  726.        return;
  727.  
  728.  
  729.  
  730. Rivest                                                         [Page 13]
  731.  
  732. RFC 1186              MD4 Message Digest Algorithm          October 1990
  733.  
  734.  
  735.      }
  736.    else /* partial block -- must be last block so finish up */
  737.      { /* Find out how many bytes and residual bits there are */
  738.        byte = count >> 3;
  739.        bit =  count & 7;
  740.        /* Copy X into XX since we need to modify it */
  741.        for (i=0;i<=byte;i++)   XX[i] = X[i];
  742.        for (i=byte+1;i<64;i++) XX[i] = 0;
  743.        /* Add padding '1' bit and low-order zeros in last byte */
  744.        mask = 1 << (7 - bit);
  745.        XX[byte] = (XX[byte] | mask) & ~( mask - 1);
  746.        /* If room for bit count, finish up with this block */
  747.        if (byte <= 55)
  748.          { for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
  749.            MDblock(MDp,(unsigned int *)XX);
  750.          }
  751.        else /* need to do two blocks to finish up */
  752.          { MDblock(MDp,(unsigned int *)XX);
  753.            for (i=0;i<56;i++) XX[i] = 0;
  754.            for (i=0;i<8;i++)  XX[56+i] = MDp->count[i];
  755.            MDblock(MDp,(unsigned int *)XX);
  756.          }
  757.        /* Set flag saying we're done with MD computation */
  758.        MDp->done = 1;
  759.      }
  760.  }
  761.  
  762.  /*
  763.  ** End of md4.c
  764.  ****************************(cut)***********************************/
  765.  
  766.  /*
  767.  ** ********************************************************************
  768.  ** md4driver.c -- sample routines to test                            **
  769.  ** MD4 message digest algorithm.                                     **
  770.  ** Updated: 2/16/90 by Ronald L. Rivest                              **
  771.  ** (C) 1990 RSA Data Security, Inc.                                  **
  772.  ** ********************************************************************
  773.  */
  774.  
  775.  #include <stdio.h>
  776.  #include "md4.h"
  777.  
  778.  /* MDtimetrial()
  779.  ** A time trial routine, to measure the speed of MD4.
  780.  ** Measures speed for 1M blocks = 64M bytes.
  781.  */
  782.  MDtimetrial()
  783.  
  784.  
  785.  
  786. Rivest                                                         [Page 14]
  787.  
  788. RFC 1186              MD4 Message Digest Algorithm          October 1990
  789.  
  790.  
  791.  { unsigned int X[16];
  792.    MDstruct MD;
  793.    int i;
  794.    double t;
  795.    for (i=0;i<16;i++) X[i] = 0x01234567 + i;
  796.    printf
  797.    ("MD4 time trial. Processing 1 million 64-character blocks...\n");
  798.    clock();
  799.    MDbegin(&MD);
  800.    for (i=0;i<1000000;i++) MDupdate(&MD,X,512);
  801.    MDupdate(&MD,X,0);
  802.    t = (double) clock(); /* in microseconds */
  803.    MDprint(&MD); printf(" is digest of 64M byte test input.\n");
  804.    printf("Seconds to process test input:   %g\n,t/1e6);
  805.    printf("Characters processed per second: %ld.\n,(int)(64e12/t));
  806.  }
  807.  
  808.  /* MDstring(s)
  809.  ** Computes the message digest for string s.
  810.  ** Prints out message digest, a space, the string (in quotes) and a
  811.  ** carriage return.
  812.  */
  813.  MDstring(s)
  814.  unsigned char *s;
  815.  { unsigned int i, len = strlen(s);
  816.    MDstruct MD;
  817.    MDbegin(&MD);
  818.    for (i=0;i+64<=len;i=i+64) MDupdate(&MD,s+i,512);
  819.    MDupdate(&MD,s+i,(len-i)*8);
  820.    MDprint(&MD);
  821.    printf(" \"%s\"\n",s);
  822.  }
  823.  
  824.  /* MDfile(filename)
  825.  ** Computes the message digest for a specified file.
  826.  ** Prints out message digest, a space, the file name, and a
  827.  ** carriage return.
  828.  */
  829.  MDfile(filename)
  830.  char *filename;
  831.  { FILE *f = fopen(filename,"rb");
  832.    unsigned char X[64];
  833.    MDstruct MD;
  834.    int b;
  835.    if (f == NULL)
  836.       { printf("%s can't be opened.\n",filename); return; }
  837.    MDbegin(&MD);
  838.    while ((b=fread(X,1,64,f))!=0) MDupdate(&MD,X,b*8);
  839.  
  840.  
  841.  
  842. Rivest                                                         [Page 15]
  843.  
  844. RFC 1186              MD4 Message Digest Algorithm          October 1990
  845.  
  846.  
  847.    MDupdate(&MD,X,0);
  848.    MDprint(&MD);
  849.    printf(" %s\n",filename);
  850.    fclose(f);
  851.  }
  852.  
  853.  /* MDfilter()
  854.  ** Writes the message digest of the data from stdin onto stdout,
  855.  ** followed by a carriage return.
  856.  */
  857.  MDfilter()
  858.  { unsigned char X[64];
  859.    MDstruct MD;
  860.    int b;
  861.    MDbegin(&MD);
  862.    while ((b=fread(X,1,64,stdin))!=0) MDupdate(&MD,X,b*8);
  863.    MDupdate(&MD,X,0);
  864.    MDprint(&MD);
  865.    printf("\n");
  866.  }
  867.  
  868.  /* MDtestsuite()
  869.  ** Run a standard suite of test data.
  870.  */
  871.  MDtestsuite()
  872.  {
  873.    printf("MD4 test suite results:\n");
  874.    MDstring("");
  875.    MDstring("a");
  876.    MDstring("abc");
  877.    MDstring("message digest");
  878.    MDstring("abcdefghijklmnopqrstuvwxyz");
  879.    MDstring
  880.    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  881.    MDfile("foo"); /* Contents of file foo are "abc" */
  882.  }
  883.  
  884.  main(argc,argv)
  885.  int argc;
  886.  char *argv[];
  887.  { int i;
  888.    /* For each command line argument in turn:
  889.    ** filename          -- prints message digest and name of file
  890.    ** -sstring          -- prints message digest and contents of string
  891.    ** -t                -- prints time trial statistics for 64M bytes
  892.    ** -x                -- execute a standard suite of test data
  893.    ** (no args)         -- writes messages digest of stdin onto stdout
  894.    */
  895.  
  896.  
  897.  
  898. Rivest                                                         [Page 16]
  899.  
  900. RFC 1186              MD4 Message Digest Algorithm          October 1990
  901.  
  902.  
  903.    if (argc==1) MDfilter();
  904.    else
  905.      for (i=1;i<argc;i++)
  906.        if (argv[i][0]=='-' && argv[i][1]=='s') MDstring(argv[i]+2);
  907.        else if (strcmp(argv[i],"-t")==0)       MDtimetrial();
  908.        else if (strcmp(argv[i],"-x")==0)       MDtestsuite();
  909.        else                                    MDfile(argv[i]);
  910.  }
  911.  
  912.  /*
  913.  ** end of md4driver.c
  914.  ****************************(cut)***********************************/
  915.  
  916.  
  917.  --------------------------------------------------------------------
  918.  --- Sample session.  Compiling and using MD4 on SUN Sparcstation ---
  919.  --------------------------------------------------------------------
  920.  >ls
  921.  total 66
  922.  -rw-rw-r--  1 rivest          3 Feb 14 17:40 abcfile
  923.  -rwxrwxr-x  1 rivest      24576 Feb 17 12:28 md4
  924.  -rw-rw-r--  1 rivest       9347 Feb 17 00:37 md4.c
  925.  -rw-rw-r--  1 rivest      25150 Feb 17 12:25 md4.doc
  926.  -rw-rw-r--  1 rivest       1844 Feb 16 21:21 md4.h
  927.  -rw-rw-r--  1 rivest       3497 Feb 17 12:27 md4driver.c
  928.  >
  929.  >cc -o md4 -O4 md4.c md4driver.c
  930.  md4.c:
  931.  md4driver.c:
  932.  Linking:
  933.  >
  934.  >md4 -x
  935.  MD4 test suite results:
  936.  31d6cfe0d16ae931b73c59d7e0c089c0 ""
  937.  bde52cb31de33e46245e05fbdbd6fb24 "a"
  938.  a448017aaf21d8525fc10ae87aa6729d "abc"
  939.  d9130a8164549fe818874806e1c7014b "message digest"
  940.  d79e1c308aa5bbcdeea8ed63df412da9 "abcdefghijklmnopqrstuvwxyz"
  941.  043f8582f241db351ce627e153e7f0e4
  942.       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  943.  a448017aaf21d8525fc10ae87aa6729d abcfile
  944.  >
  945.  >md4 -sabc -shi
  946.  a448017aaf21d8525fc10ae87aa6729d "abc"
  947.  cfaee2512bd25eb033236f0cd054e308 "hi"
  948.  >
  949.  >md4 *
  950.  a448017aaf21d8525fc10ae87aa6729d abcfile
  951.  
  952.  
  953.  
  954. Rivest                                                         [Page 17]
  955.  
  956. RFC 1186              MD4 Message Digest Algorithm          October 1990
  957.  
  958.  
  959.  d316f994da0e951cf9502928a1f73300 md4
  960.  379adb39eada0dfdbbdfdcd0d9def8c4 md4.c
  961.  9a3f73327c65954198b1f45a3aa12665 md4.doc
  962.  37fe165ac177b461ff78b86d10e4ff33 md4.h
  963.  7dcba2e2dc4d8f1408d08beb17dabb2a md4.o
  964.  08790161bfddc6f5788b4353875cb1c3 md4driver.c
  965.  1f84a7f690b0545d2d0480d5d3c26eea md4driver.o
  966.  >
  967.  >cat abcfile | md4
  968.  a448017aaf21d8525fc10ae87aa6729d
  969.  >
  970.  >md4 -t
  971.  MD4 time trial. Processing 1 million 64-character blocks...
  972.  6325bf77e5891c7c0d8104b64cc6e9ef is digest of 64M byte test input.
  973.  Seconds to process test input:   44.0982
  974.  Characters processed per second: 1451305.
  975.  >
  976.  >
  977.  ------------------------ end of sample session --------------------
  978.  
  979.       Note:  A version of this document including the C source code is
  980.       available for FTP from THEORY.LSC.MIT.EDU in the file "md4.doc".
  981.  
  982. Security Considerations
  983.  
  984.    The level of security discussed in this memo by MD4 is considered to
  985.    be sufficient for implementing very high security hybrid digital
  986.    signature schemes based on MD4 and the RSA public-key cryptosystem.
  987.  
  988. Author's Address
  989.  
  990.    Ronald L. Rivest
  991.    Massachusetts Institute of Technology
  992.    Laboratory for Computer Science
  993.    NE43-324
  994.    545 Technology Square
  995.    Cambridge, MA 02139-1986
  996.  
  997.    Phone: (617) 253-5880
  998.  
  999.    EMail: rivest@theory.lcs.mit.edu
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010. Rivest                                                         [Page 18]
  1011.