# 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
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
"""
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)