87ea92eba20299ce49c2d6a4fb04fc1725a25bc7
[platform/upstream/cryptsetup.git] / tests / password-hash-test
1 #!/bin/bash
2
3 # check hash processing in create command
4
5 CRYPTSETUP=../src/cryptsetup
6 DEV_NAME=dmc_test
7 KEY_FILE=keyfile
8
9 DEV2=$DEV_NAME"_x"
10
11 dmremove() { # device
12         udevadm settle >/dev/null 2>&1
13         dmsetup remove $1 >/dev/null 2>&1
14 }
15
16 cleanup() {
17         [ -b /dev/mapper/$DEV2 ] && dmremove $DEV2
18         [ -b /dev/mapper/$DEV_NAME ] && dmremove $DEV_NAME
19         rm -f $KEY_FILE
20         exit $1
21 }
22
23 crypt_key() # hash keysize pwd/file name outkey
24 {
25         DEV2=$DEV_NAME"_x"
26         MODE=aes-cbc-essiv:sha256
27         [ $2 -gt 256 ] && MODE=aes-xts-plain
28
29         echo -n "HASH: $1 KSIZE: $2 / $3"
30         case "$3" in
31         pwd)
32                 echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
33                 ret=$?
34                 ;;
35         stdin)
36                 echo -e -n "$4" | $CRYPTSETUP create -c $MODE -d "-" -h $1 -s $2 $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
37                 ret=$?
38                 ;;
39         file)
40                 $CRYPTSETUP create -c $MODE -d $4 -h $1 -s $2 $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
41                 ret=$?
42                 ;;
43         *)
44                 fail
45                 ;;
46         esac
47
48         if [ $ret -ne 0 ] ; then
49                 echo " [n/a]"
50                 return
51         fi
52
53         VKEY=$(dmsetup table $DEV2 --showkeys 2>/dev/null | cut -d' '  -f 5)
54         if [ "$VKEY" != "$5" ] ; then
55                 echo " [FAILED]"
56                 echo "expected: $5"
57                 echo "real key: $VKEY"
58                 cleanup 100
59         else
60                 echo " [OK]"
61         fi
62
63         dmremove $DEV2
64 }
65
66 if [ $(id -u) != 0 ]; then
67         echo "WARNING: You must be root to run this test, test skipped."
68         exit 0
69 fi
70
71 dmsetup create $DEV_NAME --table "0 10240 zero" >/dev/null 2>&1
72
73 crypt_key ripemd160   0 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
74 crypt_key ripemd160 256 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
75 crypt_key ripemd160 128 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f
76 crypt_key sha1      256 pwd "xxx" b60d121b438a380c343d5ec3c2037564b82ffef30b1e0a6ad9af7a73aa91c197
77 crypt_key sha1      128 pwd "xxx" b60d121b438a380c343d5ec3c2037564
78 crypt_key sha256    256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
79 crypt_key sha256    128 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7
80
81 crypt_key ripemd160   0 stdin "xxx"   aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
82 crypt_key ripemd160 256 stdin "xxx\n" 625ce2a8dbdf08f1de400dba7ab9fab246f2a55ad6136e6cafd6703732dab8cf
83
84 # with keyfile, hash is ignored
85 crypt_key ripemd160 256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
86 crypt_key sha256    256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
87
88 # limiting key
89 crypt_key sha256:20 256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b4580588000000000000000000000000
90 crypt_key sha256:32 256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
91
92 # key file, 80 chars
93 echo -n -e "0123456789abcdef\n\x01\x00\x03\xff\xff\r\xff\xff\n\r" \
94            "2352j3rkjhadcfasc823rqaw7e1 3dq sdq3d 2dkjqw3h2=====" >$KEY_FILE
95 KEY_FILE_HEX="303132333435363738396162636465660a010003ffff0dffff0a0d20323335326a33726b6a686164636661736338323372716177376531203364712073647133"
96
97 crypt_key ripemd160 256 file $KEY_FILE ${KEY_FILE_HEX:0:64}
98 crypt_key sha256    256 file $KEY_FILE ${KEY_FILE_HEX:0:64}
99 crypt_key sha256    128 file $KEY_FILE ${KEY_FILE_HEX:0:32}
100 crypt_key sha256    512 file $KEY_FILE $KEY_FILE_HEX
101
102 cleanup 0