: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) +
: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]
: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)
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
: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.
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):
: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)
))
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
: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.
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")
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
: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)
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
: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)
data[i:i + BLOCKSIZE],
ns2block(encrypt(sbox, key, block2ns(data[i - BLOCKSIZE:i]))),
))
- return b''.join(plaintext)[:size]
+ return b"".join(plaintext)[:size]