1# aes.test - Copyright (c) 2005 Thorsten Schloermann
2#
3# the test-values are taken from:
4#     http://csrc.nist.gov/CryptoToolkit/aes/rijndael/rijndael-vals.zip
5#     where only the first 12 entries of Know Answer Test for variable key and
6#     variable text are used
7#     Unfortunately, only encryption is tested by this.
8#
9#
10# Monte Carlo Tests with 4 Million cycles through the algorithm will need too much time
11#
12# $Id: aes.test,v 1.8 2010/07/06 19:39:00 andreas_kupries Exp $
13
14# -------------------------------------------------------------------------
15
16source [file join \
17	[file dirname [file dirname [file join [pwd] [info script]]]] \
18	devtools testutilities.tcl]
19
20testsNeedTcl     8.5
21testsNeedTcltest 2
22
23testing {
24    useLocal aes.tcl aes
25}
26
27# -------------------------------------------------------------------------
28
29# data for variable key KAT
30
31# Sample vectors from FIPS 197 specification document.
32#
33test aes-fips-C.1e {Test vector for AES-128 from FIPS-197 Appendix C.1} -setup {
34    set txt [binary format H* 00112233445566778899aabbccddeeff]
35    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
36} -body {
37    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
38    binary scan $enc H* r
39    set r
40} -cleanup {
41    unset txt key enc r
42} -result 69c4e0d86a7b0430d8cdb78070b4c55a
43
44test aes-fips-C.1d {Test vector for AES-128 from FIPS-197 Appendix C.1} -setup {
45    set txt [binary format H* 69c4e0d86a7b0430d8cdb78070b4c55a]
46    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
47} -body {
48    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
49    binary scan $enc H* r
50    set r
51} -cleanup {
52    unset txt key enc r
53} -result 00112233445566778899aabbccddeeff
54
55test aes-fips-C.2e {Test vector for AES-192 from FIPS-197 Appendix C.2} -setup {
56    set txt [binary format H* 00112233445566778899aabbccddeeff]
57    set key [binary format H* 000102030405060708090a0b0c0d0e0f1011121314151617]
58} -body {
59    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
60    binary scan $enc H* r
61    set r
62} -cleanup {
63    unset txt key enc r
64} -result dda97ca4864cdfe06eaf70a0ec0d7191
65
66test aes-fips-C.2d {Test vector for AES-192 from FIPS-197 Appendix C.2} -setup {
67    set txt [binary format H* dda97ca4864cdfe06eaf70a0ec0d7191]
68    set key [binary format H* 000102030405060708090a0b0c0d0e0f1011121314151617]
69} -body {
70    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
71    binary scan $enc H* r
72    set r
73} -cleanup {
74    unset txt key enc r
75} -result 00112233445566778899aabbccddeeff
76
77test aes-fips-C.3e {Test vector for AES-256 from FIPS-197 Appendix C.3} -setup {
78    set txt [binary format H* 00112233445566778899aabbccddeeff]
79    set key [binary format H* 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]
80} -body {
81    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
82    binary scan $enc H* r
83    set r
84} -cleanup {
85    unset txt key enc r
86} -result 8ea2b7ca516745bfeafc49904b496089
87
88test aes-fips-C.3d {Test vector for AES-256 from FIPS-197 Appendix C.3} -setup {
89    set txt [binary format H* 8ea2b7ca516745bfeafc49904b496089]
90    set key [binary format H* 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]
91} -body {
92    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
93    binary scan $enc H* r
94    set r
95} -cleanup {
96    unset txt key enc r
97} -result 00112233445566778899aabbccddeeff
98
99test aes-kat-ecb-128e {Known answer tests - AES-128 ECB encryption} -setup {
100    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
101    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
102} -body {
103    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
104    binary scan $enc H* r
105    set r
106} -cleanup {
107    unset txt key enc r
108} -result 0a940bb5416ef045f1c39458c653ea5a
109
110test aes-kat-ecb-128d {Known answer tests - AES-128 ECB decryption} -setup {
111    set txt [binary format H* 0a940bb5416ef045f1c39458c653ea5a]
112    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
113} -body {
114    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
115    binary scan $enc H* r
116    set r
117} -cleanup {
118    unset txt key enc r
119} -result 000102030405060708090a0b0c0d0e0f
120
121test aes-kat-ecb-192e {Known answer tests - AES-192 ECB encryption} -setup {
122    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
123    set key [binary format H* 000102030405060708090A0B0C0D0E0F1011121314151617]
124} -body {
125    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
126    binary scan $enc H* r
127    set r
128} -cleanup {
129    unset txt key enc r
130} -result 0060bffe46834bb8da5cf9a61ff220ae
131
132test aes-kat-ecb-192d {Known answer tests - AES-192 ECB decryption} -setup {
133    set txt [binary format H* 0060bffe46834bb8da5cf9a61ff220ae]
134    set key [binary format H* 000102030405060708090A0B0C0D0E0F1011121314151617]
135} -body {
136    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
137    binary scan $enc H* r
138    set r
139} -cleanup {
140    unset txt key enc r
141} -result 000102030405060708090a0b0c0d0e0f
142
143test aes-kat-ecb-256e {Known answer tests - AES-256 ECB encryption} -setup {
144    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
145    set key [binary format H* 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F]
146} -body {
147    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
148    binary scan $enc H* r
149    set r
150} -cleanup {
151    unset txt key enc r
152} -result 5a6e045708fb7196f02e553d02c3a692
153
154test aes-kat-ecb-256d {Known answer tests - AES-256 ECB decryption} -setup {
155    set txt [binary format H* 5a6e045708fb7196f02e553d02c3a692]
156    set key [binary format H* 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F]
157} -body {
158    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
159    binary scan $enc H* r
160    set r
161} -cleanup {
162    unset txt key enc r
163} -result 000102030405060708090a0b0c0d0e0f
164
165
166# N key ic plain cipher
167set vectors {
168    1 06a9214036b8a15b512e03d534120006 3dafba429d9eb430b422da802c9fac41
169      53696e676c6520626c6f636b206d7367 e353779c1079aeb82708942dbe77181a
170    2 c286696d887c9aa0611bbb3e2025a45a 562e17996d093d28ddb3ba695a2e6f58
171      000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
172      d296cd94c2cccf8a3a863028b5e1dc0a7586602d253cfff91b8266bea6d61ab1
173    3 6c3ea0477630ce21a2ce334aa746c2cd c782dc4c098c66cbd9cd27d825682c81
174      5468697320697320612034382d62797465206d657373616765202865786163746c7920332041455320626c6f636b7329
175      d0a02b3836451753d493665d33f0e8862dea54cdb293abc7506939276772f8d5021c19216bad525c8579695d83ba2684
176    4 56e47a38c5598974bc46903dba290349 8ce82eefbea0da3c44699ed7db51b7d9
177      a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf
178      c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55
179}
180
181foreach {n key iv pt ct} $vectors {
182    test aes-cbc-${n}e {RFC3602 AES-128 CBC mode encryption} -setup {
183        set K [binary format H* $key]
184        set I [binary format H* $iv]
185    } -body {
186        set aes [aes::aes -mode cbc -dir enc -key $K -iv $I [binary format H* $pt]]
187        binary scan $aes H* r
188        set r
189    } -cleanup {
190        unset r K I aes
191    } -result $ct
192
193    test aes-cbc-${n}d {RFC3602 AES-128 CBC mode decryption} -setup {
194        set K [binary format H* $key]
195        set I [binary format H* $iv]
196    } -body {
197        set aes [aes::aes -mode cbc -dir dec -key $K -iv $I [binary format H* $ct]]
198        binary scan $aes H* r
199        set r
200    } -cleanup {
201        unset r K I aes
202    } -result $pt
203}
204
205# Known answer tests (CBC)
206#    0 00000000000000000000000000000000 00000000000000000000000000000000
207#      00000000000000000000000000000000 8a05fc5e095af4848a08d328d3688e3d
208#    1 8a05fc5e095af4848a08d328d3688e3d 8a05fc5e095af4848a08d328d3688e3d
209#      204f17e2444381f6114ff53934c0bcd3 192d9b3aa10bb2f7846ccba0085c657a
210#    2 93286764a85146730e641888db34eb47 192d9b3aa10bb2f7846ccba0085c657a
211#      983bf6f5a6dfbcdaa19370666e83a99a 40d8daf6d1fda0a073b3bd18b7695d2e
212#    3 d3f0bd9279ace6d37dd7a5906c5db669 40d8daf6d1fda0a073b3bd18b7695d2e
213#      c48cd503a21c8ad0b2483ef15f79571d 3edbe80d69a1d2248ca55fc17c4ef3c5
214
215# Bugs
216
217# N key ic plain cipher
218set vectors {
219    1
220    3132333435363738393031323334353637383930313233343536373839303132
221    c3f0929f353c2fc78b9c6705397f22c8
222    005a0000003b00000000000000000000
223    97d94ab5d6a6bf3e9a126b67b8b3bc12
224}
225
226foreach {n key iv pt ct} $vectors {
227    test aes-cbc-x${n}e {RFC3602 AES-128 CBC mode encryption} -setup {
228        set K [binary format H* $key]
229        set I [binary format H* $iv]
230    } -body {
231        set aes [aes::aes -mode cbc -dir enc -key $K -iv $I [binary format H* $pt]]
232        binary scan $aes H* r
233        set r
234    } -cleanup {
235        unset r K I aes
236    } -result $ct
237
238    test aes-cbc-x${n}d {RFC3602 AES-128 CBC mode decryption} -setup {
239        set K [binary format H* $key]
240        set I [binary format H* $iv]
241    } -body {
242        set aes [aes::aes -mode cbc -dir dec -key $K -iv $I [binary format H* $ct]]
243        binary scan $aes H* r
244        set r
245    } -cleanup {
246        unset r K I aes
247    } -result $pt
248}
249
250test aes-sf2993029 {aes decrypt, wide integer, sf bug 2993029} -body {
251    aes::aes -hex -mode ecb -dir decrypt \
252        -key [binary format H* FEDCBA98FEDCBA98FEDCBA98FEDCBA98] \
253        [binary format H* 2a666624a86d4c29de37b520781c1069]
254} -result 01000000000000003d5afbb584a29f57
255
256# -------------------------------------------------------------------------
257
258test aes-sf-3574004-a {aes use with data starting with a dash, auto-stop} -body {
259    aes::aes -hex -mode cbc -dir encrypt -key [string repeat \\0 16] -[string repeat \\0 15]
260} -result cc45117986e38ae95944f9eeaa7b700b240fdd169eacd2a20505ef4c6507c907
261
262test aes-sf-3574004-b {aes use with data starting with a dash, double-dash} -body {
263    aes::aes -hex -mode cbc -dir encrypt -key [string repeat \\0 16] -- -[string repeat \\0 15]
264} -result cc45117986e38ae95944f9eeaa7b700b240fdd169eacd2a20505ef4c6507c907
265
266# -------------------------------------------------------------------------
267## TODO: Go through the various possible options and combinations.
268
269test aes-sf-3612645-a0 {aes use of -in option, allura 1366} -setup {
270    set key     [binary format a32 0123456789012345678901234567890123456789]
271    set encfile [tcltest::makeFile {} aes.encrypt]
272    set decfile [tcltest::makeFile {} aes.decrypt]
273    set outchan [open $encfile w]
274    fconfigure $outchan -translation binary
275    aes::aes -key $key -out $outchan "Hello World Tcl"
276    close $outchan
277    unset outchan
278} -body {
279    set inchan  [open $encfile r]
280    fconfigure $inchan -translation binary
281    set outchan [open $decfile w+]
282    aes::aes -dir decrypt -key $key -in $inchan -out $outchan
283    close $inchan
284    close $outchan
285    viewFile $decfile
286} -cleanup {
287    file delete $encfile $decfile
288    unset key encfile decfile inchan outchan
289} -result "Hello World Tcl\000"
290
291test aes-sf-3612645-a1 {aes use of -in option, allura 1366} -setup {
292    set key     [binary format a32 0123456789012345678901234567890123456789]
293    set encfile [tcltest::makeFile {} aes.encrypt]
294    set outchan [open $encfile w]
295    fconfigure $outchan -translation binary
296    aes::aes -key $key -out $outchan "Hello World Tcl"
297    close $outchan
298    unset outchan
299} -body {
300    set inchan [open $encfile r]
301    fconfigure $inchan -translation binary
302    set out [aes::aes -dir decrypt -key $key -in $inchan]
303    close $inchan
304    set out
305} -cleanup {
306    file delete $encfile
307    unset out key encfile inchan
308} -result "Hello World Tcl\000"
309
310test aes-sf-3612645-b0 {aes non-use of -in option, allura 1366} -setup {
311    set key     [binary format a32 0123456789012345678901234567890123456789]
312    set encfile [tcltest::makeFile {} aes.encrypt]
313    set decfile [tcltest::makeFile {} aes.decrypt]
314    set outchan [open $encfile w]
315    fconfigure $outchan -translation binary
316    aes::aes -key $key -out $outchan "Hello World Tcl"
317    close $outchan
318    unset outchan
319} -body {
320    set inchan  [open $encfile r]
321    fconfigure $inchan -translation binary
322    set outchan [open $decfile w+]
323    aes::aes -dir decrypt -key $key -out $outchan [read $inchan]
324    close $inchan
325    close $outchan
326    viewFile $decfile
327} -cleanup {
328    file delete $encfile $decfile
329    unset key encfile decfile inchan outchan
330} -result "Hello World Tcl\000"
331
332test aes-sf-3612645-b1 {aes non-use of -in option, allura 1366} -setup {
333    set key     [binary format a32 0123456789012345678901234567890123456789]
334    set encfile [tcltest::makeFile {} aes.encrypt]
335    set outchan [open $encfile w]
336    fconfigure $outchan -translation binary
337    aes::aes -key $key -out $outchan "Hello World Tcl"
338    close $outchan
339    unset outchan
340} -body {
341    set inchan [open $encfile r]
342    fconfigure $inchan -translation binary
343    set out [aes::aes -dir decrypt -key $key [read $inchan]]
344    close $inchan
345    set out
346} -cleanup {
347    file delete $encfile
348    unset out key encfile inchan
349} -result "Hello World Tcl\000"
350
351# -------------------------------------------------------------------------
352
353testsuiteCleanup
354
355# Local variables:
356# mode: tcl
357# indent-tabs-mode: nil
358# End:
359