Debian Codegraph @

src/luks/tests/edit-hash-luks2

raw
1 #!/bin/bash -ex
2 #
3 # Copyright (c) 2026 Red Hat, Inc.
4 # Author: Sergio Arroutbi <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 TEST=$(basename "${0}")
20 . luks-common-test-functions
21
22 . clevis-luks-common-functions
23
24 on_exit() {
25 local d
26 for d in "${TMP}" "${TMP2}"; do
27 [ ! -d "${d}" ] && continue
28 tang_stop "${d}"
29 rm -rf "${d}"
30 done
31 }
32
33 trap 'on_exit' EXIT
34 trap 'on_exit' ERR
35
36 TMP="$(mktemp -d)"
37
38 # LUKS2.
39 if ! luks2_supported; then
40 skip_test "LUKS2 is not supported."
41 fi
42
43 tang_run "${TMP}"
44 port=$(tang_get_port "${TMP}")
45
46 url="http://localhost:${port}"
47
48 cfg=$(printf '{"url":"%s"}' "${url}")
49
50 # LUKS2 with sha512.
51 DEV="${TMP}/luks2-device-sha512"
52 new_device_hash "luks2" "${DEV}" "sha512"
53
54 # Verify the device was created with sha512.
55 hash_before=$(cryptsetup luksDump "${DEV}" \
56 | sed -rn 's|^\s+Hash:\s+(\S+)$|\1|p' | head -1)
57 if [ "${hash_before}" != "sha512" ]; then
58 error "${TEST}: Device should have been formatted with sha512, got ${hash_before}."
59 fi
60
61 if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
62 error "${TEST}: Bind should have succeeded."
63 fi
64
65 # Verify the clevis slot (slot 1) uses sha512 after binding.
66 hash_after_bind=$(cryptsetup luksDump "${DEV}" \
67 | awk '/^[[:space:]]+1: luks/{found=1} found && /Hash:/{print $2; exit}')
68 if [ "${hash_after_bind}" != "sha512" ]; then
69 error "${TEST}: After binding, clevis slot hash should be sha512, got ${hash_after_bind}."
70 fi
71
72 # Now let's have another tang instance running and change the config to use
73 # the new one.
74 TMP2="$(mktemp -d)"
75 tang_run "${TMP2}"
76 port2=$(tang_get_port "${TMP2}")
77 new_url="http://localhost:${port2}"
78 new_cfg=$(printf '{"url":"%s"}' "${new_url}")
79
80 if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
81 error "${TEST}: edit should have succeeded."
82 fi
83
84 # Verify the clevis slot (slot 1) still uses sha512 after edit.
85 hash_after_edit=$(cryptsetup luksDump "${DEV}" \
86 | awk '/^[[:space:]]+1: luks/{found=1} found && /Hash:/{print $2; exit}')
87 if [ "${hash_after_edit}" != "sha512" ]; then
88 error "${TEST}: After edit, clevis slot hash should be sha512, got ${hash_after_edit}."
89 fi
90
91 # Also check via clevis_luks_get_hash.
92 hash_fn=$(clevis_luks_get_hash "${DEV}")
93 if [ "${hash_fn}" != "sha512" ]; then
94 error "${TEST}: clevis_luks_get_hash should return sha512, got ${hash_fn}."
95 fi
96
97 # Make sure we can still unlock the device.
98 if ! clevis_luks_unlock_device "${DEV}" >/dev/null; then
99 error "${TEST}: we should have been able to unlock the device"
100 fi