aboutsummaryrefslogtreecommitdiff
path: root/unit_tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests.py')
-rw-r--r--unit_tests.py44
1 files changed, 23 insertions, 21 deletions
diff --git a/unit_tests.py b/unit_tests.py
index e4c4a97..186daa9 100644
--- a/unit_tests.py
+++ b/unit_tests.py
@@ -6,6 +6,8 @@ PKCS #11 unit tests, using cryptech.py11 and the Python unit_test framework.
import unittest
import datetime
+import binascii
+import struct
import sys
from cryptech.py11 import *
@@ -187,7 +189,7 @@ class TestDevice(TestCase):
with self.assertRaises(CKR_OPERATION_NOT_INITIALIZED):
for handle in p11.C_FindObjects(session):
- self.assertIsInstance(handle, (int, long))
+ self.assertIsInstance(handle, (int, int))
with self.assertRaises(CKR_OPERATION_NOT_INITIALIZED):
p11.C_FindObjectsFinal(session)
@@ -198,7 +200,7 @@ class TestDevice(TestCase):
p11.C_FindObjectsInit(session, CKA_CLASS = CKO_PRIVATE_KEY)
for handle in p11.C_FindObjects(session):
- self.assertIsInstance(handle, (int, long))
+ self.assertIsInstance(handle, (int, int))
p11.C_FindObjectsFinal(session)
@@ -208,9 +210,9 @@ class TestKeys(TestCase):
Tests involving keys.
"""
- oid_p256 = "".join(chr(i) for i in (0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07))
- oid_p384 = "".join(chr(i) for i in (0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22))
- oid_p521 = "".join(chr(i) for i in (0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23))
+ oid_p256 = b"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07"
+ oid_p384 = b"\x06\x05\x2b\x81\x04\x00\x22"
+ oid_p521 = b"\x06\x05\x2b\x81\x04\x00\x23"
@classmethod
def setUpClass(cls):
@@ -337,22 +339,22 @@ class TestKeys(TestCase):
@staticmethod
def _build_ecpoint(x, y):
bytes_per_coordinate = (max(x.bit_length(), y.bit_length()) + 15) / 16
- value = chr(0x04) + ("%0*x%0*x" % (bytes_per_coordinate, x, bytes_per_coordinate, y)).decode("hex")
+ value = b"\x04" + binascii.unhexlify("{0:0{2}x}{1:0{2}x}".format(x, y, bytes_per_coordinate))
if len(value) < 128:
- length = chr(len(value))
+ length = struct.pack("U", len(value))
else:
n = len(value).bit_length()
- length = chr((n + 7) / 8) + ("%0*x" % ((n + 15) / 16, len(value))).decode("hex")
- tag = chr(0x04)
+ length = struct.pack("U", (n + 7) / 8) + binascii.unhexlify("{0:0{1}x}".format(len(value), (n + 15) / 16)))
+ tag = b"\x04"
return tag + length + value
def test_canned_ecdsa_p256_verify(self):
"EC-P-256 verification test from Suite B Implementer's Guide to FIPS 186-3"
Q = self._build_ecpoint(0x8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8,
0xd8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9)
- H = "7c3e883ddc8bd688f96eac5e9324222c8f30f9d6bb59e9c5f020bd39ba2b8377".decode("hex")
- r = "7214bc9647160bbd39ff2f80533f5dc6ddd70ddf86bb815661e805d5d4e6f27c".decode("hex")
- s = "7d1ff961980f961bdaa3233b6209f4013317d3e3f9e1493592dbeaa1af2bc367".decode("hex")
+ H = binascii.unhexlify("7c3e883ddc8bd688f96eac5e9324222c8f30f9d6bb59e9c5f020bd39ba2b8377")
+ r = binascii.unhexlify("7214bc9647160bbd39ff2f80533f5dc6ddd70ddf86bb815661e805d5d4e6f27c")
+ s = binascii.unhexlify("7d1ff961980f961bdaa3233b6209f4013317d3e3f9e1493592dbeaa1af2bc367")
handle = p11.C_CreateObject(
session = self.session,
CKA_CLASS = CKO_PUBLIC_KEY,
@@ -370,9 +372,9 @@ class TestKeys(TestCase):
"EC-P-384 verification test from Suite B Implementer's Guide to FIPS 186-3"
Q = self._build_ecpoint(0x1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0,
0xeb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9)
- H = "b9210c9d7e20897ab86597266a9d5077e8db1b06f7220ed6ee75bd8b45db37891f8ba5550304004159f4453dc5b3f5a1".decode("hex")
- r = "a0c27ec893092dea1e1bd2ccfed3cf945c8134ed0c9f81311a0f4a05942db8dbed8dd59f267471d5462aa14fe72de856".decode("hex")
- s = "20ab3f45b74f10b6e11f96a2c8eb694d206b9dda86d3c7e331c26b22c987b7537726577667adadf168ebbe803794a402".decode("hex")
+ H = binascii.unhexlify("b9210c9d7e20897ab86597266a9d5077e8db1b06f7220ed6ee75bd8b45db37891f8ba5550304004159f4453dc5b3f5a1")
+ r = binascii.unhexlify("a0c27ec893092dea1e1bd2ccfed3cf945c8134ed0c9f81311a0f4a05942db8dbed8dd59f267471d5462aa14fe72de856")
+ s = binascii.unhexlify("20ab3f45b74f10b6e11f96a2c8eb694d206b9dda86d3c7e331c26b22c987b7537726577667adadf168ebbe803794a402")
handle = p11.C_CreateObject(
session = self.session,
CKA_CLASS = CKO_PUBLIC_KEY,
@@ -422,13 +424,13 @@ class TestKeys(TestCase):
def assertRawRSASignatureMatches(self, handle, plain, sig):
pubkey = self._extract_rsa_public_key(handle)
result = pubkey.encrypt(sig, 0)[0]
- prefix = "\x00\x01" if False else "\x01" # XXX
- expect = prefix + "\xff" * (len(result) - len(plain) - len(prefix) - 1) + "\x00" + plain
+ prefix = b"\x00\x01" if False else b"\x01" # XXX
+ expect = prefix + b"\xff" * (len(result) - len(plain) - len(prefix) - 1) + b"\x00" + plain
self.assertEqual(result, expect)
def test_gen_sign_verify_tralala_rsa_1024(self):
"Generate/sign/verify with RSA-1024 (no hashing, message to be signed not a hash at all)"
- tralala = "tralala-en-hopsasa"
+ tralala = b"tralala-en-hopsasa"
public_key, private_key = p11.C_GenerateKeyPair(
self.session, CKM_RSA_PKCS_KEY_PAIR_GEN, CKA_MODULUS_BITS = 1024,
CKA_ID = "RSA-1024", CKA_SIGN = True, CKA_VERIFY = True, CKA_TOKEN = True)
@@ -444,7 +446,7 @@ class TestKeys(TestCase):
"Generate/sign/verify with RSA-3416 (no hashing, message to be signed not a hash at all)"
if not args.all_tests:
self.skipTest("Key length not a multiple of 32, so expected to fail (very slowly)")
- tralala = "tralala-en-hopsasa"
+ tralala = b"tralala-en-hopsasa"
public_key, private_key = p11.C_GenerateKeyPair(
self.session, CKM_RSA_PKCS_KEY_PAIR_GEN, CKA_MODULUS_BITS = 3416,
CKA_ID = "RSA-3416", CKA_SIGN = True, CKA_VERIFY = True, CKA_TOKEN = True)
@@ -562,7 +564,7 @@ class TestKeys(TestCase):
0F 1F 86 AF 45 25 4D 8F E1 1F C9 EA B3 83 4A 41
17 C1 42 B7 43 AD 51 5E F5 A2 F8 E3 25
'''
- tbs = "".join(chr(int(i, 16)) for i in tbs.split())
+ tbs = binascii.unhexlify("".join(tbs.split()))
p11.C_SignInit(self.session, CKM_SHA256_RSA_PKCS, private_key)
p11.C_SignUpdate(self.session, tbs)
sig = p11.C_SignFinal(self.session)
@@ -581,7 +583,7 @@ class TestKeys(TestCase):
def _find_objects(self, chunk_size = 10, template = None, **kwargs):
p11.C_FindObjectsInit(self.session, template, **kwargs)
for handle in p11.C_FindObjects(self.session, chunk_size):
- self.assertIsInstance(handle, (int, long))
+ self.assertIsInstance(handle, (int, int))
p11.C_FindObjectsFinal(self.session)
@unittest.skipUnless(pycrypto_loaded, "requires PyCrypto")