Check-in [2922a56dc5]
Overview
Comment:Added test suite for encrypt and decrypt commands
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | crypto
Files: files | file ages | folders
SHA3-256: 2922a56dc5a61ce06aea9effdbb1d6d71087c4834d756ebcea7c1ea56c1e164f
User & Date: bohagan on 2023-12-03 04:52:47
Other Links: branch diff | manifest | tags
Context
2023-12-03
05:44
Updated documentation for encrypt and decrypt commands check-in: 193afd38ea user: bohagan tags: crypto
04:52
Added test suite for encrypt and decrypt commands check-in: 2922a56dc5 user: bohagan tags: crypto
02:24
Added function to encrypt and decrypt using I/O channel check-in: c3fb3a49db user: bohagan tags: crypto
Changes

Modified tests/digest.test from [b15fefa043] to [fa6ff50353].

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

package require tls

# Constraints
source common.tcl

# Helper functions - See common.tcl
proc digest_read_chan {cmd filename args} {
	set ch [open $filename rb]
	set bsize [fconfigure $ch -buffersize]
	set new [$cmd {*}$args -chan $ch]
	while {![eof $new]} {set md [read $new $bsize]}
	close $new
	return $md}
proc digest_write_chan {cmd filename data args} {
	set ch [open $filename wb]
	set new [$cmd {*}$args -chan $ch]
	puts -nonewline $new $data
	flush $new
	close $new
	set ch [open $filename rb]
	set md [read $ch]
	close $ch
	return $md}
proc digest_accumulate {string args} {
	set cmd [{*}$args -command dcmd]
	 $cmd update [string range $string 0 20]
	$cmd update [string range $string 21 end]
	return [$cmd finalize]}

set test_data "Example string for message digest tests.\n"
set test_file "md_data.dat"
set test_alt_file "md_alt_data.dat"
set test_key "Example key"
::tcltest::makeFile $test_data $test_file








|
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
|
<
<
<
<







10
11
12
13
14
15
16
17






18









19




20
21
22
23
24
25
26

package require tls

# Constraints
source common.tcl

# Helper functions - See common.tcl
proc digest_read_chan {cmd filename args} {;set ch [open $filename rb];set bsize [fconfigure $ch -buffersize];set new [$cmd {*}$args -chan $ch];while {![eof $new]} {set md [read $new $bsize]};close $new;return $md}






proc digest_write_chan {cmd filename data args} {;set ch [open $filename wb];set new [$cmd {*}$args -chan $ch];puts -nonewline $new $data;flush $new;close $new;set ch [open $filename rb];set md [read $ch];close $ch;return $md}









proc digest_accumulate {string args} {;set cmd [{*}$args -command dcmd]; $cmd update [string range $string 0 20];$cmd update [string range $string 21 end];return [$cmd finalize]}





set test_data "Example string for message digest tests.\n"
set test_file "md_data.dat"
set test_alt_file "md_alt_data.dat"
set test_key "Example key"
::tcltest::makeFile $test_data $test_file

Added tests/encrypt.csv version [4af8c409bf].































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# Group,Name,Constraints,Setup,Body,Cleanup,Match,Result,Output,Error Output,Return Codes
command,package require tls,,,,,,,,,
,,,,,,,,,,
command,# Constraints,,,,,,,,,
command,source common.tcl,,,,,,,,,
,,,,,,,,,,
command,# Helper functions - See common.tcl,,,,,,,,,
command,"proc read_chan {filename args} {set ch [open $filename rb];set bsize [fconfigure $ch -buffersize];set new [{*}$args -chan $ch];set dat """";while {![eof $new]} {append dat [read $new $bsize]};close $new;return $dat}",,,,,,,,,
command,proc write_chan {filename data args} {set ch [open $filename wb];set new [{*}$args -chan $ch];puts -nonewline $new $data;flush $new;close $new;set ch [open $filename rb];set dat [read $ch];close $ch;return $dat},,,,,,,,,
command,"proc accumulate {string args} {set cmd [{*}$args -command dcmd];set ::dat """";append ::dat [$cmd update [string range $string 0 20]];append ::dat [$cmd update [string range $string 21 end]];append ::dat [$cmd finalize]}",$cmd update [string range $string 0 20];$cmd update [string range $string 21 end];return [$cmd finalize]},,,,,,,,
command,proc get_file_hex {filename} {set ch [open $filename rb];set data [read $ch];close $ch;return [binary encode hex $data]},,,,,,,,,
command,proc get_file_text {filename} {set ch [open $filename r];set data [read $ch];close $ch;return $data},,,,,,,,,
,,,,,,,,,,
command,"set test_data ""Example string for message digest tests.\n""",,,,,,,,,
command,"set unencrypted_file ""unencrypted_data.dat""",,,,,,,,,
command,"set encrypted_file ""encrypted_data.dat""",,,,,,,,,
command,"set alt_file ""result_data.dat""",,,,,,,,,
command,"set test_key ""Example key""",,,,,,,,,
command,"set test_iv ""Example iv""",,,,,,,,,
command,::tcltest::makeFile $test_data $unencrypted_file,,,,,,,,,
 ,,,,,,,,,,
