2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 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 """Key agreement 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 ukm_unmarshal(ukm):
28 """Unmarshal UKM value
33 return bytes2long(ukm[::-1])
36 def kek(curve, prv, pub, ukm, mode):
37 key = curve.exp(prv, pub[0], pub[1])
38 key = curve.exp(ukm, key[0], key[1])
39 return pub_marshal(key, mode)
42 def kek_34102001(curve, prv, pub, ukm):
43 """ Key agreement (34.10-2001, 34.11-94)
45 :param GOST3410Curve curve: curve to use
46 :param long prv: private key
47 :param pub: public key
48 :type pub: (long, long)
49 :param long ukm: user keying material, VKO-factor
50 :returns: Key Encryption Key (shared key)
51 :rtype: bytes, 32 bytes
53 Shared Key Encryption Key computation is based on
54 :rfc:`4357` VKO GOST R 34.10-2001 with little-endian
58 kek(curve, prv, pub, ukm, mode=2001),
59 "id-GostR3411-94-CryptoProParamSet",
63 def kek_34102012256(curve, prv, pub, ukm=1, mode=2012):
64 """ Key agreement (34.10-2012, 34.11-2012 256 bit)
66 :param GOST3410Curve curve: curve to use
67 :param long prv: private key
68 :param pub: public key
69 :type pub: (long, long)
70 :param long ukm: user keying material, VKO-factor
71 :returns: Key Encryption Key (shared key)
72 :rtype: bytes, 32 bytes
74 Shared Key Encryption Key computation is based on
75 :rfc:`7836` VKO GOST R 34.10-2012.
77 return GOST34112012256(kek(curve, prv, pub, ukm, mode=mode)).digest()
80 def kek_34102012512(curve, prv, pub, ukm=1):
81 """ Key agreement (34.10-2012, 34.11-2012 512 bit)
83 :param GOST3410Curve curve: curve to use
84 :param long prv: private key
85 :param pub: public key
86 :type pub: (long, long)
87 :param long ukm: user keying material, VKO-factor
88 :returns: Key Encryption Key (shared key)
89 :rtype: bytes, 32 bytes
91 Shared Key Encryption Key computation is based on
92 :rfc:`7836` VKO GOST R 34.10-2012.
94 return GOST34112012512(kek(curve, prv, pub, ukm, mode=2012)).digest()