diff options
author | Alex Stapleton <alexs@prol.etari.at> | 2014-04-30 20:47:52 +0100 |
---|---|---|
committer | Alex Stapleton <alexs@prol.etari.at> | 2014-04-30 20:47:52 +0100 |
commit | 461e83742aec0da4dc34ef0428f7b8e20f40b174 (patch) | |
tree | aaccb0bf201e389148b5d47aed0f986d5d6446ac | |
parent | 26f2d5352911ff99c9c7650c926eaa73c432064e (diff) | |
parent | be8ce55717d4c2951a76c0989a692a20aa338ea1 (diff) | |
download | cryptography-461e83742aec0da4dc34ef0428f7b8e20f40b174.tar.gz cryptography-461e83742aec0da4dc34ef0428f7b8e20f40b174.tar.bz2 cryptography-461e83742aec0da4dc34ef0428f7b8e20f40b174.zip |
Merge pull request #980 from reaperhulk/dsa-sig-serializer
dss_sig_value: Pure python conversion of (r, s) to byte stream
-rw-r--r-- | dev-requirements.txt | 1 | ||||
-rw-r--r-- | setup.py | 1 | ||||
-rw-r--r-- | tests/test_utils.py | 30 | ||||
-rw-r--r-- | tests/utils.py | 17 | ||||
-rw-r--r-- | tox.ini | 1 |
5 files changed, 45 insertions, 5 deletions
diff --git a/dev-requirements.txt b/dev-requirements.txt index 092b9914..4fff76b5 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -5,6 +5,7 @@ invoke iso8601 pep8-naming pretend +pyasn1 pytest requests sphinx @@ -44,6 +44,7 @@ requirements = [ # If you add a new dep here you probably need to add it in the tox.ini as well test_requirements = [ "pytest", + "pyasn1", "pretend", "iso8601", ] diff --git a/tests/test_utils.py b/tests/test_utils.py index 7a0b9e74..4673b49e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -27,11 +27,11 @@ from cryptography.exceptions import UnsupportedAlgorithm, _Reasons import cryptography_vectors from .utils import ( - check_backend_support, check_for_iface, load_cryptrec_vectors, - load_fips_dsa_key_pair_vectors, load_fips_dsa_sig_vectors, - load_fips_ecdsa_key_pair_vectors, load_fips_ecdsa_signing_vectors, - load_hash_vectors, load_nist_vectors, load_pkcs1_vectors, - load_rsa_nist_vectors, load_vectors_from_file, + check_backend_support, check_for_iface, der_encode_dsa_signature, + load_cryptrec_vectors, load_fips_dsa_key_pair_vectors, + load_fips_dsa_sig_vectors, load_fips_ecdsa_key_pair_vectors, + load_fips_ecdsa_signing_vectors, load_hash_vectors, load_nist_vectors, + load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file, raises_unsupported_algorithm, select_backends ) @@ -125,6 +125,26 @@ def test_check_backend_support_no_backend(): check_backend_support(item) +def test_der_encode_dsa_signature_values(): + sig = der_encode_dsa_signature(1, 1) + assert sig == b"0\x06\x02\x01\x01\x02\x01\x01" + + sig2 = der_encode_dsa_signature( + 1037234182290683143945502320610861668562885151617, + 559776156650501990899426031439030258256861634312 + ) + assert sig2 == ( + b'0-\x02\x15\x00\xb5\xaf0xg\xfb\x8bT9\x00\x13\xccg\x02\r\xdf\x1f,\x0b' + b'\x81\x02\x14b\r;"\xabP1D\x0c>5\xea\xb6\xf4\x81)\x8f\x9e\x9f\x08' + ) + + sig3 = der_encode_dsa_signature(0, 0) + assert sig3 == b"0\x06\x02\x01\x00\x02\x01\x00" + + sig4 = der_encode_dsa_signature(-1, 0) + assert sig4 == b"0\x06\x02\x01\xFF\x02\x01\x00" + + def test_load_nist_vectors(): vector_data = textwrap.dedent(""" # CAVS 11.1 diff --git a/tests/utils.py b/tests/utils.py index 60b6f5a2..5c0e2343 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -18,6 +18,9 @@ import collections import re from contextlib import contextmanager +from pyasn1.codec.der import encoder +from pyasn1.type import namedtype, univ + import pytest import six @@ -80,6 +83,20 @@ def raises_unsupported_algorithm(reason): assert exc_info.value._reason is reason +class _DSSSigValue(univ.Sequence): + componentType = namedtype.NamedTypes( + namedtype.NamedType('r', univ.Integer()), + namedtype.NamedType('s', univ.Integer()) + ) + + +def der_encode_dsa_signature(r, s): + sig = _DSSSigValue() + sig.setComponentByName('r', r) + sig.setComponentByName('s', s) + return encoder.encode(sig) + + def load_vectors_from_file(filename, loader): with cryptography_vectors.open_vector_file(filename) as vector_file: return loader(vector_file) @@ -7,6 +7,7 @@ deps = coverage iso8601 pretend + pyasn1 pytest ./vectors commands = |