Ciphertext stealing

From Wikipedia, the free encyclopedia

In cryptography, ciphertext stealing (CTS) is a general method of using a block cipher mode of operation that allows for processing of messages that are not evenly divisible into blocks without resulting in any expansion of the ciphertext, at the cost of significantly increased complexity.

Contents

[edit] General characteristics

Ciphertext stealing is the technique of altering processing of the last two blocks of plaintext, resulting in a reordered transmission of the last two blocks of ciphertext and no ciphertext expansion (i.e., the resulting ciphertext is the same length as the plaintext, as compared to the longer ciphertext that would result from most padding schemes used with ECB and CBC modes). This is accomplished by padding the last block (which is possibly incomplete) with the low order bits from the second to last ciphertext block (stealing the ciphertext from the second to last block). The (now full) last block is encrypted, and then exchanged with the second to last ciphertext block, which is then truncated to the length of the final plaintext block (thus removing the bits that were stolen), resulting in ciphertext of the same length as the original message size. In all cases, the processing of all but the last two blocks is unchanged. The scheme described is consistent with Daemen and Schneier; Meyer describes a related, but incompatible scheme (with respect to bit ordering and key use).

In principle, any block-oriented block cipher mode of operation can be used (stream-cipher-like modes can already be applied to messages of arbitrary length without padding, so they do not benefit from this technique). The common block cipher mode of operation that are coupled with ciphertext stealing are the ECB and CBC modes.

Ciphertext stealing requires the plaintext to be longer than one block. A possible workaround is to use a stream cipher like block cipher mode of operation when the plaintext length is one block or less, such as the CTR, CFB or OFB modes.

To implement CTS encryption or decryption for data of unknown length, the implementation must delay processing (and buffer) the two most recent blocks of data, so that they can be properly processed at the end of the data stream.

[edit] Ciphertext stealing mode description

In order to encrypt or decrypt data, use the standard block cipher mode of operation on all but the last two blocks of data.

The following steps describe how to handle the last two blocks of the plaintext, called Pn−1 and Pn, where the length of Pn−1 equals the block size of the cipher in bits, B, the length of the last block, Pn, is M bits, and K is the key that is in use. M can range from 1 to B, inclusive, so Pn could possibly be a complete block. The CBC mode description also makes use of the ciphertext block just previous to the blocks concerned, Cn−2, which may in fact be the IV if the plaintext fits within two blocks.

For this description, the following functions and operators are used:

  • Head (data, a): returns the first a bits of the 'data' string.
  • Tail (data, a): returns the last a bits of the 'data' string.
  • Encrypt (K, data): use the underlying block cipher in encrypt mode on the 'data' string using the key K.
  • Decrypt (K, data): use the underlying block cipher in decrypt mode on the 'data' string using the key K.
  • XOR: Bitwise Exclusive-OR. Equivalent to bitwise addition without use of a carry bit.
  • ||: Concatenation operator. Combine the strings on either side of the operator.
  • 0a: a string of a 0 bits.

[edit] ECB ciphertext stealing

Ciphertext stealing in ECB mode introduces an intra-block dependency within the last two blocks, resulting in altered error propagation behavior for the last two blocks.

[edit] ECB encryption steps

  1. En−1 = Encrypt (K, Pn−1). Encrypt Pn−1 to create En−1. This is equivalent to the behavior of standard ECB mode.
  2. Cn = Head (En−1, M). Select the first M bits of En−1 to create Cn. The final ciphertext block, Cn, is composed of the leading M bits of the second-to-last ciphertext block. In all cases, the last two blocks are sent in a different order than the corresponding plaintext blocks.
  3. Dn = Pn || Tail (En−1, BM). Pad Pn with the low order bits from En−1.
  4. Cn−1 = Encrypt (K, Dn). Encrypt Dn to create Cn−1. For the first M bits, this is equivalent to what would happen in ECB mode (other than the ciphertext ordering). For the last BM bits, this is the second time that this data has been encrypted under this key (It was already encrypted in the production of En−1 in step 2).

[edit] ECB decryption steps

  1. Dn = Decrypt (K, Cn−1). Decrypt Cn−1 to create Dn. This undoes step 4 of the encryption process.
  2. En−1 = Cn || Tail (Dn, BM). Pad Cn with the extracted ciphertext in the tail end of Dn (placed there in step 3 of the ECB encryption process).
  3. Pn = Head (Dn, M). Select the first M bits of Dn to create Pn. As described in step 3 of the ECB encryption process, the first M bits of Dn contain Pn. We queue this last (possibly partial) block for eventual output.
  4. Pn−1 = Decrypt (K, En−1). Decrypt En−1 to create Pn−1. This reverses encryption step 1.

[edit] ECB ciphertext stealing error propagation

A bit error in the transmission of Cn−1 would result in the block-wide corruption of both Pn−1 and Pn. A bit error in the transmission of Cn would result in the block-wide corruption of Pn−1. This is a significant change from ECB's error propagation behavior.

[edit] CBC ciphertext stealing

In CBC, there is already interaction between processing of different adjacent blocks, so CTS has less conceptual impact in this mode. Error propagation is affected.

[edit] CBC encryption steps

  1. Xn−1 = Pn−1 XOR Cn−2. Exclusive-OR Pn−1 with the previous ciphertext block, Cn−2, to create Xn−1. This is equivalent to the behavior of standard CBC mode.
  2. En−1 = Encrypt (K, Xn−1). Encrypt Xn−1 to create En−1. This is equivalent to the behavior of standard CBC mode.
  3. Cn = Head (En−1, M). Select the first M bits of En−1 to create Cn. The final ciphertext block, Cn, is composed of the leading M bits of the second-to-last ciphertext block. In all cases, the last two blocks are sent in a different order than the corresponding plaintext blocks.
  4. P = Pn || 0BM. Pad Pn with zeros at the end to create P of length B. The zero padding in this step is important for step 5.
  5. Dn = En−1 XOR P. Exclusive-OR En−1 with P to create Dn. For the first M bits of the block, this is equivalent to CBC mode; the first M bits of the previous block's ciphertext, En−1,are XORed with the M bits of plaintext of the last plaintext block. The zero padding of P in step 4 was important, because it makes the XOR operation's effect on the last BM bits equivalent to copying the last BM bits of En−1 to the end of Dn. These are the same bits that were stripped off of En−1 in step 3 when Cn was created.
  6. Cn−1 = Encrypt (K, Dn). Encrypt Dn to create Cn−1. For the first M bits, this is equivalent to what would happen in CBC mode (other than the ciphertext ordering). For the last BM bits, this is the second time that this data has been encrypted under this key (It was already encrypted in the production of En−1 in step 2).

[edit] CBC decryption steps

  1. Dn = Decrypt (K, Cn−1). Decrypt Cn−1 to create Dn. This undoes step 6 of the encryption process.
  2. C = Cn || 0BM. Pad Cn with zeros at the end to create a block C of length B. We are padding Cn with zeros to help in step 3.
  3. Xn = Dn XOR C. Exclusive-OR Dn with C to create Xn. Looking at the first M bits, this step has the result of XORing Cn (the first M bits of the encryption process' En−1) with the (now decrypted) Pn XOR Head (En−1, M) (see steps 4-5 of the encryption process). In other words, we have CBC decrypted the first M bits of Pn. Looking at the last BM bits, this recovers the last BM bits of En−1.
  4. Pn = Head (Xn, M). Select the first M bits of Xn to create Pn. As described in step 3, the first M bits of Xn contain Pn. We queue this last (possibly partial) block for eventual output.
  5. En−1 = Cn || Tail (Xn, BM). Append the tail (BM) bits of Xn to Cn to create En−1. As described in step 3, En−1 is composed of all of Cn (which is M bits long) appended with the last BM bits of Xn. We reassemble En−1 (which is the same En−1 seen in the encryption process) for processing in step 6.
  6. Xn−1 = Decrypt (K, En−1). Decrypt En−1 to create Xn−1. This reverses encryption step 2. Xn−1 is the same as in the encryption process.
  7. Pn−1 = Xn−1 XOR Cn−2. Exclusive-OR Xn−1 with the previous ciphertext block, Cn−2, to create Pn−1. Finally, we reverse the XOR step from step 1 of the encryption process.

[edit] CBC Implementation Notes

For CBC ciphertext stealing, there is a clever (but opaque) method of implementing the described ciphertext stealing process using a standard CBC interface. Using this method imposes a performance penalty in the decryption stage of one extra block decryption operation over what would be necessary using a dedicated implementation.

[edit] CBC ciphertext stealing encryption using a standard CBC interface
  1. Encrypt the plaintext through the last full block using the standard CBC mode.
  2. Pad the last partial block with the trailing ciphertext of the last full block
  3. Encrypt the last block (plaintext plus ciphertext)
  4. Swap the last two ciphertext blocks.
  5. Truncate the ciphertext to the length of the original plaintext.

[edit] CBC ciphertext stealing decryption using a standard CBC interface
  1. Dn = Decrypt (K, Cn−1). Decrypt the second to the last ciphertext block.
  2. Cn = Cn || Tail (Dn, BM). Pad the ciphertext to the nearest multiple of the block size using the last BM bits of block cipher decryption of the second-to-last ciphertext block.
  3. Swap the last two ciphertext blocks.
  4. Decrypt the ciphertext (except the now last block) using the standard CBC mode.
  5. Truncate the plaintext to the length of the original ciphertext.

[edit] CBC ciphertext stealing error propagation

A bit error in the transmission of Cn−1 would result in the block-wide corruption of both Pn−1 and Pn. A bit error in the transmission of Cn would result in a corresponding bit error in Pn, and in the block-wide corruption of Pn−1.


[edit] References


Block ciphers
v  d  e
Algorithms: 3-Way | AES | Akelarre | Anubis | ARIA | BaseKing | Blowfish | C2 | Camellia | CAST-128 | CAST-256 | CIKS-1 | CIPHERUNICORN-A | CIPHERUNICORN-E | CMEA | Cobra | COCONUT98 | Crab | CRYPTON | CS-Cipher | DEAL | DES | DES-X | DFC | E2 | FEAL | FROG | G-DES | GOST | Grand Cru | Hasty Pudding Cipher | Hierocrypt | ICE | IDEA | IDEA NXT | Iraqi | Intel Cascade Cipher | KASUMI | KHAZAD | Khufu and Khafre | KN-Cipher | Libelle | LOKI89/91 | LOKI97 | Lucifer | M6 | MacGuffin | Madryga | MAGENTA | MARS | Mercy | MESH | MISTY1 | MMB | MULTI2 | NewDES | NOEKEON | NUSH | Q | RC2 | RC5 | RC6 | REDOC | Red Pike | S-1 | SAFER | SC2000 | SEED | Serpent | SHACAL | SHARK | Skipjack | SMS4 | Square | TEA | Triple DES | Twofish | UES | Xenon | xmx | XTEA | Zodiac
Design: Feistel network | Key schedule | Product cipher | S-box | SPN

Attacks: Brute force | Linear / Differential / Integral cryptanalysis | Mod n | Related-key | Slide | XSL

Standardization: AES process | CRYPTREC | NESSIE

Misc: Avalanche effect | Block size | IV | Key size | Modes of operation | Piling-up lemma | Weak key

Cryptography
v  d  e
History of cryptography | Cryptanalysis | Cryptography portal | Topics in cryptography
Symmetric-key algorithm | Block cipher | Stream cipher | Public-key cryptography | Cryptographic hash function | Message authentication code | Random numbers