Uh oh!
There was an error while loading. Please reload this page.
core: split BlockCipher into block-aligned BlockCipherEncryptor/Decryptor - #96
Open
dghgit wants to merge 3 commits into
Open
core: split BlockCipher into block-aligned BlockCipherEncryptor/Decryptor#96dghgit wants to merge 3 commits into
dghgit wants to merge 3 commits into
Conversation
…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>
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 freeto 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.
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
coreahead of the first mode implementations. No implementors exist yet, so this is trait-shape only plus the matching test-framework update.Changes
BlockCipherintoBlockCipherEncryptorandBlockCipherDecryptor, mirroringKEMEncapsulator/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 minimalBlockCiphersupertrait carries the sharedMAX_SECURITY_STRENGTH. The one-shotSymmetricCipheris 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 = 1is a single block.do_encrypt_init_rng(key, &mut dyn RNG)added alongsidedo_encrypt_init, matching theencaps/encaps_rngpattern, 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 onfeature/block-cipher-padding).Test framework
TestFrameworkBlockCipher::testnow takes separateE: BlockCipherEncryptor/D: BlockCipherDecryptortype parameters and exercisesN = 1andN = 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 --checkall clean.