]> Cypherpunks.ru repositories - pygost.git/commitdiff
Split 34.11-2012 on two separate 256/512 bit modules and HMAC test vectors
authorSergey Matveev <stargrave@stargrave.org>
Sat, 19 Nov 2016 08:59:10 +0000 (11:59 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 19 Nov 2016 11:50:05 +0000 (14:50 +0300)
pygost/gost3411_2012.py
pygost/gost3411_2012_256.py [new file with mode: 0644]
pygost/gost3411_2012_512.py [new file with mode: 0644]
pygost/test_gost3411_2012.py

index 5bda13500181beef1584f9121a75797e540f6500..cea2bb59242260fadd63bf0ae7bbd1849a1db40f 100644 (file)
@@ -14,7 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-""" GOST R 34.11-2012 (Streebog) hash function
+""" GOST R 34.11-2012 (Streebog) hash function common files
 
 This is implementation of :rfc:`6986`. Most function and variable names are
 taken according to specification's terminology.
@@ -274,7 +274,3 @@ class GOST34112012(PEP247):
 
     def hexdigest(self):
         return hexenc(self.digest())
-
-
-def new(data=b'', digest_size=64):
-    return GOST34112012(data, digest_size)
diff --git a/pygost/gost3411_2012_256.py b/pygost/gost3411_2012_256.py
new file mode 100644 (file)
index 0000000..8586be5
--- /dev/null
@@ -0,0 +1,16 @@
+""" GOST R 34.11-2012 (Streebog) 256-bit hash function
+
+This is implementation of :rfc:`6986`. Most function and variable names are
+taken according to specification's terminology.
+"""
+
+from gost3411_2012 import GOST34112012
+
+
+class GOST34112012256(GOST34112012):
+    def __init__(self, data=b''):
+        super(GOST34112012256, self).__init__(data, digest_size=32)
+
+
+def new(data=b''):
+    return GOST34112012256(data)
diff --git a/pygost/gost3411_2012_512.py b/pygost/gost3411_2012_512.py
new file mode 100644 (file)
index 0000000..802bbd9
--- /dev/null
@@ -0,0 +1,16 @@
+""" GOST R 34.11-2012 (Streebog) 512-bit hash function
+
+This is implementation of :rfc:`6986`. Most function and variable names are
+taken according to specification's terminology.
+"""
+
+from gost3411_2012 import GOST34112012
+
+
+class GOST34112012512(GOST34112012):
+    def __init__(self, data=b''):
+        super(GOST34112012512, self).__init__(data, digest_size=64)
+
+
+def new(data=b''):
+    return GOST34112012512(data)
index d44c11124ed0a7864c2eff7f84280c4c753c45be..95af7a9cd793283276459957b8a39941ef9b5576 100644 (file)
 from unittest import TestCase
 import hmac
 
-from pygost import gost3411_2012
-from pygost.gost3411_2012 import GOST34112012
+from pygost import gost3411_2012_256
+from pygost import gost3411_2012_512
+from pygost.gost3411_2012_256 import GOST34112012256
+from pygost.gost3411_2012_512 import GOST34112012512
 from pygost.utils import hexdec
 
 
 class TestCopy(TestCase):
     def runTest(self):
-        m = GOST34112012()
+        m = GOST34112012256()
         c = m.copy()
         m.update(b'foobar')
         c.update(b'foo')
@@ -33,47 +35,51 @@ class TestCopy(TestCase):
         self.assertEqual(m.digest(), c.digest())
 
 
-class TestHMACPEP247(TestCase):
-    def runTest(self):
-        h = hmac.new(b'foo', digestmod=gost3411_2012)
-        h.update(b'foobar')
-        h.digest()
+class TestHMAC(TestCase):
+    """http://tc26.ru/methods/recommendation/%D0%A2%D0%9A26%D0%90%D0%9B%D0%93.pdf test vectors
+    """
+    def test_256(self):
+        for digestmod in (GOST34112012256, gost3411_2012_256):
+            self.assertEqual(
+                hmac.new(
+                    key=hexdec("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"),
+                    msg=hexdec("0126bdb87800af214341456563780100"),
+                    digestmod=digestmod,
+                ).hexdigest(),
+                "a1aa5f7de402d7b3d323f2991c8d4534013137010a83754fd0af6d7cd4922ed9",
+            )
+
+    def test_512(self):
+        for digestmod in (GOST34112012512, gost3411_2012_512):
+            self.assertEqual(
+                hmac.new(
+                    key=hexdec("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"),
+                    msg=hexdec("0126bdb87800af214341456563780100"),
+                    digestmod=digestmod,
+                ).hexdigest(),
+                "a59bab22ecae19c65fbde6e5f4e9f5d8549d31f037f9df9b905500e171923a773d5f1530f2ed7e964cb2eedc29e9ad2f3afe93b2814f79f5000ffc0366c251e6",
+            )
 
 
 class TestVectors(TestCase):
     def test_m1(self):
         m = hexdec("323130393837363534333231303938373635343332313039383736353433323130393837363534333231303938373635343332313039383736353433323130")[::-1]
         self.assertEqual(
-            GOST34112012(m).digest(),
+            GOST34112012512(m).digest(),
             hexdec("486f64c1917879417fef082b3381a4e211c324f074654c38823a7b76f830ad00fa1fbae42b1285c0352f227524bc9ab16254288dd6863dccd5b9f54a1ad0541b")[::-1]
         )
         self.assertEqual(
-            GOST34112012(m, digest_size=32).digest(),
+            GOST34112012256(m).digest(),
             hexdec("00557be5e584fd52a449b16b0251d05d27f94ab76cbaa6da890b59d8ef1e159d")[::-1]
         )
 
     def test_m2(self):
         m = hexdec("fbe2e5f0eee3c820fbeafaebef20fffbf0e1e0f0f520e0ed20e8ece0ebe5f0f2f120fff0eeec20f120faf2fee5e2202ce8f6f3ede220e8e6eee1e8f0f2d1202ce8f0f2e5e220e5d1")[::-1]
         self.assertEqual(
-            GOST34112012(m).digest(),
+            GOST34112012512(m).digest(),
             hexdec("28fbc9bada033b1460642bdcddb90c3fb3e56c497ccd0f62b8a2ad4935e85f037613966de4ee00531ae60f3b5a47f8dae06915d5f2f194996fcabf2622e6881e")[::-1]
         )
         self.assertEqual(
-            GOST34112012(m, digest_size=32).digest(),
+            GOST34112012256(m).digest(),
             hexdec("508f7e553c06501d749a66fc28c6cac0b005746d97537fa85d9e40904efed29d")[::-1]
         )
-
-
-class TestTrivial(TestCase):
-    def not_failing(self):
-        GOST34112012(b'').digest()
-        GOST34112012(b'a').digest()
-        g = GOST34112012()
-        g = GOST34112012(g.digest_size * 'x')
-        g.digest()
-
-    def test_updates(self):
-        g = GOST34112012()
-        g.update(b'foo')
-        g.update(b'bar')
-        self.assertEqual(g.digest(), GOST34112012(b'foobar').digest())