Imported Upstream version 2.3.7
[platform/upstream/cryptsetup.git] / tests / align-test
1 #!/bin/bash
2
3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
5 DEV=""
6 DEV_STACKED="luks0xbabe"
7 DEV_NAME="dummyalign"
8 MNT_DIR="./mnt_luks"
9 PWD1="93R4P4pIqAH8"
10 PWD2="mymJeD8ivEhE"
11 FAST_PBKDF="--pbkdf-force-iterations 1000"
12
13 cleanup() {
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
18         fi
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
22         sleep 1
23         rmmod scsi_debug 2>/dev/null
24         sleep 1
25 }
26
27 fail()
28 {
29         if [ -n "$1" ] ; then echo "FAIL $1" ; fi
30         echo "FAILED backtrace:"
31         while caller $frame; do ((frame++)); done
32         cleanup
33         exit 100
34 }
35
36 skip()
37 {
38         echo "TEST SKIPPED: $1"
39         cleanup
40         exit 0
41 }
42
43 function dm_crypt_features()
44 {
45         VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
46         [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
47
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.)
51
52         [ $VER_MAJ -lt 1 ] && return
53         [ $VER_MAJ -gt 1 ] && {
54                 DM_PERF_CPU=1
55                 DM_SECTOR_SIZE=1
56                 return
57         }
58
59         [ $VER_MIN -lt 14 ] && return
60         DM_PERF_CPU=1
61         if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
62                 DM_SECTOR_SIZE=1
63         fi
64 }
65
66 add_device() {
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."
70                 exit 77
71         fi
72
73         sleep 2
74         DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
75
76         if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
77                 echo "This kernel seems to not support topology info, test skipped."
78                 cleanup
79                 exit 77
80         fi
81
82         DEV="/dev/$DEV"
83         [ -b $DEV ] || fail "Cannot find $DEV."
84 }
85
86 format() # key_bits expected [forced]
87 {
88         if [ -z "$3" ] ; then
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
91         else
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
94         fi
95
96         # check the device can be activated
97         echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
98         $CRYPTSETUP close $DEV_NAME || fail
99
100         ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
101         #echo "ALIGN = $ALIGN"
102
103         [ -z "$ALIGN" ] && fail
104         [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
105
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."
109
110         $CRYPTSETUP -q luksKillSlot $DEV 1
111         [ $? -ne 0 ] && fail "Keyslot removal failed."
112
113         echo "PASSED"
114 }
115
116 get_offsets()
117 {
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
119 }
120
121 format_null()
122 {
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
126         else
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
129         fi
130
131         # check the device can be activated
132         echo | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
133         $CRYPTSETUP close $DEV_NAME || fail
134
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
141                         echo -n $j
142                         [ $? -ne 0 ] && fail
143                 done
144
145                 KOFF=$(get_offsets "Key material offset")
146                 [ -z "$KOFF" ] && fail
147                 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
148         fi
149
150         echo "]...PASSED"
151 }
152
153 format_plain() # sector size
154 {
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
159                 echo "PASSED"
160         else
161                 echo "N/A"
162         fi
163 }
164
165 format_plain_fail() # sector size
166 {
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
170                 echo "PASSED"
171         else
172                 echo "N/A"
173         fi
174 }
175
176 if [ $(id -u) != 0 ]; then
177         echo "WARNING: You must be root to run this test, test skipped."
178         exit 77
179 fi
180
181 dm_crypt_features
182 modprobe --dry-run scsi_debug || exit 77
183 cleanup
184
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
188 format 256 4096
189 format 256 2056 8
190 format 128 2048
191 format 128 1032 8
192 format 256 8192 8192
193 format 128 8192 8192
194 cleanup
195
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
199 format 256 4096
200 format 256 2056 8
201 format 128 2048
202 format 128 1032 8
203 format 256 8192 8192
204 format 128 8192 8192
205 cleanup
206
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
210 format 256 4103
211 format 256 2056 8
212 format 128 2055
213 format 128 1032 8
214 cleanup
215
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
219 format 256 4096
220 format 256 2056 8
221 format 128 2048
222 format 128 1032 8
223 cleanup
224
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
228 DEV2=$DEV
229 DEV=/dev/mapper/$DEV_STACKED
230 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
231 format 256 4096
232 format 256 2056 8
233 format 128 2048
234 format 128 1032 8
235 format 128 8192 8192
236 cleanup
237
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
240 DEV2=$DEV
241 DEV=/dev/mapper/$DEV_STACKED
242 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
243 format_plain 512
244 format_plain 1024
245 format_plain 2048
246 format_plain 4096
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"
252 format_plain 512
253 format_plain 1024
254 format_plain_fail 2048
255 format_plain_fail 4096
256 cleanup
257
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
262 format_null  64  520    1
263 format_null  64  520    8
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
281 cleanup
282
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
286 format_null  64  520    1
287 format_null  64  520    8
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
305 cleanup
306
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
318     umount $MNT_DIR
319 done
320 cleanup