]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/perl/perl/crypt.c
Add the missing rerelease target back.
[FreeBSD/FreeBSD.git] / gnu / usr.bin / perl / perl / crypt.c
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: /home/ncvs/src/gnu/usr.bin/perl/perl/crypt.c,v 1.1.1.1 1994/09/10 06:27:37 gclarkii Exp $";
40 #endif /* LIBC_SCCS and not lint */
41
42 #include <unistd.h>
43 #include <stdio.h>
44
45 /*
46  * UNIX password, and DES, encryption.
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.
50  */
51
52 #define SCRAMBLE                /* Don't leave them in plaintext */
53
54 #ifndef SCRAMBLE
55 static char     cryptresult[1+4+4+11+1];        /* "encrypted" result */
56
57 char *
58 crypt(key, setting)
59         register const char *key;
60         register const char *setting;
61 {
62         fprintf(stderr, "WARNING!  crypt(3) not present in the system!\n");
63         strncpy(cryptresult, key, sizeof cryptresult);
64         cryptresult[sizeof cryptresult - 1] = '\0';
65         return (cryptresult);
66 }
67
68 #else
69
70 char *
71 crypt(pw, salt)
72         register const char *pw;
73         register const char *salt;
74 {
75         static char     password[14];
76         long            matrix[128], *m, vector[2];
77         char            a, b, *p;
78         int             i, value;
79         unsigned short  crc;
80         unsigned long   t;
81
82         /* Ugly hack, but I'm too lazy to find the real problem - NW */
83         bzero(matrix, 128 * sizeof(long));
84
85         if (salt[0]) {
86                 a = salt[0];
87                 if (salt[1])
88                         b = salt[1];
89                 else
90                         b = a;
91         } else
92                 a = b = '0';
93         password[0] = a;
94         password[1] = b;
95         if (a > 'Z')
96                 a -= 6;
97         if (a > '9')
98                 a -= 7;
99         if (b > 'Z')
100                 b -= 6;
101         if (b > '9')
102                 b -= 7;
103         a -= '.';
104         b -= '.';
105         value = (a | (b << 6)) & 07777;
106
107         crc = value;
108         value += 1000;
109         b = 0;
110         p = (char *)pw;
111         while (value--) {
112                 if (crc & 0x8000)
113                         crc = (crc << 1) ^ 0x1021;
114                 else
115                         crc <<= 1;
116                 if (!b) {
117                         b = 8;
118                         if (!(i = *p++)) {
119                                 p = (char *)pw;
120                                 i = *p++;
121                         }
122                 }
123                 if (i & 0x80)
124                         crc ^= 1;
125                 i <<= 1;
126                 b--;
127         }
128
129         m = matrix;
130         matrix[0] = 0;
131         a = 32;
132         for (value = 07777; value >= 0; value--) {
133                 *m <<= 1;
134                 if (crc & 0x8000) {
135                         *m |= 1;
136                         crc = (crc << 1) ^ 0x1021;
137                 } else
138                         crc <<= 1;
139                 if (!b) {
140                         b = 8;
141                         if (!(i = *p++)) {
142                                 p = (char *)pw;
143                                 i = *p++;
144                         }
145                 }
146                 if (i & 0x80)
147                         crc ^= 1;
148                 i <<= 1;
149                 b--;
150                 if (!(a--)) {
151                         a = 32;
152                         *++m = 0;
153                 }
154         }
155
156         vector[0] = 0;
157         vector[1] = 0;
158         p = (char *) vector;
159         for (i = 0; i < 7; i++)
160                 if (pw[i])
161                         *p++ = pw[i];
162                 else
163                         break;
164
165         p = password + 2;
166         a = 6;
167         m = matrix;
168         *p = 0;
169         for (i = 077; i >= 0; i--) {
170                 t = *m++;
171                 t = t ^ *m++;
172                 t = t ^ vector[0];
173                 t = t ^ vector[1];
174                 b = 0;
175                 while (t) {
176                         if (t & 1)
177                                 b = 1 - b;
178                         t >>= 1;
179                 }
180                 a--;
181                 if (b)
182                         *p |= 1 << a;
183                 if (!a) {
184                         a = 6;
185                         *++p = 0;
186                 }
187         }
188
189         for (i = 2; i < 13; i++) {
190                 password[i] += '.';
191                 if (password[i] > '9')
192                         password[i] += 7;
193                 if (password[i] > 'Z')
194                         password[i] += 6;
195         }
196         password[13] = 0;
197
198         return password;
199 }
200 #endif