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