Debian Codegraph @

src/luks/tests/regen-hash-luks1

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 tang_run "${TMP}"
35 port=$(tang_get_port "${TMP}")
36
37 url="http://localhost:${port}"
38 adv="${TMP}/adv"
39 tang_get_adv "${port}" "${adv}"
40
41 cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
42
43 # LUKS1 with sha512.
44 DEV="${TMP}/luks1-device-sha512"
45 new_device_hash "luks1" "${DEV}" "sha512"
46
47 # Verify the device was created with sha512.
48 hash_before=$(cryptsetup luksDump "${DEV}" \
49 | sed -rn 's|^Hash spec:\s+(\S+)$|\1|p')
50 if [ "${hash_before}" != "sha512" ]; then
51 error "${TEST}: Device should have been formatted with sha512, got ${hash_before}."
52 fi
53
54 if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
55 error "${TEST}: Bind should have succeeded."
56 fi
57
58 # Verify hash is still sha512 after binding.
59 hash_after_bind=$(cryptsetup luksDump "${DEV}" \
60 | sed -rn 's|^Hash spec:\s+(\S+)$|\1|p')
61 if [ "${hash_after_bind}" != "sha512" ]; then
62 error "${TEST}: After binding, hash should be sha512, got ${hash_after_bind}."
63 fi
64
65 # Now let's remove the initial passphrase.
66 if ! cryptsetup luksRemoveKey --batch-mode "${DEV}" <<< "${DEFAULT_PASS}"; then
67 error "${TEST}: error removing the default password from ${DEV}."
68 fi
69
70 # Making sure we have a single slot enabled.
71 enabled=$(clevis_luks_used_slots "${DEV}" | wc -l)
72 if [ "${enabled}" -ne 1 ]; then
73 error "${TEST}: we should have only one slot enabled (${enabled})."
74 fi
75
76 SLT=1
77 old_key=$(clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}")
78
79 # Now let's try regen.
80 if ! clevis luks regen -q -d "${DEV}" -s "${SLT}"; then
81 error "${TEST}: clevis luks regen failed"
82 fi
83
84 new_key=$(clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}")
85
86 if [ "${old_key}" = "${new_key}" ]; then
87 error "${TEST}: the passphrases should be different"
88 fi
89
90 # Verify hash is still sha512 after regen.
91 hash_after_regen=$(cryptsetup luksDump "${DEV}" \
92 | sed -rn 's|^Hash spec:\s+(\S+)$|\1|p')
93 if [ "${hash_after_regen}" != "sha512" ]; then
94 error "${TEST}: After regen, hash should be sha512, got ${hash_after_regen}."
95 fi
96
97 # Also check via clevis_luks_get_hash.
98 hash_fn=$(clevis_luks_get_hash "${DEV}")
99 if [ "${hash_fn}" != "sha512" ]; then
100 error "${TEST}: clevis_luks_get_hash should return sha512, got ${hash_fn}."
101 fi