"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/password-hash-test" (13 Jan 2022, 7918 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "password-hash-test": 2.3.6_vs_2.4.0.

    1 #!/bin/bash
    2 
    3 # check hash processing in create command
    4 
    5 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
    6 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
    7 DEV_NAME=dmc_test
    8 KEY_FILE=keyfile
    9 
   10 DEV2=$DEV_NAME"_x"
   11 
   12 dmremove() { # device
   13     udevadm settle >/dev/null 2>&1
   14     dmsetup remove --retry $1 >/dev/null 2>&1
   15 }
   16 
   17 cleanup() {
   18     [ -b /dev/mapper/$DEV2 ] && dmremove $DEV2
   19     [ -b /dev/mapper/$DEV_NAME ] && dmremove $DEV_NAME
   20     rm -f $KEY_FILE
   21     exit $1
   22 }
   23 
   24 function fail()
   25 {
   26     echo " $1 [FAILED]"
   27     echo "FAILED backtrace:"
   28     while caller $frame; do ((frame++)); done
   29     cleanup 2
   30 }
   31 
   32 crypt_key() # hash keysize pwd/file name outkey [limit] [offset]
   33 {
   34     DEV2=$DEV_NAME"_x"
   35     LIMIT=""
   36     MODE=aes-cbc-essiv:sha256
   37     [ $2 -gt 256 ] && MODE=aes-xts-plain
   38     [ -n "$6" ] && LIMIT="-l $6"
   39     [ -n "$7" ] && LIMIT="$LIMIT --keyfile-offset $7"
   40 
   41     echo -n "HASH: $1 KSIZE: $2 / $3"
   42     case "$3" in
   43     pwd)
   44         echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
   45         ret=$?
   46         ;;
   47     std-)
   48         echo -e -n "$4" | $CRYPTSETUP create -c $MODE -d "-" -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
   49         ret=$?
   50         ;;
   51     stdin)
   52         echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
   53         ret=$?
   54         ;;
   55     cat)
   56         cat $4 | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
   57         ret=$?
   58         ;;
   59     cat-)
   60         cat $4 | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 -d - /dev/mapper/$DEV_NAME 2>/dev/null
   61         ret=$?
   62         ;;
   63     file)
   64         $CRYPTSETUP create -q -c $MODE -d $4 -h $1 -s $2 $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
   65         ret=$?
   66         ;;
   67     failpwd)
   68         echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null && fail "Expecting failure"
   69         echo " [OK]"
   70         return
   71         ;;
   72     *)
   73         fail ""
   74         ;;
   75     esac
   76 
   77     # ignore these cases, not all libs/kernel supports it
   78     if [ "$1" != "sha1" -a "$1" != "sha256" ] || [ $2 -gt 256 ] ; then
   79         if [ $ret -ne 0 ] ; then
   80             echo " [N/A] ($ret, SKIPPED)"
   81             return
   82         fi
   83     fi
   84 
   85     VKEY=$(dmsetup table $DEV2 --showkeys 2>/dev/null | sed 's/.*: //' | cut -d' '  -f 5)
   86     if [ "$VKEY" != "$5" ] ; then
   87         echo " [FAILED]"
   88         echo "expected: $5"
   89         echo "real key: $VKEY"
   90         cleanup 100
   91     else
   92         echo " [OK]"
   93     fi
   94 
   95     dmremove $DEV2
   96 }
   97 
   98 if [ $(id -u) != 0 ]; then
   99     echo "WARNING: You must be root to run this test, test skipped."
  100     exit 77
  101 fi
  102 
  103 dmsetup create $DEV_NAME --table "0 10240 zero" >/dev/null 2>&1
  104 
  105 crypt_key ripemd160   0 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
  106 crypt_key ripemd160 256 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
  107 crypt_key ripemd160 128 pwd "xxx" aeb26d1f69eb6dddfb9381eed4d7299f
  108 crypt_key sha1      256 pwd "xxx" b60d121b438a380c343d5ec3c2037564b82ffef30b1e0a6ad9af7a73aa91c197
  109 crypt_key sha1      128 pwd "xxx" b60d121b438a380c343d5ec3c2037564
  110 crypt_key sha256    256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
  111 crypt_key sha256    128 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7
  112 
  113 crypt_key sha256   0 std- "xxx"    cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
  114 crypt_key sha256 256 std- "xxx\n"  042aea10a0f14f2d391373599be69d53a75dde9951fc3d3cd10b6100aa7a9f24
  115 crypt_key sha256 128 std- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" \
  116                    2a42b97084779dcedf2c66405c5d296c
  117 crypt_key sha256 256 stdin "xxx"   cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
  118 crypt_key sha256   0 stdin "xxx\n" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
  119 
  120 # with keyfile, hash is ignored
  121 crypt_key ripemd160 256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
  122 crypt_key sha256    256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
  123 crypt_key unknown*  256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
  124 
  125 # limiting key
  126 crypt_key sha256:20 256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b4580588000000000000000000000000
  127 crypt_key sha256:32 256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
  128 
  129 crypt_key sha256:   256 failpwd "xxx" x
  130 crypt_key sha256:xx 256 failpwd "xxx" x
  131 
  132 # key file, 80 chars
  133 echo -n -e "0123456789abcdef\n\x01\x00\x03\xff\xff\r\xff\xff\n\r" \
  134        "2352j3rkjhadcfasc823rqaw7e1 3dq sdq3d 2dkjqw3h2=====" >$KEY_FILE
  135 KEY_FILE_HEX="303132333435363738396162636465660a010003ffff0dffff0a0d20323335326a33726b6a686164636661736338323372716177376531203364712073647133"
  136 
  137 
  138 # ignore hash if keyfile is specified
  139 crypt_key ripemd160 256 file $KEY_FILE ${KEY_FILE_HEX:0:64}
  140 crypt_key sha256    256 file $KEY_FILE ${KEY_FILE_HEX:0:64}
  141 crypt_key sha256    128 file $KEY_FILE ${KEY_FILE_HEX:0:32}
  142 crypt_key sha256    512 file $KEY_FILE $KEY_FILE_HEX
  143 
  144 # stdin can be limited
  145 crypt_key plain     128 cat /dev/zero 00000000000000000000000000000000 16
  146 crypt_key plain     128 cat /dev/zero 00000000000000000000000000000000 17
  147 
  148 # read key only up to \n
  149 crypt_key plain     128 cat $KEY_FILE ${KEY_FILE_HEX:0:28}0000 14
  150 
  151 # read full key, ignore keyfile length
  152 crypt_key plain     128 cat- $KEY_FILE ${KEY_FILE_HEX:0:32}
  153 crypt_key plain     128 cat- $KEY_FILE ${KEY_FILE_HEX:0:32} 14
  154 
  155 # but do not ignore hash if keysgfile is "-"
  156 crypt_key sha256    128 cat- $KEY_FILE f3b827c8a6f159ad8c8ed5bd5ab3f8c5
  157 crypt_key sha256    128 cat- $KEY_FILE f3b827c8a6f159ad8c8ed5bd5ab3f8c5 0
  158 crypt_key sha256    128 cat- $KEY_FILE f3b827c8a6f159ad8c8ed5bd5ab3f8c5 80
  159 crypt_key sha256    128 cat- $KEY_FILE a82c9227cc54c7475620ce85ba1fca1e 14
  160 crypt_key sha256    128 cat- $KEY_FILE 7df3f4a41a33805596be85c781cac3b4 14 2
  161 crypt_key sha256    128 cat- $KEY_FILE ebbe65a178e886ddbb778e0a5538db72 40 40
  162 
  163 # limiting plain (no hash)
  164 crypt_key plain   256     pwd "xxxxxxxx" 7878787878787878000000000000000000000000000000000000000000000000
  165 crypt_key plain:2 256     pwd "xxxxxxxx" 7878000000000000000000000000000000000000000000000000000000000000
  166 crypt_key plain:9 256 failpwd "xxxxxxxx" x
  167 
  168 crypt_key sha256    128 cat $KEY_FILE a82c9227cc54c7475620ce85ba1fca1e 14
  169 crypt_key sha256:14 128 cat $KEY_FILE a82c9227cc54c7475620ce85ba1f0000 14
  170 
  171 crypt_key sha256    128 pwd "0123456789abcdef" 9f9f5111f7b27a781f1f1ddde5ebc2dd 16
  172 crypt_key sha256    128 pwd "0123456789abcdef" 1be2e452b46d7a0d9656bbb1f768e824  4
  173 crypt_key sha256    128 pwd "0123"             1be2e452b46d7a0d9656bbb1f768e824  4
  174 
  175 # Test list of various hash algorithms
  176 crypt_key sha1        256 pwd "0123456789abcdef" fe5567e8d769550852182cdf69d74bb16dff8e295e4b6077d6d3fc73e7fe33b0
  177 crypt_key sha224      256 pwd "0123456789abcdef" 7330215f6741fd2bacbd3658681a70f65e2e90a02887989018974ce83775a568
  178 crypt_key sha256      256 pwd "0123456789abcdef" 9f9f5111f7b27a781f1f1ddde5ebc2dd2b796bfc7365c9c28b548e564176929f
  179 crypt_key sha384      256 pwd "0123456789abcdef" fc6304023487cb6f85ac80e47817760c6b153c02da46c6429649e963b031e525
  180 crypt_key sha512      256 pwd "0123456789abcdef" 1c043fbe4bca7c7920dae536c680fd44c15d71ec12cd82a2a9491b0043b57f4d
  181 crypt_key ripemd160   256 pwd "0123456789abcdef" edf4e38018cd71dd489b9c1e54b32054eb42dfad9fdcc22d629d756391a24982
  182 crypt_key whirlpool   256 pwd "0123456789abcdef" b21fc274d47d79ba45f94d67077f0846f75a7acc1a4dc560eacca300179cc142
  183 crypt_key sha3-224    256 pwd "0123456789abcdef" bbb7d56cc80a8c80e907f7d9240edc0be264aa173266b30918bc1065d59a9388
  184 crypt_key sha3-256    256 pwd "0123456789abcdef" a5df4caae9fdb5dbacf667075b709a2f30a115c43168af332062b42d4b0da01f
  185 crypt_key sha3-384    256 pwd "0123456789abcdef" 56f351f754c418892eab4009e5f85c8d5436a591014503563e9395b895526413
  186 crypt_key sha3-512    256 pwd "0123456789abcdef" 59d06155d25dffdb982729de8dce9d7855ca094d8bab8124b347c40668477056
  187 crypt_key sm3         256 pwd "0123456789abcdef" a34fdd8e843802b31a262dce5b1f501bed68ef81520de14b39657aecffaf8a86
  188 crypt_key stribog512  256 pwd "0123456789abcdef" ab1284a64c2325c0ad52494e696df9aa9b92e701605a9a1258b58be08e8240ea
  189 
  190 cleanup 0