3 CRYPTSETUP=../src/cryptsetup
8 ORIG_IMG=luks-test-orig
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"
18 KEY_SLOT1="S256-259 S260-263 R264-295 A296-299 A300-303"
19 KEY_MATERIAL1="R69632-133632"
20 KEY_MATERIAL1_EXT="S69632-133632"
22 TEST_UUID="12345678-1234-1234-1234-123456789abc"
24 function remove_mapping()
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
41 [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
46 dd if=/dev/zero of=$IMG bs=1k count=10000 >/dev/null 2>&1
52 bzip2 -cd compatimage.img.bz2 > $IMG
56 if [ ! -e $IMG ]; then
57 bzip2 -cd compatimage.img.bz2 > $IMG
63 if [ ! -e $KEY1 ]; then
64 dd if=/dev/urandom of=$KEY1 count=1 bs=32 >/dev/null 2>&1
67 if [ ! -e $KEY2 ]; then
68 dd if=/dev/urandom of=$KEY2 count=1 bs=16 >/dev/null 2>&1
72 [ -n "$1" ] && echo "CASE: $1"
79 ./differ $ORIG_IMG $IMG $1 || fail
82 function check_exists()
84 [ -b /dev/mapper/$DEV_NAME ] || fail
88 if [ $(id -u) != 0 ]; then
89 echo "WARNING: You must be root to run this test, test skipped."
95 prepare "[1] open - compat image - acceptance check" new
96 echo "compatkey" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
99 prepare "[2] open - compat image - denial check" new
100 echo "wrongkey" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME 2>/dev/null && fail
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"
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"
113 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
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
122 # Unsuccessful Key Delete - nothing may change
123 prepare "[7] unsuccessful delete"
124 echo "invalid" | $CRYPTSETUP luksKillSlot $LOOPDEV 1 2>/dev/null && fail
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
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
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
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
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
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
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
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
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
200 prepare "[17] AddKey passphrase and keyfile" wipe
201 # [0]key0 [1]key1 [2]$KEY1/1 [3]$KEY1 [4]$KEY2
202 $CRYPTSETUP -q luksFormat $LOOPDEV $KEY1 --key-slot 3 || fail
203 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 3: ENABLED" || fail
204 $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 $KEY2 --key-slot 3 2>/dev/null && fail
206 $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 $KEY2 --key-slot 4 || fail
207 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 4: ENABLED" || fail
209 echo "key0" | $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 --key-slot 0 || fail
210 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: ENABLED" || fail
211 # passphrase/passphrase
212 echo -e "key0\nkey1\n" | $CRYPTSETUP luksAddKey $LOOPDEV --key-slot 1 || fail
213 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 1: ENABLED" || fail
215 echo -e "key1\n" | $CRYPTSETUP luksAddKey $LOOPDEV $KEY1 --key-slot 2 --new-keyfile-size 1 || fail
216 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 2: ENABLED" || fail
218 prepare "[18] RemoveKey passphrase and keyfile" reuse
219 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY1 || fail
220 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 3: DISABLED" || fail
221 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY1 2>/dev/null && fail
222 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY2 --keyfile-size 1 2>/dev/null && fail
223 $CRYPTSETUP luksRemoveKey $LOOPDEV $KEY2 || fail
224 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 4: DISABLED" || fail
225 # kill slot using passphrase from 1
226 echo "key1" | $CRYPTSETUP luksKillSlot $LOOPDEV 2 || fail
227 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 2: DISABLED" || fail
228 # remove key0 / slot 0
229 echo "key0" | $CRYPTSETUP luksRemoveKey $LOOPDEV || fail
230 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: DISABLED" || fail
231 # last keyslot, in batch mode no passphrase needed...
232 $CRYPTSETUP luksKillSlot -q $LOOPDEV 1 || fail
233 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 1: DISABLED" || fail
235 prepare "[19] create & status & resize" wipe
236 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash xxx 2>/dev/null && fail
237 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --cipher aes-cbc-essiv:sha256 --offset 3 --skip 4 --readonly || fail
238 $CRYPTSETUP -q status $DEV_NAME | grep "offset:" | grep -q "3 sectors" || fail
239 $CRYPTSETUP -q status $DEV_NAME | grep "skipped:" | grep -q "4 sectors" || fail
240 $CRYPTSETUP -q status $DEV_NAME | grep "mode:" | grep -q "readonly" || fail
241 $CRYPTSETUP -q resize $DEV_NAME --size 100 || fail
242 $CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "100 sectors" || fail
243 $CRYPTSETUP -q resize $DEV_NAME || fail
244 $CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "19997 sectors" || fail
245 $CRYPTSETUP -q remove $DEV_NAME || fail
246 echo "key0" | $CRYPTSETUP create $DEV_NAME --hash sha1 $LOOPDEV || fail
247 $CRYPTSETUP -q remove $DEV_NAME || fail
248 echo "key0" | $CRYPTSETUP -q create $DEV_NAME --hash sha1 $LOOPDEV || fail
249 $CRYPTSETUP -q remove $DEV_NAME || fail
250 # verify is ignored on non-tty input
251 echo "key0" | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --verify-passphrase || fail
252 $CRYPTSETUP -q remove $DEV_NAME || fail
253 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 --key-size 255 2>/dev/null && fail
254 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 || fail
255 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 2>/dev/null && fail
256 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d blah 2>/dev/null && fail
257 $CRYPTSETUP -q remove $DEV_NAME || fail
258 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d /dev/urandom || fail
259 $CRYPTSETUP -q remove $DEV_NAME || fail
261 prepare "[20] Disallow open/create if already mapped." wipe
262 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 || fail
263 $CRYPTSETUP create $DEV_NAME $LOOPDEV -d $KEY1 2>/dev/null && fail
264 $CRYPTSETUP create $DEV_NAME2 $LOOPDEV -d $KEY1 2>/dev/null && fail
265 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV 2>/dev/null && fail
266 $CRYPTSETUP remove $DEV_NAME || fail
267 echo "key0" | $CRYPTSETUP -q luksFormat $LOOPDEV || fail
268 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME || fail
269 echo "key0" | $CRYPTSETUP luksOpen $LOOPDEV $DEV_NAME2 2>/dev/null && fail
270 $CRYPTSETUP luksClose $DEV_NAME || fail
272 prepare "[21] luksDump" wipe
273 echo "key0" | $CRYPTSETUP -q luksFormat --uuid $TEST_UUID $LOOPDEV $KEY1 || fail
274 echo "key0" | $CRYPTSETUP luksAddKey $LOOPDEV -d $KEY1 || fail
275 $CRYPTSETUP luksDump $LOOPDEV | grep -q "Key Slot 0: ENABLED" || fail
276 $CRYPTSETUP luksDump $LOOPDEV | grep -q $TEST_UUID || fail
277 echo "bad" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key 2>/dev/null && fail
278 echo "key0" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key | grep -q "MK dump:" || fail
279 $CRYPTSETUP luksDump -q $LOOPDEV --dump-master-key -d $KEY1 | grep -q "MK dump:" || fail