Imported Upstream version 2.3.7
[platform/upstream/cryptsetup.git] / tests / generators / generate-luks2-corrupted-hdr0-with-correct-chks.img.sh
1 #!/bin/bash
2
3 . lib.sh
4
5 #
6 # *** Description ***
7 #
8 # generate header with malformed json but correct checksum in primary header
9 #
10
11 # $1 full target dir
12 # $2 full source luks2 image
13
14 function prepare()
15 {
16         cp $SRC_IMG $TGT_IMG
17         test -d $TMPDIR || mkdir $TMPDIR
18         read_luks2_json0 $TGT_IMG $TMPDIR/json0
19         read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
20 }
21
22 function generate()
23 {
24         read -r json_str < $TMPDIR/json0
25         json_len=${#json_str}
26         json_len=$((json_len-1)) # to replace json closing '}'
27         json_new_str="${json_str:0:json_len},\""
28
29         while [ ${#json_new_str} -le $((LUKS2_JSON_SIZE*512)) ]; do
30                 json_new_str=$json_new_str"all_work_and_no_play_makes_Jack_a_dull_boy_"
31         done
32
33         printf "%s" $json_new_str | _dd of=$TMPDIR/json0 bs=512 count=$LUKS2_JSON_SIZE
34
35         merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0
36         erase_checksum $TMPDIR/area0
37         chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
38         write_checksum $chks0 $TMPDIR/area0
39         write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG
40 }
41
42 function check()
43 {
44         chks_res0=$(read_sha256_checksum $TGT_IMG)
45         test "$chks0" = "$chks_res0" || exit 2
46         read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
47         read -r json_str_res0 < $TMPDIR/json_res0
48         test ${#json_str_res0} -eq $((LUKS2_JSON_SIZE*512)) || exit 2
49 }
50
51 function cleanup()
52 {
53         rm -f $TMPDIR/*
54         rm -fd $TMPDIR
55 }
56
57 test $# -eq 2 || exit 1
58
59 TGT_IMG=$1/$(test_img_name $0)
60 SRC_IMG=$2
61
62 prepare
63 generate
64 check
65 cleanup