4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
35 * Warning! Things are arranged very carefully in this file to
36 * allow read-only data to be moved to the text segment. The
37 * various DES tables must appear before any function definitions
38 * (this is arranged by including them immediately below) and partab
39 * must also appear before and function definitions
40 * This arrangement allows all data up through the first text to
45 * Fast (?) software implementation of DES
46 * Has been seen going at 2000 bytes/sec on a Sun-2
48 * Won't work without 8 bit chars and 32 bit longs
51 #include <sys/types.h>
53 #include <des/softdes.h>
54 #include <des/desdata.h>
55 #include <sys/debug.h>
57 static void des_setkey(u_char userkey
[8], struct deskeydata
*kd
,
59 static void des_encrypt(u_char
*data
, struct deskeydata
*kd
);
61 #define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
65 * Software encrypt or decrypt a block of data (multiple of 8 bytes)
66 * Do the CBC ourselves if needed.
70 _des_crypt(char *buf
, size_t len
, struct desparams
*desp
)
76 struct deskeydata softkey
;
78 mode
= desp
->des_mode
;
80 des_setkey(desp
->des_key
, &softkey
, dir
);
86 for (i
= 0; i
< 8; i
++)
87 buf
[i
] ^= desp
->des_ivec
[i
];
88 des_encrypt((u_char
*)buf
, &softkey
);
89 for (i
= 0; i
< 8; i
++)
90 desp
->des_ivec
[i
] = buf
[i
];
93 for (i
= 0; i
< 8; i
++)
95 des_encrypt((u_char
*)buf
, &softkey
);
96 for (i
= 0; i
< 8; i
++) {
97 buf
[i
] ^= desp
->des_ivec
[i
];
98 desp
->des_ivec
[i
] = nextiv
[i
];
104 des_encrypt((u_char
*)buf
, &softkey
);
115 * Set the key and direction for an encryption operation
116 * We build the 16 key entries here
120 des_setkey(u_char userkey
[8], struct deskeydata
*kd
, unsigned int dir
)
126 * First, generate C and D by permuting
127 * the key. The low order bit of each
128 * 8-bit char is not used, so C and D are only 28
133 short *pcc
= (short *)PC1_C
, *pcd
= (short *)PC1_D
;
136 for (i
= 0; i
< 28; i
++) {
140 if (btst(userkey
, bit
))
143 if (btst(userkey
, bit
))
148 * To generate Ki, rotate C and D according
149 * to schedule and pick up a permutation
152 for (i
= 0; i
< 16; i
++) {
158 * Do the "left shift" (rotate)
159 * We know we always rotate by either 1 or 2 bits
160 * the shifts table tells us if its 2
177 * get Ki. Note C and D are concatenated.
185 c
= &kd
->keyval
[15 - i
];
190 bbit
= (1 << 5) << 24;
191 for (j
= 0; j
< 4; j
++) {
192 for (k
= 0; k
< 6; k
++) {
193 if (C
& (BIT28
>> PC2_C
[bit
]))
194 c
->long0
|= bbit
>> k
;
195 if (D
& (BIT28
>> PC2_D
[bit
]))
196 c
->long1
|= bbit
>> k
;
207 * Do an encryption operation
208 * Much pain is taken (with preprocessor) to avoid loops so the compiler
209 * can do address arithmetic instead of doing it at runtime.
210 * Note that the byte-to-chunk conversion is necessary to guarantee
211 * processor byte-order independence.
215 des_encrypt(u_char
*data
, struct deskeydata
*kd
)
217 chunk_t work1
, work2
;
220 * Initial permutation
221 * and byte to chunk conversion
228 work1
.byte0
= data
[0];
229 work1
.byte1
= data
[1];
230 work1
.byte2
= data
[2];
231 work1
.byte3
= data
[3];
232 work1
.byte4
= data
[4];
233 work1
.byte5
= data
[5];
234 work1
.byte6
= data
[6];
235 work1
.byte7
= data
[7];
238 for (lp
= &longtab
[0], i
= 0; i
< 32; i
++) {
244 l1
|= longtab
[pbit
-32];
248 for (lp
= &longtab
[0], i
= 32; i
< 64; i
++) {
254 l1
|= longtab
[pbit
-32];
262 * Expand 8 bits of 32 bit R to 48 bit R
265 #define do_R_to_ER(op, b) { \
266 struct R_to_ER *p = \
267 (struct R_to_ER *)&R_to_ER_tab[b][R.byte##b]; \
272 #define do_R_to_ER(op, b) { \
274 struct R_to_ER *p = &R_to_ER_tab[b][R.byte/**/b]; \
281 * Inner part of the algorithm:
282 * Expand R from 32 to 48 bits; xor key value;
283 * apply S boxes; permute 32 bits of output
285 #define do_F(iter, inR, outR) { \
297 ER.long0 = e0 ^ kd->keyval[iter].long0; \
298 ER.long1 = e1 ^ kd->keyval[iter].long1; \
300 S_tab[0][ER.byte0] + \
301 S_tab[1][ER.byte1] + \
302 S_tab[2][ER.byte2] + \
303 S_tab[3][ER.byte3] + \
304 S_tab[4][ER.byte4] + \
305 S_tab[5][ER.byte5] + \
306 S_tab[6][ER.byte6] + \
307 S_tab[7][ER.byte7]; \
309 P_tab[0][R.byte0] + \
310 P_tab[1][R.byte1] + \
311 P_tab[2][R.byte2] + \
317 * Apply inner part; do xor and exchange of 32 bit parts
319 #define cipher(iter, inR, inL, outR, outL) { \
320 do_F(iter, inR, outR); \
326 * Apply the 16 ciphering steps
329 u_int r0
, l0
, r1
, l1
;
333 cipher(0, r0
, l0
, r1
, l1
);
334 cipher(1, r1
, l1
, r0
, l0
);
335 cipher(2, r0
, l0
, r1
, l1
);
336 cipher(3, r1
, l1
, r0
, l0
);
337 cipher(4, r0
, l0
, r1
, l1
);
338 cipher(5, r1
, l1
, r0
, l0
);
339 cipher(6, r0
, l0
, r1
, l1
);
340 cipher(7, r1
, l1
, r0
, l0
);
341 cipher(8, r0
, l0
, r1
, l1
);
342 cipher(9, r1
, l1
, r0
, l0
);
343 cipher(10, r0
, l0
, r1
, l1
);
344 cipher(11, r1
, l1
, r0
, l0
);
345 cipher(12, r0
, l0
, r1
, l1
);
346 cipher(13, r1
, l1
, r0
, l0
);
347 cipher(14, r0
, l0
, r1
, l1
);
348 cipher(15, r1
, l1
, r0
, l0
);
355 * and chunk to byte conversion
364 for (lp
= &longtab
[0], i
= 0; i
< 32; i
++) {
370 l1
|= longtab
[pbit
-32];
374 for (lp
= &longtab
[0], i
= 32; i
< 64; i
++) {
380 l1
|= longtab
[pbit
-32];
386 data
[0] = work2
.byte0
;
387 data
[1] = work2
.byte1
;
388 data
[2] = work2
.byte2
;
389 data
[3] = work2
.byte3
;
390 data
[4] = work2
.byte4
;
391 data
[5] = work2
.byte5
;
392 data
[6] = work2
.byte6
;
393 data
[7] = work2
.byte7
;