Skip to content

SymmetricCipherKey: dedicated key types per algorithm - #181

Open
ounsworth wants to merge 1 commit into
bcgit:release/0.1.3alphafrom
ounsworth:cipher-dedicated-keys_v3
Open

ounsworth wants to merge 1 commit into
bcgit:release/0.1.3alphafrom
ounsworth:cipher-dedicated-keys_v3

Conversation

@ounsworth

@ounsworth ounsworth commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

(this is a refactor of #167 onto release/0.1.3alpha after #133 was merged)

The core problem being solved here is that when a symmetric cipher algorithm simply requires a key: &KeyMaterial<N>, then nothing is preventing you from, for example, using the same key for AES_128_CBC and Ascon_AEAD128.

This PR adds per-algorithm key types, such as AES_128_CBC_Key and Ascon_AEAD128_Key so that cross-algorithm key re-use can be detected and blocked at compile-time.


Claude-generated PR description:

Each symmetric cipher instantiation carries its own key type, so a key made for one algorithm cannot be used with another: a new public trait SymmetricCipherKey<KEY_LEN>, a shallow wrapper over KeyMaterial<KEY_LEN> that carries the algorithm's key policy, and a generic parameter K: SymmetricCipherKey<KEY_LEN> threaded through the cipher traits and every mode in bouncycastle-cipher. Each alias gets its key type (AES_CBC_128_Key, AES_GCM_256_Key, Ascon_AEAD128_Key, ...).

This is PR #167 (02b852b) re-applied to release/0.1.3alpha as one commit. #167 was written against feature/simple-ciphers at 20b9051; release has since taken the rest of that branch (PR #133), so the change is ported onto it:

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

…elease/0.1.3alpha)

Each symmetric cipher instantiation carries its own key type, so a key made for one algorithm
cannot be used with another: a new public trait SymmetricCipherKey<KEY_LEN>, a shallow wrapper
over KeyMaterial<KEY_LEN> that carries the algorithm's key policy, and a generic parameter
K: SymmetricCipherKey<KEY_LEN> threaded through the cipher traits and every mode in
bouncycastle-cipher. Each alias gets its key type (AES_CBC_128_Key, AES_GCM_256_Key,
Ascon_AEAD128_Key, ...).

This is PR bcgit#167 (02b852b) re-applied to release/0.1.3alpha as one commit. bcgit#167 was written
against feature/simple-ciphers at 20b9051; release has since taken the rest of that branch
(PR bcgit#133), so the change is ported onto it:

- Ascon: the key type goes onto release's Ascon_AEAD128<Dir> struct; bcgit#167's changes to the
  removed AsconAead128 wrappers and inherent API are dropped. Resume no longer re-checks the
  key, since a typed key has passed the policy when it was made. Suspended-state layout is
  unchanged.
- API added on release after 20b9051 now takes &K as well: the AEADCipherEncryptor *_nonce
  methods, CCM's one-shot and trait-pair impls, GCM's do_encrypt_init_nonce.
- The toy block cipher lives in crypto/cipher/tests/common on release; ToyCipherKey is ported
  there, and the tests/modes toys and the ctr_key_stream unit tests share one inclusion of it.
  core-test-framework drops the bouncycastle-rng dependency bcgit#167 added for the toy.
- Tests and benches that release reorganised get bcgit#167's change in their new form:
  padded_alias_tests.rs and alias_tests.rs (for cbc_/ecb_alias_tests.rs), the suspend-test
  macro, the de-duplicated aes_modes_benches, alg_name_tests, the mem-usage benches.
- bcgit#167's ECB-based rewrites of fips197_tests, sp800_38a_ecb_tests and ecb_bc-test-data use
  release's modes::hazmat::ecb::Ecb path, ModeNames bound and do_*_blocks_inplace.
- aes_benches.rs is deleted as in bcgit#167; the aes_modes_benches docs no longer refer to it.

Assisted-by: Claude:claude-fable-5-1
Assisted-by: Claude:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ounsworth
ounsworth marked this pull request as ready for review October 25, 2026 17:51
@ounsworth ounsworth added this to the 0.1.3alpha milestone Oct 25, 2026

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant