From 473c8e8ff268c52d1a948a180b2345249bff5ae6 Mon Sep 17 00:00:00 2001 From: Mike <45243121+tankf33der@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:57:21 +0200 Subject: [PATCH] crypto.cipher: fix xor_key_stream() for OFB mode, add test (#25844) --- vlib/crypto/cipher/ofb.v | 2 +- vlib/crypto/cipher/ofb_test.v | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/vlib/crypto/cipher/ofb.v b/vlib/crypto/cipher/ofb.v index 1f95fbc14..3575ab9ec 100644 --- a/vlib/crypto/cipher/ofb.v +++ b/vlib/crypto/cipher/ofb.v @@ -56,7 +56,7 @@ pub fn (mut x Ofb) xor_key_stream(mut dst []u8, src []u8) { copy(mut x.next, x.out) - n := xor_bytes(mut local_dst, local_src, x.out) + n := xor_bytes(mut local_dst, local_src, x.out[x.out_used..]) local_dst = local_dst[n..] local_src = local_src[n..] x.out_used += n diff --git a/vlib/crypto/cipher/ofb_test.v b/vlib/crypto/cipher/ofb_test.v index 99a2af9c9..0999c7c89 100644 --- a/vlib/crypto/cipher/ofb_test.v +++ b/vlib/crypto/cipher/ofb_test.v @@ -1,11 +1,12 @@ import crypto.cipher +import crypto.aes import crypto.des struct StreamCipher { cipher cipher.Stream } -fn test_ctr_stream_cipher() ! { +fn test_ofb_stream_cipher() ! { key := '123456789012345678901234'.bytes() iv := 'abcdegfh'.bytes() @@ -16,3 +17,17 @@ fn test_ctr_stream_cipher() ! { cipher: c } } + +fn test_ofb_byte_by_byte() { + key := []u8{len: 16, init: index} + iv := []u8{len: 16, init: index} + txt := []u8{len: 32, init: index} + mut out := []u8{len: 32} + + mut ofb := cipher.new_ofb(aes.new_cipher(key), iv) + for i in 0 .. 32 { + ofb.xor_key_stream(mut out[i..i + 1], txt[i..i + 1]) + } + assert out == [u8(10), 149, 9, 182, 69, 107, 246, 66, 249, 202, 158, 83, 202, 94, 228, 85, + 190, 246, 12, 182, 85, 194, 184, 92, 243, 121, 164, 215, 69, 34, 168, 124] +} -- 2.39.5