]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_gost3411_2012.py
2.3 release is ready
[pygost.git] / pygost / test_gost3411_2012.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 unittest import TestCase
19 import hmac
20
21 from pygost import gost3411_2012
22 from pygost.gost3411_2012 import GOST34112012
23 from pygost.utils import hexdec
24
25
26 class TestCopy(TestCase):
27     def runTest(self):
28         m = GOST34112012()
29         c = m.copy()
30         m.update(b'foobar')
31         c.update(b'foo')
32         c.update(b'bar')
33         self.assertEqual(m.digest(), c.digest())
34
35
36 class TestHMACPEP247(TestCase):
37     def runTest(self):
38         h = hmac.new(b'foo', digestmod=gost3411_2012)
39         h.update(b'foobar')
40         h.digest()
41
42
43 class TestVectors(TestCase):
44     def test_m1(self):
45         m = hexdec("323130393837363534333231303938373635343332313039383736353433323130393837363534333231303938373635343332313039383736353433323130")[::-1]
46         self.assertEqual(
47             GOST34112012(m).digest(),
48             hexdec("486f64c1917879417fef082b3381a4e211c324f074654c38823a7b76f830ad00fa1fbae42b1285c0352f227524bc9ab16254288dd6863dccd5b9f54a1ad0541b")[::-1]
49         )
50         self.assertEqual(
51             GOST34112012(m, digest_size=32).digest(),
52             hexdec("00557be5e584fd52a449b16b0251d05d27f94ab76cbaa6da890b59d8ef1e159d")[::-1]
53         )
54
55     def test_m2(self):
56         m = hexdec("fbe2e5f0eee3c820fbeafaebef20fffbf0e1e0f0f520e0ed20e8ece0ebe5f0f2f120fff0eeec20f120faf2fee5e2202ce8f6f3ede220e8e6eee1e8f0f2d1202ce8f0f2e5e220e5d1")[::-1]
57         self.assertEqual(
58             GOST34112012(m).digest(),
59             hexdec("28fbc9bada033b1460642bdcddb90c3fb3e56c497ccd0f62b8a2ad4935e85f037613966de4ee00531ae60f3b5a47f8dae06915d5f2f194996fcabf2622e6881e")[::-1]
60         )
61         self.assertEqual(
62             GOST34112012(m, digest_size=32).digest(),
63             hexdec("508f7e553c06501d749a66fc28c6cac0b005746d97537fa85d9e40904efed29d")[::-1]
64         )
65
66
67 class TestTrivial(TestCase):
68     def not_failing(self):
69         GOST34112012(b'').digest()
70         GOST34112012(b'a').digest()
71         g = GOST34112012()
72         g = GOST34112012(g.digest_size * 'x')
73         g.digest()
74
75     def test_updates(self):
76         g = GOST34112012()
77         g.update(b'foo')
78         g.update(b'bar')
79         self.assertEqual(g.digest(), GOST34112012(b'foobar').digest())