mirrored from https://www.bouncycastle.org/repositories/bc-rust
-
Notifications
You must be signed in to change notification settings - Fork 17
Part 7: Stream cipher traits, CFB as a stream cipher, and new CFB8 and CTR modes #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dghgit
wants to merge
72
commits into
release/0.1.3alpha
Choose a base branch
from
feature/stream-cipher
base: release/0.1.3alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
31affff
sha2: partial-bit messages, compile-time IVs, CAVP SHAVS tests (PR #88)
dghgit aec1ae4
sha3: partial-byte fixes, CAVP SHA3VS tests, mem-usage bench, release…
dghgit d604906
sha2, hmac: add SHA-512/224 and SHA-512/256 (FIPS 180-4 s. 5.3.6) and…
dghgit a57d508
rng: use core::fmt in hash_drbg80090a.rs; the only part of PRs #92-#9…
dghgit fa0be5d
sm3: add bouncycastle-sm3 (GB/T 32905-2016) and HMAC-SM3 with factory…
dghgit 34d7953
Partial bytes follow ASN.1 BIT STRING order (X.690 s. 8.6.2): message…
dghgit 1140a61
release notes: SM3 partial bytes follow the ASN.1 BIT STRING order li…
dghgit 1ad97fd
sha2, sm3: document the surviving cargo-mutants equivalences at their…
dghgit fe6fd58
core: split BlockCipher into block-aligned BlockCipherEncryptor/Decry…
dghgit 58a1fed
aes-lowmemory: add bouncycastle-aes-lowmemory, a constant-time, table…
dghgit aa9454d
modes: add BlockPermutation trait, bouncycastle-modes with AES CBC, a…
dghgit f56802e
padding: add Padding trait and bouncycastle-padding (PKCS7, PaddedEnc…
dghgit 78a4021
core, modes, aes-lowmemory: in-place block cipher API with compile-ti…
dghgit c5f60fb
modes: add AES CFB128 mode with AES_CFB_* aliases, aes*-cfb CLI subco…
dghgit 2c0567e
core: ElectronicCodeBook (was BlockPermutation), slice block hooks, b…
dghgit 157b1c8
modes: add Ecb (SP 800-38A Sec 6.1) with AES_ECB_* aliases and aes*-e…
dghgit f6cb787
padding: add NoPadding (errors when asked to pad) with Padding::ALWAY…
dghgit a1c4e41
skills: add commit-range-report, a Markdown report of a commit range …
dghgit 9c65521
mldsa, mlkem: replace the const-generic turbofish with sealed MLDSAPa…
ounsworth 57dd3d0
core: replace StreamCipher with the split StreamCipherEncryptor / Str…
dghgit 97ac6e3
modes: Cfb becomes a stream cipher taking any length with no padding,…
dghgit 5c73617
release notes: CFB becomes a stream cipher with a short final segment…
dghgit e2b534d
modes: pin the single-call vs chunked equivalence of Cfb and Cfb8 aga…
dghgit f72bfe6
modes: add Ctr (SP 800-38A Sec 6.5), a stream cipher whose nonce leng…
dghgit 0404ab9
modes: cross-check Ctr against BC Java's SICBlockCipher, which shares…
dghgit 921e2b5
sha2: partial-bit messages, compile-time IVs, CAVP SHAVS tests (PR #88)
dghgit 7b4e7fc
sha3: partial-byte fixes, CAVP SHA3VS tests, mem-usage bench, release…
dghgit c34c2f9
sha2, hmac: add SHA-512/224 and SHA-512/256 (FIPS 180-4 s. 5.3.6) and…
dghgit 7df74a6
rng: use core::fmt in hash_drbg80090a.rs; the only part of PRs #92-#9…
dghgit ac896e2
sm3: add bouncycastle-sm3 (GB/T 32905-2016) and HMAC-SM3 with factory…
dghgit f34858d
Partial bytes follow ASN.1 BIT STRING order (X.690 s. 8.6.2): message…
dghgit 46e2e79
release notes: SM3 partial bytes follow the ASN.1 BIT STRING order li…
dghgit 607cfa7
sha2, sm3: document the surviving cargo-mutants equivalences at their…
dghgit 4ca1274
core: split BlockCipher into block-aligned BlockCipherEncryptor/Decry…
dghgit a9627f6
aes-lowmemory: add bouncycastle-aes-lowmemory, a constant-time, table…
dghgit f403921
modes: add BlockPermutation trait, bouncycastle-modes with AES CBC, a…
dghgit e018929
padding: add Padding trait and bouncycastle-padding (PKCS7, PaddedEnc…
dghgit 17372c9
core, modes, aes-lowmemory: in-place block cipher API with compile-ti…
dghgit d1bee58
modes: add AES CFB128 mode with AES_CFB_* aliases, aes*-cfb CLI subco…
dghgit ca53601
core: ElectronicCodeBook (was BlockPermutation), slice block hooks, b…
dghgit 891669b
modes: add Ecb (SP 800-38A Sec 6.1) with AES_ECB_* aliases and aes*-e…
dghgit 45941d1
padding: add NoPadding (errors when asked to pad) with Padding::ALWAY…
dghgit 74e0100
skills: add commit-range-report, a Markdown report of a commit range …
dghgit 37d0b3b
core: replace StreamCipher with the split StreamCipherEncryptor / Str…
dghgit c158860
modes: Cfb becomes a stream cipher taking any length with no padding,…
dghgit 8c7ec71
release notes: CFB becomes a stream cipher with a short final segment…
dghgit 9c1b9b7
modes: pin the single-call vs chunked equivalence of Cfb and Cfb8 aga…
dghgit 5cec55d
modes: add Ctr (SP 800-38A Sec 6.5), a stream cipher whose nonce leng…
dghgit 4adbebb
modes: cross-check Ctr against BC Java's SICBlockCipher, which shares…
dghgit 4bac3b3
Reverting the SKILL.md changes about producing a report since this se…
ounsworth fc2fcb7
Merge branch 'feature/stream-cipher' of git.bouncycastle.org:bc-rust …
ounsworth d66cd0b
Restructured the mem_usage_benchmarks sub-crate
ounsworth dc248f9
Reverting the SKILL.md changes about producing a report since this se…
ounsworth 7539532
Moves sha512t_h0 tests out of unit tests and into integration tests. …
ounsworth b0bd491
rename sha2/tests/cavc_tests.rs to bc-test-data.rs to match other crates
ounsworth 097c09b
Renaming / readability of some of the SHA2 internal traits.
ounsworth 55f53f7
mem_usage_benches: fence the valgrind and ms_print snippets as text, …
dghgit 0156990
sha2: the partial-byte example's three bits are message bits, not pad…
dghgit 213473c
sha2: quote the SHA-512/t IV Generation Function as FIPS 180-4 s. 5.3…
dghgit 5fc1370
CLAUDE.md: the build and test gates need --workspace, the mem_usage_b…
dghgit b11f8f6
sha2: sha512t_h0 asserts three-digit t and formats it as three digits…
dghgit 5825050
Removing summary.md files
ounsworth 0cc2799
minor tweaks to sha2
ounsworth b282942
Gave a massive hair-cut to Claude's massive release note
ounsworth 8b48d93
Rename sha3/tasts/cavp_tests.rs to bc-test-data.rs for consistency wi…
ounsworth 6cc74c2
small tweaks to sm3
ounsworth 0558f26
doc change to the mem bench scripts
ounsworth b77f8a4
sha2: the SHA-224/256 message limit is 2^61 bytes rather than the 2^6…
dghgit cb0429c
sm3: the message limit is 2^61 bytes, GB/T 32905-2016 s. 5.1 allowing…
dghgit 9c9861c
CLAUDE.md: add a scope-of-changes section, since an unrequested refac…
dghgit 21b375c
Merge release/0.1.3alpha: HMAC and HKDF become generic utility crates…
dghgit 91b3054
sm3: reinstate HMAC-SM3 under the per-crate layout the merge introduc…
dghgit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //! AES-CBC encryption and decryption, streaming stdin to stdout. | ||
| //! | ||
| //! Only the mode wiring lives here: the IV convention, key loading, stdin framing and | ||
| //! block-alignment enforcement are all in [`crate::block_mode_cmd`], shared with the `aes*-cfb` and | ||
| //! `aes*-ecb` commands. See that module for the command-line contract. | ||
| //! | ||
| //! CBC (NIST SP 800-38A Sec 6.2) provides confidentiality only. It does not detect tampering, and | ||
| //! neither the ciphertext nor the IV is authenticated -- a flipped ciphertext bit flips the same bit | ||
| //! of the *next* block's plaintext (Appendix D). Do not decrypt data you have not authenticated | ||
| //! separately. | ||
|
|
||
| use crate::block_mode_cmd::{BLOCK_LEN, BlockModeAction, decrypt_stream, encrypt_stream, load_key}; | ||
| use bouncycastle::aes_lowmemory::{Aes128, Aes192, Aes256}; | ||
| use bouncycastle::core::key_material::KeyMaterial; | ||
| use bouncycastle::core::traits::ElectronicCodeBook; | ||
| use bouncycastle::modes::{Cbc, Decrypting, Encrypting}; | ||
|
|
||
| /// Names the mode in error messages. | ||
| const MODE: &str = "CBC"; | ||
|
|
||
| pub(crate) fn aes128_cbc_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes128, 16>(action, &load_key::<16>(key, key_file, "AES-128"), output_hex); | ||
| } | ||
|
|
||
| pub(crate) fn aes192_cbc_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes192, 24>(action, &load_key::<24>(key, key_file, "AES-192"), output_hex); | ||
| } | ||
|
|
||
| pub(crate) fn aes256_cbc_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes256, 32>(action, &load_key::<32>(key, key_file, "AES-256"), output_hex); | ||
| } | ||
|
|
||
| /// Dispatches to the shared streaming loops with `Cbc` filled in as the mode. | ||
| fn run<P, const KEY_LEN: usize>( | ||
| action: &BlockModeAction, | ||
| key: &KeyMaterial<KEY_LEN>, | ||
| output_hex: bool, | ||
| ) where | ||
| P: ElectronicCodeBook<KEY_LEN, BLOCK_LEN>, | ||
| { | ||
| match action { | ||
| BlockModeAction::Encrypt => { | ||
| encrypt_stream::<Cbc<P, Encrypting, KEY_LEN, BLOCK_LEN>, KEY_LEN, BLOCK_LEN>( | ||
| key, output_hex, MODE, | ||
| ) | ||
| } | ||
| BlockModeAction::Decrypt => { | ||
| decrypt_stream::<Cbc<P, Decrypting, KEY_LEN, BLOCK_LEN>, KEY_LEN, BLOCK_LEN>( | ||
| key, output_hex, MODE, | ||
| ) | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! AES-CFB8 encryption and decryption, streaming stdin to stdout. | ||
| //! | ||
| //! Only the mode wiring lives here: the IV convention, key loading and stdin framing are in | ||
| //! [`crate::stream_mode_cmd`] (and [`crate::block_mode_cmd`] for the key loader), shared with the | ||
| //! `aes*-cfb` commands. See those modules for the command-line contract. | ||
| //! | ||
| //! # Which CFB | ||
| //! | ||
| //! These commands are **CFB8**: the segment size is one byte (`s = 8` in NIST SP 800-38A Sec 6.3). | ||
| //! That is a different, non-interoperable mode from the CFB128 of `aes*-cfb`, not a variant of it: | ||
| //! the two ciphertexts agree on their first byte and differ everywhere after it. It also costs a | ||
| //! full AES call per byte of data, sixteen times the work of `aes*-cfb`, so prefer `aes*-cfb` | ||
| //! unless a byte-granular self-synchronising stream is required or the format demands CFB8. | ||
| //! | ||
| //! # Any length | ||
| //! | ||
| //! CFB8's segment is a single byte, so these commands accept input of any length, pad nothing, and | ||
| //! emit a ciphertext exactly as long as the plaintext. | ||
| //! | ||
| //! # Warning | ||
| //! | ||
| //! CFB8 provides confidentiality only. It does not detect tampering, and neither the ciphertext nor | ||
| //! the IV is authenticated. Appendix D, Table D.2 gives "SBE in the decryption of Cj" plus random | ||
| //! errors in the next `b/s` segments: flipping a ciphertext bit flips the *same* bit of the *same* | ||
| //! plaintext byte, corrupts the following 16 bytes, and then decryption resynchronises. Do not | ||
| //! decrypt data you have not authenticated separately. | ||
|
|
||
| use crate::block_mode_cmd::{BLOCK_LEN, BlockModeAction, load_key}; | ||
| use crate::stream_mode_cmd::run_stream_mode; | ||
| use bouncycastle::aes_lowmemory::{Aes128, Aes192, Aes256}; | ||
| use bouncycastle::core::key_material::KeyMaterial; | ||
| use bouncycastle::core::traits::ElectronicCodeBook; | ||
| use bouncycastle::modes::{Cfb8, Decrypting, Encrypting}; | ||
|
|
||
| pub(crate) fn aes128_cfb8_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes128, 16>(action, &load_key::<16>(key, key_file, "AES-128"), output_hex); | ||
| } | ||
|
|
||
| pub(crate) fn aes192_cfb8_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes192, 24>(action, &load_key::<24>(key, key_file, "AES-192"), output_hex); | ||
| } | ||
|
|
||
| pub(crate) fn aes256_cfb8_cmd( | ||
| action: &BlockModeAction, | ||
| key: &Option<String>, | ||
| key_file: &Option<String>, | ||
| output_hex: bool, | ||
| ) { | ||
| run::<Aes256, 32>(action, &load_key::<32>(key, key_file, "AES-256"), output_hex); | ||
| } | ||
|
|
||
| /// Dispatches to the shared streaming loops with `Cfb8` filled in as the mode. | ||
| fn run<P, const KEY_LEN: usize>( | ||
| action: &BlockModeAction, | ||
| key: &KeyMaterial<KEY_LEN>, | ||
| output_hex: bool, | ||
| ) where | ||
| P: ElectronicCodeBook<KEY_LEN, BLOCK_LEN>, | ||
| { | ||
| run_stream_mode::< | ||
| Cfb8<P, Encrypting, KEY_LEN, BLOCK_LEN>, | ||
| Cfb8<P, Decrypting, KEY_LEN, BLOCK_LEN>, | ||
| KEY_LEN, | ||
| BLOCK_LEN, | ||
| >(action, key, output_hex) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion point: weird to have an
aes_lowmemorywithout anaes. Should we rename this, or are we planning a fast-but-big AES implementation? I know that FIPS 197 has the EqInvCipher(), but I suspect we can just tuck that into the same crate.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This crate does not exist - see PR #115 - you already asked me to rename it, and I already have.