Skip to content

Instantly share code, notes, and snippets.

@latsku
Last active January 22, 2018 20:24
Show Gist options
  • Save latsku/fa4c7463874ceedde747 to your computer and use it in GitHub Desktop.
Save latsku/fa4c7463874ceedde747 to your computer and use it in GitHub Desktop.
TPM1.2 hashing with just the device node
#!/usr/bin/env python
import binascii
import struct
import logging
logger = logging.getLogger()
#logger.setLevel(logging.DEBUG)
TPM_TAG_RQU_COMMAND = 0x00C1
TPM_ORD_SHA1Start = 0x000000A0
TPM_ORD_SHA1Complete = 0x000000A2
def main():
str = "The quick brown fox jumps over the lazy dog"
print "SHA-1 hash of the string: " + str
try:
f = open('/dev/tpm0', 'r+b')
except IOError, e:
logging.error('Error opening the device %s: \n%r' %
('/dev/tpm0', e))
raise SystemExit(1)
## SHA1Start
fmt = '>HII'
cmd = struct.pack(fmt,
TPM_TAG_RQU_COMMAND,
struct.calcsize(fmt),
TPM_ORD_SHA1Start
)
logging.debug('Cmd =' + binascii.hexlify(cmd[0:]))
f.write(cmd)
rsp = f.read()
logging.debug('Res =' + binascii.hexlify(rsp[0:]))
## SHA1Complete
fmt = '>HIII43s'
cmd = struct.pack(fmt,
TPM_TAG_RQU_COMMAND,
struct.calcsize(fmt),
TPM_ORD_SHA1Complete,
len(str),
str
)
logging.debug('Cmd =' + binascii.hexlify(cmd[0:]))
f.write(cmd)
rsp = f.read()
logging.debug('Res =' + binascii.hexlify(rsp[0:]))
print('Hash=' + binascii.hexlify(rsp[10:]))
f.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment