Android Activity

From Crypto++ Wiki
Jump to: navigation, search
Cryptopp-android-prng.png
Android (Command Line) shows you how to build Crypto++ on the command line. This page discusses how to use the Crypto++ shared object in an Android Activity. The sample activity provides a shared object that calls into the Crypto++ shared object, builds the activity's shared object using the NDK, and packages the APK using Ant.

You can find an example of using prebuilt Crypto++ shared objects at Android-PRNG. The sample code demonstrates two things. First, it shows you how to integrate a prebuilt Crypto++ library shared object into a Ant-based or NDK-based project. It may work for Android Studio, too. It may even work for Eclipse, depending on how well it imports an Android java project with JNI.

Second, it shows you how to accumulate seed data from sensors that can be used to seed your software based random number generators. A generator should have a fresh seed applied with IncorporateEntropy before each call to GenerateBlock to ensure the fitness of the generator.

Android.mk

Android provides a modified makefile based build system. If you are interested in an Android.mk to use with ndk-build, Eclipse, or Android Sudio, then see Pull Request 3, Add Android.mk to build using android NDK. Thanks to Alex Afanasyev for providing it.

An Android.mk has not been added to the Crypto++ sources because there are some open questions about using it to safely build static archives and shared objects. See, for example, No archive symbol table (run ranlib) while building libcryptopp.a through ndk-build and How to run ranlib on an archive built through Android.mk?

Android Studio

If you are using Android Studio, then you may need to add the following. Also see JNI and Gradle in Android Studio on Stack Overflow.

sourceSets.main {
    jni.srcDirs = []
    jniLibs.srcDir 'src/main/libs'
}

PRNG.java

Android's Java interface into Crypto++ is PRNG.java. Note that libprng.so provides CryptoPP_Reseed, CryptoPP_GetBytes and friends.

Crypto++ is thread safe at the class level, meaning their is no shared data. Crypto++ objects must be guarded using an external lock in case multiple threads use the same object. PRNG.java provides the lock.

public class PRNG {
    
    static {
        System.loadLibrary("stlport_shared");
        System.loadLibrary("cryptopp");
        System.loadLibrary("prng");
    }
    
    private static native int CryptoPP_Reseed(byte[] bytes);
    
    private static native int CryptoPP_GetBytes(byte[] bytes);
    
    private static Object lock = new Object();
    
    // Class method. Returns the number of bytes consumed from the seed.
    public static int Reseed(byte[] seed) {
    	
        synchronized (lock) {
            return CryptoPP_Reseed(seed);
        }
    }
    
    // Class method. Returns the number of bytes generated.
    public static int GetBytes(byte[] bytes) {
        synchronized (lock) {
            return CryptoPP_GetBytes(bytes);
        }
    }
    
    // Instance method. Returns the number of bytes consumed from the seed.
    public int reseed(byte[] seed) {
        synchronized (lock) {
            return CryptoPP_Reseed(seed);
        }
    }
    
    // Instance method. Returns the number of bytes generated.
    public int getBytes(byte[] bytes) {
        synchronized (lock) {
            return CryptoPP_GetBytes(bytes);
        }
    }
}

Downloads

The source code for Android-PRNG is located on GitHub at Noloader | Android-PRNG.