]> Cypherpunks.ru repositories - pygost.git/blobdiff - pygost/gost28147.py
RFC 7836 already contains test vectors, so omit link to TC26 PDF
[pygost.git] / pygost / gost28147.py
index 55fd4741e1cb3ff3d9d3913333745fd0608faba4..9b2104747d0f8c3b544dfe75ceb65b52b11d4532 100644 (file)
@@ -161,7 +161,7 @@ def _K(s, _in):
 
     :param s: S-box
     :param _in: 32-bit word
-    :return: substituted 32-bit word
+    :returns: substituted 32-bit word
     """
     return (
         (s[0][(_in >> 0) & 0x0F] << 0) +
@@ -232,7 +232,7 @@ def xcrypt(seq, sbox, key, ns):
     :param bytes key: 256-bit encryption key
     :param ns: N1 and N2 integers
     :type ns: (int, int)
-    :return: resulting N1 and N2
+    :returns: resulting N1 and N2
     :rtype: (int, int)
     """
     s = SBOXES[sbox]
@@ -267,10 +267,10 @@ def ecb(key, data, action, sbox=DEFAULT_SBOX):
     :param bytes key: encryption key
     :param data: plaintext
     :type data: bytes, multiple of BLOCKSIZE
-    :param func action: encrypt/decrypt
+    :param func action: "encrypt"/"decrypt"
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
-    :return: ciphertext
+    :returns: ciphertext
     :rtype: bytes
     """
     validate_key(key)
@@ -282,14 +282,14 @@ def ecb(key, data, action, sbox=DEFAULT_SBOX):
         result.append(ns2block(action(
             sbox, key, block2ns(data[i:i + BLOCKSIZE])
         )))
-    return b''.join(result)
+    return b"".join(result)
 
 
 ecb_encrypt = partial(ecb, action=encrypt)
 ecb_decrypt = partial(ecb, action=decrypt)
 
 
-def cbc_encrypt(key, data, iv=8 * b'\x00', pad=True, sbox=DEFAULT_SBOX):
+def cbc_encrypt(key, data, iv=8 * b"\x00", pad=True, sbox=DEFAULT_SBOX):
     """ CBC encryption mode of operation
 
     :param bytes key: encryption key
@@ -299,7 +299,7 @@ def cbc_encrypt(key, data, iv=8 * b'\x00', pad=True, sbox=DEFAULT_SBOX):
     :type bool pad: perform ISO/IEC 7816-4 padding
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
-    :return: ciphertext
+    :returns: ciphertext
     :rtype: bytes
 
     34.13-2015 padding method 2 is used.
@@ -318,7 +318,7 @@ def cbc_encrypt(key, data, iv=8 * b'\x00', pad=True, sbox=DEFAULT_SBOX):
         ciphertext.append(ns2block(encrypt(sbox, key, block2ns(
             strxor(ciphertext[-1], data[i:i + BLOCKSIZE])
         ))))
-    return b''.join(ciphertext)
+    return b"".join(ciphertext)
 
 
 def cbc_decrypt(key, data, pad=True, sbox=DEFAULT_SBOX):
@@ -331,7 +331,7 @@ def cbc_decrypt(key, data, pad=True, sbox=DEFAULT_SBOX):
     :type bool pad: perform ISO/IEC 7816-4 unpadding after decryption
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
-    :return: plaintext
+    :returns: plaintext
     :rtype: bytes
     """
     validate_key(key)
@@ -348,17 +348,17 @@ def cbc_decrypt(key, data, pad=True, sbox=DEFAULT_SBOX):
         ))
     if pad:
         last_block = bytearray(plaintext[-1])
-        pad_index = last_block.rfind(b'\x80')
+        pad_index = last_block.rfind(b"\x80")
         if pad_index == -1:
             raise ValueError("Invalid padding")
         for c in last_block[pad_index + 1:]:
             if c != 0:
                 raise ValueError("Invalid padding")
         plaintext[-1] = bytes(last_block[:pad_index])
-    return b''.join(plaintext)
+    return b"".join(plaintext)
 
 
-def cnt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX):
+def cnt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX):
     """ Counter mode of operation
 
     :param bytes key: encryption key
@@ -367,7 +367,7 @@ def cnt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX):
     :type iv: bytes, BLOCKSIZE length
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
-    :return: ciphertext
+    :returns: ciphertext
     :rtype: bytes
 
     For decryption you use the same function again.
@@ -385,7 +385,7 @@ def cnt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX):
         n1 = addmod(n1, C2, 2 ** 32)
         n2 = addmod(n2, C1, 2 ** 32 - 1)
         gamma.append(ns2block(encrypt(sbox, key, (n1, n2))))
-    return strxor(b''.join(gamma), data[:size])
+    return strxor(b"".join(gamma), data[:size])
 
 
 MESH_CONST = hexdec("6900722264C904238D3ADB9646E92AC418FEAC9400ED0712C086DCC2EF4CA92B")
@@ -400,7 +400,7 @@ def meshing(key, iv, sbox=DEFAULT_SBOX):
     return key, iv
 
 
-def cfb_encrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
+def cfb_encrypt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX, mesh=False):
     """ CFB encryption mode of operation
 
     :param bytes key: encryption key
@@ -410,7 +410,7 @@ def cfb_encrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
     :param bool mesh: enable key meshing
-    :return: ciphertext
+    :returns: ciphertext
     :rtype: bytes
     """
     validate_key(key)
@@ -433,10 +433,10 @@ def cfb_encrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
             data[i:i + BLOCKSIZE],
             ns2block(encrypt(sbox, key, block2ns(ciphertext[-1]))),
         ))
-    return b''.join(ciphertext[1:])[:size]
+    return b"".join(ciphertext[1:])[:size]
 
 
-def cfb_decrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
+def cfb_decrypt(key, data, iv=8 * b"\x00", sbox=DEFAULT_SBOX, mesh=False):
     """ CFB decryption mode of operation
 
     :param bytes key: encryption key
@@ -446,7 +446,7 @@ def cfb_decrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
     :param sbox: S-box parameters to use
     :type sbox: str, SBOXES'es key
     :param bool mesh: enable key meshing
-    :return: ciphertext
+    :returns: ciphertext
     :rtype: bytes
     """
     validate_key(key)
@@ -474,4 +474,4 @@ def cfb_decrypt(key, data, iv=8 * b'\x00', sbox=DEFAULT_SBOX, mesh=False):
             data[i:i + BLOCKSIZE],
             ns2block(encrypt(sbox, key, block2ns(data[i - BLOCKSIZE:i]))),
         ))
-    return b''.join(plaintext)[:size]
+    return b"".join(plaintext)[:size]