Skip to content

Instantly share code, notes, and snippets.

@latsku
Created August 14, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latsku/c1ed52e69b63eb3f5169 to your computer and use it in GitHub Desktop.
Save latsku/c1ed52e69b63eb3f5169 to your computer and use it in GitHub Desktop.
TPM2.0 hashing with just the device node
import binascii
import struct
import logging
logger = logging.getLogger()
#logger.setLevel(logging.DEBUG)
TPM2_ST_NO_SESSIONS = 0x8001
TPM2_CC_Hash = 0x0000017D
TPM2_ALG_SHA1 = 0x0004
TPM_RH_NULL = 0x40000007
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)
## Hash
fmt = '>HIIH43sHI'
cmd = struct.pack(fmt,
TPM2_ST_NO_SESSIONS,
struct.calcsize(fmt),
TPM2_CC_Hash,
len(str),
str,
TPM2_ALG_SHA1,
TPM_RH_NULL)
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[-28:-8]))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment