home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / beos / utils / skey-1.000 / SKey-1.4.3 / Source / Sources / md4c.c next >
C/C++ Source or Header  |  1997-04-09  |  8KB  |  285 lines

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