| 1 | import crypto.aes |
| 2 | import crypto.cipher |
| 3 | import crypto.des |
| 4 | |
| 5 | struct StreamCipher { |
| 6 | cipher cipher.Stream |
| 7 | } |
| 8 | |
| 9 | fn 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 | |
| 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 | } |
| 34 | |