1 from pygost.gost3410 import pub_marshal
2 from pygost.gost34112012256 import GOST34112012256
3 from pygost.gost34112012512 import GOST34112012512
4 from pygost.gost341194 import GOST341194
5 from pygost.utils import bytes2long
8 def vko_34102001(curve, prv, pubkey, ukm):
9 """ Make Diffie-Hellman computation (34.10-2001, 34.11-94)
11 :param GOST3410Curve curve: curve to use
12 :param long prv: private key
13 :param ukm: UKM value (VKO-factor)
14 :type ukm: bytes, 8 bytes
15 :param pubkey: public key's part
16 :type pubkey: (long, long)
17 :return: Key Encryption Key (shared key)
18 :rtype: bytes, 32 bytes
20 Shared Key Encryption Key computation is based on
21 :rfc:`4357` VKO GOST 34.10-2001 with little-endian
24 key = curve.exp(prv, pubkey[0], pubkey[1])
25 key = curve.exp(bytes2long(24 * b"\x00" + ukm), key[0], key[1])
26 return GOST341194(pub_marshal(key), "GostR3411_94_CryptoProParamSet").digest()
29 def vko_34102012256(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
30 """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 256 bit)
32 :param GOST3410Curve curve: curve to use
33 :param long prv: private key
34 :param ukm: UKM value (VKO-factor)
35 :type ukm: bytes, 8 bytes
36 :param pubkey: public key's part
37 :type pubkey: (long, long)
38 :return: Key Encryption Key (shared key)
39 :rtype: bytes, 32 bytes
41 key = curve.exp(prv, pubkey[0], pubkey[1])
42 key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
43 return GOST34112012256(pub_marshal(key, mode=2012)).digest()
46 def vko_34102012512(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
47 """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 512 bit)
49 :param GOST3410Curve curve: curve to use
50 :param long prv: private key
51 :param ukm: UKM value (VKO-factor)
52 :type ukm: bytes, 8 bytes
53 :param pubkey: public key's part
54 :type pubkey: (long, long)
55 :return: Key Encryption Key (shared key)
56 :rtype: bytes, 32 bytes
58 key = curve.exp(prv, pubkey[0], pubkey[1])
59 key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
60 return GOST34112012512(pub_marshal(key, mode=2012)).digest()