]> Cypherpunks.ru repositories - pygost.git/blobdiff - pygost/gost341194.py
Unify docstring's leading space presence
[pygost.git] / pygost / gost341194.py
index adc0925f6e53e32cbbb909dfc978f9b08dd87530..ba20a6eb2c04fff687a59331c199ed7dd5a2a002 100644 (file)
@@ -1,11 +1,10 @@
 # coding: utf-8
 # PyGOST -- Pure Python GOST cryptographic functions library
 # coding: utf-8
 # PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
+# the Free Software Foundation, version 3 of the License.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 # 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-94 hash function
+"""GOST R 34.11-94 hash function
 
 This is implementation of :rfc:`5831`. Most function and variable names are
 taken according to specification's terminology.
 """
 
 from copy import copy
 
 This is implementation of :rfc:`5831`. Most function and variable names are
 taken according to specification's terminology.
 """
 
 from copy import copy
+from functools import partial
 from struct import pack
 
 from struct import pack
 
-from pygost.gost28147 import addmod
 from pygost.gost28147 import block2ns
 from pygost.gost28147 import encrypt
 from pygost.gost28147 import ns2block
 from pygost.gost28147 import validate_sbox
 from pygost.iface import PEP247
 from pygost.gost28147 import block2ns
 from pygost.gost28147 import encrypt
 from pygost.gost28147 import ns2block
 from pygost.gost28147 import validate_sbox
 from pygost.iface import PEP247
+from pygost.pbkdf2 import pbkdf2 as pbkdf2_base
 from pygost.utils import hexdec
 from pygost.utils import hexenc
 from pygost.utils import strxor
 from pygost.utils import xrange
 
 
 from pygost.utils import hexdec
 from pygost.utils import hexenc
 from pygost.utils import strxor
 from pygost.utils import xrange
 
 
-DEFAULT_SBOX = "GostR3411_94_TestParamSet"
+DEFAULT_SBOX = "id-GostR3411-94-CryptoProParamSet"
 BLOCKSIZE = 32
 C2 = 32 * b"\x00"
 C3 = hexdec(b"ff00ffff000000ffff0000ff00ffff0000ff00ff00ff00ffff00ff00ff00ff00")
 BLOCKSIZE = 32
 C2 = 32 * b"\x00"
 C3 = hexdec(b"ff00ffff000000ffff0000ff00ffff0000ff00ff00ff00ffff00ff00ff00ff00")
@@ -58,7 +58,7 @@ def P(x):
 
 
 def _chi(Y):
 
 
 def _chi(Y):
-    """ Chi function
+    """Chi function
 
     This is some kind of LFSR.
     """
 
     This is some kind of LFSR.
     """
@@ -79,7 +79,7 @@ def _chi(Y):
 
 
 def _step(hin, m, sbox):
 
 
 def _step(hin, m, sbox):
-    """ Step function
+    """Step function
 
     H_out = f(H_in, m)
     """
 
     H_out = f(H_in, m)
     """
@@ -126,7 +126,7 @@ def _step(hin, m, sbox):
 
 
 class GOST341194(PEP247):
 
 
 class GOST341194(PEP247):
-    """ GOST 34.11-94 big-endian hash
+    """GOST 34.11-94 big-endian hash
 
     >>> m = GOST341194()
     >>> m.update("foo")
 
     >>> m = GOST341194()
     >>> m.update("foo")
@@ -152,25 +152,25 @@ class GOST341194(PEP247):
         return GOST341194(copy(self.data), self.sbox)
 
     def update(self, data):
         return GOST341194(copy(self.data), self.sbox)
 
     def update(self, data):
-        """ Append data that has to be hashed
+        """Append data that has to be hashed
         """
         self.data += data
 
     def digest(self):
         """
         self.data += data
 
     def digest(self):
-        """ Get hash of the provided data
+        """Get hash of the provided data
         """
         """
-        l = 0
+        _len = 0
         checksum = 0
         h = 32 * b"\x00"
         m = self.data
         for i in xrange(0, len(m), BLOCKSIZE):
             part = m[i:i + BLOCKSIZE][::-1]
         checksum = 0
         h = 32 * b"\x00"
         m = self.data
         for i in xrange(0, len(m), BLOCKSIZE):
             part = m[i:i + BLOCKSIZE][::-1]
-            l += len(part) * 8
-            checksum = addmod(checksum, int(hexenc(part), 16), 2 ** 256)
+            _len += len(part) * 8
+            checksum = (checksum + int(hexenc(part), 16)) % (2 ** 256)
             if len(part) < BLOCKSIZE:
                 part = b"\x00" * (BLOCKSIZE - len(part)) + part
             h = _step(h, part, self.sbox)
             if len(part) < BLOCKSIZE:
                 part = b"\x00" * (BLOCKSIZE - len(part)) + part
             h = _step(h, part, self.sbox)
-        h = _step(h, 24 * b"\x00" + pack(">Q", l), self.sbox)
+        h = _step(h, 24 * b"\x00" + pack(">Q", _len), self.sbox)
 
         checksum = hex(checksum)[2:].rstrip("L")
         if len(checksum) % 2 != 0:
 
         checksum = hex(checksum)[2:].rstrip("L")
         if len(checksum) % 2 != 0:
@@ -183,3 +183,10 @@ class GOST341194(PEP247):
 
 def new(data=b"", sbox=DEFAULT_SBOX):
     return GOST341194(data, sbox)
 
 def new(data=b"", sbox=DEFAULT_SBOX):
     return GOST341194(data, sbox)
+
+
+PBKDF2_HASHER = partial(GOST341194, sbox="id-GostR3411-94-CryptoProParamSet")
+
+
+def pbkdf2(password, salt, iterations, dklen):
+    return pbkdf2_base(PBKDF2_HASHER, password, salt, iterations, dklen)