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 /* 270Sstevel@tonic-gate * Portions of this code: 280Sstevel@tonic-gate * ---------------------------------------------------------------------------- 290Sstevel@tonic-gate * "THE BEER-WARE LICENSE" (Revision 42): 300Sstevel@tonic-gate * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you 310Sstevel@tonic-gate * can do whatever you want with this stuff. If we meet some day, and you think 320Sstevel@tonic-gate * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 330Sstevel@tonic-gate * ---------------------------------------------------------------------------- 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $ 360Sstevel@tonic-gate * 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <sys/stat.h> 410Sstevel@tonic-gate #include <fcntl.h> 420Sstevel@tonic-gate #include <unistd.h> 430Sstevel@tonic-gate #include <string.h> 440Sstevel@tonic-gate #include <strings.h> 450Sstevel@tonic-gate #include <stdio.h> 460Sstevel@tonic-gate #include <errno.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <md5.h> 490Sstevel@tonic-gate #include <crypt.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate static const char crypt_alg_magic[] = "$1$"; 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define SALT_LEN 8 540Sstevel@tonic-gate 550Sstevel@tonic-gate static uchar_t itoa64[] = /* 0 ... 63 => ascii - 64 */ 560Sstevel@tonic-gate "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 570Sstevel@tonic-gate 580Sstevel@tonic-gate static void 590Sstevel@tonic-gate to64(char *s, uint64_t v, int n) 600Sstevel@tonic-gate { 610Sstevel@tonic-gate while (--n >= 0) { 62*10935Sopensolaris@drydog.com *s++ = itoa64[v & 0x3f]; 630Sstevel@tonic-gate v >>= 6; 640Sstevel@tonic-gate } 650Sstevel@tonic-gate } 660Sstevel@tonic-gate 670Sstevel@tonic-gate 68*10935Sopensolaris@drydog.com /* ARGSUSED4 */ 690Sstevel@tonic-gate char * 700Sstevel@tonic-gate crypt_genhash_impl(char *ctbuffer, 710Sstevel@tonic-gate size_t ctbufflen, 720Sstevel@tonic-gate const char *plaintext, 730Sstevel@tonic-gate const char *switchsalt, 740Sstevel@tonic-gate const char **params) 750Sstevel@tonic-gate { 760Sstevel@tonic-gate char *p; 770Sstevel@tonic-gate int sl, l, pl, i; 780Sstevel@tonic-gate uchar_t *sp, *ep; 790Sstevel@tonic-gate uchar_t final[16]; /* XXX: 16 is some number from the orig source */ 800Sstevel@tonic-gate MD5_CTX ctx, ctx1; 810Sstevel@tonic-gate const int crypt_alg_magic_len = strlen(crypt_alg_magic); 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* Refine the salt */ 840Sstevel@tonic-gate sp = (uchar_t *)switchsalt; 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* skip our magic string */ 870Sstevel@tonic-gate if (strncmp((char *)sp, crypt_alg_magic, crypt_alg_magic_len) == 0) { 880Sstevel@tonic-gate sp += crypt_alg_magic_len; 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* Salt stops at the first $, max SALT_LEN chars */ 920Sstevel@tonic-gate for (ep = sp; *ep && *ep != '$' && ep < (sp + SALT_LEN); ep++) 930Sstevel@tonic-gate continue; 940Sstevel@tonic-gate 950Sstevel@tonic-gate sl = ep - sp; 960Sstevel@tonic-gate 970Sstevel@tonic-gate MD5Init(&ctx); 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* The password first, since that is what is most unknown */ 1000Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)plaintext, strlen(plaintext)); 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /* Then our magic string */ 1030Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)crypt_alg_magic, strlen(crypt_alg_magic)); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* Then the raw salt */ 1060Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)sp, sl); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* Then just as many characters of the MD5(plaintext,salt,plaintext) */ 1090Sstevel@tonic-gate MD5Init(&ctx1); 1100Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, strlen(plaintext)); 1110Sstevel@tonic-gate MD5Update(&ctx1, sp, sl); 1120Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, strlen(plaintext)); 1130Sstevel@tonic-gate MD5Final(final, &ctx1); 1140Sstevel@tonic-gate for (pl = strlen(plaintext); pl > 0; pl -= 16) 1150Sstevel@tonic-gate MD5Update(&ctx, final, pl > 16 ? 16 : pl); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* Don't leave anything around in vm they could use. */ 118*10935Sopensolaris@drydog.com (void) memset(final, 0, sizeof (final)); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* Then something really weird... */ 1210Sstevel@tonic-gate for (i = strlen(plaintext); i; i >>= 1) { 1220Sstevel@tonic-gate if (i & 1) { 1230Sstevel@tonic-gate MD5Update(&ctx, final, 1); 1240Sstevel@tonic-gate } else { 1250Sstevel@tonic-gate MD5Update(&ctx, (uchar_t *)plaintext, 1); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* Now make the output string */ 1300Sstevel@tonic-gate (void) strlcpy(ctbuffer, crypt_alg_magic, ctbufflen); 1310Sstevel@tonic-gate (void) strncat(ctbuffer, (const char *)sp, sl); 1320Sstevel@tonic-gate (void) strlcat(ctbuffer, "$", ctbufflen); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate MD5Final(final, &ctx); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * and now, just to make sure things don't run too fast 1380Sstevel@tonic-gate * On a 60 Mhz Pentium this takes 34 msec, so you would 1390Sstevel@tonic-gate * need 30 seconds to build a 1000 entry dictionary... 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate for (i = 0; i < 1000; i++) { 1420Sstevel@tonic-gate MD5Init(&ctx1); 1430Sstevel@tonic-gate if (i & 1) 1440Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, 1450Sstevel@tonic-gate strlen(plaintext)); 1460Sstevel@tonic-gate else 1470Sstevel@tonic-gate MD5Update(&ctx1, final, 16); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate if (i % 3) 1500Sstevel@tonic-gate MD5Update(&ctx1, sp, sl); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate if (i % 7) 1530Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, 1540Sstevel@tonic-gate strlen(plaintext)); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate if (i & 1) 1570Sstevel@tonic-gate MD5Update(&ctx1, final, 16); 1580Sstevel@tonic-gate else 1590Sstevel@tonic-gate MD5Update(&ctx1, (uchar_t *)plaintext, 1600Sstevel@tonic-gate strlen(plaintext)); 1610Sstevel@tonic-gate MD5Final(final, &ctx1); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate p = ctbuffer + strlen(ctbuffer); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p, l, 4); p += 4; 1670Sstevel@tonic-gate l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p, l, 4); p += 4; 1680Sstevel@tonic-gate l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p, l, 4); p += 4; 1690Sstevel@tonic-gate l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p, l, 4); p += 4; 1700Sstevel@tonic-gate l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p, l, 4); p += 4; 1710Sstevel@tonic-gate l = final[11]; to64(p, l, 2); p += 2; 1720Sstevel@tonic-gate *p = '\0'; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* Don't leave anything around in vm they could use. */ 175*10935Sopensolaris@drydog.com (void) memset(final, 0, sizeof (final)); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate return (ctbuffer); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate 181*10935Sopensolaris@drydog.com /* ARGSUSED2 */ 1820Sstevel@tonic-gate char * 1830Sstevel@tonic-gate crypt_gensalt_impl(char *gsbuffer, 1840Sstevel@tonic-gate size_t gsbufflen, 1850Sstevel@tonic-gate const char *oldsalt, 1860Sstevel@tonic-gate const struct passwd *userinfo, 1870Sstevel@tonic-gate const char **params) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate int fd; 1900Sstevel@tonic-gate int err; 1910Sstevel@tonic-gate ssize_t got; 1920Sstevel@tonic-gate uint64_t rndval; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if ((fd = open("/dev/urandom", O_RDONLY)) == -1) { 1950Sstevel@tonic-gate return (NULL); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate (void) strlcpy(gsbuffer, crypt_alg_magic, gsbufflen); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate got = read(fd, &rndval, sizeof (rndval)); 2010Sstevel@tonic-gate if (got < sizeof (rndval)) { 2020Sstevel@tonic-gate err = errno; 2030Sstevel@tonic-gate (void) close(fd); 2040Sstevel@tonic-gate errno = err; 2050Sstevel@tonic-gate return (NULL); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate to64(&gsbuffer[strlen(crypt_alg_magic)], rndval, sizeof (rndval)); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate (void) close(fd); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate return (gsbuffer); 2120Sstevel@tonic-gate } 213