xref: /onnv-gate/usr/src/lib/libnsl/des/des_soft.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
330Sstevel@tonic-gate  * under license from the Regents of the University of California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Warning!  Things are arranged very carefully in this file to
400Sstevel@tonic-gate  * allow read-only data to be moved to the text segment.  The
410Sstevel@tonic-gate  * various DES tables must appear before any function definitions
420Sstevel@tonic-gate  * (this is arranged by including them immediately below) and partab
430Sstevel@tonic-gate  * must also appear before and function definitions
440Sstevel@tonic-gate  * This arrangement allows all data up through the first text to
450Sstevel@tonic-gate  * be moved to text.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
48*1219Sraf #include "mt.h"
490Sstevel@tonic-gate #include <sys/types.h>
500Sstevel@tonic-gate #include <des/softdes.h>
510Sstevel@tonic-gate #include <des/desdata.h>
520Sstevel@tonic-gate #ifdef sun
530Sstevel@tonic-gate #include <sys/ioctl.h>
540Sstevel@tonic-gate #include <sys/des.h>
550Sstevel@tonic-gate #else
560Sstevel@tonic-gate #include <des/des.h>
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Fast (?) software implementation of DES
620Sstevel@tonic-gate  * Has been seen going at 2000 bytes/sec on a Sun-2
630Sstevel@tonic-gate  * Works on a VAX too.
640Sstevel@tonic-gate  * Won't work without 8 bit chars and 32 bit longs
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	btst(k, b)	(k[b >> 3] & (0x80 >> (b & 07)))
680Sstevel@tonic-gate #define	BIT28	(1<<28)
690Sstevel@tonic-gate 
70132Srobinson static int	__des_encrypt(uchar_t *, struct deskeydata *);
71132Srobinson static int	__des_setkey(uchar_t[8], struct deskeydata *, unsigned);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Table giving odd parity in the low bit for ASCII characters
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate const char partab[128] = {
780Sstevel@tonic-gate 	0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
790Sstevel@tonic-gate 	0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
800Sstevel@tonic-gate 	0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
810Sstevel@tonic-gate 	0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
820Sstevel@tonic-gate 	0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
830Sstevel@tonic-gate 	0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
840Sstevel@tonic-gate 	0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
850Sstevel@tonic-gate 	0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
860Sstevel@tonic-gate 	0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
870Sstevel@tonic-gate 	0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
880Sstevel@tonic-gate 	0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
890Sstevel@tonic-gate 	0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
900Sstevel@tonic-gate 	0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
910Sstevel@tonic-gate 	0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
920Sstevel@tonic-gate 	0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
930Sstevel@tonic-gate 	0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
940Sstevel@tonic-gate };
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Add odd parity to low bit of 8 byte key
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate void
des_setparity(char * p)100132Srobinson des_setparity(char *p)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	int i;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
1050Sstevel@tonic-gate 		*p = partab[*p & 0x7f];
1060Sstevel@tonic-gate 		p++;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static const unsigned char partab_g[256] = {
1110Sstevel@tonic-gate 	0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
1120Sstevel@tonic-gate 	0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
1130Sstevel@tonic-gate 	0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
1140Sstevel@tonic-gate 	0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
1150Sstevel@tonic-gate 	0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
1160Sstevel@tonic-gate 	0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
1170Sstevel@tonic-gate 	0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
1180Sstevel@tonic-gate 	0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
1190Sstevel@tonic-gate 	0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
1200Sstevel@tonic-gate 	0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
1210Sstevel@tonic-gate 	0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
1220Sstevel@tonic-gate 	0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
1230Sstevel@tonic-gate 	0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
1240Sstevel@tonic-gate 	0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
1250Sstevel@tonic-gate 	0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
1260Sstevel@tonic-gate 	0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
1270Sstevel@tonic-gate 	0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86,
1280Sstevel@tonic-gate 	0x89, 0x89, 0x8a, 0x8a, 0x8c, 0x8c, 0x8f, 0x8f,
1290Sstevel@tonic-gate 	0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97,
1300Sstevel@tonic-gate 	0x98, 0x98, 0x9b, 0x9b, 0x9d, 0x9d, 0x9e, 0x9e,
1310Sstevel@tonic-gate 	0xa1, 0xa1, 0xa2, 0xa2, 0xa4, 0xa4, 0xa7, 0xa7,
1320Sstevel@tonic-gate 	0xa8, 0xa8, 0xab, 0xab, 0xad, 0xad, 0xae, 0xae,
1330Sstevel@tonic-gate 	0xb0, 0xb0, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb6,
1340Sstevel@tonic-gate 	0xb9, 0xb9, 0xba, 0xba, 0xbc, 0xbc, 0xbf, 0xbf,
1350Sstevel@tonic-gate 	0xc1, 0xc1, 0xc2, 0xc2, 0xc4, 0xc4, 0xc7, 0xc7,
1360Sstevel@tonic-gate 	0xc8, 0xc8, 0xcb, 0xcb, 0xcd, 0xcd, 0xce, 0xce,
1370Sstevel@tonic-gate 	0xd0, 0xd0, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd6,
1380Sstevel@tonic-gate 	0xd9, 0xd9, 0xda, 0xda, 0xdc, 0xdc, 0xdf, 0xdf,
1390Sstevel@tonic-gate 	0xe0, 0xe0, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe6,
1400Sstevel@tonic-gate 	0xe9, 0xe9, 0xea, 0xea, 0xec, 0xec, 0xef, 0xef,
1410Sstevel@tonic-gate 	0xf1, 0xf1, 0xf2, 0xf2, 0xf4, 0xf4, 0xf7, 0xf7,
1420Sstevel@tonic-gate 	0xf8, 0xf8, 0xfb, 0xfb, 0xfd, 0xfd, 0xfe, 0xfe
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * A corrected version of des_setparity (see bug 1149767).
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate void
des_setparity_g(des_block * p)1490Sstevel@tonic-gate des_setparity_g(des_block *p)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	int i;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
1540Sstevel@tonic-gate 		(*p).c[i] = partab_g[(*p).c[i]];
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Software encrypt or decrypt a block of data (multiple of 8 bytes)
1600Sstevel@tonic-gate  * Do the CBC ourselves if needed.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate int
__des_crypt(char * buf,unsigned len,struct desparams * desp)163132Srobinson __des_crypt(char *buf, unsigned len, struct desparams *desp)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate /* EXPORT DELETE START */
166132Srobinson 	short i;
167132Srobinson 	unsigned mode;
168132Srobinson 	unsigned dir;
1690Sstevel@tonic-gate 	char nextiv[8];
1700Sstevel@tonic-gate 	struct deskeydata softkey;
1710Sstevel@tonic-gate 
172132Srobinson 	mode = (unsigned)desp->des_mode;
173132Srobinson 	dir = (unsigned)desp->des_dir;
174132Srobinson 	(void) __des_setkey(desp->des_key, &softkey, dir);
1750Sstevel@tonic-gate 	while (len != 0) {
1760Sstevel@tonic-gate 		switch (mode) {
1770Sstevel@tonic-gate 		case CBC:
1780Sstevel@tonic-gate 			switch (dir) {
1790Sstevel@tonic-gate 			case ENCRYPT:
1800Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1810Sstevel@tonic-gate 					buf[i] ^= desp->des_ivec[i];
182132Srobinson 				(void) __des_encrypt((uchar_t *)buf, &softkey);
1830Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1840Sstevel@tonic-gate 					desp->des_ivec[i] = buf[i];
1850Sstevel@tonic-gate 				break;
1860Sstevel@tonic-gate 			case DECRYPT:
1870Sstevel@tonic-gate 				for (i = 0; i < 8; i++)
1880Sstevel@tonic-gate 					nextiv[i] = buf[i];
189132Srobinson 				(void) __des_encrypt((uchar_t *)buf, &softkey);
1900Sstevel@tonic-gate 				for (i = 0; i < 8; i++) {
1910Sstevel@tonic-gate 					buf[i] ^= desp->des_ivec[i];
1920Sstevel@tonic-gate 					desp->des_ivec[i] = nextiv[i];
1930Sstevel@tonic-gate 				}
1940Sstevel@tonic-gate 				break;
1950Sstevel@tonic-gate 			}
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		case ECB:
198132Srobinson 			(void) __des_encrypt((uchar_t *)buf, &softkey);
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		buf += 8;
2020Sstevel@tonic-gate 		len -= 8;
2030Sstevel@tonic-gate 	}
204132Srobinson /* EXPORT DELETE END */
2050Sstevel@tonic-gate 	return (1);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Set the key and direction for an encryption operation
2110Sstevel@tonic-gate  * We build the 16 key entries here
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static int
__des_setkey(uchar_t userkey[8],struct deskeydata * kd,unsigned dir)214132Srobinson __des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned dir)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate /* EXPORT DELETE START */
2170Sstevel@tonic-gate 	int32_t C, D;
218132Srobinson 	short i;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * First, generate C and D by permuting
2220Sstevel@tonic-gate 	 * the key. The low order bit of each
2230Sstevel@tonic-gate 	 * 8-bit char is not used, so C and D are only 28
2240Sstevel@tonic-gate 	 * bits apiece.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	{
227132Srobinson 		short bit;
228132Srobinson 		const short *pcc = PC1_C, *pcd = PC1_D;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		C = D = 0;
2310Sstevel@tonic-gate 		for (i = 0; i < 28; i++) {
2320Sstevel@tonic-gate 			C <<= 1;
2330Sstevel@tonic-gate 			D <<= 1;
2340Sstevel@tonic-gate 			bit = *pcc++;
2350Sstevel@tonic-gate 			if (btst(userkey, bit))
2360Sstevel@tonic-gate 				C |= 1;
2370Sstevel@tonic-gate 			bit = *pcd++;
2380Sstevel@tonic-gate 			if (btst(userkey, bit))
2390Sstevel@tonic-gate 				D |= 1;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * To generate Ki, rotate C and D according
2440Sstevel@tonic-gate 	 * to schedule and pick up a permutation
2450Sstevel@tonic-gate 	 * using PC2.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
248132Srobinson 		chunk_t *c;
249132Srobinson 		short j, k, bit;
2500Sstevel@tonic-gate 		uint32_t bbit;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		/*
2530Sstevel@tonic-gate 		 * Do the "left shift" (rotate)
2540Sstevel@tonic-gate 		 * We know we always rotate by either 1 or 2 bits
2550Sstevel@tonic-gate 		 * the shifts table tells us if its 2
2560Sstevel@tonic-gate 		 */
2570Sstevel@tonic-gate 		C <<= 1;
2580Sstevel@tonic-gate 		if (C & BIT28)
2590Sstevel@tonic-gate 			C |= 1;
2600Sstevel@tonic-gate 		D <<= 1;
2610Sstevel@tonic-gate 		if (D & BIT28)
2620Sstevel@tonic-gate 			D |= 1;
2630Sstevel@tonic-gate 		if (shifts[i]) {
2640Sstevel@tonic-gate 			C <<= 1;
2650Sstevel@tonic-gate 			if (C & BIT28)
2660Sstevel@tonic-gate 				C |= 1;
2670Sstevel@tonic-gate 			D <<= 1;
2680Sstevel@tonic-gate 			if (D & BIT28)
2690Sstevel@tonic-gate 				D |= 1;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		/*
2720Sstevel@tonic-gate 		 * get Ki. Note C and D are concatenated.
2730Sstevel@tonic-gate 		 */
2740Sstevel@tonic-gate 		bit = 0;
2750Sstevel@tonic-gate 		switch (dir) {
2760Sstevel@tonic-gate 		case ENCRYPT:
2770Sstevel@tonic-gate 			c = &kd->keyval[i]; break;
2780Sstevel@tonic-gate 		case DECRYPT:
2790Sstevel@tonic-gate 			c = &kd->keyval[15 - i]; break;
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 		c->long0 = 0;
2820Sstevel@tonic-gate 		c->long1 = 0;
2830Sstevel@tonic-gate 		bbit = (1 << 5) << 24;
2840Sstevel@tonic-gate 		for (j = 0; j < 4; j++) {
2850Sstevel@tonic-gate 			for (k = 0; k < 6; k++) {
2860Sstevel@tonic-gate 				if (C & (BIT28 >> PC2_C[bit]))
2870Sstevel@tonic-gate 					c->long0 |= bbit >> k;
2880Sstevel@tonic-gate 				if (D & (BIT28 >> PC2_D[bit]))
2890Sstevel@tonic-gate 					c->long1 |= bbit >> k;
2900Sstevel@tonic-gate 				bit++;
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 			bbit >>= 8;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate /* EXPORT DELETE END */
2970Sstevel@tonic-gate 	return (1);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * Do an encryption operation
3040Sstevel@tonic-gate  * Much pain is taken (with preprocessor) to avoid loops so the compiler
3050Sstevel@tonic-gate  * can do address arithmetic instead of doing it at runtime.
3060Sstevel@tonic-gate  * Note that the byte-to-chunk conversion is necessary to guarantee
3070Sstevel@tonic-gate  * processor byte-order independence.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate static int
__des_encrypt(uchar_t * data,struct deskeydata * kd)310132Srobinson __des_encrypt(uchar_t *data, struct deskeydata *kd)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate /* EXPORT DELETE START */
3130Sstevel@tonic-gate 	chunk_t work1, work2;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * Initial permutation
3170Sstevel@tonic-gate 	 * and byte to chunk conversion
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	{
3200Sstevel@tonic-gate 		const uint32_t *lp;
3210Sstevel@tonic-gate 		uint32_t l0, l1, w;
322132Srobinson 		short i, pbit;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		work1.byte0 = data[0];
3250Sstevel@tonic-gate 		work1.byte1 = data[1];
3260Sstevel@tonic-gate 		work1.byte2 = data[2];
3270Sstevel@tonic-gate 		work1.byte3 = data[3];
3280Sstevel@tonic-gate 		work1.byte4 = data[4];
3290Sstevel@tonic-gate 		work1.byte5 = data[5];
3300Sstevel@tonic-gate 		work1.byte6 = data[6];
3310Sstevel@tonic-gate 		work1.byte7 = data[7];
3320Sstevel@tonic-gate 		l0 = l1 = 0;
3330Sstevel@tonic-gate 		w = work1.long0;
3340Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
3350Sstevel@tonic-gate 			if (w & *lp++) {
3360Sstevel@tonic-gate 				pbit = IPtab[i];
3370Sstevel@tonic-gate 				if (pbit < 32)
3380Sstevel@tonic-gate 					l0 |= longtab[pbit];
3390Sstevel@tonic-gate 				else
3400Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 		w = work1.long1;
3440Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
3450Sstevel@tonic-gate 			if (w & *lp++) {
3460Sstevel@tonic-gate 				pbit = IPtab[i];
3470Sstevel@tonic-gate 				if (pbit < 32)
3480Sstevel@tonic-gate 					l0 |= longtab[pbit];
3490Sstevel@tonic-gate 				else
3500Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 		work2.long0 = l0;
3540Sstevel@tonic-gate 		work2.long1 = l1;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Expand 8 bits of 32 bit R to 48 bit R
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate #define	do_R_to_ER(op, b)	{			\
361132Srobinson 	const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b];	\
3620Sstevel@tonic-gate 	e0 op p->l0;				\
3630Sstevel@tonic-gate 	e1 op p->l1;				\
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Inner part of the algorithm:
3680Sstevel@tonic-gate  * Expand R from 32 to 48 bits; xor key value;
3690Sstevel@tonic-gate  * apply S boxes; permute 32 bits of output
3700Sstevel@tonic-gate  */
371132Srobinson /* BEGIN CSTYLED */
3720Sstevel@tonic-gate #define	do_F(iter, inR, outR) 	{			\
3730Sstevel@tonic-gate 	chunk_t R, ER;					\
3740Sstevel@tonic-gate 	uint32_t e0, e1;				\
3750Sstevel@tonic-gate 	R.long0 = inR;					\
3760Sstevel@tonic-gate 	do_R_to_ER(=, 0);				\
3770Sstevel@tonic-gate 	do_R_to_ER(|=, 1);				\
3780Sstevel@tonic-gate 	do_R_to_ER(|=, 2);				\
3790Sstevel@tonic-gate 	do_R_to_ER(|=, 3);				\
3800Sstevel@tonic-gate 	ER.long0 = e0 ^ kd->keyval[iter].long0;		\
3810Sstevel@tonic-gate 	ER.long1 = e1 ^ kd->keyval[iter].long1;		\
3820Sstevel@tonic-gate 	R.long0 =					\
3830Sstevel@tonic-gate 		S_tab[0][ER.byte0] +			\
3840Sstevel@tonic-gate 		S_tab[1][ER.byte1] +			\
3850Sstevel@tonic-gate 		S_tab[2][ER.byte2] +			\
3860Sstevel@tonic-gate 		S_tab[3][ER.byte3] +			\
3870Sstevel@tonic-gate 		S_tab[4][ER.byte4] +			\
3880Sstevel@tonic-gate 		S_tab[5][ER.byte5] +			\
3890Sstevel@tonic-gate 		S_tab[6][ER.byte6] +			\
3900Sstevel@tonic-gate 		S_tab[7][ER.byte7]; 			\
3910Sstevel@tonic-gate 	outR =						\
3920Sstevel@tonic-gate 		P_tab[0][R.byte0] +			\
3930Sstevel@tonic-gate 		P_tab[1][R.byte1] +			\
3940Sstevel@tonic-gate 		P_tab[2][R.byte2] +			\
3950Sstevel@tonic-gate 		P_tab[3][R.byte3]; 			\
3960Sstevel@tonic-gate }
397132Srobinson /* END CSTYLED */
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * Do a cipher step
4010Sstevel@tonic-gate  * Apply inner part; do xor and exchange of 32 bit parts
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate #define	cipher(iter, inR, inL, outR, outL)	{	\
4040Sstevel@tonic-gate 	do_F(iter, inR, outR);				\
4050Sstevel@tonic-gate 	outR ^= inL;					\
4060Sstevel@tonic-gate 	outL = inR;					\
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Apply the 16 ciphering steps
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	{
4130Sstevel@tonic-gate 		uint32_t r0, l0, r1, l1;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		l0 = work2.long0;
4160Sstevel@tonic-gate 		r0 = work2.long1;
4170Sstevel@tonic-gate 		cipher(0, r0, l0, r1, l1);
4180Sstevel@tonic-gate 		cipher(1, r1, l1, r0, l0);
4190Sstevel@tonic-gate 		cipher(2, r0, l0, r1, l1);
4200Sstevel@tonic-gate 		cipher(3, r1, l1, r0, l0);
4210Sstevel@tonic-gate 		cipher(4, r0, l0, r1, l1);
4220Sstevel@tonic-gate 		cipher(5, r1, l1, r0, l0);
4230Sstevel@tonic-gate 		cipher(6, r0, l0, r1, l1);
4240Sstevel@tonic-gate 		cipher(7, r1, l1, r0, l0);
4250Sstevel@tonic-gate 		cipher(8, r0, l0, r1, l1);
4260Sstevel@tonic-gate 		cipher(9, r1, l1, r0, l0);
4270Sstevel@tonic-gate 		cipher(10, r0, l0, r1, l1);
4280Sstevel@tonic-gate 		cipher(11, r1, l1, r0, l0);
4290Sstevel@tonic-gate 		cipher(12, r0, l0, r1, l1);
4300Sstevel@tonic-gate 		cipher(13, r1, l1, r0, l0);
4310Sstevel@tonic-gate 		cipher(14, r0, l0, r1, l1);
4320Sstevel@tonic-gate 		cipher(15, r1, l1, r0, l0);
4330Sstevel@tonic-gate 		work1.long0 = r0;
4340Sstevel@tonic-gate 		work1.long1 = l0;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * Final permutation
4390Sstevel@tonic-gate 	 * and chunk to byte conversion
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	{
4420Sstevel@tonic-gate 		uint32_t *lp;
4430Sstevel@tonic-gate 		uint32_t l0, l1, w;
444132Srobinson 		short i, pbit;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		l0 = l1 = 0;
4470Sstevel@tonic-gate 		w = work1.long0;
4480Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
4490Sstevel@tonic-gate 			if (w & *lp++) {
4500Sstevel@tonic-gate 				pbit = FPtab[i];
4510Sstevel@tonic-gate 				if (pbit < 32)
4520Sstevel@tonic-gate 					l0 |= longtab[pbit];
4530Sstevel@tonic-gate 				else
4540Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
4550Sstevel@tonic-gate 			}
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		w = work1.long1;
4580Sstevel@tonic-gate 		for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
4590Sstevel@tonic-gate 			if (w & *lp++) {
4600Sstevel@tonic-gate 				pbit = FPtab[i];
4610Sstevel@tonic-gate 				if (pbit < 32)
4620Sstevel@tonic-gate 					l0 |= longtab[pbit];
4630Sstevel@tonic-gate 				else
4640Sstevel@tonic-gate 					l1 |= longtab[pbit-32];
4650Sstevel@tonic-gate 			}
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 		work2.long0 = l0;
4680Sstevel@tonic-gate 		work2.long1 = l1;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	data[0] = work2.byte0;
4710Sstevel@tonic-gate 	data[1] = work2.byte1;
4720Sstevel@tonic-gate 	data[2] = work2.byte2;
4730Sstevel@tonic-gate 	data[3] = work2.byte3;
4740Sstevel@tonic-gate 	data[4] = work2.byte4;
4750Sstevel@tonic-gate 	data[5] = work2.byte5;
4760Sstevel@tonic-gate 	data[6] = work2.byte6;
4770Sstevel@tonic-gate 	data[7] = work2.byte7;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /* EXPORT DELETE END */
4800Sstevel@tonic-gate 	return (1);
4810Sstevel@tonic-gate }
482