3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
6 DEV_STACKED="luks0xbabe"
11 FAST_PBKDF="--pbkdf-force-iterations 1000"
13 FIPS_MODE=$(cat /proc/sys/crypto/fips_enabled 2>/dev/null)
15 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
16 CRYPTSETUP_LIB_VALGRIND=../.libs
20 [ -n "$FIPS_MODE" ] && [ "$FIPS_MODE" -gt 0 ]
24 udevadm settle >/dev/null 2>&1
25 if [ -d "$MNT_DIR" ] ; then
26 umount -f $MNT_DIR 2>/dev/null
27 rmdir $MNT_DIR 2>/dev/null
29 [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
30 [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME >/dev/null 2>&1
31 # FIXME scsi_debug sometimes in-use here
33 rmmod scsi_debug >/dev/null 2>&1
39 if [ -n "$1" ] ; then echo "FAIL $1" ; fi
40 echo "FAILED backtrace:"
41 while caller $frame; do ((frame++)); done
48 echo "TEST SKIPPED: $1"
53 function valgrind_setup()
55 command -v valgrind >/dev/null || fail "Cannot find valgrind."
56 [ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable."
57 export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
60 function valgrind_run()
62 INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
65 function dm_crypt_features()
67 VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
68 [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
70 VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
71 VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
72 VER_PTC=$(echo $VER_STR | cut -f 3 -d.)
74 [ $VER_MAJ -lt 1 ] && return
75 [ $VER_MAJ -gt 1 ] && {
81 [ $VER_MIN -lt 14 ] && return
83 if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
89 modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
90 if [ $? -ne 0 ] ; then
91 echo "This kernel seems to not support proper scsi_debug module, test skipped."
96 DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
98 if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
99 echo "This kernel seems to not support topology info, test skipped."
105 [ -b $DEV ] || fail "Cannot find $DEV."
108 format() # key_bits expected [forced]
110 if [ -z "$3" ] ; then
111 echo -n "Formatting using topology info ($1 bits key)..."
112 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c aes-cbc-essiv:sha256 -s $1 || fail
114 echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
115 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -s $1 -c aes-cbc-essiv:sha256 --align-payload=$3 ||fail
118 # check the device can be activated
119 echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
120 $CRYPTSETUP close $DEV_NAME || fail
122 ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
123 #echo "ALIGN = $ALIGN"
125 [ -z "$ALIGN" ] && fail
126 [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
128 # test some operation, just in case
129 echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --new-key-slot 1
130 [ $? -ne 0 ] && fail "Keyslot add failed."
132 $CRYPTSETUP -q luksKillSlot $DEV 1
133 [ $? -ne 0 ] && fail "Keyslot removal failed."
140 $CRYPTSETUP luksDump $DEV | grep "$1" | cut -s -d ':' -f 2 | sed -e 's/\s//g' -e :a -e N -e 's/\n/:/g' -e 's/\s//g' -e ta
145 if [ $3 -eq 0 ] ; then
146 echo -n "Formatting using topology info ($1 bits key) [slot 0"
147 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 || fail
149 echo -n "Formatting using forced sector alignment $3 ($1 bits key) [slot 0"
150 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 --align-payload=$3 || fail
153 # check the device can be activated
154 echo | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
155 $CRYPTSETUP close $DEV_NAME || fail
157 POFF=$(get_offsets "Payload offset")
158 [ -z "$POFF" ] && fail
159 [ $POFF != $2 ] && fail "Expected data offset differs: expected $2 != detected $POFF"
160 if [ -n "$4" ] ; then
161 for j in 1 2 3 4 5 6 7 ; do
162 echo -e "\n" | $CRYPTSETUP luksAddKey $DEV -q $FAST_PBKDF --new-key-slot $j -c null $PARAMS
167 KOFF=$(get_offsets "Key material offset")
168 [ -z "$KOFF" ] && fail
169 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
175 format_plain() # sector size
177 echo -n "Formatting plain device (sector size $1)..."
178 if [ -n "$DM_SECTOR_SIZE" ] ; then
179 echo $PWD1 | $CRYPTSETUP open --type plain --hash sha256 --sector-size $1 $DEV $DEV_NAME || fail
180 $CRYPTSETUP close $DEV_NAME || fail
187 format_plain_fail() # sector size
189 echo -n "Formatting plain device (sector size $1, must fail)..."
190 if [ -n "$DM_SECTOR_SIZE" ] ; then
191 echo $PWD1 | $CRYPTSETUP open --type plain --hash sha256 --sector-size $1 $DEV $DEV_NAME >/dev/null 2>&1 && fail
198 [ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped."
199 [ -n "$VALG" ] && valgrind_setup && CRYPTSETUP=valgrind_run
200 if [ $(id -u) != 0 ]; then
201 echo "WARNING: You must be root to run this test, test skipped."
206 modprobe --dry-run scsi_debug >/dev/null 2>&1 || skip "This kernel seems to not support proper scsi_debug module, test skipped."
208 if [ -d /sys/module/scsi_debug ] ; then
209 echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
213 echo "# Create desktop-class 4K drive"
214 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
215 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
224 echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
225 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
226 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
235 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
236 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
237 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
244 echo "# Create enterprise-class 4K drive"
245 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
246 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
253 echo "# Create classic 512B drive and stack dm-linear"
254 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
255 add_device dev_size_mb=16 sector_size=512 num_tgts=1
257 DEV=/dev/mapper/$DEV_STACKED
258 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
266 echo "# Create classic 512B drive and stack dm-linear (plain mode)"
267 add_device dev_size_mb=16 sector_size=512 num_tgts=1
269 DEV=/dev/mapper/$DEV_STACKED
270 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
275 format_plain_fail 1111
276 format_plain_fail 8192
277 echo "# Create classic 512B drive, unaligned to 4096 and stack dm-linear (plain mode)"
278 dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
279 dmsetup create $DEV_STACKED --table "0 32762 linear $DEV2 0"
282 format_plain_fail 2048
283 format_plain_fail 4096
286 # skip tests using empty passphrase (LUKS1 cipher_null)
287 if [ ! fips_mode ]; then
288 echo "# Offset check: 512B sector drive"
289 add_device dev_size_mb=16 sector_size=512 num_tgts=1
290 # |k| expO reqO expected slot offsets
291 format_null 128 2048 0 8:136:264:392:520:648:776:904
292 format_null 128 1032 1
293 format_null 128 1032 8
294 format_null 128 1152 128
295 format_null 128 2048 2048
296 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
297 format_null 256 2056 1
298 format_null 256 2056 8
299 format_null 256 2176 128
300 format_null 256 4096 2048
301 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
302 format_null 512 4040 1
303 format_null 512 4040 8
304 format_null 512 4096 128
305 format_null 512 4096 2048
308 echo "# Offset check: 4096B sector drive"
309 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
310 format_null 128 2048 0 8:136:264:392:520:648:776:904
311 format_null 128 1032 1
312 format_null 128 1032 8
313 format_null 128 1152 128
314 format_null 128 2048 2048
315 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
316 format_null 256 2056 1
317 format_null 256 2056 8
318 format_null 256 2176 128
319 format_null 256 4096 2048
320 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
321 format_null 512 4040 1
322 format_null 512 4040 8
323 format_null 512 4096 128
324 format_null 512 4096 2048
328 echo "# Create enterprise-class 4K drive with fs and LUKS images."
329 # loop device here presents 512 block but images have 4k block
330 # cryptsetup should properly use 4k block on direct-io
331 add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
332 for file in $(ls img_fs_*.img.xz) ; do
333 echo "Format using fs image $file."
334 xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
335 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
336 mount $DEV $MNT_DIR || skip "Mounting image is not available."
337 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img || fail
338 echo $PWD2 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail