]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_gost3412.py
Raise copyright years
[pygost.git] / pygost / test_gost3412.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 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 unittest import TestCase
19
20 from pygost.gost3412 import C
21 from pygost.gost3412 import GOST3412Kuznechik
22 from pygost.gost3412 import GOST3412Magma
23 from pygost.gost3412 import L
24 from pygost.gost3412 import PI
25 from pygost.utils import hexdec
26
27
28 def S(blk):
29     return bytearray(PI[v] for v in blk)
30
31
32 def R(blk):
33     return L(blk, rounds=1)
34
35
36 class STest(TestCase):
37     def test_vec1(self):
38         blk = bytearray(hexdec("ffeeddccbbaa99881122334455667700"))
39         self.assertEqual(S(blk), hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
40
41     def test_vec2(self):
42         blk = bytearray(hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
43         self.assertEqual(S(blk), hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
44
45     def test_vec3(self):
46         blk = bytearray(hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
47         self.assertEqual(S(blk), hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
48
49     def test_vec4(self):
50         blk = bytearray(hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
51         self.assertEqual(S(blk), hexdec("23ae65633f842d29c5df529c13f5acda"))
52
53
54 class RTest(TestCase):
55     def test_vec1(self):
56         blk = bytearray(hexdec("00000000000000000000000000000100"))
57         self.assertEqual(R(blk), hexdec("94000000000000000000000000000001"))
58
59     def test_vec2(self):
60         blk = bytearray(hexdec("94000000000000000000000000000001"))
61         self.assertEqual(R(blk), hexdec("a5940000000000000000000000000000"))
62
63     def test_vec3(self):
64         blk = bytearray(hexdec("a5940000000000000000000000000000"))
65         self.assertEqual(R(blk), hexdec("64a59400000000000000000000000000"))
66
67     def test_vec4(self):
68         blk = bytearray(hexdec("64a59400000000000000000000000000"))
69         self.assertEqual(R(blk), hexdec("0d64a594000000000000000000000000"))
70
71
72 class LTest(TestCase):
73     def test_vec1(self):
74         blk = bytearray(hexdec("64a59400000000000000000000000000"))
75         self.assertEqual(L(blk), hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
76
77     def test_vec2(self):
78         blk = bytearray(hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
79         self.assertEqual(L(blk), hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
80
81     def test_vec3(self):
82         blk = bytearray(hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
83         self.assertEqual(L(blk), hexdec("0e93691a0cfc60408b7b68f66b513c13"))
84
85     def test_vec4(self):
86         blk = bytearray(hexdec("0e93691a0cfc60408b7b68f66b513c13"))
87         self.assertEqual(L(blk), hexdec("e6a8094fee0aa204fd97bcb0b44b8580"))
88
89
90 class KuznechikTest(TestCase):
91     key = hexdec("8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")
92     plaintext = hexdec("1122334455667700ffeeddccbbaa9988")
93     ciphertext = hexdec("7f679d90bebc24305a468d42b9d4edcd")
94
95     def test_c(self):
96         self.assertEqual(C[0], hexdec("6ea276726c487ab85d27bd10dd849401"))
97         self.assertEqual(C[1], hexdec("dc87ece4d890f4b3ba4eb92079cbeb02"))
98         self.assertEqual(C[2], hexdec("b2259a96b4d88e0be7690430a44f7f03"))
99         self.assertEqual(C[3], hexdec("7bcd1b0b73e32ba5b79cb140f2551504"))
100         self.assertEqual(C[4], hexdec("156f6d791fab511deabb0c502fd18105"))
101         self.assertEqual(C[5], hexdec("a74af7efab73df160dd208608b9efe06"))
102         self.assertEqual(C[6], hexdec("c9e8819dc73ba5ae50f5b570561a6a07"))
103         self.assertEqual(C[7], hexdec("f6593616e6055689adfba18027aa2a08"))
104
105     def test_roundkeys(self):
106         ciph = GOST3412Kuznechik(self.key)
107         self.assertEqual(ciph.ks[0], hexdec("8899aabbccddeeff0011223344556677"))
108         self.assertEqual(ciph.ks[1], hexdec("fedcba98765432100123456789abcdef"))
109         self.assertEqual(ciph.ks[2], hexdec("db31485315694343228d6aef8cc78c44"))
110         self.assertEqual(ciph.ks[3], hexdec("3d4553d8e9cfec6815ebadc40a9ffd04"))
111         self.assertEqual(ciph.ks[4], hexdec("57646468c44a5e28d3e59246f429f1ac"))
112         self.assertEqual(ciph.ks[5], hexdec("bd079435165c6432b532e82834da581b"))
113         self.assertEqual(ciph.ks[6], hexdec("51e640757e8745de705727265a0098b1"))
114         self.assertEqual(ciph.ks[7], hexdec("5a7925017b9fdd3ed72a91a22286f984"))
115         self.assertEqual(ciph.ks[8], hexdec("bb44e25378c73123a5f32f73cdb6e517"))
116         self.assertEqual(ciph.ks[9], hexdec("72e9dd7416bcf45b755dbaa88e4a4043"))
117
118     def test_encrypt(self):
119         ciph = GOST3412Kuznechik(self.key)
120         self.assertEqual(ciph.encrypt(self.plaintext), self.ciphertext)
121
122     def test_decrypt(self):
123         ciph = GOST3412Kuznechik(self.key)
124         self.assertEqual(ciph.decrypt(self.ciphertext), self.plaintext)
125
126
127 class MagmaTest(TestCase):
128     key = hexdec("ffeeddccbbaa99887766554433221100f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
129     plaintext = hexdec("fedcba9876543210")
130     ciphertext = hexdec("4ee901e5c2d8ca3d")
131
132     def test_encrypt(self):
133         ciph = GOST3412Magma(self.key)
134         self.assertEqual(ciph.encrypt(self.plaintext), self.ciphertext)
135
136     def test_decrypt(self):
137         ciph = GOST3412Magma(self.key)
138         self.assertEqual(ciph.decrypt(self.ciphertext), self.plaintext)