Skip to content

Commit ace3eb2

Browse files
authored
x.crypto.chacha20: adds some missing bits into stream reset (#25321)
1 parent 95e1b7c commit ace3eb2

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎vlib/x/crypto/chacha20/chacha.v‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn encrypt(key []u8, nonce []u8, plaintext []u8) ![]u8 {
4040
mut stream := new_stream(key, nonce)!
4141
mut dst := []u8{len: plaintext.len}
4242
stream.keystream_full(mut dst, plaintext)
43+
unsafe { stream.reset() }
4344
return dst
4445
}
4546

@@ -49,6 +50,7 @@ pub fn decrypt(key []u8, nonce []u8, ciphertext []u8) ![]u8 {
4950
mut stream := new_stream(key, nonce)!
5051
mut dst := []u8{len: ciphertext.len}
5152
stream.keystream_full(mut dst, ciphertext)
53+
unsafe { stream.reset() }
5254
return dst
5355
}
5456

‎vlib/x/crypto/chacha20/stream.v‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import encoding.binary
99

1010
// max_64bit_counter is a 64-bit maximum internal counter of original ChaCha20 variant.
1111
const max_64bit_counter = max_u64
12-
// max_64bit_counter is a 32-bit maximum internal counter of standard IETF ChaCha20 variant.
12+
// max_32bit_counter is a 32-bit maximum internal counter of standard IETF ChaCha20 variant.
1313
const max_32bit_counter = u64(max_u32)
1414

1515
// default chacha20 quarter round number
@@ -107,11 +107,16 @@ fn new_stream(key []u8, nonce []u8) !Stream {
107107
// reset resets internal stream
108108
@[unsafe]
109109
fn (mut s Stream) reset() {
110+
s.mode = .standard
110111
s.extended = false
111112
unsafe {
112113
_ := vmemset(&s.key, 0, 32)
113114
_ := vmemset(&s.nonce, 0, 16)
114115
}
116+
s.precomp = false
117+
s.p1, s.p5, s.p9, s.p13 = u32(0), u32(0), u32(0), u32(0)
118+
s.p2, s.p6, s.p10, s.p14 = u32(0), u32(0), u32(0), u32(0)
119+
s.p3, s.p7, s.p11, s.p15 = u32(0), u32(0), u32(0), u32(0)
115120
}
116121

117122
// new_curr_state creates a new State from current stream
@@ -372,6 +377,8 @@ fn clone_state(s State) State {
372377
return sc
373378
}
374379

380+
// qround_on_state_with_quartet run qround_on_state by previously set up state values in offset
381+
// (a,b,c,d) with values from quartet (q0, q1, q2, q3)
375382
@[direct_array_access]
376383
fn qround_on_state_with_quartet(mut s State, q0 u32, q1 u32, q2 u32, q3 u32, a int, b int, c int, d int) {
377384
s[a] = q0
@@ -414,7 +421,7 @@ mut:
414421
e3 u32
415422
}
416423

417-
// chacha20 quarter round run on Quartet and stored into res
424+
// qround_on_quartet runs chacha20 quarter round run on Quartet q.
418425
fn qround_on_quartet(mut q Quartet) {
419426
// a += b; d ^= a; d <<<= 16;
420427
q.e0 += q.e1

0 commit comments

Comments
 (0)