"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/generators/generate-luks2-missing-trailing-null-byte-json0.img.sh" (24 Jul 2021, 1974 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.

    1 #!/bin/bash
    2 
    3 . lib.sh
    4 
    5 #
    6 # *** Description ***
    7 #
    8 # generate primary header with well-formed json but missing
    9 # trailing null byte.
   10 #
   11 # secondary header is corrupted on purpose as well
   12 #
   13 
   14 # $1 full target dir
   15 # $2 full source luks2 image
   16 
   17 PATTERN="\"config\":{"
   18 KEY="\"config_key\":\""
   19 
   20 function prepare()
   21 {
   22     cp $SRC_IMG $TGT_IMG
   23     test -d $TMPDIR || mkdir $TMPDIR
   24     read_luks2_json0 $TGT_IMG $TMPDIR/json0
   25     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
   26     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
   27 }
   28 
   29 function generate()
   30 {
   31     read -r json_str < $TMPDIR/json0
   32     json_len=${#json_str}
   33     pindex=$(strindex $json_str $PATTERN)
   34     test $pindex -gt 0 || exit 2
   35 
   36     offset=${#PATTERN}
   37     offset=$((offset+pindex))
   38     key_len=${#KEY}
   39     remain=$((LUKS2_JSON_SIZE*512-key_len-json_len-1)) # -1: closing '"'
   40     if [ ${json_str:offset:1} = "}" ]; then
   41         format_str="%s%s%s"
   42     else
   43         format_str="%s%s,%s"
   44         remain=$((remain-1)) # also count with separating ','
   45     fi
   46     test $remain -gt 0 || exit 2
   47 
   48     fill=$(repeat_str "X" $remain)
   49     fill=$(repeat_str "X" $remain)"\""
   50 
   51     printf $format_str $KEY $fill ${json_str:$offset} | _dd of=$TMPDIR/json0 bs=1 seek=$offset conv=notrunc
   52 
   53     merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0
   54     erase_checksum $TMPDIR/area0
   55     chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
   56     write_checksum $chks0 $TMPDIR/area0
   57     write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG
   58     kill_bin_hdr $TMPDIR/hdr1
   59     write_luks2_hdr1 $TMPDIR/hdr1 $TGT_IMG
   60 }
   61 
   62 function check()
   63 {
   64     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1
   65     local str_res1=$(head -c 6 $TMPDIR/hdr_res1)
   66     test "$str_res1" = "VACUUM" || exit 2
   67 
   68     read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
   69     chks_res0=$(read_sha256_checksum $TGT_IMG)
   70     test "$chks0" = "$chks_res0" || exit 2
   71     read -r json_str_res0 < $TMPDIR/json_res0
   72     test ${#json_str_res0} -eq $((LUKS2_JSON_SIZE*512)) || exit 2
   73 }
   74 
   75 function cleanup()
   76 {
   77     rm -f $TMPDIR/*
   78     rm -fd $TMPDIR
   79 }
   80 
   81 test $# -eq 2 || exit 1
   82 
   83 TGT_IMG=$1/$(test_img_name $0)
   84 SRC_IMG=$2
   85 
   86 prepare
   87 generate
   88 check
   89 cleanup