#include "lib9.h" #include "libcrypt.h" /* * The Data Encryption Standard. * * A DES block is represented as two unsigned longs with DES bits * 1-32 corresponding with machine bits 31-0. Keys, as unsigned longs, * are represented similarly. Bits 8, 16, ... 64 of a key are parity * bits (ie. low order bit in the byte). An expanded key's bits 1-6 * correspond to machine bits 5-0. * * This is original work, based on the algorithm as specified in * _Network Security_ by Kaufman et al. * * Permission to use this software is granted provided this comment * block remains. No responsibility, financial or otherwise, can be * accepted for any consequences arising out of the use of this material. * * * Boyd Roberts * boyd@plan9.cs.su.oz.au */ /* User interface altered by jrf (for VitaNuova Ltd) for use in Inferno * deskey() to des_key_setup() + parameter change * xkey replaced by parameter * mangle() interface altered because of xkey change * des() to block_cipher() + parameter change */ /*#define DEBUG 1*/ #define DESBLOCKLEN 8 #define DESKEYPLEN 8 #define XKEYLEN 8 /* expanded key length */ #define ROUNDS 16 /* left and right halves of a DES block */ #define L(b) (((ulong *)b)[0]) #define R(b) (((ulong *)b)[1]) #ifdef DEBUG static int edflag; /* encrypt (0) / decrypt (1) */ static int round; #endif /* static uchar xkey[ROUNDS][XKEYLEN]; */ /* expanded per-round keys */ /* initial permutation - maps 4 input bits to a 32 bit value */ static ulong ip_bits[16] = { 0x00000000, 0x00000001, 0x00000100, 0x00000101, 0x00010000, 0x00010001, 0x00010100, 0x00010101, 0x01000000, 0x01000001, 0x01000100, 0x01000101, 0x01010000, 0x01010001, 0x01010100, 0x01010101, }; /* final permutation (ip-1) - maps 4 input bits to a 32 bit value */ static ulong ip_1_bits[16] = { 0x00000000, 0x80000000, 0x00800000, 0x80800000, 0x00008000, 0x80008000, 0x00808000, 0x80808000, 0x00000080, 0x80000080, 0x00800080, 0x80800080, 0x00008080, 0x80008080, 0x00808080, 0x80808080, }; /* initial permutation of the left half of the key */ static ulong c0_bits[16] = { 0x00000000, 0x00000001, 0x00000100, 0x00000101, 0x00010000, 0x00010001, 0x00010100, 0x00010101, 0x01000000, 0x01000001, 0x01000100, 0x01000101, 0x01010000, 0x01010001, 0x01010100, 0x01010101, }; /* initial permutation of the right half of the key */ static ulong d0_bits[16] = { 0x00000000, 0x01000000, 0x00010000, 0x01010000, 0x00000100, 0x01000100, 0x00010100, 0x01010100, 0x00000010, 0x01000010, 0x00010010, 0x01010010, 0x00000110, 0x01000110, 0x00010110, 0x01010110, }; /* permutation of the left half of a per-round key */ /* maps each input byte to a 32 bit value */ static ulong kil_bits[4][256] = { /* 0 */ 0x00000000, 0x00004000, 0x00001000, 0x00005000, 0x00400000, 0x00404000, 0x00401000, 0x00405000, 0x04000000, 0x04004000, 0x04001000, 0x04005000, 0x04400000, 0x04404000, 0x04401000, 0x04405000, 0x00010000, 0x00014000, 0x00011000, 0x00015000, 0x00410000, 0x00414000, 0x00411000, 0x00415000, 0x04010000, 0x04014000, 0x04011000, 0x04015000, 0x04410000, 0x04414000, 0x04411000, 0x04415000, 0x02000000, 0x02004000, 0x02001000, 0x02005000, 0x02400000, 0x02404000, 0x02401000, 0x02405000, 0x06000000, 0x06004000, 0x06001000, 0x06005000, 0x06400000, 0x06404000, 0x06401000, 0x06405000, 0x02010000, 0x02014000, 0x02011000, 0x02015000, 0x02410000, 0x02414000, 0x02411000, 0x02415000, 0x06010000, 0x06014000, 0x06011000, 0x06015000, 0x06410000, 0x06414000, 0x06411000, 0x06415000, 0x00000100, 0x00004100, 0x00001100, 0x00005100, 0x00400100, 0x00404100, 0x00401100, 0x00405100, 0x04000100, 0x04004100, 0x04001100, 0x04005100, 0x04400100, 0x04404100, 0x04401100, 0x04405100, 0x00010100, 0x00014100, 0x00011100, 0x00015100, 0x00410100, 0x00414100, 0x00411100, 0x00415100, 0x04010100, 0x04014100, 0x04011100, 0x04015100, 0x04410100, 0x04414100, 0x04411100, 0x04415100, 0x02000100, 0x02004100, 0x02001100, 0x02005100, 0x02400100, 0x02404100, 0x02401100, 0x02405100, 0x06000100, 0x06004100, 0x06001100, 0x06005100, 0x06400100, 0x06404100, 0x06401100, 0x06405100, 0x02010100, 0x02014100, 0x02011100, 0x02015100, 0x02410100, 0x02414100, 0x02411100, 0x02415100, 0x06010100, 0x06014100, 0x06011100, 0x06015100, 0x06410100, 0x06414100, 0x06411100, 0x06415100, 0x08000000, 0x08004000, 0x08001000, 0x08005000, 0x08400000, 0x08404000, 0x08401000, 0x08405000, 0x0c000000, 0x0c004000, 0x0c001000, 0x0c005000, 0x0c400000, 0x0c404000, 0x0c401000, 0x0c405000, 0x08010000, 0x08014000, 0x08011000, 0x08015000, 0x08410000, 0x08414000, 0x08411000, 0x08415000, 0x0c010000, 0x0c014000, 0x0c011000, 0x0c015000, 0x0c410000, 0x0c414000, 0x0c411000, 0x0c415000, 0x0a000000, 0x0a004000, 0x0a001000, 0x0a005000, 0x0a400000, 0x0a404000, 0x0a401000, 0x0a405000, 0x0e000000, 0x0e004000, 0x0e001000, 0x0e005000, 0x0e400000, 0x0e404000, 0x0e401000, 0x0e405000, 0x0a010000, 0x0a014000, 0x0a011000, 0x0a015000, 0x0a410000, 0x0a414000, 0x0a411000, 0x0a415000, 0x0e010000, 0x0e014000, 0x0e011000, 0x0e015000, 0x0e410000, 0x0e414000, 0x0e411000, 0x0e415000, 0x08000100, 0x08004100, 0x08001100, 0x08005100, 0x08400100, 0x08404100, 0x08401100, 0x08405100, 0x0c000100, 0x0c004100, 0x0c001100, 0x0c005100, 0x0c400100, 0x0c404100, 0x0c401100, 0x0c405100, 0x08010100, 0x08014100, 0x08011100, 0x08015100, 0x08410100, 0x08414100, 0x08411100, 0x08415100, 0x0c010100, 0x0c014100, 0x0c011100, 0x0c015100, 0x0c410100, 0x0c414100, 0x0c411100, 0x0c415100, 0x0a000100, 0x0a004100, 0x0a001100, 0x0a005100, 0x0a400100, 0x0a404100, 0x0a401100, 0x0a405100, 0x0e000100, 0x0e004100, 0x0e001100, 0x0e005100, 0x0e400100, 0x0e404100, 0x0e401100, 0x0e405100, 0x0a010100, 0x0a014100, 0x0a011100, 0x0a015100, 0x0a410100, 0x0a414100, 0x0a411100, 0x0a415100, 0x0e010100, 0x0e014100, 0x0e011100, 0x0e015100, 0x0e410100, 0x0e414100, 0x0e411100, 0x0e415100, /* 1 */ 0x00000000, 0x00002000, 0x00800000, 0x00802000, 0x80000000, 0x80002000, 0x80800000, 0x80802000, 0x00000200, 0x00002200, 0x00800200, 0x00802200, 0x80000200, 0x80002200, 0x80800200, 0x80802200, 0x00020000, 0x00022000, 0x00820000, 0x00822000, 0x80020000, 0x80022000, 0x80820000, 0x80822000, 0x00020200, 0x00022200, 0x00820200, 0x00822200, 0x80020200, 0x80022200, 0x80820200, 0x80822200, 0x20000000, 0x20002000, 0x20800000, 0x20802000, 0xa0000000, 0xa0002000, 0xa0800000, 0xa0802000, 0x20000200, 0x20002200, 0x20800200, 0x20802200, 0xa0000200, 0xa0002200, 0xa0800200, 0xa0802200, 0x20020000, 0x20022000, 0x20820000, 0x20822000, 0xa0020000, 0xa0022000, 0xa0820000, 0xa0822000, 0x20020200, 0x20022200, 0x20820200, 0x20822200, 0xa0020200, 0xa0022200, 0xa0820200, 0xa0822200, 0x00100000, 0x00102000, 0x00900000, 0x00902000, 0x80100000, 0x80102000, 0x80900000, 0x80902000, 0x00100200, 0x00102200, 0x00900200, 0x00902200, 0x80100200, 0x80102200, 0x80900200, 0x80902200, 0x00120000, 0x00122000, 0x00920000, 0x00922000, 0x80120000, 0x80122000, 0x80920000, 0x80922000, 0x00120200, 0x00122200, 0x00920200, 0x00922200, 0x80120200, 0x80122200, 0x80920200, 0x80922200, 0x20100000, 0x20102000, 0x20900000, 0x20902000, 0xa0100000, 0xa0102000, 0xa0900000, 0xa0902000, 0x20100200, 0x20102200, 0x20900200, 0x20902200, 0xa0100200, 0xa0102200, 0xa0900200, 0xa0902200, 0x20120000, 0x20122000, 0x20920000, 0x20922000, 0xa0120000, 0xa0122000, 0xa0920000, 0xa0922000, 0x20120200, 0x20122200, 0x20920200, 0x20922200, 0xa0120200, 0xa0122200, 0xa0920200, 0xa0922200, 0x00000000, 0x00002000, 0x00800000, 0x00802000, 0x80000000, 0x80002000, 0x80800000, 0x80802000, 0x00000200, 0x00002200, 0x00800200, 0x00802200, 0x80000200, 0x80002200, 0x80800200, 0x80802200, 0x00020000, 0x00022000, 0x00820000, 0x00822000, 0x80020000, 0x80022000, 0x80820000, 0x80822000, 0x00020200, 0x00022200, 0x00820200, 0x00822200, 0x80020200, 0x80022200, 0x80820200, 0x80822200, 0x20000000, 0x20002000, 0x20800000, 0x20802000, 0xa0000000, 0xa0002000, 0xa0800000, 0xa0802000, 0x20000200, 0x20002200, 0x20800200, 0x20802200, 0xa0000200, 0xa0002200, 0xa0800200, 0xa0802200, 0x20020000, 0x20022000, 0x20820000, 0x20822000, 0xa0020000, 0xa0022000, 0xa0820000, 0xa0822000, 0x20020200, 0x20022200, 0x20820200, 0x20822200, 0xa0020200, 0xa0022200, 0xa0820200, 0xa0822200, 0x00100000, 0x00102000, 0x00900000, 0x00902000, 0x80100000, 0x80102000, 0x80900000, 0x80902000, 0x00100200, 0x00102200, 0x00900200, 0x00902200, 0x80100200, 0x80102200, 0x80900200, 0x80902200, 0x00120000, 0x00122000, 0x00920000, 0x00922000, 0x80120000, 0x80122000, 0x80920000, 0x80922000, 0x00120200, 0x00122200, 0x00920200, 0x00922200, 0x80120200, 0x80122200, 0x80920200, 0x80922200, 0x20100000, 0x20102000, 0x20900000, 0x20902000, 0xa0100000, 0xa0102000, 0xa0900000, 0xa0902000, 0x20100200, 0x20102200, 0x20900200, 0x20902200, 0xa0100200, 0xa0102200, 0xa0900200, 0xa0902200, 0x20120000, 0x20122000, 0x20920000, 0x20922000, 0xa0120000, 0xa0122000, 0xa0920000, 0xa0922000, 0x20120200, 0x20122200, 0x20920200, 0x20922200, 0xa0120200, 0xa0122200, 0xa0920200, 0xa0922200, /* 2 */ 0x00000000, 0x10000000, 0x00080000, 0x10080000, 0x00000000, 0x10000000, 0x00080000, 0x10080000, 0x00200000, 0x10200000, 0x00280000, 0x10280000, 0x00200000, 0x10200000, 0x00280000, 0x10280000, 0x00000400, 0x10000400, 0x00080400, 0x10080400, 0x00000400, 0x10000400, 0x00080400, 0x10080400, 0x00200400, 0x10200400, 0x00280400, 0x10280400, 0x00200400, 0x10200400, 0x00280400, 0x10280400, 0x00040000, 0x10040000, 0x000c0000, 0x100c0000, 0x00040000, 0x10040000, 0x000c0000, 0x100c0000, 0x00240000, 0x10240000, 0x002c0000, 0x102c0000, 0x00240000, 0x10240000, 0x002c0000, 0x102c0000, 0x00040400, 0x10040400, 0x000c0400, 0x100c0400, 0x00040400, 0x10040400, 0x000c0400, 0x100c0400, 0x00240400, 0x10240400, 0x002c0400, 0x102c0400, 0x00240400, 0x10240400, 0x002c0400, 0x102c0400, 0x00000000, 0x10000000, 0x00080000, 0x10080000, 0x00000000, 0x10000000, 0x00080000, 0x10080000, 0x00200000, 0x10200000, 0x00280000, 0x10280000, 0x00200000, 0x10200000, 0x00280000, 0x10280000, 0x00000400, 0x10000400, 0x00080400, 0x10080400, 0x00000400, 0x10000400, 0x00080400, 0x10080400, 0x00200400, 0x10200400, 0x00280400, 0x10280400, 0x00200400, 0x10200400, 0x00280400, 0x10280400, 0x00040000, 0x10040000, 0x000c0000, 0x100c0000, 0x00040000, 0x10040000, 0x000c0000, 0x100c0000, 0x00240000, 0x10240000, 0x002c0000, 0x102c0000, 0x00240000, 0x10240000, 0x002c0000, 0x102c0000, 0x00040400, 0x10040400, 0x000c0400, 0x100c0400, 0x00040400, 0x10040400, 0x000c0400, 0x100c0400, 0x00240400, 0x10240400, 0x002c0400, 0x102c0400, 0x00240400, 0x10240400, 0x002c0400, 0x102c0400, 0x40000000, 0x50000000, 0x40080000, 0x50080000, 0x40000000, 0x50000000, 0x40080000, 0x50080000, 0x40200000, 0x50200000, 0x40280000, 0x50280000, 0x40200000, 0x50200000, 0x40280000, 0x50280000, 0x40000400, 0x50000400, 0x40080400, 0x50080400, 0x40000400, 0x50000400, 0x40080400, 0x50080400, 0x40200400, 0x50200400, 0x40280400, 0x50280400, 0x40200400, 0x50200400, 0x40280400, 0x50280400, 0x40040000, 0x50040000, 0x400c0000, 0x500c0000, 0x40040000, 0x50040000, 0x400c0000, 0x500c0000, 0x40240000, 0x50240000, 0x402c0000, 0x502c0000, 0x40240000, 0x50240000, 0x402c0000, 0x502c0000, 0x40040400, 0x50040400, 0x400c0400, 0x500c0400, 0x40040400, 0x50040400, 0x400c0400, 0x500c0400, 0x40240400, 0x50240400, 0x402c0400, 0x502c0400, 0x40240400, 0x50240400, 0x402c0400, 0x502c0400, 0x40000000, 0x50000000, 0x40080000, 0x50080000, 0x40000000, 0x50000000, 0x40080000, 0x50080000, 0x40200000, 0x50200000, 0x40280000, 0x50280000, 0x40200000, 0x50200000, 0x40280000, 0x50280000, 0x40000400, 0x50000400, 0x40080400, 0x50080400, 0x40000400, 0x50000400, 0x40080400, 0x50080400, 0x40200400, 0x50200400, 0x40280400, 0x50280400, 0x40200400, 0x50200400, 0x40280400, 0x50280400, 0x40040000, 0x50040000, 0x400c0000, 0x500c0000, 0x40040000, 0x50040000, 0x400c0000, 0x500c0000, 0x40240000, 0x50240000, 0x402c0000, 0x502c0000, 0x40240000, 0x50240000, 0x402c0000, 0x502c0000, 0x40040400, 0x50040400, 0x400c0400, 0x500c0400, 0x40040400, 0x50040400, 0x400c0400, 0x500c0400, 0x40240400, 0x50240400, 0x402c0400, 0x502c0400, 0x40240400, 0x50240400, 0x402c0400, 0x502c0400, /* 3 */ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x01000000, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x00000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x01000800, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x00008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x01008000, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x00008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, 0x01008800, }; /* permutation of the right half of a per-round key */ /* maps each input byte to a 32 bit value */ static ulong kir_bits[4][256] = { /* 0 */ 0x00000000, 0x00000400, 0x00000000, 0x00000400, 0x00008000, 0x00008400, 0x00008000, 0x00008400, 0x00200000, 0x00200400, 0x00200000, 0x00200400, 0x00208000, 0x00208400, 0x00208000, 0x00208400, 0x00000100, 0x00000500, 0x00000100, 0x00000500, 0x00008100, 0x00008500, 0x00008100, 0x00008500, 0x00200100, 0x00200500, 0x00200100, 0x00200500, 0x00208100, 0x00208500, 0x00208100, 0x00208500, 0x20000000, 0x20000400, 0x20000000, 0x20000400, 0x20008000, 0x20008400, 0x20008000, 0x20008400, 0x20200000, 0x20200400, 0x20200000, 0x20200400, 0x20208000, 0x20208400, 0x20208000, 0x20208400, 0x20000100, 0x20000500, 0x20000100, 0x20000500, 0x20008100, 0x20008500, 0x20008100, 0x20008500, 0x20200100, 0x20200500, 0x20200100, 0x20200500, 0x20208100, 0x20208500, 0x20208100, 0x20208500, 0x02000000, 0x02000400, 0x02000000, 0x02000400, 0x02008000, 0x02008400, 0x02008000, 0x02008400, 0x02200000, 0x02200400, 0x02200000, 0x02200400, 0x02208000, 0x02208400, 0x02208000, 0x02208400, 0x02000100, 0x02000500, 0x02000100, 0x02000500, 0x02008100, 0x02008500, 0x02008100, 0x02008500, 0x02200100, 0x02200500, 0x02200100, 0x02200500, 0x02208100, 0x02208500, 0x02208100, 0x02208500, 0x22000000, 0x22000400, 0x22000000, 0x22000400, 0x22008000, 0x22008400, 0x22008000, 0x22008400, 0x22200000, 0x22200400, 0x22200000, 0x22200400, 0x22208000, 0x22208400, 0x22208000, 0x22208400, 0x22000100, 0x22000500, 0x22000100, 0x22000500, 0x22008100, 0x22008500, 0x22008100, 0x22008500, 0x22200100, 0x22200500, 0x22200100, 0x22200500, 0x22208100, 0x22208500, 0x22208100, 0x22208500, 0x00000200, 0x00000600, 0x00000200, 0x00000600, 0x00008200, 0x00008600, 0x00008200, 0x00008600, 0x00200200, 0x00200600, 0x00200200, 0x00200600, 0x00208200, 0x00208600, 0x00208200, 0x00208600, 0x00000300, 0x00000700, 0x00000300, 0x00000700, 0x00008300, 0x00008700, 0x00008300, 0x00008700, 0x00200300, 0x00200700, 0x00200300, 0x00200700, 0x00208300, 0x00208700, 0x00208300, 0x00208700, 0x20000200, 0x20000600, 0x20000200, 0x20000600, 0x20008200, 0x20008600, 0x20008200, 0x20008600, 0x20200200, 0x20200600, 0x20200200, 0x20200600, 0x20208200, 0x20208600, 0x20208200, 0x20208600, 0x20000300, 0x20000700, 0x20000300, 0x20000700, 0x20008300, 0x20008700, 0x20008300, 0x20008700, 0x20200300, 0x20200700, 0x20200300, 0x20200700, 0x20208300, 0x20208700, 0x20208300, 0x20208700, 0x02000200, 0x02000600, 0x02000200, 0x02000600, 0x02008200, 0x02008600, 0x02008200, 0x02008600, 0x02200200, 0x02200600, 0x02200200, 0x02200600, 0x02208200, 0x02208600, 0x02208200, 0x02208600, 0x02000300, 0x02000700, 0x02000300, 0x02000700, 0x02008300, 0x02008700, 0x02008300, 0x02008700, 0x02200300, 0x02200700, 0x02200300, 0x02200700, 0x02208300, 0x02208700, 0x02208300, 0x02208700, 0x22000200, 0x22000600, 0x22000200, 0x22000600, 0x22008200, 0x22008600, 0x22008200, 0x22008600, 0x22200200, 0x22200600, 0x22200200, 0x22200600, 0x22208200, 0x22208600, 0x22208200, 0x22208600, 0x22000300, 0x22000700, 0x22000300, 0x22000700, 0x22008300, 0x22008700, 0x22008300, 0x22008700, 0x22200300, 0x22200700, 0x22200300, 0x22200700, 0x22208300, 0x22208700, 0x22208300, 0x22208700, /* 1 */ 0x00000000, 0x00080000, 0x00000000, 0x00080000, 0x00001000, 0x00081000, 0x00001000, 0x00081000, 0x80000000, 0x80080000, 0x80000000, 0x80080000, 0x80001000, 0x80081000, 0x80001000, 0x80081000, 0x01000000, 0x01080000, 0x01000000, 0x01080000, 0x01001000, 0x01081000, 0x01001000, 0x01081000, 0x81000000, 0x81080000, 0x81000000, 0x81080000, 0x81001000, 0x81081000, 0x81001000, 0x81081000, 0x00020000, 0x000a0000, 0x00020000, 0x000a0000, 0x00021000, 0x000a1000, 0x00021000, 0x000a1000, 0x80020000, 0x800a0000, 0x80020000, 0x800a0000, 0x80021000, 0x800a1000, 0x80021000, 0x800a1000, 0x01020000, 0x010a0000, 0x01020000, 0x010a0000, 0x01021000, 0x010a1000, 0x01021000, 0x010a1000, 0x81020000, 0x810a0000, 0x81020000, 0x810a0000, 0x81021000, 0x810a1000, 0x81021000, 0x810a1000, 0x00000000, 0x00080000, 0x00000000, 0x00080000, 0x00001000, 0x00081000, 0x00001000, 0x00081000, 0x80000000, 0x80080000, 0x80000000, 0x80080000, 0x80001000, 0x80081000, 0x80001000, 0x80081000, 0x01000000, 0x01080000, 0x01000000, 0x01080000, 0x01001000, 0x01081000, 0x01001000, 0x01081000, 0x81000000, 0x81080000, 0x81000000, 0x81080000, 0x81001000, 0x81081000, 0x81001000, 0x81081000, 0x00020000, 0x000a0000, 0x00020000, 0x000a0000, 0x00021000, 0x000a1000, 0x00021000, 0x000a1000, 0x80020000, 0x800a0000, 0x80020000, 0x800a0000, 0x80021000, 0x800a1000, 0x80021000, 0x800a1000, 0x01020000, 0x010a0000, 0x01020000, 0x010a0000, 0x01021000, 0x010a1000, 0x01021000, 0x010a1000, 0x81020000, 0x810a0000, 0x81020000, 0x810a0000, 0x81021000, 0x810a1000, 0x81021000, 0x810a1000, 0x10000000, 0x10080000, 0x10000000, 0x10080000, 0x10001000, 0x10081000, 0x10001000, 0x10081000, 0x90000000, 0x90080000, 0x90000000, 0x90080000, 0x90001000, 0x90081000, 0x90001000, 0x90081000, 0x11000000, 0x11080000, 0x11000000, 0x11080000, 0x11001000, 0x11081000, 0x11001000, 0x11081000, 0x91000000, 0x91080000, 0x91000000, 0x91080000, 0x91001000, 0x91081000, 0x91001000, 0x91081000, 0x10020000, 0x100a0000, 0x10020000, 0x100a0000, 0x10021000, 0x100a1000, 0x10021000, 0x100a1000, 0x90020000, 0x900a0000, 0x90020000, 0x900a0000, 0x90021000, 0x900a1000, 0x90021000, 0x900a1000, 0x11020000, 0x110a0000, 0x11020000, 0x110a0000, 0x11021000, 0x110a1000, 0x11021000, 0x110a1000, 0x91020000, 0x910a0000, 0x91020000, 0x910a0000, 0x91021000, 0x910a1000, 0x91021000, 0x910a1000, 0x10000000, 0x10080000, 0x10000000, 0x10080000, 0x10001000, 0x10081000, 0x10001000, 0x10081000, 0x90000000, 0x90080000, 0x90000000, 0x90080000, 0x90001000, 0x90081000, 0x90001000, 0x90081000, 0x11000000, 0x11080000, 0x11000000, 0x11080000, 0x11001000, 0x11081000, 0x11001000, 0x11081000, 0x91000000, 0x91080000, 0x91000000, 0x91080000, 0x91001000, 0x91081000, 0x91001000, 0x91081000, 0x10020000, 0x100a0000, 0x10020000, 0x100a0000, 0x10021000, 0x100a1000, 0x10021000, 0x100a1000, 0x90020000, 0x900a0000, 0x90020000, 0x900a0000, 0x90021000, 0x900a1000, 0x90021000, 0x900a1000, 0x11020000, 0x110a0000, 0x11020000, 0x110a0000, 0x11021000, 0x110a1000, 0x11021000, 0x110a1000, 0x91020000, 0x910a0000, 0x91020000, 0x910a0000, 0x91021000, 0x910a1000, 0x91021000, 0x910a1000, /* 2 */ 0x00000000, 0x40000000, 0x00800000, 0x40800000, 0x00000800, 0x40000800, 0x00800800, 0x40800800, 0x00040000, 0x40040000, 0x00840000, 0x40840000, 0x00040800, 0x40040800, 0x00840800, 0x40840800, 0x00100000, 0x40100000, 0x00900000, 0x40900000, 0x00100800, 0x40100800, 0x00900800, 0x40900800, 0x00140000, 0x40140000, 0x00940000, 0x40940000, 0x00140800, 0x40140800, 0x00940800, 0x40940800, 0x08000000, 0x48000000, 0x08800000, 0x48800000, 0x08000800, 0x48000800, 0x08800800, 0x48800800, 0x08040000, 0x48040000, 0x08840000, 0x48840000, 0x08040800, 0x48040800, 0x08840800, 0x48840800, 0x08100000, 0x48100000, 0x08900000, 0x48900000, 0x08100800, 0x48100800, 0x08900800, 0x48900800, 0x08140000, 0x48140000, 0x08940000, 0x48940000, 0x08140800, 0x48140800, 0x08940800, 0x48940800, 0x00002000, 0x40002000, 0x00802000, 0x40802000, 0x00002800, 0x40002800, 0x00802800, 0x40802800, 0x00042000, 0x40042000, 0x00842000, 0x40842000, 0x00042800, 0x40042800, 0x00842800, 0x40842800, 0x00102000, 0x40102000, 0x00902000, 0x40902000, 0x00102800, 0x40102800, 0x00902800, 0x40902800, 0x00142000, 0x40142000, 0x00942000, 0x40942000, 0x00142800, 0x40142800, 0x00942800, 0x40942800, 0x08002000, 0x48002000, 0x08802000, 0x48802000, 0x08002800, 0x48002800, 0x08802800, 0x48802800, 0x08042000, 0x48042000, 0x08842000, 0x48842000, 0x08042800, 0x48042800, 0x08842800, 0x48842800, 0x08102000, 0x48102000, 0x08902000, 0x48902000, 0x08102800, 0x48102800, 0x08902800, 0x48902800, 0x08142000, 0x48142000, 0x08942000, 0x48942000, 0x08142800, 0x48142800, 0x08942800, 0x48942800, 0x00400000, 0x40400000, 0x00c00000, 0x40c00000, 0x00400800, 0x40400800, 0x00c00800, 0x40c00800, 0x00440000, 0x40440000, 0x00c40000, 0x40c40000, 0x00440800, 0x40440800, 0x00c40800, 0x40c40800, 0x00500000, 0x40500000, 0x00d00000, 0x40d00000, 0x00500800, 0x40500800, 0x00d00800, 0x40d00800, 0x00540000, 0x40540000, 0x00d40000, 0x40d40000, 0x00540800, 0x40540800, 0x00d40800, 0x40d40800, 0x08400000, 0x48400000, 0x08c00000, 0x48c00000, 0x08400800, 0x48400800, 0x08c00800, 0x48c00800, 0x08440000, 0x48440000, 0x08c40000, 0x48c40000, 0x08440800, 0x48440800, 0x08c40800, 0x48c40800, 0x08500000, 0x48500000, 0x08d00000, 0x48d00000, 0x08500800, 0x48500800, 0x08d00800, 0x48d00800, 0x08540000, 0x48540000, 0x08d40000, 0x48d40000, 0x08540800, 0x48540800, 0x08d40800, 0x48d40800, 0x00402000, 0x40402000, 0x00c02000, 0x40c02000, 0x00402800, 0x40402800, 0x00c02800, 0x40c02800, 0x00442000, 0x40442000, 0x00c42000, 0x40c42000, 0x00442800, 0x40442800, 0x00c42800, 0x40c42800, 0x00502000, 0x40502000, 0x00d02000, 0x40d02000, 0x00502800, 0x40502800, 0x00d02800, 0x40d02800, 0x00542000, 0x40542000, 0x00d42000, 0x40d42000, 0x00542800, 0x40542800, 0x00d42800, 0x40d42800, 0x08402000, 0x48402000, 0x08c02000, 0x48c02000, 0x08402800, 0x48402800, 0x08c02800, 0x48c02800, 0x08442000, 0x48442000, 0x08c42000, 0x48c42000, 0x08442800, 0x48442800, 0x08c42800, 0x48c42800, 0x08502000, 0x48502000, 0x08d02000, 0x48d02000, 0x08502800, 0x48502800, 0x08d02800, 0x48d02800, 0x08542000, 0x48542000, 0x08d42000, 0x48d42000, 0x08542800, 0x48542800, 0x08d42800, 0x48d42800, /* 3 */ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x00010000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04000000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x04010000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00004000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x00014000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04004000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, 0x04014000, }; /* S boxes */ static uchar s[XKEYLEN][64] = { /* 1 */ { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, }, /* 2 */ { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, }, /* 3 */ { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, }, /* 4 */ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, }, /* 5 */ { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, }, /* 6 */ { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, }, /* 7 */ { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, }, /* 8 */ { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11, }, }; /* permutation following the S box substitution */ /* maps each 4 bits of input to a 32 bit value */ static ulong sp_bits[XKEYLEN][16] = { 0x00000000, 0x00000002, 0x00000200, 0x00000202, 0x00008000, 0x00008002, 0x00008200, 0x00008202, 0x00800000, 0x00800002, 0x00800200, 0x00800202, 0x00808000, 0x00808002, 0x00808200, 0x00808202, 0x00000000, 0x00004000, 0x40000000, 0x40004000, 0x00000010, 0x00004010, 0x40000010, 0x40004010, 0x00080000, 0x00084000, 0x40080000, 0x40084000, 0x00080010, 0x00084010, 0x40080010, 0x40084010, 0x00000000, 0x04000000, 0x00000004, 0x04000004, 0x00010000, 0x04010000, 0x00010004, 0x04010004, 0x00000100, 0x04000100, 0x00000104, 0x04000104, 0x00010100, 0x04010100, 0x00010104, 0x04010104, 0x00000000, 0x80000000, 0x00400000, 0x80400000, 0x00001000, 0x80001000, 0x00401000, 0x80401000, 0x00000040, 0x80000040, 0x00400040, 0x80400040, 0x00001040, 0x80001040, 0x00401040, 0x80401040, 0x00000000, 0x20000000, 0x00000080, 0x20000080, 0x00040000, 0x20040000, 0x00040080, 0x20040080, 0x01000000, 0x21000000, 0x01000080, 0x21000080, 0x01040000, 0x21040000, 0x01040080, 0x21040080, 0x00000000, 0x00002000, 0x00200000, 0x00202000, 0x00000008, 0x00002008, 0x00200008, 0x00202008, 0x10000000, 0x10002000, 0x10200000, 0x10202000, 0x10000008, 0x10002008, 0x10200008, 0x10202008, 0x00000000, 0x02000000, 0x00000400, 0x02000400, 0x00100000, 0x02100000, 0x00100400, 0x02100400, 0x00000001, 0x02000001, 0x00000401, 0x02000401, 0x00100001, 0x02100001, 0x00100401, 0x02100401, 0x00000000, 0x00000800, 0x00020000, 0x00020800, 0x00000020, 0x00000820, 0x00020020, 0x00020820, 0x08000000, 0x08000800, 0x08020000, 0x08020800, 0x08000020, 0x08000820, 0x08020020, 0x08020820, }; #ifdef DEBUG static void log(uchar *b) { printf("%c %2d %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x\n", edflag ? 'D' : 'E', round, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); } #endif /* * Convert machine representation of 32 bits into 4 bytes. */ static void unpack(ulong l, uchar *b) { b[0] = l >> 24 & 0xFF; b[1] = l >> 16 & 0xFF; b[2] = l >> 8 & 0xFF; b[3] = l & 0xFF; } /* * Initial Permutation. */ static void ip(uchar *b) { int i; uchar c; ulong l; ulong r; l = 0; r = 0; for (i = 0; i < DESBLOCKLEN; i++) { c = b[i]; l |= ip_bits[(c & 0x40) >> 3 |(c & 0x10) >> 2 |(c & 0x04) >> 1 |(c & 0x01)] << i; r |= ip_bits[(c & 0x80) >> 4 |(c & 0x20) >> 3 |(c & 0x08) >> 2 |(c & 0x02) >> 1] << i; } ((ulong *)b)[0] = l; ((ulong *)b)[1] = r; } /* * Final Permutation. */ static void ip_1(ulong l, ulong r, uchar *b) { int i; uchar c; ulong pl; ulong pr; static int ls[] = { 1, 3, 5, 7, }; static int rs[] = { 0, 2, 4, 6, }; pl = 0; pr = 0; for (i = 0; i < sizeof(ulong); i++) { c = l >> 24 - i * 8 & 0xFF; pl |= ip_1_bits[c & 0xF] >> ls[i]; pr |= ip_1_bits[c >> 4 & 0xF] >> ls[i]; } for (i = 0; i < sizeof(ulong); i++) { c = r >> 24 - i * 8 & 0xFF; pl |= ip_1_bits[c & 0xF] >> rs[i]; pr |= ip_1_bits[c >> 4 & 0xF] >> rs[i]; } unpack(pl, &b[0]); unpack(pr, &b[4]); } /* * Rotate 28 bits left 1 bit. */ static ulong rol(ulong l) { int c; c = (l & 0x80000000) >> 27; l <<= 1; l |= c; return l; } /* * Rotate 28 bits left 2 bits. */ static ulong rol2(ulong l) { int c; c = (l & 0xC0000000) >> 26; l <<= 2; l |= c; return l; } /* permutation for each per-round key */ #define kp(l, t) (t[0][l >> 24 & 0xFF] | t[1][l >> 16 & 0xFF] | t[2][l >> 8 & 0xFF] | t[3][l & 0xFF]) /* * Expand a 24 bit key into 4 bytes, each containing 6 bits. */ static void kexpand(ulong k, uchar *x) { int i; ulong b; b = 0xFC000000; for (i = 0; i < XKEYLEN / 2; i++) { *x++ = (k & b) >> 26 - i * 6; b >>= 6; } } /* * Generate the per-round keys from the 56 bit key. */ void des_key_setup(uchar key[8], ulong *expkey) { uchar *k, *ek; int i; ulong c0; ulong d0; static int c0m[] = { 0xE, 0xE, 0xE, 0xE, 0xF, 0xF, 0xF, 0xF }; static int d0m[] = { 0xF, 0xF, 0xF, 0xF, 0x7, 0x7, 0x7, 0x7 }; static ulong (*rf[])(ulong) = { rol, rol, rol2, rol2, rol2, rol2, rol2, rol2, rol, rol2, rol2, rol2, rol2, rol2, rol2, rol }; k = (uchar *)key; ek = (uchar *)expkey; c0 = 0; d0 = 0; for (i = 0; i < DESKEYPLEN; i++) { uchar c; c = *k++; c0 |= c0_bits[c >> 4 & c0m[i]] << i; d0 |= d0_bits[c >> 1 & d0m[i]] << i; } for (i = 0; i < ROUNDS; i++) { c0 = (*rf[i])(c0); /* kexpand(kp(c0, kil_bits), &xkey[i][0]); */ kexpand(kp(c0, kil_bits), ek); ek += 4; d0 = (*rf[i])(d0); /* kexpand(kp(d0, kir_bits), &xkey[i][4]); */ kexpand(kp(d0, kir_bits), ek); ek += 4; } } /* * Expand a 32 bit message block to 8 bytes, each containing 6 bits. */ static void mexpand(ulong m, uchar *x) { int i; ulong b; *x++ = (m & 0x1) << 5 | (m & 0xF8000000) >> 27; b = 0x1F800000; for (i = 0; i < XKEYLEN - 2; i++) { *x++ = (m & b) >> 23 - i * 4; b >>= 4; } *x = (m & 0x1F) << 1 | m >> 31; } /* * Generate 32 bits from a 32 bit message block, per-round key, * S boxes and permutation. */ static ulong mangle(ulong m, uchar *ek) { int i; uchar xm[XKEYLEN]; mexpand(m, xm); m = 0; for (i = 0; i < XKEYLEN; i++) { uchar x; /* x = xm[i] ^ xkey[n][i]; */ x = xm[i] ^ ek[i]; /* could re-order xm and xk to avoid these shifts */ x = (x & 0x20) | (x & 0x1E) >> 1 | (x & 0x1) << 4; /* could expand s to do the permutation */ m |= sp_bits[i][s[i][x]]; } return m; } /* * Encrypt/decrypt a DES block. */ void block_cipher(ulong expkey[32], uchar v[8], int ed) { uchar *b, *ek; int i, eks; ulong l; ulong r; #ifdef DEBUG edflag = ed; round = 0; #endif b = (uchar *)v; ek = (uchar *)expkey; if (ed) { ek += 128-8; eks = -8; } else eks = 8; #ifdef DEBUG log(b); #endif ip(b); #ifdef DEBUG log(b); #endif l = L(b); r = R(b); for (i = 0; i < ROUNDS; i++) { ulong t; #ifdef DEBUG round = ed ? ROUNDS - i : i + 1; #endif t = r; /* r = mangle(r, ed ? ROUNDS - i - 1 : i) ^ l; */ r = mangle(r, ek) ^ l; ek += eks; l = t; #ifdef DEBUG L(b) = l; R(b) = r; log(b); #endif } /* undo round 16's swap before permuting */ ip_1(r, l, b); #ifdef DEBUG round++; log(b); #endif } /* if encrypt with ende bits = b2, b1, b0, decrypt with ende bits ~b0, ~b1, ~b2 */ void triple_block_cipher(ulong expkey[3][32], uchar text[8], int ende) { int i, d; ulong *ek; for (i = 0; i < 3; i++) { d = (ende & 1) == DES3D; ek = d ? expkey[2-i] : expkey[i]; ende >>= 1; block_cipher(ek, text, d); } } void setupDESstate(DESstate *s, uchar key[8], uchar *ivec) { memset(s, 0, sizeof(*s)); memmove(s->key, key, sizeof(s->key)); des_key_setup(key, s->expanded); if(ivec) memmove(s->ivec, ivec, 8); s->setup = 0xdeadbeef; } void setupDES3state(DES3state *s, uchar key[3][8], uchar *ivec) { memset(s, 0, sizeof(*s)); memmove(s->key, key, sizeof(s->key)); des_key_setup(key[0], s->expanded[0]); des_key_setup(key[1], s->expanded[1]); des_key_setup(key[2], s->expanded[2]); if(ivec) memmove(s->ivec, ivec, 8); s->setup = 0xdeadbeef; }