2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """Diffie-Hellman functions, VKO GOST R 34.10-2001/2012
20 from pygost.gost3410 import pub_marshal
21 from pygost.gost34112012256 import GOST34112012256
22 from pygost.gost34112012512 import GOST34112012512
23 from pygost.gost341194 import GOST341194
24 from pygost.utils import bytes2long
27 def vko_34102001(curve, prv, pubkey, ukm):
28 """ Make Diffie-Hellman computation (34.10-2001, 34.11-94)
30 :param GOST3410Curve curve: curve to use
31 :param long prv: private key
32 :param pubkey: public key
33 :type pubkey: (long, long)
34 :param ukm: UKM value (VKO-factor)
35 :type ukm: bytes, 8 bytes
36 :returns: Key Encryption Key (shared key)
37 :rtype: bytes, 32 bytes
39 Shared Key Encryption Key computation is based on
40 :rfc:`4357` VKO GOST R 34.10-2001 with little-endian
43 key = curve.exp(prv, pubkey[0], pubkey[1])
44 key = curve.exp(bytes2long(24 * b"\x00" + ukm), key[0], key[1])
45 return GOST341194(pub_marshal(key), "GostR3411_94_CryptoProParamSet").digest()
48 def vko_34102012256(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
49 """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 256 bit)
51 :param GOST3410Curve curve: curve to use
52 :param long prv: private key
53 :param pubkey: public key
54 :type pubkey: (long, long)
55 :param ukm: UKM value (VKO-factor)
56 :type ukm: bytes, 8 bytes
57 :returns: Key Encryption Key (shared key)
58 :rtype: bytes, 32 bytes
60 key = curve.exp(prv, pubkey[0], pubkey[1])
61 key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
62 return GOST34112012256(pub_marshal(key, mode=2012)).digest()
65 def vko_34102012512(curve, prv, pubkey, ukm=b"\x00\x00\x00\x00\x00\x00\x00\01"):
66 """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 512 bit)
68 :param GOST3410Curve curve: curve to use
69 :param long prv: private key
70 :param pubkey: public key
71 :type pubkey: (long, long)
72 :param ukm: UKM value (VKO-factor)
73 :type ukm: bytes, 8 bytes
74 :returns: Key Encryption Key (shared key)
75 :rtype: bytes, 32 bytes
77 key = curve.exp(prv, pubkey[0], pubkey[1])
78 key = curve.exp(bytes2long(ukm[::-1]), key[0], key[1])
79 return GOST34112012512(pub_marshal(key, mode=2012)).digest()