Cryptography Reference
In-Depth Information
memcpy( ( void * ) recomb_box, ( void * ) ip_block, DES_BLOCK_SIZE / 2 );
memcpy( ( void * ) ip_block, ( void * ) ( ip_block + 4 ), DES_BLOCK_SIZE / 2 );
memcpy( ( void * ) ( ip_block + 4 ), ( void * ) recomb_box,
DES_BLOCK_SIZE / 2 );
// Final permutation (undo initial permutation)
permute( ciphertext, ip_block, fp_table, DES_BLOCK_SIZE );
}
This code is a bit long, but if you followed the descriptions of the permutations
and the Feistel function, you should be able to make sense of it.
DES Decryption
One of the nice things about the way DES was specifi ed is that decryption
is the exact same as encryption, except that the key schedule is inverted.
Instead of the original key being rotated left at each round, it's rotated right.
Otherwise, the routines are identical. You can easily add decryption support
to des_block_operate , as illustrated in Listing 2-14.
Listing 2-14: “des.c” des_block_operate with decryption support
typedef enum { OP_ENCRYPT, OP_DECRYPT } op_type;
static void des_block_operate( const unsigned char plaintext[ DES_BLOCK_SIZE ],
unsigned char ciphertext[ DES_BLOCK_SIZE ],
const unsigned char key[ DES_KEY_SIZE ],
op_type operation )
{
for ( round = 0; round < 16; round++ )
{
permute( expansion_block, ip_block + 4, expansion_table, 6 );
// “Key mixing”
// rotate both halves of the initial key
if ( operation == OP_ENCRYPT )
{
rol( pc1key );
if ( !( round <= 1 || round == 8 || round == 15 ) )
{
// Rotate twice except in rounds 1, 2, 9 & 16
rol( pc1key );
}
}
permute( subkey, pc1key, pc2_table, SUBKEY_SIZE );
if ( operation == OP_DECRYPT )
{
(Continued)
 
Search WWH ::




Custom Search