1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 1989 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/*
* Warning! Things are arranged very carefully in this file to
* allow read-only data to be moved to the text segment. The
* various DES tables must appear before any function definitions
* (this is arranged by including them immediately below) and partab
* must also appear before and function definitions
* This arrangement allows all data up through the first text to
* be moved to text.
*/
/*
* Fast (?) software implementation of DES
* Has been seen going at 2000 bytes/sec on a Sun-2
* Works on a VAX too.
* Won't work without 8 bit chars and 32 bit longs
*/
#include <sys/types.h>
#include <des/des.h>
#include <des/softdes.h>
#include <des/desdata.h>
#include <sys/debug.h>
static void des_setkey(u_char userkey[8], struct deskeydata *kd,
unsigned int dir);
static void des_encrypt(u_char *data, struct deskeydata *kd);
#define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
#define BIT28 (1<<28)
/*
* Software encrypt or decrypt a block of data (multiple of 8 bytes)
* Do the CBC ourselves if needed.
*/
/* ARGSUSED */
int
_des_crypt(char *buf, size_t len, struct desparams *desp)
{
short i;
uint_t mode;
uint_t dir;
char nextiv[8];
struct deskeydata softkey;
mode = desp->des_mode;
dir = desp->des_dir;
des_setkey(desp->des_key, &softkey, dir);
while (len != 0) {
switch (mode) {
case CBC:
switch (dir) {
case ENCRYPT:
for (i = 0; i < 8; i++)
buf[i] ^= desp->des_ivec[i];
des_encrypt((u_char *)buf, &softkey);
for (i = 0; i < 8; i++)
desp->des_ivec[i] = buf[i];
break;
case DECRYPT:
for (i = 0; i < 8; i++)
nextiv[i] = buf[i];
des_encrypt((u_char *)buf, &softkey);
for (i = 0; i < 8; i++) {
buf[i] ^= desp->des_ivec[i];
desp->des_ivec[i] = nextiv[i];
}
break;
}
break;
case ECB:
des_encrypt((u_char *)buf, &softkey);
break;
}
buf += 8;
len -= 8;
}
return (1);
}
/*
* Set the key and direction for an encryption operation
* We build the 16 key entries here
*/
/* ARGSUSED */
static void
des_setkey(u_char userkey[8], struct deskeydata *kd, unsigned int dir)
{
int32_t C, D;
short i;
/*
* First, generate C and D by permuting
* the key. The low order bit of each
* 8-bit char is not used, so C and D are only 28
* bits apiece.
*/
{
short bit;
short *pcc = (short *)PC1_C, *pcd = (short *)PC1_D;
C = D = 0;
for (i = 0; i < 28; i++) {
C <<= 1;
D <<= 1;
bit = *pcc++;
if (btst(userkey, bit))
C |= 1;
bit = *pcd++;
if (btst(userkey, bit))
D |= 1;
}
}
/*
* To generate Ki, rotate C and D according
* to schedule and pick up a permutation
* using PC2.
*/
for (i = 0; i < 16; i++) {
chunk_t *c;
short j, k, bit;
int bbit;
/*
* Do the "left shift" (rotate)
* We know we always rotate by either 1 or 2 bits
* the shifts table tells us if its 2
*/
C <<= 1;
if (C & BIT28)
C |= 1;
D <<= 1;
if (D & BIT28)
D |= 1;
if (shifts[i]) {
C <<= 1;
if (C & BIT28)
C |= 1;
D <<= 1;
if (D & BIT28)
D |= 1;
}
/*
* get Ki. Note C and D are concatenated.
*/
bit = 0;
switch (dir) {
case ENCRYPT:
c = &kd->keyval[i];
break;
case DECRYPT:
c = &kd->keyval[15 - i];
break;
}
c->long0 = 0;
c->long1 = 0;
bbit = (1 << 5) << 24;
for (j = 0; j < 4; j++) {
for (k = 0; k < 6; k++) {
if (C & (BIT28 >> PC2_C[bit]))
c->long0 |= bbit >> k;
if (D & (BIT28 >> PC2_D[bit]))
c->long1 |= bbit >> k;
bit++;
}
bbit >>= 8;
}
}
}
/*
* Do an encryption operation
* Much pain is taken (with preprocessor) to avoid loops so the compiler
* can do address arithmetic instead of doing it at runtime.
* Note that the byte-to-chunk conversion is necessary to guarantee
* processor byte-order independence.
*/
/* ARGSUSED */
static void
des_encrypt(u_char *data, struct deskeydata *kd)
{
chunk_t work1, work2;
/*
* Initial permutation
* and byte to chunk conversion
*/
{
const uint32_t *lp;
uint32_t l0, l1, w;
short i, pbit;
work1.byte0 = data[0];
work1.byte1 = data[1];
work1.byte2 = data[2];
work1.byte3 = data[3];
work1.byte4 = data[4];
work1.byte5 = data[5];
work1.byte6 = data[6];
work1.byte7 = data[7];
l0 = l1 = 0;
w = work1.long0;
for (lp = &longtab[0], i = 0; i < 32; i++) {
if (w & *lp++) {
pbit = IPtab[i];
if (pbit < 32)
l0 |= longtab[pbit];
else
l1 |= longtab[pbit-32];
}
}
w = work1.long1;
for (lp = &longtab[0], i = 32; i < 64; i++) {
if (w & *lp++) {
pbit = IPtab[i];
if (pbit < 32)
l0 |= longtab[pbit];
else
l1 |= longtab[pbit-32];
}
}
work2.long0 = l0;
work2.long1 = l1;
}
/*
* Expand 8 bits of 32 bit R to 48 bit R
*/
#ifdef __STDC__
#define do_R_to_ER(op, b) { \
struct R_to_ER *p = \
(struct R_to_ER *)&R_to_ER_tab[b][R.byte##b]; \
e0 op p->l0; \
e1 op p->l1; \
}
#else
#define do_R_to_ER(op, b) { \
/*CSTYLED*/ \
struct R_to_ER *p = &R_to_ER_tab[b][R.byte/**/b]; \
e0 op p->l0; \
e1 op p->l1; \
}
#endif
/*
* Inner part of the algorithm:
* Expand R from 32 to 48 bits; xor key value;
* apply S boxes; permute 32 bits of output
*/
#define do_F(iter, inR, outR) { \
chunk_t R, ER; \
u_int e0, e1; \
R.long0 = inR; \
/*CSTYLED*/ \
do_R_to_ER(=,0); \
/*CSTYLED*/ \
do_R_to_ER(|=,1); \
/*CSTYLED*/ \
do_R_to_ER(|=,2); \
/*CSTYLED*/ \
do_R_to_ER(|=,3); \
ER.long0 = e0 ^ kd->keyval[iter].long0; \
ER.long1 = e1 ^ kd->keyval[iter].long1; \
R.long0 = \
S_tab[0][ER.byte0] + \
S_tab[1][ER.byte1] + \
S_tab[2][ER.byte2] + \
S_tab[3][ER.byte3] + \
S_tab[4][ER.byte4] + \
S_tab[5][ER.byte5] + \
S_tab[6][ER.byte6] + \
S_tab[7][ER.byte7]; \
outR = \
P_tab[0][R.byte0] + \
P_tab[1][R.byte1] + \
P_tab[2][R.byte2] + \
P_tab[3][R.byte3]; \
}
/*
* Do a cipher step
* Apply inner part; do xor and exchange of 32 bit parts
*/
#define cipher(iter, inR, inL, outR, outL) { \
do_F(iter, inR, outR); \
outR ^= inL; \
outL = inR; \
}
/*
* Apply the 16 ciphering steps
*/
{
u_int r0, l0, r1, l1;
l0 = work2.long0;
r0 = work2.long1;
cipher(0, r0, l0, r1, l1);
cipher(1, r1, l1, r0, l0);
cipher(2, r0, l0, r1, l1);
cipher(3, r1, l1, r0, l0);
cipher(4, r0, l0, r1, l1);
cipher(5, r1, l1, r0, l0);
cipher(6, r0, l0, r1, l1);
cipher(7, r1, l1, r0, l0);
cipher(8, r0, l0, r1, l1);
cipher(9, r1, l1, r0, l0);
cipher(10, r0, l0, r1, l1);
cipher(11, r1, l1, r0, l0);
cipher(12, r0, l0, r1, l1);
cipher(13, r1, l1, r0, l0);
cipher(14, r0, l0, r1, l1);
cipher(15, r1, l1, r0, l0);
work1.long0 = r0;
work1.long1 = l0;
}
/*
* Final permutation
* and chunk to byte conversion
*/
{
const uint32_t *lp;
uint32_t l0, l1, w;
short i, pbit;
l0 = l1 = 0;
w = work1.long0;
for (lp = &longtab[0], i = 0; i < 32; i++) {
if (w & *lp++) {
pbit = FPtab[i];
if (pbit < 32)
l0 |= longtab[pbit];
else
l1 |= longtab[pbit-32];
}
}
w = work1.long1;
for (lp = &longtab[0], i = 32; i < 64; i++) {
if (w & *lp++) {
pbit = FPtab[i];
if (pbit < 32)
l0 |= longtab[pbit];
else
l1 |= longtab[pbit-32];
}
}
work2.long0 = l0;
work2.long1 = l1;
}
data[0] = work2.byte0;
data[1] = work2.byte1;
data[2] = work2.byte2;
data[3] = work2.byte3;
data[4] = work2.byte4;
data[5] = work2.byte5;
data[6] = work2.byte6;
data[7] = work2.byte7;
}
|