Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@artjomb
Last active April 8, 2017 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artjomb/142e3cb3b4c6fb643b92c4f2164bee0f to your computer and use it in GitHub Desktop.
Save artjomb/142e3cb3b4c6fb643b92c4f2164bee0f to your computer and use it in GitHub Desktop.
private int BlockSize = 8;
private byte[] xor(byte[] in1, byte[] in2) {
byte[] out = new byte[in1.length];
for(int i = 0; i < in1.length; i++) {
out[i] = in1[i] ^ in2[i];
}
return out;
}
public byte[] crack(final byte[] msg) {
byte[] mac = new byte[BlockSize];
byte[] previousMac = new byte[BlockSize];
final byte[] mac0_1 = oracle.mac0(1);
final byte[] mac0_2 = oracle.mac0(2);
final byte[] o2For0 = xor(mac0_1, mac0_2);
for(int i = 0; i * BlockSize < msg.length; i++) {
byte[] block = Arrays.copyOfRange(msg, i * BlockSize, (i+1) * BlockSize);
block = xor(block, o2For0);
block = xor(block, previousMac);
final byte[] blocksForMac3 = new byte[3 * BlockSize];
System.arraycopy(block, 0, blocksForMac3, 2 * BlockSize, BlockSize);
byte[] macForBlocks = oracle.mac3(blocksForMac3);
macForBlocks = xor(macForBlocks, mac0_2);
previousMac = macForBlocks;
mac = xor(mac, macForBlocks);
}
return mac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment