Skip to content

crypto.cipher: make Stream.xor_key_stream implementers require a mutable receiver - #21974

Merged
spytheman merged 6 commits into
vlang:masterfrom
einar-hjortdal:master
Aug 1, 2024
Merged

spytheman merged 6 commits into
vlang:masterfrom
einar-hjortdal:master

Conversation

@einar-hjortdal

Copy link
Copy Markdown
Contributor

Addresses #21973

This may impact stream ciphers that implement xor_key_stream with immutable implementers, and may therefore be breaking.
Further checks should be made so that all stream ciphers implement xor_key_stream correctly, tests are missing for this scenario in all stream cipher implementations.

I am not sure how to perform such tests, help is needed to add such tests.

@einar-hjortdal einar-hjortdal reopened this Aug 1, 2024
@einar-hjortdal
einar-hjortdal marked this pull request as ready for review August 1, 2024 08:29
@einar-hjortdal

Copy link
Copy Markdown
Contributor Author

Please review tests in particular.

@einar-hjortdal einar-hjortdal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems like errors are thrown in des_ofb_test.v, these should be fixed before merge

@einar-hjortdal

Copy link
Copy Markdown
Contributor Author

Should be ready now

@spytheman spytheman changed the title cypto.cipher: make xor_key_stream implementers mutable. crypto.cipher: make xor_key_stream implementers mutable. Aug 1, 2024
@spytheman spytheman changed the title crypto.cipher: make xor_key_stream implementers mutable. crypto.cipher: make Stream.xor_key_stream implementers mutable Aug 1, 2024
@spytheman spytheman changed the title crypto.cipher: make Stream.xor_key_stream implementers mutable crypto.cipher: make Stream.xor_key_stream implementers require a mutable receiver Aug 1, 2024
@spytheman

spytheman commented Aug 1, 2024 •

Copy link
Copy Markdown
Contributor

I am not sure how to perform such tests, help is needed to add such tests.

They way you did it is fine. Another approach is having a function, that would not be called, but only checked:

fn implements(x cipher.Ctr) cipher.Stream { return x }

... which relies that the compiler will then ensure, that cipher.Ctr can be converted to a cipher.Stream interface value.

Comment thread vlib/crypto/cipher/cfb.v
Comment on lines -66 to +70
pub fn (mut x Cfb) xor_key_stream(mut dst_ []u8, src_ []u8) {
pub fn (mut x Cfb) xor_key_stream(mut dst []u8, src []u8) {
unsafe {
mut dst := *dst_
mut src := src_
if dst.len < src.len {
mut local_dst := *dst
mut local_src := src
if local_dst.len < local_src.len {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Those are good changes, just a bit unrelated to the topic of the PR.

Comment thread vlib/crypto/rc4/rc4.v
// xor_key_stream sets dst to the result of XORing src with the key stream.
// Dst and src must overlap entirely or not at all.
pub fn (mut c Cipher) xor_key_stream(mut dst []u8, mut src []u8) {
pub fn (mut c Cipher) xor_key_stream(mut dst []u8, src []u8) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good find.

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.

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.

2 participants