Skip to content

crypto: add missing docs #23864

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 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vlib/crypto/cipher/cfb.v
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn new_cfb(b Block, iv []u8, decrypt bool) Cfb {
return x
}

// xor_key_stream xors each byte in the given slice with a byte from the key stream.
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure that each byte is xor-ed?
To me it is not obvious 🤔 .

@blackshirt, @kimshrier what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I just saw the comments in the interface:

// A Stream represents a stream cipher.
pub interface Stream {
mut:
    // xor_key_stream XORs each byte in the given slice with a byte from the
    // cipher's key stream. Dst and src must overlap entirely or not at all.
    //
    // If len(dst) < len(src), xor_key_stream should panic. It is acceptable
    // to pass a dst bigger than src, and in that case, xor_key_stream will
    // only update dst[:len(src)] and will not touch the rest of dst.
    //
    // Multiple calls to xor_key_stream behave as if the concatenation of
    // the src buffers was passed in a single run. That is, Stream
    // maintains state and does not reset at each xor_key_stream call.
    xor_key_stream(mut dst []u8, src []u8)
}

Copy link
Member

Choose a reason for hiding this comment

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

Given that method implements that Stream interface, I think it is safe to assume that the same comments should apply.

Copy link
Member

Choose a reason for hiding this comment

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

I'll merge it as it is for now.

pub fn (mut x Cfb) xor_key_stream(mut dst []u8, src []u8) {
unsafe {
mut local_dst := *dst
Expand Down
1 change: 1 addition & 0 deletions vlib/crypto/cipher/ctr.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn new_ctr(b Block, iv []u8) Ctr {
}
}

// xor_key_stream xors each byte in the given slice with a byte from the key stream.
pub fn (mut x Ctr) xor_key_stream(mut dst []u8, src []u8) {
unsafe {
mut local_dst := *dst
Expand Down
1 change: 1 addition & 0 deletions vlib/crypto/cipher/ofb.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn new_ofb(b Block, iv []u8) Ofb {
return x
}

// xor_key_stream xors each byte in the given slice with a byte from the key stream.
pub fn (mut x Ofb) xor_key_stream(mut dst []u8, src []u8) {
unsafe {
mut local_dst := *dst
Expand Down
4 changes: 4 additions & 0 deletions vlib/crypto/des/des.v
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn (mut c DesCipher) generate_subkeys(key_bytes []u8) {
}
}

// encrypt a block of data using the DES algorithm
pub fn (c &DesCipher) encrypt(mut dst []u8, src []u8) {
if src.len < block_size {
panic('crypto/des: input not full block')
Expand All @@ -69,6 +70,7 @@ pub fn (c &DesCipher) encrypt(mut dst []u8, src []u8) {
encrypt_block(c.subkeys[..], mut dst, src)
}

// decrypt a block of data using the DES algorithm
pub fn (c &DesCipher) decrypt(mut dst []u8, src []u8) {
if src.len < block_size {
panic('crypto/des: input not full block')
Expand All @@ -94,6 +96,7 @@ pub fn new_triple_des_cipher(key []u8) cipher.Block {
return c
}

// encrypt a block of data using the TripleDES algorithm
pub fn (c &TripleDesCipher) encrypt(mut dst []u8, src []u8) {
if src.len < block_size {
panic('crypto/des: input not full block')
Expand Down Expand Up @@ -130,6 +133,7 @@ pub fn (c &TripleDesCipher) encrypt(mut dst []u8, src []u8) {
binary.big_endian_put_u64(mut dst, permute_final_block(pre_output))
}

// decrypt a block of data using the TripleDES algorithm
pub fn (c &TripleDesCipher) decrypt(mut dst []u8, src []u8) {
if src.len < block_size {
panic('crypto/des: input not full block')
Expand Down
1 change: 1 addition & 0 deletions vlib/crypto/rand/rand.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct ReadError {
Error
}

// msg returns the error message.
pub fn (err ReadError) msg() string {
return 'crypto.rand.read() error reading random bytes'
}
Expand Down
Loading