diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-04-30 11:13:17 -0500 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-04-30 11:13:17 -0500 |
commit | d3e3df9308498153e795f6421b83b97c493c1aba (patch) | |
tree | 91240c948f2734a2f5af749126a6a9d141ef461a /tests | |
parent | 3fc686ea06d4c2dcaf6b363ec1c54a72d995df88 (diff) | |
download | cryptography-d3e3df9308498153e795f6421b83b97c493c1aba.tar.gz cryptography-d3e3df9308498153e795f6421b83b97c493c1aba.tar.bz2 cryptography-d3e3df9308498153e795f6421b83b97c493c1aba.zip |
use pyasn1 for creating the dss-sig-value structure
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_utils.py | 11 | ||||
-rw-r--r-- | tests/utils.py | 34 |
2 files changed, 12 insertions, 33 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 7b3ffbc3..ae9f6928 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -140,17 +140,6 @@ def test_dss_sig_value(): assert sig3 == b"0\x06\x02\x01\x00\x02\x01\x00" -def test_dss_sig_value_negative(): - with pytest.raises(ValueError): - dss_sig_value(-1, 1) - - with pytest.raises(ValueError): - dss_sig_value(1, -1) - - with pytest.raises(ValueError): - dss_sig_value(-1, -1) - - def test_load_nist_vectors(): vector_data = textwrap.dedent(""" # CAVS 11.1 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): |