diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/parallel-signatures.py | 15 | ||||
-rw-r--r-- | tests/test-ecdsa.py | 53 | ||||
-rw-r--r-- | tests/test-rsa.py | 43 | ||||
-rwxr-xr-x | tests/time-keygen.py | 4 |
4 files changed, 59 insertions, 56 deletions
diff --git a/tests/parallel-signatures.py b/tests/parallel-signatures.py index 980f759..7cb7132 100755 --- a/tests/parallel-signatures.py +++ b/tests/parallel-signatures.py @@ -44,6 +44,7 @@ import uuid import xdrlib import socket import logging +import binascii import datetime import collections @@ -103,7 +104,7 @@ class PKey(cryptech.libhal.Handle): raise Return(r) @coroutine - def verify(self, data = "", signature = None): + def verify(self, data = b"", signature = None): yield self.hsm.pkey_verify(self, data = data, signature = signature) @@ -135,7 +136,7 @@ class HSM(cryptech.libhal.HSM): self._pack_args(packer, args) packer = cryptech.libhal.slip_encode(packer.get_buffer()) if self.debug_io: - logger.debug("send: %s", ":".join("{:02x}".format(ord(c)) for c in packer)) + logger.debug("send: %s", ":".join(binascii.hexlify(c) for c in packer)) yield self.iostream.write(packer) while True: try: @@ -143,11 +144,11 @@ class HSM(cryptech.libhal.HSM): except StreamClosedError: raise HAL_ERROR_RPC_TRANSPORT() if self.debug_io: - logger.debug("recv: %s", ":".join("{:02x}".format(ord(c)) for c in unpacker)) + logger.debug("recv: %s", ":".join(binascii.hexlify(c) for c in unpacker)) unpacker = cryptech.libhal.slip_decode(unpacker) if not unpacker: continue - unpacker = ContextManagedUnpacker("".join(unpacker)) + unpacker = ContextManagedUnpacker(b"".join(unpacker)) if unpacker.unpack_uint() == code: break client = unpacker.unpack_uint() @@ -209,7 +210,7 @@ def client(args, k, p, q, r, m, v, h): t0 = datetime.datetime.now() s = yield p.sign(data = m) t1 = datetime.datetime.now() - logger.debug("Signature %s: %s", n, ":".join("{:02x}".format(ord(b)) for b in s)) + logger.debug("Signature %s: %s", n, ":".join(binascii.hexlify(b) for b in s)) if args.verify and not v.verify(h, s): raise RuntimeError("RSA verification failed") r.add(t0, t1) @@ -236,11 +237,11 @@ def main(): d = k.exportKey(format = "DER", pkcs = 8) h = SHA256(args.text) v = PKCS115_SigScheme(k) - q = range(args.iterations) + q = list(range(args.iterations)) m = pkcs1_hash_and_pad(args.text) r = Result(args, args.key) - hsms = [HSM() for i in xrange(args.clients)] + hsms = [HSM() for i in range(args.clients)] for hsm in hsms: yield hsm.login(HAL_USER_NORMAL, args.pin) diff --git a/tests/test-ecdsa.py b/tests/test-ecdsa.py index f50cf59..cf21019 100644 --- a/tests/test-ecdsa.py +++ b/tests/test-ecdsa.py @@ -38,6 +38,7 @@ p384_u2 = 0xf3b240751d5d8ed394a4b5bf8e2a4c0e1e21aa51f2620a08b8c55a2bc334c96899 p384_v = 0xa0c27ec893092dea1e1bd2ccfed3cf945c8134ed0c9f81311a0f4a05942db8dbed8dd59f267471d5462aa14fe72de856 from textwrap import TextWrapper +from binascii import hexlify, unhexlify from os.path import basename from sys import argv from pyasn1.type.univ import Sequence, Choice, Integer, OctetString, ObjectIdentifier, BitString @@ -54,16 +55,16 @@ def long_to_bytes(number, order): # # This is just plain nasty. # - s = "%x" % number + s = "{:x}".format(number) s = ("0" * (order/8 - len(s))) + s - return s.decode("hex") + return unhexlify(s) -def bytes_to_bits(bytes): +def bytes_to_bits(b): # # This, on the other hand, is not just plain nasty, this is fancy nasty. # This is nasty with raisins in it. # - s = bin(long(bytes.encode("hex"), 16))[2:] + s = bin(int(hexlify(b), 16))[2:] if len(s) % 8: s = ("0" * (8 - len(s) % 8)) + s return tuple(int(i) for i in s) @@ -102,10 +103,10 @@ p384_key = encode_key(p384_d, p384_Qx, p384_Qy, 384, "1.3.132.0.34") ### -print "/*" -print " * ECDSA test data." -print " * File automatically generated by", basename(argv[0]) -print " */" +print("/*") +print(" * ECDSA test data.") +print(" * File automatically generated by", basename(argv[0])) +print(" */") curves = ("p256", "p384") vars = set() @@ -122,29 +123,29 @@ for curve in curves: for var in vars: name = curve + "_" + var value = globals().get(name, None) - if isinstance(value, (int, long)): + if isinstance(value, int): value = long_to_bytes(value, order) if value is not None: - print - print "static const uint8_t %s[] = { /* %d bytes */" % (name, len(value)) - print wrapper.fill(", ".join("0x%02x" % ord(v) for v in value)) - print "};" - -print -print "typedef struct {" -print " hal_curve_name_t curve;" + print() + print("static const uint8_t {}[] = {{ /* {:d} bytes */".format(name, len(value))) + print(wrapper.fill(", ".join("0x" + hexlify(v) for v in value))) + print("};") + +print() +print("typedef struct {") +print(" hal_curve_name_t curve;") for var in vars: - print " const uint8_t *%8s; size_t %8s_len;" % (var, var) -print "} ecdsa_tc_t;" -print -print "static const ecdsa_tc_t ecdsa_tc[] = {" + print(" const uint8_t *{0:>8}; size_t {0:>8}_len;".format(var)) +print("} ecdsa_tc_t;") +print() +print("static const ecdsa_tc_t ecdsa_tc[] = {") for curve in curves: - print " { HAL_CURVE_%s," % curve.upper() + print(" {{ HAL_CURVE_{},".format(curve.upper())) for var in vars: name = curve + "_" + var if name in globals(): - print " %-14s sizeof(%s)," % (name + ",", name) + print(" {:<14} sizeof({}),".format(name + ",", name)) else: - print " %-14s 0," % "NULL," - print " }," -print "};" + print(" {:<14} 0,".format("NULL,")) + print(" },") +print("};") diff --git a/tests/test-rsa.py b/tests/test-rsa.py index 6b52eb9..57c554d 100644 --- a/tests/test-rsa.py +++ b/tests/test-rsa.py @@ -35,6 +35,7 @@ Use PyCrypto to generate test data for Cryptech ModExp core. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from argparse import ArgumentParser, FileType +from binascii import hexlify from Crypto import __version__ as PyCryptoVersion from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 @@ -71,32 +72,32 @@ wrapper = TextWrapper(width = 78, initial_indent = " " * 2, subsequent_indent = def printlines(*lines, **kwargs): for line in lines: - args.output.write(line % kwargs + "\n") + args.output.write(line.format(**kwargs) + "\n") def trailing_comma(item, sequence): return "" if item == sequence[-1] else "," def print_hex(name, value, comment): - printlines("static const uint8_t %(name)s[] = { /* %(comment)s, %(length)d bytes */", - wrapper.fill(", ".join("0x%02x" % ord(v) for v in value)), - "};", "", + printlines("static const uint8_t {name}[] = {{ /* {comment}, {length:d} bytes */", + wrapper.fill(", ".join("0x" + hexlify(v) for v in value)), + "}};", "", name = name, comment = comment, length = len(value)) def pad_to_blocksize(value, blocksize): extra = len(value) % blocksize - return value if extra == 0 else ("\x00" * (blocksize - extra)) + value + return value if extra == 0 else (b"\x00" * (blocksize - extra)) + value # Funnily enough, PyCrypto and Cryptlib use exactly the same names for # RSA key components, see Cryptlib documentation pages 186-187 & 339. -h = SHA256.new(plaintext) +h = SHA256.new(plaintext.encode("ascii")) printlines("/*", " * RSA signature test data for Cryptech project, automatically generated by", - " * %(scriptname)s using PyCrypto version %(version)s. Do not edit.", + " * {scriptname} using PyCrypto version {version}. Do not edit.", " *", - " * Plaintext: \"%(plaintext)s\"", - " * SHA-256: %(digest)s", + " * Plaintext: \"{plaintext}\"", + " * SHA-256: {digest}", " */", "", scriptname = scriptname, version = PyCryptoVersion, @@ -122,7 +123,7 @@ for k_len in args.key_lengths: else: blocksize = 4 - printlines("/* %(k_len)d-bit RSA private key (PKCS #%(pkcs)d)", + printlines("/* {k_len:d}-bit RSA private key (PKCS #{pkcs:d})", k.exportKey(format = "PEM", pkcs = args.pkcs_encoding), "*/", "", k_len = k_len, pkcs = args.pkcs_encoding) @@ -143,22 +144,22 @@ for k_len in args.key_lengths: else: value = getattr(k, name) - print_hex("%s_%d" % (name, k_len), + print_hex("{}_{:d}".format(name, k_len), long_to_bytes(value, blocksize = blocksize), - "key component %s" % name) + "key component {}".format(name)) - print_hex("m_%d" % k_len, pad_to_blocksize(m, blocksize), "message to be signed") - print_hex("s_%d" % k_len, pad_to_blocksize(s, blocksize), "signed message") + print_hex("m_{:d}".format(k_len), pad_to_blocksize(m, blocksize), "message to be signed") + print_hex("s_{:d}".format(k_len), pad_to_blocksize(s, blocksize), "signed message") -printlines("typedef struct { const uint8_t *val; size_t len; } rsa_tc_bn_t;", - "typedef struct { size_t size; rsa_tc_bn_t %(fields)s; } rsa_tc_t;", +printlines("typedef struct {{ const uint8_t *val; size_t len; }} rsa_tc_bn_t;", + "typedef struct {{ size_t size; rsa_tc_bn_t {fields}; }} rsa_tc_t;", "", - "static const rsa_tc_t rsa_tc[] = {", + "static const rsa_tc_t rsa_tc[] = {{", fields = ", ".join(fields)) for k_len in args.key_lengths: - printlines(" { %(k_len)d,", k_len = k_len) + printlines(" {{ {k_len:d},", k_len = k_len) for field in fields: - printlines(" { %(field)s_%(k_len)d, sizeof(%(field)s_%(k_len)d) }%(comma)s", + printlines(" {{ {field}_{k_len:d}, sizeof({field}_{k_len:d}) }}{comma}", field = field, k_len = k_len, comma = trailing_comma(field, fields)) - printlines(" }%(comma)s", comma = trailing_comma(k_len, args.key_lengths)) -printlines("};") + printlines(" }}{comma}", comma = trailing_comma(k_len, args.key_lengths)) +printlines("}};") diff --git a/tests/time-keygen.py b/tests/time-keygen.py index b7311ba..14a8119 100755 --- a/tests/time-keygen.py +++ b/tests/time-keygen.py @@ -22,7 +22,7 @@ hsm.login(HAL_USER_NORMAL, args.pin) flags = HAL_KEY_FLAG_USAGE_DIGITALSIGNATURE | (HAL_KEY_FLAG_TOKEN if args.token else 0) sum = timedelta() -for n in xrange(1, args.iterations + 1): +for n in range(1, args.iterations + 1): t0 = datetime.now() @@ -34,4 +34,4 @@ for n in xrange(1, args.iterations + 1): sum += t1 - t0 - print "{:4d} this {} mean {}".format(n, t1 - t0, sum / n) + print("{:4d} this {} mean {}".format(n, t1 - t0, sum / n)) |