]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_gost3411_94.py
Consistent source code quote symbols
[pygost.git] / pygost / test_gost3411_94.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_94
22 from pygost.gost3411_94 import GOST341194
23
24
25 class TestCopy(TestCase):
26     def runTest(self):
27         m = GOST341194()
28         c = m.copy()
29         m.update(b"foobar")
30         c.update(b"foo")
31         c.update(b"bar")
32         self.assertEqual(m.digest(), c.digest())
33
34
35 class TestHMACPEP247(TestCase):
36     def runTest(self):
37         h = hmac.new(b"foo", digestmod=gost3411_94)
38         h.update(b"foobar")
39         h.digest()
40
41
42 class TestVectors(TestCase):
43     def test_empty(self):
44         self.assertEqual(
45             GOST341194(b"", "GostR3411_94_TestParamSet").hexdigest(),
46             "8d0f49492c91f45a68ff5c05d2c2b4ab78027b9aab5ce3feff5267c49cb985ce",
47         )
48
49     def test_a(self):
50         self.assertEqual(
51             GOST341194(b"a", "GostR3411_94_TestParamSet").hexdigest(),
52             "dd14f362cefd49f873a5c644431b87219c3449661f808ac8e9667c369e532cd4",
53         )
54
55     def test_abc(self):
56         self.assertEqual(
57             GOST341194(b"abc", "GostR3411_94_TestParamSet").hexdigest(),
58             "1dd5a4067c49703b75bc75c9290f5ecbb5eb85229e7277a2b2b14fc4484313f3",
59         )
60
61     def test_message_digest(self):
62         self.assertEqual(
63             GOST341194(b"message digest", "GostR3411_94_TestParamSet").hexdigest(),
64             "4d9a88a416de2fdb72de483f27652b5869243dec59be0cb6992c8fb1ec3444ad",
65         )
66
67     def test_Us(self):
68         self.assertEqual(
69             GOST341194(128 * b"U", "GostR3411_94_TestParamSet").hexdigest(),
70             "a43357fee8a926d9522a06870a66251c553e2774a0851d0cef0c1825eda3a353",
71         )
72
73     def test_dog(self):
74         self.assertEqual(
75             GOST341194(
76                 b"The quick brown fox jumps over the lazy dog",
77                 "GostR3411_94_TestParamSet",
78             ).hexdigest(),
79             "94421f6d370fa1d16ba7ac5e31296529c968047dca9bf4258ac59a0c41fab777",
80         )
81
82     def test_cog(self):
83         self.assertEqual(
84             GOST341194(
85                 b"The quick brown fox jumps over the lazy cog",
86                 "GostR3411_94_TestParamSet",
87             ).hexdigest(),
88             "45c4ee4ee1d25091312135540d6702e6677f7a73b5da31e10b8bb7aadac4eba3",
89         )
90
91     def test_rfc32(self):
92         self.assertEqual(
93             GOST341194(
94                 b"This is message, length=32 bytes",
95                 "GostR3411_94_TestParamSet",
96             ).hexdigest(),
97             "faff37a615a816691cff3ef8b68ca247e09525f39f8119832eb81975d366c4b1",
98         )
99
100     def test_rfc50(self):
101         self.assertEqual(
102             GOST341194(
103                 b"Suppose the original message has length = 50 bytes",
104                 "GostR3411_94_TestParamSet",
105             ).hexdigest(),
106             "0852f5623b89dd57aeb4781fe54df14eeafbc1350613763a0d770aa657ba1a47",
107         )
108
109
110 class TestVectorsCryptoPro(TestCase):
111     """ CryptoPro S-box test vectors
112     """
113     def test_empty(self):
114         self.assertEqual(
115             GOST341194(b"", "GostR3411_94_CryptoProParamSet").hexdigest(),
116             "c056d64c2383c44a58139c9b560111ac133e43fb840f838714840ca33c5f1e98",
117         )
118
119     def test_a(self):
120         self.assertEqual(
121             GOST341194(b"a", "GostR3411_94_CryptoProParamSet").hexdigest(),
122             "1130402fcfaaf1ef3c13e3173f105a715580f7c97900af37bf832128dd524ce7",
123         )
124
125     def test_abc(self):
126         self.assertEqual(
127             GOST341194(b"abc", "GostR3411_94_CryptoProParamSet").hexdigest(),
128             "2cd42ff986293b167e994381ed59747414dd24953677762d39d718bf6d0585b2",
129         )
130
131     def test_message_digest(self):
132         self.assertEqual(
133             GOST341194(
134                 b"message digest",
135                 "GostR3411_94_CryptoProParamSet",
136             ).hexdigest(),
137             "a01b72299bc39a540fd672a99a72b4bdfe74417386986efaeb01a42add4160bc",
138         )
139
140     def test_dog(self):
141         self.assertEqual(
142             GOST341194(
143                 b"The quick brown fox jumps over the lazy dog",
144                 "GostR3411_94_CryptoProParamSet",
145             ).hexdigest(),
146             "760a8365d570476e787254761be7656774021b1f3de56f588c501a364a290490",
147         )
148
149     def test_32(self):
150         self.assertEqual(
151             GOST341194(
152                 b"This is message, length=32 bytes",
153                 "GostR3411_94_CryptoProParamSet",
154             ).hexdigest(),
155             "eb48de3e89e71bcb695fc752d617fae757f34fa77fa58ee114c5bdb7f7c2ef2c",
156         )
157
158     def test_50(self):
159         self.assertEqual(
160             GOST341194(
161                 b"Suppose the original message has length = 50 bytes",
162                 "GostR3411_94_CryptoProParamSet",
163             ).hexdigest(),
164             "1150a63031dc611a5f5e40d93153f74ebde8216f6792c25a91cfcabc5c0c73c3",
165         )
166
167     def test_Us(self):
168         self.assertEqual(
169             GOST341194(128 * b"U", "GostR3411_94_CryptoProParamSet").hexdigest(),
170             "e8c449f608104c512710cd37fded920df1e86b211623fa27f4bb914661c74a1c",
171         )