summaryrefslogtreecommitdiff
path: root/test/tpm_test/hash_test.py
diff options
context:
space:
mode:
authorVadim Sukhomlinov <sukhomlinov@google.com>2020-06-09 17:27:38 -0700
committerCommit Bot <commit-bot@chromium.org>2020-06-11 21:00:01 +0000
commit043326f2bb542cc3e9fa74364364f933141b294d (patch)
tree704049de54ad03d7d3ac2db428d1cbb36cd514e9 /test/tpm_test/hash_test.py
parente74d8b264cab776631991e16a6a447da0ce73561 (diff)
downloadchrome-ec-043326f2bb542cc3e9fa74364364f933141b294d.tar.gz
test/tpm_test: update for Python3
Due to Python3 switch tpm_test.py stop working. Updates to make it work with Python3. cros lint complains it can't import Crypto and rsa BUG=None TEST=tpmtest.py tpmtest.py -t To test exception handling change line 167 in crypto_test.py from if real_out_text != out_text: to if real_out_text == out_text: and run tpmtest.py again. Signed-off-by: Vadim Sukhomlinov <sukhomlinov@google.com> Change-Id: I927b25ab3288274993949c53564bed73faa346e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2231974 Reviewed-by: Vadim Sukhomlinov <sukhomlinov@chromium.org> Reviewed-by: Vadim Bendebury <vbendeb@chromium.org> Tested-by: Vadim Sukhomlinov <sukhomlinov@chromium.org> Commit-Queue: Vadim Sukhomlinov <sukhomlinov@chromium.org> Auto-Submit: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Diffstat (limited to 'test/tpm_test/hash_test.py')
-rw-r--r--test/tpm_test/hash_test.py21
1 files changed, 9 insertions, 12 deletions
diff --git a/test/tpm_test/hash_test.py b/test/tpm_test/hash_test.py
index a518be043c..9a5184e261 100644
--- a/test/tpm_test/hash_test.py
+++ b/test/tpm_test/hash_test.py
@@ -10,7 +10,6 @@ from __future__ import print_function
import hashlib
import hmac
import struct
-
import subcmd
import utils
@@ -30,8 +29,8 @@ ALG_SHA384 = 2
ALG_SHA512 = 3
# A standard empty response to HASH extended commands.
-EMPTY_RESPONSE = ''.join('%c' % x for x in (0x80, 0x01, 0x00, 0x00, 0x00, 0x0c,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01))
+EMPTY_RESPONSE = bytes([0x80, 0x01, 0x00, 0x00, 0x00, 0x0c,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01])
TEST_INPUTS = (
# Hash cmd alg handle hmac_key text
(CMD_HMAC_SW, ALG_SHA256, 0, 'hmac_key1', 'some text, this time for sw hmac'),
@@ -118,21 +117,19 @@ def hash_test(tpm):
for test in TEST_INPUTS:
hash_cmd, hash_alg, handle, hmac_key, text = test
+ text = bytes(text, 'ascii')
+ hmac_key = bytes(hmac_key, 'ascii')
mode_name = cmd_map[hash_cmd]
alg_name, hash_func = alg_map[hash_alg]
test_name = '%s:%s:%d' % (mode_name, alg_name, handle)
- cmd = '%c' % hash_cmd
- cmd += '%c' % hash_alg
- cmd += '%c' % handle # Ignored for single shots
-
- cmd += struct.pack('>H', len(text))
- cmd += text
+ cmd = struct.pack('>BBBH', hash_cmd, hash_alg, handle,
+ len(text)) + text
# for HMAC add key
if hash_cmd in (CMD_HMAC_SW, CMD_HMAC_HW):
- cmd += struct.pack('>H', len(hmac_key))
- cmd += hmac_key
+ cmd += len(hmac_key).to_bytes(2, 'big') + hmac_key
+
wrapped_response = tpm.command(tpm.wrap_ext_command(subcmd.HASH, cmd))
if hash_cmd in (CMD_HASH_START, CMD_HASH_CONT):
if hash_cmd == CMD_HASH_START:
@@ -149,7 +146,7 @@ def hash_test(tpm):
elif hash_cmd == CMD_HASH:
hash_context = hash_func()
elif hash_cmd in (CMD_HMAC_SW, CMD_HMAC_HW):
- hash_context = hmac.new(bytes(hmac_key), digestmod=hash_func)
+ hash_context = hmac.new(hmac_key, digestmod=hash_func)
else:
raise subcmd.TpmTestError('Unknown command %d' % hash_cmd)
hash_context.update(text)