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 package org.mozilla.gecko.background.nativecode;
6 
7 import java.security.GeneralSecurityException;
8 
9 import org.mozilla.gecko.annotation.RobocopTarget;
10 import org.mozilla.gecko.AppConstants;
11 
12 import android.util.Log;
13 
14 @RobocopTarget
15 public class NativeCrypto {
16   static {
17     try {
18       System.loadLibrary("mozglue");
19     } catch (UnsatisfiedLinkError e) {
20       Log.wtf("NativeCrypto", "Couldn't load mozglue. Trying /data/app-lib path.");
21       try {
22         System.load("/data/app-lib/" + AppConstants.ANDROID_PACKAGE_NAME + "/libmozglue.so");
23       } catch (Throwable ee) {
24           try {
25             Log.wtf("NativeCrypto", "Couldn't load mozglue: " + ee + ". Trying /data/data path.");
26             System.load("/data/data/" + AppConstants.ANDROID_PACKAGE_NAME + "/lib/libmozglue.so");
27           } catch (UnsatisfiedLinkError eee) {
28               Log.wtf("NativeCrypto", "Failed every attempt to load mozglue. Giving up.");
29               throw new RuntimeException("Unable to load mozglue", eee);
30           }
31       }
32     }
33   }
34 
35   /**
36    * Wrapper to perform PBKDF2-HMAC-SHA-256 in native code.
37    */
pbkdf2SHA256(byte[] password, byte[] salt, int c, int dkLen)38   public native static byte[] pbkdf2SHA256(byte[] password, byte[] salt, int c, int dkLen)
39       throws GeneralSecurityException;
40 
41   /**
42    * Wrapper to perform SHA-1 in native code.
43    */
sha1(byte[] str)44   public native static byte[] sha1(byte[] str);
45 
46   /**
47    * Wrapper to perform SHA-256 init in native code. Returns a SHA-256 context.
48    */
sha256init()49   public native static byte[] sha256init();
50 
51   /**
52    * Wrapper to update a SHA-256 context in native code.
53    */
sha256update(byte[] ctx, byte[] str, int len)54   public native static void sha256update(byte[] ctx, byte[] str, int len);
55 
56   /**
57    * Wrapper to finalize a SHA-256 context in native code. Returns digest.
58    */
sha256finalize(byte[] ctx)59   public native static byte[] sha256finalize(byte[] ctx);
60 }
61