Fix JNI write-back, bad V3 api usage, duplicate length passing.
This commit is contained in:
parent
c08d3a0e8b
commit
bc0ffbd552
@ -29,7 +29,7 @@ library {
|
||||
//TODO: Cross-compiling, blocked by: https://github.com/gradle/gradle-native/issues/169
|
||||
//setTargets( "arm", "arm64", "x86-64", "x86" )
|
||||
eachPlatform {
|
||||
cppCompiler.withArguments { addAll( ["-x", "c", "-std=c11", "-DMPW_SODIUM=1"] ) }
|
||||
cppCompiler.withArguments { addAll( ["-x", "c", "-std=c11", "-Werror", "-DMPW_SODIUM=1"] ) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,16 @@
|
||||
#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); */
|
||||
/** native int _scrypt(byte[] passwd, byte[] salt, int N, int r, int p, byte[] buf); */
|
||||
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, jbyteArray salt, jint N, jint r, jint p, jbyteArray buf) {
|
||||
|
||||
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,
|
||||
const size_t keyLength = (*env)->GetArrayLength( env, buf );
|
||||
const uint8_t *key = mpw_kdf_scrypt( keyLength,
|
||||
(uint8_t *)passwdBytes, (size_t)(*env)->GetArrayLength( env, passwd ),
|
||||
(uint8_t *)saltBytes, (size_t)(*env)->GetArrayLength( env, salt ),
|
||||
(uint64_t)N, (uint32_t)r, (uint32_t)p );
|
||||
(*env)->ReleaseByteArrayElements( env, passwd, passwdBytes, JNI_ABORT );
|
||||
(*env)->ReleaseByteArrayElements( env, salt, saltBytes, JNI_ABORT );
|
||||
@ -17,6 +20,10 @@ JNIEXPORT jint JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1scryp
|
||||
if (!key)
|
||||
return ERR;
|
||||
|
||||
memcpy( buf, key, buflen );
|
||||
jbyte *bufBytes = (*env)->GetByteArrayElements( env, buf, NULL );
|
||||
memcpy( bufBytes, key, keyLength );
|
||||
(*env)->ReleaseByteArrayElements( env, buf, bufBytes, JNI_OK );
|
||||
mpw_free( &key, keyLength );
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#undef __cplusplus
|
||||
#include <jni.h>
|
||||
/* Header for class com_lyndir_masterpassword_impl_MPAlgorithmV0 */
|
||||
|
||||
@ -13,10 +12,10 @@ extern "C" {
|
||||
/*
|
||||
* Class: com_lyndir_masterpassword_impl_MPAlgorithmV0
|
||||
* Method: _scrypt
|
||||
* Signature: ([BI[BIIII[BI)I
|
||||
* Signature: ([B[BIII[B)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1scrypt
|
||||
(JNIEnv *, jobject, jbyteArray, jint, jbyteArray, jint, jint, jint, jint, jbyteArray, jint);
|
||||
(JNIEnv *, jobject, jbyteArray, jbyteArray, jint, jint, jint, jbyteArray);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -28,3 +28,11 @@ processResources {
|
||||
}
|
||||
} )
|
||||
}
|
||||
|
||||
compileJava {
|
||||
doLast {
|
||||
ant.javah( class: 'com.lyndir.masterpassword.impl.MPAlgorithmV0',
|
||||
outputFile: '../../c/src/mpw-jni.h',
|
||||
classpath: files( sourceSets.main.compileClasspath, sourceSets.main.output ).asPath )
|
||||
}
|
||||
}
|
||||
|
@ -90,15 +90,13 @@ public class MPAlgorithmV0 extends MPAlgorithm {
|
||||
@Nullable
|
||||
protected byte[] scrypt(final byte[] secret, final byte[] salt, final int keySize) {
|
||||
byte[] buffer = new byte[keySize];
|
||||
if (_scrypt(
|
||||
secret, secret.length, salt, salt.length,
|
||||
scrypt_N(), scrypt_r(), scrypt_p(), buffer, buffer.length ) < 0)
|
||||
if (_scrypt( secret, salt, scrypt_N(), scrypt_r(), scrypt_p(), buffer ) < 0)
|
||||
return null;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected native int _scrypt(byte[] passwd, int passwdlen, byte[] salt, int saltlen, int N, int r, int p, byte[] buf, int buflen);
|
||||
protected native int _scrypt(byte[] passwd, byte[] salt, int N, int r, int p, byte[] buf);
|
||||
|
||||
@Override
|
||||
public byte[] siteKey(final byte[] masterKey, final String siteName, UnsignedInteger siteCounter,
|
||||
|
@ -50,7 +50,7 @@ public class MPAlgorithmV3 extends MPAlgorithmV2 {
|
||||
logger.trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%d, r=%d, p=%d )",
|
||||
scrypt_N(), scrypt_r(), scrypt_p() );
|
||||
byte[] masterPasswordBytes = toBytes( masterPassword );
|
||||
byte[] masterKey = scrypt( masterKeySalt, masterPasswordBytes, mpw_dkLen() );
|
||||
byte[] masterKey = scrypt( masterPasswordBytes, masterKeySalt, mpw_dkLen() );
|
||||
Arrays.fill( masterKeySalt, (byte) 0 );
|
||||
Arrays.fill( masterPasswordBytes, (byte) 0 );
|
||||
if (masterKey == null)
|
||||
|
@ -60,7 +60,7 @@ public class MPTests {
|
||||
}
|
||||
|
||||
public Case getCase(final String identifier) {
|
||||
for (final Case testCase : getCases())
|
||||
for (final Case testCase : cases)
|
||||
if (identifier.equals( testCase.getIdentifier() ))
|
||||
return testCase;
|
||||
|
||||
|
@ -39,7 +39,7 @@ public class MPMasterKeyTest {
|
||||
throws Exception {
|
||||
|
||||
testSuite = new MPTestSuite();
|
||||
//testSuite.getTests().addFilters( "v0" );
|
||||
//testSuite.getTests().addFilters( "v3_type_maximum" );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user