Skip to content

Commit 9750373

Browse files
author
Johannes Holland
committed
py-tcti: add draft tcti for usage with py-tcti
1 parent 8ecf143 commit 9750373

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/tpmstream/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
__version__ = "0.1.10"
2+
3+
# make tcti_init visible for py-tcti
4+
# see https://github.com/tpm2-software/tpm2-tss/pull/2749
5+
from tpmstream.tcti import tcti_init # isort:skip

src/tpmstream/tcti/__init__.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import_error = None
2+
try:
3+
from tpm2_pytss import TCTILdr
4+
except ImportError as error:
5+
import_error = error
6+
7+
from tpmstream.io.binary import Binary
8+
from tpmstream.io.pretty import Pretty
9+
from tpmstream.spec.commands import Command, Response
10+
11+
12+
# To use tpmstream with tcti-py, we define a tcti
13+
# see https://github.com/tpm2-software/tpm2-tss/pull/2749
14+
class TpmstreamTCTI(object):
15+
def __init__(self, args: str | None):
16+
def parse_args(args: str | None):
17+
if args is None:
18+
return {"name": None, "conf": None}
19+
if ":" not in args:
20+
return {"name": args, "conf": None}
21+
22+
name, conf = args.split(":", maxsplit=1)
23+
return {"name": name, "conf": conf}
24+
25+
nameconf = parse_args(args)
26+
print(f"PYTHON: Initializing TCTI Ldr with mod: {nameconf}")
27+
self._tcti = TCTILdr(**nameconf)
28+
self._command_code = None
29+
30+
@property
31+
def magic(self):
32+
return 0x74706D7374726561
33+
34+
def receive(self, timeout: int) -> bytes:
35+
result = self._tcti.receive(timeout=timeout)
36+
37+
events = Binary.marshal(
38+
tpm_type=Response,
39+
buffer=result,
40+
command_code=self._command_code,
41+
abort_on_error=False,
42+
)
43+
for line in Pretty.unmarshal(events):
44+
if isinstance(line, bytes):
45+
print(" " + binascii.hexlify(line).decode(), end="")
46+
else:
47+
print(line)
48+
49+
return result
50+
51+
def transmit(self, buffer: bytes):
52+
events = Binary.marshal(
53+
tpm_type=Command,
54+
buffer=buffer,
55+
abort_on_error=False,
56+
)
57+
for line in Pretty.unmarshal(events):
58+
if isinstance(line, bytes):
59+
print(" " + binascii.hexlify(line).decode(), end="")
60+
else:
61+
print(line)
62+
self._command_code = int.from_bytes(buffer[6:10], byteorder="big")
63+
64+
self._tcti.transmit(buffer)
65+
66+
67+
def tcti_init(args: str) -> TpmstreamTCTI:
68+
if import_error:
69+
raise import_error
70+
return TpmstreamTCTI(args)

0 commit comments

Comments
 (0)