| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Test cryptsetup/authenticated encryption compatibility. |
| 4 | # |
| 5 | [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".." |
| 6 | CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup |
| 7 | DEV_NAME=dmi_test |
| 8 | DEV_NAME2=dmi_test_xdif |
| 9 | DEV=mode-test.img |
| 10 | HEADER_IMG=mode-test-detached.img |
| 11 | PWD1=nHjJHjI23JK |
| 12 | KEY_FILE=key.img |
| 13 | FAST_PBKDF_OPT="--pbkdf pbkdf2 --pbkdf-force-iterations 1000" |
| 14 | |
| 15 | if [ -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 16 | CRYPTSETUP_VALGRIND=$CRYPTSETUP |
| 17 | else |
| 18 | CRYPTSETUP_VALGRIND=../.libs/cryptsetup |
| 19 | CRYPTSETUP_LIB_VALGRIND=../.libs |
| 20 | fi |
| 21 | |
| 22 | dmremove() { # device |
| 23 | udevadm settle >/dev/null 2>&1 |
| 24 | dmsetup remove --retry $1 >/dev/null 2>&1 |
| 25 | } |
| 26 | |
| 27 | cleanup() { |
| 28 | [ -b /dev/mapper/$DEV_NAME ] && dmremove $DEV_NAME |
| 29 | [ -b /dev/mapper/$DEV_NAME2 ] && dmremove $DEV_NAME2 |
| 30 | [ -b /dev/mapper/"$DEV_NAME"_dif ] && dmremove "$DEV_NAME"_dif |
| 31 | [ -n "$DEV_LOOP" ] && losetup -d "$DEV_LOOP" |
| 32 | DEV_LOOP="" |
| 33 | rm -f $DEV $KEY_FILE $HEADER_IMG >/dev/null 2>&1 |
| 34 | } |
| 35 | |
| 36 | fail() |
| 37 | { |
| 38 | echo |
| 39 | [ -n "$1" ] && echo "FAIL: $1" |
| 40 | echo "FAILED backtrace:" |
| 41 | while caller $frame; do ((frame++)); done |
| 42 | cleanup |
| 43 | exit 100 |
| 44 | } |
| 45 | |
| 46 | _sigchld() { local c=$?; [ $c -eq 139 ] && fail "Segfault"; [ $c -eq 134 ] && fail "Aborted"; } |
| 47 | trap _sigchld CHLD |
| 48 | |
| 49 | skip() |
| 50 | { |
| 51 | [ -n "$1" ] && echo "$1" |
| 52 | exit 77 |
| 53 | } |
| 54 | |
| 55 | valgrind_setup() |
| 56 | { |
| 57 | command -v valgrind >/dev/null || fail "Cannot find valgrind." |
| 58 | [ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable." |
| 59 | [ ! -f valg.sh ] && fail "Unable to get location of valg runner script." |
| 60 | if [ -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 61 | export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH" |
| 62 | fi |
| 63 | } |
| 64 | |
| 65 | valgrind_run() |
| 66 | { |
| 67 | INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@" |
| 68 | } |
| 69 | |
| 70 | dm_integrity_inline_support() |
| 71 | { |
| 72 | VER_STR=$(dmsetup targets | grep integrity | cut -f2 -dv) |
| 73 | [ -z "$VER_STR" ] && fail "Failed to parse dm-integrity version." |
| 74 | |
| 75 | VER_MAJ=$(echo $VER_STR | cut -f 1 -d.) |
| 76 | VER_MIN=$(echo $VER_STR | cut -f 2 -d.) |
| 77 | |
| 78 | [ $VER_MAJ -gt 1 ] && return 0 |
| 79 | if [ $VER_MIN -ge 12 ]; then |
| 80 | return 0 |
| 81 | fi |
| 82 | return 1 |
| 83 | } |
| 84 | |
| 85 | add_device() { |
| 86 | cleanup |
| 87 | dd if=/dev/urandom of=$KEY_FILE bs=1 count=512 >/dev/null 2>&1 |
| 88 | dd if=/dev/zero of=$DEV bs=1M count=32 >/dev/null 2>&1 |
| 89 | IDEV=$DEV |
| 90 | INLINE_PARAMS="" |
| 91 | } |
| 92 | |
| 93 | add_device_inline() { |
| 94 | add_device |
| 95 | DEV_LOOP=$(losetup -f $DEV --show) |
| 96 | [ -z "$DEV_LOOP" ] && fail |
| 97 | dmsetup create $DEV_NAME2 --table "0 32768 integrity $DEV_LOOP 0 64 J 2 block_size:4096 fix_padding" |
| 98 | [ ! -b /dev/mapper/$DEV_NAME2 ] && fail |
| 99 | IDEV=/dev/mapper/$DEV_NAME2 |
| 100 | INLINE_PARAMS="--integrity-inline " |
| 101 | } |
| 102 | |
| 103 | set_LO_DEV() { # file |
| 104 | # support both /dev/loopX and /dev/loop/X |
| 105 | LO_DEV=$(losetup -l -O NAME -n -j $1 2>/dev/null | sed -e 's/loop\//loop/') |
| 106 | } |
| 107 | |
| 108 | status_check() # name value [detached] |
| 109 | { |
| 110 | if [ -n "$3" ]; then |
| 111 | PARAMS="$DEV_NAME --header $HEADER_IMG" |
| 112 | else |
| 113 | PARAMS="$DEV_NAME" |
| 114 | fi |
| 115 | |
| 116 | X=$($CRYPTSETUP status $PARAMS | grep -m1 "$1" | sed -e 's/.*:[ \t]\+//' | cut -d' ' -f1) |
| 117 | if [ "$X" != "$2" ] ; then |
| 118 | echo "[status FAIL]" |
| 119 | echo " Expecting $1:$2 got \"$X\"." |
| 120 | fail |
| 121 | fi |
| 122 | } |
| 123 | |
| 124 | dump_check() # name value |
| 125 | { |
| 126 | X=$($CRYPTSETUP luksDump $IDEV | grep -m1 "$1" | sed -e 's/.*:[ \t]\+//' | cut -d' ' -f1) |
| 127 | if [ "$X" != "$2" ] ; then |
| 128 | echo "[dump FAIL]" |
| 129 | echo " Expecting $1:$2 got \"$X\"." |
| 130 | fail |
| 131 | fi |
| 132 | } |
| 133 | |
| 134 | int_check_sum() # alg checksum |
| 135 | { |
| 136 | VSUM=$(sha256sum /dev/mapper/$DEV_NAME | cut -d' ' -f 1) |
| 137 | if [ "$VSUM" = "$2" ] ; then |
| 138 | echo -n "[CHECKSUM]" |
| 139 | else |
| 140 | echo "[FAIL]" |
| 141 | echo " Expecting $2 got $VSUM." |
| 142 | fail |
| 143 | fi |
| 144 | } |
| 145 | |
| 146 | int_error_detection() # [test_hdr] |
| 147 | { |
| 148 | echo -n "[DETECT_CORRUPTION]" |
| 149 | echo -n "XXXXX" | dd of=$IDEV bs=1M seek=8 count=1 conv=notrunc >/dev/null 2>&1 || fail "Cannot write to device." |
| 150 | if [ -n "$1" ] ; then |
| 151 | $CRYPTSETUP open -d $KEY_FILE $IDEV --header $1 $DEV_NAME || fail "Cannot activate device." |
| 152 | else |
| 153 | $CRYPTSETUP open -d $KEY_FILE $IDEV $DEV_NAME || fail "Cannot activate device." |
| 154 | fi |
| 155 | dd if=/dev/mapper/$DEV_NAME of=/dev/null >/dev/null 2>&1 && fail "Error detection failed." |
| 156 | $CRYPTSETUP close $DEV_NAME || fail "Cannot deactivate device." |
| 157 | } |
| 158 | |
| 159 | intformat() # alg integrity integrity_out key_size int_key_size sector_size csum [test_hdr] |
| 160 | { |
| 161 | echo -n "[$1:$2:$4:$6:$5]" |
| 162 | echo -n "[FORMAT]" |
| 163 | |
| 164 | # just trick, if int key size is not multiple of 16, use explicit flag |
| 165 | if [ $(($5 % 16)) -eq 0 ]; then |
| 166 | INT_PARAMS="--integrity $2 --integrity-legacy-padding" |
| 167 | else |
| 168 | INT_PARAMS="--integrity $2 --integrity-key-size $5 --integrity-legacy-padding" |
| 169 | fi |
| 170 | |
| 171 | $CRYPTSETUP luksFormat --type luks2 -q -c $1 $INT_PARAMS $INLINE_PARAMS --sector-size $6 -s $4 \ |
| 172 | $FAST_PBKDF_OPT -d $KEY_FILE $IDEV --offset 8192 >/dev/null 2>&1 |
| 173 | if [ $? -ne 0 ] ; then |
| 174 | echo "[N/A]" |
| 175 | return |
| 176 | fi |
| 177 | dump_check "cipher" $1 |
| 178 | dump_check "sector" $6 |
| 179 | dump_check "integrity" $3 |
| 180 | dump_check "Key:" $(($4 + $5)) |
| 181 | echo -n "[ACTIVATE]" |
| 182 | $CRYPTSETUP open -d $KEY_FILE $IDEV $DEV_NAME || fail "Cannot activate device." |
| 183 | set_LO_DEV $IDEV |
| 184 | status_check "cipher" $1 |
| 185 | status_check "sector size" $6 |
| 186 | status_check "integrity:" $3 |
| 187 | status_check "keysize:" $(($4 + $5)) |
| 188 | [ -n "$LO_DEV" ] && status_check "device:" $LO_DEV |
| 189 | [ $5 -gt 0 ] && status_check "integrity keysize:" $5 |
| 190 | int_check_sum $1 $7 |
| 191 | echo -n "[SUSPEND]" |
| 192 | $CRYPTSETUP luksSuspend $DEV_NAME || fail "Cannot suspend device." |
| 193 | dmsetup info $DEV_NAME | grep -q SUSPENDED || fail "Not suspended." |
| 194 | if [ -z "$INLINE_PARAMS" ]; then |
| 195 | dmsetup info "$DEV_NAME"_dif | grep -q SUSPENDED || fail "Not suspended." |
| 196 | else |
| 197 | # this hw-inline device must not be suspended |
| 198 | dmsetup info "$DEV_NAME2" | grep -q ACTIVE || fail |
| 199 | $CRYPTSETUP status $DEV_NAME | grep -q "(inline HW tags)" || fail "(inline hw tags) missing." |
| 200 | fi |
| 201 | echo -n "[RESUME]" |
| 202 | $CRYPTSETUP luksResume -d $KEY_FILE $DEV_NAME || fail "Cannot resume device." |
| 203 | dmsetup info $DEV_NAME | grep -q ACTIVE || fail "Not resumed." |
| 204 | if [ -z "$INLINE_PARAMS" ]; then |
| 205 | dmsetup info "$DEV_NAME"_dif | grep -q ACTIVE || fail "Not resumed." |
| 206 | fi |
| 207 | echo -n "[REMOVE]" |
| 208 | $CRYPTSETUP close $DEV_NAME || fail "Cannot deactivate device." |
| 209 | [ -b /dev/mapper/"$DEV_NAME"_dif ] && fail |
| 210 | int_error_detection |
| 211 | |
| 212 | # check detached header activation |
| 213 | if [ -n "$8" ] ; then |
| 214 | echo -n "[DETACHED_HDR]" |
| 215 | wipefs -a $IDEV >/dev/null 2>&1 || fail |
| 216 | $CRYPTSETUP luksFormat --type luks2 -q -c $1 $INT_PARAMS $INLINE_PARAMS --sector-size $6 -s $4 \ |
| 217 | $FAST_PBKDF_OPT -d $KEY_FILE $IDEV --header $HEADER_IMG --offset 8192 >/dev/null 2>&1 |
| 218 | $CRYPTSETUP open --header $HEADER_IMG -d $KEY_FILE $IDEV $DEV_NAME || fail "Cannot activate device." |
| 219 | set_LO_DEV $IDEV |
| 220 | status_check "cipher" $1 1 |
| 221 | status_check "sector size" $6 1 |
| 222 | status_check "integrity:" $3 1 |
| 223 | status_check "keysize:" $(($4 + $5)) 1 |
| 224 | [ -n "$LO_DEV" ] && status_check "device:" $LO_DEV 1 |
| 225 | [ $5 -gt 0 ] && status_check "integrity keysize:" $5 1 |
| 226 | int_check_sum $1 $7 |
| 227 | # check status returns values even if no --header is set |
| 228 | status_check "cipher" $1 |
| 229 | status_check "sector size" $6 |
| 230 | status_check "integrity:" $3 |
| 231 | status_check "keysize:" $(($4 + $5)) |
| 232 | [ -n "$LO_DEV" ] && status_check "device:" $LO_DEV |
| 233 | [ $5 -gt 0 ] && status_check "integrity keysize:" $5 |
| 234 | if [ -n "$INLINE_PARAMS" ]; then |
| 235 | $CRYPTSETUP status $DEV_NAME --header $HEADER_IMG | grep -q "(inline HW tags)" || fail "(inline hw tags) missing." |
| 236 | fi |
| 237 | $CRYPTSETUP close $DEV_NAME || fail "Cannot deactivate device." |
| 238 | [ -b /dev/mapper/"$DEV_NAME"_dif ] && fail |
| 239 | int_error_detection $HEADER_IMG |
| 240 | rm -f $HEADER_IMG |
| 241 | fi |
| 242 | |
| 243 | echo "[OK]" |
| 244 | } |
| 245 | |
| 246 | [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped." |
| 247 | [ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped." |
| 248 | [ -n "$VALG" ] && valgrind_setup && CRYPTSETUP=valgrind_run |
| 249 | modprobe dm-integrity >/dev/null 2>&1 |
| 250 | dmsetup targets | grep integrity >/dev/null 2>&1 || skip "Cannot find dm-integrity target, test skipped." |
| 251 | command -v wipefs >/dev/null || skip "Cannot find wipefs, test skipped." |
| 252 | |
| 253 | echo "LUKS2 authenticated mode test" |
| 254 | add_device |
| 255 | |
| 256 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 128 256 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c 1 |
| 257 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 256 256 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c 1 |
| 258 | intformat aes-xts-random hmac-sha256 hmac\(sha256\) 256 256 512 492c2d1cc9e222a850c399bfef4ed5a86bf5afc59e54f0f0c7ba8e2a64548323 |
| 259 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 256 256 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c |
| 260 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 512 256 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c |
| 261 | intformat aes-xts-random hmac-sha256 hmac\(sha256\) 512 256 512 492c2d1cc9e222a850c399bfef4ed5a86bf5afc59e54f0f0c7ba8e2a64548323 |
| 262 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 128 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 263 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 256 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 264 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 256 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b 1 |
| 265 | intformat aes-xts-random hmac-sha256 hmac\(sha256\) 256 256 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda |
| 266 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 256 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 267 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 512 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 268 | intformat aes-xts-random hmac-sha256 hmac\(sha256\) 512 256 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda |
| 269 | |
| 270 | intformat aes-cbc-essiv:sha256 hmac-sha512 hmac\(sha512\) 256 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd |
| 271 | intformat aes-xts-essiv:sha256 hmac-sha512 hmac\(sha512\) 512 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd |
| 272 | intformat aes-xts-plain64 hmac-sha512 hmac\(sha512\) 512 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd |
| 273 | intformat aes-xts-random hmac-sha512 hmac\(sha512\) 512 512 4096 621f6c03f7361c2bf8f10059ae822339223f8471c750b0cf8584fba7134bd4a2 |
| 274 | |
| 275 | intformat aes-xts-plain64 hmac-sha1 hmac\(sha1\) 512 160 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e |
| 276 | intformat aes-xts-random hmac-sha1 hmac\(sha1\) 512 160 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda |
| 277 | |
| 278 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 512 264 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c |
| 279 | intformat aes-xts-plain64 hmac-sha512 hmac\(sha512\) 512 792 512 9040d276d8bfab30bbc4bf389e152e08c13ac6fa84d49d11c1bee6e1638fd8f1 |
| 280 | |
| 281 | intformat aes-gcm-random aead aead 128 0 512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57 |
| 282 | intformat aes-gcm-random aead aead 128 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 283 | intformat aes-gcm-random aead aead 256 0 512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57 |
| 284 | intformat aes-gcm-random aead aead 256 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 285 | |
| 286 | intformat aes-ccm-random aead aead 152 0 512 288e5e9bc5be6c0bd2a74abbb72c7944da83198b5e3041dcf159e7ae250dafa8 |
| 287 | intformat aes-ccm-random aead aead 152 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e |
| 288 | intformat aes-ccm-random aead aead 280 0 512 288e5e9bc5be6c0bd2a74abbb72c7944da83198b5e3041dcf159e7ae250dafa8 |
| 289 | intformat aes-ccm-random aead aead 280 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e |
| 290 | |
| 291 | intformat chacha20-plain64 poly1305 poly1305 256 0 512 3f82eae753ff52a689ddc559c691bbdff838361bbe9a3ce8c7212e16e51b5dbe |
| 292 | intformat chacha20-random poly1305 poly1305 256 0 512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57 |
| 293 | intformat chacha20-plain64 poly1305 poly1305 256 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e |
| 294 | intformat chacha20-random poly1305 poly1305 256 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b |
| 295 | |
| 296 | intformat aegis128-random aead aead 128 0 512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c 1 |
| 297 | intformat aegis128-random aead aead 128 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b 1 |
| 298 | |
| 299 | if dm_integrity_inline_support; then |
| 300 | echo "LUKS2 authenticated mode test (inline tags)" |
| 301 | add_device_inline |
| 302 | |
| 303 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 128 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 304 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 256 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 305 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 256 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 1 |
| 306 | intformat aes-xts-random hmac-sha256 hmac\(sha256\) 256 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 307 | intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 256 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 308 | intformat aes-xts-plain64 hmac-sha256 hmac\(sha256\) 512 256 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 309 | |
| 310 | intformat aes-gcm-random aead aead 128 0 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 311 | |
| 312 | intformat chacha20-plain64 poly1305 poly1305 256 0 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 313 | intformat chacha20-random poly1305 poly1305 256 0 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 |
| 314 | |
| 315 | intformat aegis128-random aead aead 128 0 4096 cfadd44a103cbd6d5726fa07b27d7aad2f67ed3930ff96901c486a5beaf7e723 1 |
| 316 | fi |
| 317 | |
| 318 | cleanup |