Imported Upstream version 2.3.7
[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 repeat_str() {
24         printf "$1"'%.0s' $(eval "echo {1.."$(($2))"}");
25 }
26
27 function strindex()
28 {
29         local x="${1%%$2*}"
30         [[ $x = $1 ]] && echo -1 || echo ${#x}
31 }
32
33 function test_img_name()
34 {
35         local str=$(basename $1)
36         str=${str#generate-}
37         str=${str%%.sh}
38         echo $str
39 }
40
41 # read primary bin hdr
42 # 1:from 2:to
43 function read_luks2_bin_hdr0()
44 {
45         _dd if=$1 of=$2 bs=512 count=$LUKS2_BIN_HDR_SIZE
46 }
47
48 # read primary json area
49 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
50 function read_luks2_json0()
51 {
52         local _js=${4:-$LUKS2_JSON_SIZE}
53         local _js=$((_js*512/4096))
54         _dd if=$1 of=$2 bs=4096 skip=1 count=$_js
55 }
56
57 # read secondary bin hdr
58 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
59 function read_luks2_bin_hdr1()
60 {
61         _dd if=$1 of=$2 skip=${3:-$LUKS2_HDR_SIZE} bs=512 count=$LUKS2_BIN_HDR_SIZE
62 }
63
64 # read secondary json area
65 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
66 function read_luks2_json1()
67 {
68         local _js=${3:-$LUKS2_JSON_SIZE}
69         _dd if=$1 of=$2 bs=512 skip=$((2*LUKS2_BIN_HDR_SIZE+_js)) count=$_js
70 }
71
72 # read primary metadata area (bin + json)
73 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
74 function read_luks2_hdr_area0()
75 {
76         local _as=${3:-$LUKS2_HDR_SIZE}
77         local _as=$((_as*512))
78         _dd if=$1 of=$2 bs=$_as count=1
79 }
80
81 # read secondary metadata area (bin + json)
82 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
83 function read_luks2_hdr_area1()
84 {
85         local _as=${3:-$LUKS2_HDR_SIZE}
86         local _as=$((_as*512))
87         _dd if=$1 of=$2 bs=$_as skip=1 count=1
88 }
89
90 # write secondary bin hdr
91 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
92 function write_luks2_bin_hdr1()
93 {
94         _dd if=$1 of=$2 bs=512 seek=${3:-$LUKS2_HDR_SIZE} count=$LUKS2_BIN_HDR_SIZE conv=notrunc
95 }
96
97 # write primary metadata area (bin + json)
98 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
99 function write_luks2_hdr0()
100 {
101         local _as=${3:-$LUKS2_HDR_SIZE}
102         local _as=$((_as*512))
103         _dd if=$1 of=$2 bs=$_as count=1 conv=notrunc
104 }
105
106 # write secondary metadata area (bin + json)
107 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
108 function write_luks2_hdr1()
109 {
110         local _as=${3:-$LUKS2_HDR_SIZE}
111         local _as=$((_as*512))
112         _dd if=$1 of=$2 bs=$_as seek=1 count=1 conv=notrunc
113 }
114
115 # write json (includes padding)
116 # 1:json_string 2:to 3:[json size (defaults to 12KiB)]
117 function write_luks2_json()
118 {
119         local _js=${3:-$LUKS2_JSON_SIZE}
120         local len=${#1}
121         echo -n -E "$1" > $2
122         truncate -s $((_js*512)) $2
123 }
124
125 function kill_bin_hdr()
126 {
127         printf "VACUUM" | _dd of=$1 bs=1 conv=notrunc
128 }
129
130 function erase_checksum()
131 {
132         _dd if=/dev/zero of=$1 bs=1 seek=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) count=$LUKS2_BIN_HDR_CHKS_LENGTH conv=notrunc
133 }
134
135 function read_sha256_checksum()
136 {
137         _dd if=$1 bs=1 skip=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) count=32 | xxd -c 32 -p
138 }
139
140 # 1 - string with checksum
141 function write_checksum()
142 {
143         test $# -eq 2 || return 1
144         test $((${#1}/2)) -le $LUKS2_BIN_HDR_CHKS_LENGTH || { echo "too long"; return 1; }
145
146         echo $1 | xxd -r -p | _dd of=$2 bs=1 seek=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) conv=notrunc
147 }
148
149 function calc_sha256_checksum_file()
150 {
151         sha256sum $1 | cut -d ' ' -f 1
152 }
153
154 function calc_sha256_checksum_stdin()
155 {
156         sha256sum - | cut -d ' ' -f 1
157 }
158
159 # merge bin hdr with json to form metadata area
160 # 1:bin_hdr 2:json 3:to 4:[json size (defaults to 12KiB)]
161 function merge_bin_hdr_with_json()
162 {
163         local _js=${4:-$LUKS2_JSON_SIZE}
164         local _js=$((_js*512/4096))
165         _dd if=$1 of=$3 bs=4096 count=1
166         _dd if=$2 of=$3 bs=4096 seek=1 count=$_js
167 }
168
169 function _dd()
170 {
171         dd $@ status=none
172 }
173
174 function write_bin_hdr_size() {
175         printf '%016x' $2 | xxd -r -p -l 16 | _dd of=$1 bs=8 count=1 seek=1 conv=notrunc
176 }
177
178 function write_bin_hdr_offset() {
179         printf '%016x' $2 | xxd -r -p -l 16 | _dd of=$1 bs=8 count=1 seek=32 conv=notrunc
180 }