Annotation of parser3/src/lib/md5/pa_md5c.c, revision 1.12

1.4       paf         1: /** @file
                      2:        Parser: copied from apache 1.3.20  sources.
1.10      paf         3:        Replaced ap_ to pa_ prefixes. linked into all targets but Apache-module target,
                      4:        where linked targets/apache/pa_md5c.c stub instead.
1.4       paf         5: 
1.12    ! paf         6:        Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)
1.4       paf         7: */
                      8: 
1.1       paf         9: /*
                     10:  * This is work is derived from material Copyright RSA Data Security, Inc.
                     11:  *
                     12:  * The RSA copyright statement and Licence for that original material is
                     13:  * included below. This is followed by the Apache copyright statement and
                     14:  * licence for the modifications made to that material.
                     15:  */
                     16: 
                     17: /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
                     18:  */
                     19: 
                     20: /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
                     21:    rights reserved.
                     22: 
                     23:    License to copy and use this software is granted provided that it
                     24:    is identified as the "RSA Data Security, Inc. MD5 Message-Digest
                     25:    Algorithm" in all material mentioning or referencing this software
                     26:    or this function.
                     27: 
                     28:    License is also granted to make and use derivative works provided
                     29:    that such works are identified as "derived from the RSA Data
                     30:    Security, Inc. MD5 Message-Digest Algorithm" in all material
                     31:    mentioning or referencing the derived work.
                     32: 
                     33:    RSA Data Security, Inc. makes no representations concerning either
                     34:    the merchantability of this software or the suitability of this
                     35:    software for any particular purpose. It is provided "as is"
                     36:    without express or implied warranty of any kind.
                     37: 
                     38:    These notices must be retained in any copies of any part of this
                     39:    documentation and/or software.
                     40:  */
                     41: 
                     42: /* ====================================================================
                     43:  * Copyright (c) 1996-1999 The Apache Group.  All rights reserved.
                     44:  *
                     45:  * Redistribution and use in source and binary forms, with or without
                     46:  * modification, are permitted provided that the following conditions
                     47:  * are met:
                     48:  *
                     49:  * 1. Redistributions of source code must retain the above copyright
                     50:  *    notice, this list of conditions and the following disclaimer. 
                     51:  *
                     52:  * 2. Redistributions in binary form must reproduce the above copyright
                     53:  *    notice, this list of conditions and the following disclaimer in
                     54:  *    the documentation and/or other materials provided with the
                     55:  *    distribution.
                     56:  *
                     57:  * 3. All advertising materials mentioning features or use of this
                     58:  *    software must display the following acknowledgment:
                     59:  *    "This product includes software developed by the Apache Group
                     60:  *    for use in the Apache HTTP server project (http://www.apache.org/)."
                     61:  *
                     62:  * 4. The names "Apache Server" and "Apache Group" must not be used to
                     63:  *    endorse or promote products derived from this software without
                     64:  *    prior written permission. For written permission, please contact
                     65:  *    apache@apache.org.
                     66:  *
                     67:  * 5. Products derived from this software may not be called "Apache"
                     68:  *    nor may "Apache" appear in their names without prior written
                     69:  *    permission of the Apache Group.
                     70:  *
                     71:  * 6. Redistributions of any form whatsoever must retain the following
                     72:  *    acknowledgment:
                     73:  *    "This product includes software developed by the Apache Group
                     74:  *    for use in the Apache HTTP server project (http://www.apache.org/)."
                     75:  *
                     76:  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
                     77:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     78:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     79:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
                     80:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     81:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     82:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     83:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     84:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     85:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     86:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                     87:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                     88:  * ====================================================================
                     89:  *
                     90:  * This software consists of voluntary contributions made by many
                     91:  * individuals on behalf of the Apache Group and was originally based
                     92:  * on public domain software written at the National Center for
                     93:  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
                     94:  * For more information on the Apache Group and the Apache HTTP server
                     95:  * project, please see <http://www.apache.org/>.
                     96:  *
                     97:  */
                     98: 
                     99: /*
1.10      paf       100:  * The pa_MD5Encode() routine uses much code obtained from the FreeBSD 3.0
1.1       paf       101:  * MD5 crypt() function, which is licenced as follows:
                    102:  * ----------------------------------------------------------------------------
                    103:  * "THE BEER-WARE LICENSE" (Revision 42):
                    104:  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
                    105:  * can do whatever you want with this stuff. If we meet some day, and you think
                    106:  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
                    107:  * ----------------------------------------------------------------------------
                    108:  */
1.4       paf       109: 
1.12    ! paf       110: static const char * const IDENT_MD5_C="$Date: 2003/11/20 16:34:25 $";
1.1       paf       111: 
1.3       paf       112: #include <string.h>
1.2       paf       113: 
1.9       paf       114: #include "pa_md5.h"
1.1       paf       115: 
1.10      paf       116: #define pa_pa_cpystrn(strDest, strSource, count) strncpy(strDest, strSource, count)
1.1       paf       117: 
                    118: /* Constants for MD5Transform routine.
                    119:  */
                    120: 
                    121: #define S11 7
                    122: #define S12 12
                    123: #define S13 17
                    124: #define S14 22
                    125: #define S21 5
                    126: #define S22 9
                    127: #define S23 14
                    128: #define S24 20
                    129: #define S31 4
                    130: #define S32 11
                    131: #define S33 16
                    132: #define S34 23
                    133: #define S41 6
                    134: #define S42 10
                    135: #define S43 15
                    136: #define S44 21
                    137: 
                    138: static void MD5Transform(UINT4 state[4], const unsigned char block[64]);
                    139: static void Encode(unsigned char *output, const UINT4 *input,
                    140:                   unsigned int len);
                    141: static void Decode(UINT4 *output, const unsigned char *input,
                    142:                   unsigned int len);
                    143: 
                    144: static unsigned char PADDING[64] =
                    145: {
                    146:     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    147:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    148:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    149: };
                    150: 
                    151: /* F, G, H and I are basic MD5 functions.
                    152:  */
                    153: #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
                    154: #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
                    155: #define H(x, y, z) ((x) ^ (y) ^ (z))
                    156: #define I(x, y, z) ((y) ^ ((x) | (~z)))
                    157: 
                    158: /* ROTATE_LEFT rotates x left n bits.
                    159:  */
                    160: #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
                    161: 
                    162: /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
                    163:    Rotation is separate from addition to prevent recomputation.
                    164:  */
                    165: #define FF(a, b, c, d, x, s, ac) { \
                    166:  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    167:  (a) = ROTATE_LEFT ((a), (s)); \
                    168:  (a) += (b); \
                    169:   }
                    170: #define GG(a, b, c, d, x, s, ac) { \
                    171:  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    172:  (a) = ROTATE_LEFT ((a), (s)); \
                    173:  (a) += (b); \
                    174:   }
                    175: #define HH(a, b, c, d, x, s, ac) { \
                    176:  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    177:  (a) = ROTATE_LEFT ((a), (s)); \
                    178:  (a) += (b); \
                    179:   }
                    180: #define II(a, b, c, d, x, s, ac) { \
                    181:  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    182:  (a) = ROTATE_LEFT ((a), (s)); \
                    183:  (a) += (b); \
                    184:   }
                    185: 
                    186: /* MD5 initialization. Begins an MD5 operation, writing a new context.
                    187:  */
1.10      paf       188: PA_API_EXPORT(void) pa_MD5Init(PA_MD5_CTX *context)
1.1       paf       189: {
                    190:     context->count[0] = context->count[1] = 0;
                    191:     /* Load magic initialization constants. */
                    192:     context->state[0] = 0x67452301;
                    193:     context->state[1] = 0xefcdab89;
                    194:     context->state[2] = 0x98badcfe;
                    195:     context->state[3] = 0x10325476;
                    196: }
                    197: 
                    198: /* MD5 block update operation. Continues an MD5 message-digest
                    199:    operation, processing another message block, and updating the
                    200:    context.
                    201:  */
