# blowfish.test - Copyright (C) 2004 Pat Thoyts # # $Id: blowfish.test,v 1.11 2007/09/17 14:19:07 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} test blowfish-$impl-9.3 {check -dir option default is encrypt} { list [catch { set key "04B915BA43FEB5B6" set text "42FD443059577FA2" set cipher [blowfish::blowfish -mode ecb -pad "" -hex\ -key [binary format H* $key]\ -- [binary format H* $text]] } msg] $msg } {0 353882b109ce8f1a} reset_implementation } # ------------------------------------------------------------------------- testsuiteCleanup # Local variables: # mode: tcl # indent-tabs-mode: nil # End: