aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2020-05-26 15:18:19 -0400
committerRob Austein <sra@hactrn.net>2020-05-26 15:18:19 -0400
commited4c22473f5fb07006e773137ed047950e25a4d8 (patch)
tree26f7b93d5447b2e0cf5bd9c92dcda70161a46545 /tests
parent1cd42f6d3332e1edf78b06bd7dcf51f5a1a7bb23 (diff)
Wow, python-version-independent hexadecimal is painful
Diffstat (limited to 'tests')
-rwxr-xr-xtests/parallel-signatures.py15
-rw-r--r--tests/test-ecdsa.py3
-rw-r--r--tests/test-rsa.py3
3 files changed, 15 insertions, 6 deletions
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))