]> 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-2021 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, version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from unittest import TestCase
18
19 from pygost.gost3412 import C
20 from pygost.gost3412 import GOST3412Kuznechik
21 from pygost.gost3412 import GOST3412Magma
22 from pygost.gost3412 import L
23 from pygost.gost3412 import PI
24 from pygost.utils import hexdec
25
26
27 def S(blk):
28     return bytearray(PI[v] for v in blk)
29
30
31 def R(blk):
32     return L(blk, rounds=1)
33
34
35 class STest(TestCase):
36     def test_vec1(self):
37         blk = bytearray(hexdec("ffeeddccbbaa99881122334455667700"))
38         self.assertSequenceEqual(S(blk), hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
39
40     def test_vec2(self):
41         blk = bytearray(hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
42         self.assertSequenceEqual(S(blk), hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
43
44     def test_vec3(self):
45         blk = bytearray(hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
46         self.assertSequenceEqual(S(blk), hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
47
48     def test_vec4(self):
49         blk = bytearray(hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
50         self.assertSequenceEqual(S(blk), hexdec("23ae65633f842d29c5df529c13f5acda"))
51
52
53 class RTest(TestCase):
54     def test_vec1(self):
55         blk = bytearray(hexdec("00000000000000000000000000000100"))
56         self.assertSequenceEqual(R(blk), hexdec("94000000000000000000000000000001"))
57
58     def test_vec2(self):
59         blk = bytearray(hexdec("94000000000000000000000000000001"))
60         self.assertSequenceEqual(R(blk), hexdec("a5940000000000000000000000000000"))
61
62     def test_vec3(self):
63         blk = bytearray(hexdec("a5940000000000000000000000000000"))
64         self.assertSequenceEqual(R(blk), hexdec("64a59400000000000000000000000000"))
65
66     def test_vec4(self):
67         blk = bytearray(hexdec("64a59400000000000000000000000000"))
68         self.assertSequenceEqual(R(blk), hexdec("0d64a594000000000000000000000000"))
69
70
71 class LTest(TestCase):
72     def test_vec1(self):
73         blk = bytearray(hexdec("64a59400000000000000000000000000"))
74         self.assertSequenceEqual(L(blk), hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
75
76     def test_vec2(self):
77         blk = bytearray(hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
78         self.assertSequenceEqual(L(blk), hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
79
80     def test_vec3(self):
81         blk = bytearray(hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
82         self.assertSequenceEqual(L(blk), hexdec("0e93691a0cfc60408b7b68f66b513c13"))
83
84     def test_vec4(self):
85         blk = bytearray(hexdec("0e93691a0cfc60408b7b68f66b513c13"))
86         self.assertSequenceEqual(L(blk), hexdec("e6a8094fee0aa204fd97bcb0b44b8580"))
87
88
89 class KuznechikTest(TestCase):
90     key = hexdec("8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")
91     plaintext = hexdec("1122334455667700ffeeddccbbaa9988")
92     ciphertext = hexdec("7f679d90bebc24305a468d42b9d4edcd")
93
94     def test_c(self):
95         self.assertSequenceEqual(C[0], hexdec("6ea276726c487ab85d27bd10dd849401"))
96         self.assertSequenceEqual(C[1], hexdec("dc87ece4d890f4b3ba4eb92079cbeb02"))
97         self.assertSequenceEqual(C[2], hexdec("b2259a96b4d88e0be7690430a44f7f03"))
98         self.assertSequenceEqual(C[3], hexdec("7bcd1b0b73e32ba5b79cb140f2551504"))
99         self.assertSequenceEqual(C[4], hexdec("156f6d791fab511deabb0c502fd18105"))
100         self.assertSequenceEqual(C[5], hexdec("a74af7efab73df160dd208608b9efe06"))
101         self.assertSequenceEqual(C[6], hexdec("c9e8819dc73ba5ae50f5b570561a6a07"))
102         self.assertSequenceEqual(C[7], hexdec("f6593616e6055689adfba18027aa2a08"))
103
104     def test_roundkeys(self):
105         ciph = GOST3412Kuznechik(self.key)
106         self.assertSequenceEqual(ciph.ks[0], hexdec("8899aabbccddeeff0011223344556677"))
107         self.assertSequenceEqual(ciph.ks[1], hexdec("fedcba98765432100123456789abcdef"))
108         self.assertSequenceEqual(ciph.ks[2], hexdec("db31485315694343228d6aef8cc78c44"))
109         self.assertSequenceEqual(ciph.ks[3], hexdec("3d4553d8e9cfec6815ebadc40a9ffd04"))
110         self.assertSequenceEqual(ciph.ks[4], hexdec("57646468c44a5e28d3e59246f429f1ac"))
111         self.assertSequenceEqual(ciph.ks[5], hexdec("bd079435165c6432b532e82834da581b"))
112         self.assertSequenceEqual(ciph.ks[6], hexdec("51e640757e8745de705727265a0098b1"))
113         self.assertSequenceEqual(ciph.ks[7], hexdec("5a7925017b9fdd3ed72a91a22286f984"))
114         self.assertSequenceEqual(ciph.ks[8], hexdec("bb44e25378c73123a5f32f73cdb6e517"))
115         self.assertSequenceEqual(ciph.ks[9], hexdec("72e9dd7416bcf45b755dbaa88e4a4043"))
116
117     def test_encrypt(self):
118         ciph = GOST3412Kuznechik(self.key)
119         self.assertSequenceEqual(ciph.encrypt(self.plaintext), self.ciphertext)
120
121     def test_decrypt(self):
122         ciph = GOST3412Kuznechik(self.key)
123         self.assertSequenceEqual(ciph.decrypt(self.ciphertext), self.plaintext)
124
125
126 class MagmaTest(TestCase):
127     key = hexdec("ffeeddccbbaa99887766554433221100f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
128     plaintext = hexdec("fedcba9876543210")
129     ciphertext = hexdec("4ee901e5c2d8ca3d")
130
131     def test_encrypt(self):
132         ciph = GOST3412Magma(self.key)
133         self.assertSequenceEqual(ciph.encrypt(self.plaintext), self.ciphertext)
134
135     def test_decrypt(self):
136         ciph = GOST3412Magma(self.key)
137         self.assertSequenceEqual(ciph.decrypt(self.ciphertext), self.plaintext)