4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
36 * Warning! Things are arranged very carefully in this file to
37 * allow read-only data to be moved to the text segment. The
38 * various DES tables must appear before any function definitions
39 * (this is arranged by including them immediately below) and partab
40 * must also appear before and function definitions
41 * This arrangement allows all data up through the first text to
46 #define CRYPT /* cannot configure out of user-level code */
50 #include <sys/types.h>
51 #include <des/softdes.h>
52 #include <des/desdata.h>
55 #include <sys/ioctl.h>
64 * Fast (?) software implementation of DES
65 * Has been seen going at 2000 bytes/sec on a Sun-2
67 * Won't work without 8 bit chars and 32 bit longs
70 #define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
74 #endif /* def CRYPT */
76 static void des_setkey(uchar_t
[8], struct deskeydata
*, unsigned);
77 static void des_encrypt(uchar_t
*, struct deskeydata
*);
81 * Table giving odd parity in the low bit for ASCII characters
83 static char partab
[128] = {
84 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
85 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
86 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
87 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
88 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
89 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
90 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
91 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
92 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
93 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
94 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
95 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
96 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
97 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
98 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
99 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
105 * Add odd parity to low bit of 8 byte key
108 des_setparity(char *p
)
112 for (i
= 0; i
< 8; i
++) {
113 *p
= partab
[*p
& 0x7f];
117 #endif /* def _KERNEL */
121 * Software encrypt or decrypt a block of data (multiple of 8 bytes)
122 * Do the CBC ourselves if needed.
125 __des_crypt(char *buf
, unsigned int len
, struct desparams
*desp
)
131 struct deskeydata softkey
;
133 mode
= (unsigned)desp
->des_mode
;
134 dir
= (unsigned)desp
->des_dir
;
135 des_setkey(desp
->des_key
, &softkey
, dir
);
141 for (i
= 0; i
< 8; i
++)
142 buf
[i
] ^= desp
->des_ivec
[i
];
143 des_encrypt((uchar_t
*)buf
, &softkey
);
144 for (i
= 0; i
< 8; i
++)
145 desp
->des_ivec
[i
] = buf
[i
];
148 for (i
= 0; i
< 8; i
++)
150 des_encrypt((uchar_t
*)buf
, &softkey
);
151 for (i
= 0; i
< 8; i
++) {
152 buf
[i
] ^= desp
->des_ivec
[i
];
153 desp
->des_ivec
[i
] = nextiv
[i
];
159 des_encrypt((uchar_t
*)buf
, &softkey
);
170 * Set the key and direction for an encryption operation
171 * We build the 16 key entries here
174 des_setkey(uchar_t userkey
[8], struct deskeydata
*kd
, unsigned int dir
)
180 * First, generate C and D by permuting
181 * the key. The low order bit of each
182 * 8-bit char is not used, so C and D are only 28
187 const short *pcc
= PC1_C
, *pcd
= PC1_D
;
190 for (i
= 0; i
< 28; i
++) {
194 if (btst(userkey
, bit
))
197 if (btst(userkey
, bit
))
202 * To generate Ki, rotate C and D according
203 * to schedule and pick up a permutation
206 for (i
= 0; i
< 16; i
++) {
212 * Do the "left shift" (rotate)
213 * We know we always rotate by either 1 or 2 bits
214 * the shifts table tells us if its 2
231 * get Ki. Note C and D are concatenated.
236 c
= &kd
->keyval
[i
]; break;
238 c
= &kd
->keyval
[15 - i
]; break;
242 bbit
= (1 << 5) << 24;
243 for (j
= 0; j
< 4; j
++) {
244 for (k
= 0; k
< 6; k
++) {
245 if (C
& (BIT28
>> PC2_C
[bit
]))
246 c
->long0
|= bbit
>> k
;
247 if (D
& (BIT28
>> PC2_D
[bit
]))
248 c
->long1
|= bbit
>> k
;
260 * Do an encryption operation
261 * Much pain is taken (with preprocessor) to avoid loops so the compiler
262 * can do address arithmetic instead of doing it at runtime.
263 * Note that the byte-to-chunk conversion is necessary to guarantee
264 * processor byte-order independence.
267 des_encrypt(uchar_t
*data
, struct deskeydata
*kd
)
269 chunk_t work1
, work2
;
272 * Initial permutation
273 * and byte to chunk conversion
280 work1
.byte0
= data
[0];
281 work1
.byte1
= data
[1];
282 work1
.byte2
= data
[2];
283 work1
.byte3
= data
[3];
284 work1
.byte4
= data
[4];
285 work1
.byte5
= data
[5];
286 work1
.byte6
= data
[6];
287 work1
.byte7
= data
[7];
290 for (lp
= &longtab
[0], i
= 0; i
< 32; i
++) {
296 l1
|= longtab
[pbit
-32];
300 for (lp
= &longtab
[0], i
= 32; i
< 64; i
++) {
306 l1
|= longtab
[pbit
-32];
314 * Expand 8 bits of 32 bit R to 48 bit R
316 #define do_R_to_ER(op, b) { \
317 const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b]; \
323 * Inner part of the algorithm:
324 * Expand R from 32 to 48 bits; xor key value;
325 * apply S boxes; permute 32 bits of output
327 #define do_F(iter, inR, outR) { \
339 ER.long0 = e0 ^ kd->keyval[iter].long0; \
340 ER.long1 = e1 ^ kd->keyval[iter].long1; \
342 S_tab[0][ER.byte0] + \
343 S_tab[1][ER.byte1] + \
344 S_tab[2][ER.byte2] + \
345 S_tab[3][ER.byte3] + \
346 S_tab[4][ER.byte4] + \
347 S_tab[5][ER.byte5] + \
348 S_tab[6][ER.byte6] + \
349 S_tab[7][ER.byte7]; \
351 P_tab[0][R.byte0] + \
352 P_tab[1][R.byte1] + \
353 P_tab[2][R.byte2] + \
359 * Apply inner part; do xor and exchange of 32 bit parts
361 #define cipher(iter, inR, inL, outR, outL) { \
362 do_F(iter, inR, outR); \
368 * Apply the 16 ciphering steps
371 uint32_t r0
, l0
, r1
, l1
;
375 cipher(0, r0
, l0
, r1
, l1
);
376 cipher(1, r1
, l1
, r0
, l0
);
377 cipher(2, r0
, l0
, r1
, l1
);
378 cipher(3, r1
, l1
, r0
, l0
);
379 cipher(4, r0
, l0
, r1
, l1
);
380 cipher(5, r1
, l1
, r0
, l0
);
381 cipher(6, r0
, l0
, r1
, l1
);
382 cipher(7, r1
, l1
, r0
, l0
);
383 cipher(8, r0
, l0
, r1
, l1
);
384 cipher(9, r1
, l1
, r0
, l0
);
385 cipher(10, r0
, l0
, r1
, l1
);
386 cipher(11, r1
, l1
, r0
, l0
);
387 cipher(12, r0
, l0
, r1
, l1
);
388 cipher(13, r1
, l1
, r0
, l0
);
389 cipher(14, r0
, l0
, r1
, l1
);
390 cipher(15, r1
, l1
, r0
, l0
);
397 * and chunk to byte conversion
406 for (lp
= &longtab
[0], i
= 0; i
< 32; i
++) {
412 l1
|= longtab
[pbit
-32];
416 for (lp
= &longtab
[0], i
= 32; i
< 64; i
++) {
422 l1
|= longtab
[pbit
-32];
428 data
[0] = work2
.byte0
;
429 data
[1] = work2
.byte1
;
430 data
[2] = work2
.byte2
;
431 data
[3] = work2
.byte3
;
432 data
[4] = work2
.byte4
;
433 data
[5] = work2
.byte5
;
434 data
[6] = work2
.byte6
;
435 data
[7] = work2
.byte7
;
437 #endif /* def CRYPT */