blob: 957e9edc1f448360d63a460dde95dc5fff69ea11 [file] [log] [blame]
Clay Murphy32285dd2014-03-12 12:15:00 -07001page.title=Encryption
Robert Ly35f2fda2013-01-29 16:27:05 -08002@jd:body
3
4<!--
Clay Murphy32285dd2014-03-12 12:15:00 -07005 Copyright 2014 The Android Open Source Project
Robert Ly35f2fda2013-01-29 16:27:05 -08006
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
Clay Murphy32285dd2014-03-12 12:15:00 -070019
Clay Murphy5e411e62014-10-14 12:44:54 -070020<div id="qv-wrapper">
21 <div id="qv">
22 <h2>In this document</h2>
23 <ol id="auto-toc">
24 </ol>
25 </div>
26</div>
27
28<h2 id=what_is_encryption>What is encryption?</h2>
29
30<p>Encryption is the process of encoding user data on an Android device using an
31encrypted key. Once a device is encrypted, all user-created data is
32automatically encrypted before committing it to disk and all reads
33automatically decrypt data before returning it to the calling process.</p>
34
Clay Murphyec58cfb2014-10-22 11:10:53 -070035<h2 id=what_we’ve_added_for_android_l>What we’ve added for Android 5.0</h2>
Clay Murphy5e411e62014-10-14 12:44:54 -070036
37<ul>
38 <li>Created fast encryption, which only encrypts used blocks on the data partition
39to avoid first boot taking a long time. Only ext4 and f2fs filesystems
40currently support fast encryption.
41 <li>Added the <code>forceencrypt</code> flag to encrypt on first boot.
42 <li>Added support for patterns and encryption without a password.
Clay Murphy6c2619f2014-11-20 16:49:53 -080043 <li>Added hardware-backed storage of the encryption key using Trusted
44 Execution Environment’s (TEE) signing capability (such as in a TrustZone).
45 See <a href="#storing_the_encrypted_key">Storing the encrypted key</a> for more
46 details.
Clay Murphy5e411e62014-10-14 12:44:54 -070047</ul>
48
Clay Murphyec58cfb2014-10-22 11:10:53 -070049<p class="caution"><strong>Caution:</strong> Devices upgraded to Android 5.0 and then
50encrypted may be returned to an unencrypted state by factory data reset. New Android 5.0
51devices encrypted at first boot cannot be returned to an unencrypted state.</p>
52
Clay Murphy5e411e62014-10-14 12:44:54 -070053<h2 id=how_android_encryption_works>How Android encryption works</h2>
54
Clay Murphy6c2619f2014-11-20 16:49:53 -080055<p>Android disk encryption is based on <code>dm-crypt</code>, which is a kernel
56feature that works at the block device layer. Because of
57this, encryption works with Embedded MultiMediaCard<strong> (</strong>eMMC) and
58similar flash devices that present themselves to the kernel as block
Clay Murphy5e411e62014-10-14 12:44:54 -070059devices. Encryption is not possible with YAFFS, which talks directly to a raw
60NAND flash chip. </p>
61
62<p>The encryption algorithm is 128 Advanced Encryption Standard (AES) with
63cipher-block chaining (CBC) and ESSIV:SHA256. The master key is encrypted with
64128-bit AES via calls to the OpenSSL library. You must use 128 bits or more for
65the key (with 256 being optional). </p>
66
67<p class="note"><strong>Note:</strong> OEMs can use 128-bit or higher to encrypt the master key.</p>
68
Clay Murphyec58cfb2014-10-22 11:10:53 -070069<p>In the Android 5.0 release, there are four kinds of encryption states: </p>
Clay Murphy5e411e62014-10-14 12:44:54 -070070
71<ul>
72 <li>default
73 <li>PIN
74 <li>password
75 <li>pattern
76</ul>
77
Clay Murphy6c2619f2014-11-20 16:49:53 -080078<p>Upon first boot, the device creates a randomly generated 128-bit master key
79and then hashes it with a default password and stored salt. The default password is: "default_password"
80However, the resultant hash is also signed through a TEE (such as TrustZone),
81which uses a hash of the signature to encrypt the master key.</p>
82
83<p>You can find the default password defined in the Android Open Source Project <a
84href="https://android.googlesource.com/platform/system/vold/+/master/cryptfs.c">cryptfs.c</a>
85file.</p>
Clay Murphy5e411e62014-10-14 12:44:54 -070086
87<p>When the user sets the PIN/pass or password on the device, only the 128-bit key
88is re-encrypted and stored. (ie. user PIN/pass/pattern changes do NOT cause
89re-encryption of userdata.) </p>
90
91<p>Encryption is managed by <code>init</code> and <code>vold</code>. <code>init</code> calls <code>vold</code>, and vold sets properties to trigger events in init. Other parts of the system
92also look at the properties to conduct tasks such as report status, ask for a
93password, or prompt to factory reset in the case of a fatal error. To invoke
94encryption features in <code>vold</code>, the system uses the command line tool <code>vdc</code>’s <code>cryptfs</code> commands: <code>checkpw</code>, <code>restart</code>, <code>enablecrypto</code>, <code>changepw</code>, <code>cryptocomplete</code>, <code>verifypw</code>, <code>setfield</code>, <code>getfield</code>, <code>mountdefaultencrypted</code>, <code>getpwtype</code>, <code>getpw</code>, and <code>clearpw</code>.</p>
95
96<p>In order to encrypt, decrypt or wipe <code>/data</code>, <code>/data</code> must not be mounted. However, in order to show any user interface (UI), the
97framework must start and the framework requires <code>/data</code> to run. To resolve this conundrum, a temporary filesystem is mounted on <code>/data</code>. This allows Android to prompt for passwords, show progress, or suggest a data
98wipe as needed. It does impose the limitation that in order to switch from the
99temporary filesystem to the true <code>/data</code> filesystem, the system must stop every process with open files on the
100temporary filesystem and restart those processes on the real <code>/data</code> filesystem. To do this, all services must be in one of three groups: <code>core</code>, <code>main</code>, and <code>late_start</code>.</p>
101
102<ul>
103 <li><code>core</code>: Never shut down after starting.
104 <li><code>main</code>: Shut down and then restart after the disk password is entered.
105 <li><code>late_start</code>: Does not start until after <code>/data</code> has been decrypted and mounted.
106</ul>
107
108<p>To trigger these actions, the <code>vold.decrypt</code> property is set to <a href="https://android.googlesource.com/platform/system/vold/+/master/cryptfs.c">various strings</a>. To kill and restart services, the <code>init</code> commands are:</p>
109
110<ul>
111 <li><code>class_reset</code>: Stops a service but allows it to be restarted with class_start.
112 <li><code>class_start</code>: Restarts a service.
113 <li><code>class_stop</code>: Stops a service and adds a <code>SVC_DISABLED</code> flag. Stopped services do not respond to <code>class_start</code>.
114</ul>
115
116<h2 id=flows>Flows</h2>
117
118<p>There are four flows for an encrypted device. A device is encrypted just once
119and then follows a normal boot flow. </p>
120
121<ul>
122 <li>Encrypt a previously unencrypted device:
123 <ul>
124 <li>Encrypt a new device with <code>forceencrypt</code>: Mandatory encryption at first boot (starting in Android L).
125 <li>Encrypt an existing device: User-initiated encryption (Android K and earlier).
126 </ul>
127 <li>Boot an encrypted device:
128 <ul>
129 <li>Starting an encrypted device with no password: Booting an encrypted device that
Clay Murphyec58cfb2014-10-22 11:10:53 -0700130has no set password (relevant for devices running Android 5.0 and later).
Clay Murphy5e411e62014-10-14 12:44:54 -0700131 <li> Starting an encrypted device with a password: Booting an encrypted device that
132has a set password.
133 </ul>
134</ul>
135
136<p>In addition to these flows, the device can also fail to encrypt <code>/data</code>. Each of the flows are explained in detail below.</p>
137
Clay Murphy92c38f92014-10-30 18:19:30 -0700138<h3 id=encrypt_a_new_device_with_forceencrypt>Encrypt a new device with /forceencrypt</h3>
Clay Murphy5e411e62014-10-14 12:44:54 -0700139
Clay Murphyec58cfb2014-10-22 11:10:53 -0700140<p>This is the normal first boot for an Android 5.0 device. </p>
Clay Murphy5e411e62014-10-14 12:44:54 -0700141
Clay Murphy32285dd2014-03-12 12:15:00 -0700142<ol>
Clay Murphy5e411e62014-10-14 12:44:54 -0700143 <li><strong>Detect unencrypted filesystem with <code>/forceencrypt</code> flag</strong>
144
145<p>
146<code>/data</code> is not encrypted but needs to be because <code>/forceencrypt</code> mandates it.
147Unmount <code>/data</code>.</p>
148
149 <li><strong>Start encrypting <code>/data</code></strong>
150
151<p><code>vold.decrypt = "trigger_encryption"</code> triggers <code>init.rc</code>, which will cause <code>vold</code> to encrypt <code>/data</code> with no password. (None is set because this should be a new device.)</p>
152
153
154 <li><strong>Mount tmpfs</strong>
155
156
157<p><code>vold</code> mounts a tmpfs <code>/data</code> (using the tmpfs options from
158<code>ro.crypto.tmpfs_options</code>) and sets the property <code>vold.encrypt_progress</code> to 0.
159<code>vold</code> prepepares the tmpfs <code>/data</code> for booting an encrypted system and sets the
160property <code>vold.decrypt</code> to: <code>trigger_restart_min_framework</code>
Paul Lawrence707f7ef2014-05-20 11:00:23 -0700161</p>
Clay Murphy5e411e62014-10-14 12:44:54 -0700162
163 <li><strong>Bring up framework to show progress</strong>
164
165
166<p>Because the device has virtually no data to encrypt, the progress bar will
167often not actually appear because encryption happens so quickly. See <a href="#encrypt_an_existing_device">Encrypt an existing device</a> for more details about the progress UI. </p>
168
169 <li><strong>When <code>/data</code> is encrypted, take down the framework</strong>
170
Clay Murphyec58cfb2014-10-22 11:10:53 -0700171<p><code>vold</code> sets <code>vold.decrypt</code> to
172<code>trigger_default_encryption</code> which starts the
173<code>defaultcrypto</code> service. (This starts the flow below for mounting a
174default encrypted userdata.) <code>trigger_default_encryption</code> checks the
175encryption type to see if <code>/data</code> is encrypted with or without a
176password. Because Android 5.0 devices are encrypted on first boot, there should
177be no password set; therefore we decrypt and mount <code>/data</code>.</p>
Clay Murphy5e411e62014-10-14 12:44:54 -0700178
179 <li><strong>Mount <code>/data</code></strong>
180
181<p><code>init</code> then mounts <code>/data</code> on a tmpfs RAMDisk using parameters it picks up from <code>ro.crypto.tmpfs_options</code>, which is set in <code>init.rc</code>.</p>
182
183 <li><strong>Start framework</strong>
184
185<p>Set <code>vold</code> to <code>trigger_restart_framework</code>, which continues the usual boot process.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700186</ol>
Paul Lawrence707f7ef2014-05-20 11:00:23 -0700187
Clay Murphy5e411e62014-10-14 12:44:54 -0700188<h3 id=encrypt_an_existing_device>Encrypt an existing device</h3>
Clay Murphy32285dd2014-03-12 12:15:00 -0700189
Clay Murphy5e411e62014-10-14 12:44:54 -0700190<p>This is what happens when you encrypt an unencrypted Android K or earlier
191device that has been migrated to L. Note that this is the same flow as used in
192K.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700193
Clay Murphy5e411e62014-10-14 12:44:54 -0700194<p>This process is user-initiated and is referred to as “inplace encryption” in
195the code. When a user selects to encrypt a device, the UI makes sure the
196battery is fully charged and the AC adapter is plugged in so there is enough
197power to finish the encryption process.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700198
Clay Murphy5e411e62014-10-14 12:44:54 -0700199<p class="warning"><strong>Warning:</strong> If the device runs out of power and shuts down before it has finished
200encrypting, file data is left in a partially encrypted state. The device must
201be factory reset and all data is lost.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700202
Clay Murphy5e411e62014-10-14 12:44:54 -0700203<p>To enable inplace encryption, <code>vold</code> starts a loop to read each sector of the real block device and then write it
204to the crypto block device. <code>vold</code> checks to see if a sector is in use before reading and writing it, which makes
205encryption much faster on a new device that has little to no data. </p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700206
Clay Murphy5e411e62014-10-14 12:44:54 -0700207<p><strong>State of device</strong>: Set <code>ro.crypto.state = "unencrypted"</code> and execute the <code>on nonencrypted</code> <code>init</code> trigger to continue booting.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700208
Clay Murphy5e411e62014-10-14 12:44:54 -0700209<ol>
210 <li><strong>Check password</strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700211
Clay Murphy5e411e62014-10-14 12:44:54 -0700212<p>The UI calls <code>vold</code> with the command <code>cryptfs enablecrypto inplace</code> where <code>passwd</code> is the user's lock screen password.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700213
Clay Murphy5e411e62014-10-14 12:44:54 -0700214 <li><strong>Take down the framework</strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700215
Clay Murphy5e411e62014-10-14 12:44:54 -0700216<p><code>vold</code> checks for errors, returns -1 if it can't encrypt, and prints a reason in the
217log. If it can encrypt, it sets the property <code>vold.decrypt</code> to <code>trigger_shutdown_framework</code>. This causes <code>init.rc</code> to stop services in the classes <code>late_start</code> and <code>main</code>. </p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700218
Clay Murphy5e411e62014-10-14 12:44:54 -0700219 <li><strong>Unmount <code>/data</code></strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700220
Clay Murphy5e411e62014-10-14 12:44:54 -0700221<p><code>vold</code> unmounts <code>/mnt/sdcard</code> and then <code>/data</code>.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700222
Clay Murphy5e411e62014-10-14 12:44:54 -0700223 <li><strong>Start encrypting <code>/data</code></strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700224
Clay Murphy5e411e62014-10-14 12:44:54 -0700225<p><code>vold</code> then sets up the crypto mapping, which creates a virtual crypto block device
226that maps onto the real block device but encrypts each sector as it is written,
227and decrypts each sector as it is read. <code>vold</code> then creates and writes out the crypto metadata.</p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700228
Clay Murphy5e411e62014-10-14 12:44:54 -0700229 <li><strong>While it’s encrypting, mount tmpfs</strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700230
Clay Murphy5e411e62014-10-14 12:44:54 -0700231<p><code>vold</code> mounts a tmpfs <code>/data</code> (using the tmpfs options from <code>ro.crypto.tmpfs_options</code>) and sets the property <code>vold.encrypt_progress</code> to 0. <code>vold</code> prepares the tmpfs <code>/data</code> for booting an encrypted system and sets the property <code>vold.decrypt</code> to: <code>trigger_restart_min_framework</code> </p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700232
Clay Murphy5e411e62014-10-14 12:44:54 -0700233 <li><strong>Bring up framework to show progress</strong>
Clay Murphy32285dd2014-03-12 12:15:00 -0700234
Clay Murphy5e411e62014-10-14 12:44:54 -0700235<p><code>trigger_restart_min_framework </code>causes <code>init.rc</code> to start the <code>main</code> class of services. When the framework sees that <code>vold.encrypt_progress</code> is set to 0, it brings up the progress bar UI, which queries that property
236every five seconds and updates a progress bar. The encryption loop updates <code>vold.encrypt_progress</code> every time it encrypts another percent of the partition. </p>
Clay Murphy32285dd2014-03-12 12:15:00 -0700237
Clay Murphy5e411e62014-10-14 12:44:54 -0700238 <li><strong>When<code> /data</code> is encrypted, reboot</strong>
239
240<p>When <code>/data</code> is successfully encrypted, <code>vold</code> clears the flag <code>ENCRYPTION_IN_PROGRESS</code> in the metadata and reboots the system. </p>
241
242<p> If the reboot fails for some reason, <code>vold</code> sets the property <code>vold.encrypt_progress</code> to <code>error_reboot_failed</code> and the UI should display a message asking the user to press a button to
243reboot. This is not expected to ever occur.</p>
244</ol>
245
246<h3 id=starting_an_encrypted_device_with_default_encryption>Starting an encrypted device with default encryption</h3>
247
248<p>This is what happens when you boot up an encrypted device with no password.
Clay Murphyec58cfb2014-10-22 11:10:53 -0700249Because Android 5.0 devices are encrypted on first boot, there should be no set
Clay Murphy5e411e62014-10-14 12:44:54 -0700250password and therefore this is the <em>default encryption</em> state.</p>
251
252<ol>
253 <li><strong>Detect encrypted <code>/data</code> with no password</strong>
254
255<p>Detect that the Android device is encrypted because <code>/data</code>
256cannot be mounted and one of the flags <code>encryptable</code> or
257<code>forceencrypt</code> is set.</p>
258
259<p><code>vold</code> sets <code>vold.decrypt</code> to <code>trigger_default_encryption</code>, which starts the <code>defaultcrypto</code> service. <code>trigger_default_encryption</code> checks the encryption type to see if <code>/data</code> is encrypted with or without a password. </p>
260
261 <li><strong>Decrypt /data</strong>
262
263<p>Creates the <code>dm-crypt</code> device over the block device so the device is ready for use.</p>
264
265 <li><strong>Mount /data</strong>
266
267<p><code>vold</code> then mounts the decrypted real <code>/data </code>partition and then prepares the new partition. It sets the property <code>vold.post_fs_data_done</code> to 0 and then sets <code>vold.decrypt</code> to <code>trigger_post_fs_data</code>. This causes <code>init.rc</code> to run its <code>post-fs-data</code> commands. They will create any necessary directories or links and then set <code>vold.post_fs_data_done</code> to 1.</p>
268
269<p>Once <code>vold</code> sees the 1 in that property, it sets the property <code>vold.decrypt</code> to: <code>trigger_restart_framework.</code> This causes <code>init.rc</code> to start services in class <code>main</code> again and also start services in class <code>late_start</code> for the first time since boot.</p>
270
271 <li><strong>Start framework</strong>
272
273<p>Now the framework boots all its services using the decrypted <code>/data</code>, and the system is ready for use.</p>
274</ol>
275
276<h3 id=starting_an_encrypted_device_without_default_encryption>Starting an encrypted device without default encryption</h3>
277
278<p>This is what happens when you boot up an encrypted device that has a set
279password. The device’s password can be a pin, pattern, or password. </p>
280
281<ol>
282 <li><strong>Detect encrypted device with a password</strong>
283
284<p>Detect that the Android device is encrypted because the flag <code>ro.crypto.state = "encrypted"</code></p>
285
286<p><code>vold</code> sets <code>vold.decrypt</code> to <code>trigger_restart_min_framework</code> because <code>/data</code> is encrypted with a password.</p>
287
288 <li><strong>Mount tmpfs</strong>
289
290<p><code>init</code> sets five properties to save the initial mount options given for <code>/data</code> with parameters passed from <code>init.rc</code>. <code>vold</code> uses these properties to set up the crypto mapping:</p>
291
292<ol>
293 <li><code>ro.crypto.fs_type</code>
294 <li><code>ro.crypto.fs_real_blkdev</code>
295 <li><code>ro.crypto.fs_mnt_point</code>
296 <li><code>ro.crypto.fs_options</code>
297 <li><code>ro.crypto.fs_flags </code>(ASCII 8-digit hex number preceded by 0x)
298 </ol>
299
300 <li><strong>Start framework to prompt for password</strong>
301
302<p>The framework starts up and sees that <code>vold.decrypt</code> is set to <code>trigger_restart_min_framework</code>. This tells the framework that it is booting on a tmpfs <code>/data</code> disk and it needs to get the user password.</p>
303
304<p>First, however, it needs to make sure that the disk was properly encrypted. It
305sends the command <code>cryptfs cryptocomplete</code> to <code>vold</code>. <code>vold</code> returns 0 if encryption was completed successfully, -1 on internal error, or
306-2 if encryption was not completed successfully. <code>vold</code> determines this by looking in the crypto metadata for the <code>CRYPTO_ENCRYPTION_IN_PROGRESS</code> flag. If it's set, the encryption process was interrupted, and there is no
307usable data on the device. If <code>vold</code> returns an error, the UI should display a message to the user to reboot and
308factory reset the device, and give the user a button to press to do so.</p>
309
310 <li><strong>Decrypt data with password</strong>
311
312<p>Once <code>cryptfs cryptocomplete</code> is successful, the framework displays a UI asking for the disk password. The
313UI checks the password by sending the command <code>cryptfs checkpw</code> to <code>vold</code>. If the password is correct (which is determined by successfully mounting the
314decrypted <code>/data</code> at a temporary location, then unmounting it), <code>vold</code> saves the name of the decrypted block device in the property <code>ro.crypto.fs_crypto_blkdev</code> and returns status 0 to the UI. If the password is incorrect, it returns -1 to
315the UI.</p>
316
317 <li><strong>Stop framework</strong>
318
319<p>The UI puts up a crypto boot graphic and then calls <code>vold</code> with the command <code>cryptfs restart</code>. <code>vold</code> sets the property <code>vold.decrypt</code> to <code>trigger_reset_main</code>, which causes <code>init.rc</code> to do <code>class_reset main</code>. This stops all services in the main class, which allows the tmpfs <code>/data</code> to be unmounted. </p>
320
321 <li><strong>Mount <code>/data</code></strong>
322
323<p><code>vold</code> then mounts the decrypted real <code>/data </code>partition and prepares the new partition (which may never have been prepared if
324it was encrypted with the wipe option, which is not supported on first
325release). It sets the property <code>vold.post_fs_data_done</code> to 0 and then sets <code>vold.decrypt</code> to <code>trigger_post_fs_data</code>. This causes <code>init.rc</code> to run its <code>post-fs-data</code> commands. They will create any necessary directories or links and then set <code>vold.post_fs_data_done</code> to 1. Once <code>vold</code> sees the 1 in that property, it sets the property <code>vold.decrypt</code> to <code>trigger_restart_framework</code>. This causes <code>init.rc</code> to start services in class <code>main</code> again and also start services in class <code>late_start</code> for the first time since boot.</p>
326
327 <li><strong>Start full framework</strong>
328
329<p>Now the framework boots all its services using the decrypted <code>/data</code> filesystem, and the system is ready for use.</p>
330</ol>
331
332<h3 id=failure>Failure</h3>
333
334<p>A device that fails to decrypt might be awry for a few reasons. The device
335starts with the normal series of steps to boot:</p>
336
337<ol>
338 <li>Detect encrypted device with a password
339 <li>Mount tmpfs
340 <li>Start framework to prompt for password
341</ol>
342
343<p>But after the framework opens, the device can encounter some errors:</p>
344
345<ul>
346 <li>Password matches but cannot decrypt data
347 <li>User enters wrong password 30 times
348</ul>
349
350<p>If these errors are not resolved, <strong>prompt user to factory wipe</strong>:</p>
351
352<p>If <code>vold</code> detects an error during the encryption process, and if no data has been
353destroyed yet and the framework is up, <code>vold</code> sets the property <code>vold.encrypt_progress </code>to <code>error_not_encrypted</code>. The UI prompts the user to reboot and alerts them the encryption process
354never started. If the error occurs after the framework has been torn down, but
355before the progress bar UI is up, <code>vold</code> will reboot the system. If the reboot fails, it sets <code>vold.encrypt_progress</code> to <code>error_shutting_down</code> and returns -1; but there will not be anything to catch the error. This is not
356expected to happen.</p>
357
358<p>If <code>vold</code> detects an error during the encryption process, it sets <code>vold.encrypt_progress</code> to <code>error_partially_encrypted</code> and returns -1. The UI should then display a message saying the encryption
359failed and provide a button for the user to factory reset the device. </p>
360
361<h2 id=storing_the_encrypted_key>Storing the encrypted key</h2>
362
363<p>The encrypted key is stored in the crypto metadata. Hardware backing is implemented by using Trusted Execution Environment’s (TEE) signing capability.
364Previously, we encrypted the master key with a key generated by applying scrypt to the user's password and the stored salt. In order to make the key resilient
365against off-box attacks, we extend this algorithm by signing the resultant key with a stored TEE key. The resultant signature is then turned into an appropriate length key by one more application of scrypt. This key is then used to encrypt and decrypt the master key. To store this key:</p>
366
367<ol>
368 <li>Generate random 16-byte disk encryption key (DEK) and 16-byte salt.
Clay Murphy0878fe82014-11-04 16:04:37 -0800369 <li>Apply scrypt to the user password and the salt to produce 32-byte intermediate
Clay Murphy5e411e62014-10-14 12:44:54 -0700370key 1 (IK1).
371 <li>Pad IK1 with zero bytes to the size of the hardware-bound private key (HBK).
372Specifically, we pad as: 00 || IK1 || 00..00; one zero byte, 32 IK1 bytes, 223
373zero bytes.
374 <li>Sign padded IK1 with HBK to produce 256-byte IK2.
Clay Murphy0878fe82014-11-04 16:04:37 -0800375 <li>Apply scrypt to IK2 and salt (same salt as step 2) to produce 32-byte IK3.
Clay Murphy5e411e62014-10-14 12:44:54 -0700376 <li>Use the first 16 bytes of IK3 as KEK and the last 16 bytes as IV.
377 <li>Encrypt DEK with AES_CBC, with key KEK, and initialization vector IV.
378</ol>
379
380<h2 id=changing_the_password>Changing the password</h2>
381
382<p>When a user elects to change or remove their password in settings, the UI sends
383the command <code>cryptfs changepw</code> to <code>vold</code>, and <code>vold</code> re-encrypts the disk master key with the new password.</p>
384
385<h2 id=encryption_properties>Encryption properties</h2>
386
387<p><code>vold</code> and <code>init</code> communicate with each other by setting properties. Here is a list of available
388properties for encryption.</p>
389
390<h3 id=vold_properties>Vold properties </h3>
391
392<table>
393 <tr>
394 <th>Property</th>
395 <th>Description</th>
396 </tr>
397 <tr>
398 <td><code>vold.decrypt trigger_encryption</code></td>
399 <td>Encrypt the drive with no
400 password.</td>
401 </tr>
402 <tr>
403 <td><code>vold.decrypt trigger_default_encryption</code></td>
404 <td>Check the drive to see if it is encrypted with no password.
405If it is, decrypt and mount it,
406else set <code>vold.decrypt</code> to trigger_restart_min_framework.</td>
407 </tr>
408 <tr>
409 <td><code>vold.decrypt trigger_reset_main</code></td>
410 <td>Set by vold to shutdown the UI asking for the disk password.</td>
411 </tr>
412 <tr>
413 <td><code>vold.decrypt trigger_post_fs_data</code></td>
414 <td> Set by vold to prep /data with necessary directories, et al.</td>
415 </tr>
416 <tr>
417 <td><code>vold.decrypt trigger_restart_framework</code></td>
418 <td>Set by vold to start the real framework and all services.</td>
419 </tr>
420 <tr>
421 <td><code>vold.decrypt trigger_shutdown_framework</code></td>
422 <td>Set by vold to shutdown the full framework to start encryption.</td>
423 </tr>
424 <tr>
425 <td><code>vold.decrypt trigger_restart_min_framework</code></td>
426 <td>Set by vold to start the
427progress bar UI for encryption or
428prompt for password, depending on
429the value of <code>ro.crypto.state</code>.</td>
430 </tr>
431 <tr>
432 <td><code>vold.encrypt_progress</code></td>
433 <td>When the framework starts up,
434if this property is set, enter
435the progress bar UI mode.</td>
436 </tr>
437 <tr>
438 <td><code>vold.encrypt_progress 0 to 100</code></td>
439 <td>The progress bar UI should
440display the percentage value set.</td>
441 </tr>
442 <tr>
443 <td><code>vold.encrypt_progress error_partially_encrypted</code></td>
444 <td>The progress bar UI should display a message that the encryption failed, and
445give the user an option to
446factory reset the device.</td>
447 </tr>
448 <tr>
449 <td><code>vold.encrypt_progress error_reboot_failed</code></td>
450 <td>The progress bar UI should
451display a message saying encryption completed, and give the user a button to reboot the device. This error is not expected to happen.</td>
452 </tr>
453 <tr>
454 <td><code>vold.encrypt_progress error_not_encrypted</code></td>
455 <td>The progress bar UI should
456display a message saying an error
457occured, no data was encrypted or
458lost, and give the user a button to reboot the system.</td>
459 </tr>
460 <tr>
461 <td><code>vold.encrypt_progress error_shutting_down</code></td>
462 <td>The progress bar UI is not running, so it is unclear who will respond to this error. And it should never happen anyway.</td>
463 </tr>
464 <tr>
465 <td><code>vold.post_fs_data_done 0</code></td>
466 <td>Set by <code>vold</code> just before setting <code>vold.decrypt</code> to <code>trigger_post_fs_data</code>.</td>
467 </tr>
468 <tr>
469 <td><code>vold.post_fs_data_done 1</code></td>
470 <td>Set by <code>init.rc</code> or
471 <code>init.rc</code> just after finishing the task <code>post-fs-data</code>.</td>
472 </tr>
473</table>
474<h3 id=init_properties>init properties</h3>
475
476<table>
477 <tr>
478 <th>Property</th>
479 <th>Description</th>
480 </tr>
481 <tr>
482 <td><code>ro.crypto.fs_crypto_blkdev</code></td>
483 <td>Set by the <code>vold</code> command <code>checkpw</code> for later use by the <code>vold</code> command <code>restart</code>.</td>
484 </tr>
485 <tr>
486 <td><code>ro.crypto.state unencrypted</code></td>
487 <td>Set by <code>init</code> to say this system is running with an unencrypted
488 <code>/data ro.crypto.state encrypted</code>. Set by <code>init</code> to say this system is running with an encrypted <code>/data</code>.</td>
489 </tr>
490 <tr>
491 <td><p><code>ro.crypto.fs_type<br>
492 ro.crypto.fs_real_blkdev <br>
493 ro.crypto.fs_mnt_point<br>
494 ro.crypto.fs_options<br>
495 ro.crypto.fs_flags <br>
496 </code></p></td>
497 <td> These five properties are set by
498 <code>init</code> when it tries to mount <code>/data</code> with parameters passed in from
499 <code>init.rc</code>. <code>vold</code> uses these to setup the crypto mapping.</td>
500 </tr>
501 <tr>
502 <td><code>ro.crypto.tmpfs_options</code></td>
503 <td>Set by <code>init.rc</code> with the options init should use when mounting the tmpfs /data filesystem.</td>
504 </tr>
505</table>
506<h2 id=init_actions>Init actions</h2>
507
508<pre>
509on post-fs-data
Clay Murphy32285dd2014-03-12 12:15:00 -0700510on nonencrypted
511on property:vold.decrypt=trigger_reset_main
512on property:vold.decrypt=trigger_post_fs_data
513on property:vold.decrypt=trigger_restart_min_framework
514on property:vold.decrypt=trigger_restart_framework
515on property:vold.decrypt=trigger_shutdown_framework
Paul Lawrence707f7ef2014-05-20 11:00:23 -0700516on property:vold.decrypt=trigger_encryption
517on property:vold.decrypt=trigger_default_encryption
Clay Murphy5e411e62014-10-14 12:44:54 -0700518</pre>