"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/generators/generate-luks2-metadata-size-64k-inv-keyslots-size-c0.img.sh" (24 Jul 2021, 2802 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 with predefined json_size where keyslots size
    9 # overflows in data area (segment offset)
   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 function prepare()
   18 {
   19     cp $SRC_IMG $TGT_IMG
   20     test -d $TMPDIR || mkdir $TMPDIR
   21     read_luks2_json0 $TGT_IMG $TMPDIR/json0
   22     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
   23     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
   24 }
   25 
   26 function generate()
   27 {
   28     # 64KiB metadata
   29     TEST_MDA_SIZE=$LUKS2_HDR_SIZE_64K
   30 
   31     TEST_MDA_SIZE_BYTES=$((TEST_MDA_SIZE*512))
   32     TEST_JSN_SIZE=$((TEST_MDA_SIZE-LUKS2_BIN_HDR_SIZE))
   33     KEYSLOTS_OFFSET=$((TEST_MDA_SIZE*1024))
   34     JSON_DIFF=$(((TEST_MDA_SIZE-LUKS2_HDR_SIZE)*1024))
   35     JSON_SIZE=$((TEST_JSN_SIZE*512))
   36     DATA_OFFSET=16777216
   37 
   38     json_str=$(jq -c --arg jdiff $JSON_DIFF --arg jsize $JSON_SIZE --arg off $DATA_OFFSET \
   39              --arg mda $((2*TEST_MDA_SIZE_BYTES)) \
   40            '.keyslots[].area.offset |= ( . | tonumber + ($jdiff | tonumber) | tostring) |
   41             .config.json_size = $jsize |
   42             .config.keyslots_size = (((($off | tonumber) - ($mda | tonumber) + 4096)) | tostring ) |
   43             .segments."0".offset = $off' $TMPDIR/json0)
   44     test -n "$json_str" || exit 2
   45     test ${#json_str} -lt $((LUKS2_JSON_SIZE*512)) || exit 2
   46 
   47     write_luks2_json "$json_str" $TMPDIR/json0 $TEST_JSN_SIZE
   48 
   49     write_bin_hdr_size $TMPDIR/hdr0 $TEST_MDA_SIZE_BYTES
   50     write_bin_hdr_size $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   51 
   52     merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0 $TEST_JSN_SIZE
   53     merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json0 $TMPDIR/area1 $TEST_JSN_SIZE
   54 
   55     erase_checksum $TMPDIR/area0
   56     chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
   57     write_checksum $chks0 $TMPDIR/area0
   58 
   59     erase_checksum $TMPDIR/area1
   60     chks0=$(calc_sha256_checksum_file $TMPDIR/area1)
   61     write_checksum $chks0 $TMPDIR/area1
   62 
   63     kill_bin_hdr $TMPDIR/area1
   64 
   65     write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG $TEST_MDA_SIZE
   66     write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG $TEST_MDA_SIZE
   67 }
   68 
   69 function check()
   70 {
   71     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1 $TEST_MDA_SIZE
   72     local str_res1=$(head -c 6 $TMPDIR/hdr_res1)
   73     test "$str_res1" = "VACUUM" || exit 2
   74     read_luks2_json0 $TGT_IMG $TMPDIR/json_res0 $TEST_JSN_SIZE
   75     jq -c --arg koff $KEYSLOTS_OFFSET --arg jsize $JSON_SIZE --arg off $DATA_OFFSET --arg mda $((2*TEST_MDA_SIZE_BYTES)) \
   76         'if ([.keyslots[].area.offset] | map(tonumber) | min | tostring != $koff) or
   77             (.config.json_size != $jsize) or
   78             (.config.keyslots_size != (((($off | tonumber) - ($mda | tonumber) + 4096)) | tostring ))
   79         then error("Unexpected value in result json") else empty end' $TMPDIR/json_res0 || exit 5
   80 }
   81 
   82 function cleanup()
   83 {
   84     rm -f $TMPDIR/*
   85     rm -fd $TMPDIR
   86 }
   87 
   88 test $# -eq 2 || exit 1
   89 
   90 TGT_IMG=$1/$(test_img_name $0)
   91 SRC_IMG=$2
   92 
   93 prepare
   94 generate
   95 check
   96 cleanup