pythonpython-3.xencryptionkeyblock-cipher

RC-6 ciphertext does not match non 0 vectors


I came up to this problem where my RC-6 algorithm does not produce the cipher text it should (by the spec doc) well to be more clear, let me give you an example

enter image description here As you see when plain text and key are made out of zero-bytes it passes both tests -> cipher text and decryption text tests

To clarify this even more the cipher values(both correct and wrong) ,are also ordered in little-endian fashion after encrypting.

So my question is - where should I look for invalid code ?

I have a feeling that it is something to do with the byte-ordering before passing it to encryption or key-scheduling functions.

The values I pass to the key-scheduling and encryption functions are straightforward arrays of 32bit words (e.g. [0x00,0x10,0x00,0x00]) and then I move one straight to algorithm (which I wrote looking at the pseudo-code) so no other formatting done before that.

They also start as follows :

def encrypt(plaintext,S):   
    A,C = plaintext[0],plaintext[2] 
    B = modulus(plaintext[1]+S[0])
    D = modulus(plaintext[3]+S[1])  
    for i in range(1,r+1):
         ....
def keyGenerator(L):
    c = len(L)
    S = [int(0)]* (2*r+4)
    S[0] = P
    ....

I could use any help..

Thank you in advance!

By the way the official test vectors could be in THIS document's appendix


Solution

  • So I found out what was wrong in this case. It was indeed a problem with swapping bytes. Since 0's were symmetric input it would go through, and input with mixed values were working ,however giving the wrong answer.

     def swap32(x):
        return (((x << 24) & 0xFF000000) |((x <<  8) & 0x00FF0000) |
            ((x >>  8) & 0x0000FF00) |((x >> 24) & 0x000000FF)) 
    

    This function ,for swapping 8 byte blocks was very useful in my case. I had to swipe the key bytes, the plaintext bytes in the beggining of encryption, then at the end of the enryption, then at the beggining at decryption and at the end of decryption.
    I hope someone will find this useful in the future and won't be stuck in the same place like I was.. Cheers