Annotation of parser3/src/lib/md5/pa_md5c.c, revision 1.1
1.1 ! paf 1: /*
! 2: * This is work is derived from material Copyright RSA Data Security, Inc.
! 3: *
! 4: * The RSA copyright statement and Licence for that original material is
! 5: * included below. This is followed by the Apache copyright statement and
! 6: * licence for the modifications made to that material.
! 7: */
! 8:
! 9: /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
! 10: */
! 11:
! 12: /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
! 13: rights reserved.
! 14:
! 15: License to copy and use this software is granted provided that it
! 16: is identified as the "RSA Data Security, Inc. MD5 Message-Digest
! 17: Algorithm" in all material mentioning or referencing this software
! 18: or this function.
! 19:
! 20: License is also granted to make and use derivative works provided
! 21: that such works are identified as "derived from the RSA Data
! 22: Security, Inc. MD5 Message-Digest Algorithm" in all material
! 23: mentioning or referencing the derived work.
! 24:
! 25: RSA Data Security, Inc. makes no representations concerning either
! 26: the merchantability of this software or the suitability of this
! 27: software for any particular purpose. It is provided "as is"
! 28: without express or implied warranty of any kind.
! 29:
! 30: These notices must be retained in any copies of any part of this
! 31: documentation and/or software.
! 32: */
! 33:
! 34: /* ====================================================================
! 35: * Copyright (c) 1996-1999 The Apache Group. All rights reserved.
! 36: *
! 37: * Redistribution and use in source and binary forms, with or without
! 38: * modification, are permitted provided that the following conditions
! 39: * are met:
! 40: *
! 41: * 1. Redistributions of source code must retain the above copyright
! 42: * notice, this list of conditions and the following disclaimer.
! 43: *
! 44: * 2. Redistributions in binary form must reproduce the above copyright
! 45: * notice, this list of conditions and the following disclaimer in
! 46: * the documentation and/or other materials provided with the
! 47: * distribution.
! 48: *
! 49: * 3. All advertising materials mentioning features or use of this
! 50: * software must display the following acknowledgment:
! 51: * "This product includes software developed by the Apache Group
! 52: * for use in the Apache HTTP server project (http://www.apache.org/)."
! 53: *
! 54: * 4. The names "Apache Server" and "Apache Group" must not be used to
! 55: * endorse or promote products derived from this software without
! 56: * prior written permission. For written permission, please contact
! 57: * apache@apache.org.
! 58: *
! 59: * 5. Products derived from this software may not be called "Apache"
! 60: * nor may "Apache" appear in their names without prior written
! 61: * permission of the Apache Group.
! 62: *
! 63: * 6. Redistributions of any form whatsoever must retain the following
! 64: * acknowledgment:
! 65: * "This product includes software developed by the Apache Group
! 66: * for use in the Apache HTTP server project (http://www.apache.org/)."
! 67: *
! 68: * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
! 69: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 70: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
! 71: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
! 72: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! 73: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
! 74: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
! 75: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 76: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
! 77: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
! 78: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
! 79: * OF THE POSSIBILITY OF SUCH DAMAGE.
! 80: * ====================================================================
! 81: *
! 82: * This software consists of voluntary contributions made by many
! 83: * individuals on behalf of the Apache Group and was originally based
! 84: * on public domain software written at the National Center for
! 85: * Supercomputing Applications, University of Illinois, Urbana-Champaign.
! 86: * For more information on the Apache Group and the Apache HTTP server
! 87: * project, please see <http://www.apache.org/>.
! 88: *
! 89: */
! 90:
! 91: /*
! 92: * The pa_MD5Encode() routine uses much code obtained from the FreeBSD 3.0
! 93: * MD5 crypt() function, which is licenced as follows:
! 94: * ----------------------------------------------------------------------------
! 95: * "THE BEER-WARE LICENSE" (Revision 42):
! 96: * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
! 97: * can do whatever you want with this stuff. If we meet some day, and you think
! 98: * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
! 99: * ----------------------------------------------------------------------------
! 100: */
! 101:
! 102: #include "pa_config_includes.h"
! 103: #include "pa_md5.h"
! 104: //#include "ap.h"
! 105: //#ifdef CHARSET_EBCDIC
! 106: //#include "ebcdic.h"
! 107: //#endif /*CHARSET_EBCDIC*/
! 108: //#if HAVE_CRYPT_H
! 109: //#include <crypt.h>
! 110: //#endif
! 111:
! 112: #define pa_pa_cpystrn(strDest, strSource, count) strncpy(strDest, strSource, count)
! 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: */
! 184: PA_API_EXPORT(void) pa_MD5Init(PA_MD5_CTX *context)
! 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: */
! 198: PA_API_EXPORT(void) pa_MD5Update(PA_MD5_CTX *context, const unsigned char *input,
! 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: */
! 258: PA_API_EXPORT(void) pa_MD5Final(unsigned char digest[16], PA_MD5_CTX *context)
! 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
! 270: * table, because the following pa_MD5Update() re-translates
! 271: * its input (ebcdic-to-ascii).
! 272: * Otherwise, we would have to pass a "conversion" flag to pa_MD5Update()
! 273: */
! 274: ascii2ebcdic(bits,bits,8);
! 275:
! 276: /* Since everything is converted to ascii within pa_MD5Update(),
! 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);
! 285: pa_MD5Update(context, (const unsigned char *)PADDING, padLen);
! 286:
! 287: /* Append length (before padding) */
! 288: pa_MD5Update(context, (const unsigned char *)bits, 8);
! 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: */
! 419: PA_API_EXPORT(void) pa_to64(char *s, unsigned long v, int n)
! 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:
! 430: PA_API_EXPORT(void) pa_MD5Encode(const unsigned char *pw,
! 431: const unsigned char *salt,
! 432: char *result, size_t nbytes)
! 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: */
! 479: pa_MD5Init(&ctx);
! 480:
! 481: pwlen = strlen((char *)pw);
! 482: /*
! 483: * The password first, since that is what is most unknown
! 484: */
! 485: pa_MD5Update(&ctx, pw, pwlen);
! 486:
! 487: /*
! 488: * Then our magic string
! 489: */
! 490: pa_MD5Update(&ctx, (const unsigned char *) PA_MD5PW_ID, PA_MD5PW_IDLEN);
! 491:
! 492: /*
! 493: * Then the raw salt
! 494: */
! 495: pa_MD5Update(&ctx, sp, sl);
! 496:
! 497: /*
! 498: * Then just as many characters of the MD5(pw, salt, pw)
! 499: */
! 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);
! 505: for(pl = pwlen; pl > 0; pl -= 16) {
! 506: pa_MD5Update(&ctx, final, (pl > 16) ? 16 : (unsigned int) pl);
! 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) {
! 519: pa_MD5Update(&ctx, final, 1);
! 520: }
! 521: else {
! 522: pa_MD5Update(&ctx, pw, 1);
! 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: */
! 530: pa_pa_cpystrn(passwd, PA_MD5PW_ID, PA_MD5PW_IDLEN + 1);
! 531: pa_pa_cpystrn(passwd + PA_MD5PW_IDLEN, (char *)sp, sl + 1);
! 532: passwd[PA_MD5PW_IDLEN + sl] = '$';
! 533: passwd[PA_MD5PW_IDLEN + sl + 1] = '\0';
! 534:
! 535: pa_MD5Final(final, &ctx);
! 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++) {
! 543: pa_MD5Init(&ctx1);
! 544: if (i & 1) {
! 545: pa_MD5Update(&ctx1, pw, pwlen);
! 546: }
! 547: else {
! 548: pa_MD5Update(&ctx1, final, 16);
! 549: }
! 550: if (i % 3) {
! 551: pa_MD5Update(&ctx1, sp, sl);
! 552: }
! 553:
! 554: if (i % 7) {
! 555: pa_MD5Update(&ctx1, pw, pwlen);
! 556: }
! 557:
! 558: if (i & 1) {
! 559: pa_MD5Update(&ctx1, final, 16);
! 560: }
! 561: else {
! 562: pa_MD5Update(&ctx1, pw, pwlen);
! 563: }
! 564: pa_MD5Final(final,&ctx1);
! 565: }
! 566:
! 567: p = passwd + strlen(passwd);
! 568:
! 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;
! 575: *p = '\0';
! 576:
! 577: /*
! 578: * Don't leave anything around in vm they could use.
! 579: */
! 580: memset(final, 0, sizeof(final));
! 581:
! 582: pa_pa_cpystrn(result, passwd, nbytes - 1);
! 583: }
E-mail: