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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * Portions of this source code were derived from Berkeley 4.3 BSD
33 * under license from the Regents of the University of California.
37 * Warning! Things are arranged very carefully in this file to
38 * allow read-only data to be moved to the text segment. The
39 * various DES tables must appear before any function definitions
40 * (this is arranged by including them immediately below) and partab
41 * must also appear before and function definitions
42 * This arrangement allows all data up through the first text to
47 #include <sys/types.h>
48 #include <des/softdes.h>
49 #include <des/desdata.h>
51 #include <sys/ioctl.h>
56 #include <rpcsvc/nis_dhext.h>
59 * Fast (?) software implementation of DES
60 * Has been seen going at 2000 bytes/sec on a Sun-2
62 * Won't work without 8 bit chars and 32 bit longs
65 #define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
68 static int __des_encrypt(uchar_t
*, struct deskeydata
*);
69 static int __des_setkey(uchar_t
[8], struct deskeydata
*, unsigned);
73 * Table giving odd parity in the low bit for ASCII characters
75 const char partab
[128] = {
76 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
77 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
78 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
79 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
80 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
81 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
82 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
83 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
84 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
85 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
86 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
87 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
88 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
89 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
90 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
91 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
95 * Add odd parity to low bit of 8 byte key
98 des_setparity(char *p
)
102 for (i
= 0; i
< 8; i
++) {
103 *p
= partab
[*p
& 0x7f];
108 static const unsigned char partab_g
[256] = {
109 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
110 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
111 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
112 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
113 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
114 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
115 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
116 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
117 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
118 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
119 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
120 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
121 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
122 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
123 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
124 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
125 0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86,
126 0x89, 0x89, 0x8a, 0x8a, 0x8c, 0x8c, 0x8f, 0x8f,
127 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97,
128 0x98, 0x98, 0x9b, 0x9b, 0x9d, 0x9d, 0x9e, 0x9e,
129 0xa1, 0xa1, 0xa2, 0xa2, 0xa4, 0xa4, 0xa7, 0xa7,
130 0xa8, 0xa8, 0xab, 0xab, 0xad, 0xad, 0xae, 0xae,
131 0xb0, 0xb0, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb6,
132 0xb9, 0xb9, 0xba, 0xba, 0xbc, 0xbc, 0xbf, 0xbf,
133 0xc1, 0xc1, 0xc2, 0xc2, 0xc4, 0xc4, 0xc7, 0xc7,
134 0xc8, 0xc8, 0xcb, 0xcb, 0xcd, 0xcd, 0xce, 0xce,
135 0xd0, 0xd0, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd6,
136 0xd9, 0xd9, 0xda, 0xda, 0xdc, 0xdc, 0xdf, 0xdf,
137 0xe0, 0xe0, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe6,
138 0xe9, 0xe9, 0xea, 0xea, 0xec, 0xec, 0xef, 0xef,
139 0xf1, 0xf1, 0xf2, 0xf2, 0xf4, 0xf4, 0xf7, 0xf7,
140 0xf8, 0xf8, 0xfb, 0xfb, 0xfd, 0xfd, 0xfe, 0xfe
144 * A corrected version of des_setparity (see bug 1149767).
147 des_setparity_g(des_block
*p
)
151 for (i
= 0; i
< 8; i
++) {
152 (*p
).c
[i
] = partab_g
[(*p
).c
[i
]];
157 * Software encrypt or decrypt a block of data (multiple of 8 bytes)
158 * Do the CBC ourselves if needed.
161 __des_crypt(char *buf
, unsigned len
, struct desparams
*desp
)
167 struct deskeydata softkey
;
169 mode
= (unsigned)desp
->des_mode
;
170 dir
= (unsigned)desp
->des_dir
;
171 (void) __des_setkey(desp
->des_key
, &softkey
, dir
);
177 for (i
= 0; i
< 8; i
++)
178 buf
[i
] ^= desp
->des_ivec
[i
];
179 (void) __des_encrypt((uchar_t
*)buf
, &softkey
);
180 for (i
= 0; i
< 8; i
++)
181 desp
->des_ivec
[i
] = buf
[i
];
184 for (i
= 0; i
< 8; i
++)
186 (void) __des_encrypt((uchar_t
*)buf
, &softkey
);
187 for (i
= 0; i
< 8; i
++) {
188 buf
[i
] ^= desp
->des_ivec
[i
];
189 desp
->des_ivec
[i
] = nextiv
[i
];
195 (void) __des_encrypt((uchar_t
*)buf
, &softkey
);
206 * Set the key and direction for an encryption operation
207 * We build the 16 key entries here
210 __des_setkey(uchar_t userkey
[8], struct deskeydata
*kd
, unsigned dir
)
216 * First, generate C and D by permuting
217 * the key. The low order bit of each
218 * 8-bit char is not used, so C and D are only 28
223 const short *pcc
= PC1_C
, *pcd
= PC1_D
;
226 for (i
= 0; i
< 28; i
++) {
230 if (btst(userkey
, bit
))
233 if (btst(userkey
, bit
))
238 * To generate Ki, rotate C and D according
239 * to schedule and pick up a permutation
242 for (i
= 0; i
< 16; i
++) {
248 * Do the "left shift" (rotate)
249 * We know we always rotate by either 1 or 2 bits
250 * the shifts table tells us if its 2
267 * get Ki. Note C and D are concatenated.
272 c
= &kd
->keyval
[i
]; break;
274 c
= &kd
->keyval
[15 - i
]; break;
278 bbit
= (1 << 5) << 24;
279 for (j
= 0; j
< 4; j
++) {
280 for (k
= 0; k
< 6; k
++) {
281 if (C
& (BIT28
>> PC2_C
[bit
]))
282 c
->long0
|= bbit
>> k
;
283 if (D
& (BIT28
>> PC2_D
[bit
]))
284 c
->long1
|= bbit
>> k
;
297 * Do an encryption operation
298 * Much pain is taken (with preprocessor) to avoid loops so the compiler
299 * can do address arithmetic instead of doing it at runtime.
300 * Note that the byte-to-chunk conversion is necessary to guarantee
301 * processor byte-order independence.
304 __des_encrypt(uchar_t
*data
, struct deskeydata
*kd
)
306 chunk_t work1
, work2
;
309 * Initial permutation
310 * and byte to chunk conversion
317 work1
.byte0
= data
[0];
318 work1
.byte1
= data
[1];
319 work1
.byte2
= data
[2];
320 work1
.byte3
= data
[3];
321 work1
.byte4
= data
[4];
322 work1
.byte5
= data
[5];
323 work1
.byte6
= data
[6];
324 work1
.byte7
= data
[7];
327 for (lp
= (uint32_t *)&longtab
[0], i
= 0; i
< 32; i
++) {
333 l1
|= longtab
[pbit
-32];
337 for (lp
= (uint32_t *)&longtab
[0], i
= 32; i
< 64; i
++) {
343 l1
|= longtab
[pbit
-32];
351 * Expand 8 bits of 32 bit R to 48 bit R
353 #define do_R_to_ER(op, b) { \
354 const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b]; \
360 * Inner part of the algorithm:
361 * Expand R from 32 to 48 bits; xor key value;
362 * apply S boxes; permute 32 bits of output
365 #define do_F(iter, inR, outR) { \
373 ER.long0 = e0 ^ kd->keyval[iter].long0; \
374 ER.long1 = e1 ^ kd->keyval[iter].long1; \
376 S_tab[0][ER.byte0] + \
377 S_tab[1][ER.byte1] + \
378 S_tab[2][ER.byte2] + \
379 S_tab[3][ER.byte3] + \
380 S_tab[4][ER.byte4] + \
381 S_tab[5][ER.byte5] + \
382 S_tab[6][ER.byte6] + \
383 S_tab[7][ER.byte7]; \
385 P_tab[0][R.byte0] + \
386 P_tab[1][R.byte1] + \
387 P_tab[2][R.byte2] + \
394 * Apply inner part; do xor and exchange of 32 bit parts
396 #define cipher(iter, inR, inL, outR, outL) { \
397 do_F(iter, inR, outR); \
403 * Apply the 16 ciphering steps
406 uint32_t r0
, l0
, r1
, l1
;
410 cipher(0, r0
, l0
, r1
, l1
);
411 cipher(1, r1
, l1
, r0
, l0
);
412 cipher(2, r0
, l0
, r1
, l1
);
413 cipher(3, r1
, l1
, r0
, l0
);
414 cipher(4, r0
, l0
, r1
, l1
);
415 cipher(5, r1
, l1
, r0
, l0
);
416 cipher(6, r0
, l0
, r1
, l1
);
417 cipher(7, r1
, l1
, r0
, l0
);
418 cipher(8, r0
, l0
, r1
, l1
);
419 cipher(9, r1
, l1
, r0
, l0
);
420 cipher(10, r0
, l0
, r1
, l1
);
421 cipher(11, r1
, l1
, r0
, l0
);
422 cipher(12, r0
, l0
, r1
, l1
);
423 cipher(13, r1
, l1
, r0
, l0
);
424 cipher(14, r0
, l0
, r1
, l1
);
425 cipher(15, r1
, l1
, r0
, l0
);
432 * and chunk to byte conversion
441 for (lp
= (uint32_t *)&longtab
[0], i
= 0; i
< 32; i
++) {
447 l1
|= longtab
[pbit
-32];
451 for (lp
= (uint32_t *)&longtab
[0], i
= 32; i
< 64; i
++) {
457 l1
|= longtab
[pbit
-32];
463 data
[0] = work2
.byte0
;
464 data
[1] = work2
.byte1
;
465 data
[2] = work2
.byte2
;
466 data
[3] = work2
.byte3
;
467 data
[4] = work2
.byte4
;
468 data
[5] = work2
.byte5
;
469 data
[6] = work2
.byte6
;
470 data
[7] = work2
.byte7
;