From ed4c22473f5fb07006e773137ed047950e25a4d8 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Tue, 26 May 2020 15:18:19 -0400 Subject: Wow, python-version-independent hexadecimal is painful --- tests/parallel-signatures.py | 15 +++++++++++---- tests/test-ecdsa.py | 3 ++- tests/test-rsa.py | 3 ++- 3 files changed, 15 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/parallel-signatures.py b/tests/parallel-signatures.py index 7cb7132..87bac0a 100755 --- a/tests/parallel-signatures.py +++ b/tests/parallel-signatures.py @@ -44,7 +44,6 @@ import uuid import xdrlib import socket import logging -import binascii import datetime import collections @@ -81,6 +80,14 @@ globals().update((name, getattr(cryptech.libhal, name)) for prefix in ("HAL", "RPC", "SLIP"))) +if sys.version_info.major == 2: + def colon_hex(raw): + return ":".join("{:02x}".format(ord(b)) for b in raw) +else: + def colon_hex(raw): + return ":".join("{:02x}".format(b) for b in raw) + + class PKey(cryptech.libhal.Handle): def __init__(self, hsm, handle, uuid): @@ -136,7 +143,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(binascii.hexlify(c) for c in packer)) + logger.debug("send: %s", colon_hex(packer)) yield self.iostream.write(packer) while True: try: @@ -144,7 +151,7 @@ class HSM(cryptech.libhal.HSM): except StreamClosedError: raise HAL_ERROR_RPC_TRANSPORT() if self.debug_io: - logger.debug("recv: %s", ":".join(binascii.hexlify(c) for c in unpacker)) + logger.debug("recv: %s", colon_hex(unpacker)) unpacker = cryptech.libhal.slip_decode(unpacker) if not unpacker: continue @@ -210,7 +217,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(binascii.hexlify(b) for b in s)) + logger.debug("Signature %s: %s", n, colon_hex(s)) if args.verify and not v.verify(h, s): raise RuntimeError("RSA verification failed") r.add(t0, t1) diff --git a/tests/test-ecdsa.py b/tests/test-ecdsa.py index cf21019..4c14d9f 100644 --- a/tests/test-ecdsa.py +++ b/tests/test-ecdsa.py @@ -126,9 +126,10 @@ for curve in curves: if isinstance(value, int): value = long_to_bytes(value, order) if value is not None: + value = hexlify(value).decode("ascii") print() print("static const uint8_t {}[] = {{ /* {:d} bytes */".format(name, len(value))) - print(wrapper.fill(", ".join("0x" + hexlify(v) for v in value))) + print(wrapper.fill(", ".join("0x" + value[i : i + 2] for i in range(0, len(value), 2)))) print("};") print() diff --git a/tests/test-rsa.py b/tests/test-rsa.py index 57c554d..d0538ed 100644 --- a/tests/test-rsa.py +++ b/tests/test-rsa.py @@ -78,8 +78,9 @@ def trailing_comma(item, sequence): return "" if item == sequence[-1] else "," def print_hex(name, value, comment): + value = hexlify(value).decode("ascii") printlines("static const uint8_t {name}[] = {{ /* {comment}, {length:d} bytes */", - wrapper.fill(", ".join("0x" + hexlify(v) for v in value)), + wrapper.fill(", ".join("0x" + value[i : i + 2] for i in range(0, len(value), 2))) "}};", "", name = name, comment = comment, length = len(value)) -- cgit v1.2.3