]> Cypherpunks.ru repositories - pygost.git/blob - pygost/gost3410_vko.py
34.10-2012 VKO implementation and remove X.509-compatibility helpers
[pygost.git] / pygost / gost3410_vko.py
1 from pygost.gost3410 import pub_marshal
2 from pygost.gost3411_2012_256 import GOST34112012256
3 from pygost.gost3411_2012_512 import GOST34112012512
4 from pygost.gost3411_94 import GOST341194
5 from pygost.utils import bytes2long
6
7
8 def vko_34102001(curve, private_key, pubkey, ukm):
9     """ Make Diffie-Hellman computation (34.10-2001, 34.11-94)
10
11     :param GOST3410Curve curve: curve to use
12     :param long private_key: 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
19
20     Shared Key Encryption Key computation is based on
21     :rfc:`4357` VKO GOST 34.10-2001 with little-endian
22     hash output.
23     """
24     key = curve.exp(private_key, 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()[::-1]
27
28
29 def vko_34102012256(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
30     """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 256 bit)
31
32     :param GOST3410Curve curve: curve to use
33     :param long private_key: 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
40     """
41     key = curve.exp(private_key, pubkey[0], pubkey[1])
42     key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
43     return GOST34112012256(pub_marshal(key, mode=2012)).digest()
44
45
46 def vko_34102012512(curve, private_key, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
47     """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 512 bit)
48
49     :param GOST3410Curve curve: curve to use
50     :param long private_key: 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
57     """
58     key = curve.exp(private_key, pubkey[0], pubkey[1])
59     key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
60     return GOST34112012512(pub_marshal(key, mode=2012)).digest()