blob: fbb66f979c5c57993b77aba7c7e75ddd7786f2cd [file] [log] [blame]
Nick Kralevich4fb25612009-06-17 16:03:22 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import java.io.File;
Dianne Hackborn13579ed2012-11-28 18:05:36 -080020import java.io.FileNotFoundException;
Nick Kralevichb8cba952009-06-19 09:45:35 -070021import java.io.FileOutputStream;
Nick Kralevich4fb25612009-06-17 16:03:22 -070022import java.io.IOException;
Nick Kralevichb8cba952009-06-19 09:45:35 -070023import java.io.OutputStream;
24import java.io.PrintWriter;
Nick Kralevich4fb25612009-06-17 16:03:22 -070025
Nick Kralevich79619dd2013-03-04 13:05:32 -080026import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
Nick Kralevich4fb25612009-06-17 16:03:22 -070030import android.os.Binder;
31import android.os.Environment;
32import android.os.Handler;
33import android.os.Message;
Nick Kralevichb8cba952009-06-19 09:45:35 -070034import android.os.SystemProperties;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
Nick Kralevich4fb25612009-06-17 16:03:22 -070036
37/**
38 * A service designed to load and periodically save "randomness"
39 * for the Linux kernel.
40 *
41 * <p>When a Linux system starts up, the entropy pool associated with
42 * {@code /dev/random} may be in a fairly predictable state. Applications which
43 * depend strongly on randomness may find {@code /dev/random} or
44 * {@code /dev/urandom} returning predictable data. In order to counteract
45 * this effect, it's helpful to carry the entropy pool information across
46 * shutdowns and startups.
47 *
48 * <p>This class was modeled after the script in
49 * <a href="http://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html">man
50 * 4 random</a>.
Nick Kralevich4fb25612009-06-17 16:03:22 -070051 */
Nick Kralevich6967cbc2011-11-17 13:24:32 -080052public class EntropyMixer extends Binder {
53 private static final String TAG = "EntropyMixer";
Nick Kralevich4fb25612009-06-17 16:03:22 -070054 private static final int ENTROPY_WHAT = 1;
55 private static final int ENTROPY_WRITE_PERIOD = 3 * 60 * 60 * 1000; // 3 hrs
Nick Kralevichb8cba952009-06-19 09:45:35 -070056 private static final long START_TIME = System.currentTimeMillis();
57 private static final long START_NANOTIME = System.nanoTime();
Nick Kralevich4fb25612009-06-17 16:03:22 -070058
Nick Kralevich93a68392010-03-19 16:57:21 -070059 private final String randomDevice;
60 private final String entropyFile;
61
Nick Kralevich4fb25612009-06-17 16:03:22 -070062 /**
63 * Handler that periodically updates the entropy on disk.
64 */
65 private final Handler mHandler = new Handler() {
66 @Override
67 public void handleMessage(Message msg) {
68 if (msg.what != ENTROPY_WHAT) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080069 Slog.e(TAG, "Will not process invalid message");
Nick Kralevich4fb25612009-06-17 16:03:22 -070070 return;
71 }
72 writeEntropy();
73 scheduleEntropyWriter();
74 }
75 };
76
Nick Kralevich79619dd2013-03-04 13:05:32 -080077 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
78 @Override
79 public void onReceive(Context context, Intent intent) {
80 writeEntropy();
81 }
82 };
83
84 public EntropyMixer(Context context) {
85 this(context, getSystemDir() + "/entropy.dat", "/dev/urandom");
Nick Kralevich93a68392010-03-19 16:57:21 -070086 }
87
88 /** Test only interface, not for public use */
Nick Kralevich79619dd2013-03-04 13:05:32 -080089 public EntropyMixer(Context context, String entropyFile, String randomDevice) {
Nick Kralevich93a68392010-03-19 16:57:21 -070090 if (randomDevice == null) { throw new NullPointerException("randomDevice"); }
91 if (entropyFile == null) { throw new NullPointerException("entropyFile"); }
92
93 this.randomDevice = randomDevice;
94 this.entropyFile = entropyFile;
Nick Kralevich4fb25612009-06-17 16:03:22 -070095 loadInitialEntropy();
Nick Kralevichb8cba952009-06-19 09:45:35 -070096 addDeviceSpecificEntropy();
Nick Kralevich4fb25612009-06-17 16:03:22 -070097 writeEntropy();
98 scheduleEntropyWriter();
Nick Kralevich79619dd2013-03-04 13:05:32 -080099 IntentFilter broadcastFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
100 broadcastFilter.addAction(Intent.ACTION_POWER_CONNECTED);
101 broadcastFilter.addAction(Intent.ACTION_REBOOT);
102 context.registerReceiver(mBroadcastReceiver, broadcastFilter);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700103 }
104
105 private void scheduleEntropyWriter() {
106 mHandler.removeMessages(ENTROPY_WHAT);
107 mHandler.sendEmptyMessageDelayed(ENTROPY_WHAT, ENTROPY_WRITE_PERIOD);
108 }
109
110 private void loadInitialEntropy() {
111 try {
Elliott Hughes69078912011-04-04 12:15:34 -0700112 RandomBlock.fromFile(entropyFile).toFile(randomDevice, false);
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800113 } catch (FileNotFoundException e) {
114 Slog.w(TAG, "No existing entropy file -- first boot?");
Nick Kralevich4fb25612009-06-17 16:03:22 -0700115 } catch (IOException e) {
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800116 Slog.w(TAG, "Failure loading existing entropy file", e);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700117 }
118 }
119
120 private void writeEntropy() {
121 try {
Nick Kralevich79619dd2013-03-04 13:05:32 -0800122 Slog.i(TAG, "Writing entropy...");
Elliott Hughes69078912011-04-04 12:15:34 -0700123 RandomBlock.fromFile(randomDevice).toFile(entropyFile, true);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700124 } catch (IOException e) {
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800125 Slog.w(TAG, "Unable to write entropy", e);
Nick Kralevichb8cba952009-06-19 09:45:35 -0700126 }
127 }
128
129 /**
130 * Add additional information to the kernel entropy pool. The
131 * information isn't necessarily "random", but that's ok. Even
132 * sending non-random information to {@code /dev/urandom} is useful
133 * because, while it doesn't increase the "quality" of the entropy pool,
134 * it mixes more bits into the pool, which gives us a higher degree
135 * of uncertainty in the generated randomness. Like nature, writes to
136 * the random device can only cause the quality of the entropy in the
137 * kernel to stay the same or increase.
138 *
139 * <p>For maximum effect, we try to target information which varies
140 * on a per-device basis, and is not easily observable to an
141 * attacker.
142 */
143 private void addDeviceSpecificEntropy() {
144 PrintWriter out = null;
145 try {
Nick Kralevich93a68392010-03-19 16:57:21 -0700146 out = new PrintWriter(new FileOutputStream(randomDevice));
Nick Kralevichb8cba952009-06-19 09:45:35 -0700147 out.println("Copyright (C) 2009 The Android Open Source Project");
148 out.println("All Your Randomness Are Belong To Us");
149 out.println(START_TIME);
150 out.println(START_NANOTIME);
151 out.println(SystemProperties.get("ro.serialno"));
152 out.println(SystemProperties.get("ro.bootmode"));
153 out.println(SystemProperties.get("ro.baseband"));
154 out.println(SystemProperties.get("ro.carrier"));
155 out.println(SystemProperties.get("ro.bootloader"));
156 out.println(SystemProperties.get("ro.hardware"));
157 out.println(SystemProperties.get("ro.revision"));
Nick Kralevich03ce7602013-02-01 15:21:50 -0800158 out.println(SystemProperties.get("ro.build.fingerprint"));
Nick Kralevichb91ec412010-09-27 14:49:00 -0700159 out.println(new Object().hashCode());
Nick Kralevichb8cba952009-06-19 09:45:35 -0700160 out.println(System.currentTimeMillis());
161 out.println(System.nanoTime());
162 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800163 Slog.w(TAG, "Unable to add device specific data to the entropy pool", e);
Nick Kralevichb8cba952009-06-19 09:45:35 -0700164 } finally {
165 if (out != null) {
166 out.close();
167 }
Nick Kralevich4fb25612009-06-17 16:03:22 -0700168 }
169 }
170
171 private static String getSystemDir() {
172 File dataDir = Environment.getDataDirectory();
173 File systemDir = new File(dataDir, "system");
174 systemDir.mkdirs();
175 return systemDir.toString();
176 }
177}