Skip to content

Commit 0b78c9e

Browse files
Pengfei Xushuahkh
authored andcommitted
selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3
Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3. Fixed minor merge conflicts Shuah Khan <[email protected]> Signed-off-by: Pengfei Xu <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 377ff83 commit 0b78c9e

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

tools/testing/selftests/tpm2/test_smoke.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ ksft_skip=4
66

77
[ -e /dev/tpm0 ] || exit $ksft_skip
88

9-
python -m unittest -v tpm2_tests.SmokeTest
10-
python -m unittest -v tpm2_tests.AsyncTest
9+
python3 -m unittest -v tpm2_tests.SmokeTest
10+
python3 -m unittest -v tpm2_tests.AsyncTest

tools/testing/selftests/tpm2/test_space.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ ksft_skip=4
66

77
[ -e /dev/tpmrm0 ] || exit $ksft_skip
88

9-
python -m unittest -v tpm2_tests.SpaceTest
9+
python3 -m unittest -v tpm2_tests.SpaceTest

tools/testing/selftests/tpm2/tpm2.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ def __str__(self):
247247
class AuthCommand(object):
248248
"""TPMS_AUTH_COMMAND"""
249249

250-
def __init__(self, session_handle=TPM2_RS_PW, nonce='', session_attributes=0,
251-
hmac=''):
250+
def __init__(self, session_handle=TPM2_RS_PW, nonce=bytes(),
251+
session_attributes=0, hmac=bytes()):
252252
self.session_handle = session_handle
253253
self.nonce = nonce
254254
self.session_attributes = session_attributes
255255
self.hmac = hmac
256256

