Skip to content

cipher: add methods for writing keystream #1907

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

Merged
merged 2 commits into from
Jun 17, 2025
Merged

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.

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