File: aes.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (358 lines) | stat: -rw-r--r-- 13,194 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# aes.test - Copyright (c) 2005 Thorsten Schloermann
#
# the test-values are taken from:
#     http://csrc.nist.gov/CryptoToolkit/aes/rijndael/rijndael-vals.zip
#     where only the first 12 entries of Know Answer Test for variable key and
#     variable text are used
#     Unfortunately, only encryption is tested by this.
#
#
# Monte Carlo Tests with 4 Million cycles through the algorithm will need too much time
#
# $Id: aes.test,v 1.8 2010/07/06 19:39:00 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2

testing {
    useLocal aes.tcl aes
}

# -------------------------------------------------------------------------

# data for variable key KAT

# Sample vectors from FIPS 197 specification document.
#
test aes-fips-C.1e {Test vector for AES-128 from FIPS-197 Appendix C.1} -setup {
    set txt [binary format H* 00112233445566778899aabbccddeeff]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 69c4e0d86a7b0430d8cdb78070b4c55a

test aes-fips-C.1d {Test vector for AES-128 from FIPS-197 Appendix C.1} -setup {
    set txt [binary format H* 69c4e0d86a7b0430d8cdb78070b4c55a]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 00112233445566778899aabbccddeeff

test aes-fips-C.2e {Test vector for AES-192 from FIPS-197 Appendix C.2} -setup {
    set txt [binary format H* 00112233445566778899aabbccddeeff]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f1011121314151617]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result dda97ca4864cdfe06eaf70a0ec0d7191

test aes-fips-C.2d {Test vector for AES-192 from FIPS-197 Appendix C.2} -setup {
    set txt [binary format H* dda97ca4864cdfe06eaf70a0ec0d7191]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f1011121314151617]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 00112233445566778899aabbccddeeff

test aes-fips-C.3e {Test vector for AES-256 from FIPS-197 Appendix C.3} -setup {
    set txt [binary format H* 00112233445566778899aabbccddeeff]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 8ea2b7ca516745bfeafc49904b496089

test aes-fips-C.3d {Test vector for AES-256 from FIPS-197 Appendix C.3} -setup {
    set txt [binary format H* 8ea2b7ca516745bfeafc49904b496089]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 00112233445566778899aabbccddeeff

test aes-kat-ecb-128e {Known answer tests - AES-128 ECB encryption} -setup {
    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 0a940bb5416ef045f1c39458c653ea5a

test aes-kat-ecb-128d {Known answer tests - AES-128 ECB decryption} -setup {
    set txt [binary format H* 0a940bb5416ef045f1c39458c653ea5a]
    set key [binary format H* 000102030405060708090a0b0c0d0e0f]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 000102030405060708090a0b0c0d0e0f

test aes-kat-ecb-192e {Known answer tests - AES-192 ECB encryption} -setup {
    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
    set key [binary format H* 000102030405060708090A0B0C0D0E0F1011121314151617]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 0060bffe46834bb8da5cf9a61ff220ae

test aes-kat-ecb-192d {Known answer tests - AES-192 ECB decryption} -setup {
    set txt [binary format H* 0060bffe46834bb8da5cf9a61ff220ae]
    set key [binary format H* 000102030405060708090A0B0C0D0E0F1011121314151617]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 000102030405060708090a0b0c0d0e0f

test aes-kat-ecb-256e {Known answer tests - AES-256 ECB encryption} -setup {
    set txt [binary format H* 000102030405060708090a0b0c0d0e0f]
    set key [binary format H* 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F]
} -body {
    set enc [aes::aes -mode ecb -dir enc -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 5a6e045708fb7196f02e553d02c3a692

test aes-kat-ecb-256d {Known answer tests - AES-256 ECB decryption} -setup {
    set txt [binary format H* 5a6e045708fb7196f02e553d02c3a692]
    set key [binary format H* 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F]
} -body {
    set enc [aes::aes -mode ecb -dir dec -key $key $txt]
    binary scan $enc H* r
    set r
} -cleanup {
    unset txt key enc r
} -result 000102030405060708090a0b0c0d0e0f


# N key ic plain cipher
set vectors {
    1 06a9214036b8a15b512e03d534120006 3dafba429d9eb430b422da802c9fac41
      53696e676c6520626c6f636b206d7367 e353779c1079aeb82708942dbe77181a
    2 c286696d887c9aa0611bbb3e2025a45a 562e17996d093d28ddb3ba695a2e6f58
      000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
      d296cd94c2cccf8a3a863028b5e1dc0a7586602d253cfff91b8266bea6d61ab1
    3 6c3ea0477630ce21a2ce334aa746c2cd c782dc4c098c66cbd9cd27d825682c81
      5468697320697320612034382d62797465206d657373616765202865786163746c7920332041455320626c6f636b7329
      d0a02b3836451753d493665d33f0e8862dea54cdb293abc7506939276772f8d5021c19216bad525c8579695d83ba2684    
    4 56e47a38c5598974bc46903dba290349 8ce82eefbea0da3c44699ed7db51b7d9
      a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf
      c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55
}

foreach {n key iv pt ct} $vectors {
    test aes-cbc-${n}e {RFC3602 AES-128 CBC mode encryption} -setup {
        set K [binary format H* $key]
        set I [binary format H* $iv]
    } -body {
        set aes [aes::aes -mode cbc -dir enc -key $K -iv $I [binary format H* $pt]]
        binary scan $aes H* r
        set r
    } -cleanup {
        unset r K I aes
    } -result $ct
    
    test aes-cbc-${n}d {RFC3602 AES-128 CBC mode decryption} -setup {
        set K [binary format H* $key]
        set I [binary format H* $iv]
    } -body {
        set aes [aes::aes -mode cbc -dir dec -key $K -iv $I [binary format H* $ct]]
        binary scan $aes H* r
        set r
    } -cleanup {
        unset r K I aes
    } -result $pt
}

# Known answer tests (CBC)
#    0 00000000000000000000000000000000 00000000000000000000000000000000 
#      00000000000000000000000000000000 8a05fc5e095af4848a08d328d3688e3d
#    1 8a05fc5e095af4848a08d328d3688e3d 8a05fc5e095af4848a08d328d3688e3d
#      204f17e2444381f6114ff53934c0bcd3 192d9b3aa10bb2f7846ccba0085c657a
#    2 93286764a85146730e641888db34eb47 192d9b3aa10bb2f7846ccba0085c657a
#      983bf6f5a6dfbcdaa19370666e83a99a 40d8daf6d1fda0a073b3bd18b7695d2e
#    3 d3f0bd9279ace6d37dd7a5906c5db669 40d8daf6d1fda0a073b3bd18b7695d2e
#      c48cd503a21c8ad0b2483ef15f79571d 3edbe80d69a1d2248ca55fc17c4ef3c5

# Bugs

# N key ic plain cipher
set vectors {
    1
    3132333435363738393031323334353637383930313233343536373839303132
    c3f0929f353c2fc78b9c6705397f22c8
    005a0000003b00000000000000000000
    97d94ab5d6a6bf3e9a126b67b8b3bc12
}

foreach {n key iv pt ct} $vectors {
    test aes-cbc-x${n}e {RFC3602 AES-128 CBC mode encryption} -setup {
        set K [binary format H* $key]
        set I [binary format H* $iv]
    } -body {
        set aes [aes::aes -mode cbc -dir enc -key $K -iv $I [binary format H* $pt]]
        binary scan $aes H* r
        set r
    } -cleanup {
        unset r K I aes
    } -result $ct
    
    test aes-cbc-x${n}d {RFC3602 AES-128 CBC mode decryption} -setup {
        set K [binary format H* $key]
        set I [binary format H* $iv]
    } -body {
        set aes [aes::aes -mode cbc -dir dec -key $K -iv $I [binary format H* $ct]]
        binary scan $aes H* r
        set r
    } -cleanup {
        unset r K I aes
    } -result $pt
}

test aes-sf2993029 {aes decrypt, wide integer, sf bug 2993029} -body {
    aes::aes -hex -mode ecb -dir decrypt \
        -key [binary format H* FEDCBA98FEDCBA98FEDCBA98FEDCBA98] \
        [binary format H* 2a666624a86d4c29de37b520781c1069]
} -result 01000000000000003d5afbb584a29f57

# -------------------------------------------------------------------------

test aes-sf-3574004-a {aes use with data starting with a dash, auto-stop} -body {
    aes::aes -hex -mode cbc -dir encrypt -key [string repeat \\0 16] -[string repeat \\0 15]
} -result cc45117986e38ae95944f9eeaa7b700b240fdd169eacd2a20505ef4c6507c907

test aes-sf-3574004-b {aes use with data starting with a dash, double-dash} -body {
    aes::aes -hex -mode cbc -dir encrypt -key [string repeat \\0 16] -- -[string repeat \\0 15]
} -result cc45117986e38ae95944f9eeaa7b700b240fdd169eacd2a20505ef4c6507c907

# -------------------------------------------------------------------------
## TODO: Go through the various possible options and combinations.

test aes-sf-3612645-a0 {aes use of -in option, allura 1366} -setup {
    set key     [binary format a32 0123456789012345678901234567890123456789]
    set encfile [tcltest::makeFile {} aes.encrypt]
    set decfile [tcltest::makeFile {} aes.decrypt]
    set outchan [open $encfile w]
    fconfigure $outchan -translation binary
    aes::aes -key $key -out $outchan "Hello World Tcl"
    close $outchan
    unset outchan
} -body {
    set inchan  [open $encfile r]
    fconfigure $inchan -translation binary
    set outchan [open $decfile w+]
    aes::aes -dir decrypt -key $key -in $inchan -out $outchan
    close $inchan
    close $outchan
    viewFile $decfile
} -cleanup {
    file delete $encfile $decfile
    unset key encfile decfile inchan outchan
} -result "Hello World Tcl\000"

test aes-sf-3612645-a1 {aes use of -in option, allura 1366} -setup {
    set key     [binary format a32 0123456789012345678901234567890123456789]
    set encfile [tcltest::makeFile {} aes.encrypt]
    set outchan [open $encfile w]
    fconfigure $outchan -translation binary
    aes::aes -key $key -out $outchan "Hello World Tcl"
    close $outchan
    unset outchan
} -body {
    set inchan [open $encfile r]
    fconfigure $inchan -translation binary
    set out [aes::aes -dir decrypt -key $key -in $inchan]
    close $inchan
    set out
} -cleanup {
    file delete $encfile
    unset out key encfile inchan
} -result "Hello World Tcl\000"

test aes-sf-3612645-b0 {aes non-use of -in option, allura 1366} -setup {
    set key     [binary format a32 0123456789012345678901234567890123456789]
    set encfile [tcltest::makeFile {} aes.encrypt]
    set decfile [tcltest::makeFile {} aes.decrypt]
    set outchan [open $encfile w]
    fconfigure $outchan -translation binary
    aes::aes -key $key -out $outchan "Hello World Tcl"
    close $outchan
    unset outchan
} -body {
    set inchan  [open $encfile r]
    fconfigure $inchan -translation binary
    set outchan [open $decfile w+]
    aes::aes -dir decrypt -key $key -out $outchan [read $inchan]
    close $inchan
    close $outchan
    viewFile $decfile
} -cleanup {
    file delete $encfile $decfile
    unset key encfile decfile inchan outchan
} -result "Hello World Tcl\000"

test aes-sf-3612645-b1 {aes non-use of -in option, allura 1366} -setup {
    set key     [binary format a32 0123456789012345678901234567890123456789]
    set encfile [tcltest::makeFile {} aes.encrypt]
    set outchan [open $encfile w]
    fconfigure $outchan -translation binary
    aes::aes -key $key -out $outchan "Hello World Tcl"
    close $outchan
    unset outchan
} -body {
    set inchan [open $encfile r]
    fconfigure $inchan -translation binary
    set out [aes::aes -dir decrypt -key $key [read $inchan]]
    close $inchan
    set out
} -cleanup {
    file delete $encfile
    unset out key encfile inchan
} -result "Hello World Tcl\000"

# -------------------------------------------------------------------------

testsuiteCleanup

# Local variables:
# mode: tcl
# indent-tabs-mode: nil
# End: