Imported Upstream version 1.6.7
[platform/upstream/cryptsetup.git] / tests / verity-compat-test
1 #!/bin/bash
2
3 VERITYSETUP=../src/veritysetup
4
5 DEV_NAME=verity3273
6 DEV_OUT="$DEV_NAME.out"
7 IMG=verity-data
8 IMG_HASH=verity-hash
9
10 function remove_mapping()
11 {
12         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
13         [ ! -z "$LOOPDEV1" ] && losetup -d $LOOPDEV1 >/dev/null 2>&1
14         rm -f $IMG $IMG_HASH $DEV_OUT >/dev/null 2>&1
15         LOOPDEV1=""
16         LOOPDEV2=""
17 }
18
19 function fail()
20 {
21         [ -n "$1" ] && echo "$1"
22         echo "FAILED"
23         [ -f $DEV_OUT ] && cat $DEV_OUT
24         remove_mapping
25         exit 2
26 }
27
28 function skip()
29 {
30         [ -n "$1" ] && echo "$1"
31         exit 0
32 }
33
34 function prepare() # $1 dev1_siz [$2 dev2_size]
35 {
36         remove_mapping
37
38         dd if=/dev/zero of=$IMG      bs=1k count=$1 >/dev/null 2>&1
39         LOOPDEV1=$(losetup -f 2>/dev/null)
40         [ -z "$LOOPDEV1" ] && fail "No free loop device"
41         losetup $LOOPDEV1 $IMG
42
43         [ -z "$2" ] && return
44         LOOPDEV2=$IMG_HASH
45 }
46
47 function wipe()
48 {
49         dd if=/dev/zero of=$LOOPDEV1 bs=256k >/dev/null 2>&1
50         rm -f $IMG_HASH $DEV_OUT >/dev/null 2>&1
51 }
52
53 function check_exists()
54 {
55         [ -b /dev/mapper/$DEV_NAME ] || fail
56 }
57
58 function compare_out() # $1 what, $2 expected
59 {
60         OPT=$(grep -v "^#" $DEV_OUT | grep -i "$1" | sed -e s/.*\:\ // )
61         [ -z "$OPT" ] && fail
62         [ $OPT != $2 ] && fail "$1 differs ($2)"
63 }
64
65 function check_root_hash() # $1 size, $2 hash, $3 salt, $4 version, $5 hash, [$6 offset]
66 {
67         if [ -z "$LOOPDEV2" ] ; then
68                 BLOCKS=$(($6 / $1))
69                 DEV_PARAMS="$LOOPDEV1 $LOOPDEV1 \
70                            --hash-offset $6 \
71                            --data-blocks=$BLOCKS --debug"
72         else
73                 DEV_PARAMS="$LOOPDEV1 $LOOPDEV2"
74         fi
75
76         for sb in yes no; do
77         FORMAT_PARAMS="--format=$4 --data-block-size=$1 --hash-block-size=$1 --hash=$5 --salt=$3"
78         if [ $sb == yes ] ; then
79                 VERIFY_PARAMS=""
80         else
81                 FORMAT_PAFAMS="$FORMAT_PARAMS --no-superlock"
82                 VERIFY_PARAMS=$FORMAT_PARAMS
83         fi
84
85         for fail in data hash; do
86         wipe
87         echo -n "V$4(sb=$sb) $5 block size $1: "
88         $VERITYSETUP format $DEV_PARAMS $FORMAT_PARAMS >$DEV_OUT || fail
89
90         echo -n "[root hash]"
91         compare_out "root hash" $2
92         compare_out "salt" "$3"
93
94         $VERITYSETUP verify $DEV_PARAMS $VERIFY_PARAMS $2 >>$DEV_OUT 2>&1 || fail
95         echo -n "[verify]"
96
97         $VERITYSETUP create $DEV_NAME $DEV_PARAMS $VERIFY_PARAMS $2  >>$DEV_OUT 2>&1 || fail
98         check_exists
99         echo -n "[activate]"
100
101         dd if=/dev/mapper/$DEV_NAME of=/dev/null bs=$1 2>/dev/null
102         dmsetup status $DEV_NAME | grep "verity V" >/dev/null || fail
103         echo -n "[in-kernel verify]"
104
105         $VERITYSETUP remove $DEV_NAME || fail
106
107         case $fail in
108         data)
109                 dd if=/dev/urandom of=$LOOPDEV1 bs=1 seek=3456 count=8 conv=notrunc 2>/dev/null
110                 TXT="data_dev"
111                 ;;
112         hash)
113                 if [ -z "$LOOPDEV2" ] ; then
114                         dd if=/dev/urandom of=$LOOPDEV1 bs=1 seek=$((8193 + $4)) count=8 conv=notrunc 2>/dev/null
115                 else
116                         dd if=/dev/urandom of=$LOOPDEV2 bs=1 seek=8193 count=8 conv=notrunc 2>/dev/null
117                 fi
118                 TXT="hash_dev"
119                 ;;
120         esac
121
122         $VERITYSETUP verify $DEV_PARAMS $VERIFY_PARAMS $2 >>$DEV_OUT 2>&1 && \
123                 fail "userspace check for $TXT corruption"
124         $VERITYSETUP create $DEV_NAME $DEV_PARAMS $VERIFY_PARAMS $2 >>$DEV_OUT 2>&1 || \
125                 fail "activation"
126         dd if=/dev/mapper/$DEV_NAME of=/dev/null bs=$1 2>/dev/null
127         dmsetup status $DEV_NAME | grep "verity V" >/dev/null && \
128                 fail "in-kernel check for $TXT corruption"
129         $VERITYSETUP remove $DEV_NAME || fail "deactivation"
130         echo "[$TXT corruption]"
131         done
132         done
133 }
134
135 function valgrind_setup()
136 {
137         which valgrind >/dev/null 2>&1 || fail "Cannot find valgrind."
138         [ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable."
139         #export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
140 }
141
142 function valgrind_run()
143 {
144         INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${VERITYSETUP} "$@"
145 }
146
147 [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped."
148 [ ! -x "$VERITYSETUP" ] && skip "Cannot find $VERITYSETUP, test skipped."
149
150 [ -n "$VALG" ] && valgrind_setup && VERITYSETUP=valgrind_run
151 modprobe dm-verity >/dev/null 2>&1
152 dmsetup targets | grep verity >/dev/null 2>&1 || skip "Cannot find dm-verity target, test skipped."
153
154 # VERITYSETUP tests
155
156 SALT=e48da609055204e89ae53b655ca2216dd983cf3cb829f34f63a297d106d53e2d
157
158 echo "Verity tests [separate devices]"
159 prepare 8192 1024
160 check_root_hash  512 9de18652fe74edfb9b805aaed72ae2aa48f94333f1ba5c452ac33b1c39325174 $SALT 1 sha256
161 check_root_hash 1024 54d92778750495d1f80832b486ebd007617d746271511bbf0e295e143da2b3df $SALT 1 sha256
162 check_root_hash 4096 e522df0f97da4febb882ac40f30b37dc0b444bf6df418929463fa25280f09d5c $SALT 1 sha256
163 # version 0
164 check_root_hash 4096 cbbf4ebd004ef65e29b935bb635a39cf754d677f3fa10b0126da725bbdf10f7d $SALT 0 sha256
165 # no salt
166 check_root_hash 4096 ef29c902d87350f1da4bfa536e16cebc162a909bf89abe448b81ec500d4fb9bf - 1 sha256
167 # sha1
168 check_root_hash 1024 d0e9163ca8844aaa2e88fe5265a8c5d9ee494a99 $SALT 1 sha1
169 check_root_hash 1024 73509e8e868be6b8ac939817a98a3d35121413b2 dadada 1 sha1
170
171 echo "Verity tests [one device offset]"
172 prepare $((8192 + 1024))
173 check_root_hash  512 9de18652fe74edfb9b805aaed72ae2aa48f94333f1ba5c452ac33b1c39325174 $SALT 1 sha256 8388608
174 check_root_hash 1024 54d92778750495d1f80832b486ebd007617d746271511bbf0e295e143da2b3df $SALT 1 sha256 8388608
175 check_root_hash 4096 e522df0f97da4febb882ac40f30b37dc0b444bf6df418929463fa25280f09d5c $SALT 1 sha256 8388608
176 # version 0
177 check_root_hash 4096 cbbf4ebd004ef65e29b935bb635a39cf754d677f3fa10b0126da725bbdf10f7d $SALT 0 sha256 8388608
178 # no salt
179 check_root_hash 4096 ef29c902d87350f1da4bfa536e16cebc162a909bf89abe448b81ec500d4fb9bf - 1 sha256 8388608
180 # sha1
181 check_root_hash 1024 d0e9163ca8844aaa2e88fe5265a8c5d9ee494a99 $SALT 1 sha1 8388608
182 check_root_hash 1024 73509e8e868be6b8ac939817a98a3d35121413b2 dadada 1 sha1 8388608
183
184 remove_mapping
185 exit 0