File: blowfish.test

package info (click to toggle)
tcllib 1.10-dfsg-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,708 kB
  • ctags: 6,122
  • sloc: tcl: 106,354; ansic: 9,205; sh: 8,707; xml: 1,766; yacc: 753; makefile: 115; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (305 lines) | stat: -rw-r--r-- 11,453 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
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
# blowfish.test - Copyright (C) 2004 Pat Thoyts <patthoyts@users.sf.net>
#
# $Id: blowfish.test,v 1.10 2007/03/13 09:08:29 patthoyts Exp $

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

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

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal blowfish.tcl blowfish
}

# -------------------------------------------------------------------------
# Handle multiple implementation testing
#
array set preserve [array get ::blowfish::accel]

proc implementations {} {
    variable ::blowfish::accel
    foreach {a v} [array get accel] {if {$v} {lappend r $a}}
    lappend r tcl; set r
}

proc select_implementation {impl} {
    variable ::blowfish::accel
    foreach e [array names accel] { set accel($e) 0 }
    if {[string compare "tcl" $impl] != 0} {
        set accel($impl) 1
    }
}

proc reset_implementation {} {
    variable ::blowfish::accel
    array set accel [array get ::preserve]
}

# -------------------------------------------------------------------------
# Report versions
#
if {[::blowfish::LoadAccelerator trf]} {
    puts "> Trf based"
}
puts "> pure Tcl"

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

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

#test blowfish-1.0 {blowfish basic command options} {
#    list [catch {::blowfish::blowfish} msg] \
#        [string match "wrong # args*" $msg]
#   
#} {1 1}

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

# Test vectors from http://www.schneier.com/code/vectors.txt

set vectors {
    0000000000000000        0000000000000000        4EF997456198DD78
    FFFFFFFFFFFFFFFF        FFFFFFFFFFFFFFFF        51866FD5B85ECB8A
    3000000000000000        1000000000000001        7D856F9A613063F2
    1111111111111111        1111111111111111        2466DD878B963C9D
    0123456789ABCDEF        1111111111111111        61F9C3802281B096
    1111111111111111        0123456789ABCDEF        7D0CC630AFDA1EC7
    0000000000000000        0000000000000000        4EF997456198DD78
    FEDCBA9876543210        0123456789ABCDEF        0ACEAB0FC6A0A28D
    7CA110454A1A6E57        01A1D6D039776742        59C68245EB05282B
    0131D9619DC1376E        5CD54CA83DEF57DA        B1B8CC0B250F09A0
    07A1133E4A0B2686        0248D43806F67172        1730E5778BEA1DA4
    3849674C2602319E        51454B582DDF440A        A25E7856CF2651EB
    04B915BA43FEB5B6        42FD443059577FA2        353882B109CE8F1A
    0113B970FD34F2CE        059B5E0851CF143A        48F4D0884C379918
    0170F175468FB5E6        0756D8E0774761D2        432193B78951FC98
    43297FAD38E373FE        762514B829BF486A        13F04154D69D1AE5
    07A7137045DA2A16        3BDD119049372802        2EEDDA93FFD39C79
    04689104C2FD3B2F        26955F6835AF609A        D887E0393C2DA6E3
    37D06BB516CB7546        164D5E404F275232        5F99D04F5B163969
    1F08260D1AC2465E        6B056E18759F5CCA        4A057A3B24D3977B
    584023641ABA6176        004BD6EF09176062        452031C1E4FADA8E
    025816164629B007        480D39006EE762F2        7555AE39F59B87BD
    49793EBC79B3258F        437540C8698F3CFA        53C55F9CB49FC019
    4FB05E1515AB73A7        072D43A077075292        7A8E7BFA937E89A3
    49E95D6D4CA229BF        02FE55778117F12A        CF9C5D7A4986ADB5
    018310DC409B26D6        1D9D5C5018F728C2        D1ABB290658BC778
    1C587F1C13924FEF        305532286D6F295A        55CB3774D13EF201
    0101010101010101        0123456789ABCDEF        FA34EC4847B268B2
    1F1F1F1F0E0E0E0E        0123456789ABCDEF        A790795108EA3CAE
    E0FEE0FEF1FEF1FE        0123456789ABCDEF        C39E072D9FAC631D
    0000000000000000        FFFFFFFFFFFFFFFF        014933E0CDAFF6E4
    FFFFFFFFFFFFFFFF        0000000000000000        F21E9A77B71C49BC
    0123456789ABCDEF        0000000000000000        245946885754369A
    FEDCBA9876543210        FFFFFFFFFFFFFFFF        6B5C5A9C5D9E0A5A
}

set n 0
foreach impl [implementations] {
    select_implementation $impl
    foreach {key plain cipher} $vectors {
        test blowfish-$impl-2.$n "blowfish ecb test vector $n (impl $impl)" {
            list [catch {
                string toupper \
                    [::blowfish::Hex \
                         [::blowfish::blowfish -mode ecb -dir enc \
                              -key [binary format H* $key] \
                              [binary format H* $plain]]]
            } msg] $msg
        } [list 0 $cipher]
        incr n
    }
    reset_implementation
}

set vectors {
    F9AD597C49DB005E F0
    E91D21C1D961A6D6 F0E1
    E9C2B70A1BC65CF3 F0E1D2
    BE1E639408640F05 F0E1D2C3
    B39E44481BDB1E6E F0E1D2C3B4
    9457AA83B1928C0D F0E1D2C3B4A5
    8BB77032F960629D F0E1D2C3B4A596
    E87A244E2CC85E82 F0E1D2C3B4A59687
    15750E7A4F4EC577 F0E1D2C3B4A5968778
    122BA70B3AB64AE0 F0E1D2C3B4A596877869
    3A833C9AFFC537F6 F0E1D2C3B4A5968778695A
    9409DA87A90F6BF2 F0E1D2C3B4A5968778695A4B
    884F80625060B8B4 F0E1D2C3B4A5968778695A4B3C
    1F85031C19E11968 F0E1D2C3B4A5968778695A4B3C2D
    79D9373A714CA34F F0E1D2C3B4A5968778695A4B3C2D1E
    93142887EE3BE15C F0E1D2C3B4A5968778695A4B3C2D1E0F
    03429E838CE2D14B F0E1D2C3B4A5968778695A4B3C2D1E0F00
    A4299E27469FF67B F0E1D2C3B4A5968778695A4B3C2D1E0F0011
    AFD5AED1C1BC96A8 F0E1D2C3B4A5968778695A4B3C2D1E0F001122
    10851C0E3858DA9F F0E1D2C3B4A5968778695A4B3C2D1E0F00112233
    E6F51ED79B9DB21F F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344
    64A6E14AFD36B46F F0E1D2C3B4A5968778695A4B3C2D1E0F001122334455
    80C7D7D45A5479AD F0E1D2C3B4A5968778695A4B3C2D1E0F00112233445566
    05044B62FA52D080 F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677
}

set plain [binary format H* FEDCBA9876543210]
foreach impl [implementations] {
    select_implementation $impl
    set n 0
    foreach {cipher key} $vectors {
        test blowfish-$impl-4.$n "blowfish ecb test variable length key (impl $impl)" {
            list [catch {
                string toupper \
                    [::blowfish::Hex \
                         [::blowfish::blowfish \
                              -mode ecb \
                              -dir enc \
                              -key [binary format H* $key] \
                              $plain]]
            } msg] $msg
        } [list 0 $cipher]
        incr n
    }
    reset_implementation
}

set key [binary format H* 0123456789ABCDEFF0E1D2C3B4A59687]
set iv  [binary format H* FEDCBA9876543210]
set plain [binary format H* \
               37363534333231204E6F77206973207468652074696D6520666F722000000000]

