//Slide Attack //theamazingking.com #include unsigned int sbox[16] = {2, 6, 1, 13, 0, 5, 8, 7, 14, 10, 15, 11, 4, 9, 3, 12}; unsigned int revSbox[16] = {4, 2, 0, 14, 12, 5, 1, 7, 6, 13, 9, 11, 15, 3, 8, 10}; unsigned int realK0, realK1; unsigned int plain[200]; unsigned int cipher[200]; unsigned int fullSbox(unsigned int a) { unsigned int left = (a >> 4) & 0xf; unsigned int right = a & 0xf; left = sbox[left]; right = sbox[right]; return left << 4 | right; } unsigned int fullRevSbox(unsigned int a) { unsigned int left = (a >> 4) & 0xf; unsigned int right = a & 0xf; left = revSbox[left]; right = revSbox[right]; return left << 4 | right; } unsigned int oneRound(unsigned int a, unsigned int k) { return fullSbox(a ^ k); } unsigned int encrypt(unsigned int plain, unsigned int k0, unsigned int k1) { int c; for(c = 0; c < 100; c++) { if ((c % 2) == 0) //EVEN ROUNDS plain = oneRound(plain, k0); else plain = oneRound(plain, k1); } return plain; } unsigned int slidePair(unsigned int plain1, unsigned int cipher1, unsigned int plain2, unsigned int cipher2) { int k0; for(k0 = 0; k0 <= 0xff; k0++) { unsigned int k1 = fullSbox(cipher1 ^ k0) ^ fullRevSbox(cipher2); unsigned int testPlain2 = fullSbox(fullSbox(plain1 ^ k0) ^ k1); if (testPlain2 == plain2) { //VERIFY int crapped = 0; int c; for(c = 0; c < 10; c++) { if (encrypt(plain[c], k0, k1) != cipher[c]) { crapped = 1; break; } } if (crapped) continue; // printf("REAL K0 = %x K1 = %x\n", realK0, realK1); // printf(" FOUND K0 = %x K1 = %x\n", k0, k1); // printf("\n"); return 1; } } return 0; } int slideIt() { int c, d; for(c = 0; c < 30; c++) { for(d = 0; d < 30; d++) if (c != d) if (slidePair(plain[c], cipher[c], plain[d], cipher[d])) return 1; } return 0; } int bruteIt() { int c, d; for(c = 0; c <= 0xFF; c++) for(d = 0; d <= 0xFF; d++) { int crapped = 0; int e; for(e = 0; e < 10; e++) { if (encrypt(plain[e], c, d) != cipher[e]) { crapped = 1; break; } } if (crapped) continue; return 1; } return 0; } int main() { srand(time(NULL)); printf("Simple Slide Attack\n"); int goodOnes = 0; int f; for(f = 0; f < 1000; f++) { realK0 = rand() & 0xFF; realK1 = rand() & 0xFF; int c; for(c = 0; c < 200; c++) { plain[c] = rand() & 0xFF; cipher[c] = encrypt(plain[c], realK0, realK1); } if (slideIt()) goodOnes++; } printf("PERCENTAGE FOUND = %i\n", goodOnes * 100 / f); return 0; }