diff options
Diffstat (limited to 'tests/utils.py')
-rw-r--r-- | tests/utils.py | 34 |
1 files changed, 12 insertions, 22 deletions
diff --git a/tests/utils.py b/tests/utils.py index d8565130..37565b4d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -18,6 +18,9 @@ import collections import re from contextlib import contextmanager +from pyasn1.codec.ber import encoder +from pyasn1.type import namedtype, univ + import pytest import six @@ -80,31 +83,18 @@ def raises_unsupported_algorithm(reason): assert exc_info.value._reason is reason -def _int_to_asn1_int(i): - """ - Used by dss_sig_value to convert python integers to ASN.1 integer bytes. - """ - if i == 0: - return b'\x02\x01\x00' - if i < 0: - raise ValueError("This only supports positive integers right now.") - result = [] - while i: - result.append(six.int2byte(i & 0xFF)) - i >>= 8 - # ASN.1 integers are stored big endian two's complement, so add a byte if - # the ordinal value of the last byte is over 0x7f. - if ord(result[-1]) > 127: - result.append(b"\x00") - result.reverse() - packed = b''.join(result) - return b"\x02" + chr(len(packed)).encode("ascii") + packed +class DSSSigValue(univ.Sequence): + componentType = namedtype.NamedTypes( + namedtype.NamedType('r', univ.Integer()), + namedtype.NamedType('s', univ.Integer()) + ) def dss_sig_value(r, s): - combined = _int_to_asn1_int(r) + _int_to_asn1_int(s) - sig = b"0" + chr(len(combined)).encode("ascii") + combined - return sig + sig = DSSSigValue() + sig.setComponentByName('r', r) + sig.setComponentByName('s', s) + return encoder.encode(sig) def load_vectors_from_file(filename, loader): |