Debian Codegraph @

src/luks/tests/regen-hash-luks2

raw
1 #!/bin/bash -x
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
20 TEST="${0}"
21 . luks-common-test-functions
22 . clevis-luks-common-functions
23
24 function on_exit() {
25 [ -d "${TMP}" ] || return 0
26 tang_stop "${TMP}"
27 rm -rf "${TMP}"
28 }
29
30 trap 'on_exit' EXIT
31
32 TMP=$(mktemp -d)
33
34 # LUKS2.
35 if ! luks2_supported; then
36 skip_test "LUKS2 is not supported."
37 fi
38
39 tang_run "${TMP}"
40 port=$(tang_get_port "${TMP}")
41
42 url="http://localhost:${port}"
43 adv="${TMP}/adv"
44 tang_get_adv "${port}" "${adv}"
45
46 cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
47
48 # LUKS2 with sha512.
49 DEV="${TMP}/luks2-device-sha512"
50 new_device_hash "luks2" "${DEV}" "sha512"
51
52 # Verify the device was created with sha512.
53 hash_before=$(cryptsetup luksDump "${DEV}" \
54 | sed -rn 's|^\s+Hash:\s+(\S+)$|\1|p' | head -1)
55 if [ "${hash_before}" != "sha512" ]; then
56 error "${TEST}: Device should have been formatted with sha512, got ${hash_before}."
57 fi
58
59 if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
60 error "${TEST}: Bind should have succeeded."
61 fi
62
63 # Verify the clevis slot (slot 1) also uses sha512 after binding.
64 hash_after_bind=$(cryptsetup luksDump "${DEV}" \
65 | awk '/^[[:space:]]+1: luks/{found=1} found && /Hash:/{print $2; exit}')
66 if [ "${hash_after_bind}" != "sha512" ]; then
67 error "${TEST}: After binding, clevis slot hash should be sha512, got ${hash_after_bind}."
68 fi
69
70 # Now let's remove the initial passphrase.
71 if ! cryptsetup luksRemoveKey --batch-mode "${DEV}" <<< "${DEFAULT_PASS}"; then
72 error "${TEST}: error removing the default password from ${DEV}."
73 fi
74
75 # Making sure we have a single slot enabled.
76 enabled=$(clevis_luks_used_slots "${DEV}" | wc -l)
77 if [ "${enabled}" -ne 1 ]; then
78 error "${TEST}: we should have only one slot enabled (${enabled})."
79 fi
80
81 SLT=1
82 old_key=$(clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}")
83
84 # Now let's try regen.
85 if ! clevis luks regen -q -d "${DEV}" -s "${SLT}"; then
86 error "${TEST}: clevis luks regen failed"
87 fi
88
89 new_key=$(clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}")
90
91 if [ "${old_key}" = "${new_key}" ]; then
92 error "${TEST}: the passphrases should be different"
93 fi
94
95 # Verify the clevis slot (slot 1) still uses sha512 after regen.
96 hash_after_regen=$(cryptsetup luksDump "${DEV}" \
97 | awk '/^[[:space:]]+1: luks/{found=1} found && /Hash:/{print $2; exit}')
98 if [ "${hash_after_regen}" != "sha512" ]; then
99 error "${TEST}: After regen, clevis slot hash should be sha512, got ${hash_after_regen}."
100 fi
101
102 # Also check via clevis_luks_get_hash.
103 hash_fn=$(clevis_luks_get_hash "${DEV}")
104 if [ "${hash_fn}" != "sha512" ]; then
105 error "${TEST}: clevis_luks_get_hash should return sha512, got ${hash_fn}."
106 fi