Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d190338 authored by Nick Kralevich's avatar Nick Kralevich Committed by Android Git Automerger
Browse files

am 0e5b5f66: am 0841c4cb: Merge "EntropyMixer: Write entropy at shutdown /...

am 0e5b5f66: am 0841c4cb: Merge "EntropyMixer: Write entropy at shutdown / reboot / power" into jb-mr2-dev

* commit '0e5b5f66':
  EntropyMixer: Write entropy at shutdown / reboot / power
parents 2ba8de33 0e5b5f66
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -23,6 +23,10 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
@@ -44,9 +48,6 @@ import android.util.Slog;
 * <p>This class was modeled after the script in
 * <a href="http://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html">man
 * 4 random</a>.
 *
 * <p>TODO: Investigate attempting to write entropy data at shutdown time
 * instead of periodically.
 */
public class EntropyMixer extends Binder {
    private static final String TAG = "EntropyMixer";
@@ -73,12 +74,19 @@ public class EntropyMixer extends Binder {
        }
    };

    public EntropyMixer() {
        this(getSystemDir() + "/entropy.dat", "/dev/urandom");
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            writeEntropy();
        }
    };

    public EntropyMixer(Context context) {
        this(context, getSystemDir() + "/entropy.dat", "/dev/urandom");
    }

    /** Test only interface, not for public use */
    public EntropyMixer(String entropyFile, String randomDevice) {
    public EntropyMixer(Context context, String entropyFile, String randomDevice) {
        if (randomDevice == null) { throw new NullPointerException("randomDevice"); }
        if (entropyFile == null) { throw new NullPointerException("entropyFile"); }

@@ -88,6 +96,10 @@ public class EntropyMixer extends Binder {
        addDeviceSpecificEntropy();
        writeEntropy();
        scheduleEntropyWriter();
        IntentFilter broadcastFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
        broadcastFilter.addAction(Intent.ACTION_POWER_CONNECTED);
        broadcastFilter.addAction(Intent.ACTION_REBOOT);
        context.registerReceiver(mBroadcastReceiver, broadcastFilter);
    }

    private void scheduleEntropyWriter() {
@@ -107,6 +119,7 @@ public class EntropyMixer extends Binder {

    private void writeEntropy() {
        try {
            Slog.i(TAG, "Writing entropy...");
            RandomBlock.fromFile(randomDevice).toFile(entropyFile, true);
        } catch (IOException e) {
            Slog.w(TAG, "Unable to write entropy", e);
+3 −3
Original line number Diff line number Diff line
@@ -206,9 +206,6 @@ class ServerThread extends Thread {
            installer = new Installer();
            installer.ping();

            Slog.i(TAG, "Entropy Mixer");
            ServiceManager.addService("entropy", new EntropyMixer());

            Slog.i(TAG, "Power Manager");
            power = new PowerManagerService();
            ServiceManager.addService(Context.POWER_SERVICE, power);
@@ -257,6 +254,9 @@ class ServerThread extends Thread {

            ActivityManagerService.setSystemProcess();

            Slog.i(TAG, "Entropy Mixer");
            ServiceManager.addService("entropy", new EntropyMixer(context));

            Slog.i(TAG, "User Service");
            ServiceManager.addService(Context.USER_SERVICE,
                    UserManagerService.getInstance());
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public class EntropyMixerTest extends AndroidTestCase {
        assertEquals(0, FileUtils.readTextFile(file, 0, null).length());

        // The constructor has the side effect of writing to file
        new EntropyMixer("/dev/null", file.getCanonicalPath());
        new EntropyMixer(getContext(), "/dev/null", file.getCanonicalPath());

        assertTrue(FileUtils.readTextFile(file, 0, null).length() > 0);
    }