X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Fgost341194.py;h=6d58efd666de769c9358ff66b2bf5f0175681d23;hb=d59175f7b4ae74bf32dee438fb64577140e0ac23;hp=aa3cf519827e95f4063ee56c20051607fa4812a2;hpb=dacca17a69ca9a920bf69bc1dc47386bdd6d4def;p=pygost.git diff --git a/pygost/gost341194.py b/pygost/gost341194.py index aa3cf51..6d58efd 100644 --- a/pygost/gost341194.py +++ b/pygost/gost341194.py @@ -1,11 +1,10 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2017 Sergey Matveev +# Copyright (C) 2015-2020 Sergey Matveev # # 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 @@ -21,6 +20,7 @@ taken according to specification's terminology. """ from copy import copy +from functools import partial from struct import pack from pygost.gost28147 import addmod @@ -29,15 +29,14 @@ from pygost.gost28147 import encrypt from pygost.gost28147 import ns2block from pygost.gost28147 import validate_sbox from pygost.iface import PEP247 -from pygost.utils import bytes2long +from pygost.pbkdf2 import pbkdf2 as pbkdf2_base from pygost.utils import hexdec from pygost.utils import hexenc -from pygost.utils import long2bytes from pygost.utils import strxor -from pygost.utils import xrange # pylint: disable=redefined-builtin +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") @@ -187,34 +186,8 @@ def new(data=b"", sbox=DEFAULT_SBOX): return GOST341194(data, sbox) -# This implementation is based on Python 3.5.2 source code's one. -# PyGOST does not register itself in hashlib anyway, so use it instead. -def pbkdf2(password, salt, iterations, dklen): - """PBKDF2 implementation for GOST R 34.11-94 +PBKDF2_HASHER = partial(GOST341194, sbox="id-GostR3411-94-CryptoProParamSet") - Based on http://tc26.ru/methods/containers_v1/Addition_to_PKCS5_v1_0.pdf - """ - inner = GOST341194(sbox="GostR3411_94_CryptoProParamSet") - outer = GOST341194(sbox="GostR3411_94_CryptoProParamSet") - password = password + b"\x00" * (inner.block_size - len(password)) - inner.update(strxor(password, len(password) * b"\x36")) - outer.update(strxor(password, len(password) * b"\x5C")) - - def prf(msg): - icpy = inner.copy() - ocpy = outer.copy() - icpy.update(msg) - ocpy.update(icpy.digest()) - return ocpy.digest() - - dkey = b'' - loop = 1 - while len(dkey) < dklen: - prev = prf(salt + long2bytes(loop, 4)) - rkey = bytes2long(prev) - for _ in xrange(iterations - 1): - prev = prf(prev) - rkey ^= bytes2long(prev) - loop += 1 - dkey += long2bytes(rkey, inner.digest_size) - return dkey[:dklen] + +def pbkdf2(password, salt, iterations, dklen): + return pbkdf2_base(PBKDF2_HASHER, password, salt, iterations, dklen)