blob: cfdbf7d530cdc8e7a109b3b0c8c24e8eeb97fe23 [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"
Alex Klyubin6ab3d202013-10-03 13:23:13 -070039 * for the Linux kernel RNG and to mix in data from Hardware RNG (if present)
40 * into the Linux RNG.
Nick Kralevich4fb25612009-06-17 16:03:22 -070041 *
42 * <p>When a Linux system starts up, the entropy pool associated with
43 * {@code /dev/random} may be in a fairly predictable state. Applications which
44 * depend strongly on randomness may find {@code /dev/random} or
45 * {@code /dev/urandom} returning predictable data. In order to counteract
46 * this effect, it's helpful to carry the entropy pool information across
47 * shutdowns and startups.
48 *
Alex Klyubin6ab3d202013-10-03 13:23:13 -070049 * <p>On systems with Hardware RNG (/dev/hw_random), a block of output from HW
50 * RNG is mixed into the Linux RNG on EntropyMixer's startup and whenever
51 * EntropyMixer periodically runs to save a block of output from Linux RNG on
52 * disk. This mixing is done in a way that does not increase the Linux RNG's
53 * entropy estimate is not increased. This is to avoid having to trust/verify
54 * the quality and authenticity of the &quot;randomness&quot; of the HW RNG.
55 *
Nick Kralevich4fb25612009-06-17 16:03:22 -070056 * <p>This class was modeled after the script in
57 * <a href="http://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html">man
58 * 4 random</a>.
Nick Kralevich4fb25612009-06-17 16:03:22 -070059 */
Nick Kralevich6967cbc2011-11-17 13:24:32 -080060public class EntropyMixer extends Binder {
61 private static final String TAG = "EntropyMixer";
Nick Kralevich4fb25612009-06-17 16:03:22 -070062 private static final int ENTROPY_WHAT = 1;
63 private static final int ENTROPY_WRITE_PERIOD = 3 * 60 * 60 * 1000; // 3 hrs
Nick Kralevichb8cba952009-06-19 09:45:35 -070064 private static final long START_TIME = System.currentTimeMillis();
65 private static final long START_NANOTIME = System.nanoTime();
Nick Kralevich4fb25612009-06-17 16:03:22 -070066
Nick Kralevich93a68392010-03-19 16:57:21 -070067 private final String randomDevice;
Alex Klyubin6ab3d202013-10-03 13:23:13 -070068 private final String hwRandomDevice;
Nick Kralevich93a68392010-03-19 16:57:21 -070069 private final String entropyFile;
70
Nick Kralevich4fb25612009-06-17 16:03:22 -070071 /**
72 * Handler that periodically updates the entropy on disk.
73 */
74 private final Handler mHandler = new Handler() {
75 @Override
76 public void handleMessage(Message msg) {
77 if (msg.what != ENTROPY_WHAT) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080078 Slog.e(TAG, "Will not process invalid message");
Nick Kralevich4fb25612009-06-17 16:03:22 -070079 return;
80 }
Alex Klyubin6ab3d202013-10-03 13:23:13 -070081 addHwRandomEntropy();
Nick Kralevich4fb25612009-06-17 16:03:22 -070082 writeEntropy();
83 scheduleEntropyWriter();
84 }
85 };
86
Nick Kralevich79619dd2013-03-04 13:05:32 -080087 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
88 @Override
89 public void onReceive(Context context, Intent intent) {
90 writeEntropy();
91 }
92 };
93
94 public EntropyMixer(Context context) {
Alex Klyubin6ab3d202013-10-03 13:23:13 -070095 this(context, getSystemDir() + "/entropy.dat", "/dev/urandom", "/dev/hw_random");
Nick Kralevich93a68392010-03-19 16:57:21 -070096 }
97
98 /** Test only interface, not for public use */
Alex Klyubin6ab3d202013-10-03 13:23:13 -070099 public EntropyMixer(
100 Context context,
101 String entropyFile,
102 String randomDevice,
103 String hwRandomDevice) {
Nick Kralevich93a68392010-03-19 16:57:21 -0700104 if (randomDevice == null) { throw new NullPointerException("randomDevice"); }
Alex Klyubin6ab3d202013-10-03 13:23:13 -0700105 if (hwRandomDevice == null) { throw new NullPointerException("hwRandomDevice"); }
Nick Kralevich93a68392010-03-19 16:57:21 -0700106 if (entropyFile == null) { throw new NullPointerException("entropyFile"); }
107
108 this.randomDevice = randomDevice;
Alex Klyubin6ab3d202013-10-03 13:23:13 -0700109 this.hwRandomDevice = hwRandomDevice;
Nick Kralevich93a68392010-03-19 16:57:21 -0700110 this.entropyFile = entropyFile;
Nick Kralevich4fb25612009-06-17 16:03:22 -0700111 loadInitialEntropy();
Nick Kralevichb8cba952009-06-19 09:45:35 -0700112 addDeviceSpecificEntropy();
Alex Klyubin6ab3d202013-10-03 13:23:13 -0700113 addHwRandomEntropy();
Nick Kralevich4fb25612009-06-17 16:03:22 -0700114 writeEntropy();
115 scheduleEntropyWriter();
Nick Kralevich79619dd2013-03-04 13:05:32 -0800116 IntentFilter broadcastFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
117 broadcastFilter.addAction(Intent.ACTION_POWER_CONNECTED);
118 broadcastFilter.addAction(Intent.ACTION_REBOOT);
119 context.registerReceiver(mBroadcastReceiver, broadcastFilter);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700120 }
121
122 private void scheduleEntropyWriter() {
123 mHandler.removeMessages(ENTROPY_WHAT);
124 mHandler.sendEmptyMessageDelayed(ENTROPY_WHAT, ENTROPY_WRITE_PERIOD);
125 }
126
127 private void loadInitialEntropy() {
128 try {
Elliott Hughes69078912011-04-04 12:15:34 -0700129 RandomBlock.fromFile(entropyFile).toFile(randomDevice, false);
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800130 } catch (FileNotFoundException e) {
131 Slog.w(TAG, "No existing entropy file -- first boot?");
Nick Kralevich4fb25612009-06-17 16:03:22 -0700132 } catch (IOException e) {
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800133 Slog.w(TAG, "Failure loading existing entropy file", e);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700134 }
135 }
136
137 private void writeEntropy() {
138 try {
Nick Kralevich79619dd2013-03-04 13:05:32 -0800139 Slog.i(TAG, "Writing entropy...");
Elliott Hughes69078912011-04-04 12:15:34 -0700140 RandomBlock.fromFile(randomDevice).toFile(entropyFile, true);
Nick Kralevich4fb25612009-06-17 16:03:22 -0700141 } catch (IOException e) {
Dianne Hackborn13579ed2012-11-28 18:05:36 -0800142 Slog.w(TAG, "Unable to write entropy", e);
Nick Kralevichb8cba952009-06-19 09:45:35 -0700143 }
144 }
145
146 /**
147 * Add additional information to the kernel entropy pool. The
148 * information isn't necessarily "random", but that's ok. Even
149 * sending non-random information to {@code /dev/urandom} is useful
150 * because, while it doesn't increase the "quality" of the entropy pool,
151 * it mixes more bits into the pool, which gives us a higher degree
152 * of uncertainty in the generated randomness. Like nature, writes to
153 * the random device can only cause the quality of the entropy in the
154 * kernel to stay the same or increase.
155 *
156 * <p>For maximum effect, we try to target information which varies
157 * on a per-device basis, and is not easily observable to an
158 * attacker.
159 */
160 private void addDeviceSpecificEntropy() {
161 PrintWriter out = null;
162 try {
Nick Kralevich93a68392010-03-19 16:57:21 -0700163 out = new PrintWriter(new FileOutputStream(randomDevice));
Nick Kralevichb8cba952009-06-19 09:45:35 -0700164 out.println("Copyright (C) 2009 The Android Open Source Project");
165 out.println("All Your Randomness Are Belong To Us");
166 out.println(START_TIME);
167 out.println(START_NANOTIME);
168 out.println(SystemProperties.get("ro.serialno"));
169 out.println(SystemProperties.get("ro.bootmode"));
170 out.println(SystemProperties.get("ro.baseband"));
171 out.println(SystemProperties.get("ro.carrier"));
172 out.println(SystemProperties.get("ro.bootloader"));
173 out.println(SystemProperties.get("ro.hardware"));
174 out.println(SystemProperties.get("ro.revision"));
Nick Kralevich03ce7602013-02-01 15:21:50 -0800175 out.println(SystemProperties.get("ro.build.fingerprint"));
Nick Kralevichb91ec412010-09-27 14:49:00 -0700176 out.println(new Object().hashCode());
Nick Kralevichb8cba952009-06-19 09:45:35 -0700177 out.println(System.currentTimeMillis());
178 out.println(System.nanoTime());
179 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800180 Slog.w(TAG, "Unable to add device specific data to the entropy pool", e);
Nick Kralevichb8cba952009-06-19 09:45:35 -0700181 } finally {
182 if (out != null) {
183 out.close();
184 }
Nick Kralevich4fb25612009-06-17 16:03:22 -0700185 }
186 }
187
Alex Klyubin6ab3d202013-10-03 13:23:13 -0700188 /**
189 * Mixes in the output from HW RNG (if present) into the Linux RNG.
190 */
191 private void addHwRandomEntropy() {
192 try {
193 RandomBlock.fromFile(hwRandomDevice).toFile(randomDevice, false);
194 Slog.i(TAG, "Added HW RNG output to entropy pool");
195 } catch (FileNotFoundException ignored) {
196 // HW RNG not present/exposed -- ignore
197 } catch (IOException e) {
198 Slog.w(TAG, "Failed to add HW RNG output to entropy pool", e);
199 }
200 }
201
Nick Kralevich4fb25612009-06-17 16:03:22 -0700202 private static String getSystemDir() {
203 File dataDir = Environment.getDataDirectory();
204 File systemDir = new File(dataDir, "system");
205 systemDir.mkdirs();
206 return systemDir.toString();
207 }
208}