summaryrefslogtreecommitdiff
path: root/crypt_win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypt_win32.c')
-rw-r--r--crypt_win32.c355
1 files changed, 0 insertions, 355 deletions
diff --git a/crypt_win32.c b/crypt_win32.c
deleted file mode 100644
index c1afe00..0000000
--- a/crypt_win32.c
+++ /dev/null
@@ -1,355 +0,0 @@
1/*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2006 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: |
16 +----------------------------------------------------------------------+
17 */
18
19/* $Id: crypt_win32.c,v 1.1.1.1 2007-11-28 01:15:35 sesser Exp $ */
20
21/* This code is distributed under the PHP license with permission from
22 the author Jochen Obalek <jochen.obalek@bigfoot.de> */
23
24/* encrypt.c - providing 56 bit DES encryption
25 Copyright (C) 1991 Jochen Obalek
26
27 This program is free software; you can redistribute it and/or modify
28 it under the terms of the GNU General Public License as published by
29 the Free Software Foundation; either version 2, or (at your option)
30 any later version.
31
32 This program is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 GNU General Public License for more details.
36
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
40
41#include <time.h>
42#include <string.h>
43#include <stdlib.h>
44#include "crypt_md5.h"
45
46#define BS 64
47#define BS2 32
48#define KS 48
49#define KS2 24
50#define IS 56
51#define IS2 28
52
53static char schluessel[16][KS];
54
55
56static char PC1[] =
57{
58 56, 48, 40, 32, 24, 16, 8, 0,
59 57, 49, 41, 33, 25, 17, 9, 1,
60 58, 50, 42, 34, 26, 18, 10, 2,
61 59, 51, 43, 35,
62 62, 54, 46, 38, 30, 22, 14, 6,
63 61, 53, 45, 37, 29, 21, 13, 5,
64 60, 52, 44, 36, 28, 20, 12, 4,
65 27, 19, 11, 3
66};
67
68
69static char PC2[] =
70{
71 13, 16, 10, 23, 0, 4, 2, 27,
72 14, 5, 20, 9, 22, 18, 11, 3,
73 25, 7, 15, 6, 26, 19, 12, 1,
74 40, 51, 30, 36, 46, 54, 29, 39,
75 50, 44, 32, 47, 43, 48, 38, 55,
76 33, 52, 45, 41, 49, 35, 28, 31
77};
78
79
80static char IP[] =
81{
82 57, 49, 41, 33, 25, 17, 9, 1,
83 59, 51, 43, 35, 27, 19, 11, 3,
84 61, 53, 45, 37, 29, 21, 13, 5,
85 63, 55, 47, 39, 31, 23, 15, 7,
86 56, 48, 40, 32, 24, 16, 8, 0,
87 58, 50, 42, 34, 26, 18, 10, 2,
88 60, 52, 44, 36, 28, 20, 12, 4,
89 62, 54, 46, 38, 30, 22, 14, 6
90};
91
92
93static char EP[] =
94{
95 7, 39, 15, 47, 23, 55, 31, 63,
96 6, 38, 14, 46, 22, 54, 30, 62,
97 5, 37, 13, 45, 21, 53, 29, 61,
98 4, 36, 12, 44, 20, 52, 28, 60,
99 3, 35, 11, 43, 19, 51, 27, 59,
100 2, 34, 10, 42, 18, 50, 26, 58,
101 1, 33, 9, 41, 17, 49, 25, 57,
102 0, 32, 8, 40, 16, 48, 24, 56
103};
104
105
106static char E0[] =
107{
108 31, 0, 1, 2, 3, 4, 3, 4,
109 5, 6, 7, 8, 7, 8, 9, 10,
110 11, 12, 11, 12, 13, 14, 15, 16,
111 15, 16, 17, 18, 19, 20, 19, 20,
112 21, 22, 23, 24, 23, 24, 25, 26,
113 27, 28, 27, 28, 29, 30, 31, 0
114};
115
116
117static char E[KS];
118
119
120static char PERM[] =
121{
122 15, 6, 19, 20, 28, 11, 27, 16,
123 0, 14, 22, 25, 4, 17, 30, 9,
124 1, 7, 23, 13, 31, 26, 2, 8,
125 18, 12, 29, 5, 21, 10, 3, 24
126};
127
128
129static char S_BOX[][64] =
130{
131 {
132 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
133 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
134 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
135 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
136 },
137 {
138 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
139 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
140 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
141 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
142 },
143 {
144 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
145 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
146 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
147 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
148 },
149 {
150 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
151 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
152 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
153 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
154 },
155 {
156 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
157 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
158 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
159 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
160 },
161 {
162 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
163 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
164 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
165 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
166 },
167 {
168 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
169 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
170 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
171 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
172 },
173 {
174 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
175 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
176 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
177 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
178 }
179};
180
181static void
182perm (a, e, pc, n)
183 register char *a, *e;
184 register char *pc;
185 int n;
186{
187 for (; n--; pc++, a++)
188 *a = e[*pc];
189}
190
191static void
192crypt_main (nachr_l, nachr_r, schl)
193 register char *nachr_l, *nachr_r;
194 register char *schl;
195{
196 char tmp[KS];
197 register int sbval;
198 register char *tp = tmp;
199 register char *e = E;
200 register int i, j;
201
202 for (i = 0; i < 8; i++)
203 {
204 for (j = 0, sbval = 0; j < 6; j++)
205 sbval = (sbval << 1) | (nachr_r[*e++] ^ *schl++);
206 sbval = S_BOX[i][sbval];
207 for (tp += 4, j = 4; j--; sbval >>= 1)
208 *--tp = sbval & 1;
209 tp += 4;
210 }
211
212 e = PERM;
213 for (i = 0; i < BS2; i++)
214 *nachr_l++ ^= tmp[*e++];
215}
216
217void
218encrypt (char *nachr, int decr)
219{
220 char (*schl)[KS] = decr ? schluessel + 15 : schluessel;
221 char tmp[BS];
222 int i;
223
224 perm (tmp, nachr, IP, BS);
225
226 for (i = 8; i--;)
227 {
228 crypt_main (tmp, tmp + BS2, *schl);
229 if (decr)
230 schl--;
231 else
232 schl++;
233 crypt_main (tmp + BS2, tmp, *schl);
234 if (decr)
235 schl--;
236 else
237 schl++;
238 }
239
240 perm (nachr, tmp, EP, BS);
241}
242
243void
244setkey (char *schl)
245{
246 char tmp1[IS];
247 register unsigned int ls = 0x7efc;
248 register int i, j, k;
249 register int shval = 0;
250 register char *akt_schl;
251
252 memcpy (E, E0, KS);
253 perm (tmp1, schl, PC1, IS);
254
255 for (i = 0; i < 16; i++)
256 {
257 shval += 1 + (ls & 1);
258 akt_schl = schluessel[i];
259 for (j = 0; j < KS; j++)
260 {
261 if ((k = PC2[j]) >= IS2)
262 {
263 if ((k += shval) >= IS)
264 k = (k - IS2) % IS2 + IS2;
265 }
266 else if ((k += shval) >= IS2)
267 k %= IS2;
268 *akt_schl++ = tmp1[k];
269 }
270 ls >>= 1;
271 }
272}
273
274char *
275des_crypt (const char *wort, const char *salt)
276{
277 static char retkey[14];
278 char key[BS + 2];
279 char *k;
280 int tmp, keybyte;
281 int i, j;
282
283 memset (key, 0, BS + 2);
284
285 for (k = key, i = 0; i < BS; i++)
286 {
287 if (!(keybyte = *wort++))
288 break;
289 k += 7;
290 for (j = 0; j < 7; j++, i++)
291 {
292 *--k = keybyte & 1;
293 keybyte >>= 1;
294 }
295 k += 8;
296 }
297
298 setkey (key);
299 memset (key, 0, BS + 2);
300
301 for (k = E, i = 0; i < 2; i++)
302 {
303 retkey[i] = keybyte = *salt++;
304 if (keybyte > 'Z')
305 keybyte -= 'a' - 'Z' - 1;
306 if (keybyte > '9')
307 keybyte -= 'A' - '9' - 1;
308 keybyte -= '.';
309
310 for (j = 0; j < 6; j++, keybyte >>= 1, k++)
311 {
312 if (!(keybyte & 1))
313 continue;
314 tmp = *k;
315 *k = k[24];
316 k[24] = tmp;
317 }
318 }
319
320 for (i = 0; i < 25; i++)
321 encrypt (key, 0);
322
323 for (k = key, i = 0; i < 11; i++)
324 {
325 for (j = keybyte = 0; j < 6; j++)
326 {
327 keybyte <<= 1;
328 keybyte |= *k++;
329 }
330
331 keybyte += '.';
332 if (keybyte > '9')
333 keybyte += 'A' - '9' - 1;
334 if (keybyte > 'Z')
335 keybyte += 'a' - 'Z' - 1;
336 retkey[i + 2] = keybyte;
337 }
338
339 retkey[i + 2] = 0;
340
341 if (!retkey[1])
342 retkey[1] = *retkey;
343
344 return retkey;
345}
346
347char *
348crypt (const char *pw, const char *salt)
349{
350 if (strlen(salt)>MD5_MAGIC_LEN && strncmp(salt, MD5_MAGIC, MD5_MAGIC_LEN)==0) {
351 return md5_crypt(pw, salt);
352 } else {
353 return des_crypt(pw, salt);
354 }
355}