12e7f19a679c682315063ba688911edd15f3cf91
[platform/upstream/cryptsetup.git] / tests / compat-test
1 #!/bin/bash
2
3 CRYPTSETUP=../src/cryptsetup
4
5 LOOPDEV=/dev/loop5
6 DEV_NAME=dummy
7 DEV_NAME2=dummy2
8 ORIG_IMG=luks-test-orig
9 IMG=luks-test
10 KEY1=key1
11 KEY2=key2
12
13 LUKS_HEADER="S0-5 S6-7 S8-39 S40-71 S72-103 S104-107 S108-111 R112-131 R132-163 S164-167 S168-207 A0-591"
14 KEY_SLOT0="S208-211 S212-215 R216-247 A248-251 A251-255"
15 KEY_MATERIAL0="R4096-68096"
16 KEY_MATERIAL0_EXT="R4096-68096"
17
18 KEY_SLOT1="S256-259 S260-263 R264-295 A296-299 A300-303"
19 KEY_MATERIAL1="R69632-133632"
20 KEY_MATERIAL1_EXT="S69632-133632"
21
22 TEST_UUID="12345678-1234-1234-1234-123456789abc"
23
24 function remove_mapping()
25 {
26         [ -b /dev/mapper/$DEV_NAME2 ] && dmsetup remove $DEV_NAME2
27         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
28         losetup -d $LOOPDEV >/dev/null 2>&1
29         rm -f $ORIG_IMG $IMG $KEY1 $KEY2 >/dev/null 2>&1
30 }
31
32 function fail()
33 {
34         remove_mapping
35         echo "FAILED"
36         exit 2
37 }
38
39 function prepare()
40 {
41         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
42
43         case "$2" in
44         wipe)
45                 remove_mapping
46                 dd if=/dev/zero of=$IMG bs=1k count=10000 >/dev/null 2>&1
47                 sync
48                 losetup $LOOPDEV $IMG
49                 ;;
50         new)
51                 remove_mapping
52                 bzip2 -cd compatimage.img.bz2 > $IMG
53                 losetup $LOOPDEV $IMG
54                 ;;
55         reuse | *)
56                 if [ ! -e $IMG ]; then
57                         bzip2 -cd compatimage.img.bz2 > $IMG
58                         losetup $LOOPDEV $IMG
59                 fi
60                 ;;
61         esac
62
63         if [ ! -e $KEY1 ]; then
64                 dd if=/dev/urandom of=$KEY1 count=1 bs=32 >/dev/null 2>&1
65         fi
66
67         if [ ! -e $KEY2 ]; then
68                 dd if=/dev/urandom of=$KEY2 count=1 bs=16 >/dev/null 2>&1
69         fi
70
71         cp $IMG $ORIG_IMG
72         [ -n "$1" ] && echo "CASE: $1"
73 }
74
75 function check()
76 {
77         sync
78         [ -z "$1" ] && return
79         ./differ $ORIG_IMG $IMG $1 || fail
80 }
81
82 function check_exists()
83 {
84         [ -b /dev/mapper/$DEV_NAME ] || fail
85         check $1
86 }
87
88 if [ $(id -u) != 0 ]; then
89         echo "WARNING: You must be root to run this test, test skipped."
90         exit 0
91 fi
92
93 # LUKS tests
94
95 prepare "[1] open - compat image - acceptance check" new
96 echo "compatkey" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
97 check_exists
98
99 prepare "[2] open - compat image - denial check" new
100 echo "wrongkey" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME 2>/dev/null && fail
101 check
102
103 # All headers items and first key material section must change
104 prepare "[3] format" wipe
105 echo "key0" | $CRYPTSETUP -i 1000 -c aes-cbc-essiv:sha256 -s 128 luksFormat $LOOPDEV || fail
106 check "$LUKS_HEADER $KEY_SLOT0 $KEY_MATERIAL0"
107
108 prepare "[4] format using hash sha512" wipe
109 echo "key0" | $CRYPTSETUP -i 1000 -h sha512 -c aes-cbc-essiv:sha256 -s 128 luksFormat $LOOPDEV || fail
110 check "$LUKS_HEADER $KEY_SLOT0 $KEY_MATERIAL0"
111
112 prepare "[5] open"
113 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
114 check_exists
115
116 # Key Slot 1 and key material section 1 must change, the rest must not.
117 prepare "[6] add key"
118 echo -e "key0\nkey1" | $CRYPTSETUP luksAddKey $LOOPDEV || fail
119 check "$KEY_SLOT1 $KEY_MATERIAL1"
120 echo "key1" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
121
122 # Unsuccessful Key Delete - nothing may change
123 prepare "[7] unsuccessful delete"
124 echo "invalid" | $CRYPTSETUP luksKillSlot $LOOPDEV 1 2>/dev/null && fail
125 check
126
127 # Delete Key Test
128 # Key Slot 1 and key material section 1 must change, the rest must not
129 prepare "[8] successful delete"
130 $CRYPTSETUP -q luksKillSlot $LOOPDEV 1 || fail
131 check "$KEY_SLOT1 $KEY_MATERIAL1_EXT"
132 echo "key1" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME 2> /dev/null && fail
133 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
134
135 # Key Slot 1 and key material section 1 must change, the rest must not
136 prepare "[9] add key test for key files"
137 echo "key0" | $CRYPTSETUP luksAddKey $LOOPDEV $KEY1 || fail
138 check "$KEY_SLOT1 $KEY_MATERIAL1"
139 $CRYPTSETUP -d $KEY1 luksOpen $LOOPDEV $DEV_NAME || fail
140
141 # Key Slot 1 and key material section 1 must change, the rest must not
142 prepare "[10] delete key test with key1 as remaining key"
143 $CRYPTSETUP -d $KEY1 luksKillSlot $LOOPDEV 0 || fail
144 check "$KEY_SLOT0 $KEY_MATERIAL0_EXT"
145 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME 2>/dev/null && fail
146 $CRYPTSETUP luksOpen -d $KEY1 $LOOPDEV $DEV_NAME || fail
147
148 # Delete last slot
149 prepare "[11] delete last key" wipe
150 echo "key0" | $CRYPTSETUP luksFormat $LOOPDEV || fail
151 echo "key0" | $CRYPTSETUP luksKillSlot $LOOPDEV 0 || fail
152 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME 2>/dev/null && fail
153
154 # Format test for ESSIV, and some other parameters.
155 prepare "[12] parameter variation test" wipe
156 $CRYPTSETUP -q -i 1000 -c aes-cbc-essiv:sha256 -s 128 luksFormat $LOOPDEV $KEY1 || fail
157 check "$LUKS_HEADER $KEY_SLOT0 $KEY_MATERIAL0"
158 $CRYPTSETUP -d $KEY1 luksOpen $LOOPDEV $DEV_NAME || fail
159
160 prepare "[13] open/close - stacked devices" wipe
161 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV || fail
162 echo "key0" | $CRYPTSETUP -q luksOpen $LOOPDEV $DEV_NAME || fail
163 echo "key0" | $CRYPTSETUP -q luksFormat /dev/mapper/$DEV_NAME || fail
164 echo "key0" | $CRYPTSETUP -q luksOpen /dev/mapper/$DEV_NAME $DEV_NAME2 || fail
165 $CRYPTSETUP -q luksClose  $DEV_NAME2 || fail
166 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
167
168 prepare "[14] format/open - passphrase on stdin & new line" wipe
169 # stdin defined by "-" must take even newline
170 echo -n $'foo\nbar' | $CRYPTSETUP -q luksFormat $LOOPDEV - || fail
171 echo -n $'foo\nbar' | $CRYPTSETUP -q --key-file=- luksOpen $LOOPDEV $DEV_NAME || fail
172 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
173 echo -n $'foo\nbar' | $CRYPTSETUP -q luksOpen $LOOPDEV $DEV_NAME 2>/dev/null && fail
174 # now also try --key-file
175 echo -n $'foo\nbar' | $CRYPTSETUP -q luksFormat $LOOPDEV --key-file=- || fail
176 echo -n $'foo\nbar' | $CRYPTSETUP -q --key-file=- luksOpen $LOOPDEV $DEV_NAME || fail
177 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
178 # process newline if from stdin
179 echo -n $'foo\nbar' | $CRYPTSETUP -q luksFormat $LOOPDEV || fail
180 echo 'foo' | $CRYPTSETUP -q luksOpen $LOOPDEV $DEV_NAME || fail
181 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
182
183 prepare "[15] UUID - use and report provided UUID" wipe
184 echo "key0" | $CRYPTSETUP -q luksFormat --uuid blah $LOOPDEV 2>/dev/null && fail
185 echo "key0" | $CRYPTSETUP -q luksFormat --uuid $TEST_UUID $LOOPDEV || fail
186 tst=$($CRYPTSETUP -q luksUUID $LOOPDEV)
187 [ "$tst"x = "$TEST_UUID"x ] || fail
188 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV || fail
189 $CRYPTSETUP -q luksUUID --uuid $TEST_UUID $LOOPDEV || fail
190 tst=$($CRYPTSETUP -q luksUUID $LOOPDEV)
191 [ "$tst"x = "$TEST_UUID"x ] || fail
192
193 prepare "[16] luksFormat" wipe
194 echo "key0" | $CRYPTSETUP -q luksFormat --master-key-file /dev/urandom $LOOPDEV || fail
195 echo "key0" | $CRYPTSETUP -q luksFormat --master-key-file /dev/urandom $LOOPDEV -d $KEY1 || fail
196 $CRYPTSETUP -q luksFormat --master-key-file /dev/urandom -s 128 --uuid $TEST_UUID $LOOPDEV $KEY1 || fail
197 $CRYPTSETUP luksOpen -d $KEY1 $LOOPDEV $DEV_NAME || fail
198 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
199
200 prepare "[17] AddKey volume key, passphrase and keyfile" wipe
201 # masterkey
202 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV --master-key-file /dev/zero --key-slot 3 || fail
203 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 3: ENABLED" || fail
204 echo "key1" | $CRYPTSETUP luksAddKey $LOOPDEV --master-key-file /dev/zero --key-slot 4 || fail
205 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 4: ENABLED" || fail
206 echo "key2" | $CRYPTSETUP luksAddKey $LOOPDEV --master-key-file /dev/null --key-slot 5 2>/dev/null && fail
207 # [0]key0 [1]key1 [2]$KEY1/1 [3]$KEY1 [4]$KEY2
208 $CRYPTSETUP -q luksFormat $LOOPDEV $KEY1 --key-slot 3 || fail
209 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 3: ENABLED" || fail
210 $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 $KEY2 --key-slot 3 2>/dev/null && fail
211 # keyfile/keyfile
212 $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 $KEY2 --key-slot 4 || fail
213 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 4: ENABLED" || fail
214 # passphrase/keyfile
215 echo "key0" | $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 --key-slot 0 || fail
216 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: ENABLED" || fail
217 # passphrase/passphrase
218 echo -e "key0\nkey1\n" | $CRYPTSETUP luksAddKey $LOOPDEV --key-slot 1 || fail
219 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 1: ENABLED" || fail
220 # keyfile/passphrase
221 echo -e "key1\n" | $CRYPTSETUP luksAddKey $LOOPDEV $KEY1 --key-slot 2 --new-keyfile-size 1 || fail
222 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 2: ENABLED" || fail
223
224 prepare "[18] RemoveKey passphrase and keyfile" reuse
225 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY1 || fail
226 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 3: DISABLED" || fail
227 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY1 2>/dev/null && fail
228 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY2 --keyfile-size 1 2>/dev/null && fail
229 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY2 || fail
230 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 4: DISABLED" || fail
231 # kill slot using passphrase from 1
232 echo "key1" | $CRYPTSETUP luksKillSlot $LOOPDEV 2 || fail
233 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 2: DISABLED" || fail
234 # remove key0 / slot 0
235 echo "key0" | $CRYPTSETUP luksRemoveKey $LOOPDEV || fail
236 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: DISABLED" || fail
237 # last keyslot, in batch mode no passphrase needed...
238 $CRYPTSETUP luksKillSlot -q $LOOPDEV 1 || fail
239 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 1: DISABLED" || fail
240
241 prepare "[19] create & status & resize" wipe
242 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash xxx 2>/dev/null && fail
243 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --cipher aes-cbc-essiv:sha256 --offset 3 --skip 4 --readonly || fail
244 $CRYPTSETUP -q status  $DEV_NAME | grep "offset:" | grep -q "3 sectors" || fail
245 $CRYPTSETUP -q status  $DEV_NAME | grep "skipped:" | grep -q "4 sectors" || fail
246 $CRYPTSETUP -q status  $DEV_NAME | grep "mode:" | grep -q "readonly" || fail
247 $CRYPTSETUP -q resize  $DEV_NAME --size 100 || fail
248 $CRYPTSETUP -q status  $DEV_NAME | grep "size:" | grep -q "100 sectors" || fail
249 $CRYPTSETUP -q resize  $DEV_NAME || fail
250 $CRYPTSETUP -q status  $DEV_NAME | grep "size:" | grep -q "19997 sectors" || fail
251 $CRYPTSETUP -q remove  $DEV_NAME || fail
252 echo "key0" | $CRYPTSETUP create $DEV_NAME --hash sha1 $LOOPDEV || fail
253 $CRYPTSETUP -q remove  $DEV_NAME || fail
254 echo "key0" | $CRYPTSETUP -q create $DEV_NAME --hash sha1 $LOOPDEV || fail
255 $CRYPTSETUP -q remove  $DEV_NAME || fail
256 # verify is ignored on non-tty input
257 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --verify-passphrase || fail
258 $CRYPTSETUP -q remove  $DEV_NAME || fail
259 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 --key-size 255 2>/dev/null && fail
260 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1  || fail
261 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 2>/dev/null && fail
262 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d blah 2>/dev/null && fail
263 $CRYPTSETUP -q remove  $DEV_NAME || fail
264 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d /dev/urandom || fail
265 $CRYPTSETUP -q remove  $DEV_NAME || fail
266
267 prepare "[20] Disallow open/create if already mapped." wipe
268 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 || fail
269 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 2>/dev/null && fail
270 $CRYPTSETUP create $DEV_NAME2 $LOOPDEV -d $KEY1 2>/dev/null && fail
271 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV 2>/dev/null && fail
272 $CRYPTSETUP remove  $DEV_NAME || fail
273 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV || fail
274 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
275 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME2 2>/dev/null && fail
276 $CRYPTSETUP  luksClose  $DEV_NAME || fail
277
278 prepare "[21] luksDump" wipe
279 echo "key0" | $CRYPTSETUP -q luksFormat --uuid $TEST_UUID $LOOPDEV $KEY1 || fail
280 echo "key0" | $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 || fail
281 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: ENABLED" || fail
282 $CRYPTSETUP luksDump $LOOPDEV | grep -q $TEST_UUID || fail
283 echo "bad" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key 2>/dev/null && fail
284 echo "key0" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key | grep -q "MK dump:" || fail
285 $CRYPTSETUP luksDump -q $LOOPDEV --dump-master-key -d $KEY1 | grep -q "MK dump:" || fail
286
287 prepare "[22] remove disappeared device" wipe
288 dmsetup create $DEV_NAME --table "0 5000 linear $LOOPDEV 2" || fail
289 echo "key0" | $CRYPTSETUP -q -i 0 luksFormat /dev/mapper/$DEV_NAME || fail
290 echo "key0" | $CRYPTSETUP -q luksOpen /dev/mapper/$DEV_NAME $DEV_NAME2 || fail
291 # underlying device now returns error but node is still present
292 dmsetup load $DEV_NAME --table "0 5000 error" || fail
293 dmsetup resume $DEV_NAME || fail
294 $CRYPTSETUP -q luksClose $DEV_NAME2 || fail
295 dmsetup remove $DEV_NAME || fail
296
297 remove_mapping
298 exit 0