From 80abc3bc4a73f3ba6ffd40b1a03de0cc4d01e3c9 Mon Sep 17 00:00:00 2001 From: MoroccanMalinois Date: Thu, 5 Jan 2017 01:11:05 +0000 Subject: Build wallet with Android NDK --- src/common/int-util.h | 14 +++++++++++++- src/crypto/CMakeLists.txt | 11 +++++++++++ src/crypto/oaes_lib.c | 6 +++--- src/crypto/slow-hash.c | 11 ++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/common/int-util.h b/src/common/int-util.h index e9eddee9c..9ac20e582 100644 --- a/src/common/int-util.h +++ b/src/common/int-util.h @@ -36,6 +36,10 @@ #include #include +#if defined(__ANDROID__) +#include +#endif + #if defined(_MSC_VER) #include @@ -138,16 +142,24 @@ static inline uint32_t ident32(uint32_t x) { return x; } static inline uint64_t ident64(uint64_t x) { return x; } #ifndef __OpenBSD__ +# if defined(__ANDROID__) && defined(__swap32) && !defined(swap32) +# define swap32 __swap32 +# elif !defined(swap32) static inline uint32_t swap32(uint32_t x) { x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8); return (x << 16) | (x >> 16); } +# endif +# if defined(__ANDROID__) && defined(__swap64) && !defined(swap64) +# define swap64 __swap64 +# elif !defined(swap64) static inline uint64_t swap64(uint64_t x) { x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8); x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16); return (x << 32) | (x >> 32); } -#endif +# endif +#endif /* __OpenBSD__ */ #if defined(__GNUC__) #define UNUSED __attribute__((unused)) diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index 9d83caca8..1e037a07d 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -89,3 +89,14 @@ if (ARM) PROPERTY COMPILE_DEFINITIONS "NO_OPTIMIZED_MULTIPLY_ON_ARM") endif() endif() + +# Because of the way Qt works on android with JNI, the code does not live in the main android thread +# So this code runs with a 1 MB default stack size. +# This will force the use of the heap for the allocation of the scratchpad +if (ANDROID) + if( BUILD_GUI_DEPS ) + add_definitions(-DFORCE_USE_HEAP=1) + endif() +endif() + + diff --git a/src/crypto/oaes_lib.c b/src/crypto/oaes_lib.c index f054a16f4..0afec6212 100644 --- a/src/crypto/oaes_lib.c +++ b/src/crypto/oaes_lib.c @@ -39,8 +39,8 @@ #include #endif -// FreeBSD, and OpenBSD also don't need timeb.h -#if !defined(__FreeBSD__) && !defined(__OpenBSD__) +// ANDROID, FreeBSD, and OpenBSD also don't need timeb.h +#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__ANDROID__) #include #else #include @@ -499,7 +499,7 @@ static void oaes_get_seed( char buf[RANDSIZ + 1] ) #else static uint32_t oaes_get_seed(void) { - #if !defined(__FreeBSD__) && !defined(__OpenBSD__) + #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__ANDROID__) struct timeb timer; struct tm *gmTimer; char * _test = NULL; diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c index 66d9ca5d9..43b9619f3 100644 --- a/src/crypto/slow-hash.c +++ b/src/crypto/slow-hash.c @@ -1052,7 +1052,6 @@ STATIC INLINE void xor_blocks(uint8_t* a, const uint8_t* b) void cn_slow_hash(const void *data, size_t length, char *hash) { - uint8_t long_state[MEMORY]; uint8_t text[INIT_SIZE_BYTE]; uint8_t a[AES_BLOCK_SIZE]; uint8_t b[AES_BLOCK_SIZE]; @@ -1070,6 +1069,13 @@ void cn_slow_hash(const void *data, size_t length, char *hash) hash_extra_blake, hash_extra_groestl, hash_extra_jh, hash_extra_skein }; +#ifndef FORCE_USE_HEAP + uint8_t long_state[MEMORY]; +#else + uint8_t *long_state = NULL; + long_state = (uint8_t *)malloc(MEMORY); +#endif + hash_process(&state.hs, data, length); memcpy(text, state.init, INIT_SIZE_BYTE); @@ -1129,6 +1135,9 @@ void cn_slow_hash(const void *data, size_t length, char *hash) memcpy(state.init, text, INIT_SIZE_BYTE); hash_permutation(&state.hs); extra_hashes[state.hs.b[0] & 3](&state, 200, hash); +#ifdef FORCE_USE_HEAP + free(long_state); +#endif } #endif /* !aarch64 || !crypto */ -- cgit v1.2.3