1/*2* Fast implementation of the DES, as described in the Federal Register,3* Vol. 40, No. 52, p. 12134, March 17, 1975.4*5* Stuart Levy, Minnesota Supercomputer Center, April 1988.6* Currently (2007) [email protected]7* NCSA, University of Illinois Urbana-Champaign8*9* Calling sequence:10*11* typedef unsigned long keysched[32];12*13* fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * /14* unsigned char key[8];15* keysched *ks;16*17* fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * /18* unsigned char block[8]; / * data, en/decrypted in place * /19* int decrypt; / * 0=>encrypt, 1=>decrypt * /20* keysched *ks; / * key schedule, as set by fsetkey * /21*22* Key and data block representation:23* The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64)24* and the 64-bit data block (bits 1..64)25* are each stored in arrays of 8 bytes.26* Following the NBS numbering, the MSB has the bit number 1, so27* key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through28* key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64.29* In the key, "parity" bits are not checked; their values are ignored.30*31*/3233/*34===============================================================================35License36des56.c is licensed under the terms of the MIT license reproduced below.37This means that des56.c is free software and can be used for both academic38and commercial purposes at absolutely no cost.39===============================================================================40Copyright (C) 1988 Stuart Levy41Permission is hereby granted, free of charge, to any person obtaining a copy42of this software and associated documentation files (the "Software"), to deal43in the Software without restriction, including without limitation the rights44to use, copy, modify, merge, publish, distribute, sublicense, and/or sell45copies of the Software, and to permit persons to whom the Software is46furnished to do so, subject to the following conditions:47The above copyright notice and this permission notice shall be included in48all copies or substantial portions of the Software.49THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR50IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,51FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE52AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER53LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,54OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN55THE SOFTWARE.56*/575859#include "des56.h"606162/*63* Key schedule generation.64* We begin by pointlessly permuting the 56 useful key bits into65* two groups of 28 bits called C and D.66* bK_C and bK_D are indexed by C and D bit numbers, respectively,67* and give the key bit number (1..64) which should initialize that C/D bit.68* This is the "permuted choice 1" table.69*/7071static tiny bK_C[28] = {7257, 49, 41, 33, 25, 17, 9,731, 58, 50, 42, 34, 26, 18,7410, 2, 59, 51, 43, 35, 27,7519, 11, 3, 60, 52, 44, 36,76};77static tiny bK_D[28] = {7863, 55, 47, 39, 31, 23, 15,797, 62, 54, 46, 38, 30, 22,8014, 6, 61, 53, 45, 37, 29,8121, 13, 5, 28, 20, 12, 4,82};8384/*85* For speed, we invert these, building tables to map groups of86* key bits into the corresponding C and D bits.87* We represent C and D each as 28 contiguous bits right-justified in a88* word, padded on the left with zeros.89* If key byte `i' is said to contain bits Ki,0 (MSB) Ki,1 ... Ki,7 (LSB)90* then91* wC_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] gives the C bits for Ki,0..3,92* wD_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] the corresponding D bits,93* wC_K3[i][Ki,4 Ki,5 Ki,6] the C bits for Ki,4..6,94* and wD_K3[i][Ki,4 Ki,5 Ki,6] the D bits for Ki,4..6.95* Ki,7 is ignored since it is the nominal parity bit.96* We could just use a single table for [i][Ki,0 .. Ki,6] but that97* would take a lot of storage for such a rarely-used function.98*/99100static word32 wC_K4[8][16], wC_K3[8][8];101static word32 wD_K4[8][16], wD_K3[8][8];102103/*104* Successive Ci and Di for the sixteen steps in the key schedule are105* created by independent 28-bit left circular shifts on C and D.106* The shift count varies with the step number.107*/108static tiny preshift[16] = {1091, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,110};111112/*113* Each step in the key schedule is generated by selecting 48 bits114* (8 groups of 6 bits) from the appropriately shifted Ci and Di.115* bCD_KS, indexed by the key schedule bit number, gives the bit number116* in CD (CD1 = MSB of C, CD28 = LSB of C, CD29 = MSB of D, CD56 = LSB of D)117* which determines that bit of the key schedule.118* Note that only C bits (1..28) appear in the first (upper) 24 bits of119* the key schedule, and D bits (29..56) in the second (lower) 24 bits.120* This is the "permuted-choice-2" table.121*/122123static tiny bCD_KS[48] = {12414, 17, 11, 24, 1, 5,1253, 28, 15, 6, 21, 10,12623, 19, 12, 4, 26, 8,12716, 7, 27, 20, 13, 2,12841, 52, 31, 37, 47, 55,12930, 40, 51, 45, 33, 48,13044, 49, 39, 56, 34, 53,13146, 42, 50, 36, 29, 32,132};133134/*135* We invert bCD_KS into a pair of tables which map groups of 4136* C or D bits into corresponding key schedule bits.137* We represent each step of the key schedule as 8 groups of 8 bits,138* with the 6 real bits right-justified in each 8-bit group.139* hKS_C4[i][C4i+1 .. C4i+4] gives the bits in the high order (first four)140* key schedule "bytes" which correspond to C bits 4i+1 .. 4i+4.141* lKS_D4[i][D4i+1 .. D4i+4] gives the appropriate bits in the latter (last 4)142* key schedule bytes, from the corresponding D bits.143*/144145static word32 hKS_C4[7][16];146static word32 lKS_D4[7][16];147148/*149* Encryption/decryption.150* Before beginning, and after ending, we perform another useless permutation151* on the bits in the data block.152*153* The initial permutation and its inverse, final permutation154* are too simple to need a table for. If we break the input I1 .. I64 into155* 8-bit chunks I0,0 I0,1 ... I0,7 I1,0 I1,1 ... I7,7156* then the initial permutation sets LR as follows:157* L = I7,1 I6,1 I5,1 ... I0,1 I7,3 I6,3 ... I0,3 I7,5 ... I0,5 I7,7 ... I0,7158* and159* R = I7,0 I6,0 I5,0 ... I0,0 I7,2 I6,2 ... I0,2 I7,4 ... I0,4 I7,6 ... I0,6160*161* If we number the bits in the final LR similarly,162* L = L0,0 L0,1 ... L3,7 R = R0,0 R0,1 ... R3,7163* then the output is164* O = R0,7 L0,7 R1,7 L1,7 ... R3,7 L3,7 R0,6 L0,6 ... L3,6 R0,5 ... R3,0 L3,0165*166* To speed I => LR shuffling we use an array of 32-bit values indexed by167* 8-bit input bytes.168* wL_I8[ 0 I0,1 0 I0,3 0 I0,5 0 I0,7 ] = the corresponding L bits.169* Other R and L bits are derived from wL_I8 by shifting.170*171* To speed LR => O shuffling, an array of 32-bit values indexed by 4-bit lumps:172* wO_L4[ L0,4 L0,5 L0,6 L0,7 ] = the corresponding high-order 32 O bits.173*/174175static word32 wL_I8[0x55 + 1];176static word32 wO_L4[16];177178/*179* Core of encryption/decryption.180* In each key schedule stage, we:181* take 8 overlapping groups of 6 bits each from R182* (the NBS tabulates the bit selections in the E table,183* but it's so simple we just use shifting to get the right bits)184* XOR each group with the corresponding bits from the key schedule185* Use the resulting 6 bits as an index into the appropriate S table186* (there are 8 such tables, one per group of 6 bits)187* Each S entry yields 4 bits.188* The 8 groups of 4 bits are catenated into a 32-bit value.189* Those 32 bits are permuted according to the P table.190* Finally the permuted 32-bit value is XORed with L and becomes191* the R value for the next stage, while the previous R becomes the new L.192*193* Here, we merge the P permutation with the S tables by making the194* S entries be 32-bit masks, already suitably permuted.195* Also, the bits in each six-bit group must be permuted before use as196* an index into the NBS-tabulated S tables.197* We rearrange entries in wPS so that natural bit order can be used.198*/199200static word32 wPS[8][64];201202static tiny P[32] = {20316, 7, 20, 21,20429, 12, 28, 17,2051, 15, 23, 26,2065, 18, 31, 10,2072, 8, 24, 14,20832, 27, 3, 9,20919, 13, 30, 6,21022, 11, 4, 25,211};212213static tiny S[8][64] = {214{21514, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,2160,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,2174, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,21815,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,219},220221{22215, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,2233,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,2240,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,22513, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,226},227228{22910, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,23013, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,23113, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,2321,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,233},234235{2367,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,23713, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,23810, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,2393,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,240},241242{2432,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,24414,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,2454, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,24611, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,247},248249{25012, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,25110,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,2529,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,2534, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,254},255256{2574,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,25813, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,2591, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,2606,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,261},262263{26413, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,2651,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,2667,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,2672, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,268},269};270271static void buildtables( void )272{273register int i, j;274register word32 v;275word32 wC_K[64], wD_K[64];276word32 hKS_C[28], lKS_D[28];277int Smap[64];278word32 wP[32];279280#if USG281# define ZERO(array) memset((char *)(array), '\0', sizeof(array))282#else283# if BSD284# define ZERO(array) bzero((char *)(array), sizeof(array))285# else286# define ZERO(array) { register word32 *p = (word32 *)(array); \287i = sizeof(array) / sizeof(*p); \288do { *p++ = 0; } while(--i > 0); \289}290# endif291#endif292293294/* Invert permuted-choice-1 (key => C,D) */295296ZERO(wC_K);297ZERO(wD_K);298v = 1;299for(j = 28; --j >= 0; ) {300wC_K[ bK_C[j] - 1 ] = wD_K[ bK_D[j] - 1 ] = v;301v += v; /* (i.e. v <<= 1) */302}303304for(i = 0; i < 64; i++) {305int t = 8 >> (i & 3);306for(j = 0; j < 16; j++) {307if(j & t) {308wC_K4[i >> 3][j] |= wC_K[i];309wD_K4[i >> 3][j] |= wD_K[i];310if(j < 8) {311wC_K3[i >> 3][j] |= wC_K[i + 3];312wD_K3[i >> 3][j] |= wD_K[i + 3];313}314}315}316/* Generate the sequence 0,1,2,3, 8,9,10,11, ..., 56,57,58,59. */317if(t == 1) i += 4;318}319320/* Invert permuted-choice-2 */321322ZERO(hKS_C);323ZERO(lKS_D);324v = 1;325for(i = 24; (i -= 6) >= 0; ) {326j = i+5;327do {328hKS_C[ bCD_KS[j] - 1 ] = lKS_D[ bCD_KS[j+24] - 28 - 1 ] = v;329v += v; /* Like v <<= 1 but may be faster */330} while(--j >= i);331v <<= 2; /* Keep byte aligned */332}333334for(i = 0; i < 28; i++) {335v = 8 >> (i & 3);336for(j = 0; j < 16; j++) {337if(j & v) {338hKS_C4[i >> 2][j] |= hKS_C[i];339lKS_D4[i >> 2][j] |= lKS_D[i];340}341}342}343344/* Initial permutation */345346for(i = 0; i <= 0x55; i++) {347v = 0;348if(i & 64) v = (word32) 1 << 24;349if(i & 16) v |= (word32) 1 << 16;350if(i & 4) v |= (word32) 1 << 8;351if(i & 1) v |= 1;352wL_I8[i] = v;353}354355/* Final permutation */356357for(i = 0; i < 16; i++) {358v = 0;359if(i & 1) v = (word32) 1 << 24;360if(i & 2) v |= (word32) 1 << 16;361if(i & 4) v |= (word32) 1 << 8;362if(i & 8) v |= (word32) 1;363wO_L4[i] = v;364}365366/* Funny bit rearrangement on second index into S tables */367368for(i = 0; i < 64; i++) {369Smap[i] = (i & 0x20) | (i & 1) << 4 | (i & 0x1e) >> 1;370}371372/* Invert permutation P into mask indexed by R bit number */373374v = 1;375for(i = 32; --i >= 0; ) {376wP[ P[i] - 1 ] = v;377v += v;378}379380/* Build bit-mask versions of S tables, indexed in natural bit order */381382for(i = 0; i < 8; i++) {383for(j = 0; j < 64; j++) {384int k, t;385386t = S[i][ Smap[j] ];387for(k = 0; k < 4; k++) {388if(t & 8)389wPS[i][j] |= wP[4*i + k];390t += t;391}392}393}394}395396397void fsetkey(char key[8], keysched *ks)398{399register int i;400register word32 C, D;401static int built = 0;402403if(!built) {404buildtables();405built = 1;406}407408C = D = 0;409for(i = 0; i < 8; i++) {410register int v;411412v = key[i] >> 1; /* Discard "parity" bit */413C |= wC_K4[i][(v>>3) & 15] | wC_K3[i][v & 7];414D |= wD_K4[i][(v>>3) & 15] | wD_K3[i][v & 7];415}416417/*418* C and D now hold the suitably right-justified419* 28 permuted key bits each.420*/421for(i = 0; i < 16; i++) {422#ifdef CRAY423#define choice2(x, v) x[6][v&15] | x[5][(v>>4)&15] | x[4][(v>>8)&15] | \424x[3][(v>>12)&15] | x[2][(v>>16)&15] | x[1][(v>>20)&15] | \425x[0][(v>>24)&15]426#else427register word32 *ap;428429# define choice2(x, v) ( \430ap = &(x)[0][0], \431ap[16*6 + (v&15)] | \432ap[16*5 + ((v>>4)&15)] | ap[16*4 + ((v>>8)&15)] | \433ap[16*3 + ((v>>12)&15)] | ap[16*2 + ((v>>16)&15)] | \434ap[16*1 + ((v>>20)&15)] | ap[16*0 + ((v>>24)&15)] )435#endif436437438/* 28-bit left circular shift */439C <<= preshift[i];440C = ((C >> 28) & 3) | (C & (((word32)1<<28) - 1));441ks->KS[i].h = choice2(hKS_C4, C);442443D <<= preshift[i];444D = ((D >> 28) & 3) | (D & (((word32)1<<28) - 1));445ks->KS[i].l = choice2(lKS_D4, D);446}447}448449void450fencrypt(char block[8], int decrypt, keysched *ks)451{452int i;453register word32 L, R;454register struct keystage *ksp;455register word32 *ap;456457/* Initial permutation */458459L = R = 0;460i = 7;461ap = wL_I8;462do {463register int v;464465v = block[i]; /* Could optimize according to ENDIAN */466L = ap[v & 0x55] | (L << 1);467R = ap[(v >> 1) & 0x55] | (R << 1);468} while(--i >= 0);469470if(decrypt) {471ksp = &ks->KS[15];472} else {473ksp = &ks->KS[0];474}475476#ifdef CRAY477# define PS(i,j) wPS[i][j]478#else479# define PS(i,j) ap[64*(i) + (j)]480ap = &wPS[0][0];481#endif482483i = 16;484do {485register word32 k, tR;486487tR = (R >> 15) | (R << 17);488489k = ksp->h;490L ^= PS(0, ((tR >> 12) ^ (k >> 24)) & 63)491| PS(1, ((tR >> 8) ^ (k >> 16)) & 63)492| PS(2, ((tR >> 4) ^ (k >> 8)) & 63)493| PS(3, (tR ^ k) & 63);494495k = ksp->l;496L ^= PS(4, ((R >> 11) ^ (k >> 24)) & 63)497| PS(5, ((R >> 7) ^ (k >> 16)) & 63)498| PS(6, ((R >> 3) ^ (k >> 8)) & 63)499| PS(7, ((tR >> 16) ^ k) & 63);500501tR = L;502L = R;503R = tR;504505506if(decrypt)507ksp--;508else509ksp++;510} while(--i > 0);511{512register word32 t;513514#ifdef CRAY515# define FP(k) (wO_L4[ (L >> (k)) & 15 ] << 1 | wO_L4[ (R >> (k)) & 15 ])516#else517# define FP(k) (ap[ (L >> (k)) & 15 ] << 1 | ap[ (R >> (k)) & 15 ])518519ap = wO_L4;520#endif521522t = FP(0) | (FP(8) | (FP(16) | (FP(24) << 2)) << 2) << 2;523R = FP(4) | (FP(12) | (FP(20) | (FP(28) << 2)) << 2) << 2;524L = t;525}526{527register word32 t;528register char *bp;529530bp = &block[7];531t = R;532*bp = t & 255;533*--bp = (t >>= 8) & 255;534*--bp = (t >>= 8) & 255;535*--bp = (t >> 8) & 255;536t = L;537*--bp = t & 255;538*--bp = (t >>= 8) & 255;539*--bp = (t >>= 8) & 255;540*--bp = (t >> 8) & 255;541}542}543544545