"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/loopaes-test" (13 Jan 2022, 4314 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 "loopaes-test": 2.3.5_vs_2.3.6.

    1 #!/bin/bash
    2 
    3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
    4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
    5 
    6 # try to validate using loop-AES losetup/kernel if available
    7 LOSETUP_AES=/losetup-aes.old
    8 
    9 LOOP_DD_PARAM="bs=1k count=10000"
   10 DEV_NAME=dummy
   11 IMG=loopaes.img
   12 KEYv1=key_v1
   13 KEYv2=key_v2
   14 KEYv3=key_v3
   15 LOOPDEV=$(losetup -f 2>/dev/null)
   16 
   17 function dmremove() { # device
   18     udevadm settle >/dev/null 2>&1
   19     dmsetup remove --retry $1 >/dev/null 2>&1
   20 }
   21 
   22 function remove_mapping()
   23 {
   24     [ -b /dev/mapper/$DEV_NAME2 ] && dmremove $DEV_NAME2
   25     [ -b /dev/mapper/$DEV_NAME ] && dmremove $DEV_NAME
   26     losetup -d $LOOPDEV >/dev/null 2>&1
   27     rm -f $IMG $KEYv1 $KEYv2 $KEYv3 >/dev/null 2>&1
   28 }
   29 
   30 function fail()
   31 {
   32     echo "FAILED backtrace:"
   33     while caller $frame; do ((frame++)); done
   34     remove_mapping
   35     exit 2
   36 }
   37 
   38 function skip()
   39 {
   40     [ -n "$1" ] && echo "$1"
   41     exit 77
   42 }
   43 
   44 function prepare()
   45 {
   46     remove_mapping
   47     dd if=/dev/zero of=$IMG $LOOP_DD_PARAM >/dev/null 2>&1
   48     sync
   49     losetup $LOOPDEV $IMG
   50 
   51     # Prepare raw key: v1 - one key, v2 - 64 keys, v3 - 64 + one IV
   52     if [ ! -e $KEYv3 ]; then
   53         head -c 3705 /dev/urandom | uuencode -m - | head -n 66 | tail -n 65 >$KEYv3
   54         head -n 1 $KEYv3 > $KEYv1
   55         head -n 64 $KEYv3 > $KEYv2
   56     fi
   57     [ -n "$1" ] && echo -n "$1 "
   58 }
   59 
   60 function check_exists()
   61 {
   62     [ -b /dev/mapper/$DEV_NAME ] || fail
   63 }
   64 
   65 function get_offset_params() # $offset
   66 {
   67     offset=$1
   68     if [ "${offset:0:1}" = "@" ] ; then
   69         echo "-o $((${offset:1} / 512)) -p 0"
   70     else
   71         echo "-o $((offset / 512))"
   72     fi
   73 }
   74 
   75 function get_expsum() # $offset
   76 {
   77     case $1 in
   78     0)
   79        echo "31e00e0e4c233c89051cd748122fde2c98db0121ca09ba93a3820817ea037bc5"
   80        ;;
   81     @8192 | 8192)
   82        echo "bfd94392d1dd8f5d477251d21b3c736e177a4945cd4937847fc7bace82996aed"
   83        ;;
   84     @8388608 | 8388608)
   85        echo "33838fe36928a929bd7971bed7e82bd426c88193fcd692c2e6f1b9c9bfecd4d6"
   86        ;;
   87     *) fail
   88        ;;
   89     esac
   90 }
   91 
   92 function check_sum() # $key $keysize $offset [stdin|keyfile]
   93 {
   94     $CRYPTSETUP close $DEV_NAME || fail
   95 
   96     EXPSUM=$(get_expsum $3)
   97     if [ "$4" == "stdin" ] ; then
   98         cat $1 | $CRYPTSETUP loopaesOpen $LOOPDEV $DEV_NAME -s $2 --key-file - $(get_offset_params $3) >/dev/null 2>&1
   99     else
  100         $CRYPTSETUP loopaesOpen $LOOPDEV $DEV_NAME -s $2 --key-file $1 $(get_offset_params $3) >/dev/null 2>&1
  101     fi
  102     ret=$?
  103     VSUM=$(sha256sum /dev/mapper/$DEV_NAME | cut -d' ' -f 1)
  104     if [ $ret -eq 0 -a "$VSUM" = "$EXPSUM" ] ; then
  105         echo -n "[$4:OK]"
  106     else
  107         echo "[$4:FAIL]"
  108         [ "$VSUM" != "$EXPSUM" ] && echo " Expecting $EXPSUM got $VSUM."
  109         fail
  110     fi
  111 }
  112 
  113 function check_sum_losetup() # $key $alg
  114 {
  115     [ ! -x $LOSETUP_AES ] && echo && return
  116 
  117     echo -n " Verification using loop-AES: "
  118 
  119     losetup -d $LOOPDEV >/dev/null 2>&1
  120     cat $1 | $LOSETUP_AES -p 0 -e $2 -o $3 $LOOPDEV $IMG
  121     ret=$?
  122     VSUM=$(sha256sum $LOOPDEV | cut -d' ' -f 1)
  123     if [ $ret -eq 0 -a "$VSUM" = "$EXPSUM" ] ; then
  124         echo "[OK]"
  125     else
  126         echo "[FAIL]"
  127         [ "$VSUM" != "$EXPSUM" ] && echo " Expecting $EXPSUM got $VSUM (loop-AES)."
  128         fail
  129     fi
  130     losetup -d $LOOPDEV >/dev/null 2>&1
  131 }
  132 
  133 function check_version()
  134 {
  135     VER_STR=$(dmsetup version | grep Driver)
  136     VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
  137     VER_PATCH=$(echo $VER_STR | cut -f 3 -d.)
  138 
  139     test $VER_MIN -lt 19 && return 1
  140     test $VER_MIN -eq 19 -a $VER_PATCH -ge 6 && return 1 # RHEL
  141     return 0
  142 }
  143 
  144 [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped."
  145 [ -z "$LOOPDEV" ] && skip "Cannot find free loop device, test skipped."
  146 which uuencode >/dev/null 2>&1 || skip "WARNING: test require uuencode binary, test skipped."
  147 check_version || skip "Probably old kernel, test skipped."
  148 
  149 # loop-AES tests
  150 KEY_SIZES="128 256"
  151 KEY_FILES="$KEYv1 $KEYv2 $KEYv3"
  152 DEV_OFFSET="0 8192 @8192 8388608 @8388608"
  153 
  154 for key_size in $KEY_SIZES ; do
  155     for key in $KEY_FILES ; do
  156         for offset in $DEV_OFFSET ; do
  157         prepare "Open loop-AES $key / AES-$key_size / offset $offset"
  158         $CRYPTSETUP loopaesOpen $LOOPDEV $DEV_NAME \
  159             -s $key_size --key-file $key $(get_offset_params $offset) \
  160             2>/dev/null
  161         [ $? -ne 0 ] && echo "[SKIPPED]" && continue
  162         check_exists
  163         # Fill device with zeroes and reopen it
  164         dd if=/dev/zero of=/dev/mapper/$DEV_NAME $LOOP_DD_PARAM >/dev/null 2>&1
  165         check_sum $key $key_size $offset keyfile
  166         check_sum $key $key_size $offset stdin
  167         $CRYPTSETUP loopaesClose $DEV_NAME || fail
  168         check_sum_losetup $key AES$key_size $offset
  169         done
  170     done
  171 done
  172 
  173 remove_mapping
  174 exit 0