aboutsummaryrefslogtreecommitdiff
path: root/unit-tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'unit-tests.py')
-rw-r--r--unit-tests.py153
1 files changed, 77 insertions, 76 deletions
diff --git a/unit-tests.py b/unit-tests.py
index 6419b27..2bb80a8 100644
--- a/unit-tests.py
+++ b/unit-tests.py
@@ -39,6 +39,7 @@ LibHAL unit tests, using libhal.py and the Python unit_test framework.
import unittest
import datetime
+import binascii
import logging
import sys
@@ -221,7 +222,7 @@ class TestDigest(TestCase):
"""
def v(*bytes):
- return "".join(chr(b) for b in bytes)
+ return b"".join(pack("B", b) for b in bytes)
# NIST sample messages.
@@ -1210,7 +1211,7 @@ class TestPKeyMatch(TestCaseLoggedIn):
token = HAL_KEY_FLAG_TOKEN,
public = HAL_KEY_FLAG_PUBLIC,
exportable = HAL_KEY_FLAG_EXPORTABLE)
- return ", ".join(sorted(k for k, v in names.iteritems() if (flags & v) != 0))
+ return ", ".join(sorted(k for k, v in names.items() if (flags & v) != 0))
@staticmethod
def cleanup_key(uuid):
@@ -1222,7 +1223,7 @@ class TestPKeyMatch(TestCaseLoggedIn):
def load_keys(self, flags):
uuids = set()
- for obj in PreloadedKey.db.itervalues():
+ for obj in PreloadedKey.db.values():
with hsm.pkey_load(obj.der, flags) as k:
self.addCleanup(self.cleanup_key, k.uuid)
uuids.add(k.uuid)
@@ -1241,7 +1242,7 @@ class TestPKeyMatch(TestCaseLoggedIn):
def ks_match(self, mask, flags):
tags = []
uuids = set()
- for i in xrange(2):
+ for i in range(2):
uuids |= self.load_keys(flags if mask else HAL_KEY_FLAG_TOKEN * i)
tags.extend(PreloadedKey.db)
self.assertEqual(len(tags), len(uuids))
@@ -1251,14 +1252,14 @@ class TestPKeyMatch(TestCaseLoggedIn):
flags = flags,
uuids = uuids)))
- for keytype in set(HALKeyType.index.itervalues()) - {HAL_KEY_TYPE_NONE}:
+ for keytype in set(HALKeyType.index.values()) - {HAL_KEY_TYPE_NONE}:
n = 0
for n, k in self.match(mask = mask, flags = flags, uuids = uuids, type = keytype):
self.assertEqual(k.key_type, keytype)
self.assertEqual(k.get_attributes({0}).pop(0), str(keytype))
self.assertEqual(n, sum(1 for t1, t2 in tags if t1 == keytype))
- for curve in set(HALCurve.index.itervalues()) - {HAL_CURVE_NONE}:
+ for curve in set(HALCurve.index.values()) - {HAL_CURVE_NONE}:
n = 0
for n, k in self.match(mask = mask, flags = flags, uuids = uuids, curve = curve):
self.assertEqual(k.key_curve, curve)
@@ -1309,13 +1310,13 @@ class TestPKeyAttribute(TestCaseLoggedIn):
def load_and_fill(self, flags, n_keys = 1, n_attrs = 2, n_fill = 0):
pinwheel = Pinwheel()
- for i in xrange(n_keys):
- for obj in PreloadedKey.db.itervalues():
+ for i in range(n_keys):
+ for obj in PreloadedKey.db.values():
with hsm.pkey_load(obj.der, flags) as k:
pinwheel()
self.addCleanup(self.cleanup_key, k.uuid)
k.set_attributes(dict((j, "Attribute {}{}".format(j, "*" * n_fill))
- for j in xrange(n_attrs)))
+ for j in range(n_attrs)))
pinwheel()
# These sizes work with a 8192-byte keystore block; if you tweak
@@ -1360,55 +1361,55 @@ class TestPKeyAttributeP11(TestCaseLoggedIn):
def test_set_many_attributes(self):
self.k.set_attributes({
- 0x001 : "\x01",
- 0x108 : "\x01",
- 0x105 : "\x00",
- 0x002 : "\x01",
- 0x107 : "\x00",
- 0x102 : "\x45\x43\x2d\x50\x32\x35\x36",
- 0x003 : "\x45\x43\x2d\x50\x32\x35\x36",
- 0x162 : "\x00",
- 0x103 : "\x01",
- 0x000 : "\x03\x00\x00\x00",
- 0x100 : "\x03\x00\x00\x00",
- 0x101 : "",
- 0x109 : "\x00",
- 0x10c : "\x00",
- 0x110 : "",
- 0x111 : "",
- 0x163 : "\x00",
- 0x166 : "\xff\xff\xff\xff",
- 0x170 : "\x01",
- 0x210 : "\x00",
- 0x163 : "\x01",
- 0x166 : "\x40\x10\x00\x00",
- 0x180 : "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07" })
+ 0x001 : b"\x01",
+ 0x108 : b"\x01",
+ 0x105 : b"\x00",
+ 0x002 : b"\x01",
+ 0x107 : b"\x00",
+ 0x102 : b"\x45\x43\x2d\x50\x32\x35\x36",
+ 0x003 : b"\x45\x43\x2d\x50\x32\x35\x36",
+ 0x162 : b"\x00",
+ 0x103 : b"\x01",
+ 0x000 : b"\x03\x00\x00\x00",
+ 0x100 : b"\x03\x00\x00\x00",
+ 0x101 : b"",
+ 0x109 : b"\x00",
+ 0x10c : b"\x00",
+ 0x110 : b"",
+ 0x111 : b"",
+ 0x163 : b"\x00",
+ 0x166 : b"\xff\xff\xff\xff",
+ 0x170 : b"\x01",
+ 0x210 : b"\x00",
+ 0x163 : b"\x01",
+ 0x166 : b"\x40\x10\x00\x00",
+ 0x180 : b"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07" })
def test_set_many_attributes_with_deletions(self):
self.k.set_attributes({
- 0x001 : "\x01",
- 0x108 : "\x01",
- 0x105 : "\x00",
- 0x002 : "\x01",
- 0x107 : "\x00",
- 0x102 : "\x45\x43\x2d\x50\x32\x35\x36",
- 0x003 : "\x45\x43\x2d\x50\x32\x35\x36",
- 0x162 : "\x00",
- 0x103 : "\x01",
- 0x000 : "\x03\x00\x00\x00",
- 0x100 : "\x03\x00\x00\x00",
+ 0x001 : b"\x01",
+ 0x108 : b"\x01",
+ 0x105 : b"\x00",
+ 0x002 : b"\x01",
+ 0x107 : b"\x00",
+ 0x102 : b"\x45\x43\x2d\x50\x32\x35\x36",
+ 0x003 : b"\x45\x43\x2d\x50\x32\x35\x36",
+ 0x162 : b"\x00",
+ 0x103 : b"\x01",
+ 0x000 : b"\x03\x00\x00\x00",
+ 0x100 : b"\x03\x00\x00\x00",
0x101 : None,
- 0x109 : "\x00",
- 0x10c : "\x00",
+ 0x109 : b"\x00",
+ 0x10c : b"\x00",
0x110 : None,
0x111 : None,
- 0x163 : "\x00",
- 0x166 : "\xff\xff\xff\xff",
- 0x170 : "\x01",
- 0x210 : "\x00",
- 0x163 : "\x01",
- 0x166 : "\x40\x10\x00\x00",
- 0x180 : "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07" })
+ 0x163 : b"\x00",
+ 0x166 : b"\xff\xff\xff\xff",
+ 0x170 : b"\x01",
+ 0x210 : b"\x00",
+ 0x163 : b"\x01",
+ 0x166 : b"\x40\x10\x00\x00",
+ 0x180 : b"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07" })
@unittest.skipUnless(ecdsa_loaded, "Requires Python ECDSA package")
@@ -1425,7 +1426,7 @@ class TestPKeyAttributeWriteSpeedToken(TestCaseLoggedIn):
def set_attributes(self, n_attrs):
self.k.set_attributes(dict((i, "Attribute {}".format(i))
- for i in xrange(n_attrs)))
+ for i in range(n_attrs)))
def test_set_1_attribute(self):
self.set_attributes(1)
@@ -1450,7 +1451,7 @@ class TestPKeyAttributeWriteSpeedVolatile(TestCaseLoggedIn):
def set_attributes(self, n_attrs):
self.k.set_attributes(dict((i, "Attribute {}".format(i))
- for i in xrange(n_attrs)))
+ for i in range(n_attrs)))
def test_set_1_attribute(self):
self.set_attributes(1)
@@ -1472,16 +1473,16 @@ class TestPKeyAttributeReadSpeedToken(TestCaseLoggedIn):
self.k = hsm.pkey_load(der, HAL_KEY_FLAG_TOKEN)
self.addCleanup(self.k.delete)
self.k.set_attributes(dict((i, "Attribute {}".format(i))
- for i in xrange(12)))
+ for i in range(12)))
super(TestPKeyAttributeReadSpeedToken, self).setUp()
def verify_attributes(self, n_attrs, attributes):
expected = dict((i, "Attribute {}".format(i))
- for i in xrange(n_attrs))
+ for i in range(n_attrs))
self.assertEqual(attributes, expected)
def get_attributes(self, n_attrs):
- attributes = self.k.get_attributes(range(n_attrs))
+ attributes = self.k.get_attributes(list(range(n_attrs)))
self.verify_attributes(n_attrs, attributes)
def test_get_1_attribute(self):
@@ -1504,16 +1505,16 @@ class TestPKeyAttributeReadSpeedVolatile(TestCaseLoggedIn):
self.k = hsm.pkey_load(der, 0)
self.addCleanup(self.k.delete)
self.k.set_attributes(dict((i, "Attribute {}".format(i))
- for i in xrange(12)))
+ for i in range(12)))
super(TestPKeyAttributeReadSpeedVolatile, self).setUp()
def verify_attributes(self, n_attrs, attributes):
expected = dict((i, "Attribute {}".format(i))
- for i in xrange(n_attrs))
+ for i in range(n_attrs))
self.assertEqual(attributes, expected)
def get_attributes(self, n_attrs):
- attributes = self.k.get_attributes(range(n_attrs))
+ attributes = self.k.get_attributes(list(range(n_attrs)))
self.verify_attributes(n_attrs, attributes)
def test_get_1_attribute(self):
@@ -1537,7 +1538,7 @@ class TestPkeyECDSAVerificationNIST(TestCaseLoggedIn):
py_curve, py_hash).to_der()
k = hsm.pkey_load(Q, HAL_KEY_FLAG_USAGE_DIGITALSIGNATURE)
self.addCleanup(k.delete)
- k.verify(signature = (r + s).decode("hex"), data = H.decode("hex"))
+ k.verify(signature = binascii.unhexlify(r + s), data = binascii.unhexlify(H))
def test_suite_b_p256_verify(self):
self.verify(
@@ -1563,8 +1564,8 @@ class TestPkeyECDSAVerificationNIST(TestCaseLoggedIn):
@unittest.skipUnless(pycrypto_loaded, "Requires Python Crypto package")
class TestPKeyBackup(TestCaseLoggedIn):
- oid_rsaEncryption = "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
- oid_aesKeyWrap = "\x60\x86\x48\x01\x65\x03\x04\x01\x30"
+ oid_rsaEncryption = b"\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"
+ oid_aesKeyWrap = b"\x60\x86\x48\x01\x65\x03\x04\x01\x30"
@staticmethod
def parse_EncryptedPrivateKeyInfo(der, oid):
@@ -1593,7 +1594,7 @@ class TestPKeyBackup(TestCaseLoggedIn):
def encode_EncryptedPrivateKeyInfo(der, oid):
from Crypto.Util.asn1 import DerSequence, DerOctetString
return DerSequence([
- DerSequence([chr(0x06) + chr(len(oid)) + oid]).encode(),
+ DerSequence([pack("BB", 0x06, len(oid)) + oid]).encode(),
DerOctetString(der).encode()
]).encode()
@@ -1676,7 +1677,7 @@ class AESKeyWrapWithPadding(object):
@staticmethod
def _start_stop(start, stop): # Syntactic sugar
step = -1 if start > stop else 1
- return xrange(start, stop + step, step)
+ return range(start, stop + step, step)
@staticmethod
def _xor(R0, t):
@@ -1686,10 +1687,10 @@ class AESKeyWrapWithPadding(object):
"RFC 5649 section 4.1."
m = len(Q) # Plaintext length
if m % 8 != 0: # Pad Q if needed
- Q += "\x00" * (8 - (m % 8))
+ Q += b"\x00" * (8 - (m % 8))
R = [pack(">LL", 0xa65959a6, m)] # Magic MSB(32,A), build LSB(32,A)
R.extend(Q[i : i + 8] # Append Q
- for i in xrange(0, len(Q), 8))
+ for i in range(0, len(Q), 8))
n = len(R) - 1
if n == 1:
R[0], R[1] = self._encrypt(R[0], R[1])
@@ -1700,7 +1701,7 @@ class AESKeyWrapWithPadding(object):
R[0], R[i] = self._encrypt(R[0], R[i])
R[0] = self._xor(R[0], n * j + i)
assert len(R) == (n + 1) and all(len(r) == 8 for r in R)
- return "".join(R)
+ return b"".join(R)
def unwrap(self, C):
"RFC 5649 section 4.2."
@@ -1708,7 +1709,7 @@ class AESKeyWrapWithPadding(object):
raise self.UnwrapError("Ciphertext length {} is not an integral number of blocks"
.format(len(C)))
n = (len(C) / 8) - 1
- R = [C[i : i + 8] for i in xrange(0, len(C), 8)]
+ R = [C[i : i + 8] for i in range(0, len(C), 8)]
if n == 1:
R[0], R[1] = self._decrypt(R[0], R[1])
else:
@@ -1723,10 +1724,10 @@ class AESKeyWrapWithPadding(object):
.format(magic))
if m <= 8 * (n - 1) or m > 8 * n:
raise self.UnwrapError("Length encoded in AIV out of range: m {}, n {}".format(m, n))
- R = "".join(R[1:])
+ R = b"".join(R[1:])
assert len(R) == 8 * n
- if any(r != "\x00" for r in R[m:]):
- raise self.UnwrapError("Nonzero trailing bytes {}".format(R[m:].encode("hex")))
+ if any(r != b"\x00" for r in R[m:]):
+ raise self.UnwrapError("Nonzero trailing bytes {}".format(binascii.hexlify(R[m:])))
return R[:m]
@@ -1802,7 +1803,7 @@ class PreloadedECKey(PreloadedKey):
if ecdsa_loaded:
der = ECDSA_DER.unpem(pem)
car, cdr = ECDSA_DER.remove_sequence(der)
- cls._check(cdr == "")
+ cls._check(cdr == b"")
version, cdr = ECDSA_DER.remove_integer(car)
cls._check(version == 0)
algid, pkinfo = ECDSA_DER.remove_sequence(cdr)
@@ -1810,11 +1811,11 @@ class PreloadedECKey(PreloadedKey):
cls._check(oid == oid_ecPublicKey)
oid, cdr = ECDSA_DER.remove_object(cdr)
sk_curve = ECDSA_find_curve(oid)
- cls._check(cdr == "")
+ cls._check(cdr == b"")
car, cdr = ECDSA_DER.remove_octet_string(pkinfo)
- cls._check(cdr == "")
+ cls._check(cdr == b"")
car, cdr = ECDSA_DER.remove_sequence(car)
- cls._check(cdr == "")
+ cls._check(cdr == b"")
version, cdr = ECDSA_DER.remove_integer(car)
cls._check(version == 1)
privkey, cdr = ECDSA_DER.remove_octet_string(cdr)