X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Fgost3410.py;h=aa161c2c5c9b14a1db3af502ae3b01dc4fb3da75;hb=fbaa1fc82dbec2c3ebaf8d272600ef4f91649e08;hp=f4c0538476265e44194cb816cc8727b9ac2a4083;hpb=10b06c77779c91cddf073e1d1d417ada071868f4;p=pygost.git diff --git a/pygost/gost3410.py b/pygost/gost3410.py index f4c0538..aa161c2 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -1,6 +1,6 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2021 Sergey Matveev +# Copyright (C) 2015-2024 Sergey Matveev # # 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 @@ -53,8 +53,10 @@ class GOST3410Curve(object): the canonical form :param long e, d: coefficients of the equation of the elliptic curve in the twisted Edwards form + :param str name: human-readable curve name """ - def __init__(self, p, q, a, b, x, y, cofactor=1, e=None, d=None): + + def __init__(self, p, q, a, b, x, y, cofactor=1, e=None, d=None, name=None): self.p = p self.q = q self.a = a @@ -67,11 +69,15 @@ class GOST3410Curve(object): if not self.contains((x, y)): raise ValueError("Invalid parameters") self._st = None + self.name = name @property def point_size(self): return point_size(self.p) + def __repr__(self): + return "<%s: %s>" % (self.__class__.__name__, self.name) + def pos(self, v): """Make positive number """ @@ -231,10 +237,12 @@ CURVES["id-tc26-gost-3410-2012-512-paramSetTest"] = CURVES["id-tc26-gost-3410-12 CURVES["id-tc26-gost-3410-2012-512-paramSetA"] = CURVES["id-tc26-gost-3410-12-512-paramSetA"] CURVES["id-tc26-gost-3410-2012-512-paramSetB"] = CURVES["id-tc26-gost-3410-12-512-paramSetB"] CURVES["id-tc26-gost-3410-2012-512-paramSetC"] = CURVES["id-tc26-gost-3410-12-512-paramSetC"] +for _name, _curve in CURVES.items(): + _curve.name = _name DEFAULT_CURVE = CURVES["id-tc26-gost-3410-12-256-paramSetB"] -def public_key(curve, prv): +def public_key(curve, prv, mask=None): """Generate public key from the private one :param GOST3410Curve curve: curve to use @@ -242,10 +250,13 @@ def public_key(curve, prv): :returns: public key's parts, X and Y :rtype: (long, long) """ - return curve.exp(prv) + pub = curve.exp(prv) + if mask is not None: + pub = curve.exp(mask, pub[0], pub[1]) + return pub -def sign(curve, prv, digest, rand=None): +def sign(curve, prv, digest, rand=None, mask=None): """Calculate signature for provided digest :param GOST3410Curve curve: curve to use @@ -270,13 +281,18 @@ def sign(curve, prv, digest, rand=None): k = bytes2long(rand) % q if k == 0: continue - r, _ = curve.exp(k) + r, y = curve.exp(k) + if mask is not None: + r, y = curve.exp(mask, x=r, y=y) r %= q if r == 0: continue d = prv * r k *= e - s = (d + k) % q + s = d + k + if mask is not None: + s *= mask + s %= q if s == 0: continue break