doc/NEWS | 2 +- pygost/default.t.do | 1 - pygost/gost28147.py | 13 ++++++------- pygost/gost28147_mac.py | 3 +-- pygost/gost34112012.py | 11 +++++------ pygost/gost341194.py | 7 +++---- pygost/gost3412.py | 11 ++++------- pygost/gost3413.py | 21 ++++++++++----------- pygost/pbkdf2.py | 4 +--- pygost/utils.py | 3 +-- diff --git a/doc/NEWS b/doc/NEWS index 87df310701b63d4cfa349974917227d1ab48d71a095b700fff2544142f64dc38..b7d8c96da7ab5e0219e968c3e787dc6306fce460b6ccae514f232ebeb7d73e41 100644 --- a/doc/NEWS +++ b/doc/NEWS @@ -2,7 +2,7 @@ A 6.0 6.0 * Removed Python2 compatibility. * Added SESPAKE (RFC 8133) algorithm support. - * Removed utils.bytes2long and utils.long2bytes. + * Removed utils.bytes2long, utils.long2bytes, utils.xrange. A 5.13 5.13 diff --git a/pygost/default.t.do b/pygost/default.t.do deleted file mode 100644 index d61c1bc3862b86b6430f2e2994b0ac11f0839ee3cf050ab2314e779d03397b68..0000000000000000000000000000000000000000 --- a/pygost/default.t.do +++ /dev/null @@ -1 +0,0 @@ -PYTHONPATH=$PYTHONPATH:.. ${PYTHON:-python} -m unittest test_$2 diff --git a/pygost/gost28147.py b/pygost/gost28147.py index 114cfdd4afe317c8bbbb0095247bab6d3badab0ca9a6a7e7f886e5d8426f352d..033104637bcacde977d3f518dd8aa51a572fe5eb598f4adea8e55644980d5acd 100644 --- a/pygost/gost28147.py +++ b/pygost/gost28147.py @@ -28,7 +28,6 @@ from pygost.gost3413 import pad_size from pygost.gost3413 import unpad2 from pygost.utils import hexdec from pygost.utils import strxor -from pygost.utils import xrange KEYSIZE = 32 @@ -262,7 +261,7 @@ validate_sbox(sbox) if not data or len(data) % BLOCKSIZE != 0: raise ValueError("Data is not blocksize aligned") result = [] - for i in xrange(0, len(data), BLOCKSIZE): + for i in range(0, len(data), BLOCKSIZE): result.append(ns2block(action( sbox, key, block2ns(data[i:i + BLOCKSIZE]) ))) @@ -299,7 +298,7 @@ data = pad2(data, BLOCKSIZE) if len(data) % BLOCKSIZE != 0: raise ValueError("Data is not blocksize aligned") ciphertext = [iv] - for i in xrange(0, len(data), BLOCKSIZE): + for i in range(0, len(data), BLOCKSIZE): if mesh and i >= MESH_MAX_DATA and i % MESH_MAX_DATA == 0: key, _ = meshing(key, iv, sbox=sbox) ciphertext.append(ns2block(encrypt(sbox, key, block2ns( @@ -328,7 +327,7 @@ if len(data) < 2 * BLOCKSIZE: raise ValueError("There is no either data, or IV in ciphertext") iv = data[:BLOCKSIZE] plaintext = [] - for i in xrange(BLOCKSIZE, len(data), BLOCKSIZE): + for i in range(BLOCKSIZE, len(data), BLOCKSIZE): if ( mesh and (i - BLOCKSIZE) >= MESH_MAX_DATA and @@ -365,7 +364,7 @@ if not data: raise ValueError("No data supplied") n2, n1 = encrypt(sbox, key, block2ns(iv)) gamma = [] - for _ in xrange(0, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): + for _ in range(0, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): n1 = (n1 + C2) % (2 ** 32) n2 = (n2 + C1) % (2 ** 32 - 1) gamma.append(ns2block(encrypt(sbox, key, (n1, n2)))) @@ -403,7 +402,7 @@ validate_sbox(sbox) if not data: raise ValueError("No data supplied") ciphertext = [iv] - for i in xrange(0, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): + for i in range(0, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): if mesh and i >= MESH_MAX_DATA and i % MESH_MAX_DATA == 0: key, iv = meshing(key, ciphertext[-1], sbox=sbox) ciphertext.append(strxor( @@ -438,7 +437,7 @@ if not data: raise ValueError("No data supplied") plaintext = [] data = iv + data - for i in xrange(BLOCKSIZE, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): + for i in range(BLOCKSIZE, len(data) + pad_size(len(data), BLOCKSIZE), BLOCKSIZE): if ( mesh and (i - BLOCKSIZE) >= MESH_MAX_DATA and diff --git a/pygost/gost28147_mac.py b/pygost/gost28147_mac.py index db02ef69989c4c62ae9925ecb84f610f90ed54c8b098ca15d8e3f69fb4fbe76d..71d5ab403d1377c7a7e4ab67501b6f535f17628bcc4c27416088480f22c4179c 100644 --- a/pygost/gost28147_mac.py +++ b/pygost/gost28147_mac.py @@ -29,7 +29,6 @@ from pygost.gost28147 import xcrypt from pygost.gost3413 import pad1 from pygost.iface import PEP247 from pygost.utils import strxor -from pygost.utils import xrange digest_size = 8 SEQ_MAC = ( @@ -85,7 +84,7 @@ if not self.data: raise ValueError("No data processed") data = pad1(self.data, BLOCKSIZE) prev = block2ns(self.iv)[::-1] - for i in xrange(0, len(data), BLOCKSIZE): + for i in range(0, len(data), BLOCKSIZE): prev = xcrypt( SEQ_MAC, self.sbox, self.key, block2ns(strxor( data[i:i + BLOCKSIZE], diff --git a/pygost/gost34112012.py b/pygost/gost34112012.py index fc1a9aa41bc9f502a71ec99a08065843c1216875cb61a7c32f10d24511624070..e6e449b2835433b77ccefc1ba04a5c62439d841985d73c592f132684aa0f70f7 100644 --- a/pygost/gost34112012.py +++ b/pygost/gost34112012.py @@ -26,7 +26,6 @@ from pygost.iface import PEP247 from pygost.utils import hexdec from pygost.utils import strxor -from pygost.utils import xrange BLOCKSIZE = 64 @@ -165,13 +164,13 @@ def _lcache(): cache = [] - for byteN in xrange(8): - cache.append([0 for _ in xrange(256)]) - for byteN in xrange(8): - for byteVal in xrange(256): + for byteN in range(8): + cache.append([0 for _ in range(256)]) + for byteN in range(8): + for byteVal in range(256): res64 = 0 val = byteVal - for bitN in xrange(8): + for bitN in range(8): if val & 0x80 > 0: res64 ^= A[(7 - byteN) * 8 + bitN] val <<= 1 diff --git a/pygost/gost341194.py b/pygost/gost341194.py index 2159ab0c7ef4152d68a6c437bf746a6977e38e17541edb15de0391123383b22f..6eae6dd129554cd28ee202488aa4efc5693778d4b48aee0f427bee699c31ab0e 100644 --- a/pygost/gost341194.py +++ b/pygost/gost341194.py @@ -32,7 +32,6 @@ 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 DEFAULT_SBOX = "id-GostR3411-94-CryptoProParamSet" @@ -115,12 +114,12 @@ # Permute # H_out = chi^61(H_in XOR chi(m XOR chi^12(S))) x = s - for _ in xrange(12): + for _ in range(12): x = _chi(x) x = strxor(x, m) x = _chi(x) x = strxor(hin, x) - for _ in xrange(61): + for _ in range(61): x = _chi(x) return x @@ -163,7 +162,7 @@ _len = 0 checksum = 0 h = 32 * b"\x00" m = self.data - for i in xrange(0, len(m), BLOCKSIZE): + for i in range(0, len(m), BLOCKSIZE): part = m[i:i + BLOCKSIZE][::-1] _len += len(part) * 8 checksum = (checksum + int(hexenc(part), 16)) % (2 ** 256) diff --git a/pygost/gost3412.py b/pygost/gost3412.py index ccd523a01c6477f2da2ce9d3104a8659a51e2fd13614a8abb3a6a47f37523a8d..0340308506b79475642789384f9f1f0682aeaf2d7ec2420500a961a0e51707f6 100644 --- a/pygost/gost3412.py +++ b/pygost/gost3412.py @@ -23,7 +23,6 @@ from pygost.gost28147 import decrypt as gost28147_decrypt from pygost.gost28147 import encrypt as gost28147_encrypt from pygost.gost28147 import ns2block as gost28147_ns2block from pygost.utils import strxor -from pygost.utils import xrange KEYSIZE = 32 @@ -55,7 +54,7 @@ # Precalculate inverted PI value as a performance optimization. # Actually it can be computed only once and saved on the disk. ######################################################################## PIinv = bytearray(256) -for x in xrange(256): +for x in range(256): PIinv[PI[x]] = x @@ -76,12 +75,10 @@ # Precalculate all possible gf(byte, byte) values as a performance # optimization. # Actually it can be computed only once and saved on the disk. ######################################################################## - - -GF = [bytearray(256) for _ in xrange(256)] +GF = [bytearray(256) for _ in range(256)] -for x in xrange(256): - for y in xrange(256): +for x in range(256): + for y in range(256): GF[x][y] = gf(x, y) diff --git a/pygost/gost3413.py b/pygost/gost3413.py index a651ab900470bafafb40f71d62ba7193b0d178bfac4e5c6523b2741fbbc9bbaa..c89d057905d15fd0b0671307f143cad35fb1a8745ee82313e4136337adf9544a 100644 --- a/pygost/gost3413.py +++ b/pygost/gost3413.py @@ -21,7 +21,6 @@ from os import urandom from pygost.utils import strxor -from pygost.utils import xrange KEYSIZE = 32 @@ -84,7 +83,7 @@ """ if not pt or len(pt) % bs != 0: raise ValueError("Plaintext is not blocksize aligned") ct = [] - for i in xrange(0, len(pt), bs): + for i in range(0, len(pt), bs): ct.append(encrypter(pt[i:i + bs])) return b"".join(ct) @@ -99,7 +98,7 @@ """ if not ct or len(ct) % bs != 0: raise ValueError("Ciphertext is not blocksize aligned") pt = [] - for i in xrange(0, len(ct), bs): + for i in range(0, len(ct), bs): pt.append(decrypter(ct[i:i + bs])) return b"".join(pt) @@ -136,7 +135,7 @@ ctr_max_value = 1 << (8 * (bs // 2)) if _acpkm is not None: acpkm_algo_class, acpkm_section_size_in_bs = _acpkm acpkm_section_size_in_bs //= bs - for _ in xrange(0, len(data) + pad_size(len(data), bs), bs): + for _ in range(0, len(data) + pad_size(len(data), bs), bs): if ( _acpkm is not None and ctr_value != 0 and @@ -179,7 +178,7 @@ if len(iv) < bs or len(iv) % bs != 0: raise ValueError("Invalid IV size") r = [iv[i:i + bs] for i in range(0, len(iv), bs)] result = [] - for i in xrange(0, len(data) + pad_size(len(data), bs), bs): + for i in range(0, len(data) + pad_size(len(data), bs), bs): r = r[1:] + [encrypter(r[0])] result.append(strxor(r[-1], data[i:i + bs])) return b"".join(result) @@ -199,7 +198,7 @@ if len(iv) < bs or len(iv) % bs != 0: raise ValueError("Invalid IV size") r = [iv[i:i + bs] for i in range(0, len(iv), bs)] ct = [] - for i in xrange(0, len(pt), bs): + for i in range(0, len(pt), bs): ct.append(encrypter(strxor(r[0], pt[i:i + bs]))) r = r[1:] + [ct[-1]] return b"".join(ct) @@ -219,7 +218,7 @@ if len(iv) < bs or len(iv) % bs != 0: raise ValueError("Invalid IV size") r = [iv[i:i + bs] for i in range(0, len(iv), bs)] pt = [] - for i in xrange(0, len(ct), bs): + for i in range(0, len(ct), bs): blk = ct[i:i + bs] pt.append(strxor(r[0], decrypter(blk))) r = r[1:] + [blk] @@ -238,7 +237,7 @@ if len(iv) < bs or len(iv) % bs != 0: raise ValueError("Invalid IV size") r = [iv[i:i + bs] for i in range(0, len(iv), bs)] ct = [] - for i in xrange(0, len(pt) + pad_size(len(pt), bs), bs): + for i in range(0, len(pt) + pad_size(len(pt), bs), bs): ct.append(strxor(encrypter(r[0]), pt[i:i + bs])) r = r[1:] + [ct[-1]] return b"".join(ct) @@ -256,7 +255,7 @@ if len(iv) < bs or len(iv) % bs != 0: raise ValueError("Invalid IV size") r = [iv[i:i + bs] for i in range(0, len(iv), bs)] pt = [] - for i in xrange(0, len(ct) + pad_size(len(ct), bs), bs): + for i in range(0, len(ct) + pad_size(len(ct), bs), bs): blk = ct[i:i + bs] pt.append(strxor(encrypter(r[0]), blk)) r = r[1:] + [blk] @@ -295,7 +294,7 @@ tail_offset = len(data) - bs else: tail_offset = len(data) - (len(data) % bs) prev = bs * b"\x00" - for i in xrange(0, tail_offset, bs): + for i in range(0, tail_offset, bs): prev = encrypter(strxor(data[i:i + bs], prev)) tail = data[tail_offset:] return encrypter(strxor( @@ -348,7 +347,7 @@ key_section_size, bs, (KEYSIZE + bs) * sections, ) - for i in xrange(0, tail_offset, bs): + for i in range(0, tail_offset, bs): if i % section_size == 0: keymat, keymats = keymats[:KEYSIZE + bs], keymats[KEYSIZE + bs:] key, k1 = keymat[:KEYSIZE], keymat[KEYSIZE:] diff --git a/pygost/pbkdf2.py b/pygost/pbkdf2.py index 80ac943d89b8bce0cedd32971a995ebceb92b9e25e63989118a378981cb0bae1..eb08b97e19bf1e0b40da953b85384d9dc2c112b2ab7de41fd5bee9da795ac193 100644 --- a/pygost/pbkdf2.py +++ b/pygost/pbkdf2.py @@ -5,9 +5,7 @@ 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. """ - from pygost.utils import strxor -from pygost.utils import xrange def pbkdf2(hasher, password, salt, iterations, dklen): @@ -31,7 +29,7 @@ loop = 1 while len(dkey) < dklen: prev = prf(salt + loop.to_bytes(4, "big")) rkey = int.from_bytes(prev, "big") - for _ in xrange(iterations - 1): + for _ in range(iterations - 1): prev = prf(prev) rkey ^= int.from_bytes(prev, "big") loop += 1 diff --git a/pygost/utils.py b/pygost/utils.py index 61e68cd07ceb5e73df454693f275ffbb35f9370f47c0059e3cc2205cc307762f..88b7a324f6bfbe60f3af2de63f37f79a85a16fe4e5831848104a718d6aa7526f 100644 --- a/pygost/utils.py +++ b/pygost/utils.py @@ -16,7 +16,6 @@ # along with this program. If not, see . from codecs import getdecoder from codecs import getencoder -from sys import version_info xrange = range if version_info[0] == 3 else xrange @@ -30,7 +29,7 @@ ignoring remaining one. """ mlen = min(len(a), len(b)) a, b, xor = bytearray(a), bytearray(b), bytearray(mlen) - for i in xrange(mlen): + for i in range(mlen): xor[i] = a[i] ^ b[i] return bytes(xor)