From: Sergey Matveev Date: Sat, 19 Nov 2016 14:40:09 +0000 (+0300) Subject: Shorter private key variable name X-Git-Tag: 3.0~15 X-Git-Url: http://www.git.cypherpunks.ru/?p=pygost.git;a=commitdiff_plain;h=4b7fd2c1923d5803afe1b078727ee68624430af9 Shorter private key variable name --- diff --git a/pygost/gost3410.py b/pygost/gost3410.py index ed84f3e..77637c3 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -173,22 +173,22 @@ class GOST3410Curve(object): return tx, ty -def public_key(curve, private_key): +def public_key(curve, prv): """ Generate public key from the private one :param GOST3410Curve curve: curve to use - :param long private_key: private key + :param long prv: private key :return: public key's parts, X and Y :rtype: (long, long) """ - return curve.exp(private_key) + return curve.exp(prv) -def sign(curve, private_key, digest, mode=2001): +def sign(curve, prv, digest, mode=2001): """ Calculate signature for provided digest :param GOST3410Curve curve: curve to use - :param long private_key: private key + :param long prv: private key :param digest: digest for signing :type digest: bytes, 32 or 64 bytes :return: signature @@ -207,7 +207,7 @@ def sign(curve, private_key, digest, mode=2001): r %= q if r == 0: continue - d = private_key * r + d = prv * r k *= e s = (d + k) % q if s == 0: @@ -260,13 +260,13 @@ def verify(curve, pub, digest, signature, mode=2001): return lm == r -def prv_unmarshal(private_key): +def prv_unmarshal(prv): """Unmarshal private key - :param bytes private_key: serialized private key + :param bytes prv: serialized private key :rtype: long """ - return bytes2long(private_key[::-1]) + return bytes2long(prv[::-1]) def pub_marshal(pub, mode=2001): diff --git a/pygost/gost3410_vko.py b/pygost/gost3410_vko.py index e8bc13f..affe819 100644 --- a/pygost/gost3410_vko.py +++ b/pygost/gost3410_vko.py @@ -5,11 +5,11 @@ from pygost.gost341194 import GOST341194 from pygost.utils import bytes2long -def vko_34102001(curve, private_key, pubkey, ukm): +def vko_34102001(curve, prv, pubkey, ukm): """ Make Diffie-Hellman computation (34.10-2001, 34.11-94) :param GOST3410Curve curve: curve to use - :param long private_key: private key + :param long prv: private key :param ukm: UKM value (VKO-factor) :type ukm: bytes, 8 bytes :param pubkey: public key's part @@ -21,16 +21,16 @@ def vko_34102001(curve, private_key, pubkey, ukm): :rfc:`4357` VKO GOST 34.10-2001 with little-endian hash output. """ - key = curve.exp(private_key, pubkey[0], pubkey[1]) + key = curve.exp(prv, pubkey[0], pubkey[1]) key = curve.exp(bytes2long(24 * b"\x00" + ukm), key[0], key[1]) return GOST341194(pub_marshal(key), "GostR3411_94_CryptoProParamSet").digest() -def vko_34102012256(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"): +def vko_34102012256(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"): """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 256 bit) :param GOST3410Curve curve: curve to use - :param long private_key: private key + :param long prv: private key :param ukm: UKM value (VKO-factor) :type ukm: bytes, 8 bytes :param pubkey: public key's part @@ -38,16 +38,16 @@ def vko_34102012256(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x :return: Key Encryption Key (shared key) :rtype: bytes, 32 bytes """ - key = curve.exp(private_key, pubkey[0], pubkey[1]) + key = curve.exp(prv, pubkey[0], pubkey[1]) key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1]) return GOST34112012256(pub_marshal(key, mode=2012)).digest() -def vko_34102012512(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"): +def vko_34102012512(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"): """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 512 bit) :param GOST3410Curve curve: curve to use - :param long private_key: private key + :param long prv: private key :param ukm: UKM value (VKO-factor) :type ukm: bytes, 8 bytes :param pubkey: public key's part @@ -55,6 +55,6 @@ def vko_34102012512(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x :return: Key Encryption Key (shared key) :rtype: bytes, 32 bytes """ - key = curve.exp(private_key, pubkey[0], pubkey[1]) + key = curve.exp(prv, pubkey[0], pubkey[1]) key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1]) return GOST34112012512(pub_marshal(key, mode=2012)).digest() diff --git a/pygost/stubs/pygost/gost3410.pyi b/pygost/stubs/pygost/gost3410.pyi index 84028df..85128c3 100644 --- a/pygost/stubs/pygost/gost3410.pyi +++ b/pygost/stubs/pygost/gost3410.pyi @@ -21,15 +21,10 @@ class GOST3410Curve(object): def exp(self, degree: int, x: int=..., y: int=...) -> int: ... -def public_key(curve: GOST3410Curve, private_key: int) -> PublicKey: ... +def public_key(curve: GOST3410Curve, prv: int) -> PublicKey: ... -def sign( - curve: GOST3410Curve, - private_key: int, - digest: bytes, - size: int=..., -) -> bytes: ... +def sign(curve: GOST3410Curve, prv: int, digest: bytes, size: int=...) -> bytes: ... def verify( @@ -41,7 +36,7 @@ def verify( ) -> bool: ... -def prv_unmarshal(private_key: bytes) -> int: ... +def prv_unmarshal(prv: bytes) -> int: ... def pub_marshal(pub: PublicKey, mode: int) -> bytes: ... diff --git a/pygost/stubs/pygost/gost3410_vko.pyi b/pygost/stubs/pygost/gost3410_vko.pyi index 3362793..fc1f3b0 100644 --- a/pygost/stubs/pygost/gost3410_vko.pyi +++ b/pygost/stubs/pygost/gost3410_vko.pyi @@ -2,25 +2,10 @@ from pygost.gost3410 import GOST3410Curve from pygost.gost3410 import PublicKey -def vko_34102001( - curve: GOST3410Curve, - private_key: int , - pubkey: PublicKey, - ukm: bytes, -) -> bytes: ... +def vko_34102001(curve: GOST3410Curve, prv: int, pubkey: PublicKey, ukm: bytes) -> bytes: ... -def vko_34102012256( - curve: GOST3410Curve, - private_key: int, - pubkey: PublicKey, - ukm=...: bytes, -) -> bytes: ... +def vko_34102012256(curve: GOST3410Curve, prv: int, pubkey: PublicKey, ukm=...: bytes) -> bytes: ... -def vko_34102012512( - curve: GOST3410Curve, - private_key: int, - pubkey: PublicKey, - ukm=...: bytes, -) -> bytes: ... +def vko_34102012512(curve: GOST3410Curve, prv: int, pubkey: PublicKey, ukm=...: bytes) -> bytes: ... diff --git a/pygost/test_gost3410.py b/pygost/test_gost3410.py index 8279ac3..273fc49 100644 --- a/pygost/test_gost3410.py +++ b/pygost/test_gost3410.py @@ -31,19 +31,19 @@ class Test341001(TestCase): def test_rfc(self): """ Test vector from :rfc:`5832` """ - private_key = bytes(bytearray(( + prv = bytes(bytearray(( 0x7A, 0x92, 0x9A, 0xDE, 0x78, 0x9B, 0xB9, 0xBE, 0x10, 0xED, 0x35, 0x9D, 0xD3, 0x9A, 0x72, 0xC1, 0x1B, 0x60, 0x96, 0x1F, 0x49, 0x39, 0x7E, 0xEE, 0x1D, 0x19, 0xCE, 0x98, 0x91, 0xEC, 0x3B, 0x28 ))) - public_key_x = bytes(bytearray(( + pub_x = bytes(bytearray(( 0x7F, 0x2B, 0x49, 0xE2, 0x70, 0xDB, 0x6D, 0x90, 0xD8, 0x59, 0x5B, 0xEC, 0x45, 0x8B, 0x50, 0xC5, 0x85, 0x85, 0xBA, 0x1D, 0x4E, 0x9B, 0x78, 0x8F, 0x66, 0x89, 0xDB, 0xD8, 0xE5, 0x6F, 0xD8, 0x0B ))) - public_key_y = bytes(bytearray(( + pub_y = bytes(bytearray(( 0x26, 0xF1, 0xB4, 0x89, 0xD6, 0x70, 0x1D, 0xD1, 0x85, 0xC8, 0x41, 0x3A, 0x97, 0x7B, 0x3C, 0xBB, 0xAF, 0x64, 0xD1, 0xC5, 0x93, 0xD2, 0x66, 0x27, @@ -65,24 +65,24 @@ class Test341001(TestCase): 0xBC, 0xD6, 0xD3, 0xF7, 0x46, 0xB6, 0x31, 0xDF, 0x92, 0x80, 0x14, 0xF6, 0xC5, 0xBF, 0x9C, 0x40 ))) - private_key = bytes2long(private_key) + prv = bytes2long(prv) signature = signature[32:] + signature[:32] c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"]) - pubX, pubY = public_key(c, private_key) - self.assertEqual(long2bytes(pubX), public_key_x) - self.assertEqual(long2bytes(pubY), public_key_y) - s = sign(c, private_key, digest) + pubX, pubY = public_key(c, prv) + self.assertEqual(long2bytes(pubX), pub_x) + self.assertEqual(long2bytes(pubY), pub_y) + s = sign(c, prv, digest) self.assertTrue(verify(c, (pubX, pubY), digest, s)) self.assertTrue(verify(c, (pubX, pubY), digest, signature)) def test_sequence(self): c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"]) - private_key = bytes2long(urandom(32)) - pubX, pubY = public_key(c, private_key) + prv = bytes2long(urandom(32)) + pubX, pubY = public_key(c, prv) for _ in range(20): digest = urandom(32) - s = sign(c, private_key, digest, mode=2001) + s = sign(c, prv, digest, mode=2001) self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2001)) @@ -150,7 +150,7 @@ class Test34102012(TestCase): 0xDC, 0x1A, 0x18, 0xB9, 0x1B, 0x24, 0x64, 0x0B, 0x6D, 0xBB, 0x92, 0xCB, 0x1A, 0xDD, 0x37, 0x1E ))) - private_key = bytes(bytearray(( + prv = bytes(bytearray(( 0x0B, 0xA6, 0x04, 0x8A, 0xAD, 0xAE, 0x24, 0x1B, 0xA4, 0x09, 0x36, 0xD4, 0x77, 0x56, 0xD7, 0xC9, 0x30, 0x91, 0xA0, 0xE8, 0x51, 0x46, 0x69, 0x70, @@ -160,7 +160,7 @@ class Test34102012(TestCase): 0xA2, 0x63, 0x6B, 0x7B, 0xFD, 0x18, 0xAA, 0xDF, 0xC6, 0x29, 0x67, 0x82, 0x1F, 0xA1, 0x8D, 0xD4 ))) - public_key_x = bytes(bytearray(( + pub_x = bytes(bytearray(( 0x11, 0x5D, 0xC5, 0xBC, 0x96, 0x76, 0x0C, 0x7B, 0x48, 0x59, 0x8D, 0x8A, 0xB9, 0xE7, 0x40, 0xD4, 0xC4, 0xA8, 0x5A, 0x65, 0xBE, 0x33, 0xC1, 0x81, @@ -170,7 +170,7 @@ class Test34102012(TestCase): 0xF7, 0x5C, 0x45, 0x41, 0x5C, 0x1D, 0x9D, 0xD9, 0xDD, 0x33, 0x61, 0x2C, 0xD5, 0x30, 0xEF, 0xE1 ))) - public_key_y = bytes(bytearray(( + pub_y = bytes(bytearray(( 0x37, 0xC7, 0xC9, 0x0C, 0xD4, 0x0B, 0x0F, 0x56, 0x21, 0xDC, 0x3A, 0xC1, 0xB7, 0x51, 0xCF, 0xA0, 0xE2, 0x63, 0x4F, 0xA0, 0x50, 0x3B, 0x3D, 0x52, @@ -208,23 +208,23 @@ class Test34102012(TestCase): 0x6A, 0x6E, 0xEB, 0x1F, 0x56, 0x91, 0x9C, 0xB9, 0x2A, 0x98, 0x53, 0xBD, 0xE7, 0x3E, 0x5B, 0x4A ))) - private_key = bytes2long(private_key) + prv = bytes2long(prv) signature = signature[64:] + signature[:64] c = GOST3410Curve(p, q, a, b, x, y) - pubX, pubY = public_key(c, private_key) - self.assertEqual(long2bytes(pubX), public_key_x) - self.assertEqual(long2bytes(pubY), public_key_y) - s = sign(c, private_key, digest, mode=2012) + pubX, pubY = public_key(c, prv) + self.assertEqual(long2bytes(pubX), pub_x) + self.assertEqual(long2bytes(pubY), pub_y) + s = sign(c, prv, digest, mode=2012) self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2012)) self.assertTrue(verify(c, (pubX, pubY), digest, signature, mode=2012)) def test_sequence(self): c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"]) - private_key = bytes2long(urandom(64)) - pubX, pubY = public_key(c, private_key) + prv = bytes2long(urandom(64)) + pubX, pubY = public_key(c, prv) for _ in range(20): digest = urandom(64) - s = sign(c, private_key, digest, mode=2012) + s = sign(c, prv, digest, mode=2012) self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2012)) self.assertNotIn(b"\x00" * 8, s)