1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Portable C version of des() and des_key() functions. 31*7c478bd9Sstevel@tonic-gate * This version is very similar to that in Part V of Applied Cryptography 32*7c478bd9Sstevel@tonic-gate * by Bruce Schneier. 33*7c478bd9Sstevel@tonic-gate * 34*7c478bd9Sstevel@tonic-gate * This information is in the public domain 12/15/95 P. Karn 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 39*7c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT) 40*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 41*7c478bd9Sstevel@tonic-gate #else 42*7c478bd9Sstevel@tonic-gate #include <strings.h> 43*7c478bd9Sstevel@tonic-gate #endif 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include "des.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate /* 48*7c478bd9Sstevel@tonic-gate * Combined SP lookup table, linked in 49*7c478bd9Sstevel@tonic-gate * For best results, ensure that this is aligned on a 32-bit boundary; 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate static uint32_t Spbox[8][64] = { 52*7c478bd9Sstevel@tonic-gate 0x01010400U, 0x00000000U, 0x00010000U, 0x01010404U, 53*7c478bd9Sstevel@tonic-gate 0x01010004U, 0x00010404U, 0x00000004U, 0x00010000U, 54*7c478bd9Sstevel@tonic-gate 0x00000400U, 0x01010400U, 0x01010404U, 0x00000400U, 55*7c478bd9Sstevel@tonic-gate 0x01000404U, 0x01010004U, 0x01000000U, 0x00000004U, 56*7c478bd9Sstevel@tonic-gate 0x00000404U, 0x01000400U, 0x01000400U, 0x00010400U, 57*7c478bd9Sstevel@tonic-gate 0x00010400U, 0x01010000U, 0x01010000U, 0x01000404U, 58*7c478bd9Sstevel@tonic-gate 0x00010004U, 0x01000004U, 0x01000004U, 0x00010004U, 59*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x00000404U, 0x00010404U, 0x01000000U, 60*7c478bd9Sstevel@tonic-gate 0x00010000U, 0x01010404U, 0x00000004U, 0x01010000U, 61*7c478bd9Sstevel@tonic-gate 0x01010400U, 0x01000000U, 0x01000000U, 0x00000400U, 62*7c478bd9Sstevel@tonic-gate 0x01010004U, 0x00010000U, 0x00010400U, 0x01000004U, 63*7c478bd9Sstevel@tonic-gate 0x00000400U, 0x00000004U, 0x01000404U, 0x00010404U, 64*7c478bd9Sstevel@tonic-gate 0x01010404U, 0x00010004U, 0x01010000U, 0x01000404U, 65*7c478bd9Sstevel@tonic-gate 0x01000004U, 0x00000404U, 0x00010404U, 0x01010400U, 66*7c478bd9Sstevel@tonic-gate 0x00000404U, 0x01000400U, 0x01000400U, 0x00000000U, 67*7c478bd9Sstevel@tonic-gate 0x00010004U, 0x00010400U, 0x00000000U, 0x01010004U, 68*7c478bd9Sstevel@tonic-gate 0x80108020U, 0x80008000U, 0x00008000U, 0x00108020U, 69*7c478bd9Sstevel@tonic-gate 0x00100000U, 0x00000020U, 0x80100020U, 0x80008020U, 70*7c478bd9Sstevel@tonic-gate 0x80000020U, 0x80108020U, 0x80108000U, 0x80000000U, 71*7c478bd9Sstevel@tonic-gate 0x80008000U, 0x00100000U, 0x00000020U, 0x80100020U, 72*7c478bd9Sstevel@tonic-gate 0x00108000U, 0x00100020U, 0x80008020U, 0x00000000U, 73*7c478bd9Sstevel@tonic-gate 0x80000000U, 0x00008000U, 0x00108020U, 0x80100000U, 74*7c478bd9Sstevel@tonic-gate 0x00100020U, 0x80000020U, 0x00000000U, 0x00108000U, 75*7c478bd9Sstevel@tonic-gate 0x00008020U, 0x80108000U, 0x80100000U, 0x00008020U, 76*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x00108020U, 0x80100020U, 0x00100000U, 77*7c478bd9Sstevel@tonic-gate 0x80008020U, 0x80100000U, 0x80108000U, 0x00008000U, 78*7c478bd9Sstevel@tonic-gate 0x80100000U, 0x80008000U, 0x00000020U, 0x80108020U, 79*7c478bd9Sstevel@tonic-gate 0x00108020U, 0x00000020U, 0x00008000U, 0x80000000U, 80*7c478bd9Sstevel@tonic-gate 0x00008020U, 0x80108000U, 0x00100000U, 0x80000020U, 81*7c478bd9Sstevel@tonic-gate 0x00100020U, 0x80008020U, 0x80000020U, 0x00100020U, 82*7c478bd9Sstevel@tonic-gate 0x00108000U, 0x00000000U, 0x80008000U, 0x00008020U, 83*7c478bd9Sstevel@tonic-gate 0x80000000U, 0x80100020U, 0x80108020U, 0x00108000U, 84*7c478bd9Sstevel@tonic-gate 0x00000208U, 0x08020200U, 0x00000000U, 0x08020008U, 85*7c478bd9Sstevel@tonic-gate 0x08000200U, 0x00000000U, 0x00020208U, 0x08000200U, 86*7c478bd9Sstevel@tonic-gate 0x00020008U, 0x08000008U, 0x08000008U, 0x00020000U, 87*7c478bd9Sstevel@tonic-gate 0x08020208U, 0x00020008U, 0x08020000U, 0x00000208U, 88*7c478bd9Sstevel@tonic-gate 0x08000000U, 0x00000008U, 0x08020200U, 0x00000200U, 89*7c478bd9Sstevel@tonic-gate 0x00020200U, 0x08020000U, 0x08020008U, 0x00020208U, 90*7c478bd9Sstevel@tonic-gate 0x08000208U, 0x00020200U, 0x00020000U, 0x08000208U, 91*7c478bd9Sstevel@tonic-gate 0x00000008U, 0x08020208U, 0x00000200U, 0x08000000U, 92*7c478bd9Sstevel@tonic-gate 0x08020200U, 0x08000000U, 0x00020008U, 0x00000208U, 93*7c478bd9Sstevel@tonic-gate 0x00020000U, 0x08020200U, 0x08000200U, 0x00000000U, 94*7c478bd9Sstevel@tonic-gate 0x00000200U, 0x00020008U, 0x08020208U, 0x08000200U, 95*7c478bd9Sstevel@tonic-gate 0x08000008U, 0x00000200U, 0x00000000U, 0x08020008U, 96*7c478bd9Sstevel@tonic-gate 0x08000208U, 0x00020000U, 0x08000000U, 0x08020208U, 97*7c478bd9Sstevel@tonic-gate 0x00000008U, 0x00020208U, 0x00020200U, 0x08000008U, 98*7c478bd9Sstevel@tonic-gate 0x08020000U, 0x08000208U, 0x00000208U, 0x08020000U, 99*7c478bd9Sstevel@tonic-gate 0x00020208U, 0x00000008U, 0x08020008U, 0x00020200U, 100*7c478bd9Sstevel@tonic-gate 0x00802001U, 0x00002081U, 0x00002081U, 0x00000080U, 101*7c478bd9Sstevel@tonic-gate 0x00802080U, 0x00800081U, 0x00800001U, 0x00002001U, 102*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x00802000U, 0x00802000U, 0x00802081U, 103*7c478bd9Sstevel@tonic-gate 0x00000081U, 0x00000000U, 0x00800080U, 0x00800001U, 104*7c478bd9Sstevel@tonic-gate 0x00000001U, 0x00002000U, 0x00800000U, 0x00802001U, 105*7c478bd9Sstevel@tonic-gate 0x00000080U, 0x00800000U, 0x00002001U, 0x00002080U, 106*7c478bd9Sstevel@tonic-gate 0x00800081U, 0x00000001U, 0x00002080U, 0x00800080U, 107*7c478bd9Sstevel@tonic-gate 0x00002000U, 0x00802080U, 0x00802081U, 0x00000081U, 108*7c478bd9Sstevel@tonic-gate 0x00800080U, 0x00800001U, 0x00802000U, 0x00802081U, 109*7c478bd9Sstevel@tonic-gate 0x00000081U, 0x00000000U, 0x00000000U, 0x00802000U, 110*7c478bd9Sstevel@tonic-gate 0x00002080U, 0x00800080U, 0x00800081U, 0x00000001U, 111*7c478bd9Sstevel@tonic-gate 0x00802001U, 0x00002081U, 0x00002081U, 0x00000080U, 112*7c478bd9Sstevel@tonic-gate 0x00802081U, 0x00000081U, 0x00000001U, 0x00002000U, 113*7c478bd9Sstevel@tonic-gate 0x00800001U, 0x00002001U, 0x00802080U, 0x00800081U, 114*7c478bd9Sstevel@tonic-gate 0x00002001U, 0x00002080U, 0x00800000U, 0x00802001U, 115*7c478bd9Sstevel@tonic-gate 0x00000080U, 0x00800000U, 0x00002000U, 0x00802080U, 116*7c478bd9Sstevel@tonic-gate 0x00000100U, 0x02080100U, 0x02080000U, 0x42000100U, 117*7c478bd9Sstevel@tonic-gate 0x00080000U, 0x00000100U, 0x40000000U, 0x02080000U, 118*7c478bd9Sstevel@tonic-gate 0x40080100U, 0x00080000U, 0x02000100U, 0x40080100U, 119*7c478bd9Sstevel@tonic-gate 0x42000100U, 0x42080000U, 0x00080100U, 0x40000000U, 120*7c478bd9Sstevel@tonic-gate 0x02000000U, 0x40080000U, 0x40080000U, 0x00000000U, 121*7c478bd9Sstevel@tonic-gate 0x40000100U, 0x42080100U, 0x42080100U, 0x02000100U, 122*7c478bd9Sstevel@tonic-gate 0x42080000U, 0x40000100U, 0x00000000U, 0x42000000U, 123*7c478bd9Sstevel@tonic-gate 0x02080100U, 0x02000000U, 0x42000000U, 0x00080100U, 124*7c478bd9Sstevel@tonic-gate 0x00080000U, 0x42000100U, 0x00000100U, 0x02000000U, 125*7c478bd9Sstevel@tonic-gate 0x40000000U, 0x02080000U, 0x42000100U, 0x40080100U, 126*7c478bd9Sstevel@tonic-gate 0x02000100U, 0x40000000U, 0x42080000U, 0x02080100U, 127*7c478bd9Sstevel@tonic-gate 0x40080100U, 0x00000100U, 0x02000000U, 0x42080000U, 128*7c478bd9Sstevel@tonic-gate 0x42080100U, 0x00080100U, 0x42000000U, 0x42080100U, 129*7c478bd9Sstevel@tonic-gate 0x02080000U, 0x00000000U, 0x40080000U, 0x42000000U, 130*7c478bd9Sstevel@tonic-gate 0x00080100U, 0x02000100U, 0x40000100U, 0x00080000U, 131*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x40080000U, 0x02080100U, 0x40000100U, 132*7c478bd9Sstevel@tonic-gate 0x20000010U, 0x20400000U, 0x00004000U, 0x20404010U, 133*7c478bd9Sstevel@tonic-gate 0x20400000U, 0x00000010U, 0x20404010U, 0x00400000U, 134*7c478bd9Sstevel@tonic-gate 0x20004000U, 0x00404010U, 0x00400000U, 0x20000010U, 135*7c478bd9Sstevel@tonic-gate 0x00400010U, 0x20004000U, 0x20000000U, 0x00004010U, 136*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x00400010U, 0x20004010U, 0x00004000U, 137*7c478bd9Sstevel@tonic-gate 0x00404000U, 0x20004010U, 0x00000010U, 0x20400010U, 138*7c478bd9Sstevel@tonic-gate 0x20400010U, 0x00000000U, 0x00404010U, 0x20404000U, 139*7c478bd9Sstevel@tonic-gate 0x00004010U, 0x00404000U, 0x20404000U, 0x20000000U, 140*7c478bd9Sstevel@tonic-gate 0x20004000U, 0x00000010U, 0x20400010U, 0x00404000U, 141*7c478bd9Sstevel@tonic-gate 0x20404010U, 0x00400000U, 0x00004010U, 0x20000010U, 142*7c478bd9Sstevel@tonic-gate 0x00400000U, 0x20004000U, 0x20000000U, 0x00004010U, 143*7c478bd9Sstevel@tonic-gate 0x20000010U, 0x20404010U, 0x00404000U, 0x20400000U, 144*7c478bd9Sstevel@tonic-gate 0x00404010U, 0x20404000U, 0x00000000U, 0x20400010U, 145*7c478bd9Sstevel@tonic-gate 0x00000010U, 0x00004000U, 0x20400000U, 0x00404010U, 146*7c478bd9Sstevel@tonic-gate 0x00004000U, 0x00400010U, 0x20004010U, 0x00000000U, 147*7c478bd9Sstevel@tonic-gate 0x20404000U, 0x20000000U, 0x00400010U, 0x20004010U, 148*7c478bd9Sstevel@tonic-gate 0x00200000U, 0x04200002U, 0x04000802U, 0x00000000U, 149*7c478bd9Sstevel@tonic-gate 0x00000800U, 0x04000802U, 0x00200802U, 0x04200800U, 150*7c478bd9Sstevel@tonic-gate 0x04200802U, 0x00200000U, 0x00000000U, 0x04000002U, 151*7c478bd9Sstevel@tonic-gate 0x00000002U, 0x04000000U, 0x04200002U, 0x00000802U, 152*7c478bd9Sstevel@tonic-gate 0x04000800U, 0x00200802U, 0x00200002U, 0x04000800U, 153*7c478bd9Sstevel@tonic-gate 0x04000002U, 0x04200000U, 0x04200800U, 0x00200002U, 154*7c478bd9Sstevel@tonic-gate 0x04200000U, 0x00000800U, 0x00000802U, 0x04200802U, 155*7c478bd9Sstevel@tonic-gate 0x00200800U, 0x00000002U, 0x04000000U, 0x00200800U, 156*7c478bd9Sstevel@tonic-gate 0x04000000U, 0x00200800U, 0x00200000U, 0x04000802U, 157*7c478bd9Sstevel@tonic-gate 0x04000802U, 0x04200002U, 0x04200002U, 0x00000002U, 158*7c478bd9Sstevel@tonic-gate 0x00200002U, 0x04000000U, 0x04000800U, 0x00200000U, 159*7c478bd9Sstevel@tonic-gate 0x04200800U, 0x00000802U, 0x00200802U, 0x04200800U, 160*7c478bd9Sstevel@tonic-gate 0x00000802U, 0x04000002U, 0x04200802U, 0x04200000U, 161*7c478bd9Sstevel@tonic-gate 0x00200800U, 0x00000000U, 0x00000002U, 0x04200802U, 162*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x00200802U, 0x04200000U, 0x00000800U, 163*7c478bd9Sstevel@tonic-gate 0x04000002U, 0x04000800U, 0x00000800U, 0x00200002U, 164*7c478bd9Sstevel@tonic-gate 0x10001040U, 0x00001000U, 0x00040000U, 0x10041040U, 165*7c478bd9Sstevel@tonic-gate 0x10000000U, 0x10001040U, 0x00000040U, 0x10000000U, 166*7c478bd9Sstevel@tonic-gate 0x00040040U, 0x10040000U, 0x10041040U, 0x00041000U, 167*7c478bd9Sstevel@tonic-gate 0x10041000U, 0x00041040U, 0x00001000U, 0x00000040U, 168*7c478bd9Sstevel@tonic-gate 0x10040000U, 0x10000040U, 0x10001000U, 0x00001040U, 169*7c478bd9Sstevel@tonic-gate 0x00041000U, 0x00040040U, 0x10040040U, 0x10041000U, 170*7c478bd9Sstevel@tonic-gate 0x00001040U, 0x00000000U, 0x00000000U, 0x10040040U, 171*7c478bd9Sstevel@tonic-gate 0x10000040U, 0x10001000U, 0x00041040U, 0x00040000U, 172*7c478bd9Sstevel@tonic-gate 0x00041040U, 0x00040000U, 0x10041000U, 0x00001000U, 173*7c478bd9Sstevel@tonic-gate 0x00000040U, 0x10040040U, 0x00001000U, 0x00041040U, 174*7c478bd9Sstevel@tonic-gate 0x10001000U, 0x00000040U, 0x10000040U, 0x10040000U, 175*7c478bd9Sstevel@tonic-gate 0x10040040U, 0x10000000U, 0x00040000U, 0x10001040U, 176*7c478bd9Sstevel@tonic-gate 0x00000000U, 0x10041040U, 0x00040040U, 0x10000040U, 177*7c478bd9Sstevel@tonic-gate 0x10040000U, 0x10001000U, 0x10001040U, 0x00000000U, 178*7c478bd9Sstevel@tonic-gate 0x10041040U, 0x00041000U, 0x00041000U, 0x00001040U, 179*7c478bd9Sstevel@tonic-gate 0x00001040U, 0x00040040U, 0x10000000U, 0x10041000U, 180*7c478bd9Sstevel@tonic-gate }; 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate /* 183*7c478bd9Sstevel@tonic-gate * Primitive function F. 184*7c478bd9Sstevel@tonic-gate * Input is r, subkey array in keys, output is XORed into l. 185*7c478bd9Sstevel@tonic-gate * Each round consumes eight 6-bit subkeys, one for 186*7c478bd9Sstevel@tonic-gate * each of the 8 S-boxes, 2 longs for each round. 187*7c478bd9Sstevel@tonic-gate * Each long contains four 6-bit subkeys, each taking up a byte. 188*7c478bd9Sstevel@tonic-gate * The first long contains, from high to low end, the subkeys for 189*7c478bd9Sstevel@tonic-gate * S-boxes 1, 3, 5 & 7; the second contains the subkeys for S-boxes 190*7c478bd9Sstevel@tonic-gate * 2, 4, 6 & 8 (using the origin-1 S-box numbering in the standard, 191*7c478bd9Sstevel@tonic-gate * not the origin-0 numbering used elsewhere in this code) 192*7c478bd9Sstevel@tonic-gate * See comments elsewhere about the pre-rotated values of r and Spbox. 193*7c478bd9Sstevel@tonic-gate */ 194*7c478bd9Sstevel@tonic-gate #define F(l, r, key) {\ 195*7c478bd9Sstevel@tonic-gate work = ((r >> 4) | (r << 28)) ^ (key)[0];\ 196*7c478bd9Sstevel@tonic-gate l ^= Spbox[6][work & 0x3f];\ 197*7c478bd9Sstevel@tonic-gate l ^= Spbox[4][(work >> 8) & 0x3f];\ 198*7c478bd9Sstevel@tonic-gate l ^= Spbox[2][(work >> 16) & 0x3f];\ 199*7c478bd9Sstevel@tonic-gate l ^= Spbox[0][(work >> 24) & 0x3f];\ 200*7c478bd9Sstevel@tonic-gate work = r ^ (key)[1];\ 201*7c478bd9Sstevel@tonic-gate l ^= Spbox[7][work & 0x3f];\ 202*7c478bd9Sstevel@tonic-gate l ^= Spbox[5][(work >> 8) & 0x3f];\ 203*7c478bd9Sstevel@tonic-gate l ^= Spbox[3][(work >> 16) & 0x3f];\ 204*7c478bd9Sstevel@tonic-gate l ^= Spbox[1][(work >> 24) & 0x3f];\ 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate /* Encrypt or decrypt a block of data in ECB mode */ 208*7c478bd9Sstevel@tonic-gate void 209*7c478bd9Sstevel@tonic-gate des(void *cookie, uint8_t *block) 210*7c478bd9Sstevel@tonic-gate { 211*7c478bd9Sstevel@tonic-gate uint32_t *ks = (uint32_t *)cookie; 212*7c478bd9Sstevel@tonic-gate uint32_t left; 213*7c478bd9Sstevel@tonic-gate uint32_t right; 214*7c478bd9Sstevel@tonic-gate uint32_t work; 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate /* Read input block and place in left/right in big-endian order */ 217*7c478bd9Sstevel@tonic-gate left = ((uint32_t)block[0] << 24) | 218*7c478bd9Sstevel@tonic-gate ((uint32_t)block[1] << 16) | 219*7c478bd9Sstevel@tonic-gate ((uint32_t)block[2] << 8) | 220*7c478bd9Sstevel@tonic-gate (uint32_t)block[3]; 221*7c478bd9Sstevel@tonic-gate right = ((uint32_t)block[4] << 24) | 222*7c478bd9Sstevel@tonic-gate ((uint32_t)block[5] << 16) | 223*7c478bd9Sstevel@tonic-gate ((uint32_t)block[6] << 8) | 224*7c478bd9Sstevel@tonic-gate (uint32_t)block[7]; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate /* 227*7c478bd9Sstevel@tonic-gate * Hoey's clever initial permutation algorithm, from Outerbridge 228*7c478bd9Sstevel@tonic-gate * (see Schneier p 478) 229*7c478bd9Sstevel@tonic-gate * 230*7c478bd9Sstevel@tonic-gate * The convention here is the same as Outerbridge: rotate each 231*7c478bd9Sstevel@tonic-gate * register left by 1 bit, i.e., so that "left" contains permuted 232*7c478bd9Sstevel@tonic-gate * input bits 2, 3, 4, ... 1 and "right" contains 33, 34, 35, ... 32 233*7c478bd9Sstevel@tonic-gate * (using origin-1 numbering as in the FIPS). This allows us to avoid 234*7c478bd9Sstevel@tonic-gate * one of the two rotates that would otherwise be required in each of 235*7c478bd9Sstevel@tonic-gate * the 16 rounds. 236*7c478bd9Sstevel@tonic-gate */ 237*7c478bd9Sstevel@tonic-gate work = ((left >> 4) ^ right) & 0x0f0f0f0f; 238*7c478bd9Sstevel@tonic-gate right ^= work; 239*7c478bd9Sstevel@tonic-gate left ^= work << 4; 240*7c478bd9Sstevel@tonic-gate work = ((left >> 16) ^ right) & 0xffff; 241*7c478bd9Sstevel@tonic-gate right ^= work; 242*7c478bd9Sstevel@tonic-gate left ^= work << 16; 243*7c478bd9Sstevel@tonic-gate work = ((right >> 2) ^ left) & 0x33333333; 244*7c478bd9Sstevel@tonic-gate left ^= work; 245*7c478bd9Sstevel@tonic-gate right ^= (work << 2); 246*7c478bd9Sstevel@tonic-gate work = ((right >> 8) ^ left) & 0xff00ff; 247*7c478bd9Sstevel@tonic-gate left ^= work; 248*7c478bd9Sstevel@tonic-gate right ^= (work << 8); 249*7c478bd9Sstevel@tonic-gate right = (right << 1) | (right >> 31); 250*7c478bd9Sstevel@tonic-gate work = (left ^ right) & 0xaaaaaaaa; 251*7c478bd9Sstevel@tonic-gate left ^= work; 252*7c478bd9Sstevel@tonic-gate right ^= work; 253*7c478bd9Sstevel@tonic-gate left = (left << 1) | (left >> 31); 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate /* First key */ 256*7c478bd9Sstevel@tonic-gate F(left, right, ks); 257*7c478bd9Sstevel@tonic-gate F(right, left, ks + 2); 258*7c478bd9Sstevel@tonic-gate F(left, right, ks + 4); 259*7c478bd9Sstevel@tonic-gate F(right, left, ks + 6); 260*7c478bd9Sstevel@tonic-gate F(left, right, ks + 8); 261*7c478bd9Sstevel@tonic-gate F(right, left, ks + 10); 262*7c478bd9Sstevel@tonic-gate F(left, right, ks + 12); 263*7c478bd9Sstevel@tonic-gate F(right, left, ks + 14); 264*7c478bd9Sstevel@tonic-gate F(left, right, ks + 16); 265*7c478bd9Sstevel@tonic-gate F(right, left, ks + 18); 266*7c478bd9Sstevel@tonic-gate F(left, right, ks + 20); 267*7c478bd9Sstevel@tonic-gate F(right, left, ks + 22); 268*7c478bd9Sstevel@tonic-gate F(left, right, ks + 24); 269*7c478bd9Sstevel@tonic-gate F(right, left, ks + 26); 270*7c478bd9Sstevel@tonic-gate F(left, right, ks + 28); 271*7c478bd9Sstevel@tonic-gate F(right, left, ks + 30); 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate /* Inverse permutation, also from Hoey via Outerbridge and Schneier */ 274*7c478bd9Sstevel@tonic-gate right = (right << 31) | (right >> 1); 275*7c478bd9Sstevel@tonic-gate work = (left ^ right) & 0xaaaaaaaa; 276*7c478bd9Sstevel@tonic-gate left ^= work; 277*7c478bd9Sstevel@tonic-gate right ^= work; 278*7c478bd9Sstevel@tonic-gate left = (left >> 1) | (left << 31); 279*7c478bd9Sstevel@tonic-gate work = ((left >> 8) ^ right) & 0xff00ff; 280*7c478bd9Sstevel@tonic-gate right ^= work; 281*7c478bd9Sstevel@tonic-gate left ^= work << 8; 282*7c478bd9Sstevel@tonic-gate work = ((left >> 2) ^ right) & 0x33333333; 283*7c478bd9Sstevel@tonic-gate right ^= work; 284*7c478bd9Sstevel@tonic-gate left ^= work << 2; 285*7c478bd9Sstevel@tonic-gate work = ((right >> 16) ^ left) & 0xffff; 286*7c478bd9Sstevel@tonic-gate left ^= work; 287*7c478bd9Sstevel@tonic-gate right ^= work << 16; 288*7c478bd9Sstevel@tonic-gate work = ((right >> 4) ^ left) & 0x0f0f0f0f; 289*7c478bd9Sstevel@tonic-gate left ^= work; 290*7c478bd9Sstevel@tonic-gate right ^= work << 4; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate /* Put the block back into the user's buffer with final swap */ 293*7c478bd9Sstevel@tonic-gate block[0] = right >> 24; 294*7c478bd9Sstevel@tonic-gate block[1] = right >> 16; 295*7c478bd9Sstevel@tonic-gate block[2] = right >> 8; 296*7c478bd9Sstevel@tonic-gate block[3] = right; 297*7c478bd9Sstevel@tonic-gate block[4] = left >> 24; 298*7c478bd9Sstevel@tonic-gate block[5] = left >> 16; 299*7c478bd9Sstevel@tonic-gate block[6] = left >> 8; 300*7c478bd9Sstevel@tonic-gate block[7] = left; 301*7c478bd9Sstevel@tonic-gate } 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate /* Key schedule-related tables from FIPS-46 */ 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate /* permuted choice table (key) */ 306*7c478bd9Sstevel@tonic-gate static unsigned char pc1[] = { 307*7c478bd9Sstevel@tonic-gate 57, 49, 41, 33, 25, 17, 9, 308*7c478bd9Sstevel@tonic-gate 1, 58, 50, 42, 34, 26, 18, 309*7c478bd9Sstevel@tonic-gate 10, 2, 59, 51, 43, 35, 27, 310*7c478bd9Sstevel@tonic-gate 19, 11, 3, 60, 52, 44, 36, 311*7c478bd9Sstevel@tonic-gate 63, 55, 47, 39, 31, 23, 15, 312*7c478bd9Sstevel@tonic-gate 7, 62, 54, 46, 38, 30, 22, 313*7c478bd9Sstevel@tonic-gate 14, 6, 61, 53, 45, 37, 29, 314*7c478bd9Sstevel@tonic-gate 21, 13, 5, 28, 20, 12, 4 315*7c478bd9Sstevel@tonic-gate }; 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate /* number left rotations of pc1 */ 318*7c478bd9Sstevel@tonic-gate static unsigned char totrot[] = { 319*7c478bd9Sstevel@tonic-gate 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 320*7c478bd9Sstevel@tonic-gate }; 321*7c478bd9Sstevel@tonic-gate 322*7c478bd9Sstevel@tonic-gate /* permuted choice key (table) */ 323*7c478bd9Sstevel@tonic-gate static unsigned char pc2[] = { 324*7c478bd9Sstevel@tonic-gate 14, 17, 11, 24, 1, 5, 325*7c478bd9Sstevel@tonic-gate 3, 28, 15, 6, 21, 10, 326*7c478bd9Sstevel@tonic-gate 23, 19, 12, 4, 26, 8, 327*7c478bd9Sstevel@tonic-gate 16, 7, 27, 20, 13, 2, 328*7c478bd9Sstevel@tonic-gate 41, 52, 31, 37, 47, 55, 329*7c478bd9Sstevel@tonic-gate 30, 40, 51, 45, 33, 48, 330*7c478bd9Sstevel@tonic-gate 44, 49, 39, 56, 34, 53, 331*7c478bd9Sstevel@tonic-gate 46, 42, 50, 36, 29, 32 332*7c478bd9Sstevel@tonic-gate }; 333*7c478bd9Sstevel@tonic-gate 334*7c478bd9Sstevel@tonic-gate /* End of DES-defined tables */ 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate /* bit 0 is left-most in byte */ 338*7c478bd9Sstevel@tonic-gate static int bytebit[] = { 339*7c478bd9Sstevel@tonic-gate 0200, 0100, 040, 020, 010, 04, 02, 01 340*7c478bd9Sstevel@tonic-gate }; 341*7c478bd9Sstevel@tonic-gate 342*7c478bd9Sstevel@tonic-gate /* 343*7c478bd9Sstevel@tonic-gate * Generate key schedule for encryption or decryption 344*7c478bd9Sstevel@tonic-gate * depending on the value of "decrypt" 345*7c478bd9Sstevel@tonic-gate */ 346*7c478bd9Sstevel@tonic-gate void 347*7c478bd9Sstevel@tonic-gate des_key(DES_KS k, const unsigned char *key, int decrypt) 348*7c478bd9Sstevel@tonic-gate { 349*7c478bd9Sstevel@tonic-gate unsigned char pc1m[56]; /* place to modify pc1 into */ 350*7c478bd9Sstevel@tonic-gate unsigned char pcr[56]; /* place to rotate pc1 into */ 351*7c478bd9Sstevel@tonic-gate int i; 352*7c478bd9Sstevel@tonic-gate int j; 353*7c478bd9Sstevel@tonic-gate int l; 354*7c478bd9Sstevel@tonic-gate int m; 355*7c478bd9Sstevel@tonic-gate unsigned char ks[8]; 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ 358*7c478bd9Sstevel@tonic-gate l = pc1[j] - 1; /* integer bit location */ 359*7c478bd9Sstevel@tonic-gate m = l & 07; /* find bit */ 360*7c478bd9Sstevel@tonic-gate pc1m[j] = (key[l >>3 ] /* find which key byte l is in */ 361*7c478bd9Sstevel@tonic-gate & bytebit[m]) /* and which bit of that byte */ 362*7c478bd9Sstevel@tonic-gate ? 1 : 0; /* and store 1-bit result */ 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) { /* key chunk for each iteration */ 365*7c478bd9Sstevel@tonic-gate bzero(ks, sizeof (ks)); /* Clear key schedule */ 366*7c478bd9Sstevel@tonic-gate for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ 367*7c478bd9Sstevel@tonic-gate pcr[j] = pc1m[(l = j + totrot[decrypt ? 15 - i : i]) < 368*7c478bd9Sstevel@tonic-gate (j < 28 ? 28 : 56) ? l : l - 28]; 369*7c478bd9Sstevel@tonic-gate /* rotate left and right halves independently */ 370*7c478bd9Sstevel@tonic-gate for (j = 0; j < 48; j++) { /* select bits individually */ 371*7c478bd9Sstevel@tonic-gate /* check bit that goes to ks[j] */ 372*7c478bd9Sstevel@tonic-gate if (pcr[pc2[j] - 1]) { 373*7c478bd9Sstevel@tonic-gate /* mask it in if it's there */ 374*7c478bd9Sstevel@tonic-gate l = j % 6; 375*7c478bd9Sstevel@tonic-gate ks[j/6] |= bytebit[l] >> 2; 376*7c478bd9Sstevel@tonic-gate } 377*7c478bd9Sstevel@tonic-gate } 378*7c478bd9Sstevel@tonic-gate /* Now convert to packed odd/even interleaved form */ 379*7c478bd9Sstevel@tonic-gate k[i][0] = ((uint32_t)ks[0] << 24) | 380*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[2] << 16) | 381*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[4] << 8) | 382*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[6]); 383*7c478bd9Sstevel@tonic-gate k[i][1] = ((uint32_t)ks[1] << 24) | 384*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[3] << 16) | 385*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[5] << 8) | 386*7c478bd9Sstevel@tonic-gate ((uint32_t)ks[7]); 387*7c478bd9Sstevel@tonic-gate } 388*7c478bd9Sstevel@tonic-gate } 389