1

As learning to be an electronic engineer, my lecturer has requested, based on the original Xtea, to develop the FPGA solution to run 128-bit block size and 128-bit key size cryptography. I have decided to solve it by dividing the 128-bit block into four 32-bit blocks and running as double encryption. I wanted to make sure mathematics was correct first. There is the Python code I have developed. However, the decription process is not correct. Could anyone help me with that?

Python code:

def xtea_encipher_128_modified(num_rounds, v, k):
    y, z, w, x = v
    DELTA = 0x9e3779b9  # (sqrt(5) - 1) * 2**31
    sum = 0
    for _ in range(num_rounds):
        y += ((z << 4) ^ (z >> 5)) + z ^ (sum + k[sum & 3])
        y = y & 0xffffffff  # Ensure y stays within 32-bit bounds
        sum = (sum + DELTA) & 0xffffffff
        z += ((y << 4) ^ (y >> 5)) + y ^ (sum + k[sum >> 11 & 3])
        z = z & 0xffffffff  # Ensure z stays within 32-bit bounds
    w += ((x &lt;&lt; 4) ^ (x &gt;&gt; 5)) + x ^ (sum + k[sum &amp; 3])
    w = w &amp; 0xffffffff  # Ensure w stays within 32-bit bounds
    sum = (sum + DELTA) &amp; 0xffffffff
    x += ((w &lt;&lt; 4) ^ (w &gt;&gt; 5)) + w ^ (sum + k[sum &gt;&gt; 11 &amp; 3])
    x = x &amp; 0xffffffff  # Ensure x stays within 32-bit bounds

return y, z, w, x

def xtea_decipher_128_modified(num_rounds, v, k): y, z, w, x = v DELTA = 0x9e3779b9 # (sqrt(5) - 1) * 2*31 sum = (DELTA num_rounds) & 0xffffffff for _ in range(num_rounds): x -= ((w << 4 ^ w >> 5) + w) ^ (sum + k[(sum >> 11) & 3]) x = x & 0xffffffff # Ensure x stays within 32-bit bounds sum = (sum - DELTA) & 0xffffffff w -= ((x << 4 ^ x >> 5) + x) ^ (sum + k[sum & 3]) w = w & 0xffffffff # Ensure w stays within 32-bit bounds

    z -= ((y &lt;&lt; 4 ^ y &gt;&gt; 5) + y) ^ (sum + k[(sum &gt;&gt; 11) &amp; 3])
    z = z &amp; 0xffffffff  # Ensure z stays within 32-bit bounds
    sum = (sum - DELTA) &amp; 0xffffffff
    y -= ((z &lt;&lt; 4 ^ z &gt;&gt; 5) + z) ^ (sum + k[sum &amp; 3])
    y = y &amp; 0xffffffff  # Ensure y stays within 32-bit bounds

return y, z, w, x

usage example

plaintext = (0xA5A5A5A5,0x01234567,0xFEDCBA98,0x5A5A5A5A) key = (0xDEADBEEF,0x01234567,0x89ABCDEF, 0xDEADBEEF) num_rounds = 32 # number of rounds ciphertext = xtea_encipher_128_modified(num_rounds, plaintext, key) print(ciphertext) plaintext = xtea_decipher_128_modified(num_rounds, ciphertext, key) print(plaintext)

fgrieu
  • 149,326
  • 13
  • 324
  • 622
DT Nam LE
  • 11
  • 1

0 Answers0