home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Servers / apache-1.2.4-MIHS / original-source / src / md5c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  12.2 KB  |  355 lines

  1. /*
  2.  * This is work is derived from material Copyright RSA Data Security, Inc.
  3.  *
  4.  * The RSA copyright statement and Licence for that original material is
  5.  * included below. This is followed by the Apache copyright statement and
  6.  * licence for the modifications made to that material.
  7.  */
  8.  
  9. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  10.  */
  11.  
  12. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  13. rights reserved.
  14.  
  15. License to copy and use this software is granted provided that it
  16. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  17. Algorithm" in all material mentioning or referencing this software
  18. or this function.
  19.  
  20. License is also granted to make and use derivative works provided
  21. that such works are identified as "derived from the RSA Data
  22. Security, Inc. MD5 Message-Digest Algorithm" in all material
  23. mentioning or referencing the derived work.
  24.  
  25. RSA Data Security, Inc. makes no representations concerning either
  26. the merchantability of this software or the suitability of this
  27. software for any particular purpose. It is provided "as is"
  28. without express or implied warranty of any kind.
  29.  
  30. These notices must be retained in any copies of any part of this
  31. documentation and/or software.
  32.  */
  33.  
  34. /* ====================================================================
  35.  * Copyright (c) 1996,1997 The Apache Group.  All rights reserved.
  36.  *
  37.  * Redistribution and use in source and binary forms, with or without
  38.  * modification, are permitted provided that the following conditions
  39.  * are met:
  40.  *
  41.  * 1. Redistributions of source code must retain the above copyright
  42.  *    notice, this list of conditions and the following disclaimer. 
  43.  *
  44.  * 2. Redistributions in binary form must reproduce the above copyright
  45.  *    notice, this list of conditions and the following disclaimer in
  46.  *    the documentation and/or other materials provided with the
  47.  *    distribution.
  48.  *
  49.  * 3. All advertising materials mentioning features or use of this
  50.  *    software must display the following acknowledgment:
  51.  *    "This product includes software developed by the Apache Group
  52.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  53.  *
  54.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  55.  *    endorse or promote products derived from this software without
  56.  *    prior written permission.
  57.  *
  58.  * 5. Redistributions of any form whatsoever must retain the following
  59.  *    acknowledgment:
  60.  *    "This product includes software developed by the Apache Group
  61.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  62.  *
  63.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  64.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  66.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  67.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  68.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  69.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  70.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  71.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  72.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  73.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  74.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  75.  * ====================================================================
  76.  *
  77.  * This software consists of voluntary contributions made by many
  78.  * individuals on behalf of the Apache Group and was originally based
  79.  * on public domain software written at the National Center for
  80.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  81.  * For more information on the Apache Group and the Apache HTTP server
  82.  * project, please see <http://www.apache.org/>.
  83.  *
  84.  */
  85.  
  86. #include <string.h>
  87.  
  88. #include "md5.h"
  89.  
  90. /* Constants for MD5Transform routine.
  91.  */
  92.  
  93. #define S11 7
  94. #define S12 12
  95. #define S13 17
  96. #define S14 22
  97. #define S21 5
  98. #define S22 9
  99. #define S23 14
  100. #define S24 20
  101. #define S31 4
  102. #define S32 11
  103. #define S33 16
  104. #define S34 23
  105. #define S41 6
  106. #define S42 10
  107. #define S43 15
  108. #define S44 21
  109.  
  110. static void MD5Transform(UINT4 state[4], const unsigned char block[64]);
  111. static void Encode(unsigned char *output, const UINT4 *input,
  112.            unsigned int len);
  113. static void Decode(UINT4 *output, const unsigned char *input,
  114.            unsigned int len);
  115.  
  116. static unsigned char PADDING[64] =
  117. {
  118.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  119.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  120.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  121. };
  122.  
  123. /* F, G, H and I are basic MD5 functions.
  124.  */
  125. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  126. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  127. #define H(x, y, z) ((x) ^ (y) ^ (z))
  128. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  129.  
  130. /* ROTATE_LEFT rotates x left n bits.
  131.  */
  132. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  133.  
  134. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  135. Rotation is separate from addition to prevent recomputation.
  136.  */
  137. #define FF(a, b, c, d, x, s, ac) { \
  138.  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  139.  (a) = ROTATE_LEFT ((a), (s)); \
  140.  (a) += (b); \
  141.   }
  142. #define GG(a, b, c, d, x, s, ac) { \
  143.  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  144.  (a) = ROTATE_LEFT ((a), (s)); \
  145.  (a) += (b); \
  146.   }
  147. #define HH(a, b, c, d, x, s, ac) { \
  148.  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  149.  (a) = ROTATE_LEFT ((a), (s)); \
  150.  (a) += (b); \
  151.   }
  152. #define II(a, b, c, d, x, s, ac) { \
  153.  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  154.  (a) = ROTATE_LEFT ((a), (s)); \
  155.  (a) += (b); \
  156.   }
  157.  
  158. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  159.  */
  160. void
  161. MD5Init(MD5_CTX *context)
  162. {
  163.     context->count[0] = context->count[1] = 0;
  164.   /* Load magic initialization constants. */
  165.     context->state[0] = 0x67452301;
  166.     context->state[1] = 0xefcdab89;
  167.     context->state[2] = 0x98badcfe;
  168.     context->state[3] = 0x10325476;
  169. }
  170.  
  171. /* MD5 block update operation. Continues an MD5 message-digest
  172.   operation, processing another message block, and updating the
  173.   context.
  174.  */
  175. void
  176. MD5Update(MD5_CTX *context, const unsigned char *input, unsigned int inputLen)
  177. {
  178.     unsigned int i, index, partLen;
  179.  
  180.   /* Compute number of bytes mod 64 */
  181.     index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  182.  
  183.   /* Update number of bits */
  184.     if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
  185.     context->count[1]++;
  186.     context->count[1] += (UINT4)inputLen >> 29;
  187.  
  188.     partLen = 64 - index;
  189.  
  190.   /* Transform as many times as possible. */
  191.     if (inputLen >= partLen)
  192.     {
  193.     memcpy(&context->buffer[index], input, partLen);
  194.     MD5Transform(context->state, context->buffer);
  195.  
  196.     for (i = partLen; i + 63 < inputLen; i += 64)
  197.         MD5Transform(context->state, &input[i]);
  198.  
  199.     index = 0;
  200.     }
  201.     else
  202.     i = 0;
  203.  
  204.   /* Buffer remaining input */
  205.     memcpy(&context->buffer[index], &input[i], inputLen-i);
  206. }
  207.  
  208. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  209.   the message digest and zeroizing the context.
  210.  */
  211. void
  212. MD5Final(unsigned char digest[16], MD5_CTX *context)
  213. {
  214.     unsigned char bits[8];
  215.     unsigned int index, padLen;
  216.  
  217.   /* Save number of bits */
  218.     Encode (bits, context->count, 8);
  219.  
  220.   /* Pad out to 56 mod 64. */
  221.     index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  222.     padLen = (index < 56) ? (56 - index) : (120 - index);
  223.     MD5Update(context, PADDING, padLen);
  224.  
  225.   /* Append length (before padding) */
  226.     MD5Update(context, bits, 8);
  227.  
  228.   /* Store state in digest */
  229.     Encode(digest, context->state, 16);
  230.  
  231.   /* Zeroize sensitive information. */
  232.     memset(context, 0, sizeof (*context));
  233. }
  234.  
  235. /* MD5 basic transformation. Transforms state based on block. */
  236. static void
  237. MD5Transform(UINT4 state[4], const unsigned char block[64])
  238. {
  239.     UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  240.  
  241.     Decode (x, block, 64);
  242.  
  243.   /* Round 1 */
  244.     FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  245.     FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  246.     FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  247.     FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  248.     FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  249.     FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  250.     FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  251.     FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  252.     FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  253.     FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  254.     FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  255.     FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  256.     FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  257.     FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  258.     FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  259.     FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  260.     
  261.  /* Round 2 */
  262.     GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  263.     GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  264.     GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  265.     GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  266.     GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  267.     GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  268.     GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  269.     GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  270.     GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  271.     GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  272.     GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  273.     GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  274.     GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  275.     GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  276.     GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  277.     GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  278.     
  279.   /* Round 3 */
  280.     HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  281.     HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  282.     HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  283.     HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  284.     HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  285.     HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  286.     HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  287.     HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  288.     HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  289.     HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  290.     HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  291.     HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  292.     HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  293.     HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  294.     HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  295.     HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  296.  
  297.   /* Round 4 */
  298.     II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  299.     II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  300.     II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  301.     II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  302.     II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  303.     II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  304.     II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  305.     II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  306.     II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  307.     II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  308.     II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  309.     II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  310.     II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  311.     II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  312.     II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  313.     II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  314.  
  315.     state[0] += a;
  316.     state[1] += b;
  317.     state[2] += c;
  318.     state[3] += d;
  319.  
  320.   /* Zeroize sensitive information. */
  321.     memset(x, 0, sizeof (x));
  322. }
  323.  
  324. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  325.   a multiple of 4.
  326.  */
  327. static void
  328. Encode(unsigned char *output, const UINT4 *input, unsigned int len)
  329. {
  330.     unsigned int i, j;
  331.     UINT4 k;
  332.  
  333.     for (i = 0, j = 0; j < len; i++, j += 4)
  334.     {
  335.     k = input[i];
  336.     output[j] = (unsigned char)(k & 0xff);
  337.     output[j+1] = (unsigned char)((k >> 8) & 0xff);
  338.     output[j+2] = (unsigned char)((k >> 16) & 0xff);
  339.     output[j+3] = (unsigned char)((k >> 24) & 0xff);
  340.     }
  341. }
  342.  
  343. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  344.   a multiple of 4.
  345.  */
  346. static void
  347. Decode(UINT4 *output, const unsigned char *input, unsigned int len)
  348. {
  349.     unsigned int i, j;
  350.  
  351.     for (i = 0, j = 0; j < len; i++, j += 4)
  352.     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  353.         (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  354. }
  355.