Skip to content

Commit

Permalink
Python 脚本直接测试 TPM Samulator
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqun committed Mar 25, 2017
1 parent 417f0ff commit 6caeb95
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test_talking_with_simulator/simulator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
##To Build and Test the TPM 2.0 Simulator:

1. Get the TPM 2.0 simulator, 1.24 version, from the TCG web site, trustedcomputinggroup.org, and install it (this is only possible if you or your company is a TCG member):
1. Go to www.trustedcomputinggroup.org
1. Click on the member login link (you will have to sign up if you aren't already)
1. Go to groups' TPMWG' Documents ' Filter, and search for "TPM 2.0 VS". The latest one as of this writing is 1.24.
1. This will give you the source code, so you will have to follow the instructions for building it.
1. In Visual Studio 2012, open TPMcmd\simulator.sln solution file and build it.
1. Copy libeay32.dll (from OpenSSL) into TPMcmd\debug directory.
1. Run TPMcmd\debug\simulator.exe
1. To test it the following python script can be used.
NOTE: you may have to cut and paste these commands into Python interpreter one by one. I'm not a python expert, and I couldn't get the script to just run:

```
import os
import sys
import socket
from socket import socket, AF_INET, SOCK_STREAM
platformSock = socket(AF_INET, SOCK_STREAM)
platformSock.connect(('localhost', 2322))
platformSock.send('\0\0\0\1')
tpmSock = socket(AF_INET, SOCK_STREAM)
tpmSock.connect(('localhost', 2321))
# Send TPM_SEND_COMMAND
tpmSock.send('\x00\x00\x00\x08')
# Send locality
tpmSock.send('\x03')
# Send # of bytes
tpmSock.send('\x00\x00\x00\x0c')
# Send tag
tpmSock.send('\x80\x01')
# Send command size
tpmSock.send('\x00\x00\x00\x0c')
# Send command code: TPMStartup
tpmSock.send('\x00\x00\x01\x44')
# Send TPM SU
tpmSock.send('\x00\x00')
# Receive 4 bytes of 0's
reply=tpmSock.recv(18)
```
59 changes: 59 additions & 0 deletions test_talking_with_simulator/talk_with_simulator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# encoding:utf-8
import sys
import socket
from socket import socket, AF_INET, SOCK_STREAM


def main():
sys.stdout.write('start\n')
talk_with_simulator()
sys.stdout.write('end\n')


def talk_with_simulator():
"""Simulator 通讯测试
"""

platformSock = socket(AF_INET, SOCK_STREAM)
platformSock.connect(('127.0.0.1', 2322))

platformSock.send('\0\0\0\1')

tpmSock = socket(AF_INET, SOCK_STREAM)
tpmSock.connect(('127.0.0.1', 2321))

# Send TPM_SEND_COMMAND
tpmSock.send('\x00\x00\x00\x08')
# Send locality
tpmSock.send('\x03')
# Send # of bytes
tpmSock.send('\x00\x00\x00\x0c')
# Send tag
tpmSock.send('\x80\x01')
# Send command size
tpmSock.send('\x00\x00\x00\x0c')
# Send command code: TPMStartup
tpmSock.send('\x00\x00\x01\x44')
# Send TPM SU
tpmSock.send('\x00\x00')

# Receive 4 bytes of 0's
response = tpmSock.recv(18)
display_data(response, 4)
return


def display_data(data, length):
"""检查 TPM Simulator 返回的数据
:param response: Socket 套接字返回的数据
"""
for i in range(length):
ch = data[i]
print("data[%d]=0x%02x" % (i, ord(ch)))
pass



if '__main__' == __name__:
main()

0 comments on commit 6caeb95

Please sign in to comment.