17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1dbc1fedSDan OpenSolaris Anderson * Common Development and Distribution License (the "License").
6*1dbc1fedSDan OpenSolaris Anderson * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*1dbc1fedSDan OpenSolaris Anderson * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <pwd.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <crypt.h>
377c478bd9Sstevel@tonic-gate #include <md5.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #define CRYPT_ALGNAME "md5"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /* minimum number of rounds we do, not including the per-user ones */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #define BASIC_ROUND_COUNT 4096 /* enough to make things interesting */
457c478bd9Sstevel@tonic-gate #define DIGEST_LEN 16
467c478bd9Sstevel@tonic-gate #define ROUND_BUFFER_LEN 64
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * Public domain quotation courtesy of Project Gutenberg.
507c478bd9Sstevel@tonic-gate * ftp://metalab.unc.edu/pub/docs/books/gutenberg/etext98/2ws2610.txt
517c478bd9Sstevel@tonic-gate * Hamlet III.ii - 1517 bytes, including trailing NUL
527c478bd9Sstevel@tonic-gate * ANSI-C string constant concatenation is a requirement here.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static const char constant_phrase[] =
567c478bd9Sstevel@tonic-gate "To be, or not to be,--that is the question:--\n"
577c478bd9Sstevel@tonic-gate "Whether 'tis nobler in the mind to suffer\n"
587c478bd9Sstevel@tonic-gate "The slings and arrows of outrageous fortune\n"
597c478bd9Sstevel@tonic-gate "Or to take arms against a sea of troubles,\n"
607c478bd9Sstevel@tonic-gate "And by opposing end them?--To die,--to sleep,--\n"
617c478bd9Sstevel@tonic-gate "No more; and by a sleep to say we end\n"
627c478bd9Sstevel@tonic-gate "The heartache, and the thousand natural shocks\n"
637c478bd9Sstevel@tonic-gate "That flesh is heir to,--'tis a consummation\n"
647c478bd9Sstevel@tonic-gate "Devoutly to be wish'd. To die,--to sleep;--\n"
657c478bd9Sstevel@tonic-gate "To sleep! perchance to dream:--ay, there's the rub;\n"
667c478bd9Sstevel@tonic-gate "For in that sleep of death what dreams may come,\n"
677c478bd9Sstevel@tonic-gate "When we have shuffled off this mortal coil,\n"
687c478bd9Sstevel@tonic-gate "Must give us pause: there's the respect\n"
697c478bd9Sstevel@tonic-gate "That makes calamity of so long life;\n"
707c478bd9Sstevel@tonic-gate "For who would bear the whips and scorns of time,\n"
717c478bd9Sstevel@tonic-gate "The oppressor's wrong, the proud man's contumely,\n"
727c478bd9Sstevel@tonic-gate "The pangs of despis'd love, the law's delay,\n"
737c478bd9Sstevel@tonic-gate "The insolence of office, and the spurns\n"
747c478bd9Sstevel@tonic-gate "That patient merit of the unworthy takes,\n"
757c478bd9Sstevel@tonic-gate "When he himself might his quietus make\n"
767c478bd9Sstevel@tonic-gate "With a bare bodkin? who would these fardels bear,\n"
777c478bd9Sstevel@tonic-gate "To grunt and sweat under a weary life,\n"
787c478bd9Sstevel@tonic-gate "But that the dread of something after death,--\n"
797c478bd9Sstevel@tonic-gate "The undiscover'd country, from whose bourn\n"
807c478bd9Sstevel@tonic-gate "No traveller returns,--puzzles the will,\n"
817c478bd9Sstevel@tonic-gate "And makes us rather bear those ills we have\n"
827c478bd9Sstevel@tonic-gate "Than fly to others that we know not of?\n"
837c478bd9Sstevel@tonic-gate "Thus conscience does make cowards of us all;\n"
847c478bd9Sstevel@tonic-gate "And thus the native hue of resolution\n"
857c478bd9Sstevel@tonic-gate "Is sicklied o'er with the pale cast of thought;\n"
867c478bd9Sstevel@tonic-gate "And enterprises of great pith and moment,\n"
877c478bd9Sstevel@tonic-gate "With this regard, their currents turn awry,\n"
887c478bd9Sstevel@tonic-gate "And lose the name of action.--Soft you now!\n"
897c478bd9Sstevel@tonic-gate "The fair Ophelia!--Nymph, in thy orisons\n"
907c478bd9Sstevel@tonic-gate "Be all my sins remember'd.\n";
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------ */
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate static int
md5bit(uint8_t * digest,int bit_num)957c478bd9Sstevel@tonic-gate md5bit(uint8_t *digest, int bit_num)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate int byte_off;
987c478bd9Sstevel@tonic-gate int bit_off;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate bit_num %= 128; /* keep this bounded for convenience */
1017c478bd9Sstevel@tonic-gate byte_off = bit_num / 8;
1027c478bd9Sstevel@tonic-gate bit_off = bit_num % 8;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /* return the value of bit N from the digest */
1057c478bd9Sstevel@tonic-gate return ((digest[byte_off] & (0x01 << bit_off)) ? 1 : 0);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate static uchar_t itoa64[] = /* 0 ... 63 => ascii - 64 */
1097c478bd9Sstevel@tonic-gate "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate static void
to64(char * s,uint64_t v,int n)1127c478bd9Sstevel@tonic-gate to64(char *s, uint64_t v, int n)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate while (--n >= 0) {
1157c478bd9Sstevel@tonic-gate *s++ = itoa64[v & 0x3f];
1167c478bd9Sstevel@tonic-gate v >>= 6;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate #define ROUNDS "rounds="
1217c478bd9Sstevel@tonic-gate #define ROUNDSLEN (sizeof (ROUNDS) - 1)
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * get the integer value after rounds= where ever it occurs in the string.
1257c478bd9Sstevel@tonic-gate * if the last char after the int is a , or $ that is fine anything else is an
1267c478bd9Sstevel@tonic-gate * error.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate static uint32_t
getrounds(const char * s)1297c478bd9Sstevel@tonic-gate getrounds(const char *s)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate char *r, *p, *e;
1327c478bd9Sstevel@tonic-gate long val;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if (s == NULL)
1357c478bd9Sstevel@tonic-gate return (0);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate if ((r = strstr(s, ROUNDS)) == NULL) {
1387c478bd9Sstevel@tonic-gate return (0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate if (strncmp(r, ROUNDS, ROUNDSLEN) != 0) {
1427c478bd9Sstevel@tonic-gate return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate p = r + ROUNDSLEN;
1467c478bd9Sstevel@tonic-gate errno = 0;
1477c478bd9Sstevel@tonic-gate val = strtol(p, &e, 10);
1487c478bd9Sstevel@tonic-gate /*
149*1dbc1fedSDan OpenSolaris Anderson * An error occurred or there is non-numeric stuff at the end
1507c478bd9Sstevel@tonic-gate * which isn't one of the crypt(3c) special chars ',' or '$'
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate if (errno != 0 || val < 0 ||
1537c478bd9Sstevel@tonic-gate !(*e == '\0' || *e == ',' || *e == '$')) {
1547c478bd9Sstevel@tonic-gate syslog(LOG_WARNING,
1557c478bd9Sstevel@tonic-gate "crypt_sunmd5: invalid rounds specification \"%s\"", s);
1567c478bd9Sstevel@tonic-gate return (0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate return ((uint32_t)val);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
162*1dbc1fedSDan OpenSolaris Anderson /* ARGSUSED3 */
1637c478bd9Sstevel@tonic-gate char *
crypt_gensalt_impl(char * gsbuffer,size_t gsbufflen,const char * oldsalt,const struct passwd * userinfo,const char ** params)1647c478bd9Sstevel@tonic-gate crypt_gensalt_impl(char *gsbuffer,
1657c478bd9Sstevel@tonic-gate size_t gsbufflen,
1667c478bd9Sstevel@tonic-gate const char *oldsalt,
1677c478bd9Sstevel@tonic-gate const struct passwd *userinfo,
1687c478bd9Sstevel@tonic-gate const char **params)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate uint32_t confrounds = 0;
1717c478bd9Sstevel@tonic-gate uint32_t saltrounds;
1727c478bd9Sstevel@tonic-gate int i;
1737c478bd9Sstevel@tonic-gate int fd;
1747c478bd9Sstevel@tonic-gate ssize_t got;
1757c478bd9Sstevel@tonic-gate uint64_t rndval;
1767c478bd9Sstevel@tonic-gate char rndstr[sizeof (rndval) + 1]; /* rndval as a base64 string */
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate for (i = 0; params != NULL && params[i] != NULL; i++) {
1797c478bd9Sstevel@tonic-gate if (strncmp(params[i], ROUNDS, ROUNDSLEN) == 0) {
1807c478bd9Sstevel@tonic-gate confrounds = getrounds(params[i]);
1817c478bd9Sstevel@tonic-gate } else {
1827c478bd9Sstevel@tonic-gate syslog(LOG_WARNING,
1837c478bd9Sstevel@tonic-gate "crypt_sunmd5: invalid parameter %s", params[i]);
1847c478bd9Sstevel@tonic-gate errno = EINVAL;
1857c478bd9Sstevel@tonic-gate return (NULL);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * If the config file has a higher value for rounds= than what
1917c478bd9Sstevel@tonic-gate * was in the old salt use that, otherwise keep what was in the
1927c478bd9Sstevel@tonic-gate * old salt.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate saltrounds = getrounds(oldsalt);
1957c478bd9Sstevel@tonic-gate if (confrounds > saltrounds) {
1967c478bd9Sstevel@tonic-gate saltrounds = confrounds;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate if ((fd = open("/dev/random", O_RDONLY)) == -1) {
2007c478bd9Sstevel@tonic-gate goto fail;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate got = read(fd, &rndval, sizeof (rndval));
2047c478bd9Sstevel@tonic-gate if (got < sizeof (rndval)) {
2057c478bd9Sstevel@tonic-gate int err = errno;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate (void) close(fd);
2087c478bd9Sstevel@tonic-gate errno = err;
2097c478bd9Sstevel@tonic-gate goto fail;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate (void) close(fd);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate to64((char *)&rndstr, rndval, sizeof (rndval));
2147c478bd9Sstevel@tonic-gate rndstr[sizeof (rndstr) - 1] = '\0';
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (saltrounds > 0) {
2177c478bd9Sstevel@tonic-gate if (snprintf(gsbuffer, gsbufflen,
2187c478bd9Sstevel@tonic-gate "$" CRYPT_ALGNAME "," ROUNDS "%d$",
2197c478bd9Sstevel@tonic-gate saltrounds) >= gsbufflen)
2207c478bd9Sstevel@tonic-gate goto fail;
2217c478bd9Sstevel@tonic-gate } else {
2227c478bd9Sstevel@tonic-gate if (snprintf(gsbuffer, gsbufflen,
2237c478bd9Sstevel@tonic-gate "$" CRYPT_ALGNAME "$") >= gsbufflen)
2247c478bd9Sstevel@tonic-gate goto fail;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (strlcat(gsbuffer, rndstr, gsbufflen) >= gsbufflen)
2287c478bd9Sstevel@tonic-gate goto fail;
2297c478bd9Sstevel@tonic-gate if (strlcat(gsbuffer, "$", gsbufflen) >= gsbufflen)
2307c478bd9Sstevel@tonic-gate goto fail;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate return (gsbuffer);
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate fail:
2357c478bd9Sstevel@tonic-gate bzero(gsbuffer, gsbufflen);
2367c478bd9Sstevel@tonic-gate return (NULL);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
2417c478bd9Sstevel@tonic-gate char *
crypt_genhash_impl(char * ctbuffer,size_t ctbufflen,const char * plaintext,const char * salt,const char ** params)2427c478bd9Sstevel@tonic-gate crypt_genhash_impl(char *ctbuffer,
2437c478bd9Sstevel@tonic-gate size_t ctbufflen,
2447c478bd9Sstevel@tonic-gate const char *plaintext,
2457c478bd9Sstevel@tonic-gate const char *salt,
2467c478bd9Sstevel@tonic-gate const char **params)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate int i;
2497c478bd9Sstevel@tonic-gate int round;
2507c478bd9Sstevel@tonic-gate int maxrounds = BASIC_ROUND_COUNT;
2517c478bd9Sstevel@tonic-gate uint32_t l;
2527c478bd9Sstevel@tonic-gate char *puresalt;
2537c478bd9Sstevel@tonic-gate char *saltend;
2547c478bd9Sstevel@tonic-gate char *p;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate /* put all the sensitive data in a struct */
2577c478bd9Sstevel@tonic-gate struct {
2587c478bd9Sstevel@tonic-gate MD5_CTX context; /* working buffer for MD5 algorithm */
2597c478bd9Sstevel@tonic-gate uint8_t digest[DIGEST_LEN]; /* where the MD5 digest is stored */
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate int indirect_4[16]; /* extracted array of 4bit values */
2627c478bd9Sstevel@tonic-gate int shift_4[16]; /* shift schedule, vals 0..4 */
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate int s7shift; /* shift for shift_7 creation, vals 0..7 */
2657c478bd9Sstevel@tonic-gate int indirect_7[16]; /* extracted array of 7bit values */
2667c478bd9Sstevel@tonic-gate int shift_7[16]; /* shift schedule, vals 0..1 */
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate int indirect_a; /* 7bit index into digest */
2697c478bd9Sstevel@tonic-gate int shift_a; /* shift schedule, vals 0..1 */
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate int indirect_b; /* 7bit index into digest */
2727c478bd9Sstevel@tonic-gate int shift_b; /* shift schedule, vals 0..1 */
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate int bit_a; /* single bit for cointoss */
2757c478bd9Sstevel@tonic-gate int bit_b; /* single bit for cointoss */
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate char roundascii[ROUND_BUFFER_LEN]; /* ascii rep of roundcount */
2787c478bd9Sstevel@tonic-gate } data;
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * Extract the puresalt (if it exists) from the existing salt string
2837c478bd9Sstevel@tonic-gate * $md5[,rounds=%d]$<puresalt>$<optional existing encoding>
2847c478bd9Sstevel@tonic-gate */
2857c478bd9Sstevel@tonic-gate saltend = strrchr(salt, '$');
2867c478bd9Sstevel@tonic-gate if (saltend == NULL || saltend == salt) {
2877c478bd9Sstevel@tonic-gate return (NULL);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate if (saltend[1] != '\0') {
2907c478bd9Sstevel@tonic-gate size_t len = saltend - salt + 1;
2917c478bd9Sstevel@tonic-gate if ((puresalt = malloc(len)) == NULL) {
2927c478bd9Sstevel@tonic-gate return (NULL);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate (void) strlcpy(puresalt, salt, len);
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate puresalt = strdup(salt);
2977c478bd9Sstevel@tonic-gate if (puresalt == NULL) {
2987c478bd9Sstevel@tonic-gate return (NULL);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate maxrounds += getrounds(salt);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate /* initialise the context */
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate MD5Init(&data.context);
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /* update with the (hopefully entropic) plaintext */
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate MD5Update(&data.context, (uchar_t *)plaintext, strlen(plaintext));
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /* update with the (publically known) salt */
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate MD5Update(&data.context, (uchar_t *)puresalt, strlen(puresalt));
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate /* compute the digest */
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate MD5Final(data.digest, &data.context);
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate * now to delay high-speed md5 implementations that have stuff
3237c478bd9Sstevel@tonic-gate * like code inlining, loops unrolled and table lookup
3247c478bd9Sstevel@tonic-gate */
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate for (round = 0; round < maxrounds; round++) {
3277c478bd9Sstevel@tonic-gate /* re-initialise the context */
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate MD5Init(&data.context);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate /* update with the previous digest */
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate MD5Update(&data.context, data.digest, sizeof (data.digest));
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate /* populate the shift schedules for use later */
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) {
3387c478bd9Sstevel@tonic-gate int j;
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /* offset 3 -> occasionally span more than 1 int32 fetch */
3417c478bd9Sstevel@tonic-gate j = (i + 3) % 16;
3427c478bd9Sstevel@tonic-gate data.s7shift = data.digest[i] % 8;
3437c478bd9Sstevel@tonic-gate data.shift_4[i] = data.digest[j] % 5;
3447c478bd9Sstevel@tonic-gate data.shift_7[i] = (data.digest[j] >> data.s7shift)
3457c478bd9Sstevel@tonic-gate & 0x01;
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate data.shift_a = md5bit(data.digest, round);
3497c478bd9Sstevel@tonic-gate data.shift_b = md5bit(data.digest, round + 64);
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /* populate indirect_4 with 4bit values extracted from digest */
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) {
3547c478bd9Sstevel@tonic-gate /* shift the digest byte and extract four bits */
3557c478bd9Sstevel@tonic-gate data.indirect_4[i] =
3567c478bd9Sstevel@tonic-gate (data.digest[i] >> data.shift_4[i]) & 0x0f;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate * populate indirect_7 with 7bit values from digest
3617c478bd9Sstevel@tonic-gate * indexed via indirect_4
3627c478bd9Sstevel@tonic-gate */
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) {
3657c478bd9Sstevel@tonic-gate /* shift the digest byte and extract seven bits */
3667c478bd9Sstevel@tonic-gate data.indirect_7[i] = (data.digest[data.indirect_4[i]]
3677c478bd9Sstevel@tonic-gate >> data.shift_7[i]) & 0x7f;
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate * use the 7bit values to indirect into digest,
3727c478bd9Sstevel@tonic-gate * and create two 8bit values from the results.
3737c478bd9Sstevel@tonic-gate */
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate data.indirect_a = data.indirect_b = 0;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) {
3787c478bd9Sstevel@tonic-gate data.indirect_a |= (md5bit(data.digest,
3797c478bd9Sstevel@tonic-gate data.indirect_7[i]) << i);
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate data.indirect_b |= (md5bit(data.digest,
3827c478bd9Sstevel@tonic-gate data.indirect_7[i + 8]) << i);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate /* shall we utilise the top or bottom 7 bits? */
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate data.indirect_a = (data.indirect_a >> data.shift_a) & 0x7f;
3897c478bd9Sstevel@tonic-gate data.indirect_b = (data.indirect_b >> data.shift_b) & 0x7f;
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /* extract two data.digest bits */
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate data.bit_a = md5bit(data.digest, data.indirect_a);
3957c478bd9Sstevel@tonic-gate data.bit_b = md5bit(data.digest, data.indirect_b);
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate #if ALGDEBUG
3997c478bd9Sstevel@tonic-gate for (i = 0; i < 15; i++) {
400*1dbc1fedSDan OpenSolaris Anderson (void) printf("%1x-", data.indirect_4[i]);
4017c478bd9Sstevel@tonic-gate }
402*1dbc1fedSDan OpenSolaris Anderson (void) printf("%1x ", data.indirect_4[15]);
4037c478bd9Sstevel@tonic-gate for (i = 0; i < 15; i++) {
404*1dbc1fedSDan OpenSolaris Anderson (void) printf("%02x-", data.indirect_7[i]);
4057c478bd9Sstevel@tonic-gate }
406*1dbc1fedSDan OpenSolaris Anderson (void) printf("%02x ", data.indirect_7[15]);
407*1dbc1fedSDan OpenSolaris Anderson (void) printf("%02x/%02x ", data.indirect_a, data.indirect_b);
408*1dbc1fedSDan OpenSolaris Anderson (void) printf("%d^%d\n", data.bit_a, data.bit_b);
4097c478bd9Sstevel@tonic-gate #endif
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate /* xor a coin-toss; if true, mix-in the constant phrase */
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate if (data.bit_a ^ data.bit_b) {
4157c478bd9Sstevel@tonic-gate MD5Update(&data.context,
4167c478bd9Sstevel@tonic-gate (unsigned char *) constant_phrase,
4177c478bd9Sstevel@tonic-gate sizeof (constant_phrase));
4187c478bd9Sstevel@tonic-gate #if ALGDEBUG
419*1dbc1fedSDan OpenSolaris Anderson (void) printf("mixing constant_phrase\n");
4207c478bd9Sstevel@tonic-gate #endif
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate /* digest a decimal sprintf of the current roundcount */
4257c478bd9Sstevel@tonic-gate
426*1dbc1fedSDan OpenSolaris Anderson (void) snprintf(data.roundascii, ROUND_BUFFER_LEN, "%d", round);
4277c478bd9Sstevel@tonic-gate MD5Update(&data.context,
4287c478bd9Sstevel@tonic-gate (unsigned char *) data.roundascii, strlen(data.roundascii));
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /* compute/flush the digest, and loop */
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate MD5Final(data.digest, &data.context);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate #if ALGDEBUG
4377c478bd9Sstevel@tonic-gate /* print the digest */
4387c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) {
439*1dbc1fedSDan OpenSolaris Anderson (void) printf("%02x", data.digest[i]);
4407c478bd9Sstevel@tonic-gate }
441*1dbc1fedSDan OpenSolaris Anderson (void) printf("\n");
4427c478bd9Sstevel@tonic-gate #endif
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate (void) snprintf(ctbuffer, ctbufflen, "%s$", puresalt);
4457c478bd9Sstevel@tonic-gate p = ctbuffer + strlen(ctbuffer);
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate l = (data.digest[ 0]<<16) | (data.digest[ 6]<<8) | data.digest[12];
4487c478bd9Sstevel@tonic-gate to64(p, l, 4); p += 4;
4497c478bd9Sstevel@tonic-gate l = (data.digest[ 1]<<16) | (data.digest[ 7]<<8) | data.digest[13];
4507c478bd9Sstevel@tonic-gate to64(p, l, 4); p += 4;
4517c478bd9Sstevel@tonic-gate l = (data.digest[ 2]<<16) | (data.digest[ 8]<<8) | data.digest[14];
4527c478bd9Sstevel@tonic-gate to64(p, l, 4); p += 4;
4537c478bd9Sstevel@tonic-gate l = (data.digest[ 3]<<16) | (data.digest[ 9]<<8) | data.digest[15];
4547c478bd9Sstevel@tonic-gate to64(p, l, 4); p += 4;
4557c478bd9Sstevel@tonic-gate l = (data.digest[ 4]<<16) | (data.digest[10]<<8) | data.digest[ 5];
4567c478bd9Sstevel@tonic-gate to64(p, l, 4); p += 4;
4577c478bd9Sstevel@tonic-gate l = data.digest[11]; to64(p, l, 2); p += 2;
4587c478bd9Sstevel@tonic-gate *p = '\0';
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /* tidy up after ourselves */
4617c478bd9Sstevel@tonic-gate bzero(&data, sizeof (data));
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate return (ctbuffer);
4647c478bd9Sstevel@tonic-gate }
465