tpm2.py (597473720f4dc69749542bfcfed4a927a43d935e) tpm2.py (8f84bddcfac9117564721ead494db7a604fdf861)
1# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2
3import hashlib
4import os
5import socket
6import struct
7import sys
8import unittest
1# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2
3import hashlib
4import os
5import socket
6import struct
7import sys
8import unittest
9from fcntl import ioctl
9import fcntl
10import select
10
11
11
12TPM2_ST_NO_SESSIONS = 0x8001
13TPM2_ST_SESSIONS = 0x8002
14
15TPM2_CC_FIRST = 0x01FF
16
17TPM2_CC_CREATE_PRIMARY = 0x0131
18TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET = 0x0139
19TPM2_CC_CREATE = 0x0153

--- 327 unchanged lines hidden (view full) ---

347 d = [' '.join(x) for x in d]
348 d = os.linesep.join(d)
349
350 return d
351
352class Client:
353 FLAG_DEBUG = 0x01
354 FLAG_SPACE = 0x02
12TPM2_ST_NO_SESSIONS = 0x8001
13TPM2_ST_SESSIONS = 0x8002
14
15TPM2_CC_FIRST = 0x01FF
16
17TPM2_CC_CREATE_PRIMARY = 0x0131
18TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET = 0x0139
19TPM2_CC_CREATE = 0x0153

--- 327 unchanged lines hidden (view full) ---

347 d = [' '.join(x) for x in d]
348 d = os.linesep.join(d)
349
350 return d
351
352class Client:
353 FLAG_DEBUG = 0x01
354 FLAG_SPACE = 0x02
355 FLAG_NONBLOCK = 0x04
355 TPM_IOC_NEW_SPACE = 0xa200
356
357 def __init__(self, flags = 0):
358 self.flags = flags
359
360 if (self.flags & Client.FLAG_SPACE) == 0:
361 self.tpm = open('/dev/tpm0', 'r+b', buffering=0)
362 else:
363 self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
364
356 TPM_IOC_NEW_SPACE = 0xa200
357
358 def __init__(self, flags = 0):
359 self.flags = flags
360
361 if (self.flags & Client.FLAG_SPACE) == 0:
362 self.tpm = open('/dev/tpm0', 'r+b', buffering=0)
363 else:
364 self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
365
366 if (self.flags & Client.FLAG_NONBLOCK):
367 flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL)
368 flags |= os.O_NONBLOCK
369 fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
370 self.tpm_poll = select.poll()
371
365 def close(self):
366 self.tpm.close()
367
368 def send_cmd(self, cmd):
369 self.tpm.write(cmd)
372 def close(self):
373 self.tpm.close()
374
375 def send_cmd(self, cmd):
376 self.tpm.write(cmd)
377
378 if (self.flags & Client.FLAG_NONBLOCK):
379 self.tpm_poll.register(self.tpm, select.POLLIN)
380 self.tpm_poll.poll(10000)
381
370 rsp = self.tpm.read()
371
382 rsp = self.tpm.read()
383
384 if (self.flags & Client.FLAG_NONBLOCK):
385 self.tpm_poll.unregister(self.tpm)
386
372 if (self.flags & Client.FLAG_DEBUG) != 0:
373 sys.stderr.write('cmd' + os.linesep)
374 sys.stderr.write(hex_dump(cmd) + os.linesep)
375 sys.stderr.write('rsp' + os.linesep)
376 sys.stderr.write(hex_dump(rsp) + os.linesep)
377
378 rc = struct.unpack('>I', rsp[6:10])[0]
379 if rc != 0:

--- 318 unchanged lines hidden ---
387 if (self.flags & Client.FLAG_DEBUG) != 0:
388 sys.stderr.write('cmd' + os.linesep)
389 sys.stderr.write(hex_dump(cmd) + os.linesep)
390 sys.stderr.write('rsp' + os.linesep)
391 sys.stderr.write(hex_dump(rsp) + os.linesep)
392
393 rc = struct.unpack('>I', rsp[6:10])[0]
394 if rc != 0:

--- 318 unchanged lines hidden ---