]> Cypherpunks.ru repositories - pygost.git/blob - pygost/gost3410_vko.py
VKO GOST is VKO GOST R standard
[pygost.git] / pygost / gost3410_vko.py
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
6
7
8 def vko_34102001(curve, prv, pubkey, ukm):
9     """ Make Diffie-Hellman computation (34.10-2001, 34.11-94)
10
11     :param GOST3410Curve curve: curve to use
12     :param long prv: private key
13     :param pubkey: public key
14     :type pubkey: (long, long)
15     :param ukm: UKM value (VKO-factor)
16     :type ukm: bytes, 8 bytes
17     :returns: 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 R 34.10-2001 with little-endian
22     hash output.
23     """
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()
27
28
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)
31
32     :param GOST3410Curve curve: curve to use
33     :param long prv: private key
34     :param pubkey: public key
35     :type pubkey: (long, long)
36     :param ukm: UKM value (VKO-factor)
37     :type ukm: bytes, 8 bytes
38     :returns: Key Encryption Key (shared key)
39     :rtype: bytes, 32 bytes
40     """
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()
44
45
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)
48
49     :param GOST3410Curve curve: curve to use
50     :param long prv: private key
51     :param pubkey: public key
52     :type pubkey: (long, long)
53     :param ukm: UKM value (VKO-factor)
54     :type ukm: bytes, 8 bytes
55     :returns: Key Encryption Key (shared key)
56     :rtype: bytes, 32 bytes
57     """
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()