aboutsummaryrefslogtreecommitdiff
path: root/tests/test-ecdsa.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-ecdsa.py')
-rw-r--r--tests/test-ecdsa.py53
1 files changed, 27 insertions, 26 deletions
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("};")