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
- Create toolchain to build library
- Download Crypto++ and decompress
- Change GNUMakefile to build for Android
- 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 : (
Hello there! This is my first comment here so I just wanted to give a quick shout out and say I truly enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same topics? Thanks a lot!
Hi, I have the same problem, I can compile with this config
Android.mk in library :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cryptopp_static
LOCAL_MODULE_FILENAME := libcryptopp
LOCAL_SRC_FILES := \
3way.cpp \
adler32.cpp \
algebra.cpp \
algparam.cpp \
arc4.cpp \
asn.cpp \
authenc.cpp \
base32.cpp \
base64.cpp \
basecode.cpp \
bfinit.cpp \
blowfish.cpp \
blumshub.cpp \
camellia.cpp \
cast.cpp \
casts.cpp \
cbcmac.cpp \
ccm.cpp \
channels.cpp \
cmac.cpp \
cpu.cpp \
crc.cpp \
cryptlib.cpp \
default.cpp \
des.cpp \
dessp.cpp \
dh.cpp \
dh2.cpp \
dll.cpp \
dsa.cpp \
eax.cpp \
ec2n.cpp \
eccrypto.cpp \
ecp.cpp \
elgamal.cpp \
emsa2.cpp \
eprecomp.cpp \
esign.cpp \
files.cpp \
filters.cpp \
fips140.cpp \
fipsalgt.cpp \
gcm.cpp \
gf2_32.cpp \
gf2n.cpp \
gf256.cpp \
gfpcrypt.cpp \
gost.cpp \
gzip.cpp \
hex.cpp \
hmac.cpp \
hrtimer.cpp \
ida.cpp \
idea.cpp \
integer.cpp \
iterhash.cpp \
luc.cpp \
mars.cpp \
marss.cpp \
md2.cpp \
md4.cpp \
md5.cpp \
misc.cpp \
modes.cpp \
mqueue.cpp \
mqv.cpp \
nbtheory.cpp \
network.cpp \
oaep.cpp \
osrng.cpp \
panama.cpp \
pch.cpp \
pkcspad.cpp \
polynomi.cpp \
pssr.cpp \
pubkey.cpp \
queue.cpp \
rabin.cpp \
randpool.cpp \
rc2.cpp \
rc5.cpp \
rc6.cpp \
rdtables.cpp \
rijndael.cpp \
ripemd.cpp \
rng.cpp \
rsa.cpp \
rw.cpp \
safer.cpp \
salsa.cpp \
seal.cpp \
seed.cpp \
serpent.cpp \
sha.cpp \
sha3.cpp \
shacal2.cpp \
shark.cpp \
sharkbox.cpp \
simple.cpp \
skipjack.cpp \
socketft.cpp \
sosemanuk.cpp \
square.cpp \
squaretb.cpp \
strciphr.cpp \
tea.cpp \
tftables.cpp \
tiger.cpp \
tigertab.cpp \
trdlocal.cpp \
ttmac.cpp \
twofish.cpp \
vmac.cpp \
wait.cpp \
wake.cpp \
whrlpool.cpp \
winpipes.cpp \
xtr.cpp \
xtrcrypt.cpp \
zdeflate.cpp \
zinflate.cpp \
zlib.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/..
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS := -fexceptions
include $(BUILD_STATIC_LIBRARY)
Android.mk in the app
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := your lib
LOCAL_MODULE_FILENAME := lib…your lib
LOCAL_SRC_FILES := ../…..your files
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_CFLAGS += -std=c++11
LOCAL_LDLIBS += -latomic -fexceptions
LOCAL_WHOLE_STATIC_LIBRARIES += cryptopp_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,external/crypto)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS :=-std=c++11 -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1 -DOS_ANDROID
NDK_TOOLCHAIN_VERSION=4.8
APP_ABI := armeabi
Don’t forget change your config to 4.8
…like that …/android-ndk-r10/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/include