Speck algorithm not working
I am trying to implement Speck 64 bit block / 128 bit key in java. I am stuck with the encryption / decryption algorithm. My decryption algorithm cannot decrypt the cipher text correctly.
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 with functional boxing (<is a bit strange):
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 an 8 character int array.
I am setting decryption right behind encryption, so I could see if this algorithm actually works, but it doesn't and I don't know why. (I reset to their correct values before using decryption).
Links
- SIMON and SPECK families of lightweight block ciphers https://eprint.iacr.org/2013/404
- Implementation spectrum (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); }
+3
source to share
1 answer
Finally it turned 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 accidentally change the rotation functions in the encryption algorithm . This is the correct form:
for(int i = 0; i < T; i++) {
x = (rotateRight(x, alpha) + y) ^ k[i];
y = rotateLeft(y, beta) ^ x;
}
+2
source to share