Adding additional authenticated data to AES-GCM on Android

I am trying to add additional authenticated data (AAD) to AES-GCM on Android. I see a version of Java Encryption Notes about using methods GCMParameterSpecand updateAAD(...), but assuming Android is Java 6 based. I am all out of ideas. I use Spongycastle as a cryptographic library

  GCMParameterSpec s = new GCMParameterSpec(...);
  cipher.init(..., s);
  cipher.updateAAD(...);  // AAD
+4
source share
2 answers

thanks @andrey - I found a more complete sample also containing the BC mailing list

public void testGCM() {
    try {
        byte iv[] = "123456789012".getBytes();
        byte inMsg[] = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
                .getBytes();
        byte aad[] = "123456789012123456789012123456789012345678901234567890123456"
                .getBytes();
        byte key[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".getBytes();

        System.out.println("inMsgLen===" + inMsg.length);

        // encrypt
        AEADParameters parameters = new AEADParameters(
                new KeyParameter(key), 128, iv, aad);
        GCMBlockCipher gcmEngine = new GCMBlockCipher(new AESFastEngine());
        gcmEngine.init(true, parameters);

        byte[] encMsg = new byte[gcmEngine.getOutputSize(inMsg.length)];
        int encLen = gcmEngine.processBytes(inMsg, 0, inMsg.length, encMsg,
                0);
        encLen += gcmEngine.doFinal(encMsg, encLen);

        System.out.println("encLen===" + encLen);

        // decrypt
        gcmEngine.init(false, parameters);

        byte[] decMsg = new byte[gcmEngine.getOutputSize(encMsg.length)];
        int decLen = gcmEngine.processBytes(encMsg, 0, encMsg.length,
                decMsg, 0);
        decLen += gcmEngine.doFinal(decMsg, decLen);

        System.out.println("decLen===" + decLen);

        System.out.println("MSG===" + new String(decMsg));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
+4
source

From the BC mailing list :

, , JCE AAD ( AEAD: CCM, EAX).

API AAD (.init) AEADParameters ( 'relatedText').

API AAD processAADBytes(), AEADBlockCipher.

+2

All Articles