Use native Argon2 implementation

This commit is contained in:
Chih-Hsuan Yen
2019-07-16 20:42:02 +08:00
parent e081a6aa2c
commit 7d1eeb1e34
9 changed files with 89 additions and 0 deletions

3
.gitmodules vendored
View File

@@ -4,3 +4,6 @@
[submodule "src/netftpandroid"]
path = src/netftpandroid
url = https://github.com/PhilippC/netftpandroid.git
[submodule "src/java/argon2/phc-winner-argon2"]
path = src/java/argon2/phc-winner-argon2
url = https://github.com/P-H-C/phc-winner-argon2

View File

@@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace KeePassLib.Cryptography.KeyDerivation
@@ -127,8 +128,44 @@ namespace KeePassLib.Cryptography.KeyDerivation
byte[] pbSecretKey = p.GetByteArray(ParamSecretKey);
byte[] pbAssocData = p.GetByteArray(ParamAssocData);
if (pbSecretKey != null) {
throw new ArgumentOutOfRangeException("Unsupported configuration: non-null pbSecretKey");
}
if (pbAssocData != null) {
throw new ArgumentOutOfRangeException("Unsupported configuration: non-null pbAssocData");
}
/*
byte[] pbRet = Argon2d(pbMsg, pbSalt, uPar, uMem, uIt,
32, v, pbSecretKey, pbAssocData);
*/
IntPtr msgPtr = Marshal.AllocHGlobal(pbMsg.Length);
IntPtr saltPtr = Marshal.AllocHGlobal(pbSalt.Length);
IntPtr retPtr = Marshal.AllocHGlobal(32);
Marshal.Copy(pbMsg, 0, msgPtr, pbMsg.Length);
Marshal.Copy(pbSalt, 0, saltPtr, pbSalt.Length);
const UInt32 Argon2_d = 0;
int ret = argon2_hash(
(UInt32)uIt, (UInt32)(uMem / 1024), uPar,
msgPtr, (IntPtr)pbMsg.Length,
saltPtr, (IntPtr)pbSalt.Length,
retPtr, (IntPtr)32,
(IntPtr)0, (IntPtr)0, Argon2_d, v);
if (ret != 0) {
throw new Exception("argon2_hash failed with " + ret);
}
byte[] pbRet = new byte[32];
Marshal.Copy(retPtr, pbRet, 0, 32);
Marshal.FreeHGlobal(msgPtr);
Marshal.FreeHGlobal(saltPtr);
Marshal.FreeHGlobal(retPtr);
if(uMem > (100UL * 1024UL * 1024UL)) GC.Collect();
return pbRet;
@@ -143,5 +180,14 @@ namespace KeePassLib.Cryptography.KeyDerivation
MaxIterations, uMilliseconds, true);
return p;
}
[DllImport("argon2")]
static extern int argon2_hash(
UInt32 t_cost, UInt32 m_cost, UInt32 parallelism,
IntPtr pwd, IntPtr pwdlen,
IntPtr salt, IntPtr saltlen,
IntPtr hash, IntPtr hashlen,
IntPtr encoded, IntPtr encodedlen,
UInt32 type, UInt32 version);
}
}

View File

@@ -1,6 +1,11 @@
#!/bin/bash
set -e
echo '*****************************************'
echo '******* Building native libraries *******'
echo '*****************************************'
./build-native.sh
echo '*****************************************'
echo '********** Building Java parts **********'
echo '*****************************************'

View File

@@ -0,0 +1,6 @@
#!/bin/bash
set -e
pushd ../java/argon2
ndk-build
popd

1
src/java/argon2/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/libs/

View File

@@ -0,0 +1,18 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := argon2
LOCAL_SRC_FILES := \
../phc-winner-argon2/src/argon2.c \
../phc-winner-argon2/src/core.c \
../phc-winner-argon2/src/blake2/blake2b.c \
../phc-winner-argon2/src/thread.c \
../phc-winner-argon2/src/encoding.c \
../phc-winner-argon2/src/ref.c
LOCAL_CFLAGS += -I $(LOCAL_PATH)/../phc-winner-argon2/include
include $(BUILD_SHARED_LIBRARY)

View File

@@ -0,0 +1,3 @@
APP_OPTIM := release
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_PLATFORM := android-16

View File

@@ -1920,6 +1920,12 @@
<ItemGroup>
<AndroidAsset Include="Assets\publicsuffix.txt" />
</ItemGroup>
<ItemGroup>
<AndroidNativeLibrary Include="..\java\argon2\libs\armeabi-v7a\libargon2.so" />
<AndroidNativeLibrary Include="..\java\argon2\libs\arm64-v8a\libargon2.so" />
<AndroidNativeLibrary Include="..\java\argon2\libs\x86\libargon2.so" />
<AndroidNativeLibrary Include="..\java\argon2\libs\x86_64\libargon2.so" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">