257-
def __str__(self):
257+
def __bytes__(self):
258258
fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
259259
return struct.pack(fmt, self.session_handle, len(self.nonce),
260260
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +268,11 @@ def __len__(self):
268268
class SensitiveCreate(object):
269269
"""TPMS_SENSITIVE_CREATE"""
270270

271-
def __init__(self, user_auth='', data=''):
271+
def __init__(self, user_auth=bytes(), data=bytes()):
272272
self.user_auth = user_auth
273273
self.data = data
274274

275-
def __str__(self):
275+
def __bytes__(self):
276276
fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
277277
return struct.pack(fmt, len(self.user_auth), self.user_auth,
278278
len(self.data), self.data)
@@ -296,16 +296,17 @@ def __fmt(self):
296296
return '>HHIH%us%usH%us' % \
297297
(len(self.auth_policy), len(self.parameters), len(self.unique))
298298

299-
def __init__(self, object_type, name_alg, object_attributes, auth_policy='',
300-
parameters='', unique=''):
299+
def __init__(self, object_type, name_alg, object_attributes,
300+
auth_policy=bytes(), parameters=bytes(),
301+
unique=bytes()):
301302
self.object_type = object_type
302303
self.name_alg = name_alg
303304
self.object_attributes = object_attributes
304305
self.auth_policy = auth_policy
305306
self.parameters = parameters
306307
self.unique = unique
307308

308-
def __str__(self):
309+
def __bytes__(self):
309310
return struct.pack(self.__fmt(),
310311
self.object_type,
311312
self.name_alg,
@@ -343,7 +344,7 @@ def get_algorithm(name):
343344

344345
def hex_dump(d):
345346
d = [format(ord(x), '02x') for x in d]
346-
d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
347+
d = [d[i: i + 16] for i in range(0, len(d), 16)]
347348
d = [' '.join(x) for x in d]
348349
d = os.linesep.join(d)
349350

@@ -401,7 +402,7 @@ def read_pcr(self, i, bank_alg = TPM2_ALG_SHA1):
401402
pcrsel_len = max((i >> 3) + 1, 3)
402403
pcrsel = [0] * pcrsel_len
403404
pcrsel[i >> 3] = 1 << (i & 7)
404-
pcrsel = ''.join(map(chr, pcrsel))
405+
pcrsel = ''.join(map(chr, pcrsel)).encode()
405406

406407
fmt = '>HII IHB%us' % (pcrsel_len)
407408
cmd = struct.pack(fmt,
@@ -443,7 +444,7 @@ def extend_pcr(self, i, dig, bank_alg = TPM2_ALG_SHA1):
443444
TPM2_CC_PCR_EXTEND,
444445
i,
445446
len(auth_cmd),
446-
str(auth_cmd),
447+
bytes(auth_cmd),
447448
1, bank_alg, dig)
448449

449450
self.send_cmd(cmd)
@@ -457,7 +458,7 @@ def start_auth_session(self, session_type, name_alg = TPM2_ALG_SHA1):
457458
TPM2_RH_NULL,
458459
TPM2_RH_NULL,
459460
16,
460-
'\0' * 16,
461+
('\0' * 16).encode(),
461462
0,
462463
session_type,
463464
TPM2_ALG_NULL,
@@ -472,7 +473,7 @@ def __calc_pcr_digest(self, pcrs, bank_alg = TPM2_ALG_SHA1,
472473

473474
for i in pcrs:
474475
pcr = self.read_pcr(i, bank_alg)
475-
if pcr == None:
476+
if pcr is None:
476477
return None
477478
x += pcr
478479

@@ -489,15 +490,16 @@ def policy_pcr(self, handle, pcrs, bank_alg = TPM2_ALG_SHA1,
489490
pcrsel = [0] * pcrsel_len
490491
for i in pcrs:
491492
pcrsel[i >> 3] |= 1 << (i & 7)
492-
pcrsel = ''.join(map(chr, pcrsel))
493+
pcrsel = ''.join(map(chr, pcrsel)).encode()
493494

494495
fmt = '>HII IH%usIHB3s' % ds
495496
cmd = struct.pack(fmt,
496497
TPM2_ST_NO_SESSIONS,
497498
struct.calcsize(fmt),
498499
TPM2_CC_POLICY_PCR,
499500
handle,
500-
len(dig), str(dig),
501+
len(dig),
502+
bytes(dig),
501503
1,
502504
bank_alg,
503505
pcrsel_len, pcrsel)
@@ -534,7 +536,7 @@ def flush_context(self, handle):
534536

535537
self.send_cmd(cmd)
536538

537-
def create_root_key(self, auth_value = ''):
539+
def create_root_key(self, auth_value = bytes()):
538540
attributes = \
539541
Public.FIXED_TPM | \
540542
Public.FIXED_PARENT | \
@@ -570,11 +572,11 @@ def create_root_key(self, auth_value = ''):
570572
TPM2_CC_CREATE_PRIMARY,
571573
TPM2_RH_OWNER,
572574
len(auth_cmd),
573-
str(auth_cmd),
575+
bytes(auth_cmd),
574576
len(sensitive),
575-
str(sensitive),
577+
bytes(sensitive),
576578
len(public),
577-
str(public),
579+
bytes(public),
578580
0, 0)
579581

580582
return struct.unpack('>I', self.send_cmd(cmd)[10:14])[0]
@@ -587,7 +589,7 @@ def seal(self, parent_key, data, auth_value, policy_dig,
587589
attributes = 0
588590
if not policy_dig:
589591
attributes |= Public.USER_WITH_AUTH
590-
policy_dig = ''
592+
policy_dig = bytes()
591593

592594
auth_cmd = AuthCommand()
593595
sensitive = SensitiveCreate(user_auth=auth_value, data=data)
@@ -608,11 +610,11 @@ def seal(self, parent_key, data, auth_value, policy_dig,
608610
TPM2_CC_CREATE,
609611
parent_key,
610612
len(auth_cmd),
611-
str(auth_cmd),
613+
bytes(auth_cmd),
612614
len(sensitive),
613-
str(sensitive),
615+
bytes(sensitive),
614616
len(public),
615-
str(public),
617+
bytes(public),
616618
0, 0)
617619

618620
rsp = self.send_cmd(cmd)
@@ -635,7 +637,7 @@ def unseal(self, parent_key, blob, auth_value, policy_handle):
635637
TPM2_CC_LOAD,
636638
parent_key,
637639
len(auth_cmd),
638-
str(auth_cmd),
640+
bytes(auth_cmd),
639641
blob)
640642

641643
data_handle = struct.unpack('>I', self.send_cmd(cmd)[10:14])[0]
@@ -653,7 +655,7 @@ def unseal(self, parent_key, blob, auth_value, policy_handle):
653655
TPM2_CC_UNSEAL,
654656
data_handle,
655657
len(auth_cmd),
656-
str(auth_cmd))
658+
bytes(auth_cmd))
657659

658660
try:
659661
rsp = self.send_cmd(cmd)
@@ -675,7 +677,7 @@ def reset_da_lock(self):
675677
TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET,
676678
TPM2_RH_LOCKOUT,
677679
len(auth_cmd),
678-
str(auth_cmd))
680+
bytes(auth_cmd))
679681

680682
self.send_cmd(cmd)
681683

@@ -693,7 +695,7 @@ def __get_cap_cnt(self, cap, pt, cnt):
693695
more_data, cap, cnt = struct.unpack('>BII', rsp[:9])
694696
rsp = rsp[9:]
695697

696-
for i in xrange(0, cnt):
698+
for i in range(0, cnt):
697699
handle = struct.unpack('>I', rsp[:4])[0]
698700
handles.append(handle)
699701
rsp = rsp[4:]

tools/testing/selftests/tpm2/tpm2_tests.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def tearDown(self):
2020
self.client.close()
2121

