Skip to content

cipher: add methods for writing keystream#1907

Merged
newpavlov merged 2 commits intomasterfrom
cipher/write_keystream
Jun 17, 2025
Merged

cipher: add methods for writing keystream#1907
newpavlov merged 2 commits intomasterfrom
cipher/write_keystream

Conversation

@newpavlov
Copy link
Member

@newpavlov newpavlov commented Jun 16, 2025

@newpavlov newpavlov requested a review from tarcieri June 16, 2025 12:52
@newpavlov newpavlov merged commit 57aea51 into master Jun 17, 2025
11 checks passed
@newpavlov newpavlov deleted the cipher/write_keystream branch June 17, 2025 15:06
Copy link

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

/// If end of the keystream will be reached with the given data length,
/// method will panic without modifying the provided `data`.
#[inline]
fn write_keystream(&mut self, buf: &mut [u8]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any strong reason to prefer &mut [u8] over &mut [MaybeUninit<u8>]? That is exactly the type that Vec::spare_capacity_mut() returns and I'd rather not rely on compiler guessing and removing extra zeroing.

In fact the method could me something like this:

fn write_keystream(&mut self, buf: &mut [MaybeUninit<u8>]) -> &mut [u8] {

Where return type is buf with bytes filled in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to minimize amount of unsafe code in our crates and usually in practice bothering with MaybeUninit<u8> is not worth the trouble, especially with code like this which is very easy for the compiler to remove unnecessary buffer zeroization.

@tarcieri tarcieri mentioned this pull request Feb 4, 2026
tarcieri added a commit that referenced this pull request Feb 4, 2026
### Added
- Traits for tweakable block ciphers (#1721)
- Methods for writing keystream (#1907)

### Changed
- Replaced `generic-array` with `hybrid-array` (#1358)
- Rename `BlockCipher*`/`BlockMode*` (#1482)
  - `BlockEncrypt` => `BlockCipherEncrypt`
  - `BlockDecrypt` => `BlockCipherDecrypt`
  - `BlockEncryptMut` => `BlockModeEncrypt`
  - `BlockDecryptMut` => `BlockModeDecrypt`
- Split `BlockBackend` traits into 4 specific traits: (#1636)
  - `BlockCipherEncBackend`
  - `BlockCipherDecBackend`
  - `BlockModeEncBackend`
  - `BlockModeDecBackend`
- Edition changed to 2024 and MSRV bumped to 1.85 (#1759)
- Use `block_buffer::ReadBuffer` in `StreamCipherCoreWrapper` (#1959)
- Re-export of `crypto-common` moved to `cipher::common` (#2237, #2260)
- `crypto-common` dependency bumped to v0.2 (#2276)
- `blobby` requirement bumped to v0.4 (#2147)
- `inout` dependency bumped to v0.2.2 (#2149)

### Fixed
- Bugs in `StreamCipherCoreWrapper` trait implementations (#1421)
- Seeking implementation in the stream cipher wrapper (#2052)

### Removed
- `std` feature (#1691)
- `BlockCipherEncrypt::encrypt_padded*` and
`BlockCipherDecrypt::decrypt_padded*` methods.
Users of the ECB mode should use the `ecb-mode` crate instead. (#2245)
- `AsyncStreamCipher` trait (#2280)
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.

chacha20: get ChaCha8 keystream blocks without applying them to a plaintext

2 participants