1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tom Truscott. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if defined(LIBC_SCCS) && !defined(lint) 38 /* from static char sccsid[] = "@(#)crypt.c 5.11 (Berkeley) 6/25/91"; */ 39 static char rcsid[] = "$Header: /a/cvs/386BSD/src/lib/libc/gen/crypt.c,v 1.6 1993/08/29 22:03:56 nate Exp $"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 #include <unistd.h> 43 #include <stdio.h> 44 45 /* 46 * UNIX password 47 * 48 * This is just a scrambler which can be used in the case when the 49 * real libcrypt is not around. 50 * 51 * Developed by Nate Williams 52 */ 53 54 char * 55 crypt(pw, salt) 56 register const char *pw; 57 register const char *salt; 58 { 59 static char password[14]; 60 long matrix[128], *m, vector[2]; 61 char a, b, *p; 62 int i, value; 63 unsigned short crc; 64 unsigned long t; 65 66 /* Ugly hack, but I'm too lazy to find the real problem - NW */ 67 bzero(matrix, 128 * sizeof(long)); 68 69 if (salt[0]) { 70 a = salt[0]; 71 if (salt[1]) 72 b = salt[1]; 73 else 74 b = a; 75 } else 76 a = b = '0'; 77 password[0] = a; 78 password[1] = b; 79 if (a > 'Z') 80 a -= 6; 81 if (a > '9') 82 a -= 7; 83 if (b > 'Z') 84 b -= 6; 85 if (b > '9') 86 b -= 7; 87 a -= '.'; 88 b -= '.'; 89 value = (a | (b << 6)) & 07777; 90 91 crc = value; 92 value += 1000; 93 b = 0; 94 p = (char *)pw; 95 while (value--) { 96 if (crc & 0x8000) 97 crc = (crc << 1) ^ 0x1021; 98 else 99 crc <<= 1; 100 if (!b) { 101 b = 8; 102 if (!(i = *p++)) { 103 p = (char *)pw; 104 i = *p++; 105 } 106 } 107 if (i & 0x80) 108 crc ^= 1; 109 i <<= 1; 110 b--; 111 } 112 113 m = matrix; 114 matrix[0] = 0; 115 a = 32; 116 for (value = 07777; value >= 0; value--) { 117 *m <<= 1; 118 if (crc & 0x8000) { 119 *m |= 1; 120 crc = (crc << 1) ^ 0x1021; 121 } else 122 crc <<= 1; 123 if (!b) { 124 b = 8; 125 if (!(i = *p++)) { 126 p = (char *)pw; 127 i = *p++; 128 } 129 } 130 if (i & 0x80) 131 crc ^= 1; 132 i <<= 1; 133 b--; 134 if (!(a--)) { 135 a = 32; 136 *++m = 0; 137 } 138 } 139 140 vector[0] = 0; 141 vector[1] = 0; 142 p = (char *) vector; 143 for (i = 0; i < 7; i++) 144 if (pw[i]) 145 *p++ = pw[i]; 146 else 147 break; 148 149 p = password + 2; 150 a = 6; 151 m = matrix; 152 *p = 0; 153 for (i = 077; i >= 0; i--) { 154 t = *m++; 155 t = t ^ *m++; 156 t = t ^ vector[0]; 157 t = t ^ vector[1]; 158 b = 0; 159 while (t) { 160 if (t & 1) 161 b = 1 - b; 162 t >>= 1; 163 } 164 a--; 165 if (b) 166 *p |= 1 << a; 167 if (!a) { 168 a = 6; 169 *++p = 0; 170 } 171 } 172 173 for (i = 2; i < 13; i++) { 174 password[i] += '.'; 175 if (password[i] > '9') 176 password[i] += 7; 177 if (password[i] > 'Z') 178 password[i] += 6; 179 } 180 password[13] = 0; 181 182 return password; 183 } 184 185