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
|
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
|
-
-
-
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
|
# Handle hex string
if {$type eq "s" && [string length $data] > 0 && [string index $data 0] ne "\""} {
set data [format {[binary decode hex %s]} $data]
}
if {$type eq "s" && $count > 1} {
set data [format {[string repeat %s %d]} $data $count]
}
if {[string length $data] == 0} {
set data {""}
}
return $data
}
#
# Create test case and output to test file
#
proc do_test {group cipher test_num tc params fn} {
array set config [list Key "" Repeat 1 Length "" Offset 0 end end Plaintext {""} Ciphertext {""}]
array set config [list key "" repeat 1 length "" offset 0 end end plaintext {""} ciphertext {""}]
array set config $params
set end [expr {$config(Offset) + [string length $config(Plaintext)]/2 - 1}]
set end [expr {$config(offset) + [string length $config(plaintext)]/2 - 1}]
# Test info
set line [format "\ntcltest::test %s_%s-%d.%d {%s %s offset %d}" [string map [list "-" "_"] \
$group] [string map [list "-" "_"] $cipher] $test_num $tc [string totitle $fn] $cipher $config(Offset)]
$group] [string map [list "-" "_"] $cipher] $test_num $tc [string totitle $fn] $cipher $config(offset)]
# Test constraints
append line [format " \\\n\t-constraints %s" [string map [list "-" "_"] $cipher]]
# Test body
if {$fn eq "encrypt"} {
set cmd [format "tls::encrypt -cipher %s -padding 0 -key %s \\\n\t\t-data %s" $cipher \
[get_value s $config(Key)] [get_value s $config(Plaintext) $config(Repeat)]]
[get_value s $config(key)] [get_value s $config(plaintext) $config(repeat)]]
append line " \\\n\t" [format {-body {binary encode hex [string range [%s] %d %d]}} $cmd $config(Offset) $end] " \\\n\t"
append line " \\\n\t" [format {-body {binary encode hex [string range [%s] %d %d]}} $cmd $config(offset) $end] " \\\n\t"
} else {
set ecmd [format "tls::encrypt -cipher %s -padding 0 -key %s \\\n\t\t-data %s" $cipher \
[get_value s $config(Key)] [get_value s $config(Plaintext) $config(Repeat)]]
[get_value s $config(key)] [get_value s $config(plaintext) $config(repeat)]]
set cmd [format "tls::decrypt -cipher %s -padding 0 -key %s \\\n\t\t-data \[%s\]" $cipher \
[get_value s $config(Key)] $ecmd]
append line " \\\n\t" [format {-body {binary encode hex [string range [%s] %d %d]}} $cmd $config(Offset) $end] " \\\n\t"
[get_value s $config(key)] $ecmd]
append line " \\\n\t" [format {-body {binary encode hex [string range [%s] %d %d]}} $cmd $config(offset) $end] " \\\n\t"
}
# Test result
if {$fn eq "encrypt"} {
append line [format {-match exact -result %s} $config(Ciphertext)]
append line [format {-match exact -result %s} $config(ciphertext)]
} else {
append line [format {-match exact -result %s} $config(Plaintext)]
append line [format {-match exact -result %s} $config(plaintext)]
}
return $line
}
#
# Parse test vector file and create test files with test cases
#
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
-
+
|
} else {
# Append args to params
set index [string first "=" $line]
if {$index > -1} {
set key [string trim [string range $line 0 [incr index -1]]]
set value [string trim [string range $line [incr index 2] end]]
lappend params $key $value
lappend params [string tolower $key] $value
}
}
}
# Handle last test case
if {[llength $params] > 0} {
puts $out [do_test $group $cipher $test_num [incr tc] $params "encrypt"]
|