pygost/__init__.py | 2 +- pygost/gost28147.py | 28 ++++++++++++++-------------- pygost/gost28147_mac.py | 8 ++++---- pygost/gost3410.py | 10 +++++----- pygost/gost3410_vko.py | 6 +++--- pygost/gost34112012.py | 10 +++++----- pygost/gost34112012256.py | 2 +- pygost/gost34112012512.py | 2 +- pygost/gost341194.py | 12 ++++++------ pygost/gost3413.py | 2 +- pygost/mgm.py | 2 +- pygost/pbkdf2.py | 2 +- pygost/test_gost28147.py | 16 ++++++++-------- pygost/test_gost28147_mac.py | 2 +- pygost/test_gost3410.py | 4 ++-- pygost/test_gost341194.py | 2 +- pygost/utils.py | 8 ++++---- diff --git a/pygost/__init__.py b/pygost/__init__.py index e27b618cf0cc1597152af25733f915fead6c418d41c2396adfc2fd5d61c30ab5..97e903f07e8dd237e260cb630c83450876b77106ecd084787fc35862cafb0069 100644 --- a/pygost/__init__.py +++ b/pygost/__init__.py @@ -1,4 +1,4 @@ -""" Pure Python GOST cryptographic functions library. +"""Pure Python GOST cryptographic functions library. PyGOST is free software: see the file COPYING for copying conditions. """ diff --git a/pygost/gost28147.py b/pygost/gost28147.py index 97bb0a270fdd0f02885411020cb08d5c5eed28b5107142d463e7f87fa7178de8..d6694ef2aef927aed798609a9e0d868f2ee16776c865c8ac49ad6ea074f3c5c5 100644 --- a/pygost/gost28147.py +++ b/pygost/gost28147.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST 28147-89 block cipher +"""GOST 28147-89 block cipher This is implementation of :rfc:`5830` ECB, CNT, CFB and :rfc:`4357` CBC modes of operation. N1, N2, K names are taken according to @@ -148,7 +148,7 @@ SBOXES["AppliedCryptography"] = SBOXES["id-GostR3411-94-TestParamSet"] def _K(s, _in): - """ S-box substitution + """S-box substitution :param s: S-box :param _in: 32-bit word @@ -167,7 +167,7 @@ ) def block2ns(data): - """ Convert block to N1 and N2 integers + """Convert block to N1 and N2 integers """ data = bytearray(data) return ( @@ -177,7 +177,7 @@ ) def ns2block(ns): - """ Convert N1 and N2 integers to 8-byte block + """Convert N1 and N2 integers to 8-byte block """ n1, n2 = ns return bytes(bytearray(( @@ -187,7 +187,7 @@ ))) def _shift11(x): - """ 11-bit cyclic shift + """11-bit cyclic shift """ return ((x << 11) & (2 ** 32 - 1)) | ((x >> (32 - 11)) & (2 ** 32 - 1)) @@ -208,7 +208,7 @@ raise ValueError("Unknown sbox supplied") def xcrypt(seq, sbox, key, ns): - """ Perform full-round single-block operation + """Perform full-round single-block operation :param seq: sequence of K_i S-box applying (either encrypt or decrypt) :param sbox: S-box parameters to use @@ -234,19 +234,19 @@ return n1, n2 def encrypt(sbox, key, ns): - """ Encrypt single block + """Encrypt single block """ return xcrypt(SEQ_ENCRYPT, sbox, key, ns) def decrypt(sbox, key, ns): - """ Decrypt single block + """Decrypt single block """ return xcrypt(SEQ_DECRYPT, sbox, key, ns) def ecb(key, data, action, sbox=DEFAULT_SBOX): - """ ECB mode of operation + """ECB mode of operation :param bytes key: encryption key :param data: plaintext @@ -274,7 +274,7 @@ ecb_decrypt = partial(ecb, action=decrypt) def cbc_encrypt(key, data, iv=8 * b"\x00", pad=True, sbox=DEFAULT_SBOX, mesh=False): - """ CBC encryption mode of operation + """CBC encryption mode of operation :param bytes key: encryption key :param bytes data: plaintext @@ -309,7 +309,7 @@ return b"".join(ciphertext) def cbc_decrypt(key, data, pad=True, sbox=DEFAULT_SBOX, mesh=False): - """ CBC decryption mode of operation + """CBC decryption mode of operation :param bytes key: encryption key :param bytes data: ciphertext @@ -345,7 +345,7 @@ return b"".join(plaintext) def cnt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX): - """ Counter mode of operation + """Counter mode of operation :param bytes key: encryption key :param bytes data: plaintext @@ -385,7 +385,7 @@ return key, iv def cfb_encrypt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX, mesh=False): - """ CFB encryption mode of operation + """CFB encryption mode of operation :param bytes key: encryption key :param bytes data: plaintext @@ -419,7 +419,7 @@ return b"".join(ciphertext[1:]) def cfb_decrypt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX, mesh=False): - """ CFB decryption mode of operation + """CFB decryption mode of operation :param bytes key: encryption key :param bytes data: plaintext diff --git a/pygost/gost28147_mac.py b/pygost/gost28147_mac.py index 30833efa9578ed67d565f1910e5019e9dcb49eb6a41adb6a56f9e3ea7b3058bf..ead3f2555164485264fec76738ea1c1aab28cf068a72f3c934ae6659890e7ad8 100644 --- a/pygost/gost28147_mac.py +++ b/pygost/gost28147_mac.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST 28147-89 MAC +"""GOST 28147-89 MAC """ from copy import copy @@ -39,7 +39,7 @@ ) class MAC(PEP247): - """ GOST 28147-89 MAC mode of operation + """GOST 28147-89 MAC mode of operation >>> m = MAC(key=key) >>> m.update("some data") @@ -70,12 +70,12 @@ def copy(self): return MAC(self.key, copy(self.data), self.iv, self.sbox) def update(self, data): - """ Append data that has to be authenticated + """Append data that has to be authenticated """ self.data += data def digest(self): - """ Get MAC tag of supplied data + """Get MAC tag of supplied data You have to provide at least single byte of data. If you want to produce tag length of 3 bytes, then diff --git a/pygost/gost3410.py b/pygost/gost3410.py index ede297bcdc9c849166bacbbdb5dfb6f268387c89ffa470d141167b3eff33fae8..555aef526d1f04a343f5ae61a58b7085391836b5ebbc09fc03e4c07c25cb3f45 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST R 34.10 public-key signature function. +"""GOST R 34.10 public-key signature function. This is implementation of GOST R 34.10-2001 (:rfc:`5832`), GOST R 34.10-2012 (:rfc:`7091`). The difference between 2001 and 2012 is the @@ -35,7 +35,7 @@ return (512 // 8) if point.bit_length() > 256 else (256 // 8) class GOST3410Curve(object): - """ GOST 34.10 validated curve + """GOST 34.10 validated curve >>> curve = CURVES["id-GostR3410-2001-TestParamSet"] >>> prv = prv_unmarshal(urandom(32)) @@ -219,7 +219,7 @@ DEFAULT_CURVE = CURVES["id-GostR3410-2001-CryptoPro-A-ParamSet"] def public_key(curve, prv): - """ Generate public key from the private one + """Generate public key from the private one :param GOST3410Curve curve: curve to use :param long prv: private key @@ -230,7 +230,7 @@ return curve.exp(prv) def sign(curve, prv, digest, rand=None): - """ Calculate signature for provided digest + """Calculate signature for provided digest :param GOST3410Curve curve: curve to use :param long prv: private key @@ -268,7 +268,7 @@ return long2bytes(s, size) + long2bytes(r, size) def verify(curve, pub, digest, signature): - """ Verify provided digest with the signature + """Verify provided digest with the signature :param GOST3410Curve curve: curve to use :type pub: (long, long) diff --git a/pygost/gost3410_vko.py b/pygost/gost3410_vko.py index 856940530fb911870677194d3c7f951749d9cbb7fcee1f266066d9b3b21c533f..ca51860761e389528e8331c1140c3b5fc942b5635f900143c8bbe8ad5a6b14b3 100644 --- a/pygost/gost3410_vko.py +++ b/pygost/gost3410_vko.py @@ -39,7 +39,7 @@ return pub_marshal(key) def kek_34102001(curve, prv, pub, ukm): - """ Key agreement (34.10-2001, 34.11-94) + """Key agreement (34.10-2001, 34.11-94) :param GOST3410Curve curve: curve to use :param long prv: private key @@ -60,7 +60,7 @@ ).digest() def kek_34102012256(curve, prv, pub, ukm=1): - """ Key agreement (34.10-2012, 34.11-2012 256 bit) + """Key agreement (34.10-2012, 34.11-2012 256 bit) :param GOST3410Curve curve: curve to use :param long prv: private key @@ -77,7 +77,7 @@ return GOST34112012256(kek(curve, prv, pub, ukm)).digest() def kek_34102012512(curve, prv, pub, ukm=1): - """ Key agreement (34.10-2012, 34.11-2012 512 bit) + """Key agreement (34.10-2012, 34.11-2012 512 bit) :param GOST3410Curve curve: curve to use :param long prv: private key diff --git a/pygost/gost34112012.py b/pygost/gost34112012.py index 8c8ecb8495d765121458ec34b958a49a4da8aa711263124a74427669769b5519..2b931efad603e09de67e7b8acbc534e30ea7eef63bb24efb8656c7c0918dcaac 100644 --- a/pygost/gost34112012.py +++ b/pygost/gost34112012.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST R 34.11-2012 (Streebog) hash function common files +"""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. @@ -164,7 +164,7 @@ )] def add512bit(a, b): - """ Add two 512 integers + """Add two 512 integers """ a = bytearray(a) b = bytearray(b) @@ -213,7 +213,7 @@ return b"".join(res) class GOST34112012(PEP247): - """ GOST 34.11-2012 big-endian hash + """GOST 34.11-2012 big-endian hash >>> m = GOST34112012(digest_size=32) >>> m.update("foo") @@ -239,12 +239,12 @@ def digest_size(self): return self._digest_size def update(self, data): - """ Append data that has to be hashed + """Append data that has to be hashed """ self.data += data def digest(self): - """ Get hash of the provided data + """Get hash of the provided data """ hsh = BLOCKSIZE * (b"\x01" if self.digest_size == 32 else b"\x00") chk = bytearray(BLOCKSIZE * b"\x00") diff --git a/pygost/gost34112012256.py b/pygost/gost34112012256.py index 35ac2c7207b3767bc26e490f61e348593bc2174f038be636db837c7573c332ca..13bfd11ddf759b45633ed0361a49f671b2338fc95b4b4ce903e0c9dc3703535b 100644 --- a/pygost/gost34112012256.py +++ b/pygost/gost34112012256.py @@ -1,4 +1,4 @@ -""" GOST R 34.11-2012 (Streebog) 256-bit hash function +"""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. diff --git a/pygost/gost34112012512.py b/pygost/gost34112012512.py index 2c263979996bc18874479266c4917bf516baaee715fbb6ea2d284df9519bf0e4..82fb2d005ae3eba61aab8bfa17902f90cd927d59491c1b48bc679f95295a081b 100644 --- a/pygost/gost34112012512.py +++ b/pygost/gost34112012512.py @@ -1,4 +1,4 @@ -""" GOST R 34.11-2012 (Streebog) 512-bit hash function +"""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. diff --git a/pygost/gost341194.py b/pygost/gost341194.py index 2cb7ba799c427c9b9ce2ed9b08ad0a489911ca5b0533a53d87ab1226a654bb79..4d8eb3587edd9559468edcae6a71addd9e3d9455b8a508dc63131063388839f4 100644 --- a/pygost/gost341194.py +++ b/pygost/gost341194.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" 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. @@ -58,7 +58,7 @@ )) def _chi(Y): - """ Chi function + """Chi function This is some kind of LFSR. """ @@ -79,7 +79,7 @@ )) def _step(hin, m, sbox): - """ Step function + """Step function H_out = f(H_in, m) """ @@ -126,7 +126,7 @@ return x class GOST341194(PEP247): - """ GOST 34.11-94 big-endian hash + """GOST 34.11-94 big-endian hash >>> m = GOST341194() >>> m.update("foo") @@ -152,12 +152,12 @@ def copy(self): 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): - """ Get hash of the provided data + """Get hash of the provided data """ _len = 0 checksum = 0 diff --git a/pygost/gost3413.py b/pygost/gost3413.py index 321d0ef0c0328c971cbf0e4bd2bd834c962e778835ede2364d71584d7ff86a3a..3a133c98168b7ca2297db9c09fde8d690f721fd15d6a1bcfc1c8b21eb2cbfcfa 100644 --- a/pygost/gost3413.py +++ b/pygost/gost3413.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST R 34.13-2015: Modes of operation for block ciphers +"""GOST R 34.13-2015: Modes of operation for block ciphers This module currently includes only padding methods. """ diff --git a/pygost/mgm.py b/pygost/mgm.py index 0983660c4de9fab5c865f1f42269bb8abf711b912af87be2fd335693ca32bde7..452eda4473732cd20c6d1c4ed235b0a89f0285d8b151089007a2d6912dad628a 100644 --- a/pygost/mgm.py +++ b/pygost/mgm.py @@ -13,7 +13,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" Multilinear Galois Mode (MGM) block cipher mode. +"""Multilinear Galois Mode (MGM) block cipher mode. """ from hmac import compare_digest diff --git a/pygost/pbkdf2.py b/pygost/pbkdf2.py index 8fc1501d10f4061d8c40195c3c01be1759c4131397f61c923ec51b626fe156bf..0a78e7b82d320850515cf6b39cdfa15dc1de8411c2de42d8958db9bb777b1231 100644 --- a/pygost/pbkdf2.py +++ b/pygost/pbkdf2.py @@ -1,5 +1,5 @@ # coding: utf-8 -""" PBKDF2 implementation suitable for GOST R 34.11-94/34.11-2012. +"""PBKDF2 implementation suitable for GOST R 34.11-94/34.11-2012. 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. diff --git a/pygost/test_gost28147.py b/pygost/test_gost28147.py index d47481a04caa222c81f69e4fb3eb8a9ae6908e478422e2c4ec8f925aa6e2be66..37b26747651d967932845b7e80b5f72f608c2a2086ea0b62e390a131ef4c8ea5 100644 --- a/pygost/test_gost28147.py +++ b/pygost/test_gost28147.py @@ -37,7 +37,7 @@ class ECBTest(TestCase): def test_gcl(self): - """ Test vectors from libgcl3 + """Test vectors from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd") @@ -115,7 +115,7 @@ decrypted = ecb_decrypt(key, encrypted, sbox=sbox) self.assertSequenceEqual(decrypted, plaintext) def test_cryptopp(self): - """ Test vectors from Crypto++ 5.6.2 + """Test vectors from Crypto++ 5.6.2 """ sbox = "AppliedCryptography" data = ( @@ -135,7 +135,7 @@ ct = hexdec(ct) self.assertSequenceEqual(ecb_encrypt(key, pt, sbox=sbox), ct) def test_cryptomanager(self): - """ Test vector from http://cryptomanager.com/tv.html + """Test vector from http://cryptomanager.com/tv.html """ sbox = "id-GostR3411-94-TestParamSet" key = hexdec(b"75713134B60FEC45A607BB83AA3746AF4FF99DA6D1B53B5B1B402A1BAA030D1B") @@ -147,7 +147,7 @@ class CFBTest(TestCase): def test_cryptomanager(self): - """ Test vector from http://cryptomanager.com/tv.html + """Test vector from http://cryptomanager.com/tv.html """ key = hexdec(b"75713134B60FEC45A607BB83AA3746AF4FF99DA6D1B53B5B1B402A1BAA030D1B") sbox = "id-GostR3411-94-TestParamSet" @@ -171,7 +171,7 @@ hexdec(b"112233445566778899AABBCCDD800000"), ) def test_steps(self): - """ Check step-by-step operation manually + """Check step-by-step operation manually """ key = urandom(KEYSIZE) iv = urandom(BLOCKSIZE) @@ -194,7 +194,7 @@ step = strxor(plaintext[16:] + 4 * b"\x00", ns2block(step)) self.assertSequenceEqual(step[:4], ciphertext[16:]) def test_random(self): - """ Random data with various sizes + """Random data with various sizes """ key = urandom(KEYSIZE) iv = urandom(BLOCKSIZE) @@ -208,7 +208,7 @@ class CTRTest(TestCase): def test_gcl(self): - """ Test vectors from libgcl3 + """Test vectors from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"0475f6e05038fbfad2c7c390edb3ca3d1547124291ae1e8a2f79cd9ed2bcefbd") @@ -287,7 +287,7 @@ decrypted = cnt(key, encrypted, iv=iv, sbox=sbox) self.assertSequenceEqual(decrypted, plaintext) def test_gcl2(self): - """ Test vectors 2 from libgcl3 + """Test vectors 2 from libgcl3 """ sbox = "id-Gost28147-89-TestParamSet" key = hexdec(b"fc7ad2886f455b50d29008fa622b57d5c65b3c637202025799cadf0768519e8a") diff --git a/pygost/test_gost28147_mac.py b/pygost/test_gost28147_mac.py index 63adaeba7b927545b3c4e1de472e657a4c3deebe8ca7e602f34fbba02c43d1fd..433a33f3f7eb20a6c5c9b666752e0f1babc2dc6dbcbcf4201d4a14c4b9a720c8 100644 --- a/pygost/test_gost28147_mac.py +++ b/pygost/test_gost28147_mac.py @@ -20,7 +20,7 @@ from pygost.gost28147_mac import MAC class TestMAC(TestCase): - """ Test vectors generated with libgcl3 library + """Test vectors generated with libgcl3 library """ k = b"This is message\xFF length\x0032 bytes" diff --git a/pygost/test_gost3410.py b/pygost/test_gost3410.py index 8595c2fcb9bfff052b17ba68759071a753f66be72cc2c4eca8a7178a0112808d..4e18a68173ae4de14ef5bb38fa0db8c4e71486bf2fe0a768070e8fa31aef0ebd 100644 --- a/pygost/test_gost3410.py +++ b/pygost/test_gost3410.py @@ -32,7 +32,7 @@ class Test341001(TestCase): def test_rfc(self): - """ Test vector from :rfc:`5832` + """Test vector from :rfc:`5832` """ prv = bytes(bytearray(( 0x7A, 0x92, 0x9A, 0xDE, 0x78, 0x9B, 0xB9, 0xBE, @@ -122,7 +122,7 @@ s = "1081b394696ffe8e6585e7a9362d26b6325f56778aadbc081c0bfbe933d52ff5823ce288e8c4f362526080df7f70ce406a6eeb1f56919cb92a9853bde73e5b4a" self.assertSequenceEqual(hexenc(signature), s + r) def test_gcl3(self): - """ Test vector from libgcl3 + """Test vector from libgcl3 """ p = bytes2long(bytes(bytearray(( 0x45, 0x31, 0xAC, 0xD1, 0xFE, 0x00, 0x23, 0xC7, diff --git a/pygost/test_gost341194.py b/pygost/test_gost341194.py index 103152b1f10c2de39435270c28e26028c4438b100e2de51aaa76c763ab72e87e..8bd1d330a3c5e2dab5064565a825b8baff0280f52af4187c25a52209dffc7d45 100644 --- a/pygost/test_gost341194.py +++ b/pygost/test_gost341194.py @@ -98,7 +98,7 @@ ) class TestVectorsCryptoPro(TestCase): - """ CryptoPro S-box test vectors + """CryptoPro S-box test vectors """ def test_empty(self): self.assertSequenceEqual( diff --git a/pygost/utils.py b/pygost/utils.py index 81abb644a8a666f3244734eb7cbd3d5dbdb8e6f9188b5df34ae7e3e8682652fd..260073f4f9b90a6b67222defccc29ac4080bfd84b3b757019e16f9502bdaca63 100644 --- a/pygost/utils.py +++ b/pygost/utils.py @@ -23,7 +23,7 @@ xrange = range if version_info[0] == 3 else xrange def strxor(a, b): - """ XOR of two strings + """XOR of two strings This function will process only shortest length of both strings, ignoring remaining one. @@ -52,7 +52,7 @@ return _hexencoder(data)[0].decode("ascii") def bytes2long(raw): - """ Deserialize big-endian bytes into long number + """Deserialize big-endian bytes into long number :param bytes raw: binary string :returns: deserialized long number @@ -62,7 +62,7 @@ return int(hexenc(raw), 16) def long2bytes(n, size=32): - """ Serialize long number into big-endian bytestring + """Serialize long number into big-endian bytestring :param long n: long number :returns: serialized bytestring @@ -78,7 +78,7 @@ return s def modinvert(a, n): - """ Modular multiplicative inverse + """Modular multiplicative inverse :returns: inverse number. -1 if it does not exist