Debian Codegraph @

src/luks/tests/bind-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
20 TEST=$(basename "${0}")
21 . luks-common-test-functions
22
23 . clevis-luks-common-functions
24
25 on_exit() {
26 [ -d "${TMP}" ] && rm -rf "${TMP}"
27 }
28
29 trap 'on_exit' EXIT
30 trap 'exit' ERR
31
32 TMP="$(mktemp -d)"
33
34 ADV="${TMP}/adv.jws"
35 tang_create_adv "${TMP}" "${ADV}"
36 CFG="$(printf '{"url":"foobar","adv":"%s"}' "$ADV")"
37
38 # LUKS2 with sha512.
39 DEV="${TMP}/luks2-device-sha512"
40 if ! luks2_supported; then
41 skip_test "LUKS2 is not supported."
42 fi
43
44 new_device_hash "luks2" "${DEV}" "sha512"
45
46 # Verify the device was created with sha512.
47 hash_before=$(cryptsetup luksDump "${DEV}" \
48 | sed -rn 's|^\s+Hash:\s+(\S+)$|\1|p' | head -1)
49 if [ "${hash_before}" != "sha512" ]; then
50 error "${TEST}: Device should have been formatted with sha512, got ${hash_before}."
51 fi
52
53 if ! clevis luks bind -f -d "${DEV}" tang "${CFG}" <<< "${DEFAULT_PASS}"; then
54 error "${TEST}: Binding is expected to succeed."
55 fi
56
57 # After binding, check that the new clevis slot also uses sha512.
58 # For LUKS2, we check the hash of the clevis key slot (slot 1).
59 # LUKS2 luksDump format lists slots as " 1: luks2", not "Key Slot 1".
60 hash_slot1=$(cryptsetup luksDump "${DEV}" \
61 | awk '/^[[:space:]]+1: luks/{found=1} found && /Hash:/{print $2; exit}')
62 if [ "${hash_slot1}" != "sha512" ]; then
63 error "${TEST}: Clevis slot hash should be sha512, got ${hash_slot1}."
64 fi
65
66 # Also check via clevis_luks_get_hash.
67 hash_fn=$(clevis_luks_get_hash "${DEV}")
68 if [ "${hash_fn}" != "sha512" ]; then
69 error "${TEST}: clevis_luks_get_hash should return sha512, got ${hash_fn}."
70 fi