home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / main / md5c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  13.6 KB  |  393 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-1999 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. For written permission, please contact
  57.  *    apache@apache.org.
  58.  *
  59.  * 5. Products derived from this software may not be called "Apache"
  60.  *    nor may "Apache" appear in their names without prior written
  61.  *    permission of the Apache Group.
  62.  *
  63.  * 6. Redistributions of any form whatsoever must retain the following
  64.  *    acknowledgment:
  65.  *    "This product includes software developed by the Apache Group
  66.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  67.  *
  68.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  69.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  71.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  72.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  73.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  74.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  75.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  76.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  77.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  78.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  79.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  80.  * ====================================================================
  81.  *
  82.  * This software consists of voluntary contributions made by many
  83.  * individuals on behalf of the Apache Group and was originally based
  84.  * on public domain software written at the National Center for
  85.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  86.  * For more information on the Apache Group and the Apache HTTP server
  87.  * project, please see <http://www.apache.org/>.
  88.  *
  89.  */
  90.  
  91. #include <string.h>
  92.  
  93. #include "ap_config.h"
  94. #include "ap_md5.h"
  95. #ifdef CHARSET_EBCDIC
  96. #include "ebcdic.h"
  97. #endif /*CHARSET_EBCDIC*/
  98.  
  99. /* Constants for MD5Transform routine.
  100.  */
  101.  
  102. #define S11 7
  103. #define S12 12
  104. #define S13 17
  105. #define S14 22
  106. #define S21 5
  107. #define S22 9
  108. #define S23 14
  109. #define S24 20
  110. #define S31 4
  111. #define S32 11
  112. #define S33 16
  113. #define S34 23
  114. #define S41 6
  115. #define S42 10
  116. #define S43 15
  117. #define S44 21
  118.  
  119. static void MD5Transform(UINT4 state[4], const unsigned char block[64]);
  120. static void Encode(unsigned char *output, const UINT4 *input,
  121.            unsigned int len);
  122. static void Decode(UINT4 *output, const unsigned char *input,
  123.            unsigned int len);
  124.  
  125. static unsigned char PADDING[64] =
  126. {
  127.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  128.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  130. };
  131.  
  132. /* F, G, H and I are basic MD5 functions.
  133.  */
  134. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  135. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  136. #define H(x, y, z) ((x) ^ (y) ^ (z))
  137. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  138.  
  139. /* ROTATE_LEFT rotates x left n bits.
  140.  */
  141. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  142.  
  143. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  144.    Rotation is separate from addition to prevent recomputation.
  145.  */
  146. #define FF(a, b, c, d, x, s, ac) { \
  147.  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  148.  (a) = ROTATE_LEFT ((a), (s)); \
  149.  (a) += (b); \
  150.   }
  151. #define GG(a, b, c, d, x, s, ac) { \
  152.  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  153.  (a) = ROTATE_LEFT ((a), (s)); \
  154.  (a) += (b); \
  155.   }
  156. #define HH(a, b, c, d, x, s, ac) { \
  157.  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  158.  (a) = ROTATE_LEFT ((a), (s)); \
  159.  (a) += (b); \
  160.   }
  161. #define II(a, b, c, d, x, s, ac) { \
  162.  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  163.  (a) = ROTATE_LEFT ((a), (s)); \
  164.  (a) += (b); \
  165.   }
  166.  
  167. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  168.  */
  169. API_EXPORT(void) ap_MD5Init(AP_MD5_CTX * context)
  170. {
  171.     context->count[0] = context->count[1] = 0;
  172.     /* Load magic initialization constants. */
  173.     context->state[0] = 0x67452301;
  174.     context->state[1] = 0xefcdab89;
  175.     context->state[2] = 0x98badcfe;
  176.     context->state[3] = 0x10325476;
  177. }
  178.  
  179. /* MD5 block update operation. Continues an MD5 message-digest
  180.    operation, processing another message block, and updating the
  181.    context.
  182.  */
  183. API_EXPORT(void) ap_MD5Update(AP_MD5_CTX * context, const unsigned char *input,
  184.                unsigned int inputLen)
  185. {
  186.     unsigned int i, idx, partLen;
  187.  
  188.     /* Compute number of bytes mod 64 */
  189.     idx = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  190.  
  191.     /* Update number of bits */
  192.     if ((context->count[0] += ((UINT4) inputLen << 3)) < ((UINT4) inputLen << 3))
  193.     context->count[1]++;
  194.     context->count[1] += (UINT4) inputLen >> 29;
  195.  
  196.     partLen = 64 - idx;
  197.  
  198.     /* Transform as many times as possible. */
  199. #ifndef CHARSET_EBCDIC
  200.     if (inputLen >= partLen) {
  201.     memcpy(&context->buffer[idx], input, partLen);
  202.     MD5Transform(context->state, context->buffer);
  203.  
  204.     for (i = partLen; i + 63 < inputLen; i += 64)
  205.         MD5Transform(context->state, &input[i]);
  206.  
  207.     idx = 0;
  208.     }
  209.     else
  210.     i = 0;
  211.  
  212.     /* Buffer remaining input */
  213.     memcpy(&context->buffer[idx], &input[i], inputLen - i);
  214. #else /*CHARSET_EBCDIC*/
  215.     if (inputLen >= partLen) {
  216.     ebcdic2ascii_strictly(&context->buffer[idx], input, partLen);
  217.     MD5Transform(context->state, context->buffer);
  218.  
  219.     for (i = partLen; i + 63 < inputLen; i += 64) {
  220.         unsigned char inp_tmp[64];
  221.         ebcdic2ascii_strictly(inp_tmp, &input[i], 64);
  222.         MD5Transform(context->state, inp_tmp);
  223.     }
  224.  
  225.     idx = 0;
  226.     }
  227.     else
  228.     i = 0;
  229.  
  230.     /* Buffer remaining input */
  231.     ebcdic2ascii_strictly(&context->buffer[idx], &input[i], inputLen - i);
  232. #endif /*CHARSET_EBCDIC*/
  233. }
  234.  
  235. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  236.    the message digest and zeroizing the context.
  237.  */
  238. API_EXPORT(void) ap_MD5Final(unsigned char digest[16], AP_MD5_CTX * context)
  239. {
  240.     unsigned char bits[8];
  241.     unsigned int idx, padLen;
  242.  
  243.  
  244.     /* Save number of bits */
  245.     Encode(bits, context->count, 8);
  246.  
  247. #ifdef CHARSET_EBCDIC
  248.     /* XXX: @@@: In order to make this no more complex than necessary,
  249.      * this kludge converts the bits[] array using the ascii-to-ebcdic
  250.      * table, because the following ap_MD5Update() re-translates
  251.      * its input (ebcdic-to-ascii).
  252.      * Otherwise, we would have to pass a "conversion" flag to ap_MD5Update()
  253.      */
  254.     ascii2ebcdic(bits,bits,8);
  255.  
  256.     /* Since everything is converted to ascii within ap_MD5Update(), 
  257.      * the initial 0x80 (PADDING[0]) must be stored as 0x20 
  258.      */
  259.     PADDING[0] = os_toebcdic[0x80];
  260. #endif /*CHARSET_EBCDIC*/
  261.  
  262.     /* Pad out to 56 mod 64. */
  263.     idx = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  264.     padLen = (idx < 56) ? (56 - idx) : (120 - idx);
  265.     ap_MD5Update(context, PADDING, padLen);
  266.  
  267.     /* Append length (before padding) */
  268.     ap_MD5Update(context, bits, 8);
  269.  
  270.     /* Store state in digest */
  271.     Encode(digest, context->state, 16);
  272.  
  273.     /* Zeroize sensitive information. */
  274.     memset(context, 0, sizeof(*context));
  275. }
  276.  
  277. /* MD5 basic transformation. Transforms state based on block. */
  278. static void MD5Transform(UINT4 state[4], const unsigned char block[64])
  279. {
  280.     UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  281.  
  282.     Decode(x, block, 64);
  283.  
  284.     /* Round 1 */
  285.     FF(a, b, c, d, x[0], S11, 0xd76aa478);    /* 1 */
  286.     FF(d, a, b, c, x[1], S12, 0xe8c7b756);    /* 2 */
  287.     FF(c, d, a, b, x[2], S13, 0x242070db);    /* 3 */
  288.     FF(b, c, d, a, x[3], S14, 0xc1bdceee);    /* 4 */
  289.     FF(a, b, c, d, x[4], S11, 0xf57c0faf);    /* 5 */
  290.     FF(d, a, b, c, x[5], S12, 0x4787c62a);    /* 6 */
  291.     FF(c, d, a, b, x[6], S13, 0xa8304613);    /* 7 */
  292.     FF(b, c, d, a, x[7], S14, 0xfd469501);    /* 8 */
  293.     FF(a, b, c, d, x[8], S11, 0x698098d8);    /* 9 */
  294.     FF(d, a, b, c, x[9], S12, 0x8b44f7af);    /* 10 */
  295.     FF(c, d, a, b, x[10], S13, 0xffff5bb1);    /* 11 */
  296.     FF(b, c, d, a, x[11], S14, 0x895cd7be);    /* 12 */
  297.     FF(a, b, c, d, x[12], S11, 0x6b901122);    /* 13 */
  298.     FF(d, a, b, c, x[13], S12, 0xfd987193);    /* 14 */
  299.     FF(c, d, a, b, x[14], S13, 0xa679438e);    /* 15 */
  300.     FF(b, c, d, a, x[15], S14, 0x49b40821);    /* 16 */
  301.  
  302.     /* Round 2 */
  303.     GG(a, b, c, d, x[1], S21, 0xf61e2562);    /* 17 */
  304.     GG(d, a, b, c, x[6], S22, 0xc040b340);    /* 18 */
  305.     GG(c, d, a, b, x[11], S23, 0x265e5a51);    /* 19 */
  306.     GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);    /* 20 */
  307.     GG(a, b, c, d, x[5], S21, 0xd62f105d);    /* 21 */
  308.     GG(d, a, b, c, x[10], S22, 0x2441453);    /* 22 */
  309.     GG(c, d, a, b, x[15], S23, 0xd8a1e681);    /* 23 */
  310.     GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);    /* 24 */
  311.     GG(a, b, c, d, x[9], S21, 0x21e1cde6);    /* 25 */
  312.     GG(d, a, b, c, x[14], S22, 0xc33707d6);    /* 26 */
  313.     GG(c, d, a, b, x[3], S23, 0xf4d50d87);    /* 27 */
  314.     GG(b, c, d, a, x[8], S24, 0x455a14ed);    /* 28 */
  315.     GG(a, b, c, d, x[13], S21, 0xa9e3e905);    /* 29 */
  316.     GG(d, a, b, c, x[2], S22, 0xfcefa3f8);    /* 30 */
  317.     GG(c, d, a, b, x[7], S23, 0x676f02d9);    /* 31 */
  318.     GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);    /* 32 */
  319.  
  320.     /* Round 3 */
  321.     HH(a, b, c, d, x[5], S31, 0xfffa3942);    /* 33 */
  322.     HH(d, a, b, c, x[8], S32, 0x8771f681);    /* 34 */
  323.     HH(c, d, a, b, x[11], S33, 0x6d9d6122);    /* 35 */
  324.     HH(b, c, d, a, x[14], S34, 0xfde5380c);    /* 36 */
  325.     HH(a, b, c, d, x[1], S31, 0xa4beea44);    /* 37 */
  326.     HH(d, a, b, c, x[4], S32, 0x4bdecfa9);    /* 38 */
  327.     HH(c, d, a, b, x[7], S33, 0xf6bb4b60);    /* 39 */
  328.     HH(b, c, d, a, x[10], S34, 0xbebfbc70);    /* 40 */
  329.     HH(a, b, c, d, x[13], S31, 0x289b7ec6);    /* 41 */
  330.     HH(d, a, b, c, x[0], S32, 0xeaa127fa);    /* 42 */
  331.     HH(c, d, a, b, x[3], S33, 0xd4ef3085);    /* 43 */
  332.     HH(b, c, d, a, x[6], S34, 0x4881d05);    /* 44 */
  333.     HH(a, b, c, d, x[9], S31, 0xd9d4d039);    /* 45 */
  334.     HH(d, a, b, c, x[12], S32, 0xe6db99e5);    /* 46 */
  335.     HH(c, d, a, b, x[15], S33, 0x1fa27cf8);    /* 47 */
  336.     HH(b, c, d, a, x[2], S34, 0xc4ac5665);    /* 48 */
  337.  
  338.     /* Round 4 */
  339.     II(a, b, c, d, x[0], S41, 0xf4292244);    /* 49 */
  340.     II(d, a, b, c, x[7], S42, 0x432aff97);    /* 50 */
  341.     II(c, d, a, b, x[14], S43, 0xab9423a7);    /* 51 */
  342.     II(b, c, d, a, x[5], S44, 0xfc93a039);    /* 52 */
  343.     II(a, b, c, d, x[12], S41, 0x655b59c3);    /* 53 */
  344.     II(d, a, b, c, x[3], S42, 0x8f0ccc92);    /* 54 */
  345.     II(c, d, a, b, x[10], S43, 0xffeff47d);    /* 55 */
  346.     II(b, c, d, a, x[1], S44, 0x85845dd1);    /* 56 */
  347.     II(a, b, c, d, x[8], S41, 0x6fa87e4f);    /* 57 */
  348.     II(d, a, b, c, x[15], S42, 0xfe2ce6e0);    /* 58 */
  349.     II(c, d, a, b, x[6], S43, 0xa3014314);    /* 59 */
  350.     II(b, c, d, a, x[13], S44, 0x4e0811a1);    /* 60 */
  351.     II(a, b, c, d, x[4], S41, 0xf7537e82);    /* 61 */
  352.     II(d, a, b, c, x[11], S42, 0xbd3af235);    /* 62 */
  353.     II(c, d, a, b, x[2], S43, 0x2ad7d2bb);    /* 63 */
  354.     II(b, c, d, a, x[9], S44, 0xeb86d391);    /* 64 */
  355.  
  356.     state[0] += a;
  357.     state[1] += b;
  358.     state[2] += c;
  359.     state[3] += d;
  360.  
  361.     /* Zeroize sensitive information. */
  362.     memset(x, 0, sizeof(x));
  363. }
  364.  
  365. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  366.    a multiple of 4.
  367.  */
  368. static void Encode(unsigned char *output, const UINT4 *input, unsigned int len)
  369. {
  370.     unsigned int i, j;
  371.     UINT4 k;
  372.  
  373.     for (i = 0, j = 0; j < len; i++, j += 4) {
  374.     k = input[i];
  375.     output[j] = (unsigned char) (k & 0xff);
  376.     output[j + 1] = (unsigned char) ((k >> 8) & 0xff);
  377.     output[j + 2] = (unsigned char) ((k >> 16) & 0xff);
  378.     output[j + 3] = (unsigned char) ((k >> 24) & 0xff);
  379.     }
  380. }
  381.  
  382. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  383.    a multiple of 4.
  384.  */
  385. static void Decode(UINT4 *output, const unsigned char *input, unsigned int len)
  386. {
  387.     unsigned int i, j;
  388.  
  389.     for (i = 0, j = 0; j < len; i++, j += 4)
  390.     output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) |
  391.         (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
  392. }
  393.