v2 / vlib / crypto / cipher / ctr_test.v
33 lines · 27 sloc · 774 bytes · 0a143f61dcc7fa25b2c44b285c3cf736f14a3abc
Raw
1import crypto.aes
2import crypto.cipher
3import crypto.des
4
5struct StreamCipher {
6 cipher cipher.Stream
7}
8
9fn test_ctr_stream_cipher() ! {
10 key := '123456789012345678901234'.bytes()
11 iv := 'abcdegfh'.bytes()
12
13 block := des.new_cipher(key[..8])
14 c := cipher.new_ctr(block, iv)
15
16 s := StreamCipher{
17 cipher: c
18 }
19}
20
21fn 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}
34