Crypto++ module is too big.

721 views
Skip to first unread message

SeungYoup Lee

unread,
Sep 18, 2014, 11:20:59 PM9/18/14
to cryptop...@googlegroups.com

I have a problem. 

I want to only RSA module in Android ndk enviroment.
When I build Crypto++ ..(so) ..  but module size too big 
(cryptopp module, stl module, user module = 3.. too many..) 

I visits many site.. but i can't small build..
(also i sam it. .. http://morgwai.pl/ndkTutorial/ )

I want to build crypto++ static library (because reduce module size..) 
How can I start... ? 

Please help me..


Reference.. 


If you want to use crypto++ (and there are many good reasons you'd want to) this is probably a scenario where you're just best off letting the linkers do their job. I was concerned about this some time ago, and I could not do any better by hand than the optimizing linkers could.

I confirmed this by dusting off my old test app that uses crypto++ to generate a new random RSA key, sign a string and verify that string. Here are the numbers I see:

  • libcryptopp.a - crypto++ built for release as a static library using clang++ against the iOS SDK 5.0. There was no special attmept to minimize size, just built with -fvisibility=hidden -fvisibility-inlines-hidden and -Os:22.5MB

  • Empty app from the default iOS single view template, built with -Os: 34KB

  • The same empty app with "self test" code added that generates a keypair, signs (and therefore hashes) a string using RSA/SHA256, hex encodes it, prints the signature, decodes the signature and verifies the signature over the original string, built with -Os against the libcryptopp.a from my first bullet above: 389KB

The linker seems to be doing a good job here. If you're seeing something drastically different, make sure you're really looking at release binaries.

Jeffrey Walton

unread,
Sep 20, 2014, 1:52:40 PM9/20/14
to cryptop...@googlegroups.com

On Thursday, September 18, 2014 11:20:59 PM UTC-4, SeungYoup Lee wrote:

I want to only RSA module in Android ndk enviroment.
When I build Crypto++ ..(so) ..  but module size too big 
(cryptopp module, stl module, user module = 3.. too many..) 

I visits many site.. but i can't small build..
(also i sam it. .. http://morgwai.pl/ndkTutorial/ )

I want to build crypto++ static library (because reduce module size..) 
How can I start... ? 

Please help me..
I think you are doing many things you should be. The issue is the Crypto++ shared object has a lot of functionality, and you don't want or need it.

Here's how I would approach it:

1. Compile Crypto++ with -ffunction-sections -fdata-sections
2. Create a wrapped shared object, compile with -Os
3. Wrapper uses -fvisibility=hidden and -Wl,--exclude-libs,ALL
4  Wrapper links against libcryptopp.a
5. Run 'strip' after building you library

I believe strip uses --strip-unneeded. I think you can get more aggressive (like --strip-debug and --strip-all) . But the downside is your crash reports will become useless.

You should add -ffunction-sections, -fdata-sections, -fvisibility=hidden and -Wl,--exclude-libs,ALL to CFLAGS and CXXFLAGS.

There's also a page on the Crypto++ wiki covering Android at http://www.cryptopp.com/wiki/Android_%28Command_Line%29 . The wiki is still having some problems (like thumbnail images not available), but you can read it.
 

SeungYoup Lee

unread,
Sep 28, 2014, 10:50:30 AM9/28/14
to cryptop...@googlegroups.com


Hi ! First of all .. I very appreciate with your answer ! 
I don't expect answer .. but I saw your answer and I moved it. 

I saw your other article about crypto++ and I learned a lot of things ! 





CASE1 : Vmware + Ubuntu 14.0 + NDK 10r 

I saw this site. but it is very difficult for me.


So, I download source file and I unzip cryptopp-android-14.zip 

After unzip file (cryptopp-android-14.zip) i get libcryptopp.a file. 

1) I create Android Application Project in Eclipse ( also I install CDT Plugin ) 
2) I create jni folder and copy cryptopp header file (in cryptopp-android-14.zip include files) 
3) I put libcryptopp.a file in libs/libcryptopp.a
4) I reference this site http://morgwai.pl/ndkTutorial/ 

5) download NdkTutorial project and copy src folder, AndroidmManifest.xml, res folder 
(if you error file in res folder just remove!, and R file error appropreate change) 

6) I create my module in jni folder named crypt_user.cpp 


extern "C" {

JNIEXPORT jstring JNICALL Java_pl_morgwai_ndktutorial_Native_fun(JNIEnv *env,
jobject) {

string readSignater;
string publicFileName = "publicKey";
string message = "RSA System Test";

try {
AutoSeededRandomPool rng;
InvertibleRSAFunction parameters;
parameters.GenerateRandomWithKeySize(rng, 1024);
RSA::PublicKey publicKey(parameters);
.... 
} // try

catch (CryptoPP::Exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

return (env)->NewStringUTF("Hello JNI!!!!!");

}
}


7) Edit Android.mk file 

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS)
LOCAL_MODULE := static_crypt          # anything.. 
LOCAL_SRC_FILES := ../libs/libcryptopp.a        #  libs/libcryptopp.a (in cryptopp-android-14.zip include files) 
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS) 

