Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / tests / generators / lib.sh
1 #!/bin/bash
2
3 # all in 512 bytes blocks (including binary hdr (4KiB))
4 LUKS2_HDR_SIZE=32               #  16 KiB
5 LUKS2_HDR_SIZE_32K=64           #  32 KiB
6 LUKS2_HDR_SIZE_64K=128          #  64 KiB
7 LUKS2_HDR_SIZE_128K=256         # 128 KiB
8 LUKS2_HDR_SIZE_256K=512         # 256 KiB
9 LUKS2_HDR_SIZE_512K=1024        # 512 KiB
10 LUKS2_HDR_SIZE_1M=2048          #   1 MiB
11 LUKS2_HDR_SIZE_2M=4096          #   2 MiB
12 LUKS2_HDR_SIZE_4M=8192          #   4 MiB
13
14 LUKS2_BIN_HDR_SIZE=8            #   4 KiB
15 LUKS2_JSON_SIZE=$((LUKS2_HDR_SIZE-LUKS2_BIN_HDR_SIZE))
16
17 LUKS2_BIN_HDR_CHKS_OFFSET=0x1C0
18 LUKS2_BIN_HDR_CHKS_LENGTH=64
19
20 [ -z "$srcdir" ] && srcdir="."
21 TMPDIR=$srcdir/tmp
22
23 # to be set by individual generator
24 TGT_IMG=""
25 SRC_IMG=""
26
27 repeat_str() {
28         printf "$1"'%.0s' $(eval "echo {1.."$(($2))"}");
29 }
30
31 function strindex()
32 {
33         local x="${1%%$2*}"
34         [[ $x = $1 ]] && echo -1 || echo ${#x}
35 }
36
37 function test_img_name()
38 {
39         local str=$(basename $1)
40         str=${str#generate-}
41         str=${str%%.sh}
42         echo $str
43 }
44
45 # read primary bin hdr
46 # 1:from 2:to
47 function read_luks2_bin_hdr0()
48 {
49         _dd if=$1 of=$2 bs=512 count=$LUKS2_BIN_HDR_SIZE
50 }
51
52 # read primary json area
53 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
54 function read_luks2_json0()
55 {
56         local _js=${4:-$LUKS2_JSON_SIZE}
57         local _js=$((_js*512/4096))
58         _dd if=$1 of=$2 bs=4096 skip=1 count=$_js
59 }
60
61 # read secondary bin hdr
62 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
63 function read_luks2_bin_hdr1()
64 {
65         _dd if=$1 of=$2 skip=${3:-$LUKS2_HDR_SIZE} bs=512 count=$LUKS2_BIN_HDR_SIZE
66 }
67
68 # read secondary json area
69 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
70 function read_luks2_json1()
71 {
72         local _js=${3:-$LUKS2_JSON_SIZE}
73         _dd if=$1 of=$2 bs=512 skip=$((2*LUKS2_BIN_HDR_SIZE+_js)) count=$_js
74 }
75
76 # read primary metadata area (bin + json)
77 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
78 function read_luks2_hdr_area0()
79 {
80         local _as=${3:-$LUKS2_HDR_SIZE}
81         local _as=$((_as*512))
82         _dd if=$1 of=$2 bs=$_as count=1
83 }
84
85 # read secondary metadata area (bin + json)
86 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
87 function read_luks2_hdr_area1()
88 {
89         local _as=${3:-$LUKS2_HDR_SIZE}
90         local _as=$((_as*512))
91         _dd if=$1 of=$2 bs=$_as skip=1 count=1
92 }
93
94 # write secondary bin hdr
95 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
96 function write_luks2_bin_hdr1()
97 {
98         _dd if=$1 of=$2 bs=512 seek=${3:-$LUKS2_HDR_SIZE} count=$LUKS2_BIN_HDR_SIZE conv=notrunc
99 }
100
101 # write primary metadata area (bin + json)
102 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
103 function write_luks2_hdr0()
104 {
105         local _as=${3:-$LUKS2_HDR_SIZE}
106         local _as=$((_as*512))
107         _dd if=$1 of=$2 bs=$_as count=1 conv=notrunc
108 }
109
110 # write secondary metadata area (bin + json)
111 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
112 function write_luks2_hdr1()
113 {
114         local _as=${3:-$LUKS2_HDR_SIZE}
115         local _as=$((_as*512))
116         _dd if=$1 of=$2 bs=$_as seek=1 count=1 conv=notrunc
117 }
118
119 # write json (includes padding)
120 # 1:json_string 2:to 3:[json size (defaults to 12KiB)]
121 function write_luks2_json()
122 {
123         local _js=${3:-$LUKS2_JSON_SIZE}
124         local len=${#1}
125         echo -n -E "$1" > $2
126         truncate -s $((_js*512)) $2
127 }
128
129 function kill_bin_hdr()
130 {
131         printf "VACUUM" | _dd of=$1 bs=1 conv=notrunc
132 }
133
134 function erase_checksum()
135 {
136         _dd if=/dev/zero of=$1 bs=1 seek=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) count=$LUKS2_BIN_HDR_CHKS_LENGTH conv=notrunc
137 }
138
139 function read_sha256_checksum()
140 {
141         _dd if=$1 bs=1 skip=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) count=32 | xxd -c 32 -p
142 }
143
144 # 1 - string with checksum
145 function write_checksum()
146 {
147         test $# -eq 2 || return 1
148         test $((${#1}/2)) -le $LUKS2_BIN_HDR_CHKS_LENGTH || { echo "too long"; return 1; }
149
150         echo $1 | xxd -r -p | _dd of=$2 bs=1 seek=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) conv=notrunc
151 }
152
153 function calc_sha256_checksum_file()
154 {
155         sha256sum $1 | cut -d ' ' -f 1
156 }
157
158 function calc_sha256_checksum_stdin()
159 {
160         sha256sum - | cut -d ' ' -f 1
161 }
162
163 # merge bin hdr with json to form metadata area
164 # 1:bin_hdr 2:json 3:to 4:[json size (defaults to 12KiB)]
165 function merge_bin_hdr_with_json()
166 {
167         local _js=${4:-$LUKS2_JSON_SIZE}
168         local _js=$((_js*512/4096))
169         _dd if=$1 of=$3 bs=4096 count=1
170         _dd if=$2 of=$3 bs=4096 seek=1 count=$_js
171 }
172
173 function _dd()
174 {
175         dd $@ status=none
176 }
177
178 function write_bin_hdr_size() {
179         printf '%016x' $2 | xxd -r -p -l 16 | _dd of=$1 bs=8 count=1 seek=1 conv=notrunc
180 }
181
182 function write_bin_hdr_offset() {
183         printf '%016x' $2 | xxd -r -p -l 16 | _dd of=$1 bs=8 count=1 seek=32 conv=notrunc
184 }
185
186 # generic header helpers
187 # $TMPDIR/json0 - JSON hdr1
188 # $TMPDIR/json1 - JSON hdr2
189 # $TMPDIR/hdr0  - bin hdr1
190 # $TMPDIR/hdr1  - bin hdr2
191
192 # 1:target_dir 2:source_image
193 function lib_prepare()
194 {
195         test $# -eq 2 || exit 1
196
197         TGT_IMG=$1/$(test_img_name $0)
198         SRC_IMG=$2
199
200         # wipe checksums
201         CHKS0=0
202         CHKS1=0
203
204         cp $SRC_IMG $TGT_IMG
205         test -d $TMPDIR || mkdir $TMPDIR
206         read_luks2_json0 $TGT_IMG $TMPDIR/json0
207         read_luks2_json1 $TGT_IMG $TMPDIR/json1
208         read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
209         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
210 }
211
212 function lib_cleanup()
213 {
214         rm -f $TMPDIR/*
215         rm -fd $TMPDIR
216 }
217
218 function lib_mangle_json_hdr0()
219 {
220         local mda_sz=${1:-}
221         local jsn_sz=${2:-}
222         local kill_hdr=${3:-}
223
224         merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0 $jsn_sz
225         erase_checksum $TMPDIR/area0
226         CHKS0=$(calc_sha256_checksum_file $TMPDIR/area0)
227         write_checksum $CHKS0 $TMPDIR/area0
228         test -n "$kill_hdr" && kill_bin_hdr $TMPDIR/area0
229         write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG $mda_sz
230 }
231
232 function lib_mangle_json_hdr1()
233 {
234         local mda_sz=${1:-}
235         local jsn_sz=${2:-}
236         local kill_hdr=${3:-}
237
238         merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json1 $TMPDIR/area1 $jsn_sz
239         erase_checksum $TMPDIR/area1
240         CHKS1=$(calc_sha256_checksum_file $TMPDIR/area1)
241         write_checksum $CHKS1 $TMPDIR/area1
242         test -n "$kill_hdr" && kill_bin_hdr $TMPDIR/area1
243         write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG $mda_sz
244 }
245
246 function lib_mangle_json_hdr0_kill_hdr1()
247 {
248         lib_mangle_json_hdr0
249
250         kill_bin_hdr $TMPDIR/hdr1
251         write_luks2_hdr1 $TMPDIR/hdr1 $TGT_IMG
252 }
253
254 function lib_hdr0_killed()
255 {
256         local mda_sz=${1:-}
257
258         read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr_res0 $mda_sz
259         local str_res0=$(head -c 6 $TMPDIR/hdr_res0)
260         test "$str_res0" = "VACUUM"
261 }
262
263 function lib_hdr1_killed()
264 {
265         local mda_sz=${1:-}
266
267         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1 $mda_sz
268         local str_res1=$(head -c 6 $TMPDIR/hdr_res1)
269         test "$str_res1" = "VACUUM"
270 }
271
272 function lib_hdr0_checksum()
273 {
274         local chks_res0=$(read_sha256_checksum $TGT_IMG)
275         test "$CHKS0" = "$chks_res0"
276 }
277
278 function lib_hdr1_checksum()
279 {
280         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1
281         local chks_res1=$(read_sha256_checksum $TMPDIR/hdr_res1)
282         test "$CHKS1" = "$chks_res1"
283 }