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
14 LUKS2_BIN_HDR_SIZE=8 # 4 KiB
15 LUKS2_JSON_SIZE=$((LUKS2_HDR_SIZE-LUKS2_BIN_HDR_SIZE))
17 LUKS2_BIN_HDR_CHKS_OFFSET=0x1C0
18 LUKS2_BIN_HDR_CHKS_LENGTH=64
20 [ -z "$srcdir" ] && srcdir="."
24 printf "$1"'%.0s' $(eval "echo {1.."$(($2))"}");
30 [[ $x = $1 ]] && echo -1 || echo ${#x}
33 function test_img_name()
35 local str=$(basename $1)
41 # read primary bin hdr
43 function read_luks2_bin_hdr0()
45 _dd if=$1 of=$2 bs=512 count=$LUKS2_BIN_HDR_SIZE
48 # read primary json area
49 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
50 function read_luks2_json0()
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
57 # read secondary bin hdr
58 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
59 function read_luks2_bin_hdr1()
61 _dd if=$1 of=$2 skip=${3:-$LUKS2_HDR_SIZE} bs=512 count=$LUKS2_BIN_HDR_SIZE
64 # read secondary json area
65 # 1:from 2:to 3:[json only size (defaults to 12KiB)]
66 function read_luks2_json1()
68 local _js=${3:-$LUKS2_JSON_SIZE}
69 _dd if=$1 of=$2 bs=512 skip=$((2*LUKS2_BIN_HDR_SIZE+_js)) count=$_js
72 # read primary metadata area (bin + json)
73 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
74 function read_luks2_hdr_area0()
76 local _as=${3:-$LUKS2_HDR_SIZE}
77 local _as=$((_as*512))
78 _dd if=$1 of=$2 bs=$_as count=1
81 # read secondary metadata area (bin + json)
82 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
83 function read_luks2_hdr_area1()
85 local _as=${3:-$LUKS2_HDR_SIZE}
86 local _as=$((_as*512))
87 _dd if=$1 of=$2 bs=$_as skip=1 count=1
90 # write secondary bin hdr
91 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
92 function write_luks2_bin_hdr1()
94 _dd if=$1 of=$2 bs=512 seek=${3:-$LUKS2_HDR_SIZE} count=$LUKS2_BIN_HDR_SIZE conv=notrunc
97 # write primary metadata area (bin + json)
98 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
99 function write_luks2_hdr0()
101 local _as=${3:-$LUKS2_HDR_SIZE}
102 local _as=$((_as*512))
103 _dd if=$1 of=$2 bs=$_as count=1 conv=notrunc
106 # write secondary metadata area (bin + json)
107 # 1:from 2:to 3:[metadata size (defaults to 16KiB)]
108 function write_luks2_hdr1()
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
115 # write json (includes padding)
116 # 1:json_string 2:to 3:[json size (defaults to 12KiB)]
117 function write_luks2_json()
119 local _js=${3:-$LUKS2_JSON_SIZE}
122 truncate -s $((_js*512)) $2
125 function kill_bin_hdr()
127 printf "VACUUM" | _dd of=$1 bs=1 conv=notrunc
130 function erase_checksum()
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
135 function read_sha256_checksum()
137 _dd if=$1 bs=1 skip=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) count=32 | xxd -c 32 -p
140 # 1 - string with checksum
141 function write_checksum()
143 test $# -eq 2 || return 1
144 test $((${#1}/2)) -le $LUKS2_BIN_HDR_CHKS_LENGTH || { echo "too long"; return 1; }
146 echo $1 | xxd -r -p | _dd of=$2 bs=1 seek=$(printf %d $LUKS2_BIN_HDR_CHKS_OFFSET) conv=notrunc
149 function calc_sha256_checksum_file()
151 sha256sum $1 | cut -d ' ' -f 1
154 function calc_sha256_checksum_stdin()
156 sha256sum - | cut -d ' ' -f 1
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()
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
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
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