/[webpac]/openisis/0.9.9e/tcl/cryptest
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /openisis/0.9.9e/tcl/cryptest

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Dec 27 21:49:01 2004 UTC (18 years, 1 month ago) by dpavlin
File size: 6717 byte(s)
import of new openisis release, 0.9.9e

1 #!../bin/maletesh
2 #
3 # test malete crypto functions
4 #
5 namespace eval malete {
6
7 # SHA1 test vectors from http://www.itl.nist.gov/fipspubs/fip180-1.htm
8 foreach {message digest} [list \
9 abc A9993E364706816ABA3E25717850C26C9CD0D89D \
10 abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq \
11 84983E441C3BD26EBAAE4AA1F95129E5E54670F1 \
12 [string repeat a 1000000] 34AA973CD4C4DAA4F61EEB2BDBAD27316534016F \
13 ] {
14 set sha1 [string toupper [sha1 $message]]
15 if {60 < [string length $message]} {
16 set message [string ran $message 0 59]...
17 }
18 if {[string comp $sha1 $digest]} {
19 puts stderr "SHA1 $message -> $sha1 != $digest FAILED"
20 } {
21 puts "SHA1 $message -> $sha1 OK"
22 }
23 }
24
25 # sha1 with state
26 foreach len {62 63 64 65 100 127 128 129 1000 1000000} {
27 set digest [sha1 [string repeat a $len]]
28 set decr [expr {8192 < $len ? 8192 : 128 < $len ? 128 : 64}]
29 set message [string repeat a $decr]
30 set rem $len
31 for {set state ""} {40 != [string len $state]} {incr rem -$decr} {
32 if {$decr > $rem} {set message [string repeat a $rem]}
33 set state [sha1 $message $state]
34 # puts "state is $state"
35 }
36 if {[string comp $state $digest]} {
37 puts stderr "SHA1 state $len -> $state != $digest FAILED"
38 } {
39 puts "SHA1 state $len -> $state OK"
40 }
41 }
42
43 puts ""
44
45 # for the blowfish test, we need a string containing weird bytes as key
46 # so we need the bytearray workaround
47 proc hexdump bin {
48 binary scan [bytearray froms $bin] H* hex
49 string toupper $hex
50 }
51 proc hexstring hex {
52 bytearray tos [binary format H* $hex]
53 }
54 # Eric Young's Blowfish test vectors from http://schneier.com/code/vectors.txt
55 foreach {key clear cipher} {
56 0000000000000000 0000000000000000 4EF997456198DD78
57 FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 51866FD5B85ECB8A
58 3000000000000000 1000000000000001 7D856F9A613063F2
59 1111111111111111 1111111111111111 2466DD878B963C9D
60 0123456789ABCDEF 1111111111111111 61F9C3802281B096
61 1111111111111111 0123456789ABCDEF 7D0CC630AFDA1EC7
62 0000000000000000 0000000000000000 4EF997456198DD78
63 FEDCBA9876543210 0123456789ABCDEF 0ACEAB0FC6A0A28D
64 7CA110454A1A6E57 01A1D6D039776742 59C68245EB05282B
65 0131D9619DC1376E 5CD54CA83DEF57DA B1B8CC0B250F09A0
66 07A1133E4A0B2686 0248D43806F67172 1730E5778BEA1DA4
67 3849674C2602319E 51454B582DDF440A A25E7856CF2651EB
68 04B915BA43FEB5B6 42FD443059577FA2 353882B109CE8F1A
69 0113B970FD34F2CE 059B5E0851CF143A 48F4D0884C379918
70 0170F175468FB5E6 0756D8E0774761D2 432193B78951FC98
71 43297FAD38E373FE 762514B829BF486A 13F04154D69D1AE5
72 07A7137045DA2A16 3BDD119049372802 2EEDDA93FFD39C79
73 04689104C2FD3B2F 26955F6835AF609A D887E0393C2DA6E3
74 37D06BB516CB7546 164D5E404F275232 5F99D04F5B163969
75 1F08260D1AC2465E 6B056E18759F5CCA 4A057A3B24D3977B
76 584023641ABA6176 004BD6EF09176062 452031C1E4FADA8E
77 025816164629B007 480D39006EE762F2 7555AE39F59B87BD
78 49793EBC79B3258F 437540C8698F3CFA 53C55F9CB49FC019
79 4FB05E1515AB73A7 072D43A077075292 7A8E7BFA937E89A3
80 49E95D6D4CA229BF 02FE55778117F12A CF9C5D7A4986ADB5
81 018310DC409B26D6 1D9D5C5018F728C2 D1ABB290658BC778
82 1C587F1C13924FEF 305532286D6F295A 55CB3774D13EF201
83 0101010101010101 0123456789ABCDEF FA34EC4847B268B2
84 1F1F1F1F0E0E0E0E 0123456789ABCDEF A790795108EA3CAE
85 E0FEE0FEF1FEF1FE 0123456789ABCDEF C39E072D9FAC631D
86 0000000000000000 FFFFFFFFFFFFFFFF 014933E0CDAFF6E4
87 FFFFFFFFFFFFFFFF 0000000000000000 F21E9A77B71C49BC
88 0123456789ABCDEF 0000000000000000 245946885754369A
89 FEDCBA9876543210 FFFFFFFFFFFFFFFF 6B5C5A9C5D9E0A5A
90 } {
91 set keybytes [hexstring $key]
92 set clearbytes [binary format H* $clear]
93 set bin [blowfish enc -binary $keybytes $clearbytes]
94 set enc [hexdump $bin]
95 if {[string comp $enc $cipher]} {
96 puts stderr "BF $key $clear -> $enc != $cipher FAILED"
97 } {
98 set dec [hexdump [blowfish dec -binary $keybytes $bin]]
99 if {[string comp $dec $clear]} {
100 puts stderr "BF $key $clear -> $cipher -> $dec !? FAILED"
101 } {
102 puts "BF $key $clear -> $cipher OK"
103 }
104 }
105 }
106
107 # Young's CBC test
108 set now [hexdump [blowfish enc -binary -encvec FEDCBA9876543210 \
109 [hexstring 0123456789ABCDEFF0E1D2C3B4A59687] \
110 [binary format H* 37363534333231204E6F77206973207468652074696D6520666F722000] ]]
111 if {[string comp $now \
112 6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC]} {
113 puts stderr "BF cbc $now FAILED"
114 } {
115 puts "BF cbc $now OK"
116 }
117
118 puts ""
119
120 # byteorders, parts and init vectors
121 foreach bo {littleendian bigendian} {
122 # 36 chars should be padded to 5 8byte blocks
123 set once [hexdump [blowfish enc -$bo \
124 TESTKEY 0123456789abcdefghijklmnopqrstuvwxyz]]
125 puts "BF -$bo: $once OK"
126
127 set weakkey [blowfish open -$bo TESTKEY testkey]
128 puts "TESTKEY is a [expr {$weakkey ? "weak" : "good"}] key :)"
129 set off 0; set err 0
130 foreach part {01234567 89abcdef ghijklmn opqrstuv wxyz} {
131 set got [hexdump [testkey enc $part]]
132 if {[string comp $got [string range $once $off [expr {$off+15}]]]} {
133 puts stderr "BF -$bo part $part -> $got ! FAILED"
134 incr err
135 }
136 incr off 16
137 }
138 if {$err} {
139 puts stderr "BF -$bo parts 8 FAILED"
140 } {
141 puts "BF -$bo parts 8 OK"
142 }
143
144 # reset iv
145 blowfish open -$bo TESTKEY testkey
146 set off 0; set err 0
147 foreach part {0123456789abcdef ghijklmnopqrstuv wxyz} {
148 set got [hexdump [testkey enc $part]]
149 if {[string comp $got [string range $once $off [expr {$off+31}]]]} {
150 puts stderr "BF -$bo part $part -> $got ! FAILED"
151 incr err
152 }
153 incr off 32
154 }
155 if {$err} {
156 puts stderr "BF -$bo parts 16 FAILED"
157 } {
158 puts "BF -$bo parts 16 OK"
159 }
160
161 # specifying last block as iv
162 set iv [string range $once 0 15]
163 blowfish open -encvec $iv -$bo TESTKEY testkey
164 set off 16; set err 0
165 foreach part {89abcdef ghijklmn opqrstuv wxyz} {
166 set got [hexdump [testkey enc $part]]
167 if {[string comp $got [string range $once $off [expr {$off+15}]]]} {
168 puts stderr "BF -$bo part $part -> $got ! FAILED"
169 incr err
170 }
171 incr off 16
172 }
173 if {$err} {
174 puts stderr "BF -$bo parts 8/iv FAILED"
175 } {
176 puts "BF -$bo parts 8/iv OK"
177 }
178
179 # decoding 2nd & 3rd code block using 1st as iv
180 set deco [blowfish dec -decvec [string range $once 0 15] -$bo TESTKEY \
181 [binary format H* [string range $once 16 47]]]
182 if {[string comp $deco 89abcdefghijklmn]} {
183 puts stderr "BF -$bo dec/iv '$deco' FAILED"
184 } {
185 puts stderr "BF -$bo dec/iv $deco OK"
186 }
187 }
188
189 testkey close ;# == rename testkey ""
190 if {![catch {testkey enc something}]} {
191 puts stderr "testkey refused to vanish ! FAILED"
192 } {
193 puts "DONE with testkey OK"
194 }
195
196 };# namespace malete

  ViewVC Help
Powered by ViewVC 1.1.26