home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pwgenera.zip / md4c.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  9KB  |  265 lines

  1. /* md4c.c: "RSA Data Security, Inc. MD4 Message-Digest Algorithm"
  2.  
  3. Portions of this software are Copyright 1996 by Craig Metz, All Rights
  4. Reserved. The Inner Net Copyright Notice and License Agreement applies to
  5. these portions of the software.
  6.  
  7. Portions of this software are Copyright 1995 by Randall Atkinson and Dan
  8. McDonald, All Rights Reserved. All Rights under this copyright are assigned
  9. to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
  10. License Agreement applies to this software.
  11.  
  12.     History:
  13.  
  14.     Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
  15.               Use the real memcpy() and memset(). Use unified context
  16.               structure.
  17.     Modified at NRL for OPIE 2.0.
  18.     Originally from RSADSI reference code.
  19. */
  20. /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  21.  
  22.    License to copy and use this software is granted provided that it
  23.    is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  24.    Algorithm" in all material mentioning or referencing this software
  25.    or this function.
  26.  
  27.    License is also granted to make and use derivative works provided
  28.    that such works are identified as "derived from the RSA Data
  29.    Security, Inc. MD4 Message-Digest Algorithm" in all material
  30.    mentioning or referencing the derived work.
  31.  
  32.    RSA Data Security, Inc. makes no representations concerning either
  33.    the merchantability of this software or the suitability of this
  34.    software for any particular purpose. It is provided "as is"
  35.    without express or implied warranty of any kind.
  36.  
  37.    These notices must be retained in any copies of any part of this
  38.    documentation and/or software.
  39.  */
  40.  
  41. #include "opie_cfg.h"
  42. #include "opie.h"
  43.  
  44. /* Constants for MD4Transform routine.
  45.  */
  46. #define S11 3
  47. #define S12 7
  48. #define S13 11
  49. #define S14 19
  50. #define S21 3
  51. #define S22 5
  52. #define S23 9
  53. #define S24 13
  54. #define S31 3
  55. #define S32 9
  56. #define S33 11
  57. #define S34 15
  58.  
  59. static VOIDRET MD4Transform __P((UINT4[4], unsigned char[64]));
  60. static VOIDRET Encode __P((unsigned char *, UINT4 *, unsigned int));
  61. static VOIDRET Decode __P((UINT4 *, unsigned char *, unsigned int));
  62.  
  63. static unsigned char PADDING[64] =
  64. {
  65.   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  68. };
  69.  
  70. /* F, G and H are basic MD4 functions.
  71.  */
  72. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  73. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  74. #define H(x, y, z) ((x) ^ (y) ^ (z))
  75.  
  76. /* ROTATE_LEFT rotates x left n bits.
  77.  */
  78. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  79.  
  80. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  81. /* Rotation is separate from addition to prevent recomputation */
  82.  
  83. #define FF(a, b, c, d, x, s) { \
  84.     (a) += F ((b), (c), (d)) + (x); \
  85.     (a) = ROTATE_LEFT ((a), (s)); \
  86.   }
  87. #define GG(a, b, c, d, x, s) { \
  88.     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  89.     (a) = ROTATE_LEFT ((a), (s)); \
  90.   }
  91. #define HH(a, b, c, d, x, s) { \
  92.     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  93.     (a) = ROTATE_LEFT ((a), (s)); \
  94.   }
  95.  
  96. /* MD4 initialization. Begins an MD4 operation, writing a new context.
  97.  */
  98. VOIDRET opiemd4init FUNCTION((context), struct opiemdx_ctx *context)
  99. {
  100.   context->count[0] = context->count[1] = 0;
  101.  
  102.   /* Load magic initialization constants. */
  103.   context->state[0] = 0x67452301;
  104.   context->state[1] = 0xefcdab89;
  105.   context->state[2] = 0x98badcfe;
  106.   context->state[3] = 0x10325476;
  107. }
  108.  
  109. /* MD4 block update operation. Continues an MD4 message-digest
  110.    operation, processing another message block, and updating the
  111.    context.
  112.  */
  113. VOIDRET opiemd4update FUNCTION((context, input, inputLen), struct opiemdx_ctx *context AND unsigned char *input AND unsigned int inputLen)
  114. {
  115.   unsigned int i, index, partLen;
  116.  
  117.   /* Compute number of bytes mod 64 */
  118.   index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  119.   /* Update number of bits */
  120.   if ((context->count[0] += ((UINT4) inputLen << 3))
  121.       < ((UINT4) inputLen << 3))
  122.     context->count[1]++;
  123.   context->count[1] += ((UINT4) inputLen >> 29);
  124.  
  125.   partLen = 64 - index;
  126.  
  127.   /* Transform as many times as possible. */
  128.   if (inputLen >= partLen) {
  129.     memcpy((POINTER) & context->buffer[index], (POINTER) input, partLen);
  130.     MD4Transform(context->state, context->buffer);
  131.  
  132.     for (i = partLen; i + 63 < inputLen; i += 64)
  133.       MD4Transform(context->state, &input[i]);
  134.  
  135.     index = 0;
  136.   } else
  137.     i = 0;
  138.  
  139.   /* Buffer remaining input */
  140.   memcpy((POINTER) & context->buffer[index], (POINTER) & input[i], inputLen - i);
  141. }
  142.  
  143. /* MD4 finalization. Ends an MD4 message-digest operation, writing the
  144.    the message digest and zeroizing the context.
  145.  */
  146. VOIDRET opiemd4final FUNCTION((digest, context), unsigned char *digest AND struct opiemdx_ctx *context)
  147. {
  148.   unsigned char bits[8];
  149.   unsigned int index, padLen;
  150.  
  151.   /* Save number of bits */
  152.   Encode(bits, context->count, 8);
  153.  
  154.   /* Pad out to 56 mod 64. */
  155.   index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  156.   padLen = (index < 56) ? (56 - index) : (120 - index);
  157.   opiemd4update(context, PADDING, padLen);
  158.  
  159.   /* Append length (before padding) */
  160.   opiemd4update(context, bits, 8);
  161.   /* Store state in digest */
  162.   Encode(digest, context->state, 16);
  163.  
  164.   /* Zeroize sensitive information. */
  165.   memset((POINTER) context, 0, sizeof(*context));
  166. }
  167.  
  168. /* MD4 basic transformation. Transforms state based on block.
  169.  */
  170. static VOIDRET MD4Transform FUNCTION((state, block), UINT4 state[4] AND unsigned char block[64])
  171. {
  172.   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  173.  
  174.   Decode(x, block, 64);
  175.  
  176.   /* Round 1 */
  177.   FF(a, b, c, d, x[0], S11);    /* 1 */
  178.   FF(d, a, b, c, x[1], S12);    /* 2 */
  179.   FF(c, d, a, b, x[2], S13);    /* 3 */
  180.   FF(b, c, d, a, x[3], S14);    /* 4 */
  181.   FF(a, b, c, d, x[4], S11);    /* 5 */
  182.   FF(d, a, b, c, x[5], S12);    /* 6 */
  183.   FF(c, d, a, b, x[6], S13);    /* 7 */
  184.   FF(b, c, d, a, x[7], S14);    /* 8 */
  185.   FF(a, b, c, d, x[8], S11);    /* 9 */
  186.   FF(d, a, b, c, x[9], S12);    /* 10 */
  187.   FF(c, d, a, b, x[10], S13);    /* 11 */
  188.   FF(b, c, d, a, x[11], S14);    /* 12 */
  189.   FF(a, b, c, d, x[12], S11);    /* 13 */
  190.   FF(d, a, b, c, x[13], S12);    /* 14 */
  191.   FF(c, d, a, b, x[14], S13);    /* 15 */
  192.   FF(b, c, d, a, x[15], S14);    /* 16 */
  193.  
  194.   /* Round 2 */
  195.   GG(a, b, c, d, x[0], S21);    /* 17 */
  196.   GG(d, a, b, c, x[4], S22);    /* 18 */
  197.   GG(c, d, a, b, x[8], S23);    /* 19 */
  198.   GG(b, c, d, a, x[12], S24);    /* 20 */
  199.   GG(a, b, c, d, x[1], S21);    /* 21 */
  200.   GG(d, a, b, c, x[5], S22);    /* 22 */
  201.   GG(c, d, a, b, x[9], S23);    /* 23 */
  202.   GG(b, c, d, a, x[13], S24);    /* 24 */
  203.   GG(a, b, c, d, x[2], S21);    /* 25 */
  204.   GG(d, a, b, c, x[6], S22);    /* 26 */
  205.   GG(c, d, a, b, x[10], S23);    /* 27 */
  206.   GG(b, c, d, a, x[14], S24);    /* 28 */
  207.   GG(a, b, c, d, x[3], S21);    /* 29 */
  208.   GG(d, a, b, c, x[7], S22);    /* 30 */
  209.   GG(c, d, a, b, x[11], S23);    /* 31 */
  210.   GG(b, c, d, a, x[15], S24);    /* 32 */
  211.  
  212.   /* Round 3 */
  213.   HH(a, b, c, d, x[0], S31);    /* 33 */
  214.   HH(d, a, b, c, x[8], S32);    /* 34 */
  215.   HH(c, d, a, b, x[4], S33);    /* 35 */
  216.   HH(b, c, d, a, x[12], S34);    /* 36 */
  217.   HH(a, b, c, d, x[2], S31);    /* 37 */
  218.   HH(d, a, b, c, x[10], S32);    /* 38 */
  219.   HH(c, d, a, b, x[6], S33);    /* 39 */
  220.   HH(b, c, d, a, x[14], S34);    /* 40 */
  221.   HH(a, b, c, d, x[1], S31);    /* 41 */
  222.   HH(d, a, b, c, x[9], S32);    /* 42 */
  223.   HH(c, d, a, b, x[5], S33);    /* 43 */
  224.   HH(b, c, d, a, x[13], S34);    /* 44 */
  225.   HH(a, b, c, d, x[3], S31);    /* 45 */
  226.   HH(d, a, b, c, x[11], S32);    /* 46 */
  227.   HH(c, d, a, b, x[7], S33);    /* 47 */
  228.   HH(b, c, d, a, x[15], S34);    /* 48 */
  229.  
  230.   state[0] += a;
  231.   state[1] += b;
  232.   state[2] += c;
  233.   state[3] += d;
  234.  
  235.   /* Zeroize sensitive information. */
  236.   memset((POINTER) x, 0, sizeof(x));
  237. }
  238.  
  239. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  240.    a multiple of 4.
  241.  */
  242. static VOIDRET Encode FUNCTION((output, input, len), unsigned char *output AND UINT4 *input AND unsigned int len)
  243. {
  244.   unsigned int i, j;
  245.  
  246.   for (i = 0, j = 0; j < len; i++, j += 4) {
  247.     output[j] = (unsigned char) (input[i] & 0xff);
  248.     output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
  249.     output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
  250.     output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
  251.   }
  252. }
  253.  
  254. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  255.    a multiple of 4.
  256.  */
  257. static VOIDRET Decode FUNCTION((output, input, len), UINT4 *output AND unsigned char *input AND unsigned int len)
  258. {
  259.   unsigned int i, j;
  260.  
  261.   for (i = 0, j = 0; j < len; i++, j += 4)
  262.     output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) |
  263.       (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
  264. }
  265.