]> Cypherpunks.ru repositories - pygost.git/blobdiff - pygost/gost3410.py
pygost.gost3410.sign rand argument and more 34.10-2012 test vectors
[pygost.git] / pygost / gost3410.py
index 500c0c944d8d0edcd563cacd5d721498478998d0..1848ef56653d21460f1ccf247d6c22ea8c770e9d 100644 (file)
@@ -1,11 +1,10 @@
 # coding: utf-8
 # PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
+# the Free Software Foundation, version 3 of the License.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -215,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
     """
@@ -231,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)