summaryrefslogtreecommitdiff
path: root/src/lib/libc/crypt/morecrypt.c
diff options
context:
space:
mode:
authorderaadt <>1995-12-16 12:55:31 +0000
committerderaadt <>1995-12-16 12:55:31 +0000
commit767a35a43c1498fbfe4461e5456c6cd41f08de26 (patch)
treee617d22b73a45255f89c8b85781415b11dd8406a /src/lib/libc/crypt/morecrypt.c
parent8be58a122b61ab24a5524e848814fe5dd314d243 (diff)
downloadopenbsd-767a35a43c1498fbfe4461e5456c6cd41f08de26.tar.gz
openbsd-767a35a43c1498fbfe4461e5456c6cd41f08de26.tar.bz2
openbsd-767a35a43c1498fbfe4461e5456c6cd41f08de26.zip
non-USA crypto code by davidb@werj.com.au. The source has been split
& copied into two pieces so that use of crypt() pulls in a crypt.o that contains only that one programmer interface -- this permits USA export of binaries that use crypt() for authentication purposes. morecrypt.c contains the other DES programmer interfaces commonly used.
Diffstat (limited to 'src/lib/libc/crypt/morecrypt.c')
-rw-r--r--src/lib/libc/crypt/morecrypt.c338
1 files changed, 338 insertions, 0 deletions
diff --git a/src/lib/libc/crypt/morecrypt.c b/src/lib/libc/crypt/morecrypt.c
new file mode 100644
index 0000000000..85ace2ecce
--- /dev/null
+++ b/src/lib/libc/crypt/morecrypt.c
@@ -0,0 +1,338 @@
1/* $Id: morecrypt.c,v 1.1 1995/12/16 12:55:31 deraadt Exp $ */
2
3/*
4 * FreeSec: libcrypt
5 *
6 * Copyright (c) 1994 David Burren
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the author nor the names of other contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *
34 * This is an original implementation of the DES and the crypt(3) interfaces
35 * by David Burren <davidb@werj.com.au>.
36 *
37 * An excellent reference on the underlying algorithm (and related
38 * algorithms) is:
39 *
40 * B. Schneier, Applied Cryptography: protocols, algorithms,
41 * and source code in C, John Wiley & Sons, 1994.
42 *
43 * Note that in that book's description of DES the lookups for the initial,
44 * pbox, and final permutations are inverted (this has been brought to the
45 * attention of the author). A list of errata for this book has been
46 * posted to the sci.crypt newsgroup by the author and is available for FTP.
47 *
48 * ARCHITECTURE ASSUMPTIONS:
49 * This code assumes that u_longs are 32 bits. It will probably not
50 * operate on 64-bit machines without modifications.
51 * It is assumed that the 8-byte arrays passed by reference can be
52 * addressed as arrays of u_longs (ie. the CPU is not picky about
53 * alignment).
54 *
55 * NOTE:
56 * This file must copy certain chunks of crypt.c for legal reasons.
57 * crypt.c can only export the interface crypt(), to make binaries
58 * exportable from the USA. Hence, to also have the other crypto interfaces
59 * available we have to copy pieces...
60 */
61#include <sys/types.h>
62#include <sys/param.h>
63#include <pwd.h>
64
65#ifdef DEBUG
66# include <stdio.h>
67#endif
68
69static u_char IP[64] = {
70 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
71 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
72 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
73 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
74};
75
76static u_char inv_key_perm[64];
77static u_char u_key_perm[56];
78static u_char key_perm[56] = {
79 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
80 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
81 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
82 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
83};
84
85static u_char key_shifts[16] = {
86 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
87};
88
89static u_char inv_comp_perm[56];
90static u_char comp_perm[48] = {
91 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
92 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
93 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
94 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
95};
96
97/*
98 * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
99 */
100
101static u_char u_sbox[8][64];
102static u_char sbox[8][64] = {
103 {
104 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
105 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
106 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
107 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
108 },
109 {
110 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
111 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
112 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
113 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
114 },
115 {
116 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
117 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
118 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
119 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
120 },
121 {
122 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
123 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
124 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
125 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
126 },
127 {
128 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
129 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
130 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
131 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
132 },
133 {
134 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
135 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
136 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
137 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
138 },
139 {
140 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
141 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
142 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
143 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
144 },
145 {
146 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
147 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
148 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
149 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
150 }
151};
152
153static u_char un_pbox[32];
154static u_char pbox[32] = {
155 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
156 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
157};
158
159static u_int32_t bits32[32] =
160{
161 0x80000000, 0x40000000, 0x20000000, 0x10000000,
162 0x08000000, 0x04000000, 0x02000000, 0x01000000,
163 0x00800000, 0x00400000, 0x00200000, 0x00100000,
164 0x00080000, 0x00040000, 0x00020000, 0x00010000,
165 0x00008000, 0x00004000, 0x00002000, 0x00001000,
166 0x00000800, 0x00000400, 0x00000200, 0x00000100,
167 0x00000080, 0x00000040, 0x00000020, 0x00000010,
168 0x00000008, 0x00000004, 0x00000002, 0x00000001
169};
170
171static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
172
173static u_int32_t saltbits;
174static int32_t old_salt;
175static u_int32_t *bits28, *bits24;
176static u_char init_perm[64], final_perm[64];
177static u_int32_t en_keysl[16], en_keysr[16];
178static u_int32_t de_keysl[16], de_keysr[16];
179static int des_initialised = 0;
180static u_char m_sbox[4][4096];
181static u_int32_t psbox[4][256];
182static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
183static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
184static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
185static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
186static u_int32_t old_rawkey0, old_rawkey1;
187
188static u_char ascii64[] =
189 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
190/* 0000000000111111111122222222223333333333444444444455555555556666 */
191/* 0123456789012345678901234567890123456789012345678901234567890123 */
192
193static inline int
194ascii_to_bin(ch)
195 char ch;
196{
197 if (ch > 'z')
198 return(0);
199 if (ch >= 'a')
200 return(ch - 'a' + 38);
201 if (ch > 'Z')
202 return(0);
203 if (ch >= 'A')
204 return(ch - 'A' + 12);
205 if (ch > '9')
206 return(0);
207 if (ch >= '.')
208 return(ch - '.');
209 return(0);
210}
211
212int
213des_setkey(key)
214 const char *key;
215{
216 u_int32_t k0, k1, rawkey0, rawkey1;
217 int shifts, i, b, round;
218
219 if (!des_initialised)
220 des_init();
221
222 rawkey0 = ntohl(*(u_int32_t *) key);
223 rawkey1 = ntohl(*(u_int32_t *) (key + 4));
224
225 if ((rawkey0 | rawkey1)
226 && rawkey0 == old_rawkey0
227 && rawkey1 == old_rawkey1) {
228 /*
229 * Already setup for this key.
230 * This optimisation fails on a zero key (which is weak and
231 * has bad parity anyway) in order to simplify the starting
232 * conditions.
233 */
234 return(0);
235 }
236 old_rawkey0 = rawkey0;
237 old_rawkey1 = rawkey1;
238
239 /*
240 * Do key permutation and split into two 28-bit subkeys.
241 */
242 k0 = key_perm_maskl[0][rawkey0 >> 25]
243 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
244 | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
245 | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
246 | key_perm_maskl[4][rawkey1 >> 25]
247 | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
248 | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
249 | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
250 k1 = key_perm_maskr[0][rawkey0 >> 25]
251 | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
252 | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
253 | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
254 | key_perm_maskr[4][rawkey1 >> 25]
255 | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
256 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
257 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
258 /*
259 * Rotate subkeys and do compression permutation.
260 */
261 shifts = 0;
262 for (round = 0; round < 16; round++) {
263 u_int32_t t0, t1;
264 int bit;
265
266 shifts += key_shifts[round];
267
268 t0 = (k0 << shifts) | (k0 >> (28 - shifts));
269 t1 = (k1 << shifts) | (k1 >> (28 - shifts));
270
271 de_keysl[15 - round] =
272 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
273 | comp_maskl[1][(t0 >> 14) & 0x7f]
274 | comp_maskl[2][(t0 >> 7) & 0x7f]
275 | comp_maskl[3][t0 & 0x7f]
276 | comp_maskl[4][(t1 >> 21) & 0x7f]
277 | comp_maskl[5][(t1 >> 14) & 0x7f]
278 | comp_maskl[6][(t1 >> 7) & 0x7f]
279 | comp_maskl[7][t1 & 0x7f];
280
281 de_keysr[15 - round] =
282 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
283 | comp_maskr[1][(t0 >> 14) & 0x7f]
284 | comp_maskr[2][(t0 >> 7) & 0x7f]
285 | comp_maskr[3][t0 & 0x7f]
286 | comp_maskr[4][(t1 >> 21) & 0x7f]
287 | comp_maskr[5][(t1 >> 14) & 0x7f]
288 | comp_maskr[6][(t1 >> 7) & 0x7f]
289 | comp_maskr[7][t1 & 0x7f];
290 }
291 return(0);
292}
293
294int
295setkey(key)
296 char *key;
297{
298 int i, j;
299 u_int32_t packed_keys[2];
300 u_char *p;
301
302 p = (u_char *) packed_keys;
303
304 for (i = 0; i < 8; i++) {
305 p[i] = 0;
306 for (j = 0; j < 8; j++)
307 if (*key++ & 1)
308 p[i] |= bits8[j];
309 }
310 return(des_setkey(p));
311}
312
313int
314encrypt(block, flag)
315 char *block;
316 int flag;
317{
318 u_int32_t io[2];
319 u_char *p;
320 int i, j, retval;
321
322 if (!des_initialised)
323 des_init();
324
325 setup_salt(0L);
326 p = block;
327 for (i = 0; i < 2; i++) {
328 io[i] = 0L;
329 for (j = 0; j < 32; j++)
330 if (*p++ & 1)
331 io[i] |= bits32[j];
332 }
333 retval = do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
334 for (i = 0; i < 2; i++)
335 for (j = 0; j < 32; j++)
336 block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0;
337 return(retval);
338}