Repository navigation
Feat: Ciphertext Stealing/CTS - #197
Open
officialfrancismendoza wants to merge 5 commits into
Open
officialfrancismendoza wants to merge 5 commits into
officialfrancismendoza wants to merge 5 commits into
Conversation
…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
… (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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
CTSis 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 addscrypto/aes_tdes_vectors/CBC/bc-fips/. Without that directory,aes_cbc_cs_bc_fips_testsfails on a bc-test-data checkout, because the loader treats a missing file as a failure.What's new
modes::cbc_cs:CBC_CS<V>, with the NIST-named aliasesCBC_CS1,CBC_CS2andCBC_CS3.CTSis another name forCBC_CS3: same type, not a fourth mode.CS1,CS2andCS3, throughCsVariant<P>.CbcCs<'k, P, Dir, V>, withCBC_CS_FINAL_LEN = 32. Keys hand it out through the existingSymmetricEncryptionKey/SymmetricDecryptionKeypair. The IV is drawn or supplied, and the ciphertext is as long as the plaintext.AES_CBC_CS{1,2,3}_{128,192,256}andAES_CTS_{128,192,256}. ARIA, Camellia and SM4 keys get the modes throughBlockCipherKey, with no aliases of their own.ModeNamesgains three required constants,CBC_CS1_ALG_NAME,CBC_CS2_ALG_NAMEandCBC_CS3_ALG_NAME. This breaks any out-of-tree implementor. TDES and TDES2 set them to"".aes{128,192,256}-cbc-cs{1,2,3}, withaes*-ctsas visible aliases of the CS3 commands.TestFrameworkSymmetricCipher::min_message_lenoption, plus one sentence each in the error lists ofdo_encrypt_finalanddo_decrypt_final_out.How it works
CbcCswraps the existingCbcworker, so every CBC block goes through it, including decryption's four- and two-block batching.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:
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
CTS. CBC-CS2 matches exactly the three with a partial last block.AES/CBC/CS{1,2,3}Padding,AES/CTS/NoPaddingandAES/CBC/CTSPadding.Clonelist, and acompile_faildoctest (with a compiling twin) for an encryptor over aDECRYPT_ONLYcipher.cargo mutantsoncbc_cs.rs, with the vector suites live: 80 mutants, 63 caught, 17 unviable (each a compile error), none missed and none timed out.65ca4947, so that requirement can't be met for this mode.Benches and stack
encrypt_nonce_out224 B anddecrypt_out568 B, the same for every variant and key length. These are measured as the XTS figures were and documented onAES_CBC_CS1_128.Commits
core-test-framework: the minimum-message-length option.symmetric: the mode, aliases, tests, bench, stack harness, docs and release note.cli: the commands.Not in this PR
FINAL_LEN.Assisted-by: Claude:claude-opus-5-5
🤖 Generated with Claude Code