Debian Codegraph @

test/kcapi-enc-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-enc
25 TSTPREFIX="${TMPDIR}/kcapi-enc-testfiles."
26 KEYFILE_AES128="${TSTPREFIX}aes128key"
27 KEYFILE_AES256="${TSTPREFIX}aes256key"
28 OPENSSLKEY128=""
29 OPENSSLKEY256=""
30
31 ORIGPT="${TSTPREFIX}orig_pt"
32 GENPT="${TSTPREFIX}generated_pt"
33 GENCT="${TSTPREFIX}generated_ct"
34
35 IV="0123456789abcdef0123456789abcdef"
36
37 #CCM Decrypt
38 CCM_MSG="4edb58e8d5eb6bc711c43a6f3693daebde2e5524f1b55297abb29f003236e43d"
39 CCM_KEY="2861fd0253705d7875c95ba8a53171b4"
40 CCM_AAD="fb7bc304a3909e66e2e0c5ef952712dd884ce3e7324171369f2c5db1adc48c7d"
41 CCM_TAG="a7877c99"
42 CCM_TAG_FAIL="a7877c98"
43 CCM_NONCE="674742abd0f5ba"
44 CCM_EXP="8dd351509dcf1df9c33987fb31cd708dd60d65d3d4e1baa53581d891d994d723"
45
46 #GCM Encrypt
47 GCM_MSG="507937f393b2de0fa218d0a9713262f4"
48 GCM_KEY="5aa3d01e7242d7a64f5fd4ad25505390"
49 GCM_IV="94af90b40cc541173d201250"
50 GCM_AAD="0f7479e28c53d120fcf57a525e0b36a0"
51 GCM_TAGLEN="14"
52 GCM_EXP="e80e074e70b089c160c6d3863e8d2b75ac767d2d44412252eed41a220f31"
53
54 failures=0
55
56 hex2binscip-shell shell shell-project 0.0.0 hex2bin().()
57 {
58 local hex=$1
59 local dstfile=$2
60
61 echoscip-shell system . . echo. -n $hex | perlscip-shell system . . perl. -pe 's/([0-9a-f]{2})/chr hex $1/gie' > $dstfile
62 }
63
64 bin2hex_noaadscip-shell shell shell-project 0.0.0 bin2hex_noaad().()
65 {
66 local origfile=$1
67 local aadlenskip=$2
68
69 local hex=$(hexdumpscip-shell system . . hexdump. -ve '/1 "%02x"scip-shell filesystem . . `/1 "%02x"`.' -s$aadlenskip $origfile)
70
71 echoscip-shell system . . echo. $hex
72 }
73
74 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local().()
75 {
76 if [scip-shell system . . `[`. -f $ORIGPT ]
77 then
78 local bytes=$(statscip-shell system . . stat. -c %s $ORIGPT)
79 echo_pass "$bytes bytes: $@"
80 else
81 echo_pass $@
82 fi
83 }
84
85 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local().()
86 {
87 if [scip-shell system . . `[`. -f $ORIGPT ]
88 then
89 local bytes=$(statscip-shell system . . stat. -c %s $ORIGPT)
90 echo_fail "$bytes bytes: $@"
91 else
92 echo_fail $@
93 fi
94 }
95
96 init_setupscip-shell shell shell-project 0.0.0 init_setup().()
97 {
98 trap "rm -f $TSTPREFIX*; exit" 0 1 2 3 15
99
100 # CR is also character
101 # Hex key string: 3031323334353637383961626364650a
102 echoscip-shell system . . echo. "0123456789abcde" > $KEYFILE_AES128
103 OPENSSLKEY128="3031323334353637383961626364650a"
104 # Hex key string: 303132333435363738396162636465663031323334353637383961626364650a
105 echoscip-shell system . . echo. "0123456789abcdef0123456789abcde" > $KEYFILE_AES256
106 OPENSSLKEY256="303132333435363738396162636465663031323334353637383961626364650a"
107
108 hex2binscip-shell shell shell-project 0.0.0 hex2bin(). $CCM_MSG ${TSTPREFIX}ccm_msg
109 hex2binscip-shell shell shell-project 0.0.0 hex2bin(). $CCM_KEY ${TSTPREFIX}ccm_key
110 hex2binscip-shell shell shell-project 0.0.0 hex2bin(). $GCM_MSG ${TSTPREFIX}gcm_msg
111 hex2binscip-shell shell shell-project 0.0.0 hex2bin(). $GCM_KEY ${TSTPREFIX}gcm_key
112 }
113
114 gen_origscip-shell shell shell-project 0.0.0 gen_orig().()
115 {
116 local size=$1
117 size=$((size-1))
118 ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of=$ORIGPT bs=$size count=1 2>/dev/nullscip-shell filesystem . . `/dev/null`.
119
120 #ensure that the last byte is no pad-byte
121 echoscip-shell system . . echo. -n -e '\xff' >> $ORIGPT
122 }
123
124 diff_filescip-shell shell shell-project 0.0.0 diff_file().()
125 {
126 local orighash=$(sha256sumscip-shell system . . sha256sum. $1 | cutscip-shell system . . cut. -d " " -f1)
127 local genhash=$(sha256sumscip-shell system . . sha256sum. $2 | cutscip-shell system . . cut. -d " " -f1)
128 shift
129 shift
130
131 if [scip-shell system . . `[`. x"$orighash" = x"$genhash" ]
132 then
133 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local(). "$@"
134 else
135 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local(). "$@: original file ($orighash) and generated file ($genhash)"
136 fi
137
138 }
139
140 # Do not test CBC as padding is not removed
141 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout().()
142 {
143 local keyfile=$1
144
145 if [scip-shell system . . `[`. ! -f "$keyfile" ]
146 then
147 echoscip-shell system . . echo. "Keyfile $file does not exist"
148 exit 1
149 fi
150
151 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
152 keysize=$((keysize*8))
153
154 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -e -c "ctr(aes)" --iv $IV < $ORIGPT > $GENCT
155 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -d -c "ctr(aes)" --iv $IV < $GENCT > $GENPT
156
157 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "STDIN / STDOUT enc test ($keysize bits)"
158
159 eval opensslkey=\$OPENSSLKEY${keysize}
160 opensslscip-shell system . . openssl. enc -aes-$keysize-ctr -in $ORIGPT -out $GENCT.openssl -K $opensslkey -iv $IV
161 opensslscip-shell system . . openssl. enc -d -aes-$keysize-ctr -in $GENCT -out $GENPT.openssl -K $opensslkey -iv $IV
162
163 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENCT $GENCT.openssl "STDIN / STDOUT enc test ($keysize bits) (openssl generated CT)"
164 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENPT $GENPT.openssl "STDIN / STDOUT enc test ($keysize bits) (openssl generated PT)"
165
166 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -e -c "ctr(aes)" --iv $IV < $ORIGPT > $GENCT
167 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -d -c "ctr(aes)" --iv $IV < $GENCT > $GENPT
168
169 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "STDIN / STDOUT enc test (password)"
170 }
171
172 # Do not test CBC as padding is not removed
173 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout().()
174 {
175 local keyfile=$1
176
177 if [scip-shell system . . `[`. ! -f "$keyfile" ]
178 then
179 echoscip-shell system . . echo. "Keyfile $file does not exist"
180 exit 1
181 fi
182
183 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
184 keysize=$((keysize*8))
185
186 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -e -c "ctr(aes)" --iv $IV -o $GENCT < $ORIGPT
187 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -d -c "ctr(aes)" --iv $IV -o $GENPT < $GENCT
188
189 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "STDIN / FILEOUT test ($keysize bits)"
190
191 eval opensslkey=\$OPENSSLKEY${keysize}
192 opensslscip-shell system . . openssl. enc -aes-$keysize-ctr -in $ORIGPT -out $GENCT.openssl -K $opensslkey -iv $IV
193 opensslscip-shell system . . openssl. enc -d -aes-$keysize-ctr -in $GENCT -out $GENPT.openssl -K $opensslkey -iv $IV
194
195 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENCT $GENCT.openssl "STDIN / FILEOUT enc test ($keysize bits) (openssl generated CT)"
196 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENPT $GENPT.openssl "STDIN / FILEOUT enc test ($keysize bits) (openssl generated PT)"
197
198 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -e -c "ctr(aes)" --iv $IV -o $GENCT < $ORIGPT
199 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -d -c "ctr(aes)" --iv $IV -o $GENPT < $GENCT
200
201 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "STDIN / FILEOUT enc test (password)"
202 }
203
204 # Do not test CBC as padding is not removed
205 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout().()
206 {
207 local keyfile=$1
208
209 if [scip-shell system . . `[`. ! -f "$keyfile" ]
210 then
211 echoscip-shell system . . echo. "Keyfile $file does not exist"
212 exit 1
213 fi
214
215 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
216 keysize=$((keysize*8))
217
218 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -e -c "ctr(aes)" --iv $IV -i $ORIGPT > $GENCT
219 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -d -c "ctr(aes)" --iv $IV -i $GENCT > $GENPT
220
221 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "FILEIN / STDOUT enc test ($keysize bits)"
222
223 eval opensslkey=\$OPENSSLKEY${keysize}
224 opensslscip-shell system . . openssl. enc -aes-$keysize-ctr -in $ORIGPT -out $GENCT.openssl -K $opensslkey -iv $IV
225 opensslscip-shell system . . openssl. enc -d -aes-$keysize-ctr -in $GENCT -out $GENPT.openssl -K $opensslkey -iv $IV
226
227 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENCT $GENCT.openssl "FILEIN / STDOUT enc test ($keysize bits) (openssl generated CT)"
228 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENPT $GENPT.openssl "FILEIN / STDOUT enc test ($keysize bits) (openssl generated PT)"
229
230 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -e -c "ctr(aes)" --iv $IV -i $ORIGPT > $GENCT
231 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s $IV -d -c "ctr(aes)" --iv $IV -i $GENCT > $GENPT
232
233 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "FILEIN / STDOUT enc test (password)"
234 }
235
236 # Use cipher with padding requirement
237 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout().()
238 {
239 local keyfile=$1
240
241 if [scip-shell system . . `[`. ! -f "$keyfile" ]
242 then
243 echoscip-shell system . . echo. "Keyfile $file does not exist"
244 exit 1
245 fi
246
247 local keysize=$(statscip-shell system . . stat. -c %s $keyfile)
248 keysize=$((keysize*8))
249
250
251 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -e -c "cbc(aes)" --iv $IV -i $ORIGPT -o $GENCT
252 exec 10<$keyfile; run_app kcapi-enc --keyfd 10 -d -c "cbc(aes)" --iv $IV -i $GENCT -o $GENPT
253
254 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "FILEIN / FILEOUT enc test ($keysize bits)"
255
256 local ptsize=$(statscip-shell system . . stat. -c %s $ORIGPT)
257 local fullblock=$((ptsize%16))
258 local extra_openssl_opts=""
259
260 if [scip-shell system . . `[`. $fullblock -eq 0 ]
261 then
262 # OpenSSL uses PKCS#7 padding which adds an extra pad block in this case
263 # Disable PKCS#7 padding when input length is a multiple of block size
264 extra_openssl_opts="$extra_openssl_opts -nopad"
265 fi
266
267 eval opensslkey=\$OPENSSLKEY${keysize}
268 opensslscip-shell system . . openssl. enc -aes-$keysize-cbc -in $ORIGPT -out $GENCT.openssl -K $opensslkey -iv $IV $extra_openssl_opts
269 opensslscip-shell system . . openssl. enc -d -aes-$keysize-cbc -in $GENCT -out $GENPT.openssl -K $opensslkey -iv $IV $extra_openssl_opts
270
271 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENCT $GENCT.openssl "FILEIN / FILEOUT enc test ($keysize bits) (openssl generated CT)"
272 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $GENPT $GENPT.openssl "FILEIN / FILEOUT enc test ($keysize bits) (openssl generated PT)"
273
274 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s "123" -e -c "cbc(aes)" --iv $IV -i $ORIGPT -o $GENCT
275 run_app kcapi-enc -q --pbkdfiter 1000 -p "passwd" -s "123" -d -c "cbc(aes)" --iv $IV -i $GENCT -o $GENPT
276
277 diff_filescip-shell shell shell-project 0.0.0 diff_file(). $ORIGPT $GENPT "FILEIN / FILEOUT enc test (password)"
278 }
279
280 test_ccm_decscip-shell shell shell-project 0.0.0 test_ccm_dec().()
281 {
282 local aadlen=${#CCM_AAD}
283
284 aadlen=$(($aadlen/2))
285
286 exec 10<${TSTPREFIX}ccm_key; run_app kcapi-enc --keyfd 10 -d -c "ccm(aes)" -i ${TSTPREFIX}ccm_msg -o ${TSTPREFIX}ccm_out --ccm-nonce $CCM_NONCE --aad $CCM_AAD --tag $CCM_TAG
287 local hexret=$(bin2hex_noaadscip-shell shell shell-project 0.0.0 bin2hex_noaad(). ${TSTPREFIX}ccm_out $aadlen)
288
289 if [scip-shell system . . `[`. x"$hexret" != x"$CCM_EXP" ]
290 then
291 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local(). "CCM output does not match expected output (received: $hexret -- expected $CCM_EXP)"
292 else
293 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local(). "FILEIN / FILEOUT CCM decrypt"
294 fi
295
296 exec 10<${TSTPREFIX}ccm_key; run_app kcapi-enc --keyfd 10 -d -c "ccm(aes)" -i ${TSTPREFIX}ccm_msg -o ${TSTPREFIX}ccm_out --ccm-nonce $CCM_NONCE --aad $CCM_AAD --tag $CCM_TAG_FAIL -q
297
298 # 182 == -EBADMSG
299 if [scip-shell system . . `[`. $? -eq 182 ]
300 then
301 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local(). "FILEIN / FILEOUT CCM decrypt integrity violation"
302 else
303 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local(). "CCM integrity violation not caught"
304 fi
305 }
306
307 test_gcm_encscip-shell shell shell-project 0.0.0 test_gcm_enc().()
308 {
309 local aadlen=${#GCM_AAD}
310
311 aadlen=$(($aadlen/2))
312
313 exec 10<${TSTPREFIX}gcm_key; run_app kcapi-enc --keyfd 10 -e -c "gcm(aes)" -i ${TSTPREFIX}gcm_msg -o ${TSTPREFIX}gcm_out --iv $GCM_IV --aad $GCM_AAD --taglen $GCM_TAGLEN
314 local hexret=$(bin2hex_noaadscip-shell shell shell-project 0.0.0 bin2hex_noaad(). ${TSTPREFIX}gcm_out $aadlen)
315
316 if [scip-shell system . . `[`. x"$hexret" != x"$GCM_EXP" ]
317 then
318 echo_fail_localscip-shell shell shell-project 0.0.0 echo_fail_local(). "GCM output does not match expected output (received: $hexret -- expected $GCM_EXP)"
319 else
320 echo_pass_localscip-shell shell shell-project 0.0.0 echo_pass_local(). "FILEIN / FILEOUT GCM encrypt"
321 fi
322 }
323
324 init_setupscip-shell shell shell-project 0.0.0 init_setup().
325 test_gcm_encscip-shell shell shell-project 0.0.0 test_gcm_enc().
326 test_ccm_decscip-shell shell shell-project 0.0.0 test_ccm_dec().
327
328 for i in 1 15 16 29 32 257 512 1023 16385 65535 65536 65537 99999 100000 100001
329 do
330 gen_origscip-shell shell shell-project 0.0.0 gen_orig(). $i
331 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout(). $KEYFILE_AES128
332 test_stdin_stdoutscip-shell shell shell-project 0.0.0 test_stdin_stdout(). $KEYFILE_AES256
333 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout(). $KEYFILE_AES128
334 test_stdin_fileoutscip-shell shell shell-project 0.0.0 test_stdin_fileout(). $KEYFILE_AES256
335 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout(). $KEYFILE_AES128
336 test_filein_stdoutscip-shell shell shell-project 0.0.0 test_filein_stdout(). $KEYFILE_AES256
337 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout(). $KEYFILE_AES128
338 test_filein_fileoutscip-shell shell shell-project 0.0.0 test_filein_fileout(). $KEYFILE_AES256
339 done
340
341 echoscip-shell system . . echo. "==================================================================="
342 echoscip-shell system . . echo. "Number of failures: $failures"
343
344 exit $failures