command,# Test encrypt data,,,,,,,,,
command,set cipher aes-128-cbc,,,,,,,,,
command,"set hex_string ""3cea034398de64507abbc7bcf6acba55c7011100c9015c22b3c9c331d18479fed5e542ce02a3b89a0f750daf8e2a494e""",,,,,,,,,
Encrypt Decrypt Data,Encrypt aes-138-cbc,,,binary encode hex [set data [tls::encrypt -cipher $cipher -key $test_key -iv $test_iv -data $test_data]],,,$hex_string,,,
Encrypt Decrypt Data,Decrypt aes-128-cbc,,,tls::decrypt -cipher $cipher -key $test_key -iv $test_iv -data $data,,,$test_data,,,
 ,,,,,,,,,,
command,# Test encrypt file,,,,,,,,,
Encrypt Decrypt File,Encrypt aes-138-cbc,,,tls::encrypt -cipher $cipher -key $test_key -iv $test_iv -infile $unencrypted_file -outfile $encrypted_file;get_file_hex $encrypted_file,,,$hex_string,,,
Encrypt Decrypt File,Decrypt aes-128-cbc,,,tls::decrypt -cipher $cipher -key $test_key -iv $test_iv -infile $encrypted_file -outfile $alt_file;get_file_text $alt_file,,,$test_data,,,
 ,,,,,,,,,,
command,# Test encrypt using object command,,,,,,,,,
Encrypt Decrypt Command,Encrypt aes-138-cbc,,,accumulate $test_data tls::encrypt -cipher $cipher -key $test_key -iv $test_iv;binary encode hex $::dat,,,$hex_string,,,
Encrypt Decrypt Command,Decrypt aes-128-cbc,,,accumulate $::dat tls::decrypt -cipher $cipher -key $test_key -iv $test_iv;set ::dat,,,$test_data,,,
 ,,,,,,,,,,
command,# Test encrypt using read channel,,,,,,,,,
Encrypt Decrypt Channel Read,Encrypt aes-138-cbc,,,binary encode hex [read_chan $unencrypted_file tls::encrypt -cipher $cipher -key $test_key -iv $test_iv],,,$hex_string,,,
Encrypt Decrypt Channel Read,Decrypt aes-138-cbc,,,read_chan $encrypted_file tls::decrypt -cipher $cipher -key $test_key -iv $test_iv,,,$test_data,,,
 ,,,,,,,,,,
command,# Test encrypt using write channel,,,,,,,,,
Encrypt Decrypt Channel Write,Encrypt aes-138-cbc,,,binary encode hex [set data [write_chan $encrypted_file $test_data tls::encrypt -cipher $cipher -key $test_key -iv $test_iv]],,,$hex_string,,,
Encrypt Decrypt Channel Write,Decrypt aes-138-cbc,,,write_chan $alt_file $data tls::decrypt -cipher $cipher -key $test_key -iv $test_iv,,,$test_data,,,
,,,,,,,,,,
command,# Cleanup,,,,,,,,,
command,::tcltest::removeFile $unencrypted_file,,,,,,,,,
command,::tcltest::removeFile $encrypted_file,,,,,,,,,
command,::tcltest::removeFile $alt_file,,,,,,,,,

Added tests/encrypt.test version [ce8002626b].







































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# Auto generated test cases for encrypt.csv

# Load Tcl Test package
if {[lsearch [namespace children] ::tcltest] == -1} {
	package require tcltest
	namespace import ::tcltest::*
}

set auto_path [concat [list [file dirname [file dirname [info script]]]] $auto_path]

package require tls

# Constraints
source common.tcl

# Helper functions - See common.tcl
proc read_chan {filename args} {set ch [open $filename rb];set bsize [fconfigure $ch -buffersize];set new [{*}$args -chan $ch];set dat "";while {![eof $new]} {append dat [read $new $bsize]};close $new;return $dat}
proc write_chan {filename data args} {set ch [open $filename wb];set new [{*}$args -chan $ch];puts -nonewline $new $data;flush $new;close $new;set ch [open $filename rb];set dat [read $ch];close $ch;return $dat}
proc accumulate {string args} {set cmd [{*}$args -command dcmd];set ::dat "";append ::dat [$cmd update [string range $string 0 20]];append ::dat [$cmd update [string range $string 21 end]];append ::dat [$cmd finalize]}
proc get_file_hex {filename} {set ch [open $filename rb];set data [read $ch];close $ch;return [binary encode hex $data]}
proc get_file_text {filename} {set ch [open $filename r];set data [read $ch];close $ch;return $data}

set test_data "Example string for message digest tests.\n"
set unencrypted_file "unencrypted_data.dat"
set encrypted_file "encrypted_data.dat"
set alt_file "result_data.dat"
set test_key "Example key"
set test_iv "Example iv"
::tcltest::makeFile $test_data $unencrypted_file

# Test encrypt data
set cipher aes-128-cbc
set hex_string "3cea034398de64507abbc7bcf6acba55c7011100c9015c22b3c9c331d18479fed5e542ce02a3b89a0f750daf8e2a494e"


test Encrypt_Decrypt_Data-1.1 {Encrypt aes-138-cbc} -body {
	binary encode hex [set data [tls::encrypt -cipher $cipher -key $test_key -iv $test_iv -data $test_data]]
    } -result $hex_string

test Encrypt_Decrypt_Data-1.2 {Decrypt aes-128-cbc} -body {
	tls::decrypt -cipher $cipher -key $test_key -iv $test_iv -data $data
    } -result $test_data

# Test encrypt file


test Encrypt_Decrypt_File-2.1 {Encrypt aes-138-cbc} -body {
	tls::encrypt -cipher $cipher -key $test_key -iv $test_iv -infile $unencrypted_file -outfile $encrypted_file
	get_file_hex $encrypted_file
    } -result $hex_string

test Encrypt_Decrypt_File-2.2 {Decrypt aes-128-cbc} -body {
	tls::decrypt -cipher $cipher -key $test_key -iv $test_iv -infile $encrypted_file -outfile $alt_file
	get_file_text $alt_file
    } -result $test_data

# Test encrypt using object command


test Encrypt_Decrypt_Command-3.1 {Encrypt aes-138-cbc} -body {
	accumulate $test_data tls::encrypt -cipher $cipher -key $test_key -iv $test_iv
	binary encode hex $::dat
    } -result $hex_string

test Encrypt_Decrypt_Command-3.2 {Decrypt aes-128-cbc} -body {
	accumulate $::dat tls::decrypt -cipher $cipher -key $test_key -iv $test_iv
	set ::dat
    } -result $test_data

# Test encrypt using read channel


test Encrypt_Decrypt_Channel_Read-4.1 {Encrypt aes-138-cbc} -body {
	binary encode hex [read_chan $unencrypted_file tls::encrypt -cipher $cipher -key $test_key -iv $test_iv]
    } -result $hex_string

test Encrypt_Decrypt_Channel_Read-4.2 {Decrypt aes-138-cbc} -body {
	read_chan $encrypted_file tls::decrypt -cipher $cipher -key $test_key -iv $test_iv
    } -result $test_data

# Test encrypt using write channel


test Encrypt_Decrypt_Channel_Write-5.1 {Encrypt aes-138-cbc} -body {
	binary encode hex [set data [write_chan $encrypted_file $test_data tls::encrypt -cipher $cipher -key $test_key -iv $test_iv]]
    } -result $hex_string

test Encrypt_Decrypt_Channel_Write-5.2 {Decrypt aes-138-cbc} -body {
	write_chan $alt_file $data tls::decrypt -cipher $cipher -key $test_key -iv $test_iv
    } -result $test_data

# Cleanup
::tcltest::removeFile $unencrypted_file
::tcltest::removeFile $encrypted_file
::tcltest::removeFile $alt_file

# Cleanup
::tcltest::cleanupTests
return

Modified tests/info.test from [a2f4e9e8c8] to [52471437c8].

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
# Make sure path includes location of OpenSSL executable
if {[info exists ::env(OPENSSL)]} {set ::env(path) [string cat [file join $::env(OPENSSL) bin] ";" $::env(path)]}

# Constraints
source common.tcl

# Helper functions
proc lcompare {list1 list2} {set m ""
	set u ""
	foreach i $list1 {if {$i ni $list2} {lappend m $i}}
	foreach i $list2 {if {$i ni $list1} {lappend u $i}}
	return [list "missing" $m "unexpected" $u]}
proc exec_get {delim args} {return [split [exec openssl {*}$args] $delim]}
proc exec_get_ciphers {} {set list [list]
	set data [exec openssl list -cipher-algorithms]
	foreach line [split $data "\n"] {foreach {cipher null alias} [split [string trim $line]] {lappend list [string tolower $cipher]}}
	return [lsort -unique $list]}
proc exec_get_digests {} {set list [list]
	set data [exec openssl dgst -list]
	foreach line [split $data "\n"] {foreach digest $line {if {[string match "-*" $digest]} {lappend list [string trimleft $digest "-"]}}}
	return [lsort $list]}

proc exec_get_macs {} {return [list cmac hmac]}
proc list_tolower {list} {set result [list]
	foreach element $list {lappend result [string tolower $element]}
	return $result}

# Test list ciphers


test Ciphers_List-1.1 {All} -body {
	lcompare [lsort [exec_get_ciphers]] [list_tolower [lsort [::tls::ciphers]]]
    } -result {missing {rc5 rc5-cbc rc5-cfb rc5-ecb rc5-ofb} unexpected {aes-128-ccm aes-128-gcm aes-192-ccm aes-192-gcm aes-256-ccm aes-256-gcm}}







|
<
<
<
<

<
<
|
<
<
<
|
<
>

|
<
<







13
14
15
16
17
18
19
20




21


22



23

24
25
26


27
28
29
30
31
32
33
# Make sure path includes location of OpenSSL executable
if {[info exists ::env(OPENSSL)]} {set ::env(path) [string cat [file join $::env(OPENSSL) bin] ";" $::env(path)]}

# Constraints
source common.tcl

# Helper functions
proc lcompare {list1 list2} {set m "";set u "";foreach i $list1 {if {$i ni $list2} {lappend m $i}};foreach i $list2 {if {$i ni $list1} {lappend u $i}};return [list "missing" $m "unexpected" $u]}




proc exec_get {delim args} {return [split [exec openssl {*}$args] $delim]}


proc exec_get_ciphers {} {set list [list];set data [exec openssl list -cipher-algorithms];foreach line [split $data "\n"] {foreach {cipher null alias} [split [string trim $line]] {lappend list [string tolower $cipher]}};return [lsort -unique $list]}



proc exec_get_digests {} {set list [list];set data [exec openssl dgst -list];foreach line [split $data "\n"] {foreach digest $line {if {[string match "-*" $digest]} {lappend list [string trimleft $digest "-"]}}};return [lsort $list]}

proc exec_get_pks {} {set list [list];set data [exec openssl list -public-key-methods];foreach line [split $data "\n"] {if {![string match "*Type:*" $line]} {lappend list [string trim $line]}};return $list}
proc exec_get_macs {} {return [list cmac hmac]}
proc list_tolower {list} {set result [list];foreach element $list {lappend result [string tolower $element]};return $result}



# Test list ciphers


test Ciphers_List-1.1 {All} -body {
	lcompare [lsort [exec_get_ciphers]] [list_tolower [lsort [::tls::ciphers]]]
    } -result {missing {rc5 rc5-cbc rc5-cfb rc5-ecb rc5-ofb} unexpected {aes-128-ccm aes-128-gcm aes-192-ccm aes-192-gcm aes-256-ccm aes-256-gcm}}

Modified tests/make_test_files.tcl from [fcabf4c415] to [e265399d3c].

70
71
72
73
74
75
76

77



78
79
80
81
82
83
84
		if {$group ne $prev} {
		    incr test
		    set prev $group
		    puts $out ""
		}

		# Test case

		set buffer [format "\ntest %s-%d.%d {%s}" $group $test [incr cases($group)] $name]



		foreach opt [list -constraints -setup -body -cleanup -match -result -output -errorOutput -returnCodes] {
		    set cmd [string trim [set [string trimleft $opt "-"]]]
		    if {$cmd ne ""} {
			if {$opt in [list -setup -body -cleanup]} {
			    append buffer " " $opt " \{\n"
			    foreach line [split $cmd ";"] {
				append buffer \t [string trim $line] \n







>
|
>
>
>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
		if {$group ne $prev} {
		    incr test
		    set prev $group
		    puts $out ""
		}

		# Test case
		if {[string index $name 0] ne {$}} {
		    set buffer [format "\ntest %s-%d.%d {%s}" $group $test [incr cases($group)] $name]
		} else {
		    set buffer [format "\ntest %s-%d.%d %s" $group $test [incr cases($group)] $name]
		}
		foreach opt [list -constraints -setup -body -cleanup -match -result -output -errorOutput -returnCodes] {
		    set cmd [string trim [set [string trimleft $opt "-"]]]
		    if {$cmd ne ""} {
			if {$opt in [list -setup -body -cleanup]} {
			    append buffer " " $opt " \{\n"
			    foreach line [split $cmd ";"] {
				append buffer \t [string trim $line] \n