ciphers.xtea

XTEA (eXtended Tiny Encryption Algorithm) is a block cipher designed to correct weaknesses in TEA. It was published by David Wheeler and Roger Needham in 1997. XTEA operates on 64-bit blocks with a 128-bit key and uses a Feistel network with a recommended 64 rounds.

It’s still found in embedded systems and game networking protocols due to its simplicity and small code footprint.

Reference: https://en.wikipedia.org/wiki/XTEA

Attributes

DELTA

MASK

Functions

xtea_decrypt(→ bytes)

Decrypt a single 64-bit block using XTEA.

xtea_encrypt(→ bytes)

Encrypt a single 64-bit block using XTEA.

Module Contents

ciphers.xtea.xtea_decrypt(block: bytes, key: bytes, num_rounds: int = 64) bytes

Decrypt a single 64-bit block using XTEA.

Parameters:
  • block – 8 bytes of ciphertext

  • key – 16 bytes (128-bit key)

  • num_rounds – number of Feistel rounds (default 64)

Returns:

8 bytes of plaintext

Roundtrip test – encrypt then decrypt returns original plaintext: >>> key = b’x00’ * 16 >>> plaintext = b’x00’ * 8 >>> xtea_decrypt(xtea_encrypt(plaintext, key), key) == plaintext True

>>> msg = b'hello!!!'
>>> k = b'sixteenbyteskey!'
>>> xtea_decrypt(xtea_encrypt(msg, k), k) == msg
True
>>> xtea_decrypt(b'short', key)
Traceback (most recent call last):
    ...
ValueError: block must be 8 bytes
>>> xtea_decrypt(b'\x00' * 8, b'short')
Traceback (most recent call last):
    ...
ValueError: key must be 16 bytes
ciphers.xtea.xtea_encrypt(block: bytes, key: bytes, num_rounds: int = 64) bytes

Encrypt a single 64-bit block using XTEA.

Parameters:
  • block – 8 bytes of plaintext

  • key – 16 bytes (128-bit key)

  • num_rounds – number of Feistel rounds (default 64)

Returns:

8 bytes of ciphertext

>>> key = b'\x00' * 16
>>> plaintext = b'\x00' * 8
>>> ciphertext = xtea_encrypt(plaintext, key)
>>> ciphertext.hex()
'fc924d124ad0ed50'
>>> xtea_encrypt(b'hello!!!', b'sixteenbyteskey!')
b'u\x8d\x00\x17c\xb8\xf0*'
>>> xtea_encrypt(b'short', key)
Traceback (most recent call last):
    ...
ValueError: block must be 8 bytes
>>> xtea_encrypt(plaintext, b'short')
Traceback (most recent call last):
    ...
ValueError: key must be 16 bytes
ciphers.xtea.DELTA = 2654435769
ciphers.xtea.MASK = 4294967295