2
0

Initial integration of JNI with C implementation.

This commit is contained in:
Maarten Billemont 2018-05-28 23:00:05 -04:00
parent 5035c52846
commit 728a4486d3
8 changed files with 60 additions and 42 deletions

View File

@ -70,7 +70,8 @@ static MPMasterKey mpw_masterKey_v0(
// Calculate the master key.
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize,
(uint8_t *)masterPassword, strlen( masterPassword ), masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
mpw_free( &masterKeySalt, masterKeySaltSize );
if (!masterKey) {
err( "Could not derive master key: %s", strerror( errno ) );

View File

@ -62,7 +62,8 @@ static MPMasterKey mpw_masterKey_v3(
// Calculate the master key.
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize,
(uint8_t *)masterPassword, strlen( masterPassword ), masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
mpw_free( &masterKeySalt, masterKeySaltSize );
if (!masterKey) {
err( "Could not derive master key: %s", strerror( errno ) );

View File

@ -1,8 +1,22 @@
#include <string.h>
#include "mpw-jni.h"
#include "mpw-util.h"
/** native int _scrypt(byte[] passwd, int passwdlen, byte[] salt, int saltlen, int N, int r, int p, byte[] buf, int buflen); */
JNIEXPORT jint JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1scrypt(JNIEnv *env, jobject obj,
jbyteArray passwd, jint passwdlen, jbyteArray salt, jint saltlen, jint N, jint r, jint p, jbyteArray buf, jint buflen) {
jbyteArray passwd, jint passwdlen, jbyteArray salt, jint saltlen, jint N, jint r, jint p, jbyteArray buf, jint buflen) {
return -2;
jbyte *passwdBytes = (*env)->GetByteArrayElements( env, passwd, NULL );
jbyte *saltBytes = (*env)->GetByteArrayElements( env, salt, NULL );
const uint8_t *key = mpw_kdf_scrypt( (size_t)buflen, (uint8_t *)passwdBytes, (size_t)passwdlen, (uint8_t *)saltBytes, (size_t)saltlen,
(uint64_t)N, (uint32_t)r, (uint32_t)p );
(*env)->ReleaseByteArrayElements( env, passwd, passwdBytes, JNI_ABORT );
(*env)->ReleaseByteArrayElements( env, salt, saltBytes, JNI_ABORT );
if (!key)
return ERR;
memcpy( buf, key, buflen );
return OK;
}

View File

@ -1,4 +1,5 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#undef __cplusplus
#include <jni.h>
/* Header for class com_lyndir_masterpassword_impl_MPAlgorithmV0 */

View File

@ -169,10 +169,10 @@ bool __mpw_free_strings(char **strings, ...) {
return success;
}
uint8_t const *mpw_kdf_scrypt(const size_t keySize, const char *secret, const uint8_t *salt, const size_t saltSize,
uint8_t const *mpw_kdf_scrypt(const size_t keySize, const uint8_t *secret, const size_t secretSize, const uint8_t *salt, const size_t saltSize,
uint64_t N, uint32_t r, uint32_t p) {
if (!secret || !salt)
if (!secret || !salt || !secretSize || !saltSize)
return NULL;
uint8_t *key = malloc( keySize );
@ -185,7 +185,7 @@ uint8_t const *mpw_kdf_scrypt(const size_t keySize, const char *secret, const ui
return NULL;
}
#elif MPW_SODIUM
if (crypto_pwhash_scryptsalsa208sha256_ll( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize ) != 0) {
if (crypto_pwhash_scryptsalsa208sha256_ll( secret, secretSize, salt, saltSize, N, r, p, key, keySize ) != 0) {
mpw_free( &key, keySize );
return NULL;
}

View File

@ -156,7 +156,7 @@ bool __mpw_free_strings(
/** Derive a key from the given secret and salt using the scrypt KDF.
* @return A new keySize allocated buffer containing the key. */
uint8_t const *mpw_kdf_scrypt(
const size_t keySize, const char *secret, const uint8_t *salt, const size_t saltSize,
const size_t keySize, const uint8_t *secret, const size_t secretSize, const uint8_t *salt, const size_t saltSize,
uint64_t N, uint32_t r, uint32_t p);
/** Derive a subkey from the given key using the blake2b KDF.
* @return A new keySize allocated buffer containing the key. */

View File

@ -25,15 +25,14 @@
DA1554ED20B3928E00EA92C5 /* mpw-util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mpw-util.h"; sourceTree = "<group>"; };
DA1554EE20B3928E00EA92C5 /* mpw-marshal-util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mpw-marshal-util.h"; sourceTree = "<group>"; };
DA1554EF20B3928E00EA92C5 /* mpw-algorithm_v3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "mpw-algorithm_v3.c"; sourceTree = "<group>"; };
DA1554F120B392A100EA92C5 /* jni_mpw.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jni_mpw.h; sourceTree = "<group>"; };
DA1554F220B392A100EA92C5 /* jni_mpw.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = jni_mpw.c; sourceTree = "<group>"; };
DA1F44B020BCF0C200957B45 /* mpw-jni.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mpw-jni.h"; sourceTree = "<group>"; };
DA1F44B120BCF0C200957B45 /* mpw-jni.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "mpw-jni.c"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXGroup section */
DA1554D220B3924000EA92C5 = {
isa = PBXGroup;
children = (
DA1554F020B392A100EA92C5 /* jni */,
DA1554DD20B3928E00EA92C5 /* core */,
);
sourceTree = "<group>";
@ -42,36 +41,28 @@
isa = PBXGroup;
children = (
DA1554DE20B3928E00EA92C5 /* aes.c */,
DA1554DF20B3928E00EA92C5 /* mpw-algorithm.h */,
DA1554E020B3928E00EA92C5 /* base64.h */,
DA1554E120B3928E00EA92C5 /* mpw-marshal.c */,
DA1554E220B3928E00EA92C5 /* mpw-algorithm_v2.c */,
DA1554E320B3928E00EA92C5 /* mpw-types.c */,
DA1554E420B3928E00EA92C5 /* mpw-marshal-util.c */,
DA1554E520B3928E00EA92C5 /* mpw-util.c */,
DA1554E620B3928E00EA92C5 /* mpw-algorithm_v1.c */,
DA1554E720B3928E00EA92C5 /* aes.h */,
DA1554E820B3928E00EA92C5 /* mpw-marshal.h */,
DA1554E920B3928E00EA92C5 /* base64.c */,
DA1554EA20B3928E00EA92C5 /* mpw-algorithm.c */,
DA1554E020B3928E00EA92C5 /* base64.h */,
DA1554EB20B3928E00EA92C5 /* mpw-algorithm_v0.c */,
DA1554EC20B3928E00EA92C5 /* mpw-types.h */,
DA1554ED20B3928E00EA92C5 /* mpw-util.h */,
DA1554EE20B3928E00EA92C5 /* mpw-marshal-util.h */,
DA1554E620B3928E00EA92C5 /* mpw-algorithm_v1.c */,
DA1554E220B3928E00EA92C5 /* mpw-algorithm_v2.c */,
DA1554EF20B3928E00EA92C5 /* mpw-algorithm_v3.c */,
DA1554EA20B3928E00EA92C5 /* mpw-algorithm.c */,
DA1554DF20B3928E00EA92C5 /* mpw-algorithm.h */,
DA1F44B120BCF0C200957B45 /* mpw-jni.c */,
DA1F44B020BCF0C200957B45 /* mpw-jni.h */,
DA1554E420B3928E00EA92C5 /* mpw-marshal-util.c */,
DA1554EE20B3928E00EA92C5 /* mpw-marshal-util.h */,
DA1554E120B3928E00EA92C5 /* mpw-marshal.c */,
DA1554E820B3928E00EA92C5 /* mpw-marshal.h */,
DA1554E320B3928E00EA92C5 /* mpw-types.c */,
DA1554EC20B3928E00EA92C5 /* mpw-types.h */,
DA1554E520B3928E00EA92C5 /* mpw-util.c */,
DA1554ED20B3928E00EA92C5 /* mpw-util.h */,
);
name = core;
path = ../../core/c;
sourceTree = "<group>";
};
DA1554F020B392A100EA92C5 /* jni */ = {
isa = PBXGroup;
children = (
DA1554F120B392A100EA92C5 /* jni_mpw.h */,
DA1554F220B392A100EA92C5 /* jni_mpw.c */,
);
name = jni;
path = ../../core/java/algorithm/src/mpw/c;
path = ../core/c/src;
sourceTree = "<group>";
};
/* End PBXGroup section */
@ -125,32 +116,42 @@
DA1554D820B3924000EA92C5 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/include/**",
);
};
name = Debug;
};
DA1554D920B3924000EA92C5 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/include/**",
);
};
name = Release;
};
DA1554DB20B3924000EA92C5 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/include/**",
../lib/libsodium/src/libsodium/include,
);
JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home;
OTHER_CFLAGS = (
"-DMPW_SODIUM=1",
"-DMPW_CPERCIVA=0",
);
};
name = Debug;
};
DA1554DC20B3924000EA92C5 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/include/**",
../lib/libsodium/src/libsodium/include,
);
JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home;
OTHER_CFLAGS = (
"-DMPW_SODIUM=1",
"-DMPW_CPERCIVA=0",
);
};
name = Release;
};

View File

@ -2,7 +2,7 @@
<Workspace
version = "1.0">
<FileRef
location = "group:MasterPassword-JNI/MasterPassword-JNI.xcodeproj">
location = "group:MasterPassword-JNI.xcodeproj">
</FileRef>
<FileRef
location = "group:MasterPassword-iOS.xcodeproj">