]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_gost3410_vko.py
VKO functions must be called kek()
[pygost.git] / pygost / test_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
18 from os import urandom
19 from unittest import TestCase
20
21 from pygost.gost3410 import CURVE_PARAMS
22 from pygost.gost3410 import GOST3410Curve
23 from pygost.gost3410 import prv_unmarshal
24 from pygost.gost3410 import pub_unmarshal
25 from pygost.gost3410 import public_key
26 from pygost.gost3410_vko import kek_34102001
27 from pygost.gost3410_vko import kek_34102012256
28 from pygost.gost3410_vko import kek_34102012512
29 from pygost.gost3410_vko import ukm_unmarshal
30 from pygost.utils import bytes2long
31 from pygost.utils import hexdec
32
33
34 class TestVKO34102001(TestCase):
35     def test_sequence(self):
36         curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"])
37         for _ in range(10):
38             ukm = ukm_unmarshal(urandom(8))
39             prv1 = bytes2long(urandom(32))
40             prv2 = bytes2long(urandom(32))
41             pub1 = public_key(curve, prv1)
42             pub2 = public_key(curve, prv2)
43             kek1 = kek_34102001(curve, prv1, pub2, ukm)
44             kek2 = kek_34102001(curve, prv2, pub1, ukm)
45             self.assertEqual(kek1, kek2)
46             kek1 = kek_34102001(curve, prv1, pub1, ukm)
47             kek2 = kek_34102001(curve, prv2, pub2, ukm)
48             self.assertNotEqual(kek1, kek2)
49
50
51 class TestVKO34102012256(TestCase):
52     """http://tc26.ru/methods/recommendation/%D0%A2%D0%9A26%D0%90%D0%9B%D0%93.pdf test vectors
53     """
54     def test_vector(self):
55         curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
56         ukm = ukm_unmarshal(hexdec("1d80603c8544c727"))
57         prvA = prv_unmarshal(hexdec("c990ecd972fce84ec4db022778f50fcac726f46708384b8d458304962d7147f8c2db41cef22c90b102f2968404f9b9be6d47c79692d81826b32b8daca43cb667"))
58         pubA = pub_unmarshal(hexdec("aab0eda4abff21208d18799fb9a8556654ba783070eba10cb9abb253ec56dcf5d3ccba6192e464e6e5bcb6dea137792f2431f6c897eb1b3c0cc14327b1adc0a7914613a3074e363aedb204d38d3563971bd8758e878c9db11403721b48002d38461f92472d40ea92f9958c0ffa4c93756401b97f89fdbe0b5e46e4a4631cdb5a"), mode=2012)
59         prvB = prv_unmarshal(hexdec("48c859f7b6f11585887cc05ec6ef1390cfea739b1a18c0d4662293ef63b79e3b8014070b44918590b4b996acfea4edfbbbcccc8c06edd8bf5bda92a51392d0db"))
60         pubB = pub_unmarshal(hexdec("192fe183b9713a077253c72c8735de2ea42a3dbc66ea317838b65fa32523cd5efca974eda7c863f4954d1147f1f2b25c395fce1c129175e876d132e94ed5a65104883b414c9b592ec4dc84826f07d0b6d9006dda176ce48c391e3f97d102e03bb598bf132a228a45f7201aba08fc524a2d77e43a362ab022ad4028f75bde3b79"), mode=2012)
61         vko = hexdec("c9a9a77320e2cc559ed72dce6f47e2192ccea95fa648670582c054c0ef36c221")
62         self.assertEqual(kek_34102012256(curve, prvA, pubB, ukm), vko)
63         self.assertEqual(kek_34102012256(curve, prvB, pubA, ukm), vko)
64
65     def test_sequence(self):
66         curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
67         for _ in range(10):
68             ukm = ukm_unmarshal(urandom(8))
69             prv1 = bytes2long(urandom(32))
70             prv2 = bytes2long(urandom(32))
71             pub1 = public_key(curve, prv1)
72             pub2 = public_key(curve, prv2)
73             kek1 = kek_34102012256(curve, prv1, pub2, ukm)
74             kek2 = kek_34102012256(curve, prv2, pub1, ukm)
75             self.assertEqual(kek1, kek2)
76             kek1 = kek_34102012256(curve, prv1, pub1, ukm)
77             kek2 = kek_34102012256(curve, prv2, pub2, ukm)
78             self.assertNotEqual(kek1, kek2)
79
80
81 class TestVKO34102012512(TestCase):
82     """http://tc26.ru/methods/recommendation/%D0%A2%D0%9A26%D0%90%D0%9B%D0%93.pdf test vectors
83     """
84     def test_vector(self):
85         curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
86         ukm = ukm_unmarshal(hexdec("1d80603c8544c727"))
87         prvA = prv_unmarshal(hexdec("c990ecd972fce84ec4db022778f50fcac726f46708384b8d458304962d7147f8c2db41cef22c90b102f2968404f9b9be6d47c79692d81826b32b8daca43cb667"))
88         pubA = pub_unmarshal(hexdec("aab0eda4abff21208d18799fb9a8556654ba783070eba10cb9abb253ec56dcf5d3ccba6192e464e6e5bcb6dea137792f2431f6c897eb1b3c0cc14327b1adc0a7914613a3074e363aedb204d38d3563971bd8758e878c9db11403721b48002d38461f92472d40ea92f9958c0ffa4c93756401b97f89fdbe0b5e46e4a4631cdb5a"), mode=2012)
89         prvB = prv_unmarshal(hexdec("48c859f7b6f11585887cc05ec6ef1390cfea739b1a18c0d4662293ef63b79e3b8014070b44918590b4b996acfea4edfbbbcccc8c06edd8bf5bda92a51392d0db"))
90         pubB = pub_unmarshal(hexdec("192fe183b9713a077253c72c8735de2ea42a3dbc66ea317838b65fa32523cd5efca974eda7c863f4954d1147f1f2b25c395fce1c129175e876d132e94ed5a65104883b414c9b592ec4dc84826f07d0b6d9006dda176ce48c391e3f97d102e03bb598bf132a228a45f7201aba08fc524a2d77e43a362ab022ad4028f75bde3b79"), mode=2012)
91         vko = hexdec("79f002a96940ce7bde3259a52e015297adaad84597a0d205b50e3e1719f97bfa7ee1d2661fa9979a5aa235b558a7e6d9f88f982dd63fc35a8ec0dd5e242d3bdf")
92         self.assertEqual(kek_34102012512(curve, prvA, pubB, ukm), vko)
93         self.assertEqual(kek_34102012512(curve, prvB, pubA, ukm), vko)
94
95     def test_sequence(self):
96         curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
97         for _ in range(10):
98             ukm = ukm_unmarshal(urandom(8))
99             prv1 = bytes2long(urandom(32))
100             prv2 = bytes2long(urandom(32))
101             pub1 = public_key(curve, prv1)
102             pub2 = public_key(curve, prv2)
103             kek1 = kek_34102012512(curve, prv1, pub2, ukm)
104             kek2 = kek_34102012512(curve, prv2, pub1, ukm)
105             self.assertEqual(kek1, kek2)
106             kek1 = kek_34102012512(curve, prv1, pub1, ukm)
107             kek2 = kek_34102012512(curve, prv2, pub2, ukm)
108             self.assertNotEqual(kek1, kek2)