v / vlib / crypto / aes / block_generic_test.v
48 lines · 44 sloc · 1.28 KB · 383812bfbf3dc60322a71fa7ad12c420c415b074
Raw
1module aes
2
3import encoding.hex
4
5struct BlockVector {
6 key string
7 plaintext string
8 ciphertext string
9}
10
11fn test_sbox_roundtrip() {
12 for i in 0 .. 256 {
13 b := u8(i)
14 assert inv_sub_byte(sub_byte(b)) == b
15 }
16}
17
18fn test_aes_known_answer_vectors() {
19 test_cases := [
20 BlockVector{
21 key: '000102030405060708090a0b0c0d0e0f'
22 plaintext: '00112233445566778899aabbccddeeff'
23 ciphertext: '69c4e0d86a7b0430d8cdb78070b4c55a'
24 },
25 BlockVector{
26 key: '000102030405060708090a0b0c0d0e0f1011121314151617'
27 plaintext: '00112233445566778899aabbccddeeff'
28 ciphertext: 'dda97ca4864cdfe06eaf70a0ec0d7191'
29 },
30 BlockVector{
31 key: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
32 plaintext: '00112233445566778899aabbccddeeff'
33 ciphertext: '8ea2b7ca516745bfeafc49904b496089'
34 },
35 ]
36 for tc in test_cases {
37 key := hex.decode(tc.key) or { panic(err) }
38 plaintext := hex.decode(tc.plaintext) or { panic(err) }
39 expected_ciphertext := hex.decode(tc.ciphertext) or { panic(err) }
40 block := new_cipher(key)
41 mut ciphertext := []u8{len: block_size}
42 block.encrypt(mut ciphertext, plaintext)
43 assert ciphertext == expected_ciphertext
44 mut decrypted := []u8{len: block_size}
45 block.decrypt(mut decrypted, ciphertext)
46 assert decrypted == plaintext
47 }
48}
49