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

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

E-mail: