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

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.14      moko        6:        Copyright (c) 2001-2012 Art. Lebedev Studio (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.9       paf       110: #include "pa_md5.h"
1.1       paf       111: 
1.16    ! moko      112: volatile const char * IDENT_PA_MD5_C="$Id: pa_md5c.c,v 1.15 2013/07/16 21:48:36 moko Exp $" IDENT_PA_MD5_H;
1.14      moko      113: 
1.10      paf       114: #define pa_pa_cpystrn(strDest, strSource, count) strncpy(strDest, strSource, count)
1.1       paf       115: 
                    116: /* Constants for MD5Transform routine.
                    117:  */
                    118: 
                    119: #define S11 7
                    120: #define S12 12
                    121: #define S13 17
                    122: #define S14 22
                    123: #define S21 5
                    124: #define S22 9
                    125: #define S23 14
                    126: #define S24 20
                    127: #define S31 4
                    128: #define S32 11
                    129: #define S33 16
                    130: #define S34 23
                    131: #define S41 6
                    132: #define S42 10
                    133: #define S43 15
                    134: #define S44 21
                    135: 
                    136: static void MD5Transform(UINT4 state[4], const unsigned char block[64]);
                    137: static void Encode(unsigned char *output, const UINT4 *input,
                    138:                   unsigned int len);
                    139: static void Decode(UINT4 *output, const unsigned char *input,
                    140:                   unsigned int len);
                    141: 
                    142: static unsigned char PADDING[64] =
                    143: {
                    144:     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    145:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    146:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    147: };
                    148: 
                    149: /* F, G, H and I are basic MD5 functions.
                    150:  */
                    151: #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
                    152: #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
                    153: #define H(x, y, z) ((x) ^ (y) ^ (z))
                    154: #define I(x, y, z) ((y) ^ ((x) | (~z)))
                    155: 
                    156: /* ROTATE_LEFT rotates x left n bits.
                    157:  */
                    158: #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
                    159: 
                    160: /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
                    161:    Rotation is separate from addition to prevent recomputation.
                    162:  */
                    163: #define FF(a, b, c, d, x, s, ac) { \
                    164:  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    165:  (a) = ROTATE_LEFT ((a), (s)); \
                    166:  (a) += (b); \
                    167:   }
                    168: #define GG(a, b, c, d, x, s, ac) { \
                    169:  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    170:  (a) = ROTATE_LEFT ((a), (s)); \
                    171:  (a) += (b); \
                    172:   }
                    173: #define HH(a, b, c, d, x, s, ac) { \
                    174:  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    175:  (a) = ROTATE_LEFT ((a), (s)); \
                    176:  (a) += (b); \
                    177:   }
                    178: #define II(a, b, c, d, x, s, ac) { \
                    179:  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
                    180:  (a) = ROTATE_LEFT ((a), (s)); \
                    181:  (a) += (b); \
                    182:   }
                    183: 
                    184: /* MD5 initialization. Begins an MD5 operation, writing a new context.
                    185:  */
1.15      moko      186: void pa_MD5Init(PA_MD5_CTX *context)
1.1       paf       187: {
                    188:     context->count[0] = context->count[1] = 0;
                    189:     /* Load magic initialization constants. */
                    190:     context->state[0] = 0x67452301;
                    191:     context->state[1] = 0xefcdab89;
                    192:     context->state[2] = 0x98badcfe;
                    193:     context->state[3] = 0x10325476;
                    194: }
                    195: 
                    196: /* MD5 block update operation. Continues an MD5 message-digest
                    197:    operation, processing another message block, and updating the
                    198:    context.
                    199:  */
1.15      moko      200: void pa_MD5Update(PA_MD5_CTX *context, const unsigned char *input,
1.1       paf       201:                              unsigned int inputLen)
                    202: {
                    203:     unsigned int i, idx, partLen;
                    204: 
                    205:     /* Compute number of bytes mod 64 */
                    206:     idx = (unsigned int) ((context->count[0] >> 3) & 0x3F);
                    207: 
                    208:     /* Update number of bits */
                    209:     if ((context->count[0] += ((UINT4) inputLen << 3))
                    210:        < ((UINT4) inputLen << 3)) {
                    211:        context->count[1]++;
                    212:     }
                    213:     context->count[1] += (UINT4) inputLen >> 29;
                    214: 
                    215:     partLen = 64 - idx;
                    216: 
                    217:     /* Transform as many times as possible. */
                    218: #ifndef CHARSET_EBCDIC
                    219:     if (inputLen >= partLen) {
                    220:        memcpy(&context->buffer[idx], input, partLen);
                    221:        MD5Transform(context->state, context->buffer);
                    222: 
                    223:        for (i = partLen; i + 63 < inputLen; i += 64) {
                    224:            MD5Transform(context->state, &input[i]);
                    225:        }
                    226: 
                    227:        idx = 0;
                    228:     }
                    229:     else {
                    230:        i = 0;
                    231:     }
                    232: 
                    233:     /* Buffer remaining input */
                    234:     memcpy(&context->buffer[idx], &input[i], inputLen - i);
                    235: #else /*CHARSET_EBCDIC*/
                    236:     if (inputLen >= partLen) {
                    237:        ebcdic2ascii(&context->buffer[idx], input, partLen);
                    238:        MD5Transform(context->state, context->buffer);
                    239: 
                    240:        for (i = partLen; i + 63 < inputLen; i += 64) {
                    241:            unsigned char inp_tmp[64];
                    242:            ebcdic2ascii(inp_tmp, &input[i], 64);
                    243:            MD5Transform(context->state, inp_tmp);
                    244:        }
                    245: 
                    246:        idx = 0;
                    247:     }
                    248:     else {
                    249:        i = 0;
                    250:     }
                    251: 
                    252:     /* Buffer remaining input */
                    253:     ebcdic2ascii(&context->buffer[idx], &input[i], inputLen - i);
                    254: #endif /*CHARSET_EBCDIC*/
                    255: }
                    256: 
                    257: /* MD5 finalization. Ends an MD5 message-digest operation, writing the
                    258:    the message digest and zeroizing the context.
                    259:  */
1.15      moko      260: void pa_MD5Final(unsigned char digest[16], PA_MD5_CTX *context)
1.1       paf       261: {
                    262:     unsigned char bits[8];
                    263:     unsigned int idx, padLen;
                    264: 
                    265: 
                    266:     /* Save number of bits */
                    267:     Encode(bits, context->count, 8);
                    268: 
                    269: #ifdef CHARSET_EBCDIC
                    270:     /* XXX: @@@: In order to make this no more complex than necessary,
                    271:      * this kludge converts the bits[] array using the ascii-to-ebcdic
1.10      paf       272:      * table, because the following pa_MD5Update() re-translates
1.1       paf       273:      * its input (ebcdic-to-ascii).
1.10      paf       274:      * Otherwise, we would have to pass a "conversion" flag to pa_MD5Update()
1.1       paf       275:      */
                    276:     ascii2ebcdic(bits,bits,8);
                    277: 
1.10      paf       278:     /* Since everything is converted to ascii within pa_MD5Update(), 
1.1       paf       279:      * the initial 0x80 (PADDING[0]) must be stored as 0x20 
                    280:      */
                    281:     PADDING[0] = os_toebcdic[0x80];
                    282: #endif /*CHARSET_EBCDIC*/
                    283: 
                    284:     /* Pad out to 56 mod 64. */
                    285:     idx = (unsigned int) ((context->count[0] >> 3) & 0x3f);
                    286:     padLen = (idx < 56) ? (56 - idx) : (120 - idx);
1.10      paf       287:     pa_MD5Update(context, (const unsigned char *)PADDING, padLen);
1.1       paf       288: 
                    289:     /* Append length (before padding) */
1.10      paf       290:     pa_MD5Update(context, (const unsigned char *)bits, 8);
1.1       paf       291: 
                    292:     /* Store state in digest */
                    293:     Encode(digest, context->state, 16);
                    294: 
                    295:     /* Zeroize sensitive information. */
                    296:     memset(context, 0, sizeof(*context));
                    297: }
                    298: 
                    299: /* MD5 basic transformation. Transforms state based on block. */
                    300: static void MD5Transform(UINT4 state[4], const unsigned char block[64])
                    301: {
                    302:     UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
                    303: 
                    304:     Decode(x, block, 64);
                    305: 
                    306:     /* Round 1 */
                    307:     FF(a, b, c, d, x[0], S11, 0xd76aa478);     /* 1 */
                    308:     FF(d, a, b, c, x[1], S12, 0xe8c7b756);     /* 2 */
                    309:     FF(c, d, a, b, x[2], S13, 0x242070db);     /* 3 */
                    310:     FF(b, c, d, a, x[3], S14, 0xc1bdceee);     /* 4 */
                    311:     FF(a, b, c, d, x[4], S11, 0xf57c0faf);     /* 5 */
                    312:     FF(d, a, b, c, x[5], S12, 0x4787c62a);     /* 6 */
                    313:     FF(c, d, a, b, x[6], S13, 0xa8304613);     /* 7 */
                    314:     FF(b, c, d, a, x[7], S14, 0xfd469501);     /* 8 */
                    315:     FF(a, b, c, d, x[8], S11, 0x698098d8);     /* 9 */
                    316:     FF(d, a, b, c, x[9], S12, 0x8b44f7af);     /* 10 */
                    317:     FF(c, d, a, b, x[10], S13, 0xffff5bb1);    /* 11 */
                    318:     FF(b, c, d, a, x[11], S14, 0x895cd7be);    /* 12 */
                    319:     FF(a, b, c, d, x[12], S11, 0x6b901122);    /* 13 */
                    320:     FF(d, a, b, c, x[13], S12, 0xfd987193);    /* 14 */
                    321:     FF(c, d, a, b, x[14], S13, 0xa679438e);    /* 15 */
                    322:     FF(b, c, d, a, x[15], S14, 0x49b40821);    /* 16 */
                    323: 
                    324:     /* Round 2 */
                    325:     GG(a, b, c, d, x[1], S21, 0xf61e2562);     /* 17 */
                    326:     GG(d, a, b, c, x[6], S22, 0xc040b340);     /* 18 */
                    327:     GG(c, d, a, b, x[11], S23, 0x265e5a51);    /* 19 */
                    328:     GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);     /* 20 */
                    329:     GG(a, b, c, d, x[5], S21, 0xd62f105d);     /* 21 */
                    330:     GG(d, a, b, c, x[10], S22, 0x2441453);     /* 22 */
                    331:     GG(c, d, a, b, x[15], S23, 0xd8a1e681);    /* 23 */
                    332:     GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);     /* 24 */
                    333:     GG(a, b, c, d, x[9], S21, 0x21e1cde6);     /* 25 */
                    334:     GG(d, a, b, c, x[14], S22, 0xc33707d6);    /* 26 */
                    335:     GG(c, d, a, b, x[3], S23, 0xf4d50d87);     /* 27 */
                    336:     GG(b, c, d, a, x[8], S24, 0x455a14ed);     /* 28 */
                    337:     GG(a, b, c, d, x[13], S21, 0xa9e3e905);    /* 29 */
                    338:     GG(d, a, b, c, x[2], S22, 0xfcefa3f8);     /* 30 */
                    339:     GG(c, d, a, b, x[7], S23, 0x676f02d9);     /* 31 */
                    340:     GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);    /* 32 */
                    341: 
                    342:     /* Round 3 */
                    343:     HH(a, b, c, d, x[5], S31, 0xfffa3942);     /* 33 */
                    344:     HH(d, a, b, c, x[8], S32, 0x8771f681);     /* 34 */
                    345:     HH(c, d, a, b, x[11], S33, 0x6d9d6122);    /* 35 */
                    346:     HH(b, c, d, a, x[14], S34, 0xfde5380c);    /* 36 */
                    347:     HH(a, b, c, d, x[1], S31, 0xa4beea44);     /* 37 */
                    348:     HH(d, a, b, c, x[4], S32, 0x4bdecfa9);     /* 38 */
                    349:     HH(c, d, a, b, x[7], S33, 0xf6bb4b60);     /* 39 */
                    350:     HH(b, c, d, a, x[10], S34, 0xbebfbc70);    /* 40 */
                    351:     HH(a, b, c, d, x[13], S31, 0x289b7ec6);    /* 41 */
                    352:     HH(d, a, b, c, x[0], S32, 0xeaa127fa);     /* 42 */
                    353:     HH(c, d, a, b, x[3], S33, 0xd4ef3085);     /* 43 */
                    354:     HH(b, c, d, a, x[6], S34, 0x4881d05);      /* 44 */
                    355:     HH(a, b, c, d, x[9], S31, 0xd9d4d039);     /* 45 */
                    356:     HH(d, a, b, c, x[12], S32, 0xe6db99e5);    /* 46 */
                    357:     HH(c, d, a, b, x[15], S33, 0x1fa27cf8);    /* 47 */
                    358:     HH(b, c, d, a, x[2], S34, 0xc4ac5665);     /* 48 */
                    359: 
                    360:     /* Round 4 */
                    361:     II(a, b, c, d, x[0], S41, 0xf4292244);     /* 49 */
                    362:     II(d, a, b, c, x[7], S42, 0x432aff97);     /* 50 */
                    363:     II(c, d, a, b, x[14], S43, 0xab9423a7);    /* 51 */
                    364:     II(b, c, d, a, x[5], S44, 0xfc93a039);     /* 52 */
                    365:     II(a, b, c, d, x[12], S41, 0x655b59c3);    /* 53 */
                    366:     II(d, a, b, c, x[3], S42, 0x8f0ccc92);     /* 54 */
                    367:     II(c, d, a, b, x[10], S43, 0xffeff47d);    /* 55 */
                    368:     II(b, c, d, a, x[1], S44, 0x85845dd1);     /* 56 */
                    369:     II(a, b, c, d, x[8], S41, 0x6fa87e4f);     /* 57 */
                    370:     II(d, a, b, c, x[15], S42, 0xfe2ce6e0);    /* 58 */
                    371:     II(c, d, a, b, x[6], S43, 0xa3014314);     /* 59 */
                    372:     II(b, c, d, a, x[13], S44, 0x4e0811a1);    /* 60 */
                    373:     II(a, b, c, d, x[4], S41, 0xf7537e82);     /* 61 */
                    374:     II(d, a, b, c, x[11], S42, 0xbd3af235);    /* 62 */
                    375:     II(c, d, a, b, x[2], S43, 0x2ad7d2bb);     /* 63 */
                    376:     II(b, c, d, a, x[9], S44, 0xeb86d391);     /* 64 */
                    377: 
                    378:     state[0] += a;
                    379:     state[1] += b;
                    380:     state[2] += c;
                    381:     state[3] += d;
                    382: 
                    383:     /* Zeroize sensitive information. */
                    384:     memset(x, 0, sizeof(x));
                    385: }
                    386: 
                    387: /* Encodes input (UINT4) into output (unsigned char). Assumes len is
                    388:    a multiple of 4.
                    389:  */
                    390: static void Encode(unsigned char *output, const UINT4 *input, unsigned int len)
                    391: {
                    392:     unsigned int i, j;
                    393:     UINT4 k;
                    394: 
                    395:     for (i = 0, j = 0; j < len; i++, j += 4) {
                    396:        k = input[i];
                    397:        output[j] = (unsigned char) (k & 0xff);
                    398:        output[j + 1] = (unsigned char) ((k >> 8) & 0xff);
                    399:        output[j + 2] = (unsigned char) ((k >> 16) & 0xff);
                    400:        output[j + 3] = (unsigned char) ((k >> 24) & 0xff);
                    401:     }
                    402: }
                    403: 
                    404: /* Decodes input (unsigned char) into output (UINT4). Assumes len is
                    405:  * a multiple of 4.
                    406:  */
                    407: static void Decode(UINT4 *output, const unsigned char *input, unsigned int len)
                    408: {
                    409:     unsigned int i, j;
                    410: 
                    411:     for (i = 0, j = 0; j < len; i++, j += 4)
                    412:        output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) |
                    413:            (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
                    414: }
                    415: 
                    416: /*
                    417:  * The following MD5 password encryption code was largely borrowed from
                    418:  * the FreeBSD 3.0 /usr/src/lib/libcrypt/crypt.c file, which is
                    419:  * licenced as stated at the top of this file.
                    420:  */
1.15      moko      421: void pa_to64(char *s, unsigned long v, int n)
1.1       paf       422: {
                    423:     static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
                    424:        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    425: 
                    426:     while (--n >= 0) {
                    427:        *s++ = itoa64[v&0x3f];
                    428:        v >>= 6;
                    429:     }
                    430: }
                    431: 
1.15      moko      432: void pa_MD5Encode(const unsigned char *pw,
1.10      paf       433:                              const unsigned char *salt,
                    434:                              char *result, size_t nbytes)
1.1       paf       435: {
                    436:     /*
                    437:      * Minimum size is 8 bytes for salt, plus 1 for the trailing NUL,
                    438:      * plus 4 for the '$' separators, plus the password hash itself.
                    439:      * Let's leave a goodly amount of leeway.
                    440:      */
                    441: 
                    442:     char passwd[120], *p;
                    443:     const unsigned char *sp, *ep;
                    444:     unsigned char final[16];
                    445:     int i;
                    446:     unsigned int sl;
                    447:     int pl;
                    448:     unsigned int pwlen;
                    449:     PA_MD5_CTX ctx, ctx1;
                    450:     unsigned long l;
                    451: 
                    452:     /* 
                    453:      * Refine the salt first.  It's possible we were given an already-hashed
                    454:      * string as the salt argument, so extract the actual salt value from it
                    455:      * if so.  Otherwise just use the string up to the first '$' as the salt.
                    456:      */
                    457:     sp = salt;
                    458: 
                    459:     /*
                    460:      * If it starts with the magic string, then skip that.
                    461:      */
                    462:     if (strncmp((char *)sp, PA_MD5PW_ID, PA_MD5PW_IDLEN) == 0) {
                    463:        sp += PA_MD5PW_IDLEN;
                    464:     }
                    465: 
                    466:     /*
                    467:      * It stops at the first '$' or 8 chars, whichever comes first
                    468:      */
                    469:     for (ep = sp; (*ep != '\0') && (*ep != '$') && (ep < (sp + 8)); ep++) {
                    470:        continue;
                    471:     }
                    472: 
                    473:     /*
                    474:      * Get the length of the true salt
                    475:      */
                    476:     sl = ep - sp;
                    477: 
                    478:     /*
                    479:      * 'Time to make the doughnuts..'
                    480:      */
1.10      paf       481:     pa_MD5Init(&ctx);
1.1       paf       482: 
                    483:     pwlen = strlen((char *)pw);
                    484:     /*
                    485:      * The password first, since that is what is most unknown
                    486:      */
1.10      paf       487:     pa_MD5Update(&ctx, pw, pwlen);
1.1       paf       488: 
1.10      paf       489:     /*
                    490:      * Then our magic string
                    491:      */
                    492:     pa_MD5Update(&ctx, (const unsigned char *) PA_MD5PW_ID, PA_MD5PW_IDLEN);
1.1       paf       493: 
                    494:     /*
                    495:      * Then the raw salt
                    496:      */
1.10      paf       497:     pa_MD5Update(&ctx, sp, sl);
1.1       paf       498: 
                    499:     /*
                    500:      * Then just as many characters of the MD5(pw, salt, pw)
                    501:      */
1.10      paf       502:     pa_MD5Init(&ctx1);
                    503:     pa_MD5Update(&ctx1, pw, pwlen);
                    504:     pa_MD5Update(&ctx1, sp, sl);
                    505:     pa_MD5Update(&ctx1, pw, pwlen);
                    506:     pa_MD5Final(final, &ctx1);
1.1       paf       507:     for(pl = pwlen; pl > 0; pl -= 16) {
1.10      paf       508:        pa_MD5Update(&ctx, final, (pl > 16) ? 16 : (unsigned int) pl);
1.1       paf       509:     }
                    510: 
                    511:     /*
                    512:      * Don't leave anything around in vm they could use.
                    513:      */
                    514:     memset(final, 0, sizeof(final));
                    515: 
                    516:     /*
                    517:      * Then something really weird...
                    518:      */
                    519:     for (i = pwlen; i != 0; i >>= 1) {
                    520:        if (i & 1) {
1.10      paf       521:            pa_MD5Update(&ctx, final, 1);
1.1       paf       522:        }
                    523:        else {
1.10      paf       524:            pa_MD5Update(&ctx, pw, 1);
1.1       paf       525:        }
                    526:     }
                    527: 
                    528:     /*
                    529:      * Now make the output string.  We know our limitations, so we
                    530:      * can use the string routines without bounds checking.
                    531:      */
1.10      paf       532:     pa_pa_cpystrn(passwd, PA_MD5PW_ID, PA_MD5PW_IDLEN + 1);
                    533:     pa_pa_cpystrn(passwd + PA_MD5PW_IDLEN, (char *)sp, sl + 1);
1.1       paf       534:     passwd[PA_MD5PW_IDLEN + sl]     = '$';
                    535:     passwd[PA_MD5PW_IDLEN + sl + 1] = '\0';
                    536: 
1.10      paf       537:     pa_MD5Final(final, &ctx);
1.1       paf       538: 
                    539:     /*
                    540:      * And now, just to make sure things don't run too fast..
                    541:      * On a 60 Mhz Pentium this takes 34 msec, so you would
                    542:      * need 30 seconds to build a 1000 entry dictionary...
                    543:      */
                    544:     for (i = 0; i < 1000; i++) {
1.10      paf       545:        pa_MD5Init(&ctx1);
1.1       paf       546:        if (i & 1) {
1.10      paf       547:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       548:        }
                    549:        else {
1.10      paf       550:            pa_MD5Update(&ctx1, final, 16);
1.1       paf       551:        }
                    552:        if (i % 3) {
1.10      paf       553:            pa_MD5Update(&ctx1, sp, sl);
1.1       paf       554:        }
                    555: 
                    556:        if (i % 7) {
1.10      paf       557:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       558:        }
                    559: 
                    560:        if (i & 1) {
1.10      paf       561:            pa_MD5Update(&ctx1, final, 16);
1.1       paf       562:        }
                    563:        else {
1.10      paf       564:            pa_MD5Update(&ctx1, pw, pwlen);
1.1       paf       565:        }
1.10      paf       566:        pa_MD5Final(final,&ctx1);
1.1       paf       567:     }
                    568: 
                    569:     p = passwd + strlen(passwd);
                    570: 
1.10      paf       571:     l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; pa_to64(p, l, 4); p += 4;
                    572:     l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; pa_to64(p, l, 4); p += 4;
                    573:     l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; pa_to64(p, l, 4); p += 4;
                    574:     l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; pa_to64(p, l, 4); p += 4;
                    575:     l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; pa_to64(p, l, 4); p += 4;
                    576:     l =                    final[11]                ; pa_to64(p, l, 2); p += 2;
1.1       paf       577:     *p = '\0';
                    578: 
                    579:     /*
                    580:      * Don't leave anything around in vm they could use.
                    581:      */
                    582:     memset(final, 0, sizeof(final));
1.8       paf       583: 
1.10      paf       584:     pa_pa_cpystrn(result, passwd, nbytes - 1);
1.1       paf       585: }

E-mail: