Skip to content

core: split BlockCipher into block-aligned BlockCipherEncryptor/Decryptor - #96

Open
dghgit wants to merge 3 commits into
release/0.1.3alphafrom
feature/block-cipher-to-multi
Open

core: split BlockCipher into block-aligned BlockCipherEncryptor/Decryptor#96
dghgit wants to merge 3 commits into
release/0.1.3alphafrom
feature/block-cipher-to-multi

Conversation

@dghgit

@dghgitdghgit commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

I had some left over credits (I get reset at 7:00 AM Oz, time). Something similar needs to be done for symmetric cipher's as well, but there are a few things that need to get bedded down before this. I've got a second PR which I'll submit soon which does padding, I was also able to model CBC mode before I ran out, so the Encryptor/Decryptor traits have shown themselves to be adaptable enough. The actual traits are based on the KEM Encapsulator/Decapsulator traits.

The other difference, which is the addition of multi-block support is based on what we've found in the BC LTS Java release when we needed to add multi-lane support for AES. Basically the old tried and try BlockCipher class didn't make it and then we ended up having to add MultiBlockCipher and then shoehorning it into various spots in the existing API to make sure everything synced up correctly. I figured we should probably spare ourselves rediscovering that here.

There's no need for do_final in this case as the block cipher constructs assume everything is block aligned. This will make sense in the next PR which introduces support for PKCS#7 padding (CBC modeling was done with this too).

Claude Summary follows:

Reworks the block cipher streaming traits in core ahead of the first mode implementations. No implementors exist yet, so this is trait-shape only plus the matching test-framework update.

Changes

  • Split BlockCipher into BlockCipherEncryptor and BlockCipherDecryptor, mirroring KEMEncapsulator / KEMDecapsulator, so an implementation can encode direction in its type (e.g. Cbc<Aes128, Encrypting>) and a policy can permit decryption while forbidding new encryptions. A minimal BlockCipher supertrait carries the shared MAX_SECURITY_STRENGTH. The one-shot SymmetricCipher is no longer a supertrait.
  • do_{en,de}crypt_blocks[_out]<const N> replace the single-block methods, taking &[[u8; BLOCK_LEN]; N]. The block count is a const generic, so in/out lengths cannot disagree at runtime and no count parameter is needed; N = 1 is a single block.
  • do_encrypt_init_rng(key, &mut dyn RNG) added alongside do_encrypt_init, matching the encaps / encaps_rng pattern, so known-IV test vectors can be driven through the trait without exposing a caller-supplied IV.
  • do_{en,de}crypt_final[_out] removed. The traits are now strictly block-aligned; padding of arbitrary-length data belongs to a separate layer (PaddedEncryptor / PaddedDecryptor, follow-up PR on feature/block-cipher-padding).

Test framework

TestFrameworkBlockCipher::test now takes separate E: BlockCipherEncryptor / D: BlockCipherDecryptor type parameters and exercises N = 1 and N = 2, including mixed single/multi-block encrypt vs decrypt sequences, so an implementation must treat a multi-block call as exactly the sequential equivalent.

Verification

cargo build --workspace, cargo test --workspace, cargo doc (no broken intra-doc links), cargo fmt --check all clean.

dghgitand others added 2 commits August 30, 2026 17:24
…ptor
Rework the block cipher streaming traits ahead of the first mode
implementations:
- Split the single BlockCipher trait into BlockCipherEncryptor and
BlockCipherDecryptor (mirroring KEMEncapsulator/KEMDecapsulator) so
the direction can be encoded in the implementing type. A minimal
BlockCipher supertrait carries the shared MAX_SECURITY_STRENGTH.
The SymmetricCipher one-shot API is no longer a supertrait.
- Replace the single-block do_{en,de}crypt_block[_out] with
do_{en,de}crypt_blocks[_out]<const N>, taking &[[u8; BLOCK_LEN]; N]
so the block count is compile-time and in/out lengths cannot
disagree.
- Add do_encrypt_init_rng(key, &mut dyn RNG) alongside do_encrypt_init,
matching the encaps/encaps_rng pattern.
- Remove the do_{en,de}crypt_final[_out] methods. The traits are now
strictly block-aligned; padding of arbitrary-length data belongs to
a separate PaddedEncryptor/PaddedDecryptor layer to be built on top.
Update the core-test-framework block cipher test to take separate
encryptor/decryptor type parameters and to exercise N = 1 and N = 2,
including mixed single/multi-block encrypt vs decrypt sequences.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Formatting for the previous commit; no semantic change.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…traits
Provided (default) methods on BlockCipherEncryptor -- encrypt_blocks,
encrypt_blocks_rng, encrypt_blocks_out, encrypt_blocks_out_rng -- and on
BlockCipherDecryptor -- decrypt_blocks, decrypt_blocks_out -- implemented
once in the trait as init + blocks, so every block-aligned mode gets the
house-standard take-data-return-result static API at no cost to
implementors. Arbitrary-length one-shots remain the padding layer's job.
The core-test-framework block cipher test now checks the one-shots agree
with the streaming API and round-trip.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@dghgit