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 */
2261961e0fSrobinson
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf * Copyright 2006 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 /*
377c478bd9Sstevel@tonic-gate * Warning! Things are arranged very carefully in this file to
387c478bd9Sstevel@tonic-gate * allow read-only data to be moved to the text segment. The
397c478bd9Sstevel@tonic-gate * various DES tables must appear before any function definitions
407c478bd9Sstevel@tonic-gate * (this is arranged by including them immediately below) and partab
417c478bd9Sstevel@tonic-gate * must also appear before and function definitions
427c478bd9Sstevel@tonic-gate * This arrangement allows all data up through the first text to
437c478bd9Sstevel@tonic-gate * be moved to text.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
46*e8031f0aSraf #include "mt.h"
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <des/softdes.h>
497c478bd9Sstevel@tonic-gate #include <des/desdata.h>
507c478bd9Sstevel@tonic-gate #ifdef sun
517c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
527c478bd9Sstevel@tonic-gate #include <sys/des.h>
537c478bd9Sstevel@tonic-gate #else
547c478bd9Sstevel@tonic-gate #include <des/des.h>
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * Fast (?) software implementation of DES
607c478bd9Sstevel@tonic-gate * Has been seen going at 2000 bytes/sec on a Sun-2
617c478bd9Sstevel@tonic-gate * Works on a VAX too.
627c478bd9Sstevel@tonic-gate * Won't work without 8 bit chars and 32 bit longs
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate #define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
667c478bd9Sstevel@tonic-gate #define BIT28 (1<<28)
677c478bd9Sstevel@tonic-gate
6861961e0fSrobinson static int __des_encrypt(uchar_t *, struct deskeydata *);
6961961e0fSrobinson static int __des_setkey(uchar_t[8], struct deskeydata *, unsigned);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * Table giving odd parity in the low bit for ASCII characters
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate const char partab[128] = {
767c478bd9Sstevel@tonic-gate 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
777c478bd9Sstevel@tonic-gate 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
787c478bd9Sstevel@tonic-gate 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
797c478bd9Sstevel@tonic-gate 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
807c478bd9Sstevel@tonic-gate 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
817c478bd9Sstevel@tonic-gate 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
827c478bd9Sstevel@tonic-gate 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
837c478bd9Sstevel@tonic-gate 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
847c478bd9Sstevel@tonic-gate 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
857c478bd9Sstevel@tonic-gate 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
867c478bd9Sstevel@tonic-gate 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
877c478bd9Sstevel@tonic-gate 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
887c478bd9Sstevel@tonic-gate 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
897c478bd9Sstevel@tonic-gate 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
907c478bd9Sstevel@tonic-gate 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
917c478bd9Sstevel@tonic-gate 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * Add odd parity to low bit of 8 byte key
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate void
des_setparity(char * p)9861961e0fSrobinson des_setparity(char *p)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate int i;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) {
1037c478bd9Sstevel@tonic-gate *p = partab[*p & 0x7f];
1047c478bd9Sstevel@tonic-gate p++;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate static const unsigned char partab_g[256] = {
1097c478bd9Sstevel@tonic-gate 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
1107c478bd9Sstevel@tonic-gate 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
1117c478bd9Sstevel@tonic-gate 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
1127c478bd9Sstevel@tonic-gate 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
1137c478bd9Sstevel@tonic-gate 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
1147c478bd9Sstevel@tonic-gate 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
1157c478bd9Sstevel@tonic-gate 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
1167c478bd9Sstevel@tonic-gate 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
1177c478bd9Sstevel@tonic-gate 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
1187c478bd9Sstevel@tonic-gate 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
1197c478bd9Sstevel@tonic-gate 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
1207c478bd9Sstevel@tonic-gate 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
1217c478bd9Sstevel@tonic-gate 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
1227c478bd9Sstevel@tonic-gate 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
1237c478bd9Sstevel@tonic-gate 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
1247c478bd9Sstevel@tonic-gate 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
1257c478bd9Sstevel@tonic-gate 0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86,
1267c478bd9Sstevel@tonic-gate 0x89, 0x89, 0x8a, 0x8a, 0x8c, 0x8c, 0x8f, 0x8f,
1277c478bd9Sstevel@tonic-gate 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97,
1287c478bd9Sstevel@tonic-gate 0x98, 0x98, 0x9b, 0x9b, 0x9d, 0x9d, 0x9e, 0x9e,
1297c478bd9Sstevel@tonic-gate 0xa1, 0xa1, 0xa2, 0xa2, 0xa4, 0xa4, 0xa7, 0xa7,
1307c478bd9Sstevel@tonic-gate 0xa8, 0xa8, 0xab, 0xab, 0xad, 0xad, 0xae, 0xae,
1317c478bd9Sstevel@tonic-gate 0xb0, 0xb0, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb6,
1327c478bd9Sstevel@tonic-gate 0xb9, 0xb9, 0xba, 0xba, 0xbc, 0xbc, 0xbf, 0xbf,
1337c478bd9Sstevel@tonic-gate 0xc1, 0xc1, 0xc2, 0xc2, 0xc4, 0xc4, 0xc7, 0xc7,
1347c478bd9Sstevel@tonic-gate 0xc8, 0xc8, 0xcb, 0xcb, 0xcd, 0xcd, 0xce, 0xce,
1357c478bd9Sstevel@tonic-gate 0xd0, 0xd0, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd6,
1367c478bd9Sstevel@tonic-gate 0xd9, 0xd9, 0xda, 0xda, 0xdc, 0xdc, 0xdf, 0xdf,
1377c478bd9Sstevel@tonic-gate 0xe0, 0xe0, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe6,
1387c478bd9Sstevel@tonic-gate 0xe9, 0xe9, 0xea, 0xea, 0xec, 0xec, 0xef, 0xef,
1397c478bd9Sstevel@tonic-gate 0xf1, 0xf1, 0xf2, 0xf2, 0xf4, 0xf4, 0xf7, 0xf7,
1407c478bd9Sstevel@tonic-gate 0xf8, 0xf8, 0xfb, 0xfb, 0xfd, 0xfd, 0xfe, 0xfe
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * A corrected version of des_setparity (see bug 1149767).
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate void
des_setparity_g(des_block * p)1477c478bd9Sstevel@tonic-gate des_setparity_g(des_block *p)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate int i;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) {
1527c478bd9Sstevel@tonic-gate (*p).c[i] = partab_g[(*p).c[i]];
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * Software encrypt or decrypt a block of data (multiple of 8 bytes)
1587c478bd9Sstevel@tonic-gate * Do the CBC ourselves if needed.
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate int
__des_crypt(char * buf,unsigned len,struct desparams * desp)16161961e0fSrobinson __des_crypt(char *buf, unsigned len, struct desparams *desp)
1627c478bd9Sstevel@tonic-gate {
16361961e0fSrobinson short i;
16461961e0fSrobinson unsigned mode;
16561961e0fSrobinson unsigned dir;
1667c478bd9Sstevel@tonic-gate char nextiv[8];
1677c478bd9Sstevel@tonic-gate struct deskeydata softkey;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate mode = (unsigned)desp->des_mode;
1707c478bd9Sstevel@tonic-gate dir = (unsigned)desp->des_dir;
17161961e0fSrobinson (void) __des_setkey(desp->des_key, &softkey, dir);
1727c478bd9Sstevel@tonic-gate while (len != 0) {
1737c478bd9Sstevel@tonic-gate switch (mode) {
1747c478bd9Sstevel@tonic-gate case CBC:
1757c478bd9Sstevel@tonic-gate switch (dir) {
1767c478bd9Sstevel@tonic-gate case ENCRYPT:
1777c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++)
1787c478bd9Sstevel@tonic-gate buf[i] ^= desp->des_ivec[i];
17961961e0fSrobinson (void) __des_encrypt((uchar_t *)buf, &softkey);
1807c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++)
1817c478bd9Sstevel@tonic-gate desp->des_ivec[i] = buf[i];
1827c478bd9Sstevel@tonic-gate break;
1837c478bd9Sstevel@tonic-gate case DECRYPT:
1847c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++)
1857c478bd9Sstevel@tonic-gate nextiv[i] = buf[i];
18661961e0fSrobinson (void) __des_encrypt((uchar_t *)buf, &softkey);
1877c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) {
1887c478bd9Sstevel@tonic-gate buf[i] ^= desp->des_ivec[i];
1897c478bd9Sstevel@tonic-gate desp->des_ivec[i] = nextiv[i];
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate break;
1947c478bd9Sstevel@tonic-gate case ECB:
19561961e0fSrobinson (void) __des_encrypt((uchar_t *)buf, &softkey);
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate buf += 8;
1997c478bd9Sstevel@tonic-gate len -= 8;
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate return (1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate * Set the key and direction for an encryption operation
2077c478bd9Sstevel@tonic-gate * We build the 16 key entries here
2087c478bd9Sstevel@tonic-gate */
2097c478bd9Sstevel@tonic-gate static int
__des_setkey(uchar_t userkey[8],struct deskeydata * kd,unsigned dir)21061961e0fSrobinson __des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned dir)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate int32_t C, D;
21361961e0fSrobinson short i;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate * First, generate C and D by permuting
2177c478bd9Sstevel@tonic-gate * the key. The low order bit of each
2187c478bd9Sstevel@tonic-gate * 8-bit char is not used, so C and D are only 28
2197c478bd9Sstevel@tonic-gate * bits apiece.
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate {
22261961e0fSrobinson short bit;
22361961e0fSrobinson const short *pcc = PC1_C, *pcd = PC1_D;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate C = D = 0;
2267c478bd9Sstevel@tonic-gate for (i = 0; i < 28; i++) {
2277c478bd9Sstevel@tonic-gate C <<= 1;
2287c478bd9Sstevel@tonic-gate D <<= 1;
2297c478bd9Sstevel@tonic-gate bit = *pcc++;
2307c478bd9Sstevel@tonic-gate if (btst(userkey, bit))
2317c478bd9Sstevel@tonic-gate C |= 1;
2327c478bd9Sstevel@tonic-gate bit = *pcd++;
2337c478bd9Sstevel@tonic-gate if (btst(userkey, bit))
2347c478bd9Sstevel@tonic-gate D |= 1;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * To generate Ki, rotate C and D according
2397c478bd9Sstevel@tonic-gate * to schedule and pick up a permutation
2407c478bd9Sstevel@tonic-gate * using PC2.
2417c478bd9Sstevel@tonic-gate */
2427c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) {
24361961e0fSrobinson chunk_t *c;
24461961e0fSrobinson short j, k, bit;
2457c478bd9Sstevel@tonic-gate uint32_t bbit;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate * Do the "left shift" (rotate)
2497c478bd9Sstevel@tonic-gate * We know we always rotate by either 1 or 2 bits
2507c478bd9Sstevel@tonic-gate * the shifts table tells us if its 2
2517c478bd9Sstevel@tonic-gate */
2527c478bd9Sstevel@tonic-gate C <<= 1;
2537c478bd9Sstevel@tonic-gate if (C & BIT28)
2547c478bd9Sstevel@tonic-gate C |= 1;
2557c478bd9Sstevel@tonic-gate D <<= 1;
2567c478bd9Sstevel@tonic-gate if (D & BIT28)
2577c478bd9Sstevel@tonic-gate D |= 1;
2587c478bd9Sstevel@tonic-gate if (shifts[i]) {
2597c478bd9Sstevel@tonic-gate C <<= 1;
2607c478bd9Sstevel@tonic-gate if (C & BIT28)
2617c478bd9Sstevel@tonic-gate C |= 1;
2627c478bd9Sstevel@tonic-gate D <<= 1;
2637c478bd9Sstevel@tonic-gate if (D & BIT28)
2647c478bd9Sstevel@tonic-gate D |= 1;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * get Ki. Note C and D are concatenated.
2687c478bd9Sstevel@tonic-gate */
2697c478bd9Sstevel@tonic-gate bit = 0;
2707c478bd9Sstevel@tonic-gate switch (dir) {
2717c478bd9Sstevel@tonic-gate case ENCRYPT:
2727c478bd9Sstevel@tonic-gate c = &kd->keyval[i]; break;
2737c478bd9Sstevel@tonic-gate case DECRYPT:
2747c478bd9Sstevel@tonic-gate c = &kd->keyval[15 - i]; break;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate c->long0 = 0;
2777c478bd9Sstevel@tonic-gate c->long1 = 0;
2787c478bd9Sstevel@tonic-gate bbit = (1 << 5) << 24;
2797c478bd9Sstevel@tonic-gate for (j = 0; j < 4; j++) {
2807c478bd9Sstevel@tonic-gate for (k = 0; k < 6; k++) {
2817c478bd9Sstevel@tonic-gate if (C & (BIT28 >> PC2_C[bit]))
2827c478bd9Sstevel@tonic-gate c->long0 |= bbit >> k;
2837c478bd9Sstevel@tonic-gate if (D & (BIT28 >> PC2_D[bit]))
2847c478bd9Sstevel@tonic-gate c->long1 |= bbit >> k;
2857c478bd9Sstevel@tonic-gate bit++;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate bbit >>= 8;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate return (1);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate * Do an encryption operation
2987c478bd9Sstevel@tonic-gate * Much pain is taken (with preprocessor) to avoid loops so the compiler
2997c478bd9Sstevel@tonic-gate * can do address arithmetic instead of doing it at runtime.
3007c478bd9Sstevel@tonic-gate * Note that the byte-to-chunk conversion is necessary to guarantee
3017c478bd9Sstevel@tonic-gate * processor byte-order independence.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate static int
__des_encrypt(uchar_t * data,struct deskeydata * kd)30461961e0fSrobinson __des_encrypt(uchar_t *data, struct deskeydata *kd)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate chunk_t work1, work2;
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate * Initial permutation
3107c478bd9Sstevel@tonic-gate * and byte to chunk conversion
3117c478bd9Sstevel@tonic-gate */
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate const uint32_t *lp;
3147c478bd9Sstevel@tonic-gate uint32_t l0, l1, w;
31561961e0fSrobinson short i, pbit;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate work1.byte0 = data[0];
3187c478bd9Sstevel@tonic-gate work1.byte1 = data[1];
3197c478bd9Sstevel@tonic-gate work1.byte2 = data[2];
3207c478bd9Sstevel@tonic-gate work1.byte3 = data[3];
3217c478bd9Sstevel@tonic-gate work1.byte4 = data[4];
3227c478bd9Sstevel@tonic-gate work1.byte5 = data[5];
3237c478bd9Sstevel@tonic-gate work1.byte6 = data[6];
3247c478bd9Sstevel@tonic-gate work1.byte7 = data[7];
3257c478bd9Sstevel@tonic-gate l0 = l1 = 0;
3267c478bd9Sstevel@tonic-gate w = work1.long0;
3277c478bd9Sstevel@tonic-gate for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
3287c478bd9Sstevel@tonic-gate if (w & *lp++) {
3297c478bd9Sstevel@tonic-gate pbit = IPtab[i];
3307c478bd9Sstevel@tonic-gate if (pbit < 32)
3317c478bd9Sstevel@tonic-gate l0 |= longtab[pbit];
3327c478bd9Sstevel@tonic-gate else
3337c478bd9Sstevel@tonic-gate l1 |= longtab[pbit-32];
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate w = work1.long1;
3377c478bd9Sstevel@tonic-gate for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
3387c478bd9Sstevel@tonic-gate if (w & *lp++) {
3397c478bd9Sstevel@tonic-gate pbit = IPtab[i];
3407c478bd9Sstevel@tonic-gate if (pbit < 32)
3417c478bd9Sstevel@tonic-gate l0 |= longtab[pbit];
3427c478bd9Sstevel@tonic-gate else
3437c478bd9Sstevel@tonic-gate l1 |= longtab[pbit-32];
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate work2.long0 = l0;
3477c478bd9Sstevel@tonic-gate work2.long1 = l1;
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate * Expand 8 bits of 32 bit R to 48 bit R
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate #define do_R_to_ER(op, b) { \
35461961e0fSrobinson const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b]; \
3557c478bd9Sstevel@tonic-gate e0 op p->l0; \
3567c478bd9Sstevel@tonic-gate e1 op p->l1; \
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate * Inner part of the algorithm:
3617c478bd9Sstevel@tonic-gate * Expand R from 32 to 48 bits; xor key value;
3627c478bd9Sstevel@tonic-gate * apply S boxes; permute 32 bits of output
3637c478bd9Sstevel@tonic-gate */
36461961e0fSrobinson /* BEGIN CSTYLED */
3657c478bd9Sstevel@tonic-gate #define do_F(iter, inR, outR) { \
3667c478bd9Sstevel@tonic-gate chunk_t R, ER; \
3677c478bd9Sstevel@tonic-gate uint32_t e0, e1; \
3687c478bd9Sstevel@tonic-gate R.long0 = inR; \
3697c478bd9Sstevel@tonic-gate do_R_to_ER(=, 0); \
3707c478bd9Sstevel@tonic-gate do_R_to_ER(|=, 1); \
3717c478bd9Sstevel@tonic-gate do_R_to_ER(|=, 2); \
3727c478bd9Sstevel@tonic-gate do_R_to_ER(|=, 3); \
3737c478bd9Sstevel@tonic-gate ER.long0 = e0 ^ kd->keyval[iter].long0; \
3747c478bd9Sstevel@tonic-gate ER.long1 = e1 ^ kd->keyval[iter].long1; \
3757c478bd9Sstevel@tonic-gate R.long0 = \
3767c478bd9Sstevel@tonic-gate S_tab[0][ER.byte0] + \
3777c478bd9Sstevel@tonic-gate S_tab[1][ER.byte1] + \
3787c478bd9Sstevel@tonic-gate S_tab[2][ER.byte2] + \
3797c478bd9Sstevel@tonic-gate S_tab[3][ER.byte3] + \
3807c478bd9Sstevel@tonic-gate S_tab[4][ER.byte4] + \
3817c478bd9Sstevel@tonic-gate S_tab[5][ER.byte5] + \
3827c478bd9Sstevel@tonic-gate S_tab[6][ER.byte6] + \
3837c478bd9Sstevel@tonic-gate S_tab[7][ER.byte7]; \
3847c478bd9Sstevel@tonic-gate outR = \
3857c478bd9Sstevel@tonic-gate P_tab[0][R.byte0] + \
3867c478bd9Sstevel@tonic-gate P_tab[1][R.byte1] + \
3877c478bd9Sstevel@tonic-gate P_tab[2][R.byte2] + \
3887c478bd9Sstevel@tonic-gate P_tab[3][R.byte3]; \
3897c478bd9Sstevel@tonic-gate }
39061961e0fSrobinson /* END CSTYLED */
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * Do a cipher step
3947c478bd9Sstevel@tonic-gate * Apply inner part; do xor and exchange of 32 bit parts
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate #define cipher(iter, inR, inL, outR, outL) { \
3977c478bd9Sstevel@tonic-gate do_F(iter, inR, outR); \
3987c478bd9Sstevel@tonic-gate outR ^= inL; \
3997c478bd9Sstevel@tonic-gate outL = inR; \
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * Apply the 16 ciphering steps
4047c478bd9Sstevel@tonic-gate */
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate uint32_t r0, l0, r1, l1;
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate l0 = work2.long0;
4097c478bd9Sstevel@tonic-gate r0 = work2.long1;
4107c478bd9Sstevel@tonic-gate cipher(0, r0, l0, r1, l1);
4117c478bd9Sstevel@tonic-gate cipher(1, r1, l1, r0, l0);
4127c478bd9Sstevel@tonic-gate cipher(2, r0, l0, r1, l1);
4137c478bd9Sstevel@tonic-gate cipher(3, r1, l1, r0, l0);
4147c478bd9Sstevel@tonic-gate cipher(4, r0, l0, r1, l1);
4157c478bd9Sstevel@tonic-gate cipher(5, r1, l1, r0, l0);
4167c478bd9Sstevel@tonic-gate cipher(6, r0, l0, r1, l1);
4177c478bd9Sstevel@tonic-gate cipher(7, r1, l1, r0, l0);
4187c478bd9Sstevel@tonic-gate cipher(8, r0, l0, r1, l1);
4197c478bd9Sstevel@tonic-gate cipher(9, r1, l1, r0, l0);
4207c478bd9Sstevel@tonic-gate cipher(10, r0, l0, r1, l1);
4217c478bd9Sstevel@tonic-gate cipher(11, r1, l1, r0, l0);
4227c478bd9Sstevel@tonic-gate cipher(12, r0, l0, r1, l1);
4237c478bd9Sstevel@tonic-gate cipher(13, r1, l1, r0, l0);
4247c478bd9Sstevel@tonic-gate cipher(14, r0, l0, r1, l1);
4257c478bd9Sstevel@tonic-gate cipher(15, r1, l1, r0, l0);
4267c478bd9Sstevel@tonic-gate work1.long0 = r0;
4277c478bd9Sstevel@tonic-gate work1.long1 = l0;
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate * Final permutation
4327c478bd9Sstevel@tonic-gate * and chunk to byte conversion
4337c478bd9Sstevel@tonic-gate */
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate uint32_t *lp;
4367c478bd9Sstevel@tonic-gate uint32_t l0, l1, w;
43761961e0fSrobinson short i, pbit;
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate l0 = l1 = 0;
4407c478bd9Sstevel@tonic-gate w = work1.long0;
4417c478bd9Sstevel@tonic-gate for (lp = (uint32_t *)&longtab[0], i = 0; i < 32; i++) {
4427c478bd9Sstevel@tonic-gate if (w & *lp++) {
4437c478bd9Sstevel@tonic-gate pbit = FPtab[i];
4447c478bd9Sstevel@tonic-gate if (pbit < 32)
4457c478bd9Sstevel@tonic-gate l0 |= longtab[pbit];
4467c478bd9Sstevel@tonic-gate else
4477c478bd9Sstevel@tonic-gate l1 |= longtab[pbit-32];
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate w = work1.long1;
4517c478bd9Sstevel@tonic-gate for (lp = (uint32_t *)&longtab[0], i = 32; i < 64; i++) {
4527c478bd9Sstevel@tonic-gate if (w & *lp++) {
4537c478bd9Sstevel@tonic-gate pbit = FPtab[i];
4547c478bd9Sstevel@tonic-gate if (pbit < 32)
4557c478bd9Sstevel@tonic-gate l0 |= longtab[pbit];
4567c478bd9Sstevel@tonic-gate else
4577c478bd9Sstevel@tonic-gate l1 |= longtab[pbit-32];
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate work2.long0 = l0;
4617c478bd9Sstevel@tonic-gate work2.long1 = l1;
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate data[0] = work2.byte0;
4647c478bd9Sstevel@tonic-gate data[1] = work2.byte1;
4657c478bd9Sstevel@tonic-gate data[2] = work2.byte2;
4667c478bd9Sstevel@tonic-gate data[3] = work2.byte3;
4677c478bd9Sstevel@tonic-gate data[4] = work2.byte4;
4687c478bd9Sstevel@tonic-gate data[5] = work2.byte5;
4697c478bd9Sstevel@tonic-gate data[6] = work2.byte6;
4707c478bd9Sstevel@tonic-gate data[7] = work2.byte7;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate return (1);
4737c478bd9Sstevel@tonic-gate }
474