From 81f7616bd01fad3b08dca12b1801ea2537bc508d Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 28 May 2018 23:00:05 -0400 Subject: [PATCH] Initial integration of JNI with C implementation. --- src/mpw-algorithm_v0.c | 3 ++- src/mpw-algorithm_v3.c | 3 ++- src/mpw-jni.c | 18 ++++++++++++++++-- src/mpw-jni.h | 1 + src/mpw-util.c | 6 +++--- src/mpw-util.h | 2 +- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/mpw-algorithm_v0.c b/src/mpw-algorithm_v0.c index b1f237d..49299d8 100644 --- a/src/mpw-algorithm_v0.c +++ b/src/mpw-algorithm_v0.c @@ -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 ) ); diff --git a/src/mpw-algorithm_v3.c b/src/mpw-algorithm_v3.c index a3caebd..8dfb299 100644 --- a/src/mpw-algorithm_v3.c +++ b/src/mpw-algorithm_v3.c @@ -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 ) ); diff --git a/src/mpw-jni.c b/src/mpw-jni.c index 720c6df..e40a6b9 100644 --- a/src/mpw-jni.c +++ b/src/mpw-jni.c @@ -1,8 +1,22 @@ +#include + #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; } diff --git a/src/mpw-jni.h b/src/mpw-jni.h index 990e344..0975630 100644 --- a/src/mpw-jni.h +++ b/src/mpw-jni.h @@ -1,4 +1,5 @@ /* DO NOT EDIT THIS FILE - it is machine generated */ +#undef __cplusplus #include /* Header for class com_lyndir_masterpassword_impl_MPAlgorithmV0 */ diff --git a/src/mpw-util.c b/src/mpw-util.c index 1eca80a..4f30fe1 100644 --- a/src/mpw-util.c +++ b/src/mpw-util.c @@ -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; } diff --git a/src/mpw-util.h b/src/mpw-util.h index d0d4da9..15b0422 100644 --- a/src/mpw-util.h +++ b/src/mpw-util.h @@ -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. */