Debian Codegraph @

src/luks/tests/get-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 if ! luks2_supported; then
25 skip_test "LUKS2 is not supported."
26 fi
27
28 on_exit() {
29 [ -d "${TMP}" ] && rm -rf "${TMP}"
30 }
31
32 trap 'on_exit' EXIT
33 trap 'exit' ERR
34
35 TMP="$(mktemp -d)"
36
37 # Test 1: LUKS2 device with default hash (sha256) — verify correct extraction.
38 DEV="${TMP}/luks2-device-sha256"
39 new_device_hash "luks2" "${DEV}" "sha256"
40
41 hash=$(clevis_luks_get_hash "${DEV}")
42 if [ "${hash}" != "sha256" ]; then
43 error "${TEST}: expected sha256, got '${hash}'."
44 fi
45
46 # Test 2: LUKS2 device with sha512 — verify correct extraction.
47 DEV="${TMP}/luks2-device-sha512"
48 new_device_hash "luks2" "${DEV}" "sha512"
49
50 hash=$(clevis_luks_get_hash "${DEV}")
51 if [ "${hash}" != "sha512" ]; then
52 error "${TEST}: expected sha512, got '${hash}'."
53 fi
54
55 # Test 3: LUKS2 device with sha384 — verify correct extraction.
56 DEV="${TMP}/luks2-device-sha384"
57 new_device_hash "luks2" "${DEV}" "sha384"
58
59 hash=$(clevis_luks_get_hash "${DEV}")
60 if [ "${hash}" != "sha384" ]; then
61 error "${TEST}: expected sha384, got '${hash}'."
62 fi
63
64 # Test 4: LUKS2 — verify we get keyslot hash, not digest hash.
65 # Create device with sha512. The digest section uses sha256 by default.
66 # Our function must return the keyslot hash (sha512), not the digest hash.
67 DEV="${TMP}/luks2-device-keyslot-check"
68 new_device_hash "luks2" "${DEV}" "sha512"
69
70 # Verify that the digest section does contain a different hash.
71 digest_hash=$(cryptsetup luksDump "${DEV}" \
72 | awk '/^Digests:/{in_digest=1} in_digest && /Hash:/{print $2; exit}')
73
74 keyslot_hash=$(clevis_luks_get_hash "${DEV}")
75 if [ "${keyslot_hash}" != "sha512" ]; then
76 error "${TEST}: keyslot hash should be sha512, got '${keyslot_hash}'."
77 fi
78
79 # If the digest hash differs, this confirms we are targeting the right section.
80 if [ -n "${digest_hash}" ] && [ "${digest_hash}" != "sha512" ]; then
81 echo "${TEST}: confirmed digest hash (${digest_hash}) differs from" \
82 "keyslot hash (${keyslot_hash}) — extraction targets correct section."
83 fi
84
85 # Test 5: LUKS2 with sha1 — verify correct extraction.
86 DEV="${TMP}/luks2-device-sha1"
87 new_device_hash "luks2" "${DEV}" "sha1"
88
89 hash=$(clevis_luks_get_hash "${DEV}")
90 if [ "${hash}" != "sha1" ]; then
91 error "${TEST}: expected sha1, got '${hash}'."
92 fi