Merge "EntropyMixer: Write entropy at shutdown / reboot / power" into jb-mr2-dev

This commit is contained in:
Nick Kralevich
2013-03-04 22:39:29 +00:00
committed by Android (Google) Code Review
3 changed files with 23 additions and 10 deletions

View File

@ -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);

View File

@ -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());

View File

@ -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);
}