Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / tests / generators / generate-luks2-corrupted-hdr1-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 secondary 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_json1 $TGT_IMG $TMPDIR/json1
19         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
20 }
21
22 function generate()
23 {
24         read -r json_str < $TMPDIR/json1
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/json1 bs=512 count=$LUKS2_JSON_SIZE
34
35         merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json1 $TMPDIR/area1
36         erase_checksum $TMPDIR/area1
37         chks1=$(calc_sha256_checksum_file $TMPDIR/area1)
38         write_checksum $chks1 $TMPDIR/area1
39         write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG
40 }
41
42 function check()
43 {
44         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1
45         chks_res1=$(read_sha256_checksum $TMPDIR/hdr_res1)
46         test "$chks1" = "$chks_res1" || exit 2
47         read_luks2_json1 $TGT_IMG $TMPDIR/json_res1
48         read -r json_str_res1 < $TMPDIR/json_res1
49         test ${#json_str_res1} -eq $((LUKS2_JSON_SIZE*512)) || exit 2
50 }
51
52 function cleanup()
53 {
54         rm -f $TMPDIR/*
55         rm -fd $TMPDIR
56 }
57
58 test $# -eq 2 || exit 1
59
60 TGT_IMG=$1/$(test_img_name $0)
61 SRC_IMG=$2
62
63 prepare
64 generate
65 check
66 cleanup