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 /*
277c478bd9Sstevel@tonic-gate * Portions of this code:
287c478bd9Sstevel@tonic-gate * ----------------------------------------------------------------------------
297c478bd9Sstevel@tonic-gate * "THE BEER-WARE LICENSE" (Revision 42):
307c478bd9Sstevel@tonic-gate * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
317c478bd9Sstevel@tonic-gate * can do whatever you want with this stuff. If we meet some day, and you think
327c478bd9Sstevel@tonic-gate * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
337c478bd9Sstevel@tonic-gate * ----------------------------------------------------------------------------
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include <md5.h>
497c478bd9Sstevel@tonic-gate #include <crypt.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static const char crypt_alg_magic[] = "$1$";
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define SALT_LEN 8
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static uchar_t itoa64[] = /* 0 ... 63 => ascii - 64 */
567c478bd9Sstevel@tonic-gate "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static void
to64(char * s,uint64_t v,int n)597c478bd9Sstevel@tonic-gate to64(char *s, uint64_t v, int n)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate while (--n >= 0) {
62*1dbc1fedSDan OpenSolaris Anderson *s++ = itoa64[v & 0x3f];
637c478bd9Sstevel@tonic-gate v >>= 6;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate
68*1dbc1fedSDan OpenSolaris Anderson /* ARGSUSED4 */
697c478bd9Sstevel@tonic-gate char *
crypt_genhash_impl(char * ctbuffer,size_t ctbufflen,const char * plaintext,const char * switchsalt,const char ** params)707c478bd9Sstevel@tonic-gate crypt_genhash_impl(char *ctbuffer,
717c478bd9Sstevel@tonic-gate size_t ctbufflen,
727c478bd9Sstevel@tonic-gate const char *plaintext,
737c478bd9Sstevel@tonic-gate const char *switchsalt,
747c478bd9Sstevel@tonic-gate const char **params)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate char *p;
777c478bd9Sstevel@tonic-gate int sl, l, pl, i;
787c478bd9Sstevel@tonic-gate uchar_t *sp, *ep;
797c478bd9Sstevel@tonic-gate uchar_t final[16]; /* XXX: 16 is some number from the orig source */
807c478bd9Sstevel@tonic-gate MD5_CTX ctx, ctx1;
817c478bd9Sstevel@tonic-gate const int crypt_alg_magic_len = strlen(crypt_alg_magic);
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /* Refine the salt */
847c478bd9Sstevel@tonic-gate sp = (uchar_t *)switchsalt;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /* skip our magic string */
877c478bd9Sstevel@tonic-gate if (strncmp((char *)sp, crypt_alg_magic, crypt_alg_magic_len) == 0) {
887c478bd9Sstevel@tonic-gate sp += crypt_alg_magic_len;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* Salt stops at the first $, max SALT_LEN chars */
927c478bd9Sstevel@tonic-gate for (ep = sp; *ep && *ep != '$' && ep < (sp + SALT_LEN); ep++)
937c478bd9Sstevel@tonic-gate continue;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate sl = ep - sp;
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate MD5Init(&ctx);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /* The password first, since that is what is most unknown */
1007c478bd9Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)plaintext, strlen(plaintext));
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /* Then our magic string */
1037c478bd9Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)crypt_alg_magic, strlen(crypt_alg_magic));
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /* Then the raw salt */
1067c478bd9Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)sp, sl);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /* Then just as many characters of the MD5(plaintext,salt,plaintext) */
1097c478bd9Sstevel@tonic-gate MD5Init(&ctx1);
1107c478bd9Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, strlen(plaintext));
1117c478bd9Sstevel@tonic-gate MD5Update(&ctx1, sp, sl);
1127c478bd9Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, strlen(plaintext));
1137c478bd9Sstevel@tonic-gate MD5Final(final, &ctx1);
1147c478bd9Sstevel@tonic-gate for (pl = strlen(plaintext); pl > 0; pl -= 16)
1157c478bd9Sstevel@tonic-gate MD5Update(&ctx, final, pl > 16 ? 16 : pl);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /* Don't leave anything around in vm they could use. */
118*1dbc1fedSDan OpenSolaris Anderson (void) memset(final, 0, sizeof (final));
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /* Then something really weird... */
1217c478bd9Sstevel@tonic-gate for (i = strlen(plaintext); i; i >>= 1) {
1227c478bd9Sstevel@tonic-gate if (i & 1) {
1237c478bd9Sstevel@tonic-gate MD5Update(&ctx, final, 1);
1247c478bd9Sstevel@tonic-gate } else {
1257c478bd9Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)plaintext, 1);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /* Now make the output string */
1307c478bd9Sstevel@tonic-gate (void) strlcpy(ctbuffer, crypt_alg_magic, ctbufflen);
1317c478bd9Sstevel@tonic-gate (void) strncat(ctbuffer, (const char *)sp, sl);
1327c478bd9Sstevel@tonic-gate (void) strlcat(ctbuffer, "$", ctbufflen);
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate MD5Final(final, &ctx);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * and now, just to make sure things don't run too fast
1387c478bd9Sstevel@tonic-gate * On a 60 Mhz Pentium this takes 34 msec, so you would
1397c478bd9Sstevel@tonic-gate * need 30 seconds to build a 1000 entry dictionary...
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate for (i = 0; i < 1000; i++) {
1427c478bd9Sstevel@tonic-gate MD5Init(&ctx1);
1437c478bd9Sstevel@tonic-gate if (i & 1)
1447c478bd9Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext,
1457c478bd9Sstevel@tonic-gate strlen(plaintext));
1467c478bd9Sstevel@tonic-gate else
1477c478bd9Sstevel@tonic-gate MD5Update(&ctx1, final, 16);
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate if (i % 3)
1507c478bd9Sstevel@tonic-gate MD5Update(&ctx1, sp, sl);
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (i % 7)
1537c478bd9Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext,
1547c478bd9Sstevel@tonic-gate strlen(plaintext));
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (i & 1)
1577c478bd9Sstevel@tonic-gate MD5Update(&ctx1, final, 16);
1587c478bd9Sstevel@tonic-gate else
1597c478bd9Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext,
1607c478bd9Sstevel@tonic-gate strlen(plaintext));
1617c478bd9Sstevel@tonic-gate MD5Final(final, &ctx1);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate p = ctbuffer + strlen(ctbuffer);
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p, l, 4); p += 4;
1677c478bd9Sstevel@tonic-gate l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p, l, 4); p += 4;
1687c478bd9Sstevel@tonic-gate l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p, l, 4); p += 4;
1697c478bd9Sstevel@tonic-gate l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p, l, 4); p += 4;
1707c478bd9Sstevel@tonic-gate l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p, l, 4); p += 4;
1717c478bd9Sstevel@tonic-gate l = final[11]; to64(p, l, 2); p += 2;
1727c478bd9Sstevel@tonic-gate *p = '\0';
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* Don't leave anything around in vm they could use. */
175*1dbc1fedSDan OpenSolaris Anderson (void) memset(final, 0, sizeof (final));
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate return (ctbuffer);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate
181*1dbc1fedSDan OpenSolaris Anderson /* ARGSUSED2 */
1827c478bd9Sstevel@tonic-gate char *
crypt_gensalt_impl(char * gsbuffer,size_t gsbufflen,const char * oldsalt,const struct passwd * userinfo,const char ** params)1837c478bd9Sstevel@tonic-gate crypt_gensalt_impl(char *gsbuffer,
1847c478bd9Sstevel@tonic-gate size_t gsbufflen,
1857c478bd9Sstevel@tonic-gate const char *oldsalt,
1867c478bd9Sstevel@tonic-gate const struct passwd *userinfo,
1877c478bd9Sstevel@tonic-gate const char **params)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate int fd;
1907c478bd9Sstevel@tonic-gate int err;
1917c478bd9Sstevel@tonic-gate ssize_t got;
1927c478bd9Sstevel@tonic-gate uint64_t rndval;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
1957c478bd9Sstevel@tonic-gate return (NULL);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate (void) strlcpy(gsbuffer, crypt_alg_magic, gsbufflen);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate got = read(fd, &rndval, sizeof (rndval));
2017c478bd9Sstevel@tonic-gate if (got < sizeof (rndval)) {
2027c478bd9Sstevel@tonic-gate err = errno;
2037c478bd9Sstevel@tonic-gate (void) close(fd);
2047c478bd9Sstevel@tonic-gate errno = err;
2057c478bd9Sstevel@tonic-gate return (NULL);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate to64(&gsbuffer[strlen(crypt_alg_magic)], rndval, sizeof (rndval));
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate (void) close(fd);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate return (gsbuffer);
2127c478bd9Sstevel@tonic-gate }
213