xref: /illumos-gate/usr/src/lib/libnsl/des/des_soft.c (revision 61961e0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Warning!  Things are arranged very carefully in this file to
407c478bd9Sstevel@tonic-gate  * allow read-only data to be moved to the text segment.  The
417c478bd9Sstevel@tonic-gate  * various DES tables must appear before any function definitions
427c478bd9Sstevel@tonic-gate  * (this is arranged by including them immediately below) and partab
437c478bd9Sstevel@tonic-gate  * must also appear before and function definitions
447c478bd9Sstevel@tonic-gate  * This arrangement allows all data up through the first text to
457c478bd9Sstevel@tonic-gate  * be moved to text.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <des/softdes.h>
507c478bd9Sstevel@tonic-gate #include <des/desdata.h>
517c478bd9Sstevel@tonic-gate #ifdef sun
527c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
537c478bd9Sstevel@tonic-gate #include <sys/des.h>
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate #include <des/des.h>
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Fast (?) software implementation of DES
617c478bd9Sstevel@tonic-gate  * Has been seen going at 2000 bytes/sec on a Sun-2
627c478bd9Sstevel@tonic-gate  * Works on a VAX too.
637c478bd9Sstevel@tonic-gate  * Won't work without 8 bit chars and 32 bit longs
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	btst(k, b)	(k[b >> 3] & (0x80 >> (b & 07)))
677c478bd9Sstevel@tonic-gate #define	BIT28	(1<<28)
687c478bd9Sstevel@tonic-gate 
69*61961e0fSrobinson static int	__des_encrypt(uchar_t *, struct deskeydata *);
70*61961e0fSrobinson static int	__des_setkey(uchar_t[8], struct deskeydata *, unsigned);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Table giving odd parity in the low bit for ASCII characters
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate const char partab[128] = {
777c478bd9Sstevel@tonic-gate 	0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
787c478bd9Sstevel@tonic-gate 	0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
797c478bd9Sstevel@tonic-gate 	0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
807c478bd9Sstevel@tonic-gate 	0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
817c478bd9Sstevel@tonic-gate 	0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
827c478bd9Sstevel@tonic-gate 	0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
837c478bd9Sstevel@tonic-gate 	0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
847c478bd9Sstevel@tonic-gate 	0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
857c478bd9Sstevel@tonic-gate 	0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
867c478bd9Sstevel@tonic-gate 	0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
877c478bd9Sstevel@tonic-gate 	0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
887c478bd9Sstevel@tonic-gate 	0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
897c478bd9Sstevel@tonic-gate 	0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
907c478bd9Sstevel@tonic-gate 	0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
917c478bd9Sstevel@tonic-gate 	0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
927c478bd9Sstevel@tonic-gate 	0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Add odd parity to low bit of 8 byte key
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate void
99*61961e0fSrobinson des_setparity(char *p)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int i;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
1047c478bd9Sstevel@tonic-gate 		*p = partab[*p & 0x7f];
1057c478bd9Sstevel@tonic-gate 		p++;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static const unsigned char partab_g[256] = {
1107c478bd9Sstevel@tonic-gate 	0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
1117c478bd9Sstevel@tonic-gate 	0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
1127c478bd9Sstevel@tonic-gate 	0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
1137c478bd9Sstevel@tonic-gate 	0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
1147c478bd9Sstevel@tonic-gate 	0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
1157c478bd9Sstevel@tonic-gate 	0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
1167c478bd9Sstevel@tonic-gate 	0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
1177c478bd9Sstevel@tonic-gate 	0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
1187c478bd9Sstevel@tonic-gate 	0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
1197c478bd9Sstevel@tonic-gate 	0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
1207c478bd9Sstevel@tonic-gate 	0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
1217c478bd9Sstevel@tonic-gate 	0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
1227c478bd9Sstevel@tonic-gate 	0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
1237c478bd9Sstevel@tonic-gate 	0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
1247c478bd9Sstevel@tonic-gate 	0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
1257c478bd9Sstevel@tonic-gate 	0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
1267c478bd9Sstevel@tonic-gate 	0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86,
1277c478bd9Sstevel@tonic-gate 	0x89, 0x89, 0x8a, 0x8a, 0x8c, 0x8c, 0x8f, 0x8f,
1287c478bd9Sstevel@tonic-gate 	0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97,
1297c478bd9Sstevel@tonic-gate 	0x98, 0x98, 0x9b, 0x9b, 0x9d, 0x9d, 0x9e, 0x9e,
1307c478bd9Sstevel@tonic-gate 	0xa1, 0xa1, 0xa2, 0xa2, 0xa4, 0xa4, 0xa7, 0xa7,
1317c478bd9Sstevel@tonic-gate 	0xa8, 0xa8, 0xab, 0xab, 0xad, 0xad, 0xae, 0xae,
1327c478bd9Sstevel@tonic-gate 	0xb0, 0xb0, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb6,
1337c478bd9Sstevel@tonic-gate 	0xb9, 0xb9, 0xba, 0xba, 0xbc, 0xbc, 0xbf, 0xbf,
1347c478bd9Sstevel@tonic-gate 	0xc1, 0xc1, 0xc2, 0xc2, 0xc4, 0xc4, 0xc7, 0xc7,
1357c478bd9Sstevel@tonic-gate 	0xc8, 0xc8, 0xcb, 0xcb, 0xcd, 0xcd, 0xce, 0xce,
1367c478bd9Sstevel@tonic-gate 	0xd0, 0xd0, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd6,
1377c478bd9Sstevel@tonic-gate 	0xd9, 0xd9, 0xda, 0xda, 0xdc, 0xdc, 0xdf, 0xdf,
1387c478bd9Sstevel@tonic-gate 	0xe0, 0xe0, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe6,
1397c478bd9Sstevel@tonic-gate 	0xe9, 0xe9, 0xea, 0xea, 0xec, 0xec, 0xef, 0xef,
1407c478bd9Sstevel@tonic-gate 	0xf1, 0xf1, 0xf2, 0xf2, 0xf4, 0xf4, 0xf7, 0xf7,
1417c478bd9Sstevel@tonic-gate 	0xf8, 0xf8, 0xfb, 0xfb, 0xfd, 0xfd, 0xfe, 0xfe
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * A corrected version of des_setparity (see bug 1149767).
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate void
1487c478bd9Sstevel@tonic-gate des_setparity_g(des_block *p)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int i;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
1537c478bd9Sstevel@tonic-gate 		(*p).c[i] = partab_g[(*p).c[i]];
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Software encrypt or decrypt a block of data (multiple of 8 bytes)
1597c478bd9Sstevel@tonic-gate  * Do the CBC ourselves if needed.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate int
162*61961e0fSrobinson __des_crypt(char *buf, unsigned len, struct desparams *desp)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
165*61961e0fSrobinson 	short i;
166*61961e0fSrobinson 	unsigned mode;
167*61961e0fSrobinson 	unsigned dir;
1687c478bd9Sstevel@tonic-gate 	char nextiv[8];
1697c478bd9Sstevel@tonic-gate 	struct deskeydata softkey;
1707c478bd9Sstevel@tonic-gate 
171*61961e0fSrobinson 	mode = (unsigned)desp->des_mode;
172*61961e0fSrobinson 	dir = (unsigned)desp->des_dir;
173*61961e0fSrobinson 	(void) __des_setkey(desp->des_key, &softkey, dir);
1747c478bd9Sstevel@tonic-gate 	while (len != 0) {
1757c478bd9Sstevel@tonic-gate 		switch (mode) {
1767c478bd9Sstevel@tonic-gate 		case CBC:
1777c478bd9Sstevel@tonic-gate 			switch (dir) {
1787c478bd9Sstevel@tonic-gate 			case ENCRYPT:
1797c478bd9Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1807c478bd9Sstevel@tonic-gate 					buf[i] ^= desp->des_ivec[i];
181*61961e0fSrobinson 				(void) __des_encrypt((uchar_t *)buf, &softkey);
1827c478bd9Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1837c478bd9Sstevel@tonic-gate 					desp->des_ivec[i] = buf[i];
1847c478bd9Sstevel@tonic-gate 				break;
1857c478bd9Sstevel@tonic-gate 			case DECRYPT:
1867c478bd9Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1877c478bd9Sstevel@tonic-gate 					nextiv[i] = buf[i];
188*61961e0fSrobinson 				(void) __des_encrypt((uchar_t *)buf, &softkey);
1897c478bd9Sstevel@tonic-gate 				for (i = 0; i < 8; i++) {
1907c478bd9Sstevel@tonic-gate 					buf[i] ^= desp->des_ivec[i];
1917c478bd9Sstevel@tonic-gate 					desp->des_ivec[i] = nextiv[i];
1927c478bd9Sstevel@tonic-gate 				}
1937c478bd9Sstevel@tonic-gate 				break;
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		case ECB:
197*61961e0fSrobinson 			(void) __des_encrypt((uchar_t *)buf, &softkey);
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		buf += 8;
2017c478bd9Sstevel@tonic-gate 		len -= 8;
2027c478bd9Sstevel@tonic-gate 	}
203*61961e0fSrobinson /* EXPORT DELETE END */
2047c478bd9Sstevel@tonic-gate 	return (1);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Set the key and direction for an encryption operation
2107c478bd9Sstevel@tonic-gate  * We build the 16 key entries here
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int
213*61961e0fSrobinson __des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned dir)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
2167c478bd9Sstevel@tonic-gate 	int32_t C, D;
217*61961e0fSrobinson 	short i;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * First, generate C and D by permuting
2217c478bd9Sstevel@tonic-gate 	 * the key. The low order bit of each
2227c478bd9Sstevel@tonic-gate 	 * 8-bit char is not used, so C and D are only 28
2237c478bd9Sstevel@tonic-gate 	 * bits apiece.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	{
226*61961e0fSrobinson 		short bit;
227*61961e0fSrobinson 		const short *pcc = PC1_C, *pcd = PC1_D;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		C = D = 0;
2307c478bd9Sstevel@tonic-gate 		for (i = 0; i < 28; i++) {
2317c478bd9Sstevel@tonic-gate 			C <<= 1;
2327c478bd9Sstevel@tonic-gate 			D <<= 1;
2337c478bd9Sstevel@tonic-gate 			bit = *pcc++;
2347c478bd9Sstevel@tonic-gate 			if (btst(userkey, bit))
2357c478bd9Sstevel@tonic-gate 				C |= 1;
2367c478bd9Sstevel@tonic-gate 			bit = *pcd++;
2377c478bd9Sstevel@tonic-gate 			if (btst(userkey, bit))
2387c478bd9Sstevel@tonic-gate 				D |= 1;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * To generate Ki, rotate C and D according
2437c478bd9Sstevel@tonic-gate 	 * to schedule and pick up a permutation
2447c478bd9Sstevel@tonic-gate 	 * using PC2.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
247*61961e0fSrobinson 		chunk_t *c;
248*61961e0fSrobinson 		short j, k, bit;
2497c478bd9Sstevel@tonic-gate 		uint32_t bbit;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		/*
2527c478bd9Sstevel@tonic-gate 		 * Do the "left shift" (rotate)
2537c478bd9Sstevel@tonic-gate 		 * We know we always rotate by either 1 or 2 bits
2547c478bd9Sstevel@tonic-gate 		 * the shifts table tells us if its 2
2557c478bd9Sstevel@tonic-gate 		 */
2567c478bd9Sstevel@tonic-gate 		C <<= 1;
2577c478bd9Sstevel@tonic-gate 		if (C & BIT28)
2587c478bd9Sstevel@tonic-gate 			C |= 1;
2597c478bd9Sstevel@tonic-gate 		D <<= 1;
2607c478bd9Sstevel@tonic-gate 		if (D & BIT28)
2617c478bd9Sstevel@tonic-gate 			D |= 1;
2627c478bd9Sstevel@tonic-gate 		if (shifts[i]) {
2637c478bd9Sstevel@tonic-gate 			C <<= 1;
2647c478bd9Sstevel@tonic-gate 			if (C & BIT28)
2657c478bd9Sstevel@tonic-gate 				C |= 1;
2667c478bd9Sstevel@tonic-gate 			D <<= 1;
2677c478bd9Sstevel@tonic-gate 			if (D & BIT28)
2687c478bd9Sstevel@tonic-gate 				D |= 1;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * get Ki. Note C and D are concatenated.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		bit = 0;
2747c478bd9Sstevel@tonic-gate 		switch (dir) {
2757c478bd9Sstevel@tonic-gate 		case ENCRYPT:
2767c478bd9Sstevel@tonic-gate 			c = &kd->keyval[i]; break;
2777c478bd9Sstevel@tonic-gate 		case DECRYPT:
2787c478bd9Sstevel@tonic-gate 			c = &kd->keyval[15 - i]; break;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		c->long0 = 0;
2817c478bd9Sstevel@tonic-gate 		c->long1 = 0;
2827c478bd9Sstevel@tonic-gate 		bbit = (1 << 5) << 24;
2837c478bd9Sstevel@tonic-gate 		for (j = 0; j < 4; j++) {
2847c478bd9Sstevel@tonic-gate 			for (k = 0; k < 6; k++) {
2857c478bd9Sstevel@tonic-gate 				if (C & (BIT28 >> PC2_C[bit]))
2867c478bd9Sstevel@tonic-gate 					c->long0 |= bbit >> k;
2877c478bd9Sstevel@tonic-gate 				if (D & (BIT28 >> PC2_D[bit]))
2887c478bd9Sstevel@tonic-gate 					c->long1 |= bbit >> k;
2897c478bd9Sstevel@tonic-gate 				bit++;
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 			bbit >>= 8;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
2967c478bd9Sstevel@tonic-gate 	return (1);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * Do an encryption operation
3037c478bd9Sstevel@tonic-gate  * Much pain is taken (with preprocessor) to avoid loops so the compiler
3047c478bd9Sstevel@tonic-gate  * can do address arithmetic instead of doing it at runtime.
3057c478bd9Sstevel@tonic-gate  * Note that the byte-to-chunk conversion is necessary to guarantee
3067c478bd9Sstevel@tonic-gate  * processor byte-order independence.
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate static int
309*61961e0fSrobinson __des_encrypt(uchar_t *data, struct deskeydata *kd)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
3127c478bd9Sstevel@tonic-gate 	chunk_t work1, work2;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Initial permutation
3167c478bd9Sstevel@tonic-gate 	 * and byte to chunk conversion
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	{
3197c478bd9Sstevel@tonic-gate 		const uint32_t *lp;
3207c478bd9Sstevel@tonic-gate 		uint32_t l0, l1, w;
321*61961e0fSrobinson 		short i, pbit;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		work1.byte0 = data[0];
3247c478bd9Sstevel@tonic-gate 		work1.byte1 = data[1];
3257c478bd9Sstevel@tonic-gate 		work1.byte2 = data[2];
3267c478bd9Sstevel@tonic-gate 		work1.byte3 = data[3];
3277c478bd9Sstevel@tonic-gate 		work1.byte4 = data[4];
3287c478bd9Sstevel@tonic-gate 		work1.byte5 = data[5];
3297c478bd9Sstevel@tonic-gate 		work1.byte6 = data[6];
3307c478bd9Sstevel@tonic-gate 		work1.byte7 = data[7];
3317c478bd9Sstevel@tonic-gate 		l0 = l1 = 0;
3327c478bd9Sstevel@tonic-gate 		w = work1.long0;
3337c478bd9Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
3347c478bd9Sstevel@tonic-gate 			if (w & *lp++) {
3357c478bd9Sstevel@tonic-gate 				pbit = IPtab[i];
3367c478bd9Sstevel@tonic-gate 				if (pbit < 32)
3377c478bd9Sstevel@tonic-gate 					l0 |= longtab[pbit];
3387c478bd9Sstevel@tonic-gate 				else
3397c478bd9Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
3407c478bd9Sstevel@tonic-gate 			}
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		w = work1.long1;
3437c478bd9Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
3447c478bd9Sstevel@tonic-gate 			if (w & *lp++) {
3457c478bd9Sstevel@tonic-gate 				pbit = IPtab[i];
3467c478bd9Sstevel@tonic-gate 				if (pbit < 32)
3477c478bd9Sstevel@tonic-gate 					l0 |= longtab[pbit];
3487c478bd9Sstevel@tonic-gate 				else
3497c478bd9Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 		work2.long0 = l0;
3537c478bd9Sstevel@tonic-gate 		work2.long1 = l1;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * Expand 8 bits of 32 bit R to 48 bit R
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate #define	do_R_to_ER(op, b)	{			\
360*61961e0fSrobinson 	const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b];	\
3617c478bd9Sstevel@tonic-gate 	e0 op p->l0;				\
3627c478bd9Sstevel@tonic-gate 	e1 op p->l1;				\
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * Inner part of the algorithm:
3677c478bd9Sstevel@tonic-gate  * Expand R from 32 to 48 bits; xor key value;
3687c478bd9Sstevel@tonic-gate  * apply S boxes; permute 32 bits of output
3697c478bd9Sstevel@tonic-gate  */
370*61961e0fSrobinson /* BEGIN CSTYLED */
3717c478bd9Sstevel@tonic-gate #define	do_F(iter, inR, outR) 	{			\
3727c478bd9Sstevel@tonic-gate 	chunk_t R, ER;					\
3737c478bd9Sstevel@tonic-gate 	uint32_t e0, e1;				\
3747c478bd9Sstevel@tonic-gate 	R.long0 = inR;					\
3757c478bd9Sstevel@tonic-gate 	do_R_to_ER(=, 0);				\
3767c478bd9Sstevel@tonic-gate 	do_R_to_ER(|=, 1);				\
3777c478bd9Sstevel@tonic-gate 	do_R_to_ER(|=, 2);				\
3787c478bd9Sstevel@tonic-gate 	do_R_to_ER(|=, 3);				\
3797c478bd9Sstevel@tonic-gate 	ER.long0 = e0 ^ kd->keyval[iter].long0;		\
3807c478bd9Sstevel@tonic-gate 	ER.long1 = e1 ^ kd->keyval[iter].long1;		\
3817c478bd9Sstevel@tonic-gate 	R.long0 =					\
3827c478bd9Sstevel@tonic-gate 		S_tab[0][ER.byte0] +			\
3837c478bd9Sstevel@tonic-gate 		S_tab[1][ER.byte1] +			\
3847c478bd9Sstevel@tonic-gate 		S_tab[2][ER.byte2] +			\
3857c478bd9Sstevel@tonic-gate 		S_tab[3][ER.byte3] +			\
3867c478bd9Sstevel@tonic-gate 		S_tab[4][ER.byte4] +			\
3877c478bd9Sstevel@tonic-gate 		S_tab[5][ER.byte5] +			\
3887c478bd9Sstevel@tonic-gate 		S_tab[6][ER.byte6] +			\
3897c478bd9Sstevel@tonic-gate 		S_tab[7][ER.byte7]; 			\
3907c478bd9Sstevel@tonic-gate 	outR =						\
3917c478bd9Sstevel@tonic-gate 		P_tab[0][R.byte0] +			\
3927c478bd9Sstevel@tonic-gate 		P_tab[1][R.byte1] +			\
3937c478bd9Sstevel@tonic-gate 		P_tab[2][R.byte2] +			\
3947c478bd9Sstevel@tonic-gate 		P_tab[3][R.byte3]; 			\
3957c478bd9Sstevel@tonic-gate }
396*61961e0fSrobinson /* END CSTYLED */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * Do a cipher step
4007c478bd9Sstevel@tonic-gate  * Apply inner part; do xor and exchange of 32 bit parts
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate #define	cipher(iter, inR, inL, outR, outL)	{	\
4037c478bd9Sstevel@tonic-gate 	do_F(iter, inR, outR);				\
4047c478bd9Sstevel@tonic-gate 	outR ^= inL;					\
4057c478bd9Sstevel@tonic-gate 	outL = inR;					\
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Apply the 16 ciphering steps
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	{
4127c478bd9Sstevel@tonic-gate 		uint32_t r0, l0, r1, l1;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		l0 = work2.long0;
4157c478bd9Sstevel@tonic-gate 		r0 = work2.long1;
4167c478bd9Sstevel@tonic-gate 		cipher(0, r0, l0, r1, l1);
4177c478bd9Sstevel@tonic-gate 		cipher(1, r1, l1, r0, l0);
4187c478bd9Sstevel@tonic-gate 		cipher(2, r0, l0, r1, l1);
4197c478bd9Sstevel@tonic-gate 		cipher(3, r1, l1, r0, l0);
4207c478bd9Sstevel@tonic-gate 		cipher(4, r0, l0, r1, l1);
4217c478bd9Sstevel@tonic-gate 		cipher(5, r1, l1, r0, l0);
4227c478bd9Sstevel@tonic-gate 		cipher(6, r0, l0, r1, l1);
4237c478bd9Sstevel@tonic-gate 		cipher(7, r1, l1, r0, l0);
4247c478bd9Sstevel@tonic-gate 		cipher(8, r0, l0, r1, l1);
4257c478bd9Sstevel@tonic-gate 		cipher(9, r1, l1, r0, l0);
4267c478bd9Sstevel@tonic-gate 		cipher(10, r0, l0, r1, l1);
4277c478bd9Sstevel@tonic-gate 		cipher(11, r1, l1, r0, l0);
4287c478bd9Sstevel@tonic-gate 		cipher(12, r0, l0, r1, l1);
4297c478bd9Sstevel@tonic-gate 		cipher(13, r1, l1, r0, l0);
4307c478bd9Sstevel@tonic-gate 		cipher(14, r0, l0, r1, l1);
4317c478bd9Sstevel@tonic-gate 		cipher(15, r1, l1, r0, l0);
4327c478bd9Sstevel@tonic-gate 		work1.long0 = r0;
4337c478bd9Sstevel@tonic-gate 		work1.long1 = l0;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Final permutation
4387c478bd9Sstevel@tonic-gate 	 * and chunk to byte conversion
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	{
4417c478bd9Sstevel@tonic-gate 		uint32_t *lp;
4427c478bd9Sstevel@tonic-gate 		uint32_t l0, l1, w;
443*61961e0fSrobinson 		short i, pbit;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		l0 = l1 = 0;
4467c478bd9Sstevel@tonic-gate 		w = work1.long0;
4477c478bd9Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
4487c478bd9Sstevel@tonic-gate 			if (w & *lp++) {
4497c478bd9Sstevel@tonic-gate 				pbit = FPtab[i];
4507c478bd9Sstevel@tonic-gate 				if (pbit < 32)
4517c478bd9Sstevel@tonic-gate 					l0 |= longtab[pbit];
4527c478bd9Sstevel@tonic-gate 				else
4537c478bd9Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		w = work1.long1;
4577c478bd9Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
4587c478bd9Sstevel@tonic-gate 			if (w & *lp++) {
4597c478bd9Sstevel@tonic-gate 				pbit = FPtab[i];
4607c478bd9Sstevel@tonic-gate 				if (pbit < 32)
4617c478bd9Sstevel@tonic-gate 					l0 |= longtab[pbit];
4627c478bd9Sstevel@tonic-gate 				else
4637c478bd9Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		work2.long0 = l0;
4677c478bd9Sstevel@tonic-gate 		work2.long1 = l1;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	data[0] = work2.byte0;
4707c478bd9Sstevel@tonic-gate 	data[1] = work2.byte1;
4717c478bd9Sstevel@tonic-gate 	data[2] = work2.byte2;
4727c478bd9Sstevel@tonic-gate 	data[3] = work2.byte3;
4737c478bd9Sstevel@tonic-gate 	data[4] = work2.byte4;
4747c478bd9Sstevel@tonic-gate 	data[5] = work2.byte5;
4757c478bd9Sstevel@tonic-gate 	data[6] = work2.byte6;
4767c478bd9Sstevel@tonic-gate 	data[7] = work2.byte7;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
4797c478bd9Sstevel@tonic-gate 	return (1);
4807c478bd9Sstevel@tonic-gate }
481