-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully replace Java mpw algorithm implementation with proxy to standar…
…d C implementation.
- Loading branch information
Showing
33 changed files
with
361 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,113 @@ | ||
#include <string.h> | ||
|
||
#include "mpw-jni.h" | ||
#include "mpw-algorithm.h" | ||
#include "mpw-util.h" | ||
|
||
/** 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, 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 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 ); | ||
|
||
if (!key) | ||
return ERR; | ||
|
||
jbyte *bufBytes = (*env)->GetByteArrayElements( env, buf, NULL ); | ||
memcpy( bufBytes, key, keyLength ); | ||
(*env)->ReleaseByteArrayElements( env, buf, bufBytes, JNI_OK ); | ||
mpw_free( &key, keyLength ); | ||
|
||
return OK; | ||
// TODO: We may need to zero the jbytes safely. | ||
|
||
/* native int _masterKey(final String fullName, final byte[] masterPassword, final Version version) */ | ||
JNIEXPORT jbyteArray JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1masterKey(JNIEnv *env, jobject obj, | ||
jstring fullName, jbyteArray masterPassword, jint algorithmVersion) { | ||
|
||
const char *fullNameString = (*env)->GetStringUTFChars( env, fullName, NULL ); | ||
jbyte *masterPasswordString = (*env)->GetByteArrayElements( env, masterPassword, NULL ); | ||
|
||
MPMasterKey masterKeyBytes = mpw_masterKey( fullNameString, (char *)masterPasswordString, (MPAlgorithmVersion)algorithmVersion ); | ||
(*env)->ReleaseStringUTFChars( env, fullName, fullNameString ); | ||
(*env)->ReleaseByteArrayElements( env, masterPassword, masterPasswordString, JNI_ABORT ); | ||
|
||
if (!masterKeyBytes) | ||
return NULL; | ||
|
||
jbyteArray masterKey = (*env)->NewByteArray( env, (jsize)MPMasterKeySize ); | ||
(*env)->SetByteArrayRegion( env, masterKey, 0, (jsize)MPMasterKeySize, (jbyte *)masterKeyBytes ); | ||
mpw_free( &masterKeyBytes, MPMasterKeySize ); | ||
|
||
return masterKey; | ||
} | ||
|
||
/* native int _siteKey(final byte[] masterKey, final String siteName, final long siteCounter, | ||
final MPKeyPurpose keyPurpose, @Nullable final String keyContext, final Version version) */ | ||
JNIEXPORT jbyteArray JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1siteKey(JNIEnv *env, jobject obj, | ||
jbyteArray masterKey, jstring siteName, jlong siteCounter, jint keyPurpose, jstring keyContext, jint algorithmVersion) { | ||
|
||
jbyte *masterKeyBytes = (*env)->GetByteArrayElements( env, masterKey, NULL ); | ||
const char *siteNameString = (*env)->GetStringUTFChars( env, siteName, NULL ); | ||
const char *keyContextString = keyContext? (*env)->GetStringUTFChars( env, keyContext, NULL ): NULL; | ||
MPMasterKey siteKeyBytes = mpw_siteKey( | ||
(MPMasterKey)masterKeyBytes, siteNameString, (MPCounterValue)siteCounter, | ||
(MPKeyPurpose)keyPurpose, keyContextString, (MPAlgorithmVersion)algorithmVersion ); | ||
(*env)->ReleaseByteArrayElements( env, masterKey, masterKeyBytes, JNI_ABORT ); | ||
(*env)->ReleaseStringUTFChars( env, siteName, siteNameString ); | ||
(*env)->ReleaseStringUTFChars( env, keyContext, keyContextString ); | ||
|
||
if (!siteKeyBytes) | ||
return NULL; | ||
|
||
jbyteArray siteKey = (*env)->NewByteArray( env, (jsize)MPMasterKeySize ); | ||
(*env)->SetByteArrayRegion( env, siteKey, 0, (jsize)MPMasterKeySize, (jbyte *)siteKeyBytes ); | ||
mpw_free( &siteKeyBytes, MPSiteKeySize ); | ||
|
||
return siteKey; | ||
} | ||
|
||
/* native String _siteResult(final byte[] masterKey, final byte[] siteKey, final String siteName, final long siteCounter, | ||
final MPKeyPurpose keyPurpose, @Nullable final String keyContext, | ||
final MPResultType resultType, @Nullable final String resultParam, final Version version) */ | ||
JNIEXPORT jstring JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1siteResult(JNIEnv *env, jobject obj, | ||
jbyteArray masterKey, jbyteArray siteKey, jstring siteName, jlong siteCounter, jint keyPurpose, jstring keyContext, | ||
jint resultType, jstring resultParam, jint algorithmVersion) { | ||
|
||
jbyte *masterKeyBytes = (*env)->GetByteArrayElements( env, masterKey, NULL ); | ||
jbyte *siteKeyBytes = (*env)->GetByteArrayElements( env, siteKey, NULL ); | ||
const char *siteNameString = (*env)->GetStringUTFChars( env, siteName, NULL ); | ||
const char *keyContextString = keyContext? (*env)->GetStringUTFChars( env, keyContext, NULL ): NULL; | ||
const char *resultParamString = resultParam? (*env)->GetStringUTFChars( env, resultParam, NULL ): NULL; | ||
const char *siteResultString = mpw_siteResult( | ||
(MPMasterKey)masterKeyBytes, siteNameString, (MPCounterValue)siteCounter, | ||
(MPKeyPurpose)keyPurpose, keyContextString, (MPResultType)resultType, resultParamString, (MPAlgorithmVersion)algorithmVersion ); | ||
(*env)->ReleaseByteArrayElements( env, masterKey, masterKeyBytes, JNI_ABORT ); | ||
(*env)->ReleaseByteArrayElements( env, siteKey, siteKeyBytes, JNI_ABORT ); | ||
(*env)->ReleaseStringUTFChars( env, siteName, siteNameString ); | ||
(*env)->ReleaseStringUTFChars( env, keyContext, keyContextString ); | ||
(*env)->ReleaseStringUTFChars( env, resultParam, resultParamString ); | ||
|
||
if (!siteResultString) | ||
return NULL; | ||
|
||
jstring siteResult = (*env)->NewStringUTF( env, siteResultString ); | ||
mpw_free_string( &siteResultString ); | ||
|
||
return siteResult; | ||
} | ||
|
||
/* native String _siteState(final byte[] masterKey, final byte[] siteKey, final String siteName, final long siteCounter, | ||
final MPKeyPurpose keyPurpose, @Nullable final String keyContext, | ||
final MPResultType resultType, final String resultParam, final Version version) */ | ||
JNIEXPORT jstring JNICALL Java_com_lyndir_masterpassword_impl_MPAlgorithmV0__1siteState(JNIEnv *env, jobject obj, | ||
jbyteArray masterKey, jbyteArray siteKey, jstring siteName, jlong siteCounter, jint keyPurpose, jstring keyContext, | ||
jint resultType, jstring resultParam, jint algorithmVersion) { | ||
|
||
jbyte *masterKeyBytes = (*env)->GetByteArrayElements( env, masterKey, NULL ); | ||
jbyte *siteKeyBytes = (*env)->GetByteArrayElements( env, siteKey, NULL ); | ||
const char *siteNameString = (*env)->GetStringUTFChars( env, siteName, NULL ); | ||
const char *keyContextString = keyContext? (*env)->GetStringUTFChars( env, keyContext, NULL ): NULL; | ||
const char *resultParamString = (*env)->GetStringUTFChars( env, resultParam, NULL ); | ||
const char *siteStateString = mpw_siteState( | ||
(MPMasterKey)masterKeyBytes, siteNameString, (MPCounterValue)siteCounter, | ||
(MPKeyPurpose)keyPurpose, keyContextString, (MPResultType)resultType, resultParamString, (MPAlgorithmVersion)algorithmVersion ); | ||
(*env)->ReleaseByteArrayElements( env, masterKey, masterKeyBytes, JNI_ABORT ); | ||
(*env)->ReleaseByteArrayElements( env, siteKey, siteKeyBytes, JNI_ABORT ); | ||
(*env)->ReleaseStringUTFChars( env, siteName, siteNameString ); | ||
(*env)->ReleaseStringUTFChars( env, keyContext, keyContextString ); | ||
(*env)->ReleaseStringUTFChars( env, resultParam, resultParamString ); | ||
|
||
if (!siteStateString) | ||
return NULL; | ||
|
||
jstring siteState = (*env)->NewStringUTF( env, siteStateString ); | ||
mpw_free_string( &siteStateString ); | ||
|
||
return siteState; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithmException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//============================================================================== | ||
// This file is part of Master Password. | ||
// Copyright (c) 2011-2017, Maarten Billemont. | ||
// | ||
// Master Password is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Master Password is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You can find a copy of the GNU General Public License in the | ||
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>. | ||
//============================================================================== | ||
|
||
package com.lyndir.masterpassword; | ||
|
||
/** | ||
* @author lhunath, 2017-09-21 | ||
*/ | ||
public class MPAlgorithmException extends MPException { | ||
|
||
public MPAlgorithmException(final String message) { | ||
super( message ); | ||
} | ||
|
||
public MPAlgorithmException(final String message, final Throwable cause) { | ||
super( message, cause ); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//============================================================================== | ||
// This file is part of Master Password. | ||
// Copyright (c) 2011-2017, Maarten Billemont. | ||
// | ||
// Master Password is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Master Password is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You can find a copy of the GNU General Public License in the | ||
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>. | ||
//============================================================================== | ||
|
||
package com.lyndir.masterpassword; | ||
|
||
/** | ||
* @author lhunath, 2018-06-03 | ||
*/ | ||
public class MPException extends Exception { | ||
|
||
public MPException(final String message) { | ||
super( message ); | ||
} | ||
|
||
public MPException(final String message, final Throwable cause) { | ||
super( message, cause ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.