Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speck algorithm not working

I am trying to implement Speck 64bit block / 128bit key cipher in java. I'm stuck at encryption / decryption algorithm. My decryption algorithm can't decrypt cipher text properly.

My implementation:

  • Encryption:

    int m = 4; //key words
    int T = 27; //rounds
    int alpha = 8; //alpha
    int beta = 3; //beta
    int x,y;
    int[] l = new int[2*T], k = new int[T];
    /* *************** KEY EXTENSTION ***************** */
    for(int i = 0; i < T-1; i++) {
        l[i+m-1] = (k[i] + rotateRight(l[i], alpha)) ^ i;
        k[i+1] = rotateLeft(k[i], beta) ^ l[i+m-1]; 
        //System.out.println(k[i]);
    }
    /* *************** ENCRYPTION ********************* */
    for(int i = 0; i < T; i++) {
        x = (rotateLeft(x, alpha) + y) ^ k[i];
        y = rotateRight(y, beta) ^ x;
        //System.out.println(y);
    }
    
  • Decryption:

    /* *************** KEY EXTENSTION ***************** */
    for(int i = 0; i < T-1; i++) {
        l[i+m-1] = (k[i] + rotateRight(l[i], alpha)) ^ i;
        k[i+1] = rotateLeft(k[i], beta) ^ l[i+m-1];
        //System.out.println(k[i]);
    }
    /* *************** DECRYPTION ********************* */         
    for(int i = T-1; i >= 0; i--) {
        y = rotateRight(y, beta) ^ x;
        x = (rotateLeft(x, alpha) - y) ^ k[i];
        //System.out.println(y);
    }
    

x and y are initialized by function boxing(<- little bit weird):

    x = boxing(plainText, 0, 1);
    y = boxing(plainText, 1, 2);

public static int boxing(int[] content, int i, int count) {
    int temp[] = new int[count];
        temp[i] |= content[i*4] & 0xff;
        temp[i] = temp[i] << 8 | content[i*4+1] & 0xff;
        temp[i] = temp[i] << 8 | content[i*4+2] & 0xff;
        temp[i] = temp[i] << 8 | content[i*4+3] & 0xff;
        //System.out.println(temp[from]);

    return temp[i];
}

Note that content is int array of 8 characters.

I put decryption right behind encryption, so I could see if this algorithm is really working, but it isn't and I don't know why. (I reset variables to their proper values, before I used decryption).

References

  • The SIMON and SPECK Families of Lightweight Block Ciphers https://eprint.iacr.org/2013/404
  • Speck implementation (not mine)
    https://github.com/timw/bc-java/blob/feature/simon-speck/core/src/main/java/org/bouncycastle/crypto/engines/SpeckEngine.java

EDIT:

  • Rotate functions:

    public static int rotateLeft(int number, int amount) {
       return number << amount | number >>> (32-amount);
    }
    public static int rotateRight(int number, int amount) {
       return number >>> amount | number << (32-amount);
    }
    
like image 493
Amphoru Avatar asked Apr 26 '26 13:04

Amphoru


1 Answers

Finally figured out. My decryption algorithm should look like this:

    for(int i = T-1; i >= 0; i--) {
        y = rotateRight(x ^ y, beta);
        x = rotateLeft((x ^ k[i]) - y, alpha);
    }

And I accidently swap rotate functions in encryption algorithm. This is correct form:

    for(int i = 0; i < T; i++) {
        x = (rotateRight(x, alpha) + y) ^ k[i];
        y = rotateLeft(y, beta) ^ x;
    }
like image 98
Amphoru Avatar answered Apr 28 '26 01:04

Amphoru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!