3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
6 DEV_STACKED="luks0xbabe"
11 FAST_PBKDF="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"
14 udevadm settle >/dev/null 2>&1
15 if [ -d "$MNT_DIR" ] ; then
16 umount -f $MNT_DIR 2>/dev/null
17 rmdir $MNT_DIR 2>/dev/null
19 [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
20 [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME >/dev/null 2>&1
21 # FIXME scsi_debug sometimes in-use here
23 rmmod scsi_debug 2>/dev/null
29 if [ -n "$1" ] ; then echo "FAIL $1" ; fi
30 echo "FAILED backtrace:"
31 while caller $frame; do ((frame++)); done
38 echo "TEST SKIPPED: $1"
43 function dm_crypt_features()
45 VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
46 [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
48 VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
49 VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
50 VER_PTC=$(echo $VER_STR | cut -f 3 -d.)
52 [ $VER_MAJ -lt 1 ] && return
53 [ $VER_MAJ -gt 1 ] && {
59 [ $VER_MIN -lt 14 ] && return
61 if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
67 modprobe scsi_debug $@ delay=0
68 if [ $? -ne 0 ] ; then
69 echo "This kernel seems to not support proper scsi_debug module, test skipped."
74 DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
76 if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
77 echo "This kernel seems to not support topology info, test skipped."
83 [ -b $DEV ] || fail "Cannot find $DEV."
86 format() # expected [forced] [encryption_sector_size]
92 if [ "${2:0:1}" = "s" ]; then
97 test "${3:0:1}" = "s" && _sec_size=${3:1}
99 test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"
101 if [ -z "$2" ] ; then
102 echo -n "Formatting using topology info$_smsg..."
103 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 || fail
105 echo -n "Formatting using forced sector alignment $2$_smsg..."
106 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null || fail
109 # check the device can be activated
110 if [ -n "$DM_SECTOR_SIZE" ] ; then
111 echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
112 $CRYPTSETUP close $DEV_NAME || fail
115 ALIGN=$($CRYPTSETUP luksDump $DEV | tee /tmp/last_dump | grep -A1 "0: crypt" | grep "offset:" | cut -d ' ' -f2)
116 # echo "ALIGN = $ALIGN"
118 [ -z "$ALIGN" ] && fail
120 [ $ALIGN -ne $_exp ] && fail "Expected alignment differs: expected $_exp != detected $ALIGN"
122 # test some operation, just in case
123 echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --key-slot 1
124 [ $? -ne 0 ] && fail "Keyslot add failed."
126 $CRYPTSETUP -q luksKillSlot $DEV 1
127 [ $? -ne 0 ] && fail "Keyslot removal failed."
132 format_fail() # expected [forced] [encryption_sector_size]
138 if [ "${2:0:1}" = "s" ]; then
143 test "${3:0:1}" = "s" && _sec_size=${3:1}
145 test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"
147 if [ -z "$2" ] ; then
148 echo -n "Formatting using topology info$_smsg (must fail)..."
149 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 && fail
151 echo -n "Formatting using forced sector alignment $2$_smsg (must fail)..."
152 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null 2>&1 && fail
158 if [ $(id -u) != 0 ]; then
159 echo "WARNING: You must be root to run this test, test skipped."
164 modprobe --dry-run scsi_debug || exit 77
167 add_device dev_size_mb=32
168 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q >/dev/null || fail
169 EXPCT=$($CRYPTSETUP luksDump $DEV | grep "offset: " | cut -f 2 -d ' ')
170 test "$EXPCT" -gt 512 || fail
172 echo "Default alignment detected: $EXPCT sectors"
175 echo "# Create desktop-class 4K drive"
176 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
177 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1
183 format $EXPCT 1 s1024
184 format $EXPCT 1 s2048
185 format $EXPCT 1 s4096
187 format $EXPCT 8 s1024
188 format $EXPCT 8 s2048
189 format $EXPCT 8 s4096
190 format $((EXPCT+1)) $((EXPCT+1))
191 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
192 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
193 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
195 format $EXPCT $EXPCT s1024
196 format $EXPCT $EXPCT s2048
197 format $EXPCT $EXPCT s4096
200 echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
201 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
202 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
208 format $EXPCT 1 s1024
209 format $EXPCT 1 s2048
210 format $EXPCT 1 s4096
212 format $EXPCT 8 s1024
213 format $EXPCT 8 s2048
214 format $EXPCT 8 s4096
215 format $((EXPCT+1)) $((EXPCT+1))
216 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
217 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
218 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
220 format $EXPCT $EXPCT s1024
221 format $EXPCT $EXPCT s2048
222 format $EXPCT $EXPCT s4096
225 echo "# Create drive with misaligned opt-io to page-size (some bad USB enclosures)"
226 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0, opt-io=33553920)"
227 add_device dev_size_mb=32 sector_size=512 num_tgts=1 opt_blks=65535
233 format $EXPCT 1 s1024
234 format $EXPCT 1 s2048
235 format $EXPCT 1 s4096
237 format $EXPCT 8 s1024
238 format $EXPCT 8 s2048
239 format $EXPCT 8 s4096
240 format $((EXPCT+1)) $((EXPCT+1))
241 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
242 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
243 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
245 format $EXPCT $EXPCT s1024
246 format $EXPCT $EXPCT s2048
247 format $EXPCT $EXPCT s4096
250 echo "# Create desktop-class 4K drive w/ 1-sector shift (original bug report)"
251 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=512)"
252 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=1 num_tgts=1
254 format_fail $((EXPCT+1)) s1024
255 format_fail $((EXPCT+1)) s2048
256 format_fail $((EXPCT+1)) s4096
258 format $EXPCT 1 s1024
259 format $EXPCT 1 s2048
260 format $EXPCT 1 s4096
262 format $EXPCT 8 s1024
263 format $EXPCT 8 s2048
264 format $EXPCT 8 s4096
265 format $((EXPCT+1)) $((EXPCT+1))
266 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
267 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
268 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
270 format $EXPCT $EXPCT s1024
271 format $EXPCT $EXPCT s2048
272 format $EXPCT $EXPCT s4096
275 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
276 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
277 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
279 format_fail $((EXPCT+7)) s1024
280 format_fail $((EXPCT+7)) s2048
281 format_fail $((EXPCT+7)) s4096
283 format $EXPCT 1 s1024
284 format $EXPCT 1 s2048
285 format $EXPCT 1 s4096
287 format $EXPCT 8 s1024
288 format $EXPCT 8 s2048
289 format $EXPCT 8 s4096
290 format $((EXPCT+1)) $((EXPCT+1))
291 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
292 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
293 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
295 format $EXPCT $EXPCT s1024
296 format $EXPCT $EXPCT s2048
297 format $EXPCT $EXPCT s4096
300 echo "# Create enterprise-class 4K drive"
301 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
302 add_device dev_size_mb=32 sector_size=4096 num_tgts=1 opt_blks=64
308 format $EXPCT 1 s1024
309 format $EXPCT 1 s2048
310 format $EXPCT 1 s4096
312 format $EXPCT 8 s1024
313 format $EXPCT 8 s2048
314 format $EXPCT 8 s4096
315 #FIXME: kernel limits issue?
316 ##format $((EXPCT+1)) $((EXPCT+1))
317 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
318 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
319 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
321 format $EXPCT $EXPCT s1024
322 format $EXPCT $EXPCT s2048
323 format $EXPCT $EXPCT s4096
326 echo "# Create classic 512B drive and stack dm-linear"
327 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
328 add_device dev_size_mb=32 sector_size=512 num_tgts=1
330 DEV=/dev/mapper/$DEV_STACKED
331 dmsetup create $DEV_STACKED --table "0 65536 linear $DEV2 0"
337 format $EXPCT 1 s1024
338 format $EXPCT 1 s2048
339 format $EXPCT 1 s4096
341 format $EXPCT 8 s1024
342 format $EXPCT 8 s2048
343 format $EXPCT 8 s4096
344 format $((EXPCT+1)) $((EXPCT+1))
345 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
346 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
347 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
349 format $EXPCT $EXPCT s1024
350 format $EXPCT $EXPCT s2048
351 format $EXPCT $EXPCT s4096
354 echo "# Create enterprise-class 4K drive with fs and LUKS images."
355 # loop device here presents 512 block but images have 4k block
356 # cryptsetup should properly use 4k block on direct-io
357 add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
358 for file in $(ls img_fs_*.img.xz) ; do
359 echo "Format using fs image $file."
360 xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
361 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
362 mount $DEV $MNT_DIR || skip "Mounting image is not available."
363 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --offset 8192 || fail
364 echo $PWD2 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail