xref: /onnv-gate/usr/src/lib/crypt_modules/sunmd5/sunmd5.c (revision 10935:fb2b65d50333)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*10935Sopensolaris@drydog.com  * Common Development and Distribution License (the "License").
6*10935Sopensolaris@drydog.com  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10935Sopensolaris@drydog.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <fcntl.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <pwd.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <crypt.h>
370Sstevel@tonic-gate #include <md5.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #define	CRYPT_ALGNAME	"md5"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* minimum number of rounds we do, not including the per-user ones */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	BASIC_ROUND_COUNT 4096 /* enough to make things interesting */
450Sstevel@tonic-gate #define	DIGEST_LEN	16
460Sstevel@tonic-gate #define	ROUND_BUFFER_LEN	64
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Public domain quotation courtesy of Project Gutenberg.
500Sstevel@tonic-gate  * ftp://metalab.unc.edu/pub/docs/books/gutenberg/etext98/2ws2610.txt
510Sstevel@tonic-gate  * Hamlet III.ii - 1517 bytes, including trailing NUL
520Sstevel@tonic-gate  * ANSI-C string constant concatenation is a requirement here.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static const char constant_phrase[] =
560Sstevel@tonic-gate 	"To be, or not to be,--that is the question:--\n"
570Sstevel@tonic-gate 	"Whether 'tis nobler in the mind to suffer\n"
580Sstevel@tonic-gate 	"The slings and arrows of outrageous fortune\n"
590Sstevel@tonic-gate 	"Or to take arms against a sea of troubles,\n"
600Sstevel@tonic-gate 	"And by opposing end them?--To die,--to sleep,--\n"
610Sstevel@tonic-gate 	"No more; and by a sleep to say we end\n"
620Sstevel@tonic-gate 	"The heartache, and the thousand natural shocks\n"
630Sstevel@tonic-gate 	"That flesh is heir to,--'tis a consummation\n"
640Sstevel@tonic-gate 	"Devoutly to be wish'd. To die,--to sleep;--\n"
650Sstevel@tonic-gate 	"To sleep! perchance to dream:--ay, there's the rub;\n"
660Sstevel@tonic-gate 	"For in that sleep of death what dreams may come,\n"
670Sstevel@tonic-gate 	"When we have shuffled off this mortal coil,\n"
680Sstevel@tonic-gate 	"Must give us pause: there's the respect\n"
690Sstevel@tonic-gate 	"That makes calamity of so long life;\n"
700Sstevel@tonic-gate 	"For who would bear the whips and scorns of time,\n"
710Sstevel@tonic-gate 	"The oppressor's wrong, the proud man's contumely,\n"
720Sstevel@tonic-gate 	"The pangs of despis'd love, the law's delay,\n"
730Sstevel@tonic-gate 	"The insolence of office, and the spurns\n"
740Sstevel@tonic-gate 	"That patient merit of the unworthy takes,\n"
750Sstevel@tonic-gate 	"When he himself might his quietus make\n"
760Sstevel@tonic-gate 	"With a bare bodkin? who would these fardels bear,\n"
770Sstevel@tonic-gate 	"To grunt and sweat under a weary life,\n"
780Sstevel@tonic-gate 	"But that the dread of something after death,--\n"
790Sstevel@tonic-gate 	"The undiscover'd country, from whose bourn\n"
800Sstevel@tonic-gate 	"No traveller returns,--puzzles the will,\n"
810Sstevel@tonic-gate 	"And makes us rather bear those ills we have\n"
820Sstevel@tonic-gate 	"Than fly to others that we know not of?\n"
830Sstevel@tonic-gate 	"Thus conscience does make cowards of us all;\n"
840Sstevel@tonic-gate 	"And thus the native hue of resolution\n"
850Sstevel@tonic-gate 	"Is sicklied o'er with the pale cast of thought;\n"
860Sstevel@tonic-gate 	"And enterprises of great pith and moment,\n"
870Sstevel@tonic-gate 	"With this regard, their currents turn awry,\n"
880Sstevel@tonic-gate 	"And lose the name of action.--Soft you now!\n"
890Sstevel@tonic-gate 	"The fair Ophelia!--Nymph, in thy orisons\n"
900Sstevel@tonic-gate 	"Be all my sins remember'd.\n";
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /* ------------------------------------------------------------------ */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static int
md5bit(uint8_t * digest,int bit_num)950Sstevel@tonic-gate md5bit(uint8_t *digest, int bit_num)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	int byte_off;
980Sstevel@tonic-gate 	int bit_off;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	bit_num %= 128; /* keep this bounded for convenience */
1010Sstevel@tonic-gate 	byte_off = bit_num / 8;
1020Sstevel@tonic-gate 	bit_off = bit_num % 8;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/* return the value of bit N from the digest */
1050Sstevel@tonic-gate 	return ((digest[byte_off] & (0x01 << bit_off)) ? 1 : 0);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static uchar_t itoa64[] =		/* 0 ... 63 => ascii - 64 */
1090Sstevel@tonic-gate 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static void
to64(char * s,uint64_t v,int n)1120Sstevel@tonic-gate to64(char *s, uint64_t v, int n)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	while (--n >= 0) {
115*10935Sopensolaris@drydog.com 		*s++ = itoa64[v & 0x3f];
1160Sstevel@tonic-gate 		v >>= 6;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #define	ROUNDS		"rounds="
1210Sstevel@tonic-gate #define	ROUNDSLEN	(sizeof (ROUNDS) - 1)
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * get the integer value after rounds= where ever it occurs in the string.
1250Sstevel@tonic-gate  * if the last char after the int is a , or $ that is fine anything else is an
1260Sstevel@tonic-gate  * error.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static uint32_t
getrounds(const char * s)1290Sstevel@tonic-gate getrounds(const char *s)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	char *r, *p, *e;
1320Sstevel@tonic-gate 	long val;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (s == NULL)
1350Sstevel@tonic-gate 		return (0);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if ((r = strstr(s, ROUNDS)) == NULL) {
1380Sstevel@tonic-gate 		return (0);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (strncmp(r, ROUNDS, ROUNDSLEN) != 0) {
1420Sstevel@tonic-gate 		return (0);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	p = r + ROUNDSLEN;
1460Sstevel@tonic-gate 	errno = 0;
1470Sstevel@tonic-gate 	val = strtol(p, &e, 10);
1480Sstevel@tonic-gate 	/*
149*10935Sopensolaris@drydog.com 	 * An error occurred or there is non-numeric stuff at the end
1500Sstevel@tonic-gate 	 * which isn't one of the crypt(3c) special chars ',' or '$'
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	if (errno != 0 || val < 0 ||
1530Sstevel@tonic-gate 	    !(*e == '\0' || *e == ',' || *e == '$')) {
1540Sstevel@tonic-gate 		syslog(LOG_WARNING,
1550Sstevel@tonic-gate 		    "crypt_sunmd5: invalid rounds specification \"%s\"", s);
1560Sstevel@tonic-gate 		return (0);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return ((uint32_t)val);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
162*10935Sopensolaris@drydog.com /* ARGSUSED3 */
1630Sstevel@tonic-gate char *
crypt_gensalt_impl(char * gsbuffer,size_t gsbufflen,const char * oldsalt,const struct passwd * userinfo,const char ** params)1640Sstevel@tonic-gate crypt_gensalt_impl(char *gsbuffer,
1650Sstevel@tonic-gate 	    size_t gsbufflen,
1660Sstevel@tonic-gate 	    const char *oldsalt,
1670Sstevel@tonic-gate 	    const struct passwd *userinfo,
1680Sstevel@tonic-gate 	    const char **params)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	uint32_t confrounds = 0;
1710Sstevel@tonic-gate 	uint32_t saltrounds;
1720Sstevel@tonic-gate 	int i;
1730Sstevel@tonic-gate 	int fd;
1740Sstevel@tonic-gate 	ssize_t got;
1750Sstevel@tonic-gate 	uint64_t rndval;
1760Sstevel@tonic-gate 	char rndstr[sizeof (rndval) + 1];	/* rndval as a base64 string */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	for (i = 0; params != NULL && params[i] != NULL; i++) {
1790Sstevel@tonic-gate 		if (strncmp(params[i], ROUNDS, ROUNDSLEN) == 0) {
1800Sstevel@tonic-gate 			confrounds = getrounds(params[i]);
1810Sstevel@tonic-gate 		} else {
1820Sstevel@tonic-gate 			syslog(LOG_WARNING,
1830Sstevel@tonic-gate 			    "crypt_sunmd5: invalid parameter %s", params[i]);
1840Sstevel@tonic-gate 			errno = EINVAL;
1850Sstevel@tonic-gate 			return (NULL);
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * If the config file has a higher value for rounds= than what
1910Sstevel@tonic-gate 	 * was in the old salt use that, otherwise keep what was in the
1920Sstevel@tonic-gate 	 * old salt.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	saltrounds = getrounds(oldsalt);
1950Sstevel@tonic-gate 	if (confrounds > saltrounds) {
1960Sstevel@tonic-gate 		saltrounds = confrounds;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if ((fd = open("/dev/random", O_RDONLY)) == -1) {
2000Sstevel@tonic-gate 		goto fail;
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	got = read(fd, &rndval, sizeof (rndval));
2040Sstevel@tonic-gate 	if (got < sizeof (rndval)) {
2050Sstevel@tonic-gate 		int err = errno;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		(void) close(fd);
2080Sstevel@tonic-gate 		errno = err;
2090Sstevel@tonic-gate 		goto fail;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	(void) close(fd);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	to64((char *)&rndstr, rndval, sizeof (rndval));
2140Sstevel@tonic-gate 	rndstr[sizeof (rndstr) - 1] = '\0';
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (saltrounds > 0) {
2170Sstevel@tonic-gate 		if (snprintf(gsbuffer, gsbufflen,
2180Sstevel@tonic-gate 		    "$" CRYPT_ALGNAME "," ROUNDS "%d$",
2190Sstevel@tonic-gate 		    saltrounds) >= gsbufflen)
2200Sstevel@tonic-gate 			goto fail;
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		if (snprintf(gsbuffer, gsbufflen,
2230Sstevel@tonic-gate 		    "$" CRYPT_ALGNAME "$") >= gsbufflen)
2240Sstevel@tonic-gate 			goto fail;
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (strlcat(gsbuffer, rndstr, gsbufflen) >= gsbufflen)
2280Sstevel@tonic-gate 		goto fail;
2290Sstevel@tonic-gate 	if (strlcat(gsbuffer, "$", gsbufflen) >= gsbufflen)
2300Sstevel@tonic-gate 		goto fail;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	return (gsbuffer);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate fail:
2350Sstevel@tonic-gate 	bzero(gsbuffer, gsbufflen);
2360Sstevel@tonic-gate 	return (NULL);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*ARGSUSED4*/
2410Sstevel@tonic-gate char *
crypt_genhash_impl(char * ctbuffer,size_t ctbufflen,const char * plaintext,const char * salt,const char ** params)2420Sstevel@tonic-gate crypt_genhash_impl(char *ctbuffer,
2430Sstevel@tonic-gate 	    size_t ctbufflen,
2440Sstevel@tonic-gate 	    const char *plaintext,
2450Sstevel@tonic-gate 	    const char *salt,
2460Sstevel@tonic-gate 	    const char **params)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	int i;
2490Sstevel@tonic-gate 	int round;
2500Sstevel@tonic-gate 	int maxrounds = BASIC_ROUND_COUNT;
2510Sstevel@tonic-gate 	uint32_t l;
2520Sstevel@tonic-gate 	char *puresalt;
2530Sstevel@tonic-gate 	char *saltend;
2540Sstevel@tonic-gate 	char *p;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* put all the sensitive data in a struct */
2570Sstevel@tonic-gate 	struct {
2580Sstevel@tonic-gate 		MD5_CTX context;	/* working buffer for MD5 algorithm */
2590Sstevel@tonic-gate 		uint8_t digest[DIGEST_LEN]; /* where the MD5 digest is stored */
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		int indirect_4[16]; /* extracted array of 4bit values */
2620Sstevel@tonic-gate 		int shift_4[16];	/* shift schedule, vals 0..4 */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		int s7shift;	/* shift for shift_7 creation, vals  0..7 */
2650Sstevel@tonic-gate 		int indirect_7[16]; /* extracted array of 7bit values */
2660Sstevel@tonic-gate 		int shift_7[16];	/* shift schedule, vals 0..1 */
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		int indirect_a;	 /* 7bit index into digest */
2690Sstevel@tonic-gate 		int shift_a;		/* shift schedule, vals 0..1 */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		int indirect_b;	 /* 7bit index into digest */
2720Sstevel@tonic-gate 		int shift_b;		/* shift schedule, vals 0..1 */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		int bit_a;		  /* single bit for cointoss */
2750Sstevel@tonic-gate 		int bit_b;		  /* single bit for cointoss */
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		char roundascii[ROUND_BUFFER_LEN]; /* ascii rep of roundcount */
2780Sstevel@tonic-gate 	} data;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Extract the puresalt (if it exists) from the existing salt string
2830Sstevel@tonic-gate 	 * $md5[,rounds=%d]$<puresalt>$<optional existing encoding>
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	saltend = strrchr(salt, '$');
2860Sstevel@tonic-gate 	if (saltend == NULL || saltend == salt) {
2870Sstevel@tonic-gate 		return (NULL);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	if (saltend[1] != '\0') {
2900Sstevel@tonic-gate 		size_t len = saltend - salt + 1;
2910Sstevel@tonic-gate 		if ((puresalt = malloc(len)) == NULL) {
2920Sstevel@tonic-gate 			return (NULL);
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 		(void) strlcpy(puresalt, salt, len);
2950Sstevel@tonic-gate 	} else {
2960Sstevel@tonic-gate 		puresalt = strdup(salt);
2970Sstevel@tonic-gate 		if (puresalt == NULL) {
2980Sstevel@tonic-gate 			return (NULL);
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	maxrounds += getrounds(salt);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* initialise the context */
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	MD5Init(&data.context);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/* update with the (hopefully entropic) plaintext */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	MD5Update(&data.context, (uchar_t *)plaintext, strlen(plaintext));
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/* update with the (publically known) salt */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	MD5Update(&data.context, (uchar_t *)puresalt, strlen(puresalt));
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/* compute the digest */
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	MD5Final(data.digest, &data.context);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * now to delay high-speed md5 implementations that have stuff
3230Sstevel@tonic-gate 	 * like code inlining, loops unrolled and table lookup
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	for (round = 0; round < maxrounds; round++) {
3270Sstevel@tonic-gate 		/* re-initialise the context */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		MD5Init(&data.context);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		/* update with the previous digest */
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		MD5Update(&data.context, data.digest, sizeof (data.digest));
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		/* populate the shift schedules for use later */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
3380Sstevel@tonic-gate 			int j;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		/* offset 3 -> occasionally span more than 1 int32 fetch */
3410Sstevel@tonic-gate 			j = (i + 3) % 16;
3420Sstevel@tonic-gate 			data.s7shift = data.digest[i] % 8;
3430Sstevel@tonic-gate 			data.shift_4[i] = data.digest[j] % 5;
3440Sstevel@tonic-gate 			data.shift_7[i] = (data.digest[j] >> data.s7shift)
3450Sstevel@tonic-gate 			    & 0x01;
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		data.shift_a = md5bit(data.digest, round);
3490Sstevel@tonic-gate 		data.shift_b = md5bit(data.digest, round + 64);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		/* populate indirect_4 with 4bit values extracted from digest */
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
3540Sstevel@tonic-gate 			/* shift the digest byte and extract four bits */
3550Sstevel@tonic-gate 			data.indirect_4[i] =
3560Sstevel@tonic-gate 			    (data.digest[i] >> data.shift_4[i]) & 0x0f;
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		/*
3600Sstevel@tonic-gate 		 * populate indirect_7 with 7bit values from digest
3610Sstevel@tonic-gate 		 * indexed via indirect_4
3620Sstevel@tonic-gate 		 */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
3650Sstevel@tonic-gate 			/* shift the digest byte and extract seven bits */
3660Sstevel@tonic-gate 			data.indirect_7[i] = (data.digest[data.indirect_4[i]]
3670Sstevel@tonic-gate 			    >> data.shift_7[i]) & 0x7f;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		/*
3710Sstevel@tonic-gate 		 * use the 7bit values to indirect into digest,
3720Sstevel@tonic-gate 		 * and create two 8bit values from the results.
3730Sstevel@tonic-gate 		 */
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 		data.indirect_a = data.indirect_b = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		for (i = 0; i < 8; i++) {
3780Sstevel@tonic-gate 			data.indirect_a |= (md5bit(data.digest,
3790Sstevel@tonic-gate 			    data.indirect_7[i]) << i);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			data.indirect_b |= (md5bit(data.digest,
3820Sstevel@tonic-gate 			    data.indirect_7[i + 8]) << i);
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		/* shall we utilise the top or bottom 7 bits? */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		data.indirect_a = (data.indirect_a >> data.shift_a) & 0x7f;
3890Sstevel@tonic-gate 		data.indirect_b = (data.indirect_b >> data.shift_b) & 0x7f;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		/* extract two data.digest bits */
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		data.bit_a = md5bit(data.digest, data.indirect_a);
3950Sstevel@tonic-gate 		data.bit_b = md5bit(data.digest, data.indirect_b);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate #if ALGDEBUG
3990Sstevel@tonic-gate 		for (i = 0; i < 15; i++) {
400*10935Sopensolaris@drydog.com 			(void) printf("%1x-", data.indirect_4[i]);
4010Sstevel@tonic-gate 		}
402*10935Sopensolaris@drydog.com 		(void) printf("%1x ", data.indirect_4[15]);
4030Sstevel@tonic-gate 		for (i = 0; i < 15; i++) {
404*10935Sopensolaris@drydog.com 			(void) printf("%02x-", data.indirect_7[i]);
4050Sstevel@tonic-gate 		}
406*10935Sopensolaris@drydog.com 		(void) printf("%02x ", data.indirect_7[15]);
407*10935Sopensolaris@drydog.com 		(void) printf("%02x/%02x ", data.indirect_a, data.indirect_b);
408*10935Sopensolaris@drydog.com 		(void) printf("%d^%d\n", data.bit_a, data.bit_b);
4090Sstevel@tonic-gate #endif
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 		/* xor a coin-toss; if true, mix-in the constant phrase */
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (data.bit_a ^ data.bit_b) {
4150Sstevel@tonic-gate 			MD5Update(&data.context,
4160Sstevel@tonic-gate 			    (unsigned char *) constant_phrase,
4170Sstevel@tonic-gate 			    sizeof (constant_phrase));
4180Sstevel@tonic-gate #if ALGDEBUG
419*10935Sopensolaris@drydog.com 			(void) printf("mixing constant_phrase\n");
4200Sstevel@tonic-gate #endif
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		/* digest a decimal sprintf of the current roundcount */
4250Sstevel@tonic-gate 
426*10935Sopensolaris@drydog.com 		(void) snprintf(data.roundascii, ROUND_BUFFER_LEN, "%d", round);
4270Sstevel@tonic-gate 		MD5Update(&data.context,
4280Sstevel@tonic-gate 		    (unsigned char *) data.roundascii, strlen(data.roundascii));
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/* compute/flush the digest, and loop */
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		MD5Final(data.digest, &data.context);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate #if ALGDEBUG
4370Sstevel@tonic-gate 	/* print the digest */
4380Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
439*10935Sopensolaris@drydog.com 		(void) printf("%02x", data.digest[i]);
4400Sstevel@tonic-gate 	}
441*10935Sopensolaris@drydog.com 	(void) printf("\n");
4420Sstevel@tonic-gate #endif
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	(void) snprintf(ctbuffer, ctbufflen, "%s$", puresalt);
4450Sstevel@tonic-gate 	p = ctbuffer + strlen(ctbuffer);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	l = (data.digest[ 0]<<16) | (data.digest[ 6]<<8) | data.digest[12];
4480Sstevel@tonic-gate 	to64(p, l, 4); p += 4;
4490Sstevel@tonic-gate 	l = (data.digest[ 1]<<16) | (data.digest[ 7]<<8) | data.digest[13];
4500Sstevel@tonic-gate 	to64(p, l, 4); p += 4;
4510Sstevel@tonic-gate 	l = (data.digest[ 2]<<16) | (data.digest[ 8]<<8) | data.digest[14];
4520Sstevel@tonic-gate 	to64(p, l, 4); p += 4;
4530Sstevel@tonic-gate 	l = (data.digest[ 3]<<16) | (data.digest[ 9]<<8) | data.digest[15];
4540Sstevel@tonic-gate 	to64(p, l, 4); p += 4;
4550Sstevel@tonic-gate 	l = (data.digest[ 4]<<16) | (data.digest[10]<<8) | data.digest[ 5];
4560Sstevel@tonic-gate 	to64(p, l, 4); p += 4;
4570Sstevel@tonic-gate 	l = data.digest[11]; to64(p, l, 2); p += 2;
4580Sstevel@tonic-gate 	*p = '\0';
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/* tidy up after ourselves */
4610Sstevel@tonic-gate 	bzero(&data, sizeof (data));
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	return (ctbuffer);
4640Sstevel@tonic-gate }
465