Debian Codegraph @

test/kcapi-dgst-test.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 #
3 # Copyright (C) 2017 - 2022, Stephan Mueller <[email protected]>
4 #
5 # License: see LICENSE file in root directory
6 #
7 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
8 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
10 # WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
11 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
12 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
13 # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
14 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
16 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
17 # USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
18 # DAMAGE.
19 #
20
21 DIRNAME="$(dirnamescip-shell system . . dirname. "$0")"
22 . "$DIRNAME/libtest.sh"
23
24 find_platform kcapi-dgst
25 TSTPREFIX="${TMPDIR}/kcapi-dgst-testfiles."
26 KEYFILE_128="${TSTPREFIX}128key"
27 KEYFILE_256="${TSTPREFIX}256key"
28 OPENSSLKEY128=""
29 OPENSSLKEY256=""
30
31 ORIGPT="${TSTPREFIX}orig_pt"
32 GENDGST="${TSTPREFIX}generated_dgst"
33
34 SALT="0123456789abcdef0123456789abcdef"
35
36 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local().()
37 {
38 local bytes=$(statscip-shell system . . stat. -c %s $ORIGPT)
39 echo_pass "$bytes bytes: $@"
40 }
41
42 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local().()
43 {
44 local bytes=$(statscip-shell system . . stat. -c %s $ORIGPT)
45 echo_fail "$bytes bytes: $@"
46 }
47
48 init_setupscip-shell shell shell-project 0.0.0 init_setup().()
49 {
50 trap "rm -f $TSTPREFIX*; exit" 0 1 2 3 15
51
52 # CR is also character
53 # Hex key string: 3031323334353637383961626364650a
54 echoscip-shell system . . echo. -n "0123456789abcdef" > $KEYFILE_128
55 OPENSSLKEY128="0123456789abcdef"
56 # Hex key string: 303132333435363738396162636465663031323334353637383961626364650a
57 echoscip-shell system . . echo. -n "0123456789abcdef0123456789abcdef" > $KEYFILE_256
58 OPENSSLKEY256="0123456789abcdef0123456789abcdef"
59 }
60
61 gen_origscip-shell shell shell-project 0.0.0 gen_orig().()
62 {
63 local size=$1
64 touchscip-shell system . . touch. $ORIGPT
65 ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of=$ORIGPT bs=$size count=1 2>/dev/nullscip-shell filesystem . . `/dev/null`.
66 }
67
68 diff_filescip-shell shell shell-project 0.0.0 diff_file().()
69 {
70 local orighash=$(sha256sumscip-shell system . . sha256sum. $1 | cutscip-shell system . . cut. -d " " -f1)
71 local genhash=$(sha256sumscip-shell system . . sha256sum. $2 | cutscip-shell system . . cut. -d " " -f1)
72 shift
73 shift
74
75 if [scip-shell system . . `[`. x"$orighash" = x"$genhash" ]
76 then
77 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local(). "$@"
78 else
79 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local(). "$@: original file ($orighash) and generated file ($genhash)"
80 fi
81
82 }
83
84 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout().()
85 {
86 local keyfile=$1
87
88 if [scip-shell system . . `[`. ! -f "$keyfile" ]
89 then
90 echoscip-shell system . . echo. "Keyfile $file does not exist"
91 exit 1
92 fi
93
94 run_app kcapi-dgst -c "sha256" --hex < $ORIGPT > $GENDGST
95 echoscip-shell system . . echo. >> $GENDGST
96 opensslscip-shell system . . openssl. dgst -sha256 $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
97 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "STDIN / STDOUT test (hash)"
98
99 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
100 keysize=$((keysize*8))
101 eval opensslkey=\$OPENSSLKEY${keysize}
102
103 exec 10<$keyfile; run_app kcapi-dgst --keyfd 10 -c "hmac(sha256)" --hex < $ORIGPT > $GENDGST
104 echoscip-shell system . . echo. >> $GENDGST
105 opensslscip-shell system . . openssl. dgst -sha256 -hmac $opensslkey $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
106 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "STDIN / STDOUT test (keyed MD $keysize bits)"
107
108 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" < $ORIGPT > $GENDGST
109 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" < $ORIGPT > $GENDGST.2
110
111 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.2 "STDIN / STDOUT test (password)"
112 }
113
114 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout().()
115 {
116 local keyfile=$1
117
118 if [scip-shell system . . `[`. ! -f "$keyfile" ]
119 then
120 echoscip-shell system . . echo. "Keyfile $file does not exist"
121 exit 1
122 fi
123
124 run_app kcapi-dgst -c "sha256" --hex -o $GENDGST < $ORIGPT
125 echoscip-shell system . . echo. >> $GENDGST
126 opensslscip-shell system . . openssl. dgst -sha256 $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
127 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "STDIN / FILEOUT test (hash)"
128
129 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
130 keysize=$((keysize*8))
131 eval opensslkey=\$OPENSSLKEY${keysize}
132
133 exec 10<$keyfile; run_app kcapi-dgst --keyfd 10 -c "hmac(sha256)" --hex -o $GENDGST < $ORIGPT
134 echoscip-shell system . . echo. >> $GENDGST
135 opensslscip-shell system . . openssl. dgst -sha256 -hmac $opensslkey $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
136 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "STDIN / FILEOUT test (keyed MD $keysize bits)"
137
138 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -o $GENDGST < $ORIGPT
139 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -o $GENDGST.2 < $ORIGPT
140
141 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.2 "STDIN / FILEOUT test (password)"
142 }
143
144 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout().()
145 {
146 local keyfile=$1
147
148 if [scip-shell system . . `[`. ! -f "$keyfile" ]
149 then
150 echoscip-shell system . . echo. "Keyfile $file does not exist"
151 exit 1
152 fi
153
154 run_app kcapi-dgst -c "sha256" --hex -i $ORIGPT > $GENDGST
155 echoscip-shell system . . echo. >> $GENDGST
156 opensslscip-shell system . . openssl. dgst -sha256 $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
157 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "FILEIN / STDOUT test (hash)"
158
159 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
160 keysize=$((keysize*8))
161 eval opensslkey=\$OPENSSLKEY${keysize}
162
163 exec 10<$keyfile; run_app kcapi-dgst --keyfd 10 -c "hmac(sha256)" --hex -i $ORIGPT > $GENDGST
164 echoscip-shell system . . echo. >> $GENDGST
165 opensslscip-shell system . . openssl. dgst -sha256 -hmac $opensslkey $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
166 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "FILEIN / STDOUT test (keyed MD $keysize bits)"
167
168 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -i $ORIGPT > $GENDGST
169 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -i $ORIGPT > $GENDGST.2
170
171 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.2 "FILEIN / STDOUT test (password)"
172 }
173
174 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout().()
175 {
176 local keyfile=$1
177
178 local keyfile=$1
179
180 if [scip-shell system . . `[`. ! -f "$keyfile" ]
181 then
182 echoscip-shell system . . echo. "Keyfile $file does not exist"
183 exit 1
184 fi
185
186 run_app kcapi-dgst -c "sha256" --hex -i $ORIGPT -o $GENDGST
187 echoscip-shell system . . echo. >> $GENDGST
188 opensslscip-shell system . . openssl. dgst -sha256 $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
189 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "FILEIN / FILEOUT test (hash)"
190
191 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
192 keysize=$((keysize*8))
193 eval opensslkey=\$OPENSSLKEY${keysize}
194
195 exec 10<$keyfile; run_app kcapi-dgst --keyfd 10 -c "hmac(sha256)" --hex -i $ORIGPT -o $GENDGST
196 echoscip-shell system . . echo. >> $GENDGST
197 opensslscip-shell system . . openssl. dgst -sha256 -hmac $opensslkey $ORIGPT | awkscip-shell system . . awk. 'BEGIN {FS="= "} {print $2}' > $GENDGST.openssl
198 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.openssl "FILEIN / FILEOUT test (keyed MD $keysize bits)"
199
200 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -i $ORIGPT -o $GENDGST
201 run_app kcapi-dgst -q --pbkdfiter 1000 -p "passwd" -s $SALT -c "hmac(sha256)" -i $ORIGPT -o $GENDGST.2
202
203 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENDGST $GENDGST.2 "FILEIN / FILEOUT test (password)"
204 }
205
206 init_setupscip-shell shell shell-project 0.0.0 init_setup().
207
208 for i in 0 1 15 16 29 32 257 512 1023 16385 65535 65536 65537 99999 100000 100001
209 do
210 gen_origscip-shell shell shell-project 0.0.0 gen_orig(). $i
211 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout(). $KEYFILE_128
212 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout(). $KEYFILE_256
213 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout(). $KEYFILE_128
214 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout(). $KEYFILE_256
215 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout(). $KEYFILE_128
216 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout(). $KEYFILE_256
217 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout(). $KEYFILE_128
218 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout(). $KEYFILE_256
219 done
220
221 echoscip-shell system . . echo. "==================================================================="
222 echoscip-shell system . . echo. "Number of failures: $failures"
223
224 exit $failures