Cryptography Reference
In-Depth Information
// “Key mixing”
// rotate both halves of the initial key
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 );
xor( expansion_block, subkey, 6 );
// Substitution; “copy” from updated expansion block to ciphertext block
memset( ( void * ) substitution_block, 0, DES_BLOCK_SIZE / 2 );
substitution_block[ 0 ] =
sbox[ 0 ][ ( expansion_block[ 0 ] & 0xFC ) >> 2 ] << 4;
substitution_block[ 0 ] |=
sbox[ 1 ][ ( expansion_block[ 0 ] & 0x03 ) << 4 |
( expansion_block[ 1 ] & 0xF0 ) >> 4 ];
substitution_block[ 1 ] =
sbox[ 2 ][ ( expansion_block[ 1 ] & 0x0F ) << 2 |
( expansion_block[ 2 ] & 0xC0 ) >> 6 ] << 4;
substitution_block[ 1 ] |=
sbox[ 3 ][ ( expansion_block[ 2 ] & 0x3F ) ];
substitution_block[ 2 ] =
sbox[ 4 ][ ( expansion_block[ 3 ] & 0xFC ) >> 2 ] << 4;
substitution_block[ 2 ] |=
sbox[ 5 ][ ( expansion_block[ 3 ] & 0x03 ) << 4 |
( expansion_block[ 4 ] & 0xF0 ) >> 4 ];
substitution_block[ 3 ] =
sbox[ 6 ][ ( expansion_block[ 4 ] & 0x0F ) << 2 |
( expansion_block[ 5 ] & 0xC0 ) >> 6 ] << 4;
substitution_block[ 3 ] |=
sbox[ 7 ][ ( expansion_block[ 5 ] & 0x3F ) ];
// Permutation
permute( pbox_target, substitution_block, p_table, DES_BLOCK_SIZE / 2 );
// Recombination. XOR the pbox with left half and then switch sides.
memcpy( ( void * ) recomb_box, ( void * ) ip_block, DES_BLOCK_SIZE / 2 );
memcpy( ( void * ) ip_block, ( void * ) ( ip_block + 4 ),
DES_BLOCK_SIZE / 2 );
xor( recomb_box, pbox_target, DES_BLOCK_SIZE / 2 );
memcpy( ( void * ) ( ip_block + 4 ), ( void * ) recomb_box,
DES_BLOCK_SIZE / 2 );
}
// Swap one last time
Search WWH ::




Custom Search