]> git.proxmox.com Git - swtpm.git/blame - tests/_test_hashing2
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / _test_hashing2
CommitLineData
01aa2ed3
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4#set -x
5
313cf75c
SB
6ROOT=${abs_top_builddir:-$(pwd)/..}
7TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
01aa2ed3
SB
9VTPM_NAME="vtpm-test-hashing2"
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
cce7503c 11export TPM_PATH="$(mktemp -d)" || exit 1
01aa2ed3
SB
12STATE_FILE=$TPM_PATH/tpm-00.permall
13VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
15SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
16SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
17
18function cleanup()
19{
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
47c7ea77 22 kill_quiet -9 $pid
01aa2ed3
SB
23 fi
24 rm -rf $TPM_PATH
25}
26
27trap "cleanup" EXIT
28
313cf75c
SB
29[ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
30source ${TESTDIR}/common
01aa2ed3
SB
31
32rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
33
34run_swtpm ${SWTPM_INTERFACE}
35
100317d5 36display_processes_by_name "$SWTPM"
01aa2ed3 37
47c7ea77 38kill_quiet -0 ${SWTPM_PID}
01aa2ed3
SB
39if [ $? -ne 0 ]; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42fi
43
44# Init the TPM
45run_swtpm_ioctl ${SWTPM_INTERFACE} -i
46if [ $? -ne 0 ]; then
47 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
48 exit 1
49fi
50
51sleep 0.5
52
47c7ea77 53kill_quiet -0 ${SWTPM_PID} 2>/dev/null
01aa2ed3
SB
54if [ $? -ne 0 ]; then
55 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
56 exit 1
57fi
58
59# Startup the TPM
01aa2ed3
SB
60RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
61exp=' 00 c4 00 00 00 0a 00 00 00 00'
62if [ "$RES" != "$exp" ]; then
63 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
64 echo "expected: $exp"
65 echo "received: $RES"
66 exit 1
67fi
68
69# Check the TPM Established bit before the hashing
70RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
71if [ $? -ne 0 ]; then
72 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
73 exit 1
74fi
75exp='tpmEstablished is 0'
76if [ "$RES" != "$exp" ]; then
77 echo "Error (1): TPM Established flag has wrong value."
78 echo "expected: $exp"
79 echo "received: $RES"
80 exit 1
81fi
82
83run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
84if [ $? -ne 0 ]; then
85 echo "Error: Hash command did not work."
86 exit 1
87fi
88
89# Read PCR 17
01aa2ed3
SB
90RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
91exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
92if [ "$RES" != "$exp" ]; then
93 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
94 echo "expected: $exp"
95 echo "received: $RES"
96 exit 1
97fi
98
99# Check the TPM Established bit after the hashing
100RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
101if [ $? -ne 0 ]; then
102 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
103 exit 1
104fi
105exp='tpmEstablished is 1'
106if [ "$RES" != "$exp" ]; then
107 echo "Error (2): TPM Established flag has wrong value."
108 echo "expected: $exp"
109 echo "received: $RES"
110 exit 1
111fi
112
113# Reset the establishment bit; we switch to locality 0 and reset via locality 3
114run_swtpm_ioctl ${SWTPM_INTERFACE} -l 0
115if [ $? -ne 0 ]; then
116 echo "Error: Could not set locality 0"
117 exit 1
118fi
119
120for ((l = 0; l <= 2; l++)); do
121 # Resetting via locality 2 must fail
f759520c 122 ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l 2>&1)"
01aa2ed3
SB
123 if [ $? -eq 0 ]; then
124 echo "Error: Could reset the establishment bit via locality $l"
125 exit 1
126 fi
f759520c
SB
127 exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d"
128 if [ "$ERR" != "$exp" ]; then
129 echo "Error: Unexpected error message"
130 echo "Received: $ERR"
131 echo "Expected: $exp"
132 exit 1
133 fi
01aa2ed3
SB
134done
135
136# We expect the same results for the TPM_ResetEstablishment command
137for ((l = 0; l <= 2; l++)); do
138 # Set locality
139 run_swtpm_ioctl ${SWTPM_INTERFACE} -l $l
140 if [ $? -ne 0 ]; then
141 echo "Error: Could not choose locality $l"
142 exit 1
143 fi
144 # Have to use external echo command
01aa2ed3
SB
145 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
146 exp=' 00 c4 00 00 00 0a 00 00 00 3d'
147 if [ "$RES" != "$exp" ]; then
148 echo "Error: Could reset TPM establishment bit in locality $l using command"
149 echo "expected: $exp"
150 echo "received: $RES"
151 exit 1
152 fi
153done
154
155# Resetting via locality 3 must work
156run_swtpm_ioctl ${SWTPM_INTERFACE} -l 3
157if [ $? -ne 0 ]; then
158 echo "Error: Could not reset the establishment bit via locality 3"
159 exit 1
160fi
161
01aa2ed3
SB
162RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
163exp=' 00 c4 00 00 00 0a 00 00 00 00'
164if [ "$RES" != "$exp" ]; then
165 echo "Error: Could reset TPM establishment bit in locality 3 using command"
166 echo "expected: $exp"
167 echo "received: $RES"
168 exit 1
169fi
170
171# Check the TPM Established bit after the reset
172RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
173exp='tpmEstablished is 0'
174if [ "$RES" != "$exp" ]; then
175 echo "Error (3): TPM Established flag has wrong value."
176 echo "expected: $exp"
177 echo "received: $RES"
178 exit 1
179fi
180
181# Read from a file
182dd if=/dev/zero bs=1024 count=1024 2>/dev/null |\
183 run_swtpm_ioctl ${SWTPM_INTERFACE} -h -
184
185# Read PCR 17
01aa2ed3
SB
186RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
187exp=' 00 c4 00 00 00 1e 00 00 00 00 d8 0e 7a 7b 3c 37 88 7d b4 c2 88 08 1d a7 53 f6 4b 11 3a 9c'
188if [ "$RES" != "$exp" ]; then
189 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
190 echo "expected: $exp"
191 echo "received: $RES"
192 exit 1
193fi
194
195run_swtpm_ioctl ${SWTPM_INTERFACE} -s
196if [ $? -ne 0 ]; then
197 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
198 exit 1
199fi
200
45d2d092 201if wait_process_gone ${SWTPM_PID} 4; then
01aa2ed3
SB
202 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
203 exit 1
204fi
205
206if [ ! -e $STATE_FILE ]; then
207 echo "Error: TPM state file $STATE_FILE does not exist."
208 exit 1
209fi
210
211echo "OK"
212
213exit 0