fts(3) updates
[unix-history] / lib / libc / gen / crypt.c
CommitLineData
6131a5db
RM
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)
6ea1178a 38/* from static char sccsid[] = "@(#)crypt.c 5.11 (Berkeley) 6/25/91"; */
50acf9bf 39static char rcsid[] = "$Header: /a/cvs/386BSD/src/lib/libc/gen/crypt.c,v 1.3 1993/07/21 17:42:51 nate Exp $";
6131a5db
RM
40#endif /* LIBC_SCCS and not lint */
41
42#include <unistd.h>
6ea1178a 43#include <stdio.h>
6131a5db
RM
44
45/*
46 * UNIX password, and DES, encryption.
6ea1178a
NW
47 *
48 * since this is non-exportable, this is just a dummy. if you want real
49 * encryption, make sure you've got libcrypt.a around.
6131a5db
RM
50 */
51
6ea1178a
NW
52#ifndef DES
53#define SCRAMBLE /* Don't leave them in plaintext */
6131a5db
RM
54#endif
55
6ea1178a
NW
56#ifndef SCRAMBLE
57static char cryptresult[1+4+4+11+1]; /* "encrypted" result */
6131a5db 58
6131a5db
RM
59char *
60crypt(key, setting)
61 register const char *key;
62 register const char *setting;
63{
6ea1178a
NW
64 fprintf(stderr, "WARNING! crypt(3) not present in the system!\n");
65 strncpy(cryptresult, key, sizeof cryptresult);
66 cryptresult[sizeof cryptresult - 1] = '\0';
67 return (cryptresult);
68}
6131a5db 69
6ea1178a 70#else
6131a5db 71
6ea1178a
NW
72char *
73crypt(pw, salt)
bd2147f6
NW
74 register const char *pw;
75 register const char *salt;
6ea1178a
NW
76{
77 static char password[14];
78 long matrix[128], *m, vector[2];
79 char a, b, *p;
80 int i, value;
81 unsigned short crc;
82 unsigned long t;
83
84 if (salt[0]) {
85 a = salt[0];
86 if (salt[1])
87 b = salt[1];
88 else
89 b = a;
90 } else
91 a = b = '0';
92 password[0] = a;
93 password[1] = b;
94 if (a > 'Z')
95 a -= 6;
96 if (a > '9')
97 a -= 7;
98 if (b > 'Z')
99 b -= 6;
100 if (b > '9')
101 b -= 7;
102 a -= '.';
103 b -= '.';
104 value = (a | (b << 6)) & 07777;
105
106 crc = value;
107 value += 1000;
108 b = 0;
50acf9bf 109 p = (char *)pw;
6ea1178a
NW
110 while (value--) {
111 if (crc & 0x8000)
112 crc = (crc << 1) ^ 0x1021;
113 else
114 crc <<= 1;
115 if (!b) {
116 b = 8;
117 if (!(i = *p++)) {
50acf9bf 118 p = (char *)pw;
6ea1178a 119 i = *p++;
6131a5db 120 }
6131a5db 121 }
6ea1178a
NW
122 if (i & 0x80)
123 crc ^= 1;
124 i <<= 1;
125 b--;
126 }
127
128 m = matrix;
129 matrix[0] = 0;
130 a = 32;
131 for (value = 07777; value >= 0; value--) {
132 *m <<= 1;
133 if (crc & 0x8000) {
134 *m |= 1;
135 crc = (crc << 1) ^ 0x1021;
136 } else
137 crc <<= 1;
138 if (!b) {
139 b = 8;
140 if (!(i = *p++)) {
50acf9bf 141 p = (char *)pw;
6ea1178a
NW
142 i = *p++;
143 }
144 }
145 if (i & 0x80)
146 crc ^= 1;
147 i <<= 1;
148 b--;
149 if (!(a--)) {
150 a = 32;
151 *++m = 0;
6131a5db 152 }
6131a5db
RM
153 }
154
6ea1178a
NW
155 vector[0] = 0;
156 vector[1] = 0;
157 p = (char *) vector;
158 for (i = 0; i < 7; i++)
159 if (pw[i])
160 *p++ = pw[i];
161 else
162 break;
163
164 p = password + 2;
165 a = 6;
166 m = matrix;
167 *p = 0;
168 for (i = 077; i >= 0; i--) {
169 t = *m++ ^ *m++ ^ vector[0] ^ vector[1];
170 b = 0;
171 while (t) {
172 if (t & 1)
173 b ^= 1;
174 t >>= 1;
175 }
176 a--;
177 if (b)
178 *p |= 1 << a;
179 if (!a) {
180 a = 6;
181 *++p = 0;
182 }
6131a5db 183 }
6131a5db 184
6ea1178a
NW
185 for (i = 2; i < 13; i++) {
186 password[i] += '.';
187 if (password[i] > '9')
188 password[i] += 7;
189 if (password[i] > 'Z')
190 password[i] += 6;
191 }
192 password[13] = 0;
6131a5db 193
6ea1178a 194 return password;
6131a5db 195}
6ea1178a 196#endif
6131a5db 197
6131a5db
RM
198des_setkey(key)
199 register const char *key;
200{
6ea1178a 201 fprintf(stderr, "WARNING! des_setkey(3) not present in the system!\n");
6131a5db
RM
202 return (0);
203}
204
6131a5db 205des_cipher(in, out, salt, num_iter)
6ea1178a
NW
206 const char *in;
207 char *out;
208 long salt;
209 int num_iter;
6131a5db 210{
6ea1178a
NW
211 fprintf(stderr, "WARNING! des_cipher(3) not present in the system!\n");
212 bcopy(in, out, 8);
6131a5db
RM
213 return (0);
214}
215
6131a5db
RM
216setkey(key)
217 register const char *key;
218{
6ea1178a
NW
219 fprintf(stderr, "WARNING! setkey(3) not present in the system!\n");
220 return (0);
6131a5db
RM
221}
222
6131a5db 223encrypt(block, flag)
6ea1178a
NW
224 register char *block;
225 int flag;
6131a5db 226{
6ea1178a 227 fprintf(stderr, "WARNING! encrypt(3) not present in the system!\n");
6131a5db
RM
228 return (0);
229}