]> Cypherpunks.ru repositories - pygost.git/commitdiff
Digest size for 34.10 does not depend on 2001/2012 implementations
authorSergey Matveev <stargrave@stargrave.org>
Sat, 19 Nov 2016 14:09:36 +0000 (17:09 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 19 Nov 2016 14:43:56 +0000 (17:43 +0300)
pygost/gost3410.py
pygost/test_gost3410.py

index 8ef068f650a46bdf31739adc30e297571711799b..4fce335379fdb55b7fb1aec35b1244f69d4f33dc 100644 (file)
@@ -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]))
index 9704320dafd24c8114454990e07e1c2de9681f56..6a86f0b184a313c6ef1a1f1f153296857fdd5c5b 100644 (file)
@@ -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)