| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # Test mode compatibility, check input + kernel and cryptsetup cipher status |
| 4 | # |
| 5 | [scip-shell system . . `[`. -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".." |
| 6 | CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup |
| 7 | DEV_NAME=dmc_test |
| 8 | HEADER_IMG=mode-test.img |
| 9 | PASSWORD=3xrododenron |
| 10 | PASSWORD1=$PASSWORD |
| 11 | KEY="7c0dc5dfd0c9191381d92e6ebb3b29e7f0dba53b0de132ae23f5726727173540" |
| 12 | FAST_PBKDF2="--pbkdf pbkdf2 --pbkdf-force-iterations 1000" |
| 13 | |
| 14 | # cipher-chainmode-ivopts:ivmode |
| 15 | CIPHERS="aes twofish serpent" |
| 16 | MODES="cbc lrw xts" |
| 17 | IVMODES="null benbi plain plain64 essiv:sha256" |
| 18 | |
| 19 | LOOPDEV=$(losetupscip-shell system . . losetup. -f 2>/dev/nullscip-shell filesystem . . `/dev/null`.) |
| 20 | |
| 21 | if [scip-shell system . . `[`. -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 22 | CRYPTSETUP_VALGRIND=$CRYPTSETUP |
| 23 | else |
| 24 | CRYPTSETUP_VALGRIND=../.libs/cryptsetup |
| 25 | CRYPTSETUP_LIB_VALGRIND=../.libs |
| 26 | fi |
| 27 | |
| 28 | dmremovescip-shell shell shell-project 0.0.0 dmremove().() { # device |
| 29 | udevadm settle >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 30 | dmsetup remove --retry $1 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 31 | } |
| 32 | |
| 33 | cleanupscip-shell shell shell-project 0.0.0 cleanup().() { |
| 34 | [scip-shell system . . `[`. -b /dev/mapper/"$DEV_NAME"_tstdev ] && dmremovescip-shell shell shell-project 0.0.0 dmremove(). "$DEV_NAME"_tstdev |
| 35 | [scip-shell system . . `[`. -b /dev/mapper/$DEV_NAME ] && dmremovescip-shell shell shell-project 0.0.0 dmremove(). $DEV_NAME |
| 36 | losetupscip-shell system . . losetup. -d $LOOPDEV >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 37 | rmscip-shell system . . rm. -f $HEADER_IMG >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 38 | } |
| 39 | |
| 40 | failscip-shell shell shell-project 0.0.0 fail().() |
| 41 | { |
| 42 | [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1" |
| 43 | echoscip-shell system . . echo. "FAILED backtrace:" |
| 44 | while caller $frame; do ((frame++)); done |
| 45 | cleanupscip-shell shell shell-project 0.0.0 cleanup(). |
| 46 | exit 100 |
| 47 | } |
| 48 | |
| 49 | _sigchldscip-shell shell shell-project 0.0.0 _sigchld().() { local c=$?; [scip-shell system . . `[`. $c -eq 139 ] && failscip-shell shell shell-project 0.0.0 fail(). "Segfault"; [scip-shell system . . `[`. $c -eq 134 ] && failscip-shell shell shell-project 0.0.0 fail(). "Aborted"; } |
| 50 | trap _sigchld CHLD |
| 51 | |
| 52 | skipscip-shell shell shell-project 0.0.0 skip().() |
| 53 | { |
| 54 | [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1" |
| 55 | exit 77 |
| 56 | } |
| 57 | |
| 58 | dm_crypt_capi_supportscip-shell shell shell-project 0.0.0 dm_crypt_capi_support().() |
| 59 | { |
| 60 | VER_STR=$(dmsetup targets | grepscip-shell system . . grep. crypt | cutscip-shell system . . cut. -f2 -dv) |
| 61 | [scip-shell system . . `[`. -z "$VER_STR" ] && failscip-shell shell shell-project 0.0.0 fail(). "Failed to parse dm-crypt version." |
| 62 | |
| 63 | VER_MAJ=$(echoscip-shell system . . echo. $VER_STR | cutscip-shell system . . cut. -f 1 -d.) |
| 64 | VER_MIN=$(echoscip-shell system . . echo. $VER_STR | cutscip-shell system . . cut. -f 2 -d.) |
| 65 | VER_PTC=$(echoscip-shell system . . echo. $VER_STR | cutscip-shell system . . cut. -f 3 -d.) |
| 66 | |
| 67 | [scip-shell system . . `[`. $VER_MAJ -gt 1 ] && return 0 |
| 68 | if [scip-shell system . . `[`. $VER_MIN -ge 16 ]; then |
| 69 | return 0 |
| 70 | fi |
| 71 | |
| 72 | return 1 |
| 73 | } |
| 74 | |
| 75 | valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup().() |
| 76 | { |
| 77 | command -v valgrind >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot find valgrind." |
| 78 | [scip-shell system . . `[`. ! -f $CRYPTSETUP_VALGRIND ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of cryptsetup executable." |
| 79 | [scip-shell system . . `[`. ! -f valg.sh ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of valg runner script." |
| 80 | if [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 81 | export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH" |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | valgrind_runscip-shell shell shell-project 0.0.0 valgrind_run().() |
| 86 | { |
| 87 | INFOSTRING="$(basenamescip-shell system . . basename. ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@" |
| 88 | } |
| 89 | |
| 90 | |
| 91 | add_devicescip-shell shell shell-project 0.0.0 add_device().() { |
| 92 | cleanupscip-shell shell shell-project 0.0.0 cleanup(). |
| 93 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=$HEADER_IMG bs=1M count=6 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 94 | syncscip-shell system . . sync. |
| 95 | losetupscip-shell system . . losetup. $LOOPDEV $HEADER_IMG >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 96 | dmsetup create $DEV_NAME --table "0 10240 linear $LOOPDEV 8" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 97 | } |
| 98 | |
| 99 | dmcrypt_checkscip-shell shell shell-project 0.0.0 dmcrypt_check().() # device outstring |
| 100 | { |
| 101 | X=$(dmsetup table $1 2>/dev/nullscip-shell filesystem . . `/dev/null`. | sedscip-shell system . . sed. 's/.*: //' | cutscip-shell system . . cut. -d' ' -f 4) |
| 102 | if [scip-shell system . . `[`. "$X" = $2 ] ; then |
| 103 | echoscip-shell system . . echo. -n "[table OK]" |
| 104 | else |
| 105 | echoscip-shell system . . echo. "[table FAIL]" |
| 106 | echoscip-shell system . . echo. " Expecting $2 got $X." |
| 107 | failscip-shell shell shell-project 0.0.0 fail(). |
| 108 | fi |
| 109 | |
| 110 | X=$($CRYPTSETUP status $1 | grepscip-shell system . . grep. cipher: | sedscip-shell system . . sed. s/\.\*cipher:\\s*//) |
| 111 | if [scip-shell system . . `[`. $X = $2 ] ; then |
| 112 | echoscip-shell system . . echo. -n "[status OK]" |
| 113 | else |
| 114 | echoscip-shell system . . echo. "[status FAIL]" |
| 115 | echoscip-shell system . . echo. " Expecting $2 got \"$X\"." |
| 116 | failscip-shell shell shell-project 0.0.0 fail(). |
| 117 | fi |
| 118 | |
| 119 | dmremovescip-shell shell shell-project 0.0.0 dmremove(). $1 |
| 120 | } |
| 121 | |
| 122 | dmcrypt_check_sumscip-shell shell shell-project 0.0.0 dmcrypt_check_sum().() # cipher device |
| 123 | { |
| 124 | EXPSUM="c036cbb7553a909f8b8877d4461924307f27ecb66cff928eeeafd569c3887e29" |
| 125 | # Fill device with zeroes and reopen it |
| 126 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=/dev/mapper/$2 bs=1M count=6 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 127 | syncscip-shell system . . sync. |
| 128 | dmremovescip-shell shell shell-project 0.0.0 dmremove(). $2 |
| 129 | |
| 130 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c $1 -s 256 $2 /dev/mapper/$DEV_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 131 | ret=$? |
| 132 | VSUM=$(sha256sumscip-shell system . . sha256sum. /dev/mapper/$2 | cutscip-shell system . . cut. -d' ' -f 1) |
| 133 | if [scip-shell system . . `[`. $ret -eq 0 -a "$VSUM" = "$EXPSUM" ] ; then |
| 134 | echoscip-shell system . . echo. -n "[OK]" |
| 135 | else |
| 136 | echoscip-shell system . . echo. "[FAIL]" |
| 137 | echoscip-shell system . . echo. " Expecting $EXPSUM got $VSUM." |
| 138 | failscip-shell shell shell-project 0.0.0 fail(). |
| 139 | fi |
| 140 | |
| 141 | dmremovescip-shell shell shell-project 0.0.0 dmremove(). $2 |
| 142 | } |
| 143 | |
| 144 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt().() |
| 145 | { |
| 146 | OUT=$2 |
| 147 | [scip-shell system . . `[`. -z "$OUT" ] && OUT=$1 |
| 148 | printfscip-shell system . . printf. "%-31s" "$1" |
| 149 | |
| 150 | echoscip-shell system . . echo. -n -e "PLAIN:" |
| 151 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c $1 -s 256 "$DEV_NAME"_tstdev /dev/mapper/$DEV_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 152 | if [scip-shell system . . `[`. $? -eq 0 ] ; then |
| 153 | dmcrypt_checkscip-shell shell shell-project 0.0.0 dmcrypt_check(). "$DEV_NAME"_tstdev $OUT |
| 154 | else |
| 155 | echoscip-shell system . . echo. -n "[N/A]" |
| 156 | fi |
| 157 | |
| 158 | echoscip-shell system . . echo. -n -e " LUKS1:" |
| 159 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP luksFormat --type luks1 $FAST_PBKDF2 -c $1 -s 256 /dev/mapper/$DEV_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 160 | if [scip-shell system . . `[`. $? -eq 0 ] ; then |
| 161 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP luksOpen /dev/mapper/$DEV_NAME "$DEV_NAME"_tstdev >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). |
| 162 | dmcrypt_checkscip-shell shell shell-project 0.0.0 dmcrypt_check(). "$DEV_NAME"_tstdev $OUT |
| 163 | else |
| 164 | echoscip-shell system . . echo. -n "[N/A]" |
| 165 | fi |
| 166 | |
| 167 | echoscip-shell system . . echo. -n -e " LUKS2:" |
| 168 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP luksFormat --type luks2 --pbkdf pbkdf2 $FAST_PBKDF2 -c $1 -s 256 --offset 8192 /dev/mapper/$DEV_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 169 | if [scip-shell system . . `[`. $? -eq 0 ] ; then |
| 170 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP luksOpen /dev/mapper/$DEV_NAME "$DEV_NAME"_tstdev >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). |
| 171 | dmcrypt_checkscip-shell shell shell-project 0.0.0 dmcrypt_check(). "$DEV_NAME"_tstdev $OUT |
| 172 | else |
| 173 | echoscip-shell system . . echo. -n "[N/A]" |
| 174 | fi |
| 175 | |
| 176 | # repeated device creation must return the same checksum |
| 177 | echoscip-shell system . . echo. -n -e " CHECKSUM:" |
| 178 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c $1 -s 256 "$DEV_NAME"_tstdev /dev/mapper/$DEV_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 179 | if [scip-shell system . . `[`. $? -eq 0 ] ; then |
| 180 | dmcrypt_check_sumscip-shell shell shell-project 0.0.0 dmcrypt_check_sum(). "$1" "$DEV_NAME"_tstdev |
| 181 | else |
| 182 | echoscip-shell system . . echo. -n "[N/A]" |
| 183 | fi |
| 184 | echoscip-shell system . . echo. |
| 185 | } |
| 186 | |
| 187 | [scip-shell system . . `[`. $(idscip-shell system . . id. -u) != 0 ] && skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: You must be root to run this test, test skipped." |
| 188 | [scip-shell system . . `[`. -z "$LOOPDEV" ] && skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find free loop device, test skipped." |
| 189 | [scip-shell system . . `[`. ! -x "$CRYPTSETUP" ] && skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find $CRYPTSETUP, test skipped." |
| 190 | [scip-shell system . . `[`. -n "$VALG" ] && valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup(). && CRYPTSETUP=valgrind_run |
| 191 | |
| 192 | add_devicescip-shell shell shell-project 0.0.0 add_device(). |
| 193 | |
| 194 | # compatibility modes |
| 195 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). aes aes-cbc-plain |
| 196 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). aes-plain aes-cbc-plain |
| 197 | |
| 198 | # empty cipher |
| 199 | PASSWORD="" |
| 200 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). null cipher_null-ecb |
| 201 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). cipher_null cipher_null-ecb |
| 202 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). cipher_null-ecb |
| 203 | |
| 204 | PASSWORD=$PASSWORD1 |
| 205 | # codebook doesn't support IV at all |
| 206 | for cipher in $CIPHERS ; do |
| 207 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). "$cipher-ecb" |
| 208 | done |
| 209 | |
| 210 | for cipher in $CIPHERS ; do |
| 211 | for mode in $MODES ; do |
| 212 | for ivmode in $IVMODES ; do |
| 213 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). "$cipher-$mode-$ivmode" |
| 214 | done |
| 215 | done |
| 216 | done |
| 217 | |
| 218 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). xchacha12,aes-adiantum-plain64 |
| 219 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). xchacha20,aes-adiantum-plain64 |
| 220 | |
| 221 | dmcryptscip-shell shell shell-project 0.0.0 dmcrypt(). aes-hctr2-plain64 |
| 222 | |
| 223 | echoscip-shell system . . echo. -n "CAPI format:" |
| 224 | if dm_crypt_capi_supportscip-shell shell shell-project 0.0.0 dm_crypt_capi_support(). ; then |
| 225 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c 'capi:xts(aes)-plain64' -s 256 "$DEV_NAME"_tstdev /dev/mapper/$DEV_NAME || failscip-shell shell shell-project 0.0.0 fail(). |
| 226 | $CRYPTSETUP close "$DEV_NAME"_tstdev || failscip-shell shell shell-project 0.0.0 fail(). |
| 227 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c 'capi:xts(ecb(aes-generic))-plain64' -s 256 "$DEV_NAME"_tstdev /dev/mapper/$DEV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`. && failscip-shell shell shell-project 0.0.0 fail(). |
| 228 | dmsetup create "$DEV_NAME"_tstdev --table "0 8 crypt capi:xts(aes)-plain64 $KEY 0 /dev/mapper/$DEV_NAME 0" || failscip-shell shell shell-project 0.0.0 fail(). |
| 229 | $CRYPTSETUP status "$DEV_NAME"_tstdev 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. "type:" | grepscip-shell system . . grep. -q "n/a" || failscip-shell shell shell-project 0.0.0 fail(). |
| 230 | $CRYPTSETUP close "$DEV_NAME"_tstdev 2>/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). |
| 231 | |
| 232 | # Do not confuse aes-plain with capi:xts(plain)-plain |
| 233 | echoscip-shell system . . echo. $PASSWORD | $CRYPTSETUP create -h sha256 -c 'capi:xts(aes)-plain' -s 256 "$DEV_NAME"_tstdev /dev/mapper/$DEV_NAME || failscip-shell shell shell-project 0.0.0 fail(). |
| 234 | $CRYPTSETUP close "$DEV_NAME"_tstdev || failscip-shell shell shell-project 0.0.0 fail(). |
| 235 | echoscip-shell system . . echo. [OK] |
| 236 | else |
| 237 | echoscip-shell system . . echo. [N/A] |
| 238 | fi |
| 239 | |
| 240 | cleanupscip-shell shell shell-project 0.0.0 cleanup(). |
| 241 | exit 0 |