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

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

E-mail: