3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
6 DEV_STACKED="luks0xbabe"
11 FAST_PBKDF="--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() # key_bits expected [forced]
89 echo -n "Formatting using topology info ($1 bits key)..."
90 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c aes-cbc-essiv:sha256 -s $1 || fail
92 echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
93 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -s $1 -c aes-cbc-essiv:sha256 --align-payload=$3 ||fail
96 # check the device can be activated
97 echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
98 $CRYPTSETUP close $DEV_NAME || fail
100 ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
101 #echo "ALIGN = $ALIGN"
103 [ -z "$ALIGN" ] && fail
104 [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
106 # test some operation, just in case
107 echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --key-slot 1
108 [ $? -ne 0 ] && fail "Keyslot add failed."
110 $CRYPTSETUP -q luksKillSlot $DEV 1
111 [ $? -ne 0 ] && fail "Keyslot removal failed."
118 $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
123 if [ $3 -eq 0 ] ; then
124 echo -n "Formatting using topology info ($1 bits key) [slot 0"
125 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 || fail
127 echo -n "Formatting using forced sector alignment $3 ($1 bits key) [slot 0"
128 echo | $CRYPTSETUP luksFormat --type luks1 $DEV -q $FAST_PBKDF -c null -s $1 --align-payload=$3 || fail
131 # check the device can be activated
132 echo | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
133 $CRYPTSETUP close $DEV_NAME || fail
135 POFF=$(get_offsets "Payload offset")
136 [ -z "$POFF" ] && fail
137 [ $POFF != $2 ] && fail "Expected data offset differs: expected $2 != detected $POFF"
138 if [ -n "$4" ] ; then
139 for j in 1 2 3 4 5 6 7 ; do
140 echo -e "\n" | $CRYPTSETUP luksAddKey $DEV -q $FAST_PBKDF --key-slot $j -c null $PARAMS
145 KOFF=$(get_offsets "Key material offset")
146 [ -z "$KOFF" ] && fail
147 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
153 format_plain() # sector size
155 echo -n "Formatting plain device (sector size $1)..."
156 if [ -n "$DM_SECTOR_SIZE" ] ; then
157 echo $PWD1 | $CRYPTSETUP open --type plain --hash sha256 --sector-size $1 $DEV $DEV_NAME || fail
158 $CRYPTSETUP close $DEV_NAME || fail
165 format_plain_fail() # sector size
167 echo -n "Formatting plain device (sector size $1, must fail)..."
168 if [ -n "$DM_SECTOR_SIZE" ] ; then
169 echo $PWD1 | $CRYPTSETUP open --type plain --hash sha256 --sector-size $1 $DEV $DEV_NAME >/dev/null 2>&1 && fail
176 if [ $(id -u) != 0 ]; then
177 echo "WARNING: You must be root to run this test, test skipped."
182 modprobe --dry-run scsi_debug || exit 77
185 echo "# Create desktop-class 4K drive"
186 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
187 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
196 echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
197 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
198 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
207 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
208 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
209 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
216 echo "# Create enterprise-class 4K drive"
217 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
218 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
225 echo "# Create classic 512B drive and stack dm-linear"
226 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
227 add_device dev_size_mb=16 sector_size=512 num_tgts=1
229 DEV=/dev/mapper/$DEV_STACKED
230 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
238 echo "# Create classic 512B drive and stack dm-linear (plain mode)"
239 add_device dev_size_mb=16 sector_size=512 num_tgts=1
241 DEV=/dev/mapper/$DEV_STACKED
242 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
247 format_plain_fail 1111
248 format_plain_fail 8192
249 echo "# Create classic 512B drive, unaligned to 4096 and stack dm-linear (plain mode)"
250 dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
251 dmsetup create $DEV_STACKED --table "0 32762 linear $DEV2 0"
254 format_plain_fail 2048
255 format_plain_fail 4096
258 echo "# Offset check: 512B sector drive"
259 add_device dev_size_mb=16 sector_size=512 num_tgts=1
260 # |k| expO reqO expected slot offsets
261 format_null 64 2048 0 8:72:136:200:264:328:392:456
264 format_null 64 640 128
265 format_null 64 2048 2048
266 format_null 128 2048 0 8:136:264:392:520:648:776:904
267 format_null 128 1032 1
268 format_null 128 1032 8
269 format_null 128 1152 128
270 format_null 128 2048 2048
271 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
272 format_null 256 2056 1
273 format_null 256 2056 8
274 format_null 256 2176 128
275 format_null 256 4096 2048
276 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
277 format_null 512 4040 1
278 format_null 512 4040 8
279 format_null 512 4096 128
280 format_null 512 4096 2048
283 echo "# Offset check: 4096B sector drive"
284 add_device dev_size_mb=16 sector_size=4096 num_tgts=1 opt_blks=64
285 format_null 64 2048 0 8:72:136:200:264:328:392:456
288 format_null 64 640 128
289 format_null 64 2048 2048
290 format_null 128 2048 0 8:136:264:392:520:648:776:904
291 format_null 128 1032 1
292 format_null 128 1032 8
293 format_null 128 1152 128
294 format_null 128 2048 2048
295 format_null 256 4096 0 8:264:520:776:1032:1288:1544:1800
296 format_null 256 2056 1
297 format_null 256 2056 8
298 format_null 256 2176 128
299 format_null 256 4096 2048
300 format_null 512 4096 0 8:512:1016:1520:2024:2528:3032:3536
301 format_null 512 4040 1
302 format_null 512 4040 8
303 format_null 512 4096 128
304 format_null 512 4096 2048
307 echo "# Create enterprise-class 4K drive with fs and LUKS images."
308 # loop device here presents 512 block but images have 4k block
309 # cryptsetup should properly use 4k block on direct-io
310 add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
311 for file in $(ls img_fs_*.img.xz) ; do
312 echo "Format using fs image $file."
313 xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
314 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
315 mount $DEV $MNT_DIR || skip "Mounting image is not available."
316 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img || fail
317 echo $PWD2 | $CRYPTSETUP luksFormat --type luks1 --key-size 256 $FAST_PBKDF $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail