From 176360adaf2861d7665c29382acae3b9a7318070 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 19 Nov 2016 17:09:36 +0300 Subject: [PATCH] Digest size for 34.10 does not depend on 2001/2012 implementations --- pygost/gost3410.py | 22 +++++++--------------- pygost/test_gost3410.py | 16 +++++++--------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pygost/gost3410.py b/pygost/gost3410.py index 8ef068f..4fce335 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -29,11 +29,9 @@ from pygost.utils import long2bytes from pygost.utils import modinvert -SIZE_3410_2001 = 32 -SIZE_3410_2012 = 64 MODE2SIZE = { - 2001: SIZE_3410_2001, - 2012: SIZE_3410_2012, + 2001: 32, + 2012: 64, } @@ -186,20 +184,17 @@ def public_key(curve, private_key): return curve.exp(private_key) -def sign(curve, private_key, digest, size=SIZE_3410_2001): +def sign(curve, private_key, digest, mode=2001): """ Calculate signature for provided digest :param GOST3410Curve curve: curve to use :param long private_key: private key :param digest: digest for signing :type digest: bytes, 32 or 64 bytes - :param size: signature size - :type size: 32 (for 34.10-2001) or 64 (for 34.10-2012) :return: signature :rtype: bytes, 64 or 128 bytes """ - if len(digest) != size: - raise ValueError("Invalid digest length") + size = MODE2SIZE[mode] q = curve.q e = bytes2long(digest) % q if e == 0: @@ -221,7 +216,7 @@ def sign(curve, private_key, digest, size=SIZE_3410_2001): return long2bytes(s, size) + long2bytes(r, size) -def verify(curve, pubkeyX, pubkeyY, digest, signature, size=SIZE_3410_2001): +def verify(curve, pubkeyX, pubkeyY, digest, signature, mode=2001): """ Verify provided digest with the signature :param GOST3410Curve curve: curve to use @@ -231,12 +226,9 @@ def verify(curve, pubkeyX, pubkeyY, digest, signature, size=SIZE_3410_2001): :type digest: bytes, 32 or 64 bytes :param signature: signature to verify with :type signature: bytes, 64 or 128 bytes - :param size: signature size - :type size: 32 (for 34.10-2001) or 64 (for 34.10-2012) :rtype: bool """ - if len(digest) != size: - raise ValueError("Invalid digest length") + size = MODE2SIZE[mode] if len(signature) != size * 2: raise ValueError("Invalid signature length") q = curve.q @@ -294,6 +286,6 @@ def pub_unmarshal(pub, mode=2001): :type pub: bytes :rtype: (long, long) """ - pub = pub[::-1] size = MODE2SIZE[mode] + pub = pub[::-1] return (bytes2long(pub[size:]), bytes2long(pub[:size])) diff --git a/pygost/test_gost3410.py b/pygost/test_gost3410.py index 9704320..6a86f0b 100644 --- a/pygost/test_gost3410.py +++ b/pygost/test_gost3410.py @@ -22,8 +22,6 @@ from pygost.gost3410 import CURVE_PARAMS from pygost.gost3410 import GOST3410Curve from pygost.gost3410 import public_key from pygost.gost3410 import sign -from pygost.gost3410 import SIZE_3410_2001 -from pygost.gost3410 import SIZE_3410_2012 from pygost.gost3410 import verify from pygost.utils import bytes2long from pygost.utils import long2bytes @@ -84,8 +82,8 @@ class Test341001(TestCase): pubX, pubY = public_key(c, private_key) for _ in range(20): digest = urandom(32) - s = sign(c, private_key, digest, size=SIZE_3410_2001) - self.assertTrue(verify(c, pubX, pubY, digest, s, size=SIZE_3410_2001)) + s = sign(c, private_key, digest, mode=2001) + self.assertTrue(verify(c, pubX, pubY, digest, s, mode=2001)) class Test34102012(TestCase): @@ -217,9 +215,9 @@ class Test34102012(TestCase): pubX, pubY = public_key(c, private_key) self.assertEqual(long2bytes(pubX), public_key_x) self.assertEqual(long2bytes(pubY), public_key_y) - s = sign(c, private_key, digest, size=SIZE_3410_2012) - self.assertTrue(verify(c, pubX, pubY, digest, s, size=SIZE_3410_2012)) - self.assertTrue(verify(c, pubX, pubY, digest, signature, size=SIZE_3410_2012)) + s = sign(c, private_key, digest, mode=2012) + self.assertTrue(verify(c, pubX, pubY, digest, s, mode=2012)) + self.assertTrue(verify(c, pubX, pubY, digest, signature, mode=2012)) def test_sequence(self): c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"]) @@ -227,6 +225,6 @@ class Test34102012(TestCase): pubX, pubY = public_key(c, private_key) for _ in range(20): digest = urandom(64) - s = sign(c, private_key, digest, size=SIZE_3410_2012) - self.assertTrue(verify(c, pubX, pubY, digest, s, size=SIZE_3410_2012)) + s = sign(c, private_key, digest, mode=2012) + self.assertTrue(verify(c, pubX, pubY, digest, s, mode=2012)) self.assertNotIn(b"\x00" * 8, s) -- 2.44.0