"Fossies" - the Fresh Open Source Software Archive

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