]> git.proxmox.com Git - swtpm.git/blame - tests/_test_hashing
tests: Apply patches to IBM TSS2 test suite
[swtpm.git] / tests / _test_hashing
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-hashing"
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: Could not initialize the ${SWTPM_INTERFACE} TPM."
48 exit 1
49fi
50
47c7ea77 51kill_quiet -0 ${SWTPM_PID} 2>/dev/null
01aa2ed3
SB
52if [ $? -ne 0 ]; then
53 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
54 exit 1
55fi
56
57# Startup the TPM
58swtpm_open_cmddev ${SWTPM_INTERFACE} 100
59RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
60exp=' 00 c4 00 00 00 0a 00 00 00 00'
61if [ "$RES" != "$exp" ]; then
62 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
63 echo "expected: $exp"
64 echo "received: $RES"
65 exit 1
66fi
67
68# Check the TPM Established bit before the hashing
69RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
70if [ $? -ne 0 ]; then
71 echo "Error: Could not get the TPM Established flag."
72 exit 1
73fi
74exp='tpmEstablished is 0'
75if [ "$RES" != "$exp" ]; then
76 echo "Error (1): TPM Established flag has wrong value."
77 echo "expected: $exp"
78 echo "received: $RES"
79 exit 1
80fi
81
82run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
83if [ $? -ne 0 ]; then
84 echo "Error: Hash command did not work."
85 exit 1
86fi
87
88# Read PCR 17
89swtpm_open_cmddev ${SWTPM_INTERFACE} 100
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."
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 via locality 3
114run_swtpm_ioctl ${SWTPM_INTERFACE} -l 3
115if [ $? -ne -0 ]; then
116 echo "Error: Could not set locality 3"
117 exit 1
118fi
119
120# \x40 or \x0B seems to confuse 'normal' echo
121swtpm_open_cmddev ${SWTPM_INTERFACE} 100
122RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0A\x40\x00\x00\x0B')
123exp=' 00 c4 00 00 00 0a 00 00 00 00'
124if [ "$RES" != "$exp" ]; then
125 echo "Error: Could not reset the establishment bit"
126 echo "expected: $exp"
127 echo "received: $RES"
128 exit 1
129fi
130
131# Check the TPM Established bit after the reset
132RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
133if [ $? -ne 0 ]; then
134 echo "Error: Could not get the TPM Established flag."
135 exit 1
136fi
137exp='tpmEstablished is 0'
138if [ "$RES" != "$exp" ]; then
139 echo "Error (3): TPM Established flag has wrong value."
140 echo "expected: $exp"
141 echo "received: $RES"
142 exit 1
143fi
144
145# back to locality 0
146run_swtpm_ioctl ${SWTPM_INTERFACE} -l 0
147if [ $? -ne 0 ]; then
148 echo "Error: Could not set locality 0."
149 exit 1
150fi
151
152# Read from a file
153dd if=/dev/zero bs=1024 count=1024 2>/dev/null| \
154 run_swtpm_ioctl ${SWTPM_INTERFACE} -h -
155
156# Read PCR 17
157swtpm_open_cmddev ${SWTPM_INTERFACE} 100
158RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
159exp=' 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'
160if [ "$RES" != "$exp" ]; then
161 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
162 echo "expected: $exp"
163 echo "received: $RES"
164 exit 1
165fi
166
167run_swtpm_ioctl ${SWTPM_INTERFACE} -s
168if [ $? -ne 0 ]; then
169 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
170 exit 1
171fi
172
45d2d092 173if wait_process_gone ${SWTPM_PID} 4; then
01aa2ed3
SB
174 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
175 exit 1
176fi
177
178if [ ! -e $STATE_FILE ]; then
179 echo "Error: TPM state file $STATE_FILE does not exist."
180 exit 1
181fi
182
183echo "OK"
184
185exit 0