From: Sergey Matveev Date: Fri, 27 Dec 2019 10:35:30 +0000 (+0300) Subject: pygost.gost3410.sign rand argument and more 34.10-2012 test vectors X-Git-Tag: 4.2~2 X-Git-Url: http://www.git.cypherpunks.ru/?p=pygost.git;a=commitdiff_plain;h=39eec912f1876d3437889c5ef9187687460ebda8;hp=c8b2d178e5315f14a31ef82454b3ce36bd4e7f7d pygost.gost3410.sign rand argument and more 34.10-2012 test vectors --- diff --git a/VERSION b/VERSION index 7d5c902..bf77d54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1 +4.2 diff --git a/download.texi b/download.texi index efc7f9c..043e1fe 100644 --- a/download.texi +++ b/download.texi @@ -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. diff --git a/news.texi b/news.texi index 852e836..a5775db 100644 --- 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 diff --git a/pygost/gost3410.py b/pygost/gost3410.py index a6b1a7e..1848ef5 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -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) diff --git a/pygost/stubs/pygost/gost3410.pyi b/pygost/stubs/pygost/gost3410.pyi index f2071cc..afab265 100644 --- a/pygost/stubs/pygost/gost3410.pyi +++ b/pygost/stubs/pygost/gost3410.pyi @@ -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( diff --git a/pygost/test_gost3410.py b/pygost/test_gost3410.py index 02e44ed..9d0b686 100644 --- a/pygost/test_gost3410.py +++ b/pygost/test_gost3410.py @@ -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 """ diff --git a/setup.py b/setup.py index 798e822..aa255a5 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,6 @@ setup( "COPYING", "INSTALL", "NEWS", - "PUBKEY.asc", "README", "THANKS", "VERSION",