Skip to content

Feat: Ciphertext Stealing/CTS - #197

Open
officialfrancismendoza wants to merge 5 commits into
bcgit:mainfrom
officialfrancismendoza:feature/officialfrancismendoza/185-cipher-text-stealing-CTS
Open

officialfrancismendoza wants to merge 5 commits into
bcgit:mainfrom
officialfrancismendoza:feature/officialfrancismendoza/185-cipher-text-stealing-CTS

Conversation

@officialfrancismendoza

Copy link
Copy Markdown
Contributor

CBC ciphertext stealing: CBC-CS1, CBC-CS2 and CBC-CS3, with CTS as CBC-CS3 (#185)

Adds the three ciphertext-stealing variants from the Addendum to NIST SP 800-38A (October 2010). They work over any 128-bit block cipher. CTS is CBC-CS3, as RFC 2040 Sec 8 defines it.

Merge the bc-test-data branch first. It is branch feature/officialfrancismendoza/185-cipher-text-stealing-CTS (bf0cd8f), which adds crypto/aes_tdes_vectors/CBC/bc-fips/. Without that directory, aes_cbc_cs_bc_fips_tests fails on a bc-test-data checkout, because the loader treats a missing file as a failure.

What's new

  • Mode types in modes::cbc_cs: CBC_CS<V>, with the NIST-named aliases CBC_CS1, CBC_CS2 and CBC_CS3. CTS is another name for CBC_CS3: same type, not a fourth mode.
  • Variant markers: sealed CS1, CS2 and CS3, through CsVariant<P>.
  • Worker: CbcCs<'k, P, Dir, V>, with CBC_CS_FINAL_LEN = 32. Keys hand it out through the existing SymmetricEncryptionKey / SymmetricDecryptionKey pair. The IV is drawn or supplied, and the ciphertext is as long as the plaintext.
  • AES worker aliases: AES_CBC_CS{1,2,3}_{128,192,256} and AES_CTS_{128,192,256}. ARIA, Camellia and SM4 keys get the modes through BlockCipherKey, with no aliases of their own.
  • ModeNames gains three required constants, CBC_CS1_ALG_NAME, CBC_CS2_ALG_NAME and CBC_CS3_ALG_NAME. This breaks any out-of-tree implementor. TDES and TDES2 set them to "".
  • CLI: aes{128,192,256}-cbc-cs{1,2,3}, with aes*-cts as visible aliases of the CS3 commands.
  • core-test-framework: a new TestFrameworkSymmetricCipher::min_message_len option, plus one sentence each in the error lists of do_encrypt_final and do_decrypt_final_out.

How it works

  • Reuse of CBC. CbcCs wraps the existing Cbc worker, so every CBC block goes through it, including decryption's four- and two-block batching.
  • Streaming. The worker holds back the last 17 to 32 bytes, because only the last two portions depend on where the message ends.
  • The finals follow the addendum step by step. Encryption runs CBC-CS1-Encrypt steps 2 to 5. Decryption first reverses CBC-CS2's and CBC-CS3's reordering, then runs CBC-CS1-Decrypt steps 2 to 6.
  • The only variant-specific code is CsVariant::swaps, which says whether the last two portions are written swapped: never for CS1, when the last block is partial for CS2, always for CS3.

Spec decisions

  • A one-block message is its CBC encryption under every variant. The addendum's steps refer to a block Cn-1 that doesn't exist when there is only one block. Three sources agree on plain CBC:

    • the addendum's Sec 2 note ("When Pn* is a complete block, CBC-CS1-Encrypt is equivalent to CBC encryption");
    • all 6,213 one-block NIST ACVP cases, CBC-CS3's included;
    • bc-fips.

    RFC 3962 Sec 5 differs: it encrypts one block in ECB mode without the IV, and pads shorter input. This code does neither.

  • Less than one block is refused at the final with InvalidInputLength, in both directions, as XTS refuses short input.

  • Lengths are in bytes. Sec 1 explicitly allows an implementation to restrict lengths to multiples of 8 bits.

Verification

  • NIST ACVP vectors (bc-test-data, both sets there): 14,158 byte-length cases, all three key lengths, both directions. Each runs through both one-shots and streamed in several piece sizes. The 5,848 bit-length cases are skipped and counted.
  • RFC 3962 Appendix B: the six AES-128 vectors pass through CTS. CBC-CS2 matches exactly the three with a partial last block.
  • bc-fips 2.1.3 (Maven Central, signed by the Legion of the Bouncy Castle). The JCA provider names it uses are AES/CBC/CS{1,2,3}Padding, AES/CTS/NoPadding and AES/CBC/CTSPadding.
    • 129 records across AES-128/192/256 are byte-identical in both directions, for all three variants and CTS.
    • Its 9 refused short lengths are refused here too.
    • bc-fips decrypted all 516 ciphertexts that the release CLI produced from those records, each byte-identical to its own. This check ran once, outside the suite.
  • Behaviour tests: each variant checked against its definition rebuilt over plain CBC, at every length from 16 to 101 bytes and at eight longer ones up to 4097. Also: the boundary lengths; streaming at every split point of a few lengths, and byte by byte up to 65 bytes; a refused update consuming nothing.
  • Shared suites: core-test-framework's symmetric suite and a comparison against each cipher's own CBC, run for AES, ARIA, Camellia and SM4.
  • Structural tests over toy ciphers, including proof that decryption takes CBC's batched paths.
  • Compile-time checks: CTS's type identity, the not-Clone list, and a compile_fail doctest (with a compiling twin) for an encryptor over a DECRYPT_ONLY cipher.
  • cargo mutants on cbc_cs.rs, with the vector suites live: 80 mutants, 63 caught, 17 unviable (each a compile error), none missed and none timed out.
  • Wycheproof has no CTS vectors at the pinned commit 65ca4947, so that requirement can't be met for this mode.

Benches and stack

  • Benchmark: on 16 KiB + 5 bytes, CBC-CS3 measured within 0.1% of CBC encrypting and 4% behind it decrypting (293.9 and 105.5 µs against 293.7 and 101.1), and 7% and 10% behind in a second run on a busier machine. The CBC-CS3 bench is a one-shot into a separate buffer; CBC's works in place.
  • Stack: encrypt_nonce_out 224 B and decrypt_out 568 B, the same for every variant and key length. These are measured as the XTS figures were and documented on AES_CBC_CS1_128.

Commits

  1. core-test-framework: the minimum-message-length option.
  2. symmetric: the mode, aliases, tests, bench, stack harness, docs and release note.
  3. cli: the commands.

Not in this PR

  • TDES: 64-bit blocks would need a second FINAL_LEN.
  • An in-place one-shot: the key traits offer in-place calls only for stream ciphers.
  • Aliases and CLI commands for ARIA, Camellia and SM4.

Assisted-by: Claude:claude-opus-5-5

🤖 Generated with Claude Code

…r suite

TestFrameworkSymmetricCipher::test_key round-trips every length from 0. CBC
with ciphertext stealing (issue bcgit#185) is defined only on messages of at least
one block (Addendum to NIST SP 800-38A, Sec 1), so the suite gains
min_message_len beside required_alignment and fixed_message_len: every shorter
length must be refused with InvalidInputLength by encrypt_out and the streaming
do_encrypt_final, and as a ciphertext by decrypt_out and do_decrypt_final, the
one-shots leaving their buffers zeroized. The default, 0, leaves every existing
caller as it was.

The error lists of SymmetricCipherEncryptor::do_encrypt_final and
SymmetricCipherDecryptor::do_decrypt_final_out name InvalidInputLength, which
such an implementor returns.

Assisted-by: Claude:claude-opus-5-5
…cgit#185)

The three ciphertext-stealing variants of the Addendum to NIST SP 800-38A
(October 2010) over any 128-bit block permutation, in modes::cbc_cs. CBC_CS<V>
is the mode type, named CBC_CS1, CBC_CS2 and CBC_CS3 after the NIST names, with
the sealed variants CS1, CS2 and CS3; CTS is CBC_CS3 under another name, the
ciphertext stealing of RFC 2040 Sec 8 (and of RFC 3962), so AES128Key<CTS> is
AES128Key<CBC_CS3>. A key hands out CbcCs<'k, P, Dir, V> through the
SymmetricEncryptionKey / SymmetricDecryptionKey pair, under a 16-byte IV drawn
or supplied, and the ciphertext is as long as the plaintext.

CbcCs wraps the existing Cbc worker, so every CBC block, and decryption's
four- and two-block batching, is Cbc's. It holds back the last 17 to 32 bytes,
since only the last two portions depend on where the message ends, and its
finals run CBC-CS1-Encrypt steps 2 to 5 and CBC-CS1-Decrypt steps 2 to 6 on
them, after CBC-CS2's and CBC-CS3's reordering. The one variant-specific rule,
CsVariant::swaps, says whether the last two portions are written swapped:
never (CS1), when the last block is partial (CS2), always (CS3). FINAL_LEN is
two blocks, CBC_CS_FINAL_LEN = 32.

A message of exactly one block is its CBC encryption under every variant. The
Addendum's steps name a Cn-1 that does not exist at n = 1; its Sec 2 note
("When Pn* is a complete block, CBC-CS1-Encrypt is equivalent to CBC
encryption"), all 6,213 one-block cases of the NIST ACVP vectors (CBC-CS3's
included) and bc-fips agree on CBC. RFC 3962 Sec 5 encrypts one block in ECB
mode instead, without the IV, and pads a shorter message; neither is done
here. A message shorter than one block is refused at the final with
InvalidInputLength, as XTS refuses one, in both directions. Lengths are in
bytes, the restriction to multiples of 8 bits that Sec 1 allows.

ModeNames gains CBC_CS1_ALG_NAME, CBC_CS2_ALG_NAME and CBC_CS3_ALG_NAME:
"AES_CBC_CS1_128" and so on, "ARIA_CBC_CS1_128", "Camellia_CBC_CS1_128",
"SM4_CBC_CS1", and "" for TDES and TDES2, whose 64-bit block the mode is not
implemented over. bouncycastle_symmetric::aes gains the worker aliases
AES_CBC_CS{1,2,3}_{128,192,256} and AES_CTS_{128,192,256}; ARIA, Camellia and
SM4 keys get the modes through BlockCipherKey, with no aliases of their own.

Tests:
- NIST ACVP AES-CBC-CS1/CS2/CS3 from bc-test-data, both sets there (vsId 42 and
  4014534-6), all key lengths and both directions: 14,158 octet-length cases
  through both one-shots and streamed in 7-, 16- and 17-byte pieces, and byte
  by byte up to 1 KiB; the 5,848 bit-length cases are skipped and counted.
- RFC 3962 Appendix B's six AES-128 vectors through CTS, and CBC-CS2 matching
  exactly those with a partial last block.
- bc-fips 2.1.3 interop vectors (bc-test-data
  crypto/aes_tdes_vectors/CBC/bc-fips, a separate commit there): 129 records
  under AES-128/192/256, all three variants and CTS, byte-identical in both
  directions, and the 9 lengths below a block refused here as there.
- Each variant at every length from 16 to 101 bytes, and at eight longer ones
  up to 4097, against its definition built over plain CBC; one block is CBC;
  whole blocks are CBC under CS1 and CS2 and CBC with the last two blocks
  swapped under CS3; CS2 is CS3 with a partial last block and CS1 the other
  order; a ciphertext decrypts only under its own variant; 0, 1 and 15 bytes
  refused through every route with the buffers zeroized; streaming at every
  split point of a few lengths and byte by byte up to 65 bytes; a refused
  update consumes nothing.
- core-test-framework's symmetric suite over every variant for AES, ARIA,
  Camellia and SM4 through cipher-test-framework (mode_keys::stealing_modes,
  under min_message_len = 16) and against each cipher's CBC
  (mode_keys::cbc_cs_agrees_with_cbc); the construction suites; the toy
  permutations, including decryption taking CBC's batched paths; the names,
  CTS's type identity, the not-Clone list, and a compile_fail doctest, with
  its compiling twin, for an encryptor over a DECRYPT_ONLY permutation.
- Wycheproof has no CTS vectors at the pinned commit (65ca4947).

cargo mutants on cbc_cs.rs, with the vector suites live: 80 mutants, 63
caught, 17 unviable (each a compile error: Default::default() for a type with
no Default, or an anyhow / std::io error in a no_std crate), none missed and
none timed out.

Benches: one CBC-CS3 encrypt / decrypt pair, one-shot on 16 KiB + 5 bytes,
beside CBC's pair, which works in place on 16 KiB. On the development
machine CBC-CS3 measured within 0.1% of CBC encrypting and 4% behind it
decrypting (293.9 and 105.5 us against 293.7 and 101.1), and 7% and 10%
behind in a second run on a busier machine. Stack: cbc_cs_* benches in
bench_aes_mem_usage; encrypt_nonce_out 224 B and decrypt_out 568 B, the same
for every variant and key length, read as the XTS figures were, are in the
docs of AES_CBC_CS1_128.

Assisted-by: Claude:claude-opus-5-5
…S3 (issue bcgit#185)

Nine subcommands, aes{128,192,256}-cbc-cs{1,2,3}, over one generic cbc_cs_cmd
that main instantiates per variant and key length, with aes{128,192,256}-cts
as clap visible aliases of the CS3 commands. They take the aes*-cbc commands'
IV framing (drawn and written ahead of the ciphertext, or supplied with --iv /
--iv-file) through the shared block-mode loops, whose final buffer is the
mode's two blocks; a worker holds back at most 32 bytes, so a call releases at
most the 1 KiB the loops read, which their buffer holds. The body is as long
as the input, and input shorter than one block is refused with an error exit
in both directions. The release note's ciphertext-stealing entry names the
commands.

Tests (cli/tests/aes_cbc_cs_cli_tests.rs): RFC 3962 Appendix B through
aes128-cts and aes128-cbc-cs3 under --iv; every command against the library
under a supplied IV, CTS against CBC-CS3; round trips with a drawn IV, fresh
per run; unpadded lengths from 16 bytes to either side of the 1 KiB reads; 0,
1 and 15 bytes refused in both directions; wrong key lengths; the help
listing. Once, outside the suite, the release binary encrypted every record of
bc-test-data's bc-fips interop file under each variant and CTS (516
ciphertexts) and bc-fips 2.1.3 decrypted all of them to their plaintexts
(check/CheckCbcCs.java there), each byte-identical to bc-fips's own; the 36
runs below a block were refused.

Assisted-by: Claude:claude-opus-5-5
@officialfrancismendoza officialfrancismendoza added the enhancement New feature or request label Nov 27, 2026
… (issue bcgit#185)

The Mode impl of CBC_CS<V> was bounded on Clone + ModeNames alone, so a
key such as BlockCipherKey<TDESInternal, CBC_CS3> could be built from a
valid three-key TDEA key. It reported the empty name TDES gave the mode,
and it could neither encrypt nor decrypt, the role impls asking for a
128-bit ElectronicCodeBook. The impl is now bounded on
BlockPermutation<16>, as GCM's, GMAC's, KW's and KWP's are, so a key for
a 64-bit block cipher under CBC-CS is not a type (E0277).

The CBC_CS1_ALG_NAME, CBC_CS2_ALG_NAME and CBC_CS3_ALG_NAME constants move
out of ModeNames into a new modes::CbcCsNames, implemented only by the
permutations the modes are implemented over (AES, ARIA, Camellia, SM4 and
the test toys used with them), as GCM-SIV's name is in GcmSivPermutation
rather than ModeNames. TDES, TDES2, CCM's unit-test permutation and the
forward-only test toy no longer carry CBC-CS names, and ModeNames is as
it was before CBC-CS, so the change no longer breaks an out-of-tree
ModeNames implementor. CsVariant<P> and the code generic over it, the
cipher-test-framework suites and the CLI's cbc_cs_cmd, name the new
trait.

A compile_fail doctest on CBC_CS shows the refusal, beside a twin in
which the same permutation under CBC is a type, so the pair fails if the
refusal goes or the example stops compiling for another reason (stable
rustdoc does not check the E0277 the fence names).

Checked on the tree with the CLI commit that follows: the symmetric
crate's suites and doctests (155 binaries, 1173 passed; the one failure,
wycheproof_ascon_aead128, is the local Wycheproof clone lacking its
vector file), the CLI suite in release (244 passed), rustdoc with
warnings denied, clippy (no warning on a line this branch adds), cargo
test --workspace --all-targets --no-run, and the crate's and the
umbrella's --no-default-features and alloc builds.

Assisted-by: Claude:claude-opus-5-5
…bcgit#185)

The --iv help of the eight CBC-CS commands other than aes128-cbc-cs1
pointed at "the IV paragraph above", which only aes128-cbc-cs1's help
has; it now points at aes128-cbc-cs1, as those commands' own text does.
The --iv-file help of all nine loses its stray comma and is wrapped as
--key-file's is. The other block-mode commands share both texts and are
left for a change of their own.

The release note is one line, as QUALITY_AND_STYLE.md asks of release
notes, and says the CLI is AES-only: ARIA, Camellia and SM4 have the
modes in the library and no CBC-CS commands.

Checked: the CLI suite in release (34 binaries, 244 passed) and rustdoc
with warnings denied.

Assisted-by: Claude:claude-opus-5-5
@officialfrancismendoza

Copy link
Copy Markdown
Contributor Author

Numerous merge conflicts post-update of main branch given api consistency changes, so updating feature branch accordingly

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant