Cryptography Reference
In-Depth Information
{
aes_block_decrypt( input, output, key, key_length );
xor( output, iv, AES_BLOCK_SIZE );
memcpy( ( void * ) iv, ( void * ) input, AES_BLOCK_SIZE ); // CBC
input += AES_BLOCK_SIZE;
output += AES_BLOCK_SIZE;
input_len -= AES_BLOCK_SIZE;
}
}
Notice the similarities between aes_encrypt / aes_decrypt and des_oper-
ate . CBC and block iteration are implemented the same in both cases. In fact,
CBC and block iteration are the same for all block ciphers. If you were going to
be implementing many more block ciphers, it would be worth the investment
to generalize these operations so you could just pass in a function pointer to
a generic block_operate function. Don't bother here, though, because you're
fi nished with block ciphers.
Finally — you do want the AES encryption/decryption routines to be inter-
changeable with the DES and 3DES encryption/decryption routines. For that to
be possible, the method signatures must be the same. Therefore, go ahead and
implement a couple of top-level functions as shown in Listing 2-44.
Listing 2-44: “aes.c” AES encryption and decryption routines
void aes_128_encrypt( const unsigned char *plaintext,
const int plaintext_len,
unsigned char ciphertext[],
const unsigned char *iv,
const unsigned char *key )
{
aes_encrypt( plaintext, plaintext_len, ciphertext, iv, key, 16 );
}
void aes_128_decrypt( const unsigned char *ciphertext,
const int ciphertext_len,
unsigned char plaintext[],
const unsigned char *iv,
const unsigned char *key )
{
aes_decrypt( ciphertext, ciphertext_len, plaintext, iv, key, 16 );
}
void aes_256_encrypt( const unsigned char *plaintext,
const int plaintext_len,
unsigned char ciphertext[],
const unsigned char *iv,
const unsigned char *key )
{
aes_encrypt( plaintext, plaintext_len, ciphertext, iv, key, 32 );
}
 
Search WWH ::




Custom Search