]> git.proxmox.com Git - swtpm.git/blame - tests/_test_hashing2
tests: Set test-check local user.name and user.email before git am
[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}"
11export TPM_PATH=$(mktemp -d)
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
60swtpm_open_cmddev ${SWTPM_INTERFACE} 100
61RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
62exp=' 00 c4 00 00 00 0a 00 00 00 00'
63if [ "$RES" != "$exp" ]; then
64 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
65 echo "expected: $exp"
66 echo "received: $RES"
67 exit 1
68fi
69
70# Check the TPM Established bit before the hashing
71RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
72if [ $? -ne 0 ]; then
73 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
74 exit 1
75fi
76exp='tpmEstablished is 0'
77if [ "$RES" != "$exp" ]; then
78 echo "Error (1): TPM Established flag has wrong value."
79 echo "expected: $exp"
80 echo "received: $RES"
81 exit 1
82fi
83
84run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
85if [ $? -ne 0 ]; then
86 echo "Error: Hash command did not work."
87 exit 1
88fi
89
90# Read PCR 17
91swtpm_open_cmddev ${SWTPM_INTERFACE} 100
92RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
93exp=' 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'
94if [ "$RES" != "$exp" ]; then
95 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
96 echo "expected: $exp"
97 echo "received: $RES"
98 exit 1
99fi
100
101# Check the TPM Established bit after the hashing
102RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
103if [ $? -ne 0 ]; then
104 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
105 exit 1
106fi
107exp='tpmEstablished is 1'
108if [ "$RES" != "$exp" ]; then
109 echo "Error (2): TPM Established flag has wrong value."
110 echo "expected: $exp"
111 echo "received: $RES"
112 exit 1
113fi
114
115# Reset the establishment bit; we switch to locality 0 and reset via locality 3
116run_swtpm_ioctl ${SWTPM_INTERFACE} -l 0
117if [ $? -ne 0 ]; then
118 echo "Error: Could not set locality 0"
119 exit 1
120fi
121
122for ((l = 0; l <= 2; l++)); do
123 # Resetting via locality 2 must fail
f759520c 124 ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l 2>&1)"
01aa2ed3
SB
125 if [ $? -eq 0 ]; then
126 echo "Error: Could reset the establishment bit via locality $l"
127 exit 1
128 fi
f759520c
SB
129 exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d"
130 if [ "$ERR" != "$exp" ]; then
131 echo "Error: Unexpected error message"
132 echo "Received: $ERR"
133 echo "Expected: $exp"
134 exit 1
135 fi
01aa2ed3
SB
136done
137
138# We expect the same results for the TPM_ResetEstablishment command
139for ((l = 0; l <= 2; l++)); do
140 # Set locality
141 run_swtpm_ioctl ${SWTPM_INTERFACE} -l $l
142 if [ $? -ne 0 ]; then
143 echo "Error: Could not choose locality $l"
144 exit 1
145 fi
146 # Have to use external echo command
147 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
148 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
149 exp=' 00 c4 00 00 00 0a 00 00 00 3d'
150 if [ "$RES" != "$exp" ]; then
151 echo "Error: Could reset TPM establishment bit in locality $l using command"
152 echo "expected: $exp"
153 echo "received: $RES"
154 exit 1
155 fi
156done
157
158# Resetting via locality 3 must work
159run_swtpm_ioctl ${SWTPM_INTERFACE} -l 3
160if [ $? -ne 0 ]; then
161 echo "Error: Could not reset the establishment bit via locality 3"
162 exit 1
163fi
164
165swtpm_open_cmddev ${SWTPM_INTERFACE} 100
166RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
167exp=' 00 c4 00 00 00 0a 00 00 00 00'
168if [ "$RES" != "$exp" ]; then
169 echo "Error: Could reset TPM establishment bit in locality 3 using command"
170 echo "expected: $exp"
171 echo "received: $RES"
172 exit 1
173fi
174
175# Check the TPM Established bit after the reset
176RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
177exp='tpmEstablished is 0'
178if [ "$RES" != "$exp" ]; then
179 echo "Error (3): TPM Established flag has wrong value."
180 echo "expected: $exp"
181 echo "received: $RES"
182 exit 1
183fi
184
185# Read from a file
186dd if=/dev/zero bs=1024 count=1024 2>/dev/null |\
187 run_swtpm_ioctl ${SWTPM_INTERFACE} -h -
188
189# Read PCR 17
190swtpm_open_cmddev ${SWTPM_INTERFACE} 100
191RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
192exp=' 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'
193if [ "$RES" != "$exp" ]; then
194 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
195 echo "expected: $exp"
196 echo "received: $RES"
197 exit 1
198fi
199
200run_swtpm_ioctl ${SWTPM_INTERFACE} -s
201if [ $? -ne 0 ]; then
202 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
203 exit 1
204fi
205
45d2d092 206if wait_process_gone ${SWTPM_PID} 4; then
01aa2ed3
SB
207 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
208 exit 1
209fi
210
211if [ ! -e $STATE_FILE ]; then
212 echo "Error: TPM state file $STATE_FILE does not exist."
213 exit 1
214fi
215
216echo "OK"
217
218exit 0