LOCAL_MODULE    := mymodule
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/cryptopp \

FILE_LIST := \
$(wildcard $(LOCAL_PATH)/*.c*) \
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)  

# Add ..
LOCAL_CFLAGS := -Os
LOCAL_CFLAGS += -ffunction-sections
LOCAL_CFLAGS += -fdata-sections
LOCAL_CFLAGS += -fvisibility=hidden
LOCAL_CFLAGS += -Wl
LOCAL_CFLAGS += --exclude-libs
LOCAL_STATIC_LIBRARIES := static_crypt 

include $(BUILD_SHARED_LIBRARY) 


8) Edit Application.mk 
APP_ABI := armeabi 
APP_STL := stlport_static 
APP_PLATFORM := android-8
APP_CFLAGS += -Wno-error=format-security
APP_CFLAGS += -fexceptions \
-fpermissive \
-frtti



9) build command ndk-build 
move project position 
ex)   /workspace/Staticbuild/jni
and ndk-build 


$ ndk-build
[armeabi] SharedLibrary  : libmymodule.so
[armeabi] Install        : libmymodule.so 


10) After build the module size 5KB !! 
11) For test, I Edit 

package pl.morgwai.ndktutorial;


public class Native {

public static final String LOG_TAG = Native.class.getSimpleName();

static {
System.loadLibrary("mymodule");
}

public native String fun();
}







CASE2 : window  + NDK 10r 

I download Crypto++ 5.6.2 and build in eclipse with window enviroment 
( setting :  install cygwin for window & install eclipse CDT plugin ) 

Environment
- Eclipse with CDT
- window 
- cygwin 
- NDK, SDK (android) 


First, I build raw crypto++ source code static library. 
1) Create Android Application Project in Eclipse
2) Create JNI Folder and copy Crypto++ source code all (Specific position) 
3) In Eclipse project, click right click . new -> convert c++ project ( select static library option) 
4) create Android.mk file and Application.mk file 


< Application.mk > 
APP_ABI := armeabi
APP_STL := stlport_static
APP_CFLAGS += -Wno-error=format-security
APP_CFLAGS += -fexceptions \
-fpermissive

< Android.mk > 
# sattic library build file 
include $(CLEAR_VARS)
# module name ramdom .. (you want) 
LOCAL_MODULE    := random name 
# put crptopp all file (jni/cryptopp) 
FILE_LIST := $(wildcard $(LOCAL_PATH)/cryptopp/*.c*)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) 

include $(BUILD_STATIC_LIBRARY) 

# dummy so file 
include $(CLEAR_VARS)
LOCAL_MODULE    := garbage
LOCAL_STATIC_LIBRARIES :=  random name 
include $(BUILD_SHARED_LIBRARY)

5) Chaing eclipse project setting properties 
-C/C++ build : builder Settings - build command : ndk-build.cmd
-C/C++ General : Path and symbols - Include Setting position toolchain.. 

6) build and get libcrypto.a ( project - obj - local - armeabi - XXX.a) 




Second, I create another project 
1) Create Android Application Project in Eclipse
2) Create Jni Folder and copyt crypto++ header file 
3) And write my code under jni folder ( reference crypto++ api ) + put libcrypto.a file 
4) Edit Android.mk, Application.mk and build so library 


LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS)
LOCAL_MODULE := cryptopplibrary
# static build library position
LOCAL_SRC_FILES := ../libs/libcrpytopp.a        
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) 
LOCAL_LDLIBS := -llog -lz
LOCAL_MODULE    := temp
LOCAL_CFLAGS := -Os
LOCAL_CFLAGS += -ffunction-sections
LOCAL_CFLAGS += -fdata-sections
LOCAL_CFLAGS += -fvisibility=hidden
LOCAL_CFLAGS += -Wl
LOCAL_CFLAGS += --exclude-libs

LOCAL_C_INCLUDES := \
$(LOCAL_PATH) \
$(LOCAL_PATH)/cryptopp \

FILE_LIST := \
$(wildcard $(LOCAL_PATH)/*.c*) \
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)  
LOCAL_STATIC_LIBRARIES := cryptopplibrary
include $(BUILD_SHARED_LIBRARY) 

4) After build , I create my crypto library (only i want function ) 
The size is down!! 


Jeffrey Walton

unread,
Aug 27, 2018, 9:41:09 AM8/27/18
to Crypto++ Users


On Thursday, September 18, 2014 at 11:20:59 PM UTC-4, SeungYoup Lee wrote:

I have a problem. 

I want to only RSA module in Android ndk enviroment.
When I build Crypto++ ..(so) ..  but module size too big 
(cryptopp module, stl module, user module = 3.. too many..) 

I visits many site.. but i can't small build..
(also i sam it. .. http://morgwai.pl/ndkTutorial/ )

I want to build crypto++ static library (because reduce module size..) 
How can I start... ?

This is a bit late but... We added a wiki page on creating wrapper DLLs. Also see https://www.cryptopp.com/wiki/Wrapper_DLL .

Jeff
Reply all
Reply to author
Forward
0 new messages