aboutsummaryrefslogtreecommitdiff
path: root/tests/test-ecdsa.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2020-06-10 15:38:05 -0400
committerRob Austein <sra@hactrn.net>2020-06-10 15:38:05 -0400
commitd015707e0e26bafbfad72511c0a47cbb66c90971 (patch)
tree534830d09f64ad57a47eed63521ee4eb7ac18938 /tests/test-ecdsa.py
parent2f21fc43638cf39da07ab85dd21546bc34515730 (diff)
Fix remaining Python 3 unit test string encoding bug
Really just one bug, but confusingly masked by an interaction between generators and our XDR context manager, so don't use the context manager in the one generator method in the cryptech.libhal API. Also run reindent.py on a few old test modules.
Diffstat (limited to 'tests/test-ecdsa.py')
-rw-r--r--tests/test-ecdsa.py108
1 files changed, 54 insertions, 54 deletions
diff --git a/tests/test-ecdsa.py b/tests/test-ecdsa.py
index 4c14d9f..4989619 100644
--- a/tests/test-ecdsa.py
+++ b/tests/test-ecdsa.py
@@ -52,27 +52,27 @@ from pyasn1.codec.der.decoder import decode as DER_Decode
wrapper = TextWrapper(width = 78, initial_indent = " " * 2, subsequent_indent = " " * 2)
def long_to_bytes(number, order):
- #
- # This is just plain nasty.
- #
- s = "{:x}".format(number)
- s = ("0" * (order/8 - len(s))) + s
- return unhexlify(s)
+ #
+ # This is just plain nasty.
+ #
+ s = "{:x}".format(number)
+ s = ("0" * (order/8 - len(s))) + s
+ return unhexlify(s)
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(int(hexlify(b), 16))[2:]
- if len(s) % 8:
- s = ("0" * (8 - len(s) % 8)) + s
- return tuple(int(i) for i in s)
+ #
+ # This, on the other hand, is not just plain nasty, this is fancy nasty.
+ # This is nasty with raisins in it.
+ #
+ 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)
###
def encode_sig(r, s, order):
- return long_to_bytes(r, order) + long_to_bytes(s, order)
+ return long_to_bytes(r, order) + long_to_bytes(s, order)
p256_sig = encode_sig(p256_r, p256_s, 256)
p384_sig = encode_sig(p384_r, p384_s, 384)
@@ -80,23 +80,23 @@ p384_sig = encode_sig(p384_r, p384_s, 384)
###
class ECPrivateKey(Sequence):
- componentType = NamedTypes(
- NamedType("version", Integer(namedValues = NamedValues(("ecPrivkeyVer1", 1))
- ).subtype(subtypeSpec = Integer.subtypeSpec + SingleValueConstraint(1))),
- NamedType("privateKey", OctetString()),
- OptionalNamedType("parameters", ObjectIdentifier().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 0))),
- OptionalNamedType("publicKey", BitString().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 1))))
+ componentType = NamedTypes(
+ NamedType("version", Integer(namedValues = NamedValues(("ecPrivkeyVer1", 1))
+ ).subtype(subtypeSpec = Integer.subtypeSpec + SingleValueConstraint(1))),
+ NamedType("privateKey", OctetString()),
+ OptionalNamedType("parameters", ObjectIdentifier().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 0))),
+ OptionalNamedType("publicKey", BitString().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 1))))
def encode_key(d, Qx, Qy, order, oid):
- private_key = long_to_bytes(d, order)
- public_key = bytes_to_bits(chr(0x04) + long_to_bytes(Qx, order) + long_to_bytes(Qy, order))
- parameters = oid
- key = ECPrivateKey()
- key["version"] = 1
- key["privateKey"] = private_key
- key["parameters"] = parameters
- key["publicKey"] = public_key
- return DER_Encode(key)
+ private_key = long_to_bytes(d, order)
+ public_key = bytes_to_bits(chr(0x04) + long_to_bytes(Qx, order) + long_to_bytes(Qy, order))
+ parameters = oid
+ key = ECPrivateKey()
+ key["version"] = 1
+ key["privateKey"] = private_key
+ key["parameters"] = parameters
+ key["publicKey"] = public_key
+ return DER_Encode(key)
p256_key = encode_key(p256_d, p256_Qx, p256_Qy, 256, "1.2.840.10045.3.1.7")
p384_key = encode_key(p384_d, p384_Qx, p384_Qy, 384, "1.3.132.0.34")
@@ -112,41 +112,41 @@ curves = ("p256", "p384")
vars = set()
for name in dir():
- head, sep, tail = name.partition("_")
- if head in curves:
- vars.add(tail)
+ head, sep, tail = name.partition("_")
+ if head in curves:
+ vars.add(tail)
vars = sorted(vars)
for curve in curves:
- order = int(curve[1:])
- for var in vars:
- name = curve + "_" + var
- value = globals().get(name, None)
- 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" + value[i : i + 2] for i in range(0, len(value), 2))))
- print("};")
+ order = int(curve[1:])
+ for var in vars:
+ name = curve + "_" + var
+ value = globals().get(name, None)
+ 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" + value[i : i + 2] for i in range(0, len(value), 2))))
+ print("};")
print()
print("typedef struct {")
print(" hal_curve_name_t curve;")
for var in vars:
- print(" const uint8_t *{0:>8}; size_t {0:>8}_len;".format(var))
+ 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_{},".format(curve.upper()))
- for var in vars:
- name = curve + "_" + var
- if name in globals():
- print(" {:<14} sizeof({}),".format(name + ",", name))
- else:
- print(" {:<14} 0,".format("NULL,"))
- print(" },")
+ print(" {{ HAL_CURVE_{},".format(curve.upper()))
+ for var in vars:
+ name = curve + "_" + var
+ if name in globals():
+ print(" {:<14} sizeof({}),".format(name + ",", name))
+ else:
+ print(" {:<14} 0,".format("NULL,"))
+ print(" },")
print("};")