home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / lsapi / md4c.c < prev    next >
C/C++ Source or Header  |  1996-04-30  |  10KB  |  325 lines

  1. /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
  2.  */
  3.  
  4. /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  5.  
  6.    License to copy and use this software is granted provided that it
  7.    is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  8.    Algorithm" in all material mentioning or referencing this software
  9.    or this function.
  10.  
  11.    License is also granted to make and use derivative works provided
  12.    that such works are identified as "derived from the RSA Data
  13.    Security, Inc. MD4 Message-Digest Algorithm" in all material
  14.    mentioning or referencing the derived work.
  15.  
  16.    RSA Data Security, Inc. makes no representations concerning either
  17.    the merchantability of this software or the suitability of this
  18.    software for any particular purpose. It is provided "as is"
  19.    without express or implied warranty of any kind.
  20.  
  21.    These notices must be retained in any copies of any part of this
  22.    documentation and/or software.
  23.  */
  24.  
  25. /* This code differs from the MD4 implementation contained in Internet
  26.    RFC-1320 in the following respects:
  27.  
  28.    1. Faster boolean computations for the "F" and "G" functions, as
  29.       suggested by Richard Schoeppel, rcs@cs.arizona.edu, have been included.
  30.       The original code contained in the RFC is retained as comments.
  31.  
  32.    2. PROTO_LIST was removed from the function prototypes.
  33.  */
  34.  
  35. #include "md4.h"
  36.  
  37. /* Constants for MD4Transform routine.
  38.  */
  39. #define S11 3
  40. #define S12 7
  41. #define S13 11
  42. #define S14 19
  43. #define S21 3
  44. #define S22 5
  45. #define S23 9
  46. #define S24 13
  47. #define S31 3
  48. #define S32 9
  49. #define S33 11
  50. #define S34 15
  51.  
  52. static void MD4Transform ( UINT4 *state, unsigned char *block ) ;
  53. static void Encode ( unsigned char *output,
  54.                      UINT4 *input, unsigned int len ) ;
  55. static void Decode ( UINT4 *output,
  56.                      unsigned char *input, unsigned int len ) ;
  57. static void MD4_memcpy ( POINTER output, POINTER input, unsigned int len ) ;
  58. static void MD4_memset ( POINTER output, int value, unsigned int len ) ;
  59.  
  60.  
  61. static unsigned char PADDING[64] = {
  62.   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  63.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  64.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  65. };
  66.  
  67. /* F, G and H are basic MD4 functions.
  68.    Faster methods suggested by Richard Schoeppel, rcs@cs.arizona.edu
  69.  */
  70.  
  71. /* #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))              original code */
  72. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))               /* faster method */
  73. /* #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) original code */
  74. #define G(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))       /* faster method */
  75. #define H(x, y, z) ((x) ^ (y) ^ (z))
  76.  
  77. /* ROTATE_LEFT rotates x left n bits.
  78.  */
  79. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  80.  
  81. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  82. /* Rotation is separate from addition to prevent recomputation */
  83.  
  84. #define FF(a, b, c, d, x, s) { \
  85.     (a) += F ((b), (c), (d)) + (x); \
  86.     (a) = ROTATE_LEFT ((a), (s)); \
  87.   }
  88. #define GG(a, b, c, d, x, s) { \
  89.     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  90.     (a) = ROTATE_LEFT ((a), (s)); \
  91.   }
  92. #define HH(a, b, c, d, x, s) { \
  93.     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  94.     (a) = ROTATE_LEFT ((a), (s)); \
  95.   }
  96.  
  97. /* MD4 initialization. Begins an MD4 operation, writing a new context.
  98.  */
  99.  
  100. void MD4Init ( MD4_CTX *context )               /* context */
  101.  
  102. {
  103.   context->count[0] = context->count[1] = 0;
  104.  
  105.   /* Load magic initialization constants.
  106.    */
  107.   context->state[0] = 0x67452301;
  108.   context->state[1] = 0xefcdab89;
  109.   context->state[2] = 0x98badcfe;
  110.   context->state[3] = 0x10325476;
  111. }
  112.  
  113. /* MD4 block update operation. Continues an MD4 message-digest
  114.      operation, processing another message block, and updating the
  115.      context.
  116.  */
  117.  
  118. void MD4Update ( MD4_CTX *context,              /* context */
  119.                  POINTER input,                 /* input block */
  120.                  unsigned int inputLen )        /* length of input block */
  121.  
  122. {
  123.   unsigned int i, index, partLen;
  124.  
  125.   /* Compute number of bytes mod 64 */
  126.   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  127.   /* Update number of bits */
  128.   if ((context->count[0] += ((UINT4)inputLen << 3))
  129.       < ((UINT4)inputLen << 3))
  130.     context->count[1]++;
  131.   context->count[1] += ((UINT4)inputLen >> 29);
  132.  
  133.   partLen = 64 - index;
  134.  
  135.   /* Transform as many times as possible.
  136.    */
  137.   if (inputLen >= partLen) {
  138.     MD4_memcpy
  139.       ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  140.     MD4Transform (context->state, context->buffer);
  141.  
  142.     for (i = partLen; i + 63 < inputLen; i += 64)
  143.       MD4Transform (context->state, &input[i]);
  144.  
  145.     index = 0;
  146.   }
  147.   else
  148.     i = 0;
  149.  
  150.   /* Buffer remaining input */
  151.   MD4_memcpy
  152.     ((POINTER)&context->buffer[index], (POINTER)&input[i],
  153.      inputLen-i);
  154. }
  155.  
  156. /* MD4 finalization. Ends an MD4 message-digest operation, writing the
  157.      the message digest and zeroizing the context.
  158.  */
  159.  
  160. void MD4Final ( unsigned char *digest,          /* 16-byte message digest */
  161.                 MD4_CTX *context )              /* context */
  162.  
  163. {
  164.   unsigned char bits[8];
  165.   unsigned int index, padLen;
  166.  
  167.   /* Save number of bits */
  168.   Encode (bits, context->count, 8);
  169.  
  170.   /* Pad out to 56 mod 64.
  171.    */
  172.   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  173.   padLen = (index < 56) ? (56 - index) : (120 - index);
  174.   MD4Update (context, PADDING, padLen);
  175.  
  176.   /* Append length (before padding) */
  177.   MD4Update (context, bits, 8);
  178.   /* Store state in digest */
  179.   Encode (digest, context->state, 16);
  180.  
  181.   /* Zeroize sensitive information.
  182.    */
  183.   MD4_memset ((POINTER)context, 0, sizeof (*context));
  184.  
  185. }
  186.  
  187. /* MD4 basic transformation. Transforms state based on block.
  188.  */
  189.  
  190. static void MD4Transform ( UINT4 *state,        /* 16-byte state */
  191.                            unsigned char *block ) /* 64-byte block */
  192.  
  193. {
  194.   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  195.  
  196.   Decode (x, block, 64);
  197.  
  198.   /* Round 1 */
  199.   FF (a, b, c, d, x[ 0], S11); /* 1 */
  200.   FF (d, a, b, c, x[ 1], S12); /* 2 */
  201.   FF (c, d, a, b, x[ 2], S13); /* 3 */
  202.   FF (b, c, d, a, x[ 3], S14); /* 4 */
  203.   FF (a, b, c, d, x[ 4], S11); /* 5 */
  204.   FF (d, a, b, c, x[ 5], S12); /* 6 */
  205.   FF (c, d, a, b, x[ 6], S13); /* 7 */
  206.   FF (b, c, d, a, x[ 7], S14); /* 8 */
  207.   FF (a, b, c, d, x[ 8], S11); /* 9 */
  208.   FF (d, a, b, c, x[ 9], S12); /* 10 */
  209.   FF (c, d, a, b, x[10], S13); /* 11 */
  210.   FF (b, c, d, a, x[11], S14); /* 12 */
  211.   FF (a, b, c, d, x[12], S11); /* 13 */
  212.   FF (d, a, b, c, x[13], S12); /* 14 */
  213.   FF (c, d, a, b, x[14], S13); /* 15 */
  214.   FF (b, c, d, a, x[15], S14); /* 16 */
  215.  
  216.   /* Round 2 */
  217.   GG (a, b, c, d, x[ 0], S21); /* 17 */
  218.   GG (d, a, b, c, x[ 4], S22); /* 18 */
  219.   GG (c, d, a, b, x[ 8], S23); /* 19 */
  220.   GG (b, c, d, a, x[12], S24); /* 20 */
  221.   GG (a, b, c, d, x[ 1], S21); /* 21 */
  222.   GG (d, a, b, c, x[ 5], S22); /* 22 */
  223.   GG (c, d, a, b, x[ 9], S23); /* 23 */
  224.   GG (b, c, d, a, x[13], S24); /* 24 */
  225.   GG (a, b, c, d, x[ 2], S21); /* 25 */
  226.   GG (d, a, b, c, x[ 6], S22); /* 26 */
  227.   GG (c, d, a, b, x[10], S23); /* 27 */
  228.   GG (b, c, d, a, x[14], S24); /* 28 */
  229.   GG (a, b, c, d, x[ 3], S21); /* 29 */
  230.   GG (d, a, b, c, x[ 7], S22); /* 30 */
  231.   GG (c, d, a, b, x[11], S23); /* 31 */
  232.   GG (b, c, d, a, x[15], S24); /* 32 */
  233.  
  234.   /* Round 3 */
  235.   HH (a, b, c, d, x[ 0], S31); /* 33 */
  236.   HH (d, a, b, c, x[ 8], S32); /* 34 */
  237.   HH (c, d, a, b, x[ 4], S33); /* 35 */
  238.   HH (b, c, d, a, x[12], S34); /* 36 */
  239.   HH (a, b, c, d, x[ 2], S31); /* 37 */
  240.   HH (d, a, b, c, x[10], S32); /* 38 */
  241.   HH (c, d, a, b, x[ 6], S33); /* 39 */
  242.   HH (b, c, d, a, x[14], S34); /* 40 */
  243.   HH (a, b, c, d, x[ 1], S31); /* 41 */
  244.   HH (d, a, b, c, x[ 9], S32); /* 42 */
  245.   HH (c, d, a, b, x[ 5], S33); /* 43 */
  246.   HH (b, c, d, a, x[13], S34); /* 44 */
  247.   HH (a, b, c, d, x[ 3], S31); /* 45 */
  248.   HH (d, a, b, c, x[11], S32); /* 46 */
  249.   HH (c, d, a, b, x[ 7], S33); /* 47 */
  250.   HH (b, c, d, a, x[15], S34); /* 48 */
  251.  
  252.   state[0] += a;
  253.   state[1] += b;
  254.   state[2] += c;
  255.   state[3] += d;
  256.  
  257.   /* Zeroize sensitive information.
  258.    */
  259.   MD4_memset ((POINTER)x, 0, sizeof (x));
  260. }
  261.  
  262. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  263.      a multiple of 4.
  264.  */
  265.  
  266. static void Encode ( unsigned char *output,
  267.                      UINT4 *input,
  268.                      unsigned int len )
  269.  
  270. {
  271.   unsigned int i, j;
  272.  
  273.   for (i = 0, j = 0; j < len; i++, j += 4) {
  274.     output[j] = (unsigned char)(input[i] & 0xff);
  275.     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  276.     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  277.     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  278.   }
  279. }
  280.  
  281. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  282.      a multiple of 4.
  283.  */
  284.  
  285. static void Decode ( UINT4 *output,
  286.                      unsigned char *input,
  287.                      unsigned int len )
  288.  
  289. {
  290.   unsigned int i, j;
  291.  
  292.   for (i = 0, j = 0; j < len; i++, j += 4)
  293.     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  294.       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  295. }
  296.  
  297. /* Note: Replace "for loop" with standard memcpy if possible.
  298.  */
  299.  
  300. static void MD4_memcpy ( POINTER output,    /* target string to copy to */
  301.                          POINTER input,     /* source string to copy from */
  302.                          unsigned int len ) /* number of bytes to copy */
  303.  
  304. {
  305.   unsigned int i;
  306.  
  307.   for (i = 0; i < len; i++)
  308.     output[i] = input[i];
  309. }
  310.  
  311. /* Note: Replace "for loop" with standard memset if possible.
  312.  */
  313.  
  314. static void MD4_memset ( POINTER output,     /* first location to set */
  315.                          int value,          /* value to set string to */
  316.                          unsigned int len )  /* number of bytes to set */
  317.  
  318. {
  319.   unsigned int i;
  320.  
  321.   for (i = 0; i < len; i++)
  322.     ((char *)output)[i] = (char)value;
  323. }
  324.  
  325.