foreach impl [implementations] {
    select_implementation $impl
    test blowfish-$impl-6.1 "blowfish cbc mode (impl $impl)" {
        list [catch {
            string toupper \
                [::blowfish::Hex \
                     [::blowfish::blowfish \
                          -dir enc \
                          -mode cbc \
                          -iv $iv \
                          -key $key \
                          $plain]]
        } msg] $msg
    } [list 0 \
           6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC]
    reset_implementation
}

#cfb E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3
#ofb E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA

foreach impl [implementations] {
    select_implementation $impl

    test blowfish-$impl-7.1 {test reset of initialization vector on cbc mode} {
        list [catch {
            set iv  [string repeat \x00 8]
            set pt  "01234567abcdefgh01234567"
            set tok [blowfish::Init cbc secret $iv]
            set ct1 [blowfish::Encrypt $tok $pt]
            blowfish::Reset $tok $iv
            set ct2 [blowfish::Encrypt $tok $pt]
            blowfish::Final $tok
            string equal $ct1 $ct2
        } msg] $msg
    } {0 1}
    
    test blowfish-$impl-7.2 {test reset of initialization vector on cbc mode} {
        list [catch {
            set pt  "01234567abcdefgh01234567"
            set tok [blowfish::Init cbc secret [string repeat \x00 8]]
            set ct1 [blowfish::Encrypt $tok $pt]
            blowfish::Reset $tok [string repeat \x01 8]
            set ct2 [blowfish::Encrypt $tok $pt]
            blowfish::Final $tok
            string equal $ct1 $ct2
        } msg] $msg
    } {0 0}

    test blowfish-$impl-8.0 {check hyphen in crypt data} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "ececho cleardata"
            set cipher [blowfish::blowfish -mode ecb -key $key -dir encrypt $text]
            set plain [blowfish::blowfish -mode ecb -key $key -dir decrypt $cipher]
            string compare $plain $text
        } msg] $msg
    } {0 0}

    test blowfish-$impl-8.1 {check hyphen in plaintext} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "-cecho cleardata"
            set cipher [blowfish::blowfish -mode ecb -key $key -dir encrypt $text]
            set plain [blowfish::blowfish -mode ecb -key $key -dir decrypt $cipher]
            string compare $plain $text
        } msg] $msg
    } {0 0}

    test blowfish-$impl-8.2 {check hyphen in key} {
        list [catch {
            set key "-P/AD/oGb6q/"
            set text "ececho cleardata"
            set cipher [blowfish::blowfish -mode ecb -key $key -dir encrypt $text]
            set plain [blowfish::blowfish -mode ecb -key $key -dir decrypt $cipher]
            string compare $plain $text
        } msg] $msg
    } {0 0}

    test blowfish-$impl-8.3 {check option termination} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "ececho cleardata"
            set cipher [blowfish::blowfish -mode ecb -key $key -dir encrypt -- $text]
            set plain [blowfish::blowfish -mode ecb -key $key -dir decrypt -- $cipher]
            string compare $plain $text
        } msg] $msg
    } {0 0}

    test blowfish-$impl-9.0 {check -pad option} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "01234"
            set cipher [blowfish::blowfish -mode ecb -hex -key $key \
                            -dir encrypt -pad a -- $text]
        } msg] $msg
    } {0 8927243c2d7d568c}

    test blowfish-$impl-9.1 {check -pad option} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "01234"
            set cipher [blowfish::blowfish -mode ecb -hex -key $key \
                            -dir encrypt -pad { } -- $text]
        } msg] $msg
    } {0 3fc711286b8eca79}

    test blowfish-$impl-9.2 {check -pad option} {
        list [catch {
            set key "uP/AD/oGb6q/"
            set text "01234"
            set cipher [blowfish::blowfish -mode ecb -hex -key $key \
                            -dir encrypt -pad "" -- $text]
        } msg] \
            [expr {
                   [string equal $msg \
                        {invalid block size: blocks must be 8 bytes}] \
                || [string equal $msg \
                        {can not encrypt incomplete block at end of input}]}]
    } {1 1}

    reset_implementation
}

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

testsuiteCleanup

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