1.10      paf       202: PA_API_EXPORT(void) pa_MD5Update(PA_MD5_CTX *context, const unsigned char *input,
1.1       paf       203:                              unsigned int inputLen)
                    204: {
                    205:     unsigned int i, idx, partLen;
                    206: 
                    207:     /* Compute number of bytes mod 64 */
                    208:     idx = (unsigned int) ((context->count[0] >> 3) & 0x3F);
                    209: 
                    210:     /* Update number of bits */
                    211:     if ((context->count[0] += ((UINT4) inputLen << 3))
                    212:        < ((UINT4) inputLen << 3)) {
                    213:        context->count[1]++;
                    214:     }
                    215:     context->count[1] += (UINT4) inputLen >> 29;
                    216: 
                    217:     partLen = 64 - idx;
                    218: 
                    219:     /* Transform as many times as possible. */
                    220: #ifndef CHARSET_EBCDIC
                    221:     if (inputLen >= partLen) {
                    222:        memcpy(&context->buffer[idx], input, partLen);
                    223:        MD5Transform(context->state, context->buffer);
                    224: 
                    225:        for (i = partLen; i + 63 < inputLen; i += 64) {
                    226:            MD5Transform(context->state, &input[i]);
                    227:        }
                    228: 
                    229:        idx = 0;
                    230:     }
                    231:     else {
                    232:        i = 0;
                    233:     }
                    234: 
                    235:     /* Buffer remaining input */
                    236:     memcpy(&context->buffer[idx], &input[i], inputLen - i);
                    237: #else /*CHARSET_EBCDIC*/
                    238:     if (inputLen >= partLen) {
                    239:        ebcdic2ascii(&context->buffer[idx], input, partLen);
                    240:        MD5Transform(context->state, context->buffer);
                    241: 
                    242:        for (i = partLen; i + 63 < inputLen; i += 64) {
                    243:            unsigned char inp_tmp[64];
                    244:            ebcdic2ascii(inp_tmp, &input[i], 64);
                    245:            MD5Transform(context->state, inp_tmp);
                    246:        }
                    247: 
                    248:        idx = 0;
                    249:     }
                    250:     else {
                    251:        i = 0;
                    252:     }
                    253: 
                    254:     /* Buffer remaining input */
                    255:     ebcdic2ascii(&context->buffer[idx], &input[i], inputLen - i);
                    256: #endif /*CHARSET_EBCDIC*/
                    257: }
                    258: 
                    259: /* MD5 finalization. Ends an MD5 message-digest operation, writing the
                    260:    the message digest and zeroizing the context.
                    261:  */
1.10      paf       262: PA_API_EXPORT(void) pa_MD5Final(unsigned char digest[16], PA_MD5_CTX *context)
1.1       paf       263: {
                    264:     unsigned char bits[8];
                    265:     unsigned int idx, padLen;
                    266: 
                    267: 
                    268:     /* Save number of bits */
                    269:     Encode(bits, context->count, 8);
                    270: 
                    271: #ifdef CHARSET_EBCDIC
                    272:     /* XXX: @@@: In order to make this no more complex than necessary,
                    273:      * this kludge converts the bits[] array using the ascii-to-ebcdic
1.10      paf       274:      * table, because the following pa_MD5Update() re-translates
1.1       paf       275:      * its input (ebcdic-to-ascii).
1.10      paf       276:      * Otherwise, we would have to pass a "conversion" flag to pa_MD5Update()
1.1       paf       277:      */
                    278:     ascii2ebcdic(bits,bits,8);
                    279: 
1.10      paf       280:     /* Since everything is converted to ascii within pa_MD5Update(), 
1.1       paf       281:      * the initial 0x80 (PADDING[0]) must be stored as 0x20 
                    282:      */
                    283:     PADDING[0] = os_toebcdic[0x80];
                    284: #endif /*CHARSET_EBCDIC*/
                    285: 
                    286:     /* Pad out to 56 mod 64. */
                    287:     idx = (unsigned int) ((context->count[0] >> 3) & 0x3f);
                    288:     padLen = (idx < 56) ? (56 - idx) : (120 - idx);
1.10      paf       289:     pa_MD5Update(context, (const unsigned char *)PADDING, padLen);
1.1       paf       290: 
                    291:     /* Append length (before padding) */
1.10      paf       292:     pa_MD5Update(context, (const unsigned char *)bits, 8);
1.1       paf       293: 
                    294:     /* Store state in digest */
                    295:     Encode(digest, context->state, 16);
                    296: 
                    297:     /* Zeroize sensitive information. */
                    298:     memset(context, 0, sizeof(*context));
                    299: }
                    300: 
                    301: /* MD5 basic transformation. Transforms state based on block. */
                    302: static void MD5Transform(UINT4 state[4], const unsigned char block[64])
                    303: {
                    304:     UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
                    305: 
                    306:     Decode(x, block, 64);
                    307: 
                    308:     /* Round 1 */
                    309:     FF(a, b, c, d, x[0], S11, 0xd76aa478);     /* 1 */
                    310:     FF(d, a, b, c, x[1], S12, 0xe8c7b756);     /* 2 */
                    311:     FF(c, d, a, b, x[2], S13, 0x242070db);     /* 3 */
                    312:     FF(b, c, d, a, x[3], S14, 0xc1bdceee);     /* 4 */
                    313:     FF(a, b, c, d, x[4], S11, 0xf57c0faf);     /* 5 */
                    314:     FF(d, a, b, c, x[5], S12, 0x4787c62a);     /* 6 */
                    315:     FF(c, d, a, b, x[6], S13, 0xa8304613);     /* 7 */
                    316:     FF(b, c, d, a, x[7], S14, 0xfd469501);     /* 8 */
                    317:     FF(a, b, c, d, x[8], S11, 0x698098d8);     /* 9 */
                    318:     FF(d, a, b, c, x[9], S12, 0x8b44f7af);     /* 10 */
                    319:     FF(c, d, a, b, x[10], S13, 0xffff5bb1);    /* 11 */
                    320:     FF(b, c, d, a, x[11], S14, 0x895cd7be);    /* 12 */
                    321:     FF(a, b, c, d, x[12], S11, 0x6b901122);    /* 13 */
                    322:     FF(d, a, b, c, x[13], S12, 0xfd987193);    /* 14 */
                    323:     FF(c, d, a, b, x[14], S13, 0xa679438e);    /* 15 */
                    324:     FF(b, c, d, a, x[15], S14, 0x49b40821);    /* 16 */
                    325: 
                    326:     /* Round 2 */
                    327:     GG(a, b, c, d, x[1], S21, 0xf61e2562);     /* 17 */
                    328:     GG(d, a, b, c, x[6], S22, 0xc040b340);     /* 18 */
                    329:     GG(c, d, a, b, x[11], S23, 0x265e5a51);    /* 19 */
                    330:     GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);     /* 20 */
                    331:     GG(a, b, c, d, x[5], S21, 0xd62f105d);     /* 21 */
                    332:     GG(d, a, b, c, x[10], S22, 0x2441453);     /* 22 */
                    333:     GG(c, d, a, b, x[15], S23, 0xd8a1e681);    /* 23 */
                    334:     GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);     /* 24 */
                    335:     GG(a, b, c, d, x[9], S21, 0x21e1cde6);     /* 25 */
                    336:     GG(d, a, b, c, x[14], S22, 0xc33707d6);    /* 26 */
                    337:     GG(c, d, a, b, x[3], S23, 0xf4d50d87);     /* 27 */
                    338:     GG(b, c, d, a, x[8], S24, 0x455a14ed);     /* 28 */
                    339:     GG(a, b, c, d, x[13], S21, 0xa9e3e905);    /* 29 */
                    340:     GG(d, a, b, c, x[2], S22, 0xfcefa3f8);     /* 30 */
                    341:     GG(c, d, a, b, x[7], S23, 0x676f02d9);     /* 31 */
                    342:     GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);    /* 32 */
                    343: 
                    344:     /* Round 3 */
                    345:     HH(a, b, c, d, x[5], S31, 0xfffa3942);     /* 33 */
                    346:     HH(d, a, b, c, x[8], S32, 0x8771f681);     /* 34 */
                    347:     HH(c, d, a, b, x[11], S33, 0x6d9d6122);    /* 35 */
                    348:     HH(b, c, d, a, x[14], S34, 0xfde5380c);    /* 36 */
                    349:     HH(a, b, c, d, x[1], S31, 0xa4beea44);     /* 37 */
                    350:     HH(d, a, b, c, x[4], S32, 0x4bdecfa9);     /* 38 */
                    351:     HH(c, d, a, b, x[7], S33, 0xf6bb4b60);     /* 39 */
                    352:     HH(b, c, d, a, x[10], S34, 0xbebfbc70);    /* 40 */
                    353:     HH(a, b, c, d, x[13], S31, 0x289b7ec6);    /* 41 */
                    354:     HH(d, a, b, c, x[0], S32, 0xeaa127fa);     /* 42 */
                    355:     HH(c, d, a, b, x[3], S33, 0xd4ef3085);     /* 43 */
                    356:     HH(b, c, d, a, x[6], S34, 0x4881d05);      /* 44 */
                    357:     HH(a, b, c, d, x[9], S31, 0xd9d4d039);     /* 45 */
                    358:     HH(d, a, b, c, x[12], S32, 0xe6db99e5);    /* 46 */
                    359:     HH(c, d, a, b, x[15], S33, 0x1fa27cf8);    /* 47 */
                    360:     HH(b, c, d, a, x[2], S34, 0xc4ac5665);     /* 48 */
                    361: 
                    362:     /* Round 4 */
                    363:     II(a, b, c, d, x[0], S41, 0xf4292244);     /* 49 */
                    364:     II(d, a, b, c, x[7], S42, 0x432aff97);     /* 50 */
                    365:     II(c, d, a, b, x[14], S43, 0xab9423a7);    /* 51 */
                    366:     II(b, c, d, a, x[5], S44, 0xfc93a039);     /* 52 */
                    367:     II(a, b, c, d, x[12], S41, 0x655b59c3);    /* 53 */
                    368:     II(d, a, b, c, x[3], S42, 0x8f0ccc92);     /* 54 */
                    369:     II(c, d, a, b, x[10], S43, 0xffeff47d);    /* 55 */
                    370:     II(b, c, d, a, x[1], S44, 0x85845dd1);     /* 56 */
                    371:     II(a, b, c, d, x[8], S41, 0x6fa87e4f);     /* 57 */
                    372:     II(d, a, b, c, x[15], S42, 0xfe2ce6e0);    /* 58 */
                    373:     II(c, d, a, b, x[6], S43, 0xa3014314);     /* 59 */
                    374:     II(b, c, d, a, x[13], S44, 0x4e0811a1);    /* 60 */
                    375:     II(a, b, c, d, x[4], S41, 0xf7537e82);     /* 61 */
                    376:     II(d, a, b, c, x[11], S42, 0xbd3af235);    /* 62 */
                    377:     II(c, d, a, b, x[2], S43, 0x2ad7d2bb);     /* 63 */
                    378:     II(b, c, d, a, x[9], S44, 0xeb86d391);     /* 64 */
                    379: 
                    380:     state[0] += a;
                    381:     state[1] += b;
                    382:     state[2] += c;
                    383:     state[3] += d;
                    384: 
                    385:     /* Zeroize sensitive information. */
                    386:     memset(x, 0, sizeof(x));
                    387: }
                    388: 
                    389: /* Encodes input (UINT4) into output (unsigned char). Assumes len is
                    390:    a multiple of 4.
                    391:  */
                    392: static void Encode(unsigned char *output, const UINT4 *input, unsigned int len)
                    393: {
                    394:     unsigned int i, j;
                    395:     UINT4 k;
                    396: 
                    397:     for (i = 0, j = 0; j < len; i++, j += 4) {
                    398:        k = input[i];
                    399:        output[j] = (unsigned char) (k & 0xff);
                    400:        output[j + 1] = (unsigned char) ((k >> 8) & 0xff);
                    401:        output[j + 2] = (unsigned char) ((k >> 16) & 0xff);
                    402:        output[j + 3] = (unsigned char) ((k >> 24) & 0xff);
                    403:     }
                    404: }
                    405: 
                    406: /* Decodes input (unsigned char) into output (UINT4). Assumes len is
                    407:  * a multiple of 4.
                    408:  */
                    409: static void Decode(UINT4 *output, const unsigned char *input, unsigned int len)
                    410: {
                    411:     unsigned int i, j;
                    412: 
                    413:     for (i = 0, j = 0; j < len; i++, j += 4)
                    414:        output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) |
                    415:            (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
                    416: }
                    417: 
                    418: /*
                    419:  * The following MD5 password encryption code was largely borrowed from
                    420:  * the FreeBSD 3.0 /usr/src/lib/libcrypt/crypt.c file, which is
                    421:  * licenced as stated at the top of this file.
                    422:  */
1.10      paf       423: PA_API_EXPORT(void) pa_to64(char *s, unsigned long v, int n)
1.1       paf       424: {
                    425:     static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
                    426:        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    427: 
                    428:     while (--n >= 0) {
                    429:        *s++ = itoa64[v&0x3f];
                    430:        v >>= 6;
                    431:     }
                    432: }
                    433: 
1.10      paf       434: PA_API_EXPORT(void) pa_MD5Encode(const unsigned char *pw,
                    435:                              const unsigned char *salt,
                    436:                              char *result, size_t nbytes)
1.1       paf       437: {
                    438:     /*
                    439:      * Minimum size is 8 bytes for salt, plus 1 for the trailing NUL,
                    440:      * plus 4 for the '$' separators, plus the password hash itself.
                    441:      * Let's leave a goodly amount of leeway.
                    442:      */
                    443: 
                    444:     char passwd[120], *p;
                    445:     const unsigned char *sp, *ep;
                    446:     unsigned char final[16];
                    447:     int i;
                    448:     unsigned int sl;
                    449:     int pl;
                    450:     unsigned int pwlen;
                    451:     PA_MD5_CTX ctx, ctx1;
                    452:     unsigned long l;
                    453: 
                    454:     /* 
                    455:      * Refine the salt first.  It's possible we were given an already-hashed
                    456:      * string as the salt argument, so extract the actual salt value from it
                    457:      * if so.  Otherwise just use the string up to the first '$' as the salt.
                    458:      */
                    459:     sp = salt;
                    460: 
                    461:     /*
                    462:      * If it starts with the magic string, then skip that.
                    463:      */
                    464:     if (strncmp((char *)sp, PA_MD5PW_ID, PA_MD5PW_IDLEN) == 0) {
                    465:        sp += PA_MD5PW_IDLEN;
                    466:     }
                    467: 
                    468:     /*
                    469:      * It stops at the first '$' or 8 chars, whichever comes first
                    470:      */
                    471:     for (ep = sp; (*ep != '\0') && (*ep != '$') && (ep < (sp + 8)); ep++) {
                    472:        continue;
                    473:     }
                    474: 
                    475:     /*
                    476:      * Get the length of the true salt
                    477:      */
                    478:     sl = ep - sp;
                    479: 
                    480:     /*
                    481:      * 'Time to make the doughnuts..'
                    482:      */
1.10      paf       483:     pa_MD5Init(&ctx);
1.1       paf       484: 
                    485:     pwlen = strlen((char *)pw);
                    486:     /*
                    487:      * The password first, since that is what is most unknown
                    488:      */
1.10      paf       489:     pa_MD5Update(&ctx, pw, pwlen);
1.1       paf       490: 
1.10      paf       491:     /*
                    492:      * Then our magic string
                    493:      */
                    494:     pa_MD5Update(&ctx, (const unsigned char *) PA_MD5PW_ID, PA_MD5PW_IDLEN);
1.1       paf       495: 
                    496:     /*
                    497:      * Then the raw salt
                    498:      */
1.10      paf       499:     pa_MD5Update(&ctx, sp, sl);
1.1       paf       500: 
                    501:     /*
                    502:      * Then just as many characters of the MD5(pw, salt, pw)
                    503:      */
1.10      paf       504:     pa_MD5Init(&ctx1);
                    505:     pa_MD5Update(&ctx1, pw, pwlen);
                    506:     pa_MD5Update(&ctx1, sp, sl);
                    507:     pa_MD5Update(&ctx1, pw, pwlen);
                    508:     pa_MD5Final(final, &ctx1);
1.1       paf       509:     for(pl = pwlen; pl > 0; pl -= 16) {
1.10      paf       510:        pa_MD5Update(&ctx, final, (pl > 16) ? 16 : (unsigned int) pl);
1.1       paf       511:     }
                    512: 
                    513:     /*
                    514:      * Don't leave anything around in vm they could use.
                    515:      */
                    516:     memset(final, 0, sizeof(final));
                    517: 
                    518:     /*
                    519:      * Then something really weird...
                    520:      */
                    521:     for (i = pwlen; i != 0; i >>= 1) {
                    522:        if (i & 1) {
1.10      paf       523:            pa_MD5Update(&ctx, final, 1);
1.1       paf       524:        }
                    525:        else {
1.10      paf       526:            pa_MD5Update(&ctx, pw, 1);
1.1       paf       527:        }
                    528:     }
                    529: 
                    530:     /*
                    531:      * Now make the output string.  We know our limitations, so we
                    532:      * can use the string routines without bounds checking.
                    533:      */
1.10      paf       534:     pa_pa_cpystrn(passwd, PA_MD5PW_ID, PA_MD5PW_IDLEN + 1);
                    535:     pa_pa_cpystrn(passwd + PA_MD5PW_IDLEN, (char *)sp, sl + 1);
1.1       paf       536:     passwd[PA_MD5PW_IDLEN + sl]     = '$';
                    537:     passwd[PA_MD5PW_IDLEN + sl + 1] = '\0';
                    538: 
1.10      paf       539:     pa_MD5Final(final, &ctx);
1.1       paf       540: 
                    541:     /*
                    542:      * And now, just to make sure things don't run too fast..
                    543:      * On a 60 Mhz Pentium this takes 34 msec, so you would
                    544:      * need 30 seconds to build a 1000 entry dictionary...
                    545:      */
                    546:     for (i = 0; i < 1000; i++) {
1.10      paf       547:        pa_MD5Init(&ctx1);
1.1       paf       548:        if (i & 1) {
1.10      paf       549:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       550:        }
                    551:        else {
1.10      paf       552:            pa_MD5Update(&ctx1, final, 16);
1.1       paf       553:        }
                    554:        if (i % 3) {
1.10      paf       555:            pa_MD5Update(&ctx1, sp, sl);
1.1       paf       556:        }
                    557: 
                    558:        if (i % 7) {
1.10      paf       559:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       560:        }
                    561: 
                    562:        if (i & 1) {
1.10      paf       563:            pa_MD5Update(&ctx1, final, 16);
1.1       paf       564:        }
                    565:        else {
1.10      paf       566:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       567:        }
1.10      paf       568:        pa_MD5Final(final,&ctx1);
1.1       paf       569:     }
                    570: 
                    571:     p = passwd + strlen(passwd);
                    572: 
1.10      paf       573:     l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; pa_to64(p, l, 4); p += 4;
                    574:     l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; pa_to64(p, l, 4); p += 4;
                    575:     l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; pa_to64(p, l, 4); p += 4;
                    576:     l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; pa_to64(p, l, 4); p += 4;
                    577:     l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; pa_to64(p, l, 4); p += 4;
                    578:     l =                    final[11]                ; pa_to64(p, l, 2); p += 2;
1.1       paf       579:     *p = '\0';
                    580: 
                    581:     /*
                    582:      * Don't leave anything around in vm they could use.
                    583:      */
                    584:     memset(final, 0, sizeof(final));
1.8       paf       585: 
1.10      paf       586:     pa_pa_cpystrn(result, passwd, nbytes - 1);
1.1       paf       587: }

E-mail: