Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import java.io.File; |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 20 | import java.io.FileNotFoundException; |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 21 | import java.io.FileOutputStream; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 22 | import java.io.IOException; |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 23 | import java.io.PrintWriter; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 24 | |
Nick Kralevich | 79619dd | 2013-03-04 13:05:32 -0800 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 29 | import android.os.Binder; |
| 30 | import android.os.Environment; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 33 | import android.os.SystemProperties; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 34 | import android.util.Slog; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * A service designed to load and periodically save "randomness" |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 38 | * for the Linux kernel RNG and to mix in data from Hardware RNG (if present) |
| 39 | * into the Linux RNG. |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 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 | * |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 48 | * <p>On systems with Hardware RNG (/dev/hw_random), a block of output from HW |
| 49 | * RNG is mixed into the Linux RNG on EntropyMixer's startup and whenever |
| 50 | * EntropyMixer periodically runs to save a block of output from Linux RNG on |
| 51 | * disk. This mixing is done in a way that does not increase the Linux RNG's |
| 52 | * entropy estimate is not increased. This is to avoid having to trust/verify |
| 53 | * the quality and authenticity of the "randomness" of the HW RNG. |
| 54 | * |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 55 | * <p>This class was modeled after the script in |
| 56 | * <a href="http://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html">man |
| 57 | * 4 random</a>. |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 58 | */ |
Nick Kralevich | 6967cbc | 2011-11-17 13:24:32 -0800 | [diff] [blame] | 59 | public class EntropyMixer extends Binder { |
| 60 | private static final String TAG = "EntropyMixer"; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 61 | private static final int ENTROPY_WHAT = 1; |
| 62 | private static final int ENTROPY_WRITE_PERIOD = 3 * 60 * 60 * 1000; // 3 hrs |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 63 | private static final long START_TIME = System.currentTimeMillis(); |
| 64 | private static final long START_NANOTIME = System.nanoTime(); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 65 | |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 66 | private final String randomDevice; |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 67 | private final String hwRandomDevice; |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 68 | private final String entropyFile; |
| 69 | |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Handler that periodically updates the entropy on disk. |
| 72 | */ |
Alex Klyubin | 4444444 | 2017-12-13 14:40:13 -0800 | [diff] [blame] | 73 | private final Handler mHandler = new Handler(IoThread.getHandler().getLooper()) { |
| 74 | // IMPLEMENTATION NOTE: This handler runs on the I/O thread to avoid I/O on the main thread. |
| 75 | // The reason we're using our own Handler instead of IoThread.getHandler() is to create our |
| 76 | // own ID space for the "what" parameter of messages seen by the handler. |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public void handleMessage(Message msg) { |
| 79 | if (msg.what != ENTROPY_WHAT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 80 | Slog.e(TAG, "Will not process invalid message"); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 83 | addHwRandomEntropy(); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 84 | writeEntropy(); |
| 85 | scheduleEntropyWriter(); |
| 86 | } |
| 87 | }; |
| 88 | |
Nick Kralevich | 79619dd | 2013-03-04 13:05:32 -0800 | [diff] [blame] | 89 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 90 | @Override |
| 91 | public void onReceive(Context context, Intent intent) { |
| 92 | writeEntropy(); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | public EntropyMixer(Context context) { |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 97 | this(context, getSystemDir() + "/entropy.dat", "/dev/urandom", "/dev/hw_random"); |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** Test only interface, not for public use */ |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 101 | public EntropyMixer( |
| 102 | Context context, |
| 103 | String entropyFile, |
| 104 | String randomDevice, |
| 105 | String hwRandomDevice) { |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 106 | if (randomDevice == null) { throw new NullPointerException("randomDevice"); } |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 107 | if (hwRandomDevice == null) { throw new NullPointerException("hwRandomDevice"); } |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 108 | if (entropyFile == null) { throw new NullPointerException("entropyFile"); } |
| 109 | |
| 110 | this.randomDevice = randomDevice; |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 111 | this.hwRandomDevice = hwRandomDevice; |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 112 | this.entropyFile = entropyFile; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 113 | loadInitialEntropy(); |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 114 | addDeviceSpecificEntropy(); |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 115 | addHwRandomEntropy(); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 116 | writeEntropy(); |
| 117 | scheduleEntropyWriter(); |
Nick Kralevich | 79619dd | 2013-03-04 13:05:32 -0800 | [diff] [blame] | 118 | IntentFilter broadcastFilter = new IntentFilter(Intent.ACTION_SHUTDOWN); |
| 119 | broadcastFilter.addAction(Intent.ACTION_POWER_CONNECTED); |
| 120 | broadcastFilter.addAction(Intent.ACTION_REBOOT); |
Alex Klyubin | 4444444 | 2017-12-13 14:40:13 -0800 | [diff] [blame] | 121 | context.registerReceiver( |
| 122 | mBroadcastReceiver, |
| 123 | broadcastFilter, |
| 124 | null, // do not require broadcaster to hold any permissions |
| 125 | mHandler // process received broadcasts on the I/O thread instead of the main thread |
| 126 | ); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void scheduleEntropyWriter() { |
| 130 | mHandler.removeMessages(ENTROPY_WHAT); |
| 131 | mHandler.sendEmptyMessageDelayed(ENTROPY_WHAT, ENTROPY_WRITE_PERIOD); |
| 132 | } |
| 133 | |
| 134 | private void loadInitialEntropy() { |
| 135 | try { |
Elliott Hughes | 6907891 | 2011-04-04 12:15:34 -0700 | [diff] [blame] | 136 | RandomBlock.fromFile(entropyFile).toFile(randomDevice, false); |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 137 | } catch (FileNotFoundException e) { |
| 138 | Slog.w(TAG, "No existing entropy file -- first boot?"); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 139 | } catch (IOException e) { |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 140 | Slog.w(TAG, "Failure loading existing entropy file", e); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | private void writeEntropy() { |
| 145 | try { |
Nick Kralevich | 79619dd | 2013-03-04 13:05:32 -0800 | [diff] [blame] | 146 | Slog.i(TAG, "Writing entropy..."); |
Elliott Hughes | 6907891 | 2011-04-04 12:15:34 -0700 | [diff] [blame] | 147 | RandomBlock.fromFile(randomDevice).toFile(entropyFile, true); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 148 | } catch (IOException e) { |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 149 | Slog.w(TAG, "Unable to write entropy", e); |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Add additional information to the kernel entropy pool. The |
| 155 | * information isn't necessarily "random", but that's ok. Even |
| 156 | * sending non-random information to {@code /dev/urandom} is useful |
| 157 | * because, while it doesn't increase the "quality" of the entropy pool, |
| 158 | * it mixes more bits into the pool, which gives us a higher degree |
| 159 | * of uncertainty in the generated randomness. Like nature, writes to |
| 160 | * the random device can only cause the quality of the entropy in the |
| 161 | * kernel to stay the same or increase. |
| 162 | * |
| 163 | * <p>For maximum effect, we try to target information which varies |
| 164 | * on a per-device basis, and is not easily observable to an |
| 165 | * attacker. |
| 166 | */ |
| 167 | private void addDeviceSpecificEntropy() { |
| 168 | PrintWriter out = null; |
| 169 | try { |
Nick Kralevich | 93a6839 | 2010-03-19 16:57:21 -0700 | [diff] [blame] | 170 | out = new PrintWriter(new FileOutputStream(randomDevice)); |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 171 | out.println("Copyright (C) 2009 The Android Open Source Project"); |
| 172 | out.println("All Your Randomness Are Belong To Us"); |
| 173 | out.println(START_TIME); |
| 174 | out.println(START_NANOTIME); |
| 175 | out.println(SystemProperties.get("ro.serialno")); |
| 176 | out.println(SystemProperties.get("ro.bootmode")); |
| 177 | out.println(SystemProperties.get("ro.baseband")); |
| 178 | out.println(SystemProperties.get("ro.carrier")); |
| 179 | out.println(SystemProperties.get("ro.bootloader")); |
| 180 | out.println(SystemProperties.get("ro.hardware")); |
| 181 | out.println(SystemProperties.get("ro.revision")); |
Nick Kralevich | 03ce760 | 2013-02-01 15:21:50 -0800 | [diff] [blame] | 182 | out.println(SystemProperties.get("ro.build.fingerprint")); |
Nick Kralevich | b91ec41 | 2010-09-27 14:49:00 -0700 | [diff] [blame] | 183 | out.println(new Object().hashCode()); |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 184 | out.println(System.currentTimeMillis()); |
| 185 | out.println(System.nanoTime()); |
| 186 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 187 | Slog.w(TAG, "Unable to add device specific data to the entropy pool", e); |
Nick Kralevich | b8cba95 | 2009-06-19 09:45:35 -0700 | [diff] [blame] | 188 | } finally { |
| 189 | if (out != null) { |
| 190 | out.close(); |
| 191 | } |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Mixes in the output from HW RNG (if present) into the Linux RNG. |
| 197 | */ |
| 198 | private void addHwRandomEntropy() { |
Alex Klyubin | c78a463 | 2017-12-18 10:59:02 -0800 | [diff] [blame] | 199 | if (!new File(hwRandomDevice).exists()) { |
| 200 | // HW RNG not present/exposed -- ignore |
| 201 | return; |
| 202 | } |
| 203 | |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 204 | try { |
| 205 | RandomBlock.fromFile(hwRandomDevice).toFile(randomDevice, false); |
| 206 | Slog.i(TAG, "Added HW RNG output to entropy pool"); |
Alex Klyubin | 6ab3d20 | 2013-10-03 13:23:13 -0700 | [diff] [blame] | 207 | } catch (IOException e) { |
| 208 | Slog.w(TAG, "Failed to add HW RNG output to entropy pool", e); |
| 209 | } |
| 210 | } |
| 211 | |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 212 | private static String getSystemDir() { |
| 213 | File dataDir = Environment.getDataDirectory(); |
| 214 | File systemDir = new File(dataDir, "system"); |
| 215 | systemDir.mkdirs(); |
| 216 | return systemDir.toString(); |
| 217 | } |
| 218 | } |