]> Cypherpunks.ru repositories - pygost.git/blob - pygost/gost3410_vko.py
UKM is actually just a number
[pygost.git] / pygost / gost3410_vko.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2016 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 """Diffie-Hellman 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 vko_34102001(curve, prv, pubkey, ukm):
37     """ Make Diffie-Hellman computation (34.10-2001, 34.11-94)
38
39     :param GOST3410Curve curve: curve to use
40     :param long prv: private key
41     :param pubkey: public key
42     :type pubkey: (long, long)
43     :param long ukm: user keying material, VKO-factor
44     :returns: Key Encryption Key (shared key)
45     :rtype: bytes, 32 bytes
46
47     Shared Key Encryption Key computation is based on
48     :rfc:`4357` VKO GOST R 34.10-2001 with little-endian
49     hash output.
50     """
51     key = curve.exp(prv, pubkey[0], pubkey[1])
52     key = curve.exp(ukm, key[0], key[1])
53     return GOST341194(pub_marshal(key), "GostR3411_94_CryptoProParamSet").digest()
54
55
56 def vko_34102012256(curve, prv, pubkey, ukm=1):
57     """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 256 bit)
58
59     :param GOST3410Curve curve: curve to use
60     :param long prv: private key
61     :param pubkey: public key
62     :type pubkey: (long, long)
63     :param long ukm: user keying material, VKO-factor
64     :returns: Key Encryption Key (shared key)
65     :rtype: bytes, 32 bytes
66     """
67     key = curve.exp(prv, pubkey[0], pubkey[1])
68     key = curve.exp(ukm, key[0], key[1])
69     return GOST34112012256(pub_marshal(key, mode=2012)).digest()
70
71
72 def vko_34102012512(curve, prv, pubkey, ukm=1):
73     """ Make Diffie-Hellman computation (34.10-2012, 34.11-2012 512 bit)
74
75     :param GOST3410Curve curve: curve to use
76     :param long prv: private key
77     :param pubkey: public key
78     :type pubkey: (long, long)
79     :param long ukm: user keying material, VKO-factor
80     :returns: Key Encryption Key (shared key)
81     :rtype: bytes, 32 bytes
82     """
83     key = curve.exp(prv, pubkey[0], pubkey[1])
84     key = curve.exp(ukm, key[0], key[1])
85     return GOST34112012512(pub_marshal(key, mode=2012)).digest()