There are two parts to this; a UNIX DES implementation and some Plan 9
support for UNIX which uses the DES implementaton. The Plan 9 support
gives you encrypt(2) and netkey(2).
The only significant difference between my DES and the Plan 9 DES
is that Plan 9 packs a DES key into 7 bytes, while I use 8. In the
Plan 9 support there's a function (des9key) which does the key
conversion (Plan 9 -> 8 bytes). The versions of encrypt and decrypt
should call des9key() before invoking deskey(), so it's transparent.
The DES implementation has a Makefile, manual entry and test program.
The Plan 9 support comes with no Makefile support, that's up to you.
# To unbundle, run this file
echo 9des.c
sed 's/.//' >9des.c <<'//GO.SYSIN DD 9des.c'
-/*
- * Plan 9 DES encryption.
- */
-
-#include "des.h"
-
-int
-encrypt(void *key, void *data, int len)
-{
- int n;
- int r;
- int i;
- char *b;
-
- if (len < DESBLOCKLEN)
- return 0;
-
- deskey(key);
-
- len -= DESBLOCKLEN;
- n = len / 7 + 1;
- r = len % 7;
-
- b = data;
-
- for (i = 0; i < n; i++)
- {
- des(b, 0);
- b += 7;
- }
-
- if (r)
- {
- b = data;
- des(&b[len], 0);
- }
-
- return 1;
-}
-
-/*
- * Plan 9 DES decryption.
- */
-int
-decrypt(void *key, void *data, int len)
-{
- int n;
- int r;
- int i;
- char *b;
-
- if (len < DESBLOCKLEN)
- return 0;
-
- deskey(key);
-
- len -= DESBLOCKLEN;
- n = len / 7 + 1;
- r = len % 7;
-
- b = data;
-
- if (r)
- des(&b[len], 1);
-
- b = &b[len - r];
-
- for (i = 0; i < n; i++)
- {
- des(b, 1);
- b -= 7;
- }
-
- return 1;
-}
-
-/*
- * Convert a Plan 9 key to a DES key.
- */
-uchar *
-des9key(uchar *key)
-{
- int i;
- int m1[] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F };
- int m2[] = { 0x00, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
- static uchar nkey[DESKEYPLEN];
-
- nkey[0] = key[0] & 0xFE;
-
- for (i = 1; i < 7; i++)
- nkey[i] = (key[i - 1] & m1[i]) << 8 - i | (key[i] & m2[i]) >> i;
-
- nkey[7] = (key[6] & 0x7F) << 1;
-
-
- return nkey;
-}
-
-/*
- * Securenet challenge encryption.
- */
-int
-netcrypt(void *key, void *data)
-{
- uchar b[DESBLOCKLEN];
-
- strncpy((char *)b, (char *)data, DESBLOCKLEN);
-
- if (!encrypt(des9key(key), b, DESBLOCKLEN))
- return 0;
-
- sprint((char *)data, "%.2ux%.2ux%.2ux%.2ux", b[0], b[1], b[2], b[3]);
- return 1;
-}
-
-/*
- * Map a hexadecimal string to Securenet decimal string.
- */
-void
-sechex(char *buf)
-{
- char *p;
-
- for (p = buf; *p != '\0'; p++)
- {
- switch (*p)
- {
- case 'A':
- case 'B':
- case 'C':
- case 'a':
- case 'b':
- case 'c':
- *p = '2';
- break;
-
- case 'D':
- case 'E':
- case 'F':
- case 'd':
- case 'e':
- case 'f':
- *p = '3';
- break;
- }
- }
-}
//GO.SYSIN DD 9des.c
echo Makefile
sed 's/.//' >Makefile <<'//GO.SYSIN DD Makefile'
-#
-# Makefile for libdes.a
-#
-
-LIB = libdes.a
-MAN = des.3
-
-SRC = \
- Makefile \
- README \
- des.h \
- des.c \
- test.c \
- $(MAN)
-
-CAT = des.3.cat
-
-all : test $(CAT)
- ./test
-
-$(LIB) : des.o
- -rm -f $(LIB)
- ar qv $(LIB) des.o
-
-test : test.o $(LIB)
- $(CC) -o test test.o $(LIB)
-
-$(CAT) : $(MAN)
- nroff -man $(MAN) > $(CAT)
-
-shar : $(SRC)
- shar $(SRC) > des.shar
-
-clean :
- -rm -f *.o $(CAT) des.shar
-
-nuke : clean
- -rm -f test $(LIB)
-
-des.o : des.h des.c
-test.o : des.h test.c
//GO.SYSIN DD Makefile
echo README
sed 's/.//' >README <<'//GO.SYSIN DD README'
-This is the source to an implementation of the Data Encryption
-Standard. The Makefile creates libdes.a, its manual entry and a test
-program. Running the test program validates the algorithm in the
-encryption direction and prints a message of 'all ok'. Any other
-message indicates trouble.
-
-
-Boyd Roberts
-boyd@plan9.cs.su.oz.au
//GO.SYSIN DD README
echo des.3
sed 's/.//' >des.3 <<'//GO.SYSIN DD des.3'
-.TH des 3
-.SH NAME
-des \- Data Encryption Standard encryption
-.SH SYNOPSIS
-#include "des.h"
-.sp
-void des(void *block, int ed)
-.sp
-void deskey(void *key)
-.SH DESCRIPTION
-.I Des
-and
-.I deskey
-implement encryption and decryption using the Data Encryption Standard
-.SM (DES).
-Encryption and decryption are performed destructively
-on a
-.I block
-of DESBLOCKLEN (defined as 8 in "des.h") bytes by
-.IR des .
-The
-.I ed
-parameter specifies the encryption direction; 0 for encryption, 1 for
-decryption.
-.PP
-Before encryption or decryption is performed the
-.I key
-must be set with
-.IR deskey .
-A
-.I key
-consists of a block of DESKEYPLEN (defined as 8 in "des.h") bytes which
-specify the 56 bit key; the low order bit of each byte is the parity bit,
-which is discarded. The
-.I key
-remains in use until another is set.
//GO.SYSIN DD des.3
echo des.c
sed 's/.//' >des.c <<'//GO.SYSIN DD des.c'
-/*
- * 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
- */
-
-#include "des.h"
-
-/*#define DEBUG 1*/
-
-#define XKEYLEN 8 /* expanded key length */
-#define ROUNDS 16
-
-/* manipulate the left and right halves of a DES block */
-#include <string.h>
-
-#define L_GET(b, x) memcpy(&x, &b[0], DESBLOCKLEN / 2)
-#define R_GET(b, x) memcpy(&x, &b[DESBLOCKLEN / 2], DESBLOCKLEN / 2)
-
-#define L_SET(b, x) memcpy(&b[0], &x, DESBLOCKLEN / 2)
-#define R_SET(b, x) memcpy(&b[DESBLOCKLEN / 2], &x, DESBLOCKLEN / 2)
-
-#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 u32bit 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 u32bit 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 u32bit 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 u32bit 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 u32bit 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 u32bit 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 u32bit 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(u32bit 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;
- u32bit l;
- u32bit 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;
- }
-
- L_SET(b, l);
- R_SET(b, r);
-}
-
-/*
- * Final Permutation.
- */
-static void
-ip_1(u32bit l, u32bit r, uchar *b)
-{
- int i;
- uchar c;
- u32bit pl;
- u32bit pr;
- static int ls[] = { 1, 3, 5, 7, };
- static int rs[] = { 0, 2, 4, 6, };
-
- pl = 0;
- pr = 0;
-
- for (i = 0; i < sizeof(u32bit); 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(u32bit); 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 u32bit
-rol(u32bit l)
-{
- int c;
-
- c = (l & 0x80000000) >> 27;
- l <<= 1;
- l |= c;
-
- return l;
-}
-
-/*
- * Rotate 28 bits left 2 bits.
- */
-static u32bit
-rol2(u32bit 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(u32bit k, uchar *x)
-{
- int i;
- u32bit 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
-deskey(void *key)
-{
- uchar *k;
- int i;
-u32bit j;
- u32bit c0;
- u32bit d0;
- static int c0m[] = { 0xE, 0xE, 0xE, 0xE, 0xF, 0xF, 0xF, 0xF };
- static int d0m[] = { 0xF, 0xF, 0xF, 0xF, 0x7, 0x7, 0x7, 0x7 };
- static u32bit (*rf[])(u32bit) = { rol, rol, rol2, rol2, rol2, rol2, rol2, rol2, rol, rol2, rol2, rol2, rol2, rol2, rol2, rol };
-
- k = (uchar *)key;
- 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]);
- d0 = (*rf[i])(d0);
- kexpand(kp(d0, kir_bits), &xkey[i][4]);
- }
-}
-
-/*
- * Expand a 32 bit message block to 8 bytes, each containing 6 bits.
- */
-static void
-mexpand(u32bit m, uchar *x)
-{
- int i;
- u32bit 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 u32bit
-mangle(u32bit m, int n)
-{
- int i;
- uchar xm[XKEYLEN];
-
- mexpand(m, xm);
-
- m = 0;
-
- for (i = 0; i < XKEYLEN; i++)
- {
- uchar x;
-
- x = xm[i] ^ xkey[n][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
-des(void *v, int ed)
-{
- uchar *b;
- int i;
- u32bit l;
- u32bit r;
-
-#ifdef DEBUG
- edflag = ed;
- round = 0;
-#endif
- b = (uchar *)v;
-
-#ifdef DEBUG
- log(b);
-#endif
-
- ip(b);
-
-#ifdef DEBUG
- log(b);
-#endif
-
- L_GET(b, l);
- R_GET(b, r);
-
- for (i = 0; i < ROUNDS; i++)
- {
- u32bit t;
-
-#ifdef DEBUG
- round = ed ? ROUNDS - i : i + 1;
-#endif
-
- t = r;
- r = mangle(r, ed ? ROUNDS - i - 1 : i) ^ l;
- l = t;
-#ifdef DEBUG
- L_SET(b, l); R_SET(b, r); log(b);
-#endif
- }
-
- /* undo round 16's swap before permuting */
- ip_1(r, l, b);
-
-#ifdef DEBUG
- round++;
- log(b);
-#endif
-}
//GO.SYSIN DD des.c
echo des.h
sed 's/.//' >des.h <<'//GO.SYSIN DD des.h'
-#define DESBLOCKLEN 8
-#define DESKEYPLEN 8
-
-typedef unsigned char uchar;
-
-#if __alpha
-typedef unsigned int u32bit;
-#else
-typedef unsigned long u32bit;
-#endif
-
-extern void deskey(void *);
-extern void des(void *, int);
//GO.SYSIN DD des.h
echo test.c
sed 's/.//' >test.c <<'//GO.SYSIN DD test.c'
-#include "des.h"
-
-typedef uchar block[DESBLOCKLEN];
-typedef struct test test;
-
-struct test
-{
- block key;
- block plain;
- block cipher;
-};
-
-struct test tests[] =
-{
- /* 0000000000000000 0000000000000000 8CA64DE9C1B123A7 */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7,
- /* FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 7359B2163E4EDC58 */
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x73, 0x59, 0xB2, 0x16, 0x3E, 0x4E, 0xDC, 0x58,
- /* 1111111111111111 1111111111111111 F40379AB9E0EC533 */
- 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
- 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
- 0xF4, 0x03, 0x79, 0xAB, 0x9E, 0x0E, 0xC5, 0x33,
- /* 0123456789ABCDEF 1111111111111111 17668DFC7292532D */
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
- 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
- 0x17, 0x66, 0x8D, 0xFC, 0x72, 0x92, 0x53, 0x2D,
- /* 1111111111111111 0123456789ABCDEF 8A5AE1F81AB8F2DD */
- 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
- 0x8A, 0x5A, 0xE1, 0xF8, 0x1A, 0xB8, 0xF2, 0xDD,
- /* 0000000000000000 0000000000000000 8CA64DE9C1B123A7 */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7,
- /* 0101010101010101 0123456789ABCDEF 617B3A0CE8F07100 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
- 0x61, 0x7B, 0x3A, 0x0C, 0xE8, 0xF0, 0x71, 0x00,
- /* 0000000000000000 FFFFFFFFFFFFFFFF 355550B2150E2451 */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x35, 0x55, 0x50, 0xB2, 0x15, 0x0E, 0x24, 0x51,
- /* FFFFFFFFFFFFFFFF 0000000000000000 CAAAAF4DEAF1DBAE */
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xCA, 0xAA, 0xAF, 0x4D, 0xEA, 0xF1, 0xDB, 0xAE,
- /* 0123456789ABCDEF 0000000000000000 D5D44FF720683D0D */
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xD5, 0xD4, 0x4F, 0xF7, 0x20, 0x68, 0x3D, 0x0D,
- /* 0101010101010101 95F8A5E5DD31D900 8000000000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x95, 0xF8, 0xA5, 0xE5, 0xDD, 0x31, 0xD9, 0x00,
- 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 20B9E767B2FB1456 0800000000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x20, 0xB9, 0xE7, 0x67, 0xB2, 0xFB, 0x14, 0x56,
- 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 D9031B0271BD5A0A 0080000000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xD9, 0x03, 0x1B, 0x02, 0x71, 0xBD, 0x5A, 0x0A,
- 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 ADD0CC8D6E5DEBA1 0008000000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xAD, 0xD0, 0xCC, 0x8D, 0x6E, 0x5D, 0xEB, 0xA1,
- 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 2B9F982F20037FA9 0000800000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x2B, 0x9F, 0x98, 0x2F, 0x20, 0x03, 0x7F, 0xA9,
- 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 E7FCE22557D23C97 0000080000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xE7, 0xFC, 0xE2, 0x25, 0x57, 0xD2, 0x3C, 0x97,
- 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 750D079407521363 0000008000000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x75, 0x0D, 0x07, 0x94, 0x07, 0x52, 0x13, 0x63,
- 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 D106FF0BED5255D7 0000000800000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xD1, 0x06, 0xFF, 0x0B, 0xED, 0x52, 0x55, 0xD7,
- 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
- /* 0101010101010101 E943D7568AEC0C5C 0000000080000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xE9, 0x43, 0xD7, 0x56, 0x8A, 0xEC, 0x0C, 0x5C,
- 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
- /* 0101010101010101 CA3A2B036DBC8502 0000000008000000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xCA, 0x3A, 0x2B, 0x03, 0x6D, 0xBC, 0x85, 0x02,
- 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- /* 0101010101010101 25EB5FC3F8CF0621 0000000000800000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x25, 0xEB, 0x5F, 0xC3, 0xF8, 0xCF, 0x06, 0x21,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
- /* 0101010101010101 8B54536F2F3E64A8 0000000000080000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x8B, 0x54, 0x53, 0x6F, 0x2F, 0x3E, 0x64, 0xA8,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
- /* 0101010101010101 1029D55E880EC2D0 0000000000008000 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x10, 0x29, 0xD5, 0x5E, 0x88, 0x0E, 0xC2, 0xD0,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
- /* 0101010101010101 8405D1ABE24FB942 0000000000000800 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x84, 0x05, 0xD1, 0xAB, 0xE2, 0x4F, 0xB9, 0x42,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
- /* 0101010101010101 2FBC291A570DB5C4 0000000000000080 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x2F, 0xBC, 0x29, 0x1A, 0x57, 0x0D, 0xB5, 0xC4,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
- /* 0101010101010101 CC083F1E6D9E85F6 0000000000000008 */
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0xCC, 0x08, 0x3F, 0x1E, 0x6D, 0x9E, 0x85, 0xF6,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
-};
-
-static void
-log(uchar *b, char *s)
-{
- printf("%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x%s", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], s);
-}
-
-int
-cmp(block b1, block b2)
-{
- int i;
-
- for (i = 0; i < DESBLOCKLEN; i++)
- {
- if (b1[i] != b2[i])
- return 1;
- }
-
- return 0;
-}
-
-main()
-{
- int i;
- int ok;
-
- ok = 1;
-
- for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
- {
- deskey(tests[i].key);
-
- des(tests[i].plain, 0);
-
- if (cmp(tests[i].plain, tests[i].cipher) != 0)
- {
- printf("E %2d expected ", i);
- log(tests[i].cipher, " encrypted ");
- log(tests[i].plain, "\n");
- ok = 0;
- }
- }
-
- if (ok)
- printf("all ok\n");
-
- exit(!ok);
-}
//GO.SYSIN DD test.c
|