File: aes.test

package info (click to toggle)
tcllib 1.8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,628 kB
  • ctags: 4,897
  • sloc: tcl: 88,012; sh: 7,856; ansic: 4,174; xml: 1,765; yacc: 753; perl: 84; f90: 84; makefile: 60; python: 33; ruby: 13; php: 11
file content (222 lines) | stat: -rw-r--r-- 8,860 bytes parent folder | download
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
# 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.3 2005/09/01 09:27:29 patthoyts Exp $

# -------------------------------------------------------------------------
# Initialize the test package
#
if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

# -------------------------------------------------------------------------
# Ensure we test _this_ local copy and one installed somewhere else.
#
package forget aes
catch {namespace delete ::aes}
if {[catch {source [file join [file dirname [info script]] aes.tcl]} msg]} {
    puts "skipped [file tail [info script]]: $msg"
}

# -------------------------------------------------------------------------
# Setup any constraints
#

# -------------------------------------------------------------------------
# Report version in test
#
puts "- aes [package present aes] (pure Tcl)"

# -------------------------------------------------------------------------
# Now the package specific tests....
# -------------------------------------------------------------------------

# 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} {
    list [catch {
        set txt [binary format H* 00112233445566778899aabbccddeeff]
        set key [binary format H* 000102030405060708090a0b0c0d0e0f]
        set enc [aes::aes -mode ecb -dir enc -key $key $txt]
        binary scan $enc H* r
        set r
    } msg] $msg
} {0 69c4e0d86a7b0430d8cdb78070b4c55a}

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

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

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

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

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


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

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

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

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

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

test aes-kat-ecb-256d {Known answer tests - AES-256 ECB decryption} {
    list [catch {
        set txt [binary format H* 5a6e045708fb7196f02e553d02c3a692]
        set key [binary format H* 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F]
        set enc [aes::aes -mode ecb -dir dec -key $key $txt]
        binary scan $enc H* r
        set r
    } msg] $msg
} {0 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} {
        list [catch {
            set K [binary format H* $key]
            set I [binary format H* $iv]
            set aes [aes::aes -mode cbc -dir enc -key $K -iv $I [binary format H* $pt]]
            binary scan $aes H* r
            set r
        } msg] $msg
    } [list 0 $ct]
    
    test aes-cbc-${n}d {RFC3602 AES-128 CBC mode decryption} {
        list [catch {
            set K [binary format H* $key]
            set I [binary format H* $iv]
            set aes [aes::aes -mode cbc -dir dec -key $K -iv $I [binary format H* $ct]]
            binary scan $aes H* r
            set r
        } msg] $msg
    } [list 0 $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

::tcltest::cleanupTests

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