67 #define DES_SCHEDULE 0x0d556aab 87 #define SBYTE( x, y ) ( ( (y) << 4 ) | (x) ) 95 #define SBOX( x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf, \ 96 y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, ya, yb, yc, yd, ye, yf ) \ 97 SBYTE ( x0, y0 ), SBYTE ( x1, y1 ), SBYTE ( x2, y2 ), SBYTE ( x3, y3 ),\ 98 SBYTE ( x4, y4 ), SBYTE ( x5, y5 ), SBYTE ( x6, y6 ), SBYTE ( x7, y7 ),\ 99 SBYTE ( x8, y8 ), SBYTE ( x9, y9 ), SBYTE ( xa, ya ), SBYTE ( xb, yb ),\ 100 SBYTE ( xc, yc ), SBYTE ( xd, yd ), SBYTE ( xe, ye ), SBYTE ( xf, yf ) 105 SBOX ( 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
106 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 ),
107 SBOX ( 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
108 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 )
111 SBOX ( 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
112 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 ),
113 SBOX ( 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
114 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 )
117 SBOX ( 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
118 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 ),
119 SBOX ( 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
120 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 )
123 SBOX ( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
124 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 ),
125 SBOX ( 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
126 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 )
129 SBOX ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
130 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 ),
131 SBOX ( 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
132 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 )
135 SBOX ( 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
136 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 ),
137 SBOX ( 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
138 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 )
141 SBOX ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
142 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 ),
143 SBOX ( 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
144 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 )
147 SBOX ( 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
148 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 ),
149 SBOX ( 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
150 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 )
163 #define DES_PC2( x ) ( (x) + ( ( (x) > 28 ) ? 4 : 0 ) ) 181 #define DES_PC2R( r1, c3, c2, c1, c0, r0 ) \ 214 #define DES_GENERATOR( PERMUTATION, OFFSET, INV5, BIT5, INV4, BIT4, \ 215 INV3, BIT3, INV2, BIT2, INV1, BIT1, INV0, BIT0 ) \ 217 .permutation = (PERMUTATION), \ 218 .seed = ( ( (INV0) << 31 ) | ( (BIT0) << 28 ) | \ 219 ( (INV1) << 27 ) | ( (BIT1) << 24 ) | \ 220 ( (INV2) << 23 ) | ( (BIT2) << 20 ) | \ 221 ( (INV3) << 19 ) | ( (BIT3) << 16 ) | \ 222 ( (INV4) << 15 ) | ( (BIT4) << 12 ) | \ 223 ( (INV5) << 11 ) | ( (BIT5) << 8 ) | \ 224 ( ( uint32_t ) sizeof (PERMUTATION) - 1 ) | \ 240 DES_PC2R ( 41, 52, 31, 37, 47, 55 ),
241 DES_PC2R ( 30, 40, 51, 45, 33, 48 ),
242 DES_PC2R ( 44, 49, 39, 56, 34, 53 ),
243 DES_PC2R ( 46, 42, 50, 36, 29, 32 ),
252 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
253 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
266 DES_GENERATOR (
des_ip, 1, 1, 2, 1, 1, 1, 0, 0, 4, 0, 3, 1, 5 ),
282 DES_GENERATOR (
des_fp, 1, 0, 0, 0, 2, 0, 1, 1, 5, 1, 4, 1, 3 ),
287 DES_GENERATOR (
des_pc1c, 1, 1, 2, 1, 1, 1, 0, 0, 5, 0, 4, 0, 3 ),
297 DES_GENERATOR (
des_pc1d, 0, 1, 2, 1, 1, 1, 0, 1, 5, 1, 4, 1, 3 ),
309 unsigned int index = 0;
340 accum |= (
bit & 0x01 );
342 }
while ( accum & 0x80 );
345 accum += (
seed & 0x01 );
354 ( (
seed & 0xfe ) + 1 ) );
389 while ( (
bit = *(permutation++) ) ) {
391 if (
in[
bit / 8 ] & ( 0x80 >> (
bit % 8 ) ) )
394 mask = ror8 ( mask, 1 );
395 if ( mask == 0x80 ) {
417 for ( i = 0 ; i < 8 ; i++ ) {
460 lookup = (
in ^
key );
475 sub =
des_s[i][ lookup & 0x1f ];
476 sub >>= ( ( lookup >> 29 ) & 4 );
499 left =
block->left.dword;
500 right =
block->right.dword;
506 block->left.dword = right;
513 block->right.dword ^= left;
539 rkey = ( ( (
void * ) rkey ) +
offset );
543 DBGC ( &
tmp,
"DES %scrypted %08x%08x => ",
567 sdword =
rol32 ( sdword, 1 );
595 DBGC ( des,
"DES %p new key:\n", des );
602 DBGC2 ( des,
"DES %p C[ 0]=%07x D[ 0]=%07x\n",
607 for ( schedule =
DES_SCHEDULE ; schedule ; schedule >>= 1 ) {
614 if ( ! ( schedule & 1 ) )
619 DBGC2 ( des,
"DES %p C[%2zd]=%07x D[%2zd]=%07x K[%2zd]=" 620 "%08x%08x\n", des, ( ( rkey - des->
rkey ) + 1 ),
622 ( ( rkey - des->
rkey ) + 1 ),
624 ( ( rkey - des->
rkey ) + 1 ),
672 -sizeof ( des->
rkey[0] ) );
static const uint8_t des_p[33]
DES data permutation (P)
#define EINVAL
Invalid argument.
#define DES_PC2R(r1, c3, c2, c1, c0, r0)
Define six bits of permuted choice 2 (PC2)
uint8_t byte[4]
Raw bytes.
static void des_generate(struct des_generator *generator)
Generate DES permutation.
static unsigned int unsigned int reg
static unsigned int unsigned int bit
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
A DES 32-bit dword value.
A DES systematic permutation generator.
void cipher_null_setiv(void *ctx __unused, const void *iv __unused, size_t ivlen __unused)
static void des_encrypt(void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
struct init_fn des_init_fn __init_fn(INIT_NORMAL)
Initialisation function.
ECB_CIPHER(des_ecb, des_ecb_algorithm, des_algorithm, struct des_context, DES_BLOCKSIZE)
static uint8_t des_pc1d[33]
DES permuted choice 1 (PC1) "D" register.
static void des_permute(const uint8_t *permutation, const uint8_t *in, uint8_t *out)
Perform bit permutation.
struct cipher_algorithm des_cbc_algorithm
struct golan_eq_context ctx
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct cipher_algorithm des_algorithm
Basic DES algorithm.
#define DES_SCHEDULE
DES shift schedule.
static u32 ror32(u32 v, int bits)
Rotate 32-bit value right.
Electronic codebook (ECB)
static uint8_t des_pc1c[29]
DES permuted choice 1 (PC1) "C" register.
union des_round_key rkey[DES_ROUNDS]
Round keys.
#define DES_ROUNDS
Number of DES rounds.
#define INIT_NORMAL
Normal initialisation.
An initialisation function.
#define be32_to_cpu(value)
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void des_round(union des_block *block, const union des_round_key *rkey)
Perform a single DES round.
static const uint8_t des_s[8][32]
DES S-boxes S1..S8.
struct cipher_algorithm des_ecb_algorithm
#define SBOX(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf, y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, ya, yb, yc, yd, ye, yf)
Define a row pair in a DES S-box.
static struct des_generator des_generators[]
DES permutation generators.
static void des_decrypt(void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
uint32_t dword[2]
32-bit big-endian dwords
#define DES_GENERATOR(PERMUTATION, OFFSET, INV5, BIT5, INV4, BIT4, INV3, BIT3, INV2, BIT2, INV1, BIT1, INV0, BIT0)
Define a DES permutation generator.
static uint32_t des_rol28(uint32_t dword)
Rotate 28-bit word.
uint32_t dword
32-bit big-endian dword
uint8_t byte[8]
Raw bytes.
uint8_t * permutation
Permutation.
#define cpu_to_be32(value)
int8_t step[8]
6-bit step key byte
static const uint8_t des_pc2[65]
DES permuted choice 2 (PC2)
uint8_t block[3][8]
DES-encrypted blocks.
#define DES_BLOCKSIZE
DES blocksize.
static void des_init(void)
Initialise permutations.
uint16_t offset
Offset to command line.
CBC_CIPHER(des_cbc, des_cbc_algorithm, des_algorithm, struct des_context, DES_BLOCKSIZE)
static void des_rounds(const union des_block *in, union des_block *out, const union des_round_key *rkey, ssize_t offset)
Perform all DES rounds.
const char * name
Algorithm name.
void cipher_null_auth(void *ctx __unused, void *auth __unused)
static uint8_t des_fp[65]
DES final / inverse initial permutation (FP / IP^-1)
static uint8_t des_ip[65]
DES initial permutation (IP)
static uint32_t des_sbox(uint32_t in, const union des_round_key *rkey)
Perform DES S-box substitution.
static int des_setkey(void *ctx, const void *key, size_t keylen)
Set key.