]> Cypherpunks.ru repositories - pygost.git/commitdiff
pygost.gost3410.sign rand argument and more 34.10-2012 test vectors
authorSergey Matveev <stargrave@stargrave.org>
Fri, 27 Dec 2019 10:35:30 +0000 (13:35 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 27 Dec 2019 11:19:08 +0000 (14:19 +0300)
VERSION
download.texi
news.texi
pygost/gost3410.py
pygost/stubs/pygost/gost3410.pyi
pygost/test_gost3410.py
setup.py

diff --git a/VERSION b/VERSION
index 7d5c902e777905446c67de7e52a945b88fd2038c..bf77d549685a9e09678fbbda05a071b312cf2de3 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-4.1
+4.2
index efc7f9c2b56fbec0875d9c91b0264ad71dae5994..043e1fed4b0112f3eedc21395a62f00fdce72974 100644 (file)
@@ -1,7 +1,7 @@
 @node Download
 @unnumbered Download
 
-@set VERSION 4.1
+@set VERSION 4.2
 
 No additional dependencies except Python 2.7/3.x interpreter are required.
 
index 852e836ecc6bdbe4e32a1511be90bdfabcad4693..a5775dbdeab3da8b7fb1455f60f33c883449f86c 100644 (file)
--- a/news.texi
+++ b/news.texi
@@ -3,6 +3,14 @@
 
 @table @strong
 
+@anchor{Release 4.2}
+@item 4.2
+    @itemize
+    @item @code{pygost.gost3410.sign} accepts predefined @code{rand}om
+        data used for k/r generation
+    @item More test vectors for 34.10-2012
+    @end itemize
+
 @anchor{Release 4.1}
 @item 4.1
     @itemize
index a6b1a7eab87e4ce492dc8392285175b007575291..1848ef56653d21460f1ccf247d6c22ea8c770e9d 100644 (file)
@@ -214,13 +214,15 @@ def public_key(curve, prv):
     return curve.exp(prv)
 
 
-def sign(curve, prv, digest, mode=2001):
+def sign(curve, prv, digest, rand=None, mode=2001):
     """ Calculate signature for provided digest
 
     :param GOST3410Curve curve: curve to use
     :param long prv: private key
     :param digest: digest for signing
     :type digest: bytes, 32 or 64 bytes
+    :param rand: optional predefined random data used for k/r generation
+    :type rand: bytes, 32 or 64 bytes
     :returns: signature
     :rtype: bytes, 64 or 128 bytes
     """
@@ -230,7 +232,11 @@ def sign(curve, prv, digest, mode=2001):
     if e == 0:
         e = 1
     while True:
-        k = bytes2long(urandom(size)) % q
+        if rand is None:
+            rand = urandom(size)
+        elif len(rand) != size:
+            raise ValueError("rand length != %d" % size)
+        k = bytes2long(rand) % q
         if k == 0:
             continue
         r, _ = curve.exp(k)
index f2071cc4f3040813443b00902fb809ed0b34b7ae..afab2659e665c19d430c88b6869674f5f2e8a97c 100644 (file)
@@ -40,7 +40,13 @@ class GOST3410Curve(object):
 def public_key(curve: GOST3410Curve, prv: int) -> PublicKey: ...
 
 
-def sign(curve: GOST3410Curve, prv: int, digest: bytes, mode: int=...) -> bytes: ...
+def sign(
+        curve: GOST3410Curve,
+        prv: int,
+        digest: bytes,
+        rand: bytes=None,
+        mode: int=...,
+) -> bytes: ...
 
 
 def verify(
index 02e44edcb8775695414a5a4530869092d07c337d..9d0b686f4874590212f19afed95708165acd6305 100644 (file)
@@ -26,6 +26,7 @@ from pygost.gost3410 import verify
 from pygost.gost3410 import xy2uv
 from pygost.utils import bytes2long
 from pygost.utils import hexdec
+from pygost.utils import hexenc
 from pygost.utils import long2bytes
 
 
@@ -89,6 +90,37 @@ class Test341001(TestCase):
 
 
 class Test34102012(TestCase):
+    def test_1(self):
+        """Test vector from 34.10-2012 standard itself
+        """
+        curve = CURVES["id-GostR3410-2001-TestParamSet"]
+        prv = bytes2long(hexdec("7A929ADE789BB9BE10ED359DD39A72C11B60961F49397EEE1D19CE9891EC3B28"))
+        digest = hexdec("2DFBC1B372D89A1188C09C52E0EEC61FCE52032AB1022E8E67ECE6672B043EE5")
+        rand = hexdec("77105C9B20BCD3122823C8CF6FCC7B956DE33814E95B7FE64FED924594DCEAB3")
+        signature = sign(curve, prv, digest, rand)
+        r = "41aa28d2f1ab148280cd9ed56feda41974053554a42767b83ad043fd39dc0493"
+        s = "01456c64ba4642a1653c235a98a60249bcd6d3f746b631df928014f6c5bf9c40"
+        self.assertSequenceEqual(hexenc(signature), s + r)
+
+    def test_2(self):
+        """Test vector from 34.10-2012 standard itself
+        """
+        curve = GOST3410Curve(
+            p=3623986102229003635907788753683874306021320925534678605086546150450856166624002482588482022271496854025090823603058735163734263822371964987228582907372403,
+            q=3623986102229003635907788753683874306021320925534678605086546150450856166623969164898305032863068499961404079437936585455865192212970734808812618120619743,
+            a=7,
+            b=1518655069210828534508950034714043154928747527740206436194018823352809982443793732829756914785974674866041605397883677596626326413990136959047435811826396,
+            x=1928356944067022849399309401243137598997786635459507974357075491307766592685835441065557681003184874819658004903212332884252335830250729527632383493573274,
+            y=2288728693371972859970012155529478416353562327329506180314497425931102860301572814141997072271708807066593850650334152381857347798885864807605098724013854,
+        )
+        prv = bytes2long(hexdec("0BA6048AADAE241BA40936D47756D7C93091A0E8514669700EE7508E508B102072E8123B2200A0563322DAD2827E2714A2636B7BFD18AADFC62967821FA18DD4"))
+        digest = hexdec("3754F3CFACC9E0615C4F4A7C4D8DAB531B09B6F9C170C533A71D147035B0C5917184EE536593F4414339976C647C5D5A407ADEDB1D560C4FC6777D2972075B8C")
+        rand = hexdec("0359E7F4B1410FEACC570456C6801496946312120B39D019D455986E364F365886748ED7A44B3E794434006011842286212273A6D14CF70EA3AF71BB1AE679F1")
+        signature = sign(curve, prv, digest, rand, mode=2012)
+        r = "2f86fa60a081091a23dd795e1e3c689ee512a3c82ee0dcc2643c78eea8fcacd35492558486b20f1c9ec197c90699850260c93bcbcd9c5c3317e19344e173ae36"
+        s = "1081b394696ffe8e6585e7a9362d26b6325f56778aadbc081c0bfbe933d52ff5823ce288e8c4f362526080df7f70ce406a6eeb1f56919cb92a9853bde73e5b4a"
+        self.assertSequenceEqual(hexenc(signature), s + r)
+
     def test_gcl3(self):
         """ Test vector from libgcl3
         """
index 798e8223364b71cb6486e3d50836c63788d153bf..aa255a53d11047aa1579b593d7062b87dea6f78a 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -32,7 +32,6 @@ setup(
             "COPYING",
             "INSTALL",
             "NEWS",
-            "PUBKEY.asc",
             "README",
             "THANKS",
             "VERSION",