Crypto++ in Android(Build)

Crypto++ Library is a free C++ class library of cryptographic schemes. Crypto++

This covers a lot of cryptographic

In Android, default Java cryptographic API is not covered a lot, and slow a bit.

To use Crypto++, we can

Good Explanation to build as Android NDK shared library is Android NDK Advanced Tutorial

This explanation is for 5.6.1, but 5.6.2 works too.

Steps

  1. Create toolchain to build library
  2. Download Crypto++ and decompress
  3. Change GNUMakefile to build for Android
  4. Add libcryptopp.so to Android Native Project

Create toolchain

Please refer Toolchain
This is other my entry.

Environment variables

We need some environment variables, NDK, SYSROOT, CXX, CC

Env title Description Example
NDK Android NDK root /opt/android/android-ndk-r8
SYSROOT Toolchain architecture root /opt/android/android-ndk-r8/platforms/android-14/arch-arm
CC gcc command name arm-linux-androideabi-gcc
CXX g++ command name arm-linux-androideabi-g++

Also, add toolchain root to path

To check env vars

env

Show all env

Build Crypto++ under Android gcc, g++

Crypto++ has already GNUMakefile (Makefile) to build.
But, this is for PC architecture ex.. x86_64.

I tried to build Linux Mint15, problem to build and can use it!
But, our target is Android.

You need to change a bit in GNUMakefile.

ifneq ($(GCC42_OR_LATER),0)
ifeq ($(UNAME),Darwin)
CXXFLAGS += -arch x86_64 -arch i386
else
CXXFLAGS += -march=armv7-a
endif
endif

This is the part to change. Maybe about 40L I think.
This is architecture part please change from -march=native to armv7-a( if you want to use armv5te), use armv5te.

And remove linker option glibc pthread,… LDFLAGS += -pthread
because Android is enabled pthread by default

Add $(LDLIBS)

This is final style to build libcryptopp.so

libcryptopp.so: $(LIBOBJS)
	$(CXX) -shared -o $@ $(LIBOBJS) $(LDFLAGS) $(LDLIBS)

Add these flags to build C++ env

CXXFLAGS += -nostdinc++ -I$(NDK)/sources/cxx-stl/stlport/stlport
LDFLAGS += -nodefaultlibs -L$(NDK)/sources/cxx-stl/stlport/libs/armeabi
LDLIBS += -lstlport_shared -lc -lm -ldl -lgcc

Add following description to wait.h ( header file)

#include <sys/select.h>

Build command of Crypto++

Finally, Let’s build.

make

Make does compile all sources and create static library …

make dynamic

After make, build shared library to use for Android(.so)

We can get libcryptopp.so

Problem

Android STL has 2 version. STLport and GNU STL
Both doesn’t work when using std:string class.
std:string class was handled as basic_string in.

Android basic_string doesn’t have enough feature

I couldn’t fix it : (