Mastering Algorithms With C - Kyle Loudon [188]
/**************************************************************************
* *
* Begin the computation of f. *
* *
**************************************************************************/
memcpy(fblk, rblk, 4);
/**************************************************************************
* *
* Permute and expand the copy of the right block into 48 bits. *
* *
**************************************************************************/
permute(fblk, DesExpansion, 48);
/**************************************************************************
* *
* Apply the appropriate subkey for the round. *
* *
**************************************************************************/
if (direction == encipher) {
/***********************************************************************
* *
* For enciphering, subkeys are applied in increasing order. *
* *
***********************************************************************/
bit_xor(fblk, subkeys[i], xblk, 48);
memcpy(fblk, xblk, 6);
}
else {
/***********************************************************************
* *
* For deciphering, subkeys are applied in decreasing order. *
* *
***********************************************************************/
bit_xor(fblk, subkeys[15 - i], xblk, 48);
memcpy(fblk, xblk, 6);
}
/**************************************************************************
* *
* Do the S-box substitutions. *
* *
**************************************************************************/
p = 0;
for (j = 0; j < 8; j++) {
/***********************************************************************
* *
* Compute a row and column into the S-box tables. *
* *
***********************************************************************/
row = (bit_get(fblk, (j * 6)+0) * 2) + (bit_get(fblk, (j * 6)+5) * 1);
col = (bit_get(fblk, (j * 6)+1) * 8) + (bit_get(fblk, (j * 6)+2) * 4) +
(bit_get(fblk, (j * 6)+3) * 2) + (bit_get(fblk, (j * 6)+4) * 1);
/***********************************************************************
* *
* Do the S-box substitution for the current six-bit block. *
* *
***********************************************************************/
sblk = (unsigned char)DesSbox[j][row][col];
for (k = 4; k < 8; k++) {
bit_set(fblk, p, bit_get(&sblk, k));
p++;
}
}
/**************************************************************************
* *
* Do the P-box permutation to complete f. *
* *
**************************************************************************/
permute(fblk, DesPbox, 32);
/**************************************************************************
* *
* Compute the XOR of the left block and f. *
* *
**************************************************************************/
bit_xor(lblk, fblk, xblk, 32);
/**************************************************************************
* *
* Set the left block for the round. *
* *
**************************************************************************/
memcpy(lblk, rblk, 4);
/**************************************************************************
* *
* Set the right block for the round. *
* *
**************************************************************************/
memcpy(rblk, xblk, 4);
}
/*****************************************************************************
* *
* Set the target text to the rejoined final right and left blocks. *
* *
*****************************************************************************/
memcpy(&target[0], rblk, 4);
memcpy(&target[4], lblk, 4);
/*****************************************************************************
* *
* Do the final permutation. *
* *
*****************************************************************************/
permute(target, DesFinal, 64);
return 0;
}
/*****************************************************************************
* *
* ----------------------------- des_encipher ----------------------------- *
* *
*****************************************************************************/