Imported Upstream version 2.3.7
[platform/upstream/cryptsetup.git] / tests / luks2-integrity-test
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=mode-test.img
9 PWD1=nHjJHjI23JK
10 KEY_FILE=key.img
11 FAST_PBKDF_OPT="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"
12
13 dmremove() { # device
14         udevadm settle >/dev/null 2>&1
15         dmsetup remove $1 >/dev/null 2>&1
16 }
17
18 cleanup() {
19         [ -b /dev/mapper/$DEV_NAME ] && dmremove $DEV_NAME
20         [ -b /dev/mapper/"$DEV_NAME"_dif ] && dmremove "$DEV_NAME"_dif
21         rm -f $DEV $KEY_FILE >/dev/null 2>&1
22 }
23
24 fail()
25 {
26         echo
27         [ -n "$1" ] && echo "FAIL: $1"
28         echo "FAILED backtrace:"
29         while caller $frame; do ((frame++)); done
30         cleanup
31         exit 100
32 }
33
34 skip()
35 {
36         [ -n "$1" ] && echo "$1"
37         exit 77
38 }
39
40 add_device() {
41         cleanup
42         dd if=/dev/urandom of=$KEY_FILE bs=1 count=512 >/dev/null 2>&1
43         dd if=/dev/zero of=$DEV bs=1M count=32 >/dev/null 2>&1
44         sync
45 }
46
47 status_check() # name value
48 {
49         #$CRYPTSETUP status $DEV_NAME
50         X=$($CRYPTSETUP status $DEV_NAME | grep -m1 "$1" | sed -e 's/.*:[ \t]\+//' | cut -d' ' -f1)
51         if [ "$X" != "$2" ] ; then
52                 echo "[status FAIL]"
53                 echo " Expecting $1:$2 got \"$X\"."
54                 fail
55         fi
56 }
57
58 dump_check() # name value
59 {
60         #$CRYPTSETUP luksDump $DEV
61         X=$($CRYPTSETUP luksDump $DEV | grep -m1 "$1" | sed -e 's/.*:[ \t]\+//' | cut -d' ' -f1)
62         if [ "$X" != "$2" ] ; then
63                 echo "[dump FAIL]"
64                 echo " Expecting $1:$2 got \"$X\"."
65                 fail
66         fi
67 }
68
69 int_check_sum() # alg checksum
70 {
71         VSUM=$(sha256sum /dev/mapper/$DEV_NAME | cut -d' ' -f 1)
72         if [ "$VSUM" = "$2" ] ; then
73                 echo -n "[CHECKSUM]"
74         else
75                 echo "[FAIL]"
76                 echo " Expecting $2 got $VSUM."
77                 fail
78         fi
79 }
80
81 int_error_detection() # alg int sector_size
82 {
83         # FIXME: this is just a trivial failure
84         echo -n "[DETECT_CORRUPTION]"
85         echo -n "XXXXX" | dd of=$DEV bs=1M seek=28 count=1 conv=notrunc >/dev/null 2>&1 || fail "Cannot write to device."
86         $CRYPTSETUP open -d $KEY_FILE $DEV $DEV_NAME || fail "Cannot activate device."
87         dd if=/dev/mapper/$DEV_NAME  of=/dev/null >/dev/null 2>&1 && fail "Error detection failed."
88         $CRYPTSETUP close $DEV_NAME || fail "Cannot deactivate device."
89 }
90
91 intformat() # alg integrity integrity_out key_size int_key_size sector_size csum
92 {
93         echo -n "[$1:$2:$4:$6]"
94         echo -n "[FORMAT]"
95         $CRYPTSETUP luksFormat --type luks2 -q -c $1 --integrity $2 --sector-size $6 -s $4 \
96                 $FAST_PBKDF_OPT -d $KEY_FILE $DEV --offset 8192 --integrity-legacy-padding >/dev/null 2>&1
97         if [ $? -ne 0 ] ; then
98                 echo "[N/A]"
99                 return
100         fi
101         dump_check "cipher" $1
102         dump_check "sector" $6
103         dump_check "integrity" $3
104         dump_check "Key:" $(($4 + $5))
105         echo -n "[ACTIVATE]"
106         $CRYPTSETUP open -d $KEY_FILE $DEV $DEV_NAME || fail "Cannot activate device."
107         status_check "cipher" $1
108         status_check "sector size" $6
109         status_check "integrity:" $3
110         status_check "keysize:" $(($4 + $5))
111         [ $5 -gt 0 ] && status_check "integrity keysize:" $5
112         int_check_sum $1 $7
113         echo -n "[REMOVE]"
114         $CRYPTSETUP close $DEV_NAME || fail "Cannot deactivate device."
115         int_error_detection
116         echo "[OK]"
117 }
118
119
120 [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped."
121 [ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped."
122 modprobe dm-integrity >/dev/null 2>&1
123 dmsetup targets | grep integrity >/dev/null 2>&1 || skip "Cannot find dm-integrity target, test skipped."
124
125 add_device
126
127 intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 128 256  512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c
128 intformat aes-xts-plain64      hmac-sha256 hmac\(sha256\) 256 256  512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c
129 intformat aes-xts-random       hmac-sha256 hmac\(sha256\) 256 256  512 492c2d1cc9e222a850c399bfef4ed5a86bf5afc59e54f0f0c7ba8e2a64548323
130 intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 256 256  512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c
131 intformat aes-xts-plain64      hmac-sha256 hmac\(sha256\) 512 256  512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c
132 intformat aes-xts-random       hmac-sha256 hmac\(sha256\) 512 256  512 492c2d1cc9e222a850c399bfef4ed5a86bf5afc59e54f0f0c7ba8e2a64548323
133 intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 128 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
134 intformat aes-xts-plain64      hmac-sha256 hmac\(sha256\) 256 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
135 intformat aes-xts-random       hmac-sha256 hmac\(sha256\) 256 256 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda
136 intformat aes-cbc-essiv:sha256 hmac-sha256 hmac\(sha256\) 256 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
137 intformat aes-xts-plain64      hmac-sha256 hmac\(sha256\) 512 256 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
138 intformat aes-xts-random       hmac-sha256 hmac\(sha256\) 512 256 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda
139
140 intformat aes-cbc-essiv:sha256 hmac-sha512 hmac\(sha512\) 256 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd
141 intformat aes-xts-essiv:sha256 hmac-sha512 hmac\(sha512\) 512 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd
142 intformat aes-xts-plain64      hmac-sha512 hmac\(sha512\) 512 512 4096 9873d864fccb866521e79c9f0f75ad0c578d6bd7620399bbf4779e698c6e92fd
143 intformat aes-xts-random       hmac-sha512 hmac\(sha512\) 512 512 4096 621f6c03f7361c2bf8f10059ae822339223f8471c750b0cf8584fba7134bd4a2
144
145 intformat aes-xts-plain64 hmac-sha1 hmac\(sha1\) 512 160 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e
146 intformat aes-xts-random  hmac-sha1 hmac\(sha1\) 512 160 4096 8c0463f5ac09613674bdf40b0ff6f985edbc3de04e51fdc688873cb333ef3cda
147
148 intformat aes-gcm-random aead aead 128 0  512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57
149 intformat aes-gcm-random aead aead 128 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
150 intformat aes-gcm-random aead aead 256 0  512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57
151 intformat aes-gcm-random aead aead 256 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
152
153 intformat aes-ccm-random aead aead 152 0  512 288e5e9bc5be6c0bd2a74abbb72c7944da83198b5e3041dcf159e7ae250dafa8
154 intformat aes-ccm-random aead aead 152 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e
155 intformat aes-ccm-random aead aead 280 0  512 288e5e9bc5be6c0bd2a74abbb72c7944da83198b5e3041dcf159e7ae250dafa8
156 intformat aes-ccm-random aead aead 280 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e
157
158 intformat chacha20-plain64 poly1305 poly1305 256 0  512 3f82eae753ff52a689ddc559c691bbdff838361bbe9a3ce8c7212e16e51b5dbe
159 intformat chacha20-random  poly1305 poly1305 256 0  512 5f6f3f6be03c74d9aaaeaf40dd310c99a20e2786045f78a1fc6a0b189d231f57
160 intformat chacha20-plain64 poly1305 poly1305 256 0 4096 7370c66a92708fb71b186931468be6aa9b26f4f88373b00b1c57360b9ee1304e
161 intformat chacha20-random  poly1305 poly1305 256 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
162
163 intformat aegis128-random  aead aead 128 0  512 ee501705a084cd0ab6f4a28014bcf62b8bfa3434de00b82743c50b3abf06232c
164 intformat aegis128-random  aead aead 128 0 4096 358d6beceddf593aff6b22c31684e0df9c226330aff5812e060950215217d21b
165
166 cleanup