Skip to content

Commit 473c8e8

Browse files
authored
crypto.cipher: fix xor_key_stream() for OFB mode, add test (#25844)
1 parent a8a5e80 commit 473c8e8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

vlib/crypto/cipher/ofb.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn (mut x Ofb) xor_key_stream(mut dst []u8, src []u8) {
5656

5757
copy(mut x.next, x.out)
5858

59-
n := xor_bytes(mut local_dst, local_src, x.out)
59+
n := xor_bytes(mut local_dst, local_src, x.out[x.out_used..])
6060
local_dst = local_dst[n..]
6161
local_src = local_src[n..]
6262
x.out_used += n

vlib/crypto/cipher/ofb_test.v

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import crypto.cipher
2+
import crypto.aes
23
import crypto.des
34

45
struct StreamCipher {
56
cipher cipher.Stream
67
}
78

8-
fn test_ctr_stream_cipher() ! {
9+
fn test_ofb_stream_cipher() ! {
910
key := '123456789012345678901234'.bytes()
1011
iv := 'abcdegfh'.bytes()
1112

@@ -16,3 +17,17 @@ fn test_ctr_stream_cipher() ! {
1617
cipher: c
1718
}
1819
}
20+
21+
fn test_ofb_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_ofb(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+
190, 246, 12, 182, 85, 194, 184, 92, 243, 121, 164, 215, 69, 34, 168, 124]
33+
}

0 commit comments

Comments
 (0)