vxx / vlib / crypto / aes / aes_gcm_test.v
205 lines · 186 sloc · 6.06 KB · 63e43d751ce33dd9cd12ac385720ce28764486eb
Raw
1module aes
2
3import encoding.hex
4
5// Test vectors from "The Galois/Counter Mode of Operation (GCM)",
6// and NIST SP 800-38D worked examples.
7fn test_aes128_gcm_vector() {
8 key := hex.decode('feffe9928665731c6d6a8f9467308308')!
9 iv := hex.decode('cafebabefacedbaddecaf888')!
10 plaintext :=
11 hex.decode('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39')!
12 ad := hex.decode('feedfacedeadbeeffeedfacedeadbeefabaddad2')!
13 want_ct :=
14 hex.decode('42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091')!
15 want_tag := hex.decode('5bc94fbc3221a5db94fae95ae7121a47')!
16
17 mut g := new_aes_gcm(key)!
18 out := g.encrypt(plaintext, iv, ad)!
19 ct := out[..out.len - 16]
20 tag := out[out.len - 16..]
21 assert ct == want_ct
22 assert tag == want_tag
23
24 dec := g.decrypt(out, iv, ad)!
25 assert dec == plaintext
26}
27
28fn test_aes256_gcm_vector() {
29 key := hex.decode('feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308')!
30 iv := hex.decode('cafebabefacedbaddecaf888')!
31 plaintext :=
32 hex.decode('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39')!
33 ad := hex.decode('feedfacedeadbeeffeedfacedeadbeefabaddad2')!
34 want_ct :=
35 hex.decode('522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662')!
36 want_tag := hex.decode('76fc6ece0f4e1768cddf8853bb2d551b')!
37
38 mut g := new_aes_gcm(key)!
39 out := g.encrypt(plaintext, iv, ad)!
40 assert out[..out.len - 16] == want_ct
41 assert out[out.len - 16..] == want_tag
42 assert g.decrypt(out, iv, ad)! == plaintext
43}
44
45fn test_gcm_tamper_detected() {
46 key := hex.decode('00000000000000000000000000000000')!
47 iv := hex.decode('000000000000000000000000')!
48 mut g := new_aes_gcm(key)!
49 out := g.encrypt('hello'.bytes(), iv, []u8{})!
50 mut bad := out.clone()
51 bad[0] ^= 0xff
52 if _ := g.decrypt(bad, iv, []u8{}) {
53 assert false, 'tampered ciphertext should fail authentication'
54 }
55}
56
57// The test materials taken and adapted from CAVP Testing: Block Cipher Modes.
58// Its only take the samples, not all tests was performed, some of them was not supported.
59// See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/gcmtestvectors.zip
60fn test_avcp_1() ! {
61 // [Keylen = 128]
62 // [IVlen = 96]
63 // [PTlen = 0]
64 // [AADlen = 0]
65 // [Taglen = 128]
66
67 // Count = 0
68 key := hex.decode('11754cd72aec309bf52f7687212e8957')!
69 iv := hex.decode('3c819d9a9bed087615030b65')!
70 pt := hex.decode('')!
71 aad := hex.decode('')!
72 ct := hex.decode('')!
73 tag := hex.decode('250327c674aaf477aef2675748cf6971')!
74
75 mut g := new_aes_gcm(key)!
76 out := g.encrypt(pt, iv, aad)!
77
78 // ciphertext only contains the tag
79 assert out == tag
80}
81
82fn test_avcp_2() ! {
83 // [Keylen = 128]
84 // [IVlen = 96]
85 // [PTlen = 128]
86 // [AADlen = 128]
87 // [Taglen = 120] // 15 bytes
88 //
89 // Count = 0
90 key := hex.decode('89c54b0d3bc3c397d5039058c220685f')!
91 iv := hex.decode('bc7f45c00868758d62d4bb4d')!
92 pt := hex.decode('582670b0baf5540a3775b6615605bd05')!
93 aad := hex.decode('48d16cda0337105a50e2ed76fd18e114')!
94 ct := hex.decode('fc2d4c4eee2209ddbba6663c02765e69')!
95 tag := hex.decode('55e783b00156f5da0446e2970b877f')!
96
97 mut g := new_aes_gcm(key)!
98 out := g.encrypt(pt, iv, aad)!
99 assert out[0..pt.len] == ct
100 // the tag len was truncated to 15-bytes
101 assert out[pt.len..pt.len + 15].hex() == tag.hex()
102
103 mut g2 := new_aes_gcm(key)!
104 ptx := g2.decrypt(out, iv, aad)!
105 assert ptx == pt
106}
107
108struct ItemTest {
109 count int
110 key string
111 iv string
112 pt string
113 aad string
114 ct string
115 tag string
116}
117
118fn test_avcp_3() ! {
119 // [Keylen = 128]
120 // [IVlen = 96]
121 // [PTlen = 128]
122 // [AADlen = 160]
123 // [Taglen = 128]
124 tag_len := 16
125
126 for t in tests_data {
127 key := hex.decode(t.key)!
128 iv := hex.decode(t.iv)!
129 pt := hex.decode(t.pt)!
130 aad := hex.decode(t.aad)!
131 ct := hex.decode(t.ct)!
132 tag := hex.decode(t.tag)!
133
134 mut g := new_aes_gcm(key)!
135 out := g.encrypt(pt, iv, aad)!
136 assert out[0..pt.len] == ct
137 assert out[pt.len..pt.len + tag_len] == tag
138 }
139}
140
141const tests_data = [
142 ItemTest{
143 count: 0
144 key: 'd4a22488f8dd1d5c6c19a7d6ca17964c'
145 iv: 'f3d5837f22ac1a0425e0d1d5'
146 pt: '7b43016a16896497fb457be6d2a54122'
147 aad: 'f1c5d424b83f96c6ad8cb28ca0d20e475e023b5a'
148 ct: 'c2bd67eef5e95cac27e3b06e3031d0a8'
149 tag: 'f23eacf9d1cdf8737726c58648826e9c'
150 },
151 ItemTest{
152 count: 1
153 key: 'e8899345e4d89b76f7695ddf2a24bb3c'
154 iv: '9dfaeb5d73372ceb06ca7bbe'
155 pt: 'c2807e403e9babf645268c92bc9d1de6'
156 aad: 'fed0b45a9a7b07c6da5474907f5890e317e74a42'
157 ct: '8e44bf07454255aa9e36eb34cdfd0036'
158 tag: '2f501e5249aa595a53e1985e90346a22'
159 },
160 ItemTest{
161 count: 2
162 key: 'c1629d6320b9da80a23c81be53f0ef57'
163 iv: 'b8615f6ffa30668947556cd8'
164 pt: '65771ab52532c9cdfcb3a9eb7b8193df'
165 aad: '5f2955e4301852a70684f978f89e7a61531f0861'
166 ct: 'c2a72d693181c819f69b42b52088d3a2'
167 tag: 'cadaee305d8bb6d70259a6503280d99a'
168 },
169 ItemTest{
170 count: 3
171 key: '196ed78281bb7543d60e68cca2aaa941'
172 iv: '6e7d2c8f135715532a075c50'
173 pt: '15b42e7ea21a8ad5dcd7a9bba0253d44'
174 aad: 'd6fc98c632d2e2641041ff7384d92a8358ae9abe'
175 ct: '06e5cc81c2d022cb2b5de5a881c62d09'
176 tag: '28e8cad3346ce583d5eebaa796e50974'
177 },
178 ItemTest{
179 count: 4
180 key: '55fe8a1bdc6806ed2f4a84891db943a0'
181 iv: 'af4d0ba0a90f1e713d71ae94'
182 pt: '81315972f0b1aeaa005363e9eca09d7a'
183 aad: '677cd4e6c0a67913085dba4cc2a778b894e174ad'
184 ct: 'c47bcb27c5a8d9beb19fee38b90861b7'
185 tag: 'e061ee4868edf2d969e875b8685ca8a9'
186 },
187 ItemTest{
188 count: 5
189 key: '6d86a855508657f804091be2290a17e0'
190 iv: '65dce18a4461afd83f1480f5'
191 pt: '0423bd1c8aea943637c7c3b0ca61d54b'
192 aad: 'e0ef8f0e1f442a2c090568d2af336ec59f57c896'
193 ct: '53505d449369c9bcd8a138740ea6602e'
194 tag: '86f928b4532825af9cac3820234afe73'
195 },
196 ItemTest{
197 count: 6
198 key: '66bd7b5dfd0aaaed8bb8890eee9b9c9a'
199 iv: '6e92bf7e8fd0fb932451fdf2'
200 pt: '8005865c8794b79612447f5ef33397d0'
201 aad: '60459c681bda631ece1aacca4a7b1b369c56d2bb'
202 ct: '83b99253de05625aa8e68490bb368bb9'
203 tag: '65d444b02a23e854a85423217562d07f'
204 },
205]
206