encrypt.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* encrypt.c - providing 56 bit DES encryption
10  * Copyright (C) 1991 Jochen Obalek
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 
26 #include "squid.h"
27 
28 #include <time.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #define BS 64
33 #define BS2 32
34 #define KS 48
35 #define KS2 24
36 #define IS 56
37 #define IS2 28
38 
39 static char schluessel[16][KS];
40 
41 static char PC1[] = {
42  56, 48, 40, 32, 24, 16, 8, 0,
43  57, 49, 41, 33, 25, 17, 9, 1,
44  58, 50, 42, 34, 26, 18, 10, 2,
45  59, 51, 43, 35,
46  62, 54, 46, 38, 30, 22, 14, 6,
47  61, 53, 45, 37, 29, 21, 13, 5,
48  60, 52, 44, 36, 28, 20, 12, 4,
49  27, 19, 11, 3
50 };
51 
52 static char PC2[] = {
53  13, 16, 10, 23, 0, 4, 2, 27,
54  14, 5, 20, 9, 22, 18, 11, 3,
55  25, 7, 15, 6, 26, 19, 12, 1,
56  40, 51, 30, 36, 46, 54, 29, 39,
57  50, 44, 32, 47, 43, 48, 38, 55,
58  33, 52, 45, 41, 49, 35, 28, 31
59 };
60 
61 static char IP[] = {
62  57, 49, 41, 33, 25, 17, 9, 1,
63  59, 51, 43, 35, 27, 19, 11, 3,
64  61, 53, 45, 37, 29, 21, 13, 5,
65  63, 55, 47, 39, 31, 23, 15, 7,
66  56, 48, 40, 32, 24, 16, 8, 0,
67  58, 50, 42, 34, 26, 18, 10, 2,
68  60, 52, 44, 36, 28, 20, 12, 4,
69  62, 54, 46, 38, 30, 22, 14, 6
70 };
71 
72 static char EP[] = {
73  7, 39, 15, 47, 23, 55, 31, 63,
74  6, 38, 14, 46, 22, 54, 30, 62,
75  5, 37, 13, 45, 21, 53, 29, 61,
76  4, 36, 12, 44, 20, 52, 28, 60,
77  3, 35, 11, 43, 19, 51, 27, 59,
78  2, 34, 10, 42, 18, 50, 26, 58,
79  1, 33, 9, 41, 17, 49, 25, 57,
80  0, 32, 8, 40, 16, 48, 24, 56
81 };
82 
83 static char E0[] = {
84  31, 0, 1, 2, 3, 4, 3, 4,
85  5, 6, 7, 8, 7, 8, 9, 10,
86  11, 12, 11, 12, 13, 14, 15, 16,
87  15, 16, 17, 18, 19, 20, 19, 20,
88  21, 22, 23, 24, 23, 24, 25, 26,
89  27, 28, 27, 28, 29, 30, 31, 0
90 };
91 
92 static char E[KS];
93 
94 static char PERM[] = {
95  15, 6, 19, 20, 28, 11, 27, 16,
96  0, 14, 22, 25, 4, 17, 30, 9,
97  1, 7, 23, 13, 31, 26, 2, 8,
98  18, 12, 29, 5, 21, 10, 3, 24
99 };
100 
101 static char S_BOX[][64] = {
102  {
103  14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
104  3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
105  4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
106  15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
107  },
108  {
109  15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
110  9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
111  0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
112  5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
113  },
114  {
115  10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
116  1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
117  13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
118  11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
119  },
120  {
121  7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
122  1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
123  10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
124  15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
125  },
126  {
127  2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
128  8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
129  4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
130  15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
131  },
132  {
133  12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
134  0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
135  9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
136  7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
137  },
138  {
139  4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
140  3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
141  1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
142  10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
143  },
144  {
145  13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
146  10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
147  7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
148  0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
149  }
150 };
151 
152 static void
153 perm(a, e, pc, n)
154 register char *a, *e;
155 register char *pc;
156 int n;
157 {
158  for (; n--; pc++, a++)
159  *a = e[(int)*pc];
160 }
161 
162 static void
163 crypt_main(nachr_l, nachr_r, schl)
164 register char *nachr_l, *nachr_r;
165 register char *schl;
166 {
167  char tmp[KS];
168  register int sbval;
169  register char *tp = tmp;
170  register char *e = E;
171  register int i, j;
172 
173  for (i = 0; i < 8; i++) {
174  for (j = 0, sbval = 0; j < 6; j++)
175  sbval = (sbval << 1) | (nachr_r[(int)*e++] ^ *schl++);
176  sbval = S_BOX[i][sbval];
177  for (tp += 4, j = 4; j--; sbval >>= 1)
178  *--tp = sbval & 1;
179  tp += 4;
180  }
181 
182  e = PERM;
183  for (i = 0; i < BS2; i++)
184  *nachr_l++ ^= tmp[(int)*e++];
185 }
186 
187 void
188 encrypt(char *nachr, int decr)
189 {
190  char (*schl)[KS] = decr ? schluessel + 15 : schluessel;
191  char tmp[BS];
192  int i;
193 
194  perm(tmp, nachr, IP, BS);
195 
196  for (i = 8; i--;) {
197  crypt_main(tmp, tmp + BS2, *schl);
198  if (decr)
199  schl--;
200  else
201  schl++;
202  crypt_main(tmp + BS2, tmp, *schl);
203  if (decr)
204  schl--;
205  else
206  schl++;
207  }
208 
209  perm(nachr, tmp, EP, BS);
210 }
211 
212 void
213 setkey(char *schl)
214 {
215  char tmp1[IS];
216  register unsigned int ls = 0x7efc;
217  register int i, j, k;
218  register int shval = 0;
219  register char *akt_schl;
220 
221  memcpy(E, E0, KS);
222  perm(tmp1, schl, PC1, IS);
223 
224  for (i = 0; i < 16; i++) {
225  shval += 1 + (ls & 1);
226  akt_schl = schluessel[i];
227  for (j = 0; j < KS; j++) {
228  if ((k = PC2[j]) >= IS2) {
229  if ((k += shval) >= IS)
230  k = (k - IS2) % IS2 + IS2;
231  } else if ((k += shval) >= IS2)
232  k %= IS2;
233  *akt_schl++ = tmp1[k];
234  }
235  ls >>= 1;
236  }
237 }
238 
239 char *
240 crypt(const char *wort, const char *salt)
241 {
242  static char retkey[14];
243  char key[BS + 2];
244  char *k;
245  int tmp, keybyte;
246  int i, j;
247 
248  memset(key, 0, BS + 2);
249 
250  for (k = key, i = 0; i < BS; i++) {
251  if (!(keybyte = *wort++))
252  break;
253  k += 7;
254  for (j = 0; j < 7; j++, i++) {
255  *--k = keybyte & 1;
256  keybyte >>= 1;
257  }
258  k += 8;
259  }
260 
261  setkey(key);
262  memset(key, 0, BS + 2);
263 
264  for (k = E, i = 0; i < 2; i++) {
265  retkey[i] = keybyte = *salt++;
266  if (keybyte > 'Z')
267  keybyte -= 'a' - 'Z' - 1;
268  if (keybyte > '9')
269  keybyte -= 'A' - '9' - 1;
270  keybyte -= '.';
271 
272  for (j = 0; j < 6; j++, keybyte >>= 1, k++) {
273  if (!(keybyte & 1))
274  continue;
275  tmp = *k;
276  *k = k[24];
277  k[24] = tmp;
278  }
279  }
280 
281  for (i = 0; i < 25; i++)
282  encrypt(key, 0);
283 
284  for (k = key, i = 0; i < 11; i++) {
285  for (j = keybyte = 0; j < 6; j++) {
286  keybyte <<= 1;
287  keybyte |= *k++;
288  }
289 
290  keybyte += '.';
291  if (keybyte > '9')
292  keybyte += 'A' - '9' - 1;
293  if (keybyte > 'Z')
294  keybyte += 'a' - 'Z' - 1;
295  retkey[i + 2] = keybyte;
296  }
297 
298  retkey[i + 2] = 0;
299 
300  if (!retkey[1])
301  retkey[1] = *retkey;
302 
303  return retkey;
304 }
305 
int i
Definition: membanger.c:49
#define BS2
Definition: encrypt.c:33
void encrypt(char *nachr, int decr)
Definition: encrypt.c:188
#define BS
Definition: encrypt.c:32
static char S_BOX[][64]
Definition: encrypt.c:101
static void perm(char *a, char *e, char *pc, int n)
Definition: encrypt.c:153
static char E0[]
Definition: encrypt.c:83
static void crypt_main(char *nachr_l, char *nachr_r, char *schl)
Definition: encrypt.c:163
bool SIGHDLR int STUB void int
Definition: stub_tools.cc:68
void setkey(char *schl)
Definition: encrypt.c:213
#define KS
Definition: encrypt.c:34
static char PC1[]
Definition: encrypt.c:41
static char PC2[]
Definition: encrypt.c:52
static char PERM[]
Definition: encrypt.c:94
char * crypt(const char *wort, const char *salt)
Definition: encrypt.c:240
#define IS
Definition: encrypt.c:36
static char E[KS]
Definition: encrypt.c:92
void const cache_key * key
int a
Definition: membanger.c:50
static char EP[]
Definition: encrypt.c:72
static char IP[]
Definition: encrypt.c:61
#define IS2
Definition: encrypt.c:37
static char schluessel[16][KS]
Definition: encrypt.c:39

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors