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

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                         B. Kaliski
  8. Request for Comments: 1319                              RSA Laboratories
  9. Updates: RFC 1115                                             April 1992
  10.  
  11.  
  12.                      The MD2 Message-Digest Algorithm
  13.  
  14. Status of this Memo
  15.  
  16.    This memo provides information for the Internet community.  It does
  17.    not specify an Internet standard.  Distribution of this memo is
  18.    unlimited.
  19.  
  20. Acknowlegements
  21.  
  22.    The description of MD2 is based on material prepared by John Linn and
  23.    Ron Rivest.  Their permission to incorporate that material is greatly
  24.    appreciated.
  25.  
  26. Table of Contents
  27.  
  28.    1. Executive Summary                                                1
  29.    2. Terminology and Notation                                         2
  30.    3. MD2 Algorithm Description                                        2
  31.    4. Summary                                                          4
  32.    References                                                          5
  33.    APPENDIX A - Reference Implementation                               5
  34.    Security Considerations                                            17
  35.    Author's Address                                                   17
  36.  
  37. 1. Executive Summary
  38.  
  39.    This document describes the MD2 message-digest algorithm. 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 MD2
  45.    algorithm is intended for digital signature applications, where a
  46.    large file must be "compressed" in a secure manner before being
  47.    signed with a private (secret) key under a public-key cryptosystem
  48.    such as RSA.
  49.  
  50.    License to use MD2 is granted for non-commerical Internet Privacy-
  51.    Enhanced Mail [1-3].
  52.  
  53.    This document is an update to the August 1989 RFC 1115 [3], which
  54.    also gives a reference implementation of MD2. The main differences
  55.  
  56.  
  57.  
  58. Kaliski                                                         [Page 1]
  59.  
  60. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  61.  
  62.  
  63.    are that a textual description of MD2 is included, and that the
  64.    reference implementation of MD2 is more portable.
  65.  
  66.    For OSI-based applications, MD2's object identifier is
  67.  
  68.    md2 OBJECT IDENTIFIER ::=
  69.    iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 2}
  70.  
  71.    In the X.509 type AlgorithmIdentifier [4], the parameters for MD2
  72.    should have type NULL.
  73.  
  74. 2. Terminology and Notation
  75.  
  76.    In this document, a "byte" is an eight-bit quantity.
  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 X xor Y denote the bit-wise XOR of X and Y.
  84.  
  85. 3. MD2 Algorithm Description
  86.  
  87.    We begin by supposing that we have a b-byte message as input, and
  88.    that we wish to find its message digest. Here b is an arbitrary
  89.    nonnegative integer; b may be zero, and it may be arbitrarily large.
  90.    We imagine the bytes of the message written down as follows:
  91.  
  92.                    m_0 m_1 ... m_{b-1}
  93.  
  94.    The following five steps are performed to compute the message digest
  95.    of the message.
  96.  
  97. 3.1 Step 1. Append Padding Bytes
  98.  
  99.    The message is "padded" (extended) so that its length (in bytes) is
  100.    congruent to 0, modulo 16. That is, the message is extended so that
  101.    it is a multiple of 16 bytes long. Padding is always performed, even
  102.    if the length of the message is already congruent to 0, modulo 16.
  103.  
  104.    Padding is performed as follows: "i" bytes of value "i" are appended
  105.    to the message so that the length in bytes of the padded message
  106.    becomes congruent to 0, modulo 16. At least one byte and at most 16
  107.    16 bytes are appended.
  108.  
  109.    At this point the resulting message (after padding with bytes) has a
  110.    length that is an exact multiple of 16 bytes. Let M[0 ... N-1] denote
  111.  
  112.  
  113.  
  114. Kaliski                                                         [Page 2]
  115.  
  116. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  117.  
  118.  
  119.    the bytes of the resulting message, where N is a multiple of 16.
  120.  
  121. 3.2 Step 2. Append Checksum
  122.  
  123.    A 16-byte checksum of the message is appended to the result of the
  124.    previous step.
  125.  
  126.    This step uses a 256-byte "random" permutation constructed from the
  127.    digits of pi. Let S[i] denote the i-th element of this table. The
  128.    table is given in the appendix.
  129.  
  130.    Do the following:
  131.  
  132.       /* Clear checksum. */
  133.       For i = 0 to 15 do:
  134.          Set C[i] to 0.
  135.       end /* of loop on i */
  136.  
  137.       Set L to 0.
  138.  
  139.       /* Process each 16-word block. */
  140.       For i = 0 to N/16-1 do
  141.  
  142.          /* Checksum block i. */
  143.          For j = 0 to 15 do
  144.             Set c to M[i*16+j].
  145.             Set C[j] to S[c xor L].
  146.             Set L to C[j].
  147.           end /* of loop on j */
  148.        end /* of loop on i */
  149.  
  150.    The 16-byte checksum C[0 ... 15] is appended to the message. Let M[0
  151.    with checksum), where N' = N + 16.
  152.  
  153. 3.3 Step 3. Initialize MD Buffer
  154.  
  155.    A 48-byte buffer X is used to compute the message digest. The buffer
  156.    is initialized to zero.
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Kaliski                                                         [Page 3]
  171.  
  172. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  173.  
  174.  
  175. 3.4 Step 4. Process Message in 16-Byte Blocks
  176.  
  177.    This step uses the same 256-byte permutation S as step 2 does.
  178.  
  179.    Do the following:
  180.  
  181.       /* Process each 16-word block. */
  182.       For i = 0 to N'/16-1 do
  183.  
  184.          /* Copy block i into X. */
  185.          For j = 0 to 15 do
  186.             Set X[16+j] to M[i*16+j].
  187.             Set X[32+j] to (X[16+j] xor X[j]).
  188.           end /* of loop on j */
  189.  
  190.          Set t to 0.
  191.  
  192.          /* Do 18 rounds. */
  193.          For j = 0 to 17 do
  194.  
  195.             /* Round j. */
  196.             For k = 0 to 47 do
  197.                Set t and X[k] to (X[k] xor S[t]).
  198.             end /* of loop on k */
  199.  
  200.             Set t to (t+j) modulo 256.
  201.          end /* of loop on j */
  202.  
  203.       end /* of loop on i */
  204.  
  205. 3.5 Step 5. Output
  206.  
  207.    The message digest produced as output is X[0 ... 15]. That is, we
  208.    begin with X[0], and end with X[15].
  209.  
  210.    This completes the description of MD2. A reference implementation in
  211.    C is given in the appendix.
  212.  
  213. 4. Summary
  214.  
  215.    The MD2 message-digest algorithm is simple to implement, and provides
  216.    a "fingerprint" or message digest of a message of arbitrary length.
  217.    It is conjectured that the difficulty of coming up with two messages
  218.    having the same message digest is on the order of 2^64 operations,
  219.    and that the difficulty of coming up with any message having a given
  220.    message digest is on the order of 2^128 operations. The MD2 algorithm
  221.    has been carefully scrutinized for weaknesses. It is, however, a
  222.    relatively new algorithm and further security analysis is of course
  223.  
  224.  
  225.  
  226. Kaliski                                                         [Page 4]
  227.  
  228. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  229.  
  230.  
  231.    justified, as is the case with any new proposal of this sort.
  232.  
  233. References
  234.  
  235.    [1] Linn, J., "Privacy Enhancement for Internet Electronic Mail: Part
  236.        I -- Message Encipherment and Authentication Procedures", RFC
  237.        1113, DEC,  IAB Privacy Task Force, August 1989.
  238.  
  239.    [2] Kent, S., and J. Linn, "Privacy Enhancement for Internet
  240.        Electronic Mail: Part II -- Certificate-Based Key Management",
  241.        RFC 1114, BBNCC, DEC, IAB Privacy Task Force, August 1989.
  242.  
  243.    [3] Linn, J., "Privacy Enhancement for Internet Electronic Mail: Part
  244.        III -- Algorithms, Modes, and Identifiers", RFC 1115 DEC, IAB
  245.        Privacy Task Force, August 1989.
  246.  
  247.    [4] CCITT Recommendation X.509 (1988), "The Directory -
  248.        Authentication Framework".
  249.  
  250. APPENDIX A - Reference Implementation
  251.  
  252.    This appendix contains the following files taken from RSAREF: A
  253.    Cryptographic Toolkit for Privacy-Enhanced Mail:
  254.  
  255.      global.h -- global header file
  256.  
  257.      md2.h -- header file for MD2
  258.  
  259.      md2c.c -- source code for MD2
  260.  
  261. For more information on RSAREF, send email to <rsaref@rsa.com>.
  262.  
  263. The appendix also includes the following file:
  264.  
  265.      mddriver.c -- test driver for MD2, MD4 and MD5
  266.  
  267. The driver compiles for MD5 by default but can compile for MD2 or MD4 if
  268. the symbol MD is defined on the C compiler command line as 2 or 4.
  269.  
  270. A.1 global.h
  271.  
  272. /* GLOBAL.H - RSAREF types and constants
  273.  */
  274.  
  275. /* PROTOTYPES should be set to one if and only if the compiler supports
  276.      function argument prototyping.
  277.    The following makes PROTOTYPES default to 0 if it has not already
  278.      been defined with C compiler flags.
  279.  
  280.  
  281.  
  282. Kaliski                                                         [Page 5]
  283.  
  284. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  285.  
  286.  
  287.  */
  288. #ifndef PROTOTYPES
  289. #define PROTOTYPES 0
  290. #endif
  291.  
  292. /* POINTER defines a generic pointer type */
  293. typedef unsigned char *POINTER;
  294.  
  295. /* UINT2 defines a two byte word */
  296. typedef unsigned short int UINT2;
  297.  
  298. /* UINT4 defines a four byte word */
  299. typedef unsigned long int UINT4;
  300.  
  301. /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  302.    If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  303.      returns an empty list.
  304.  */
  305. #if PROTOTYPES
  306. #define PROTO_LIST(list) list
  307. #else
  308. #define PROTO_LIST(list) ()
  309. #endif
  310.  
  311. A.2 md2.h
  312.  
  313. /* MD2.H - header file for MD2C.C
  314.  */
  315.  
  316. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  317.    rights reserved.
  318.  
  319.    License to copy and use this software is granted for
  320.    non-commercial Internet Privacy-Enhanced Mail provided that it is
  321.    identified as the "RSA Data Security, Inc. MD2 Message Digest
  322.    Algorithm" in all material mentioning or referencing this software
  323.    or this function.
  324.  
  325.    RSA Data Security, Inc. makes no representations concerning either
  326.    the merchantability of this software or the suitability of this
  327.    software for any particular purpose. It is provided "as is"
  328.    without express or implied warranty of any kind.
  329.  
  330.    These notices must be retained in any copies of any part of this
  331.    documentation and/or software.
  332.  */
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Kaliski                                                         [Page 6]
  339.  
  340. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  341.  
  342.  
  343. typedef struct {
  344.   unsigned char state[16];                                 /* state */
  345.   unsigned char checksum[16];                           /* checksum */
  346.   unsigned int count;                 /* number of bytes, modulo 16 */
  347.   unsigned char buffer[16];                         /* input buffer */
  348. } MD2_CTX;
  349.  
  350. void MD2Init PROTO_LIST ((MD2_CTX *));
  351. void MD2Update PROTO_LIST
  352.   ((MD2_CTX *, unsigned char *, unsigned int));
  353. void MD2Final PROTO_LIST ((unsigned char [16], MD2_CTX *));
  354.  
  355. A.3 md2c.c
  356.  
  357. /* MD2C.C - RSA Data Security, Inc., MD2 message-digest algorithm
  358.  */
  359.  
  360. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  361.    rights reserved.
  362.  
  363.    License to copy and use this software is granted for
  364.    non-commercial Internet Privacy-Enhanced Mail provided that it is
  365.    identified as the "RSA Data Security, Inc. MD2 Message Digest
  366.    Algorithm" in all material mentioning or referencing this software
  367.    or this function.
  368.  
  369.    RSA Data Security, Inc. makes no representations concerning either
  370.    the merchantability of this software or the suitability of this
  371.    software for any particular purpose. It is provided "as is"
  372.    without express or implied warranty of any kind.
  373.  
  374.    These notices must be retained in any copies of any part of this
  375.    documentation and/or software.
  376.  */
  377.  
  378. #include "global.h"
  379. #include "md2.h"
  380.  
  381. static void MD2Transform PROTO_LIST
  382.   ((unsigned char [16], unsigned char [16], unsigned char [16]));
  383. static void MD2_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  384. static void MD2_memset PROTO_LIST ((POINTER, int, unsigned int));
  385.  
  386. /* Permutation of 0..255 constructed from the digits of pi. It gives a
  387.    "random" nonlinear byte substitution operation.
  388.  */
  389. static unsigned char PI_SUBST[256] = {
  390.   41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
  391.  
  392.  
  393.  
  394. Kaliski                                                         [Page 7]
  395.  
  396. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  397.  
  398.  
  399.   19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
  400.   76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
  401.   138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
  402.   245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
  403.   148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
  404.   39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
  405.   181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
  406.   150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
  407.   112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
  408.   96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  409.   85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
  410.   234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
  411.   129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
  412.   8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
  413.   203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
  414.   166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
  415.   31, 26, 219, 153, 141, 51, 159, 17, 131, 20
  416. };
  417.  
  418. static unsigned char *PADDING[] = {
  419.   (unsigned char *)"",
  420.   (unsigned char *)"\001",
  421.   (unsigned char *)"\002\002",
  422.   (unsigned char *)"\003\003\003",
  423.   (unsigned char *)"\004\004\004\004",
  424.   (unsigned char *)"\005\005\005\005\005",
  425.   (unsigned char *)"\006\006\006\006\006\006",
  426.   (unsigned char *)"\007\007\007\007\007\007\007",
  427.   (unsigned char *)"\010\010\010\010\010\010\010\010",
  428.   (unsigned char *)"\011\011\011\011\011\011\011\011\011",
  429.   (unsigned char *)"\012\012\012\012\012\012\012\012\012\012",
  430.   (unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013",
  431.   (unsigned char *)"\014\014\014\014\014\014\014\014\014\014\014\014",
  432.   (unsigned char *)
  433.     "\015\015\015\015\015\015\015\015\015\015\015\015\015",
  434.   (unsigned char *)
  435.     "\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
  436.   (unsigned char *)
  437.     "\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
  438.   (unsigned char *)
  439.     "\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
  440. };
  441.  
  442. /* MD2 initialization. Begins an MD2 operation, writing a new context.
  443.  */
  444. void MD2Init (context)
  445. MD2_CTX *context;                                        /* context */
  446. {
  447.  
  448.  
  449.  
  450. Kaliski                                                         [Page 8]
  451.  
  452. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  453.  
  454.  
  455.   context->count = 0;
  456.   MD2_memset ((POINTER)context->state, 0, sizeof (context->state));
  457.   MD2_memset
  458.     ((POINTER)context->checksum, 0, sizeof (context->checksum));
  459. }
  460.  
  461. /* MD2 block update operation. Continues an MD2 message-digest
  462.      operation, processing another message block, and updating the
  463.      context.
  464.  */
  465. void MD2Update (context, input, inputLen)
  466. MD2_CTX *context;                                        /* context */
  467. unsigned char *input;                                /* input block */
  468. unsigned int inputLen;                     /* length of input block */
  469. {
  470.   unsigned int i, index, partLen;
  471.  
  472.   /* Update number of bytes mod 16 */
  473.   index = context->count;
  474.   context->count = (index + inputLen) & 0xf;
  475.  
  476.   partLen = 16 - index;
  477.  
  478.   /* Transform as many times as possible.
  479.     */
  480.   if (inputLen >= partLen) {
  481.     MD2_memcpy
  482.       ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  483.     MD2Transform (context->state, context->checksum, context->buffer);
  484.  
  485.     for (i = partLen; i + 15 < inputLen; i += 16)
  486.       MD2Transform (context->state, context->checksum, &input[i]);
  487.  
  488.     index = 0;
  489.   }
  490.   else
  491.     i = 0;
  492.  
  493.   /* Buffer remaining input */
  494.   MD2_memcpy
  495.     ((POINTER)&context->buffer[index], (POINTER)&input[i],
  496.      inputLen-i);
  497. }
  498.  
  499. /* MD2 finalization. Ends an MD2 message-digest operation, writing the
  500.      message digest and zeroizing the context.
  501.  */
  502. void MD2Final (digest, context)
  503.  
  504.  
  505.  
  506. Kaliski                                                         [Page 9]
  507.  
  508. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  509.  
  510.  
  511. unsigned char digest[16];                         /* message digest */
  512. MD2_CTX *context;                                        /* context */
  513. {
  514.   unsigned int index, padLen;
  515.  
  516.   /* Pad out to multiple of 16.
  517.    */
  518.   index = context->count;
  519.   padLen = 16 - index;
  520.   MD2Update (context, PADDING[padLen], padLen);
  521.  
  522.   /* Extend with checksum */
  523.   MD2Update (context, context->checksum, 16);
  524.  
  525.   /* Store state in digest */
  526.   MD2_memcpy ((POINTER)digest, (POINTER)context->state, 16);
  527.  
  528.   /* Zeroize sensitive information.
  529.    */
  530.   MD2_memset ((POINTER)context, 0, sizeof (*context));
  531. }
  532.  
  533. /* MD2 basic transformation. Transforms state and updates checksum
  534.      based on block.
  535.  */
  536. static void MD2Transform (state, checksum, block)
  537. unsigned char state[16];
  538. unsigned char checksum[16];
  539. unsigned char block[16];
  540. {
  541.   unsigned int i, j, t;
  542.   unsigned char x[48];
  543.  
  544.   /* Form encryption block from state, block, state ^ block.
  545.    */
  546.   MD2_memcpy ((POINTER)x, (POINTER)state, 16);
  547.   MD2_memcpy ((POINTER)x+16, (POINTER)block, 16);
  548.   for (i = 0; i < 16; i++)
  549.     x[i+32] = state[i] ^ block[i];
  550.  
  551.   /* Encrypt block (18 rounds).
  552.    */
  553.   t = 0;
  554.   for (i = 0; i < 18; i++) {
  555.     for (j = 0; j < 48; j++)
  556.       t = x[j] ^= PI_SUBST[t];
  557.     t = (t + i) & 0xff;
  558.   }
  559.  
  560.  
  561.  
  562. Kaliski                                                        [Page 10]
  563.  
  564. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  565.  
  566.  
  567.   /* Save new state */
  568.   MD2_memcpy ((POINTER)state, (POINTER)x, 16);
  569.  
  570.   /* Update checksum.
  571.    */
  572.   t = checksum[15];
  573.   for (i = 0; i < 16; i++)
  574.     t = checksum[i] ^= PI_SUBST[block[i] ^ t];
  575.  
  576.   /* Zeroize sensitive information.
  577.    */
  578.   MD2_memset ((POINTER)x, 0, sizeof (x));
  579. }
  580.  
  581. /* Note: Replace "for loop" with standard memcpy if possible.
  582.  */
  583. static void MD2_memcpy (output, input, len)
  584. POINTER output;
  585. POINTER input;
  586. unsigned int len;
  587. {
  588.   unsigned int i;
  589.  
  590.   for (i = 0; i < len; i++)
  591.     output[i] = input[i];
  592. }
  593.  
  594. /* Note: Replace "for loop" with standard memset if possible.
  595.  */
  596. static void MD2_memset (output, value, len)
  597. POINTER output;
  598. int value;
  599. unsigned int len;
  600. {
  601.   unsigned int i;
  602.  
  603.   for (i = 0; i < len; i++)
  604.     ((char *)output)[i] = (char)value;
  605. }
  606.  
  607. A.4 mddriver.c
  608.  
  609. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  610.  */
  611.  
  612. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  613.    rights reserved.
  614.  
  615.  
  616.  
  617.  
  618. Kaliski                                                        [Page 11]
  619.  
  620. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  621.  
  622.  
  623.    RSA Data Security, Inc. makes no representations concerning either
  624.    the merchantability of this software or the suitability of this
  625.    software for any particular purpose. It is provided "as is"
  626.    without express or implied warranty of any kind.
  627.  
  628.    These notices must be retained in any copies of any part of this
  629.    documentation and/or software.
  630.  */
  631.  
  632. /* The following makes MD default to MD5 if it has not already been
  633.      defined with C compiler flags.
  634.  */
  635. #ifndef MD
  636. #define MD MD5
  637. #endif
  638.  
  639. #include <stdio.h>
  640. #include <time.h>
  641. #include <string.h>
  642. #include "global.h"
  643. #if MD == 2
  644. #include "md2.h"
  645. #endif
  646. #if MD == 4
  647. #include "md4.h"
  648. #endif
  649. #if MD == 5
  650. #include "md5.h"
  651. #endif
  652.  
  653. /* Length of test block, number of test blocks.
  654.  */
  655. #define TEST_BLOCK_LEN 1000
  656. #define TEST_BLOCK_COUNT 1000
  657.  
  658. static void MDString PROTO_LIST ((char *));
  659. static void MDTimeTrial PROTO_LIST ((void));
  660. static void MDTestSuite PROTO_LIST ((void));
  661. static void MDFile PROTO_LIST ((char *));
  662. static void MDFilter PROTO_LIST ((void));
  663. static void MDPrint PROTO_LIST ((unsigned char [16]));
  664.  
  665. #if MD == 2
  666. #define MD_CTX MD2_CTX
  667. #define MDInit MD2Init
  668. #define MDUpdate MD2Update
  669. #define MDFinal MD2Final
  670. #endif
  671.  
  672.  
  673.  
  674. Kaliski                                                        [Page 12]
  675.  
  676. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  677.  
  678.  
  679. #if MD == 4
  680. #define MD_CTX MD4_CTX
  681. #define MDInit MD4Init
  682. #define MDUpdate MD4Update
  683. #define MDFinal MD4Final
  684. #endif
  685. #if MD == 5
  686. #define MD_CTX MD5_CTX
  687. #define MDInit MD5Init
  688. #define MDUpdate MD5Update
  689. #define MDFinal MD5Final
  690. #endif
  691.  
  692. /* Main driver.
  693.  
  694.    Arguments (may be any combination):
  695.      -sstring - digests string
  696.      -t       - runs time trial
  697.      -x       - runs test script
  698.      filename - digests file
  699.      (none)   - digests standard input
  700.  */
  701. int main (argc, argv)
  702. int argc;
  703. char *argv[];
  704. {
  705.   int i;
  706.  
  707.   if (argc > 1)
  708.     for (i = 1; i < argc; i++)
  709.       if (argv[i][0] == '-' && argv[i][1] == 's')
  710.         MDString (argv[i] + 2);
  711.       else if (strcmp (argv[i], "-t") == 0)
  712.         MDTimeTrial ();
  713.       else if (strcmp (argv[i], "-x") == 0)
  714.         MDTestSuite ();
  715.       else
  716.         MDFile (argv[i]);
  717.   else
  718.     MDFilter ();
  719.  
  720.   return (0);
  721. }
  722.  
  723. /* Digests a string and prints the result.
  724.  */
  725. static void MDString (string)
  726. char *string;
  727.  
  728.  
  729.  
  730. Kaliski                                                        [Page 13]
  731.  
  732. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  733.  
  734.  
  735. {
  736.   MD_CTX context;
  737.   unsigned char digest[16];
  738.   unsigned int len = strlen (string);
  739.  
  740.   MDInit (&context);
  741.   MDUpdate (&context, string, len);
  742.   MDFinal (digest, &context);
  743.  
  744.   printf ("MD%d (\"%s\") = ", MD, string);
  745.   MDPrint (digest);
  746.   printf ("\n");
  747. }
  748.  
  749. /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
  750.      blocks.
  751.  */
  752. static void MDTimeTrial ()
  753. {
  754.   MD_CTX context;
  755.   time_t endTime, startTime;
  756.   unsigned char block[TEST_BLOCK_LEN], digest[16];
  757.   unsigned int i;
  758.  
  759.   printf
  760.     ("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
  761.      TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
  762.  
  763.   /* Initialize block */
  764.   for (i = 0; i < TEST_BLOCK_LEN; i++)
  765.     block[i] = (unsigned char)(i & 0xff);
  766.  
  767.   /* Start timer */
  768.   time (&startTime);
  769.  
  770.   /* Digest blocks */
  771.   MDInit (&context);
  772.   for (i = 0; i < TEST_BLOCK_COUNT; i++)
  773.     MDUpdate (&context, block, TEST_BLOCK_LEN);
  774.   MDFinal (digest, &context);
  775.  
  776.   /* Stop timer */
  777.   time (&endTime);
  778.  
  779.   printf (" done\n");
  780.   printf ("Digest = ");
  781.   MDPrint (digest);
  782.   printf ("\nTime = %ld seconds\n", (long)(endTime-startTime));
  783.  
  784.  
  785.  
  786. Kaliski                                                        [Page 14]
  787.  
  788. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  789.  
  790.  
  791.   printf
  792.     ("Speed = %ld bytes/second\n",
  793.      (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime));
  794. }
  795. /* Digests a reference suite of strings and prints the results.
  796.  */
  797. static void MDTestSuite ()
  798. {
  799.   printf ("MD%d test suite:\n", MD);
  800.  
  801.   MDString ("");
  802.   MDString ("a");
  803.   MDString ("abc");
  804.   MDString ("message digest");
  805.   MDString ("abcdefghijklmnopqrstuvwxyz");
  806.   MDString
  807.     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  808.   MDString
  809.     ("1234567890123456789012345678901234567890\
  810. 1234567890123456789012345678901234567890");
  811. }
  812.  
  813. /* Digests a file and prints the result.
  814.  */
  815. static void MDFile (filename)
  816. char *filename;
  817. {
  818.   FILE *file;
  819.   MD_CTX context;
  820.   int len;
  821.   unsigned char buffer[1024], digest[16];
  822.  
  823.   if ((file = fopen (filename, "rb")) == NULL)
  824.     printf ("%s can't be opened\n", filename);
  825.  
  826.   else {
  827.     MDInit (&context);
  828.     while (len = fread (buffer, 1, 1024, file))
  829.       MDUpdate (&context, buffer, len);
  830.     MDFinal (digest, &context);
  831.  
  832.     fclose (file);
  833.  
  834.     printf ("MD%d (%s) = ", MD, filename);
  835.     MDPrint (digest);
  836.     printf ("\n");
  837.   }
  838. }
  839.  
  840.  
  841.  
  842. Kaliski                                                        [Page 15]
  843.  
  844. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  845.  
  846.  
  847. /* Digests the standard input and prints the result.
  848.  */
  849. static void MDFilter ()
  850. {
  851.   MD_CTX context;
  852.   int len;
  853.   unsigned char buffer[16], digest[16];
  854.  
  855.   MDInit (&context);
  856.   while (len = fread (buffer, 1, 16, stdin))
  857.     MDUpdate (&context, buffer, len);
  858.   MDFinal (digest, &context);
  859.  
  860.   MDPrint (digest);
  861.   printf ("\n");
  862. }
  863.  
  864. /* Prints a message digest in hexadecimal.
  865.  */
  866. static void MDPrint (digest)
  867. unsigned char digest[16];
  868. {
  869.   unsigned int i;
  870.  
  871.   for (i = 0; i < 16; i++)
  872.     printf ("%02x", digest[i]);
  873. }
  874.  
  875. A.5 Test suite
  876.  
  877.    The MD2 test suite (driver option "-x") should print the following
  878.    results:
  879.  
  880. MD2 test suite:
  881. MD2 ("") = 8350e5a3e24c153df2275c9f80692773
  882. MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
  883. MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
  884. MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
  885. MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
  886. MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
  887. da33def2a42df13975352846c30338cd
  888. MD2 ("123456789012345678901234567890123456789012345678901234567890123456
  889. 78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898. Kaliski                                                        [Page 16]
  899.  
  900. RFC 1319              MD2 Message-Digest Algorithm            April 1992
  901.  
  902.  
  903. Security Considerations
  904.  
  905.    The level of security discussed in this memo is considered to be
  906.    sufficient for implementing very high security hybrid digital
  907.    signature schemes based on MD2 and a public-key cryptosystem.
  908.  
  909. Author's Address
  910.  
  911.    Burton S. Kaliski Jr.
  912.    RSA Laboratories (a division of RSA Data Security, Inc.)
  913.    10 Twin Dolphin Drive
  914.    Redwood City, CA  94065
  915.  
  916.    Phone: (415) 595-8782
  917.    FAX: (415) 595-4126
  918.    EMail: burt@rsa.com
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954. Kaliski                                                        [Page 17]
  955.