1

I am creating a product whose firmware is updated using an android phone. The android application automatically downloads an encrypted version of the firmware, decrypts it, and sends it to the devices boot-loader. In order to generate the same secret key I specificy the password and salt in the code. I'm worried the apk will be decompiled and someone will be able to decrypt our firmware.

Is there a better way to decrypt/encrypt files or protect the code?

Code:

private byte[] DecryptFile(byte[] encryptedFileBuffer) {        

    final int iterationCount = 10;

    byte[] dataDecrypted = null;
    SecretKey secKey = null;
    try {
        byte[] salt = "salt1234".getBytes();
        String accessThingy = "Password";
        KeySpec keySpec = new PBEKeySpec(accessThingy.toCharArray(), salt, iterationCount);
        secKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);


        Cipher desCipher;
        // Create the cipher 
        desCipher = Cipher.getInstance(secKey.getAlgorithm());          
        desCipher.init(Cipher.DECRYPT_MODE, secKey,paramSpec);

        dataDecrypted = desCipher.doFinal(encrptedFileBuffer);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return dataDecrypted;

}

2 Answers2

1

Yes and no.

No, if the decryption routine can be executed by an attacker (and why wouldn't it be) then the firmware would be compromised. The only way to avoid this is to add protection to the key on the device. You can think of OS/hardware support for this, or about storing the key/password outside of the device for instance. But a single compromised device would leak the firmware. This is the DRM conundrum.

And yes as you seem to use PBKDF1, MD5 and DES, none of which is particularly safe. MD5 is the most broken algorithm in that list, but it is the one that is least likely to actually become a problem. You should be using PBKDF2, SHA-2 and AES instead. Try this answer, Java 8 also has added support for PBKDF2 with SHA-2. Or you could actually use a fully random key instead of using password based encryption (PBE).

You may also want to consider asymmetric primitives (ECDSA/RSA) for encryption and code signing.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
0

Would it be possible to move the decryption to the device itself? This way the code would be less accessible to the end user assuming that there wasn't any way to read back the program from the device (which would also cause an issue here).

Ben Pye
  • 807
  • 7
  • 14