xref: /freebsd/lib/libcrypt/crypt.c (revision 0e27b8d587f5e89fe4f0920e8efb06f4f6eba863)
10e27b8d5SGeoff Rehmet /*
20e27b8d5SGeoff Rehmet  * Copyright (c) 1989 The Regents of the University of California.
30e27b8d5SGeoff Rehmet  * All rights reserved.
40e27b8d5SGeoff Rehmet  *
50e27b8d5SGeoff Rehmet  * This code is derived from software contributed to Berkeley by
60e27b8d5SGeoff Rehmet  * Tom Truscott.
70e27b8d5SGeoff Rehmet  *
80e27b8d5SGeoff Rehmet  * Redistribution and use in source and binary forms, with or without
90e27b8d5SGeoff Rehmet  * modification, are permitted provided that the following conditions
100e27b8d5SGeoff Rehmet  * are met:
110e27b8d5SGeoff Rehmet  * 1. Redistributions of source code must retain the above copyright
120e27b8d5SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer.
130e27b8d5SGeoff Rehmet  * 2. Redistributions in binary form must reproduce the above copyright
140e27b8d5SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer in the
150e27b8d5SGeoff Rehmet  *    documentation and/or other materials provided with the distribution.
160e27b8d5SGeoff Rehmet  * 3. All advertising materials mentioning features or use of this software
170e27b8d5SGeoff Rehmet  *    must display the following acknowledgement:
180e27b8d5SGeoff Rehmet  *	This product includes software developed by the University of
190e27b8d5SGeoff Rehmet  *	California, Berkeley and its contributors.
200e27b8d5SGeoff Rehmet  * 4. Neither the name of the University nor the names of its contributors
210e27b8d5SGeoff Rehmet  *    may be used to endorse or promote products derived from this software
220e27b8d5SGeoff Rehmet  *    without specific prior written permission.
230e27b8d5SGeoff Rehmet  *
240e27b8d5SGeoff Rehmet  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
250e27b8d5SGeoff Rehmet  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
260e27b8d5SGeoff Rehmet  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
270e27b8d5SGeoff Rehmet  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
280e27b8d5SGeoff Rehmet  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
290e27b8d5SGeoff Rehmet  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
300e27b8d5SGeoff Rehmet  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
310e27b8d5SGeoff Rehmet  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
320e27b8d5SGeoff Rehmet  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
330e27b8d5SGeoff Rehmet  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
340e27b8d5SGeoff Rehmet  * SUCH DAMAGE.
350e27b8d5SGeoff Rehmet  */
360e27b8d5SGeoff Rehmet 
370e27b8d5SGeoff Rehmet #if defined(LIBC_SCCS) && !defined(lint)
380e27b8d5SGeoff Rehmet /* from static char sccsid[] = "@(#)crypt.c	5.11 (Berkeley) 6/25/91"; */
390e27b8d5SGeoff Rehmet static char rcsid[] = "$Header: /a/cvs/386BSD/src/lib/libc/gen/crypt.c,v 1.6 1993/08/29 22:03:56 nate Exp $";
400e27b8d5SGeoff Rehmet #endif /* LIBC_SCCS and not lint */
410e27b8d5SGeoff Rehmet 
420e27b8d5SGeoff Rehmet #include <unistd.h>
430e27b8d5SGeoff Rehmet #include <stdio.h>
440e27b8d5SGeoff Rehmet 
450e27b8d5SGeoff Rehmet /*
460e27b8d5SGeoff Rehmet  * UNIX password
470e27b8d5SGeoff Rehmet  *
480e27b8d5SGeoff Rehmet  * This is just a scrambler which can be used in the case when the
490e27b8d5SGeoff Rehmet  * real libcrypt is not around.
500e27b8d5SGeoff Rehmet  *
510e27b8d5SGeoff Rehmet  * Developed by Nate Williams
520e27b8d5SGeoff Rehmet  */
530e27b8d5SGeoff Rehmet 
540e27b8d5SGeoff Rehmet char *
550e27b8d5SGeoff Rehmet crypt(pw, salt)
560e27b8d5SGeoff Rehmet 	register const char *pw;
570e27b8d5SGeoff Rehmet 	register const char *salt;
580e27b8d5SGeoff Rehmet {
590e27b8d5SGeoff Rehmet 	static char     password[14];
600e27b8d5SGeoff Rehmet 	long            matrix[128], *m, vector[2];
610e27b8d5SGeoff Rehmet 	char            a, b, *p;
620e27b8d5SGeoff Rehmet 	int             i, value;
630e27b8d5SGeoff Rehmet 	unsigned short  crc;
640e27b8d5SGeoff Rehmet 	unsigned long   t;
650e27b8d5SGeoff Rehmet 
660e27b8d5SGeoff Rehmet 	/* Ugly hack, but I'm too lazy to find the real problem - NW */
670e27b8d5SGeoff Rehmet 	bzero(matrix, 128 * sizeof(long));
680e27b8d5SGeoff Rehmet 
690e27b8d5SGeoff Rehmet 	if (salt[0]) {
700e27b8d5SGeoff Rehmet 		a = salt[0];
710e27b8d5SGeoff Rehmet 		if (salt[1])
720e27b8d5SGeoff Rehmet 			b = salt[1];
730e27b8d5SGeoff Rehmet 		else
740e27b8d5SGeoff Rehmet 			b = a;
750e27b8d5SGeoff Rehmet 	} else
760e27b8d5SGeoff Rehmet 		a = b = '0';
770e27b8d5SGeoff Rehmet 	password[0] = a;
780e27b8d5SGeoff Rehmet 	password[1] = b;
790e27b8d5SGeoff Rehmet 	if (a > 'Z')
800e27b8d5SGeoff Rehmet 		a -= 6;
810e27b8d5SGeoff Rehmet 	if (a > '9')
820e27b8d5SGeoff Rehmet 		a -= 7;
830e27b8d5SGeoff Rehmet 	if (b > 'Z')
840e27b8d5SGeoff Rehmet 		b -= 6;
850e27b8d5SGeoff Rehmet 	if (b > '9')
860e27b8d5SGeoff Rehmet 		b -= 7;
870e27b8d5SGeoff Rehmet 	a -= '.';
880e27b8d5SGeoff Rehmet 	b -= '.';
890e27b8d5SGeoff Rehmet 	value = (a | (b << 6)) & 07777;
900e27b8d5SGeoff Rehmet 
910e27b8d5SGeoff Rehmet 	crc = value;
920e27b8d5SGeoff Rehmet 	value += 1000;
930e27b8d5SGeoff Rehmet 	b = 0;
940e27b8d5SGeoff Rehmet 	p = (char *)pw;
950e27b8d5SGeoff Rehmet 	while (value--) {
960e27b8d5SGeoff Rehmet 		if (crc & 0x8000)
970e27b8d5SGeoff Rehmet 			crc = (crc << 1) ^ 0x1021;
980e27b8d5SGeoff Rehmet 		else
990e27b8d5SGeoff Rehmet 			crc <<= 1;
1000e27b8d5SGeoff Rehmet 		if (!b) {
1010e27b8d5SGeoff Rehmet 			b = 8;
1020e27b8d5SGeoff Rehmet 			if (!(i = *p++)) {
1030e27b8d5SGeoff Rehmet 				p = (char *)pw;
1040e27b8d5SGeoff Rehmet 				i = *p++;
1050e27b8d5SGeoff Rehmet 			}
1060e27b8d5SGeoff Rehmet 		}
1070e27b8d5SGeoff Rehmet 		if (i & 0x80)
1080e27b8d5SGeoff Rehmet 			crc ^= 1;
1090e27b8d5SGeoff Rehmet 		i <<= 1;
1100e27b8d5SGeoff Rehmet 		b--;
1110e27b8d5SGeoff Rehmet 	}
1120e27b8d5SGeoff Rehmet 
1130e27b8d5SGeoff Rehmet 	m = matrix;
1140e27b8d5SGeoff Rehmet 	matrix[0] = 0;
1150e27b8d5SGeoff Rehmet 	a = 32;
1160e27b8d5SGeoff Rehmet 	for (value = 07777; value >= 0; value--) {
1170e27b8d5SGeoff Rehmet 		*m <<= 1;
1180e27b8d5SGeoff Rehmet 		if (crc & 0x8000) {
1190e27b8d5SGeoff Rehmet 			*m |= 1;
1200e27b8d5SGeoff Rehmet 			crc = (crc << 1) ^ 0x1021;
1210e27b8d5SGeoff Rehmet 		} else
1220e27b8d5SGeoff Rehmet 			crc <<= 1;
1230e27b8d5SGeoff Rehmet 		if (!b) {
1240e27b8d5SGeoff Rehmet 			b = 8;
1250e27b8d5SGeoff Rehmet 			if (!(i = *p++)) {
1260e27b8d5SGeoff Rehmet 				p = (char *)pw;
1270e27b8d5SGeoff Rehmet 				i = *p++;
1280e27b8d5SGeoff Rehmet 			}
1290e27b8d5SGeoff Rehmet 		}
1300e27b8d5SGeoff Rehmet 		if (i & 0x80)
1310e27b8d5SGeoff Rehmet 			crc ^= 1;
1320e27b8d5SGeoff Rehmet 		i <<= 1;
1330e27b8d5SGeoff Rehmet 		b--;
1340e27b8d5SGeoff Rehmet 		if (!(a--)) {
1350e27b8d5SGeoff Rehmet 			a = 32;
1360e27b8d5SGeoff Rehmet 			*++m = 0;
1370e27b8d5SGeoff Rehmet 		}
1380e27b8d5SGeoff Rehmet 	}
1390e27b8d5SGeoff Rehmet 
1400e27b8d5SGeoff Rehmet 	vector[0] = 0;
1410e27b8d5SGeoff Rehmet 	vector[1] = 0;
1420e27b8d5SGeoff Rehmet 	p = (char *) vector;
1430e27b8d5SGeoff Rehmet 	for (i = 0; i < 7; i++)
1440e27b8d5SGeoff Rehmet 		if (pw[i])
1450e27b8d5SGeoff Rehmet 			*p++ = pw[i];
1460e27b8d5SGeoff Rehmet 		else
1470e27b8d5SGeoff Rehmet 			break;
1480e27b8d5SGeoff Rehmet 
1490e27b8d5SGeoff Rehmet 	p = password + 2;
1500e27b8d5SGeoff Rehmet 	a = 6;
1510e27b8d5SGeoff Rehmet 	m = matrix;
1520e27b8d5SGeoff Rehmet 	*p = 0;
1530e27b8d5SGeoff Rehmet 	for (i = 077; i >= 0; i--) {
1540e27b8d5SGeoff Rehmet 		t = *m++;
1550e27b8d5SGeoff Rehmet 		t = t ^ *m++;
1560e27b8d5SGeoff Rehmet 		t = t ^ vector[0];
1570e27b8d5SGeoff Rehmet 		t = t ^ vector[1];
1580e27b8d5SGeoff Rehmet 		b = 0;
1590e27b8d5SGeoff Rehmet 		while (t) {
1600e27b8d5SGeoff Rehmet 			if (t & 1)
1610e27b8d5SGeoff Rehmet 				b = 1 - b;
1620e27b8d5SGeoff Rehmet 			t >>= 1;
1630e27b8d5SGeoff Rehmet 		}
1640e27b8d5SGeoff Rehmet 		a--;
1650e27b8d5SGeoff Rehmet 		if (b)
1660e27b8d5SGeoff Rehmet 			*p |= 1 << a;
1670e27b8d5SGeoff Rehmet 		if (!a) {
1680e27b8d5SGeoff Rehmet 			a = 6;
1690e27b8d5SGeoff Rehmet 			*++p = 0;
1700e27b8d5SGeoff Rehmet 		}
1710e27b8d5SGeoff Rehmet 	}
1720e27b8d5SGeoff Rehmet 
1730e27b8d5SGeoff Rehmet 	for (i = 2; i < 13; i++) {
1740e27b8d5SGeoff Rehmet 		password[i] += '.';
1750e27b8d5SGeoff Rehmet 		if (password[i] > '9')
1760e27b8d5SGeoff Rehmet 			password[i] += 7;
1770e27b8d5SGeoff Rehmet 		if (password[i] > 'Z')
1780e27b8d5SGeoff Rehmet 			password[i] += 6;
1790e27b8d5SGeoff Rehmet 	}
1800e27b8d5SGeoff Rehmet 	password[13] = 0;
1810e27b8d5SGeoff Rehmet 
1820e27b8d5SGeoff Rehmet 	return password;
1830e27b8d5SGeoff Rehmet }
1840e27b8d5SGeoff Rehmet 
185