]> Cypherpunks.ru repositories - pygost.git/blob - pygost/gost3410_vko.py
7b7e10d42a608fa778f2a5dc118e45a6ee533cd8
[pygost.git] / pygost / gost3410_vko.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2018 Sergey Matveev <stargrave@stargrave.org>
4 #
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.
9 #
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.
14 #
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
18 """
19
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
25
26
27 def ukm_unmarshal(ukm):
28     """Unmarshal UKM value
29
30     :type ukm: bytes
31     :rtype: long
32     """
33     return bytes2long(ukm[::-1])
34
35
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)
40
41
42 def kek_34102001(curve, prv, pub, ukm):
43     """ Key agreement (34.10-2001, 34.11-94)
44
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
52
53     Shared Key Encryption Key computation is based on
54     :rfc:`4357` VKO GOST R 34.10-2001 with little-endian
55     hash output.
56     """
57     return GOST341194(
58         kek(curve, prv, pub, ukm, mode=2001),
59         "GostR3411_94_CryptoProParamSet",
60     ).digest()
61
62
63 def kek_34102012256(curve, prv, pub, ukm=1, mode=2012):
64     """ Key agreement (34.10-2012, 34.11-2012 256 bit)
65
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
73
74     Shared Key Encryption Key computation is based on
75     :rfc:`7836` VKO GOST R 34.10-2012.
76     """
77     return GOST34112012256(kek(curve, prv, pub, ukm, mode=mode)).digest()
78
79
80 def kek_34102012512(curve, prv, pub, ukm=1):
81     """ Key agreement (34.10-2012, 34.11-2012 512 bit)
82
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
90
91     Shared Key Encryption Key computation is based on
92     :rfc:`7836` VKO GOST R 34.10-2012.
93     """
94     return GOST34112012512(kek(curve, prv, pub, ukm, mode=2012)).digest()