2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 from unittest import TestCase
20 from pygost.gost3412 import C
21 from pygost.gost3412 import GOST3412Kuz
22 from pygost.gost3412 import L
23 from pygost.gost3412 import PI
24 from pygost.utils import hexdec
28 return bytearray(PI[v] for v in blk)
32 return L(blk, rounds=1)
35 class STest(TestCase):
37 blk = bytearray(hexdec("ffeeddccbbaa99881122334455667700"))
38 self.assertEqual(S(blk), hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
41 blk = bytearray(hexdec("b66cd8887d38e8d77765aeea0c9a7efc"))
42 self.assertEqual(S(blk), hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
45 blk = bytearray(hexdec("559d8dd7bd06cbfe7e7b262523280d39"))
46 self.assertEqual(S(blk), hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
49 blk = bytearray(hexdec("0c3322fed531e4630d80ef5c5a81c50b"))
50 self.assertEqual(S(blk), hexdec("23ae65633f842d29c5df529c13f5acda"))
53 class RTest(TestCase):
55 blk = bytearray(hexdec("00000000000000000000000000000100"))
56 self.assertEqual(R(blk), hexdec("94000000000000000000000000000001"))
59 blk = bytearray(hexdec("94000000000000000000000000000001"))
60 self.assertEqual(R(blk), hexdec("a5940000000000000000000000000000"))
63 blk = bytearray(hexdec("a5940000000000000000000000000000"))
64 self.assertEqual(R(blk), hexdec("64a59400000000000000000000000000"))
67 blk = bytearray(hexdec("64a59400000000000000000000000000"))
68 self.assertEqual(R(blk), hexdec("0d64a594000000000000000000000000"))
71 class LTest(TestCase):
73 blk = bytearray(hexdec("64a59400000000000000000000000000"))
74 self.assertEqual(L(blk), hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
77 blk = bytearray(hexdec("d456584dd0e3e84cc3166e4b7fa2890d"))
78 self.assertEqual(L(blk), hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
81 blk = bytearray(hexdec("79d26221b87b584cd42fbc4ffea5de9a"))
82 self.assertEqual(L(blk), hexdec("0e93691a0cfc60408b7b68f66b513c13"))
85 blk = bytearray(hexdec("0e93691a0cfc60408b7b68f66b513c13"))
86 self.assertEqual(L(blk), hexdec("e6a8094fee0aa204fd97bcb0b44b8580"))
89 class KuznechikTest(TestCase):
90 key = hexdec("8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")
91 plaintext = hexdec("1122334455667700ffeeddccbbaa9988")
92 ciphertext = hexdec("7f679d90bebc24305a468d42b9d4edcd")
95 self.assertEqual(C[0], hexdec("6ea276726c487ab85d27bd10dd849401"))
96 self.assertEqual(C[1], hexdec("dc87ece4d890f4b3ba4eb92079cbeb02"))
97 self.assertEqual(C[2], hexdec("b2259a96b4d88e0be7690430a44f7f03"))
98 self.assertEqual(C[3], hexdec("7bcd1b0b73e32ba5b79cb140f2551504"))
99 self.assertEqual(C[4], hexdec("156f6d791fab511deabb0c502fd18105"))
100 self.assertEqual(C[5], hexdec("a74af7efab73df160dd208608b9efe06"))
101 self.assertEqual(C[6], hexdec("c9e8819dc73ba5ae50f5b570561a6a07"))
102 self.assertEqual(C[7], hexdec("f6593616e6055689adfba18027aa2a08"))
104 def test_roundkeys(self):
105 ciph = GOST3412Kuz(self.key)
106 self.assertEqual(ciph.ks[0], hexdec("8899aabbccddeeff0011223344556677"))
107 self.assertEqual(ciph.ks[1], hexdec("fedcba98765432100123456789abcdef"))
108 self.assertEqual(ciph.ks[2], hexdec("db31485315694343228d6aef8cc78c44"))
109 self.assertEqual(ciph.ks[3], hexdec("3d4553d8e9cfec6815ebadc40a9ffd04"))
110 self.assertEqual(ciph.ks[4], hexdec("57646468c44a5e28d3e59246f429f1ac"))
111 self.assertEqual(ciph.ks[5], hexdec("bd079435165c6432b532e82834da581b"))
112 self.assertEqual(ciph.ks[6], hexdec("51e640757e8745de705727265a0098b1"))
113 self.assertEqual(ciph.ks[7], hexdec("5a7925017b9fdd3ed72a91a22286f984"))
114 self.assertEqual(ciph.ks[8], hexdec("bb44e25378c73123a5f32f73cdb6e517"))
115 self.assertEqual(ciph.ks[9], hexdec("72e9dd7416bcf45b755dbaa88e4a4043"))
117 def test_encrypt(self):
118 ciph = GOST3412Kuz(self.key)
119 self.assertEqual(ciph.encrypt(self.plaintext), self.ciphertext)
121 def test_decrypt(self):
122 ciph = GOST3412Kuz(self.key)
123 self.assertEqual(ciph.decrypt(self.ciphertext), self.plaintext)