2
0

Fix JNI write-back, bad V3 api usage, duplicate length passing.

This commit is contained in:
Maarten Billemont 2018-06-03 17:58:24 -04:00
parent c08d3a0e8b
commit bc0ffbd552
8 changed files with 27 additions and 15 deletions

View File

@ -29,7 +29,7 @@ library {
//TODO: Cross-compiling, blocked by: https://github.com/gradle/gradle-native/issues/169 //TODO: Cross-compiling, blocked by: https://github.com/gradle/gradle-native/issues/169
//setTargets( "arm", "arm64", "x86-64", "x86" ) //setTargets( "arm", "arm64", "x86-64", "x86" )
eachPlatform { eachPlatform {
cppCompiler.withArguments { addAll( ["-x", "c", "-std=c11", "-DMPW_SODIUM=1"] ) } cppCompiler.withArguments { addAll( ["-x", "c", "-std=c11", "-Werror", "-DMPW_SODIUM=1"] ) }
} }
} }
} }

View File

@ -3,13 +3,16 @@
#include "mpw-jni.h" #include "mpw-jni.h"
#include "mpw-util.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, 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 *passwdBytes = (*env)->GetByteArrayElements( env, passwd, NULL );
jbyte *saltBytes = (*env)->GetByteArrayElements( env, salt, 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 ); (uint64_t)N, (uint32_t)r, (uint32_t)p );
(*env)->ReleaseByteArrayElements( env, passwd, passwdBytes, JNI_ABORT ); (*env)->ReleaseByteArrayElements( env, passwd, passwdBytes, JNI_ABORT );
(*env)->ReleaseByteArrayElements( env, salt, saltBytes, 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) if (!key)
return ERR; 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; return OK;
} }

View File

@ -1,5 +1,4 @@
/* DO NOT EDIT THIS FILE - it is machine generated */ /* DO NOT EDIT THIS FILE - it is machine generated */
#undef __cplusplus
#include <jni.h> #include <jni.h>
/* Header for class com_lyndir_masterpassword_impl_MPAlgorithmV0 */ /* Header for class com_lyndir_masterpassword_impl_MPAlgorithmV0 */
@ -13,10 +12,10 @@ extern "C" {
/* /*
* Class: com_lyndir_masterpassword_impl_MPAlgorithmV0 * Class: com_lyndir_masterpassword_impl_MPAlgorithmV0
* Method: _scrypt * Method: _scrypt
* Signature: ([BI[BIIII[BI)I * Signature: ([B[BIII[B)I
*/ */
JNIEXPORT jint JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1scrypt 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 #ifdef __cplusplus
} }

View File

@ -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 )
}
}

View File

@ -90,15 +90,13 @@ public class MPAlgorithmV0 extends MPAlgorithm {
@Nullable @Nullable
protected byte[] scrypt(final byte[] secret, final byte[] salt, final int keySize) { protected byte[] scrypt(final byte[] secret, final byte[] salt, final int keySize) {
byte[] buffer = new byte[keySize]; byte[] buffer = new byte[keySize];
if (_scrypt( if (_scrypt( secret, salt, scrypt_N(), scrypt_r(), scrypt_p(), buffer ) < 0)
secret, secret.length, salt, salt.length,
scrypt_N(), scrypt_r(), scrypt_p(), buffer, buffer.length ) < 0)
return null; return null;
return buffer; 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 @Override
public byte[] siteKey(final byte[] masterKey, final String siteName, UnsignedInteger siteCounter, public byte[] siteKey(final byte[] masterKey, final String siteName, UnsignedInteger siteCounter,

View File

@ -50,7 +50,7 @@ public class MPAlgorithmV3 extends MPAlgorithmV2 {
logger.trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%d, r=%d, p=%d )", logger.trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%d, r=%d, p=%d )",
scrypt_N(), scrypt_r(), scrypt_p() ); scrypt_N(), scrypt_r(), scrypt_p() );
byte[] masterPasswordBytes = toBytes( masterPassword ); 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( masterKeySalt, (byte) 0 );
Arrays.fill( masterPasswordBytes, (byte) 0 ); Arrays.fill( masterPasswordBytes, (byte) 0 );
if (masterKey == null) if (masterKey == null)

View File

@ -60,7 +60,7 @@ public class MPTests {
} }
public Case getCase(final String identifier) { public Case getCase(final String identifier) {
for (final Case testCase : getCases()) for (final Case testCase : cases)
if (identifier.equals( testCase.getIdentifier() )) if (identifier.equals( testCase.getIdentifier() ))
return testCase; return testCase;

View File

@ -39,7 +39,7 @@ public class MPMasterKeyTest {
throws Exception { throws Exception {
testSuite = new MPTestSuite(); testSuite = new MPTestSuite();
//testSuite.getTests().addFilters( "v0" ); //testSuite.getTests().addFilters( "v3_type_maximum" );
} }
@Test @Test