Cryptography Reference
In-Depth Information
}
void rc4_128_encrypt( const unsigned char *plaintext,
const int plaintext_len,
unsigned char ciphertext[],
void *state,
const unsigned char *key )
{
rc4_operate( plaintext, plaintext_len, ciphertext, key, 16,
( rc4_state * ) state );
}
void rc4_128_decrypt( const unsigned char *ciphertext,
const int ciphertext_len,
unsigned char plaintext[],
void *state,
const unsigned char *key )
{
rc4_operate( ciphertext, ciphertext_len, plaintext, key, 16,
( rc4_state * ) state );
}
If you compare these functions to des_encrypt , des3_encrypt and aes_encrypt ,
notice that they're almost identical except that the fourth parameter, the state ,
is a void pointer rather than an unsigned char pointer to an initialization vec-
tor. In fact, go ahead and change all eight encrypt/decrypt functions to accept
void pointers and cast them to the proper type. This commonality enables you
to switch from one encryption function to another by just changing a function
pointer. You will take advantage of this fl exibility in Chapter 6, when TLS itself is
examined — all of the functions developed in this chapter will be reused there.
Converting a Block Cipher to a Stream Cipher: The OFB
and COUNTER Block-Chaining Modes
Actually, a block cipher can be converted into a stream cipher. If you look at the
way CBC works, notice that the initialization vector is XORed with the input
and then the result is encrypted. What if you reverse that? What if you encrypt
the CBC, and then XOR that with the input? As it turns out, you end up with a
cipher just as secure as one that had its initialization vector applied fi rst and then
encrypted. This method of chaining is called OFB or output-feedback mode. The
principal benefi t of OFB is that the input doesn't have to be block-aligned. As
long as the initialization vector itself is of the correct block length — which it is
for every block except the very last — the fi nal block can just truncate its output.
The decryptor recognizes this short block and updates its output accordingly.
OFB isn't used in SSL. CTR mode didn't make it into TLS until version 1.2, so
this topic is revisited in Chapter 9 when AEAD encryption in TLS 1.2 is discussed.
Search WWH ::




Custom Search