Score:1

XTEA-based encryption to handle up to 128bit block size

sj flag

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 << 4) ^ (x >> 5)) + x ^ (sum + k[sum & 3])
        w = w & 0xffffffff  # Ensure w stays within 32-bit bounds
        sum = (sum + DELTA) & 0xffffffff
        x += ((w << 4) ^ (w >> 5)) + w ^ (sum + k[sum >> 11 & 3])
        x = x & 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 << 4 ^ y >> 5) + y) ^ (sum + k[(sum >> 11) & 3])
        z = z & 0xffffffff  # Ensure z stays within 32-bit bounds
        sum = (sum - DELTA) & 0xffffffff
        y -= ((z << 4 ^ z >> 5) + z) ^ (sum + k[sum & 3])
        y = y & 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)
Maarten Bodewes avatar
in flag
Beware that coding related questions are, at least officially, deemed off topic.
Maarten Bodewes avatar
in flag
Debug tip: Try an single round, and see where the intermediate variable values change, possibly by printing them out in hexadecimal form (creating a log method usually works better than stepping through them, due to the relatively large state). You may also be able to use a "conditional breakpoint" and call a function to print out the values, as not to leave trace calls within performance related code.
fgrieu avatar
ng flag
Hint: what is `num_rounds` at at the end of encryption? After fixing the related error, decryption will undo encryption. But there's a more serious issue: there is zero [diffusion](https://en.wikipedia.org/wiki/Confusion_and_diffusion#Diffusion) between the halves (w,x) and (y,z) of a 128-block, making this in effect a mere alternation of two 64-bit block ciphers each slightly different from XTEA in their key schedule. Also these two related ciphers share the same key, which is very un-academical, and very possibly dangerous.
fgrieu avatar
ng flag
For reasons [there](https://crypto.stackexchange.com/posts/comments/228016), I don't see why we should consider XTEA over TEA. And given that the target is hardware, and that 64-to-128 bit extension of (X)TEA is more difficult than it seems, I'd suggest [Simon](https://en.wikipedia.org/wiki/Simon_(cipher)), which has a 128-bit-key-and-block version.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.