2222
def test_seal_with_auth(self):
23-
data = 'X' * 64
24-
auth = 'A' * 15
23+
data = ('X' * 64).encode()
24+
auth = ('A' * 15).encode()
2525

2626
blob = self.client.seal(self.root_key, data, auth, None)
2727
result = self.client.unseal(self.root_key, blob, auth, None)
@@ -30,8 +30,8 @@ def test_seal_with_auth(self):
3030
def test_seal_with_policy(self):
3131
handle = self.client.start_auth_session(tpm2.TPM2_SE_TRIAL)
3232

33-
data = 'X' * 64
34-
auth = 'A' * 15
33+
data = ('X' * 64).encode()
34+
auth = ('A' * 15).encode()
3535
pcrs = [16]
3636

3737
try:
@@ -58,23 +58,24 @@ def test_seal_with_policy(self):
5858
self.assertEqual(data, result)
5959

6060
def test_unseal_with_wrong_auth(self):
61-
data = 'X' * 64
62-
auth = 'A' * 20
61+
data = ('X' * 64).encode()
62+
auth = ('A' * 20).encode()
6363
rc = 0
6464

6565
blob = self.client.seal(self.root_key, data, auth, None)
6666
try:
67-
result = self.client.unseal(self.root_key, blob, auth[:-1] + 'B', None)
68-
except ProtocolError, e:
67+
result = self.client.unseal(self.root_key, blob,
68+
auth[:-1] + 'B'.encode(), None)
69+
except ProtocolError as e:
6970
rc = e.rc
7071

7172
self.assertEqual(rc, tpm2.TPM2_RC_AUTH_FAIL)
7273

7374
def test_unseal_with_wrong_policy(self):
7475
handle = self.client.start_auth_session(tpm2.TPM2_SE_TRIAL)
7576

76-
data = 'X' * 64
77-
auth = 'A' * 17
77+
data = ('X' * 64).encode()
78+
auth = ('A' * 17).encode()
7879
pcrs = [16]
7980

8081
try:
@@ -91,7 +92,7 @@ def test_unseal_with_wrong_policy(self):
9192
# This should succeed.
9293

9394
ds = tpm2.get_digest_size(tpm2.TPM2_ALG_SHA1)
94-
self.client.extend_pcr(1, 'X' * ds)
95+
self.client.extend_pcr(1, ('X' * ds).encode())
9596

9697
handle = self.client.start_auth_session(tpm2.TPM2_SE_POLICY)
9798

@@ -108,7 +109,7 @@ def test_unseal_with_wrong_policy(self):
108109

109110
# Then, extend a PCR that is part of the policy and try to unseal.
110111
# This should fail.
111-
self.client.extend_pcr(16, 'X' * ds)
112+
self.client.extend_pcr(16, ('X' * ds).encode())
112113

113114
handle = self.client.start_auth_session(tpm2.TPM2_SE_POLICY)
114115

@@ -119,7 +120,7 @@ def test_unseal_with_wrong_policy(self):
119120
self.client.policy_password(handle)
120121

121122
result = self.client.unseal(self.root_key, blob, auth, handle)
122-
except ProtocolError, e:
123+
except ProtocolError as e:
123124
rc = e.rc
124125
self.client.flush_context(handle)
125126
except:
@@ -130,13 +131,13 @@ def test_unseal_with_wrong_policy(self):
130131

131132
def test_seal_with_too_long_auth(self):
132133
ds = tpm2.get_digest_size(tpm2.TPM2_ALG_SHA1)
133-
data = 'X' * 64
134-
auth = 'A' * (ds + 1)
134+
data = ('X' * 64).encode()
135+
auth = ('A' * (ds + 1)).encode()
135136

136137
rc = 0
137138
try:
138139
blob = self.client.seal(self.root_key, data, auth, None)
139-
except ProtocolError, e:
140+
except ProtocolError as e:
140141
rc = e.rc
141142

142143
self.assertEqual(rc, tpm2.TPM2_RC_SIZE)
@@ -152,7 +153,7 @@ def test_too_short_cmd(self):
152153
0xDEADBEEF)
153154

154155
self.client.send_cmd(cmd)
155-
except IOError, e:
156+
except IOError as e:
156157
rejected = True
157158
except:
158159
pass
@@ -212,7 +213,7 @@ def test_send_two_cmds(self):
212213
self.client.tpm.write(cmd)
213214
rsp = self.client.tpm.read()
214215

215-
except IOError, e:
216+
except IOError as e:
216217
# read the response
217218
rsp = self.client.tpm.read()
218219
rejected = True
@@ -283,7 +284,7 @@ def test_invalid_cc(self):
283284
rc = 0
284285
try:
285286
space1.send_cmd(cmd)
286-
except ProtocolError, e:
287+
except ProtocolError as e:
287288
rc = e.rc
288289

289290
self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |

0 commit comments

Comments
 (0)