diff --git a/.gitignore b/.gitignore index 060fc59..a43646d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,13 @@ build/ android/.cxx +android/cmake_install.cmake +android/CMakeCache.txt +android/Makefile +android/CMakeFiles + +ios/Classes/cmake_install.cmake +ios/Classes/CMakeCache.txt +ios/Classes/cmake-* +ios/Classes/CMakeFiles + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index 53449da..65bb367 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -1,5 +1,5 @@ - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e956857..5e7e0e2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,26 +2,14 @@ + - - - - - - + - - - - - - - - - + - - + + @@ -58,11 +46,20 @@ - + - - + + + + + + + + + + + @@ -76,7 +73,7 @@ - + @@ -85,14 +82,26 @@ + + + + + + + + + argon2_ffi Plat - argon2i_hash_raw main + hashpwd + argon2i_hash_raw + print + printf @@ -101,24 +110,10 @@ - - @@ -211,10 +206,13 @@ + - + + + @@ -243,153 +241,40 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - + + - + - - + + diff --git a/README.md b/README.md index ce23e96..559295f 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,16 @@ # argon2_ffi -A new flutter plugin project. +Simple wrapper around argon2 for usage in https://github.com/authpass/kdbx.dart -## Getting Started +## To compile -This project is a starting point for a Flutter -[plug-in package](https://flutter.dev/developing-packages/), -a specialized package that includes platform-specific implementation code for -Android and/or iOS. +``` +cd ios/Classes +cmake --build . +``` -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. +### Using docker: + +``` +docker run -v `pwd`:/build --rm rikorose/gcc-cmake bash -c "cd /build/ios/Classes && cmake . && cmake --build ." +``` diff --git a/example/pubspec.lock b/example/pubspec.lock index 2a03c76..930ccc0 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -78,6 +78,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3" + ffi_helper: + dependency: transitive + description: + name: ffi_helper + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.0" flutter: dependency: "direct main" description: flutter diff --git a/ios/Classes/CMakeLists.txt b/ios/Classes/CMakeLists.txt new file mode 100644 index 0000000..7673e22 --- /dev/null +++ b/ios/Classes/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.4.1) # for example + +add_library(argon2_ffi + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + argon2_ffi.c + + argon2src/argon2.c + argon2src/core.c + argon2src/encoding.c + argon2src/genkat.c + argon2src/ref.c + argon2src/thread.c + argon2src/blake2/blake2b.c + ) +#set_target_properties(argon2_ffi PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/argon2_ffi.so) diff --git a/ios/Classes/argon2_ffi.c b/ios/Classes/argon2_ffi.c index 324363e..63c43d3 100644 --- a/ios/Classes/argon2_ffi.c +++ b/ios/Classes/argon2_ffi.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "argon2src/argon2.h" //extern "C" __attribute__((visibility("default"))) __attribute__((used)) @@ -13,78 +14,123 @@ int32_t native_add(int32_t x, int32_t y) { #define SALTLEN 16 #define PWD "password" +#define DEBUG 0 +#define PRINT_DEBUG(fmt, ...) \ + do { if (DEBUG) printf(fmt, ##__VA_ARGS__); } while (0) + // base64 encoding https://nachtimwald.com/2017/11/18/base64-encode-and-decode-in-c/ -size_t b64_encoded_size(size_t inlen) -{ - size_t ret; +size_t b64_encoded_size(size_t inlen) { + size_t ret; - ret = inlen; - if (inlen % 3 != 0) - ret += 3 - (inlen % 3); - ret /= 3; - ret *= 4; + ret = inlen; + if (inlen % 3 != 0) + ret += 3 - (inlen % 3); + ret /= 3; + ret *= 4; - return ret; + return ret; } const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -char *b64_encode(const unsigned char *in, size_t len) -{ - char *out; - size_t elen; - size_t i; - size_t j; - size_t v; - - if (in == NULL || len == 0) - return NULL; - - elen = b64_encoded_size(len); - out = malloc(elen+1); - out[elen] = '\0'; - - for (i=0, j=0; i> 18) & 0x3F]; - out[j+1] = b64chars[(v >> 12) & 0x3F]; - if (i+1 < len) { - out[j+2] = b64chars[(v >> 6) & 0x3F]; - } else { - out[j+2] = '='; - } - if (i+2 < len) { - out[j+3] = b64chars[v & 0x3F]; - } else { - out[j+3] = '='; - } - } - - return out; +char *b64_encode(const unsigned char *in, size_t len) { + char *out; + size_t elen; + size_t i; + size_t j; + size_t v; + + if (in == NULL || len == 0) + return NULL; + + elen = b64_encoded_size(len); + out = malloc(elen + 1); + out[elen] = '\0'; + + for (i = 0, j = 0; i < len; i += 3, j += 4) { + v = in[i]; + v = i + 1 < len ? v << 8 | in[i + 1] : v << 8; + v = i + 2 < len ? v << 8 | in[i + 2] : v << 8; + + out[j] = b64chars[(v >> 18) & 0x3F]; + out[j + 1] = b64chars[(v >> 12) & 0x3F]; + if (i + 1 < len) { + out[j + 2] = b64chars[(v >> 6) & 0x3F]; + } else { + out[j + 2] = '='; + } + if (i + 2 < len) { + out[j + 3] = b64chars[v & 0x3F]; + } else { + out[j + 3] = '='; + } + } + + return out; } -char* hashStuff(char* hashPwd) { +char *hashStuff(char *hashPwd) { uint8_t hash1[HASHLEN]; uint8_t salt[SALTLEN]; - memset( salt, 0x00, SALTLEN ); + memset(salt, 0x00, SALTLEN); - uint8_t *pwd = (uint8_t *)strdup(PWD); - uint32_t pwdlen = strlen((char *)pwd); + uint8_t *pwd = (uint8_t *) strdup(PWD); + uint32_t pwdlen = strlen((char *) pwd); uint32_t t_cost = 2; // 1-pass computation - uint32_t m_cost = (1<<16); // 64 mebibytes memory usage + uint32_t m_cost = (1 << 16); // 64 mebibytes memory usage uint32_t parallelism = 1; // number of threads and lanes - argon2i_hash_raw(t_cost, m_cost, parallelism, pwd, pwdlen, salt, SALTLEN, hash1, HASHLEN); + argon2i_hash_raw(t_cost, m_cost, parallelism, pwd, pwdlen, salt, SALTLEN, + hash1, HASHLEN); char *b64ret = b64_encode(hash1, HASHLEN); return b64ret; } +void debugBytes(uint8_t *bytes, int length) { + PRINT_DEBUG("length: %d -- ", length); + for (int i = 0; i < length; i++) { + PRINT_DEBUG("%02x ", bytes[i]); + } +} + +char *hp_argon2_hash(uint8_t *key, size_t keylen, uint8_t *salt, size_t saltlen, + uint32_t m_cost, + uint32_t t_cost /* iterations*/, uint32_t parallelism, + size_t hashlen, + uint8_t type, int32_t version) { + uint8_t hash1[hashlen]; + PRINT_DEBUG("keylen: %ld, saltlen: %ld, m_cost: %ld, t_cost: %ld, hashlen: %ld, type: %d, version: %02x\n", + keylen, saltlen, m_cost, t_cost, hashlen, type, version); + PRINT_DEBUG("key: "); + + debugBytes(key, keylen); + PRINT_DEBUG("\n"); + + PRINT_DEBUG("salt: "); + debugBytes(salt, saltlen); + PRINT_DEBUG("\n"); + + if (type == 0) { + PRINT_DEBUG("argon2d\n"); + argon2d_hash_raw(t_cost, m_cost, parallelism, key, keylen, salt, + saltlen, + hash1, hashlen); + } else if (type == 1) { + argon2i_hash_raw(t_cost, m_cost, parallelism, key, keylen, salt, + saltlen, + hash1, hashlen); + } + + PRINT_DEBUG("hash: "); + debugBytes(hash1, hashlen); + PRINT_DEBUG("\n"); + char *b64ret = b64_encode(hash1, hashlen); + return b64ret; +} + diff --git a/lib/argon2_ffi.dart b/lib/argon2_ffi.dart index 9ae723b..50d0fc3 100644 --- a/lib/argon2_ffi.dart +++ b/lib/argon2_ffi.dart @@ -25,7 +25,4 @@ class Argon2Ffi { } int addIt(int x, int y) => _nativeAdd(x, y); - - String hashStuff(String password) => - Utf8.fromUtf8(_hashStuff(Utf8.toUtf8(password))); } diff --git a/pubspec.lock b/pubspec.lock index 9ae5c3a..901eb56 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -64,6 +64,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3" + ffi_helper: + dependency: "direct main" + description: + name: ffi_helper + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.0" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index c223c94..16963c9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,7 @@ dependencies: flutter: sdk: flutter ffi: ^0.1.3 + ffi_helper: ^1.4.0 dev_dependencies: