Skip to content

Commit 0a143f6

Browse files
authored
crypto.cipher: fix xor_key_stream() for CTR mode, add test vector created with OpenSSL (#25866)
1 parent 219ac05 commit 0a143f6

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

vlib/crypto/cipher/ctr.v

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,17 @@ pub fn (mut x Ctr) xor_key_stream(mut dst []u8, src []u8) {
6666
if x.out_used == x.out.len {
6767
x.b.encrypt(mut x.out, x.next)
6868
x.out_used = 0
69+
// increment counter
70+
for i := x.next.len - 1; i >= 0; i-- {
71+
x.next[i]++
72+
if x.next[i] != 0 {
73+
break
74+
}
75+
}
6976
}
7077

7178
n := xor_bytes(mut local_dst, local_src, x.out[x.out_used..])
7279

73-
// increment counter
74-
for i := x.next.len - 1; i >= 0; i-- {
75-
x.next[i]++
76-
if x.next[i] != 0 {
77-
break
78-
}
79-
}
80-
8180
local_dst = local_dst[n..]
8281
local_src = local_src[n..]
8382
x.out_used += n

vlib/crypto/cipher/ctr_test.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import crypto.aes
12
import crypto.cipher
23
import crypto.des
34

@@ -16,3 +17,17 @@ fn test_ctr_stream_cipher() ! {
1617
cipher: c
1718
}
1819
}
20+
21+
fn test_ctr_byte_by_byte() {
22+
key := []u8{len: 16, init: index}
23+
iv := []u8{len: 16, init: index}
24+
txt := []u8{len: 32, init: index}
25+
mut out := []u8{len: 32}
26+
27+
mut ofb := cipher.new_ctr(aes.new_cipher(key), iv)
28+
for i in 0 .. 32 {
29+
ofb.xor_key_stream(mut out[i..i + 1], txt[i..i + 1])
30+
}
31+
assert out == [u8(10), 149, 9, 182, 69, 107, 246, 66, 249, 202, 158, 83, 202, 94, 228, 85,
32+
18, 114, 254, 135, 114, 13, 100, 129, 130, 195, 231, 20, 87, 185, 17, 195]
33+
}

0 commit comments

Comments
 (0)