Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "NativeCrypto.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <jni.h> |
michael@0 | 8 | |
michael@0 | 9 | #include <errno.h> |
michael@0 | 10 | #include <stdlib.h> |
michael@0 | 11 | #include <inttypes.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/SHA1.h" |
michael@0 | 14 | #include "pbkdf2_sha256.h" |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Helper function to invoke native PBKDF2 function with JNI |
michael@0 | 18 | * arguments. |
michael@0 | 19 | */ |
michael@0 | 20 | extern "C" JNIEXPORT jbyteArray JNICALL Java_org_mozilla_gecko_background_nativecode_NativeCrypto_pbkdf2SHA256 |
michael@0 | 21 | (JNIEnv *env, jclass jc, jbyteArray jpassword, jbyteArray jsalt, jint c, jint dkLen) { |
michael@0 | 22 | if (dkLen < 0) { |
michael@0 | 23 | env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), |
michael@0 | 24 | "dkLen should not be less than 0"); |
michael@0 | 25 | return NULL; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | jbyte *password = env->GetByteArrayElements(jpassword, NULL); |
michael@0 | 29 | size_t passwordLen = env->GetArrayLength(jpassword); |
michael@0 | 30 | |
michael@0 | 31 | jbyte *salt = env->GetByteArrayElements(jsalt, NULL); |
michael@0 | 32 | size_t saltLen = env->GetArrayLength(jsalt); |
michael@0 | 33 | |
michael@0 | 34 | uint8_t hashResult[dkLen]; |
michael@0 | 35 | PBKDF2_SHA256((uint8_t *) password, passwordLen, (uint8_t *) salt, saltLen, |
michael@0 | 36 | (uint64_t) c, hashResult, (size_t) dkLen); |
michael@0 | 37 | |
michael@0 | 38 | env->ReleaseByteArrayElements(jpassword, password, JNI_ABORT); |
michael@0 | 39 | env->ReleaseByteArrayElements(jsalt, salt, JNI_ABORT); |
michael@0 | 40 | |
michael@0 | 41 | jbyteArray out = env->NewByteArray(dkLen); |
michael@0 | 42 | if (out == NULL) { |
michael@0 | 43 | return NULL; |
michael@0 | 44 | } |
michael@0 | 45 | env->SetByteArrayRegion(out, 0, dkLen, (jbyte *) hashResult); |
michael@0 | 46 | |
michael@0 | 47 | return out; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | using namespace mozilla; |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Helper function to invoke native SHA-1 function with JNI arguments. |
michael@0 | 54 | */ |
michael@0 | 55 | extern "C" JNIEXPORT jbyteArray JNICALL Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha1 |
michael@0 | 56 | (JNIEnv *env, jclass jc, jbyteArray jstr) { |
michael@0 | 57 | jbyte *str = env->GetByteArrayElements(jstr, NULL); |
michael@0 | 58 | size_t strLen = env->GetArrayLength(jstr); |
michael@0 | 59 | |
michael@0 | 60 | SHA1Sum sha1; |
michael@0 | 61 | SHA1Sum::Hash hashResult; |
michael@0 | 62 | sha1.update((void *) str, (uint32_t) strLen); |
michael@0 | 63 | sha1.finish(hashResult); |
michael@0 | 64 | |
michael@0 | 65 | env->ReleaseByteArrayElements(jstr, str, JNI_ABORT); |
michael@0 | 66 | |
michael@0 | 67 | jbyteArray out = env->NewByteArray(SHA1Sum::HashSize); |
michael@0 | 68 | if (out == NULL) { |
michael@0 | 69 | return NULL; |
michael@0 | 70 | } |
michael@0 | 71 | env->SetByteArrayRegion(out, 0, SHA1Sum::HashSize, (jbyte *) hashResult); |
michael@0 | 72 | |
michael@0 | 73 | return out; |
michael@0 | 74 | } |