use of org.spongycastle.crypto.modes.EAXBlockCipher in project fitness-app by seemoo-lab.
the class Crypto method encryptDump.
/*
Fitbit Flex/One/Charge firmware uses XTEA in EAX mode, we encrypt according to this.
TODO: if we find a vulnerability for AES trackers, this method should also be able to use AES/EAX...
Length fields must match, otherwise result can become null ...
*/
public static String encryptDump(byte[] dump, Activity activity) throws InvalidCipherTextException {
int headerlength = 14;
int inlength = 0;
int plainlength = 0;
int trailerlenght = 11;
String outStr = "";
byte[] header = new byte[headerlength];
byte[] trailer = new byte[trailerlenght];
inlength = dump.length;
plainlength = inlength - headerlength - trailerlenght;
byte[] plain = new byte[plainlength];
System.arraycopy(dump, 0, header, 0, headerlength);
System.arraycopy(dump, headerlength, plain, 0, plainlength);
System.arraycopy(dump, inlength - trailerlenght, trailer, 0, trailerlenght);
// Set Crypt-Byte in header
header[4] = (byte) 0x01;
// Nonce should not be zero... //TODO make nonce random
// header[6] = (byte) 0xab;
// header[7] = (byte) 0xcd;
// get the nonce from the dump
byte[] nonce = Arrays.copyOfRange(header, 6, 10);
// cmac (tag) length in bytes
int mac_len = 8 * 8;
XTEAEngine engine = new XTEAEngine();
EAXBlockCipher eax = new EAXBlockCipher(engine);
Log.e(TAG, "key: " + getKey());
Log.e(TAG, "nonce: " + Utilities.byteArrayToHexString(nonce));
AEADParameters params = new AEADParameters(new KeyParameter(getKey()), mac_len, nonce, null);
// TODO switch true to false here to implement a decryption method, apply it to microdumps/megadumps
eax.init(true, params);
byte[] result = new byte[eax.getOutputSize(plainlength)];
int resultlength = eax.processBytes(plain, 0, plainlength, result, 0);
eax.doFinal(result, resultlength);
byte[] out = new byte[inlength];
System.arraycopy(header, 0, out, 0, headerlength);
System.arraycopy(result, 0, out, headerlength, result.length);
System.arraycopy(header, headerlength - 4, out, headerlength + result.length, 3);
outStr = Utilities.byteArrayToHexString(out);
return outStr;
}
Aggregations