1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "NativeCrypto.h"
6 #include "APKOpen.h"
7 
8 #include <jni.h>
9 
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <inttypes.h>
13 
14 #include "mozilla/SHA1.h"
15 #include "pbkdf2_sha256.h"
16 
17 /**
18  * Helper function to invoke native PBKDF2 function with JNI
19  * arguments.
20  */
21 extern "C" JNIEXPORT jbyteArray MOZ_JNICALL
Java_org_mozilla_gecko_background_nativecode_NativeCrypto_pbkdf2SHA256(JNIEnv * env,jclass jc,jbyteArray jpassword,jbyteArray jsalt,jint c,jint dkLen)22 Java_org_mozilla_gecko_background_nativecode_NativeCrypto_pbkdf2SHA256(
23     JNIEnv* env, jclass jc, jbyteArray jpassword, jbyteArray jsalt, jint c,
24     jint dkLen) {
25   if (dkLen < 0) {
26     env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"),
27                   "dkLen should not be less than 0");
28     return nullptr;
29   }
30 
31   jbyte* password = env->GetByteArrayElements(jpassword, nullptr);
32   size_t passwordLen = env->GetArrayLength(jpassword);
33 
34   jbyte* salt = env->GetByteArrayElements(jsalt, nullptr);
35   size_t saltLen = env->GetArrayLength(jsalt);
36 
37   uint8_t hashResult[dkLen];
38   PBKDF2_SHA256((uint8_t*)password, passwordLen, (uint8_t*)salt, saltLen,
39                 (uint64_t)c, hashResult, (size_t)dkLen);
40 
41   env->ReleaseByteArrayElements(jpassword, password, JNI_ABORT);
42   env->ReleaseByteArrayElements(jsalt, salt, JNI_ABORT);
43 
44   jbyteArray out = env->NewByteArray(dkLen);
45   if (out == nullptr) {
46     return nullptr;
47   }
48   env->SetByteArrayRegion(out, 0, dkLen, (jbyte*)hashResult);
49 
50   return out;
51 }
52 
53 using namespace mozilla;
54 
55 /**
56  * Helper function to invoke native SHA-1 function with JNI arguments.
57  */
58 extern "C" JNIEXPORT jbyteArray MOZ_JNICALL
Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha1(JNIEnv * env,jclass jc,jbyteArray jstr)59 Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha1(
60     JNIEnv* env, jclass jc, jbyteArray jstr) {
61   jbyte* str = env->GetByteArrayElements(jstr, nullptr);
62   size_t strLen = env->GetArrayLength(jstr);
63 
64   SHA1Sum sha1;
65   SHA1Sum::Hash hashResult;
66   sha1.update((void*)str, (uint32_t)strLen);
67   sha1.finish(hashResult);
68 
69   env->ReleaseByteArrayElements(jstr, str, JNI_ABORT);
70 
71   jbyteArray out = env->NewByteArray(SHA1Sum::kHashSize);
72   if (out == nullptr) {
73     return nullptr;
74   }
75   env->SetByteArrayRegion(out, 0, SHA1Sum::kHashSize, (jbyte*)hashResult);
76 
77   return out;
78 }
79 
80 /**
81  * Helper function to invoke native SHA-256 init with JNI arguments.
82  */
83 extern "C" JNIEXPORT jbyteArray MOZ_JNICALL
Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256init(JNIEnv * env,jclass jc)84 Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256init(
85     JNIEnv* env, jclass jc) {
86   jbyteArray out = env->NewByteArray(sizeof(SHA256_CTX));
87   if (nullptr == out) {
88     return nullptr;
89   }
90 
91   SHA256_CTX* shaContext = (SHA256_CTX*)env->GetByteArrayElements(out, nullptr);
92   SHA256_Init(shaContext);
93 
94   env->ReleaseByteArrayElements(out, (jbyte*)shaContext, 0);
95 
96   return out;
97 }
98 
99 /**
100  * Helper function to invoke native SHA-256 update with JNI arguments.
101  */
102 extern "C" JNIEXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256update(JNIEnv * env,jclass jc,jbyteArray jctx,jbyteArray jstr,jint len)103 Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256update(
104     JNIEnv* env, jclass jc, jbyteArray jctx, jbyteArray jstr, jint len) {
105   jbyte* str = env->GetByteArrayElements(jstr, nullptr);
106 
107   SHA256_CTX* shaContext =
108       (SHA256_CTX*)env->GetByteArrayElements(jctx, nullptr);
109 
110   SHA256_Update(shaContext, (void*)str, (size_t)len);
111 
112   env->ReleaseByteArrayElements(jstr, str, JNI_ABORT);
113   env->ReleaseByteArrayElements(jctx, (jbyte*)shaContext, 0);
114 
115   return;
116 }
117 
118 /**
119  * Helper function to invoke native SHA-256 finalize with JNI arguments.
120  */
121 extern "C" JNIEXPORT jbyteArray MOZ_JNICALL
Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256finalize(JNIEnv * env,jclass jc,jbyteArray jctx)122 Java_org_mozilla_gecko_background_nativecode_NativeCrypto_sha256finalize(
123     JNIEnv* env, jclass jc, jbyteArray jctx) {
124   SHA256_CTX* shaContext =
125       (SHA256_CTX*)env->GetByteArrayElements(jctx, nullptr);
126 
127   unsigned char* digest = new unsigned char[32];
128   SHA256_Final(digest, shaContext);
129 
130   env->ReleaseByteArrayElements(jctx, (jbyte*)shaContext, JNI_ABORT);
131 
132   jbyteArray out = env->NewByteArray(32);
133   if (nullptr != out) {
134     env->SetByteArrayRegion(out, 0, 32, (jbyte*)digest);
135   }
136 
137   delete[] digest;
138 
139   return out;
140 }
141