Imported Upstream version 2.6.1
[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 FIPS_MODE=$(cat /proc/sys/crypto/fips_enabled 2>/dev/null)
14
15 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
16 CRYPTSETUP_LIB_VALGRIND=../.libs
17
18 function fips_mode()
19 {
20         [ -n "$FIPS_MODE" ] && [ "$FIPS_MODE" -gt 0 ]
21 }
22
23 cleanup() {
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
28         fi
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
32         sleep 1
33         rmmod scsi_debug >/dev/null 2>&1
34         sleep 1
35 }
36
37 fail()
38 {
39         if [ -n "$1" ] ; then echo "FAIL $1" ; fi
40         echo "FAILED backtrace:"
41         while caller $frame; do ((frame++)); done
42         cleanup
43         exit 100
44 }
45
46 skip()
47 {
48         echo "TEST SKIPPED: $1"
49         cleanup
50         exit 77
51 }
52
53 function valgrind_setup()
54 {
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"
58 }
59
60 function valgrind_run()
61 {
62         INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
63 }
64
65 function dm_crypt_features()
66 {
67         VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
68         [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
69
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.)
73
74         [ $VER_MAJ -lt 1 ] && return
75         [ $VER_MAJ -gt 1 ] && {
76                 DM_PERF_CPU=1
77                 DM_SECTOR_SIZE=1
78                 return
79         }
80
81         [ $VER_MIN -lt 14 ] && return
82         DM_PERF_CPU=1
83         if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
84                 DM_SECTOR_SIZE=1
85         fi
86 }
87
88 add_device() {
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."
92                 exit 77
93         fi
94
95         sleep 2
96         DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
97
98         if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
99                 echo "This kernel seems to not support topology info, test skipped."
100                 cleanup
101                 exit 77
102         fi
103
104         DEV="/dev/$DEV"
105         [ -b $DEV ] || fail "Cannot find $DEV."
106 }
107
108 format() # key_bits expected [forced]
109 {
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
113         else
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
116         fi
117
118         # check the device can be activated
119         echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
120         $CRYPTSETUP close $DEV_NAME || fail
121
122         ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
123         #echo "ALIGN = $ALIGN"
124
125         [ -z "$ALIGN" ] && fail
126         [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
127
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."
131
132         $CRYPTSETUP -q luksKillSlot $DEV 1
133         [ $? -ne 0 ] && fail "Keyslot removal failed."
134
135         echo "PASSED"
136 }
137
138 get_offsets()
139 {
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
141 }
142
143 format_null()
144 {
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
148         else
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
151         fi
152
153         # check the device can be activated
154         echo | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
155         $CRYPTSETUP close $DEV_NAME || fail
156
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
163                         echo -n $j
164                         [ $? -ne 0 ] && fail
165                 done
166
167                 KOFF=$(get_offsets "Key material offset")
168                 [ -z "$KOFF" ] && fail
169                 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
170         fi
171
172         echo "]...PASSED"
173 }
174
175 format_plain() # sector size
176 {
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
181                 echo "PASSED"
182         else
183                 echo "N/A"
184         fi
185 }
186
187 format_plain_fail() # sector size
188 {
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
192                 echo "PASSED"
193         else
194                 echo "N/A"
195         fi
196 }
197
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."
202         exit 77
203 fi
204
205 dm_crypt_features
206 modprobe --dry-run scsi_debug >/dev/null 2>&1 || skip "This kernel seems to not support proper scsi_debug module, test skipped."
207 cleanup
208 if [ -d /sys/module/scsi_debug ] ; then
209         echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
210         exit 77
211 fi
212
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
216 format 256 4096
217 format 256 2056 8
218 format 128 2048
219 format 128 1032 8
220 format 256 8192 8192
221 format 128 8192 8192
222 cleanup
223
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
227 format 256 4096
228 format 256 2056 8
229 format 128 2048
230 format 128 1032 8
231 format 256 8192 8192
232 format 128 8192 8192
233 cleanup
234
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
238 format 256 4103
239 format 256 2056 8
240 format 128 2055
241 format 128 1032 8
242 cleanup
243
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
247 format 256 4096
248 format 256 2056 8
249 format 128 2048
250 format 128 1032 8
251 cleanup
252
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
256 DEV2=$DEV
257 DEV=/dev/mapper/$DEV_STACKED
258 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
259 format 256 4096
260 format 256 2056 8
261 format 128 2048
262 format 128 1032 8
263 format 128 8192 8192
264 cleanup
265
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
268 DEV2=$DEV
269 DEV=/dev/mapper/$DEV_STACKED
270 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
271 format_plain 512
272 format_plain 1024
273 format_plain 2048
274 format_plain 4096
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"
280 format_plain 512
281 format_plain 1024
282 format_plain_fail 2048
283 format_plain_fail 4096
284 cleanup
285
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
306 cleanup
307
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
325 cleanup
326 fi
327
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
339         umount $MNT_DIR
340 done
341 cleanup