Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / tests / luks1-compat-test
1 #!/bin/bash
2
3 # check luks1 images parsing
4
5 # NOTE: if image with whirlpool hash fails, check
6 # that you are not using old gcrypt with flawed whirlpool
7 # (see cryptsetup debug output)
8
9 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
10 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
11 TST_DIR=luks1-images
12 MAP=luks1tst
13 KEYFILE=keyfile1
14
15 [ -z "$srcdir" ] && srcdir="."
16
17 function remove_mapping()
18 {
19         [ -b /dev/mapper/$MAP ] && dmsetup remove --retry $MAP
20 }
21
22 function fail()
23 {
24         [ -n "$1" ] && echo "$1"
25         echo " [FAILED]"
26         echo "FAILED backtrace:"
27         while caller $frame; do ((frame++)); done
28         remove_mapping
29         exit 2
30 }
31
32 function skip()
33 {
34         [ -n "$1" ] && echo "$1"
35         echo "Test skipped."
36         exit 77
37 }
38
39 function test_one()
40 {
41         $CRYPTSETUP benchmark -c "$1" -s "$2" | grep -v "#" || skip
42 }
43
44 function test_required()
45 {
46         which lsblk >/dev/null 2>&1 || skip "WARNING: lsblk tool required."
47
48         echo "REQUIRED KDF TEST"
49         $CRYPTSETUP benchmark -h whirlpool | grep "N/A" && skip
50
51         echo "REQUIRED CIPHERS TEST"
52         echo "#     Algorithm | Key |  Encryption |  Decryption"
53
54         test_one aes-xts 256
55         test_one twofish-xts 256
56         test_one serpent-xts 256
57         test_one aes-cbc 256
58         test_one aes-lrw 256
59 }
60
61 export LANG=C
62
63 test_required
64 [ ! -d $TST_DIR ] && tar xJf $srcdir/luks1-images.tar.xz --no-same-owner
65
66 echo "PASSPHRASE CHECK"
67 for file in $(ls $TST_DIR/luks1_*) ; do
68         echo -n " $file"
69         $CRYPTSETUP luksOpen -d $TST_DIR/$KEYFILE $file --test-passphrase 2>/dev/null
70         ret=$?
71         # ignore missing whirlpool (pwd failed is exit code 2)
72         [ $ret -eq 1 ] && (echo $file | grep -q -e "whirlpool") && echo " [N/A]" && continue
73         # ignore flawed whirlpool (pwd failed is exit code 2)
74         [ $ret -eq 2 ] && (echo $file | grep -q -e "whirlpool") && \
75                 ($CRYPTSETUP luksDump $file --debug | grep -q -e "flawed whirlpool") && \
76                 echo " [IGNORED (flawed Whirlpool library)]" && continue
77         [ $ret -ne 0 ] && fail
78         echo " [OK]"
79 done
80
81 if [ $(id -u) != 0 ]; then
82         echo "WARNING: You must be root to run activation part of test, test skipped."
83         exit 0
84 fi
85
86 echo "ACTIVATION FS UUID CHECK"
87 for file in $(ls $TST_DIR/luks1_*) ; do
88         echo -n " $file"
89         $CRYPTSETUP luksOpen -d $TST_DIR/$KEYFILE $file $MAP 2>/dev/null
90         ret=$?
91         # ignore missing whirlpool (pwd failed is exit code 2)
92         [ $ret -eq 1 ] && (echo $file | grep -q -e "whirlpool") && echo " [N/A]" && continue
93         # ignore flawed whirlpool (pwd failed is exit code 2)
94         [ $ret -eq 2 ] && (echo $file | grep -q -e "whirlpool") && \
95                 ($CRYPTSETUP luksDump $file --debug | grep -q -e "flawed whirlpool") && \
96                 echo " [IGNORED (flawed Whirlpool library)]" && continue
97         [ $ret -ne 0 ] && fail
98         $CRYPTSETUP status $MAP >/dev/null || fail
99         $CRYPTSETUP status /dev/mapper/$MAP >/dev/null || fail
100         UUID=$(lsblk -n -o UUID /dev/mapper/$MAP)
101         $CRYPTSETUP remove $MAP || fail
102         [ "$UUID" != "DEAD-BABE" ] && fail "UUID check failed."
103         echo " [OK]"
104 done