]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_gost341194.py
Unify docstring's leading space presence
[pygost.git] / pygost / test_gost341194.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2020 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 skip
18 from unittest import TestCase
19 import hmac
20
21 from pygost import gost341194
22 from pygost.gost341194 import GOST341194
23 from pygost.gost341194 import pbkdf2
24 from pygost.utils import hexenc
25
26
27 class TestCopy(TestCase):
28     def runTest(self):
29         m = GOST341194()
30         c = m.copy()
31         m.update(b"foobar")
32         c.update(b"foo")
33         c.update(b"bar")
34         self.assertSequenceEqual(m.digest(), c.digest())
35
36
37 class TestHMACPEP247(TestCase):
38     def runTest(self):
39         h = hmac.new(b"foo", digestmod=gost341194)
40         h.update(b"foobar")
41         h.digest()
42
43
44 class TestVectors(TestCase):
45     def test_empty(self):
46         self.assertSequenceEqual(
47             GOST341194(b"", "id-GostR3411-94-TestParamSet").hexdigest(),
48             "ce85b99cc46752fffee35cab9a7b0278abb4c2d2055cff685af4912c49490f8d",
49         )
50
51     def test_a(self):
52         self.assertSequenceEqual(
53             GOST341194(b"a", "id-GostR3411-94-TestParamSet").hexdigest(),
54             "d42c539e367c66e9c88a801f6649349c21871b4344c6a573f849fdce62f314dd",
55         )
56
57     def test_abc(self):
58         self.assertSequenceEqual(
59             GOST341194(b"abc", "id-GostR3411-94-TestParamSet").hexdigest(),
60             "f3134348c44fb1b2a277729e2285ebb5cb5e0f29c975bc753b70497c06a4d51d",
61         )
62
63     def test_message_digest(self):
64         self.assertSequenceEqual(
65             GOST341194(b"message digest", "id-GostR3411-94-TestParamSet").hexdigest(),
66             "ad4434ecb18f2c99b60cbe59ec3d2469582b65273f48de72db2fde16a4889a4d",
67         )
68
69     def test_Us(self):
70         self.assertSequenceEqual(
71             GOST341194(128 * b"U", "id-GostR3411-94-TestParamSet").hexdigest(),
72             "53a3a3ed25180cef0c1d85a074273e551c25660a87062a52d926a9e8fe5733a4",
73         )
74
75     def test_dog(self):
76         self.assertSequenceEqual(
77             GOST341194(b"The quick brown fox jumps over the lazy dog", "id-GostR3411-94-TestParamSet",).hexdigest(),
78             "77b7fa410c9ac58a25f49bca7d0468c9296529315eaca76bd1a10f376d1f4294",
79         )
80
81     def test_cog(self):
82         self.assertSequenceEqual(
83             GOST341194(b"The quick brown fox jumps over the lazy cog", "id-GostR3411-94-TestParamSet",).hexdigest(),
84             "a3ebc4daaab78b0be131dab5737a7f67e602670d543521319150d2e14eeec445",
85         )
86
87     def test_rfc32(self):
88         self.assertSequenceEqual(
89             GOST341194(b"This is message, length=32 bytes", "id-GostR3411-94-TestParamSet",).hexdigest(),
90             "b1c466d37519b82e8319819ff32595e047a28cb6f83eff1c6916a815a637fffa",
91         )
92
93     def test_rfc50(self):
94         self.assertSequenceEqual(
95             GOST341194(b"Suppose the original message has length = 50 bytes", "id-GostR3411-94-TestParamSet",).hexdigest(),
96             "471aba57a60a770d3a76130635c1fbea4ef14de51f78b4ae57dd893b62f55208",
97         )
98
99
100 class TestVectorsCryptoPro(TestCase):
101     """CryptoPro S-box test vectors
102     """
103     def test_empty(self):
104         self.assertSequenceEqual(
105             GOST341194(b"", "id-GostR3411-94-CryptoProParamSet").hexdigest(),
106             "981e5f3ca30c841487830f84fb433e13ac1101569b9c13584ac483234cd656c0",
107         )
108
109     def test_a(self):
110         self.assertSequenceEqual(
111             GOST341194(b"a", "id-GostR3411-94-CryptoProParamSet").hexdigest(),
112             "e74c52dd282183bf37af0079c9f78055715a103f17e3133ceff1aacf2f403011",
113         )
114
115     def test_abc(self):
116         self.assertSequenceEqual(
117             GOST341194(b"abc", "id-GostR3411-94-CryptoProParamSet").hexdigest(),
118             "b285056dbf18d7392d7677369524dd14747459ed8143997e163b2986f92fd42c",
119         )
120
121     def test_message_digest(self):
122         self.assertSequenceEqual(
123             GOST341194(b"message digest", "id-GostR3411-94-CryptoProParamSet",).hexdigest(),
124             "bc6041dd2aa401ebfa6e9886734174febdb4729aa972d60f549ac39b29721ba0",
125         )
126
127     def test_dog(self):
128         self.assertSequenceEqual(
129             GOST341194(b"The quick brown fox jumps over the lazy dog", "id-GostR3411-94-CryptoProParamSet",).hexdigest(),
130             "9004294a361a508c586fe53d1f1b02746765e71b765472786e4770d565830a76",
131         )
132
133     def test_32(self):
134         self.assertSequenceEqual(
135             GOST341194(b"This is message, length=32 bytes", "id-GostR3411-94-CryptoProParamSet",).hexdigest(),
136             "2cefc2f7b7bdc514e18ea57fa74ff357e7fa17d652c75f69cb1be7893ede48eb",
137         )
138
139     def test_50(self):
140         self.assertSequenceEqual(
141             GOST341194(b"Suppose the original message has length = 50 bytes", "id-GostR3411-94-CryptoProParamSet",).hexdigest(),
142             "c3730c5cbccacf915ac292676f21e8bd4ef75331d9405e5f1a61dc3130a65011",
143         )
144
145     def test_Us(self):
146         self.assertSequenceEqual(
147             GOST341194(128 * b"U", "id-GostR3411-94-CryptoProParamSet").hexdigest(),
148             "1c4ac7614691bbf427fa2316216be8f10d92edfd37cd1027514c1008f649c4e8",
149         )
150
151
152 class TestPBKDF2(TestCase):
153     """http://tc26.ru/methods/containers_v1/Addition_to_PKCS5_v1_0.pdf test vectors
154     """
155     def test_1(self):
156         self.assertSequenceEqual(
157             hexenc(pbkdf2(b"password", b"salt", 1, 32)),
158             "7314e7c04fb2e662c543674253f68bd0b73445d07f241bed872882da21662d58",
159         )
160
161     def test_2(self):
162         self.assertSequenceEqual(
163             hexenc(pbkdf2(b"password", b"salt", 2, 32)),
164             "990dfa2bd965639ba48b07b792775df79f2db34fef25f274378872fed7ed1bb3",
165         )
166
167     def test_3(self):
168         self.assertSequenceEqual(
169             hexenc(pbkdf2(b"password", b"salt", 4096, 32)),
170             "1f1829a94bdff5be10d0aeb36af498e7a97467f3b31116a5a7c1afff9deadafe",
171         )
172
173     @skip("it takes too long")
174     def test_4(self):
175         self.assertSequenceEqual(
176             hexenc(pbkdf2(b"password", b"salt", 16777216, 32)),
177             "a57ae5a6088396d120850c5c09de0a525100938a59b1b5c3f7810910d05fcd97",
178         )
179
180     def test_5(self):
181         self.assertSequenceEqual(
182             hexenc(pbkdf2(
183                 b"passwordPASSWORDpassword",
184                 b"saltSALTsaltSALTsaltSALTsaltSALTsalt",
185                 4096,
186                 40,
187             )),
188             "788358c69cb2dbe251a7bb17d5f4241f265a792a35becde8d56f326b49c85047b7638acb4764b1fd",
189         )
190
191     def test_6(self):
192         self.assertSequenceEqual(
193             hexenc(pbkdf2(
194                 b"pass\x00word",
195                 b"sa\x00lt",
196                 4096,
197                 20,
198             )),
199             "43e06c5590b08c0225242373127edf9c8e9c3291",
200         )