terl / lazysodium-android

An Android implementation of the Libsodium cryptography library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
108 stars 25 forks source link

cryptoAeadAES256GCM is not available #59

Open AndroidCrypto opened 1 year ago

AndroidCrypto commented 1 year ago

I'm trying to implement a string encryption using AES-256 in GCM mode with Lazysodium but the code fails and ls.cryptoAeadAES256GCMIsAvailable() return false.

Running the (nearly) same code with ChaCha20Poly1305 gives the expected cipher- and decrypted-texts.

My dev-machine is a Mac with MacOs Ventura 13.1 and M1-chip.

I'm using these dependencies and they got loaded without any Gradle- or build error:

implementation 'com.goterl:lazysodium-android:5.1.0@aar'
implementation 'net.java.dev.jna:jna:5.12.1@aar'

I setup a simple Android app (link to the app: https://github.com/AndroidCrypto/LazysodiumSymmetricEncryption) that shows the complete workflow, here are the essential parts:

        // note: outputKey is a 32 bytes long byte array from a key derivation
        // generate a random nonce
        byte[] nonceByte = ls.nonce(AEAD.AES256GCM_NPUBBYTES);
        Log.i(TAG, "generate a random nonce for encryption: " + ls.sodiumBin2Hex(nonceByte));
        // generate a key from outputKey
        Key key = Key.fromBytes(outputKey);
        Log.i(TAG, "generate a key from outputKey");
        // encrypt
        Log.i(TAG, "encrypt the plaintext using AES-256 GCM algorithm");
        String ciphertext = ls.encrypt(plaintext, null, nonceByte, key, AEAD.Method.AES256GCM);
        // ciphertext is in hex encoding
        Log.i(TAG, "ciphertext: " + ciphertext);
        // decrypt
        Log.i(TAG, "decrypt the ciphertext using the same algorithm, key and nonce");
        String decryptedtext = "";
        if (ls.cryptoAeadAES256GCMIsAvailable()) {
            Log.i(TAG, "AEAD AES-256 GCM is available");
            try {
                decryptedtext = ls.decrypt(ciphertext, null, nonceByte, key, AEAD.Method.AES256GCM);
            } catch (AEADBadTagException e) {
                e.printStackTrace();
                //Log.e(TAG, e.getMessage());
            }
            Log.i(TAG, "decryptedtext: " + decryptedtext);
            Log.i(TAG, "plaintext equals to decrptedtext: " + decryptedtext.equals(plaintext));
        } else {
            Log.e(TAG, "AEAD AES-256 GCM is not available");
        }

Greetings and Happy new year to everyone Michael