Danielle Roberts | a136b01 | 2016-07-13 19:48:03 -0700 | [diff] [blame^] | 1 | page.title=Full-Disk Encryption |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
| 5 | Copyright 2014 The Android Open Source Project |
| 6 | |
| 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 | --> |
| 19 | |
| 20 | <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 | <p>Full-disk encryption is the process of encoding all user data on an Android device using an |
| 29 | encrypted key. Once a device is encrypted, all user-created data is |
| 30 | automatically encrypted before committing it to disk and all reads |
| 31 | automatically decrypt data before returning it to the calling process.</p> |
| 32 | |
| 33 | <p> |
| 34 | Full-disk encryption was introduced to Android in 4.4, but Android 5.0 introduced |
| 35 | these new features:</p> |
| 36 | <ul> |
| 37 | <li>Created fast encryption, which only encrypts used blocks on the data partition |
| 38 | to avoid first boot taking a long time. Only ext4 and f2fs filesystems |
| 39 | currently support fast encryption. |
| 40 | <li>Added the <a href="{@docRoot}devices/storage/config.html"><code>forceencrypt</code> |
| 41 | fstab flag</a> to encrypt on first boot. |
| 42 | <li>Added support for patterns and encryption without a password. |
| 43 | <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. |
| 47 | </ul> |
| 48 | |
| 49 | <p class="caution"><strong>Caution:</strong> Devices upgraded to Android 5.0 and then |
| 50 | encrypted may be returned to an unencrypted state by factory data reset. New Android 5.0 |
| 51 | devices encrypted at first boot cannot be returned to an unencrypted state.</p> |
| 52 | |
| 53 | <h2 id=how_android_encryption_works>How Android full-disk encryption works</h2> |
| 54 | |
| 55 | <p>Android full-disk encryption is based on <code>dm-crypt</code>, which is a kernel |
| 56 | feature that works at the block device layer. Because of |
| 57 | this, encryption works with Embedded MultiMediaCard<strong> (</strong>eMMC) and |
| 58 | similar flash devices that present themselves to the kernel as block |
| 59 | devices. Encryption is not possible with YAFFS, which talks directly to a raw |
| 60 | NAND flash chip. </p> |
| 61 | |
| 62 | <p>The encryption algorithm is 128 Advanced Encryption Standard (AES) with |
| 63 | cipher-block chaining (CBC) and ESSIV:SHA256. The master key is encrypted with |
| 64 | 128-bit AES via calls to the OpenSSL library. You must use 128 bits or more for |
| 65 | the 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 | |
| 69 | <p>In the Android 5.0 release, there are four kinds of encryption states: </p> |
| 70 | |
| 71 | <ul> |
| 72 | <li>default |
| 73 | <li>PIN |
| 74 | <li>password |
| 75 | <li>pattern |
| 76 | </ul> |
| 77 | |
| 78 | <p>Upon first boot, the device creates a randomly generated 128-bit master key |
| 79 | and then hashes it with a default password and stored salt. The default password is: "default_password" |
| 80 | However, the resultant hash is also signed through a TEE (such as TrustZone), |
| 81 | which 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 |
| 84 | href="https://android.googlesource.com/platform/system/vold/+/master/cryptfs.c">cryptfs.c</a> |
| 85 | file.</p> |
| 86 | |
| 87 | <p>When the user sets the PIN/pass or password on the device, only the 128-bit key |
| 88 | is re-encrypted and stored. (ie. user PIN/pass/pattern changes do NOT cause |
| 89 | re-encryption of userdata.) Note that |
| 90 | <a href="http://developer.android.com/guide/topics/admin/device-admin.html">managed device</a> |
| 91 | may be subject to PIN, pattern, or password restrictions.</p> |
| 92 | |
| 93 | <p>Encryption is managed by <code>init</code> and <code>vold</code>. |
| 94 | <code>init</code> calls <code>vold</code>, and vold sets properties to trigger |
| 95 | events in init. Other parts of the system |
| 96 | also look at the properties to conduct tasks such as report status, ask for a |
| 97 | password, or prompt to factory reset in the case of a fatal error. To invoke |
| 98 | encryption features in <code>vold</code>, the system uses the command line tool |
| 99 | <code>vdc</code>’s <code>cryptfs</code> commands: <code>checkpw</code>, |
| 100 | <code>restart</code>, <code>enablecrypto</code>, <code>changepw</code>, |
| 101 | <code>cryptocomplete</code>, <code>verifypw</code>, <code>setfield</code>, |
| 102 | <code>getfield</code>, <code>mountdefaultencrypted</code>, <code>getpwtype</code>, |
| 103 | <code>getpw</code>, and <code>clearpw</code>.</p> |
| 104 | |
| 105 | <p>In order to encrypt, decrypt or wipe <code>/data</code>, <code>/data</code> |
| 106 | must not be mounted. However, in order to show any user interface (UI), the |
| 107 | framework must start and the framework requires <code>/data</code> to run. To |
| 108 | resolve this conundrum, a temporary filesystem is mounted on <code>/data</code>. |
| 109 | This allows Android to prompt for passwords, show progress, or suggest a data |
| 110 | wipe as needed. It does impose the limitation that in order to switch from the |
| 111 | temporary filesystem to the true <code>/data</code> filesystem, the system must |
| 112 | stop every process with open files on the temporary filesystem and restart those |
| 113 | processes on the real <code>/data</code> filesystem. To do this, all services |
| 114 | must be in one of three groups: <code>core</code>, <code>main</code>, and |
| 115 | <code>late_start</code>.</p> |
| 116 | |
| 117 | <ul> |
| 118 | <li><code>core</code>: Never shut down after starting. |
| 119 | <li><code>main</code>: Shut down and then restart after the disk password is entered. |
| 120 | <li><code>late_start</code>: Does not start until after <code>/data</code> has been decrypted and mounted. |
| 121 | </ul> |
| 122 | |
| 123 | <p>To trigger these actions, the <code>vold.decrypt</code> property is set to |
| 124 | <a href="https://android.googlesource.com/platform/system/vold/+/master/cryptfs.c">various strings</a>. |
| 125 | To kill and restart services, the <code>init</code> commands are:</p> |
| 126 | |
| 127 | <ul> |
| 128 | <li><code>class_reset</code>: Stops a service but allows it to be restarted with class_start. |
| 129 | <li><code>class_start</code>: Restarts a service. |
| 130 | <li><code>class_stop</code>: Stops a service and adds a <code>SVC_DISABLED</code> flag. |
| 131 | Stopped services do not respond to <code>class_start</code>. |
| 132 | </ul> |
| 133 | |
| 134 | <h2 id=flows>Flows</h2> |
| 135 | |
| 136 | <p>There are four flows for an encrypted device. A device is encrypted just once |
| 137 | and then follows a normal boot flow. </p> |
| 138 | |
| 139 | <ul> |
| 140 | <li>Encrypt a previously unencrypted device: |
| 141 | <ul> |
| 142 | <li>Encrypt a new device with <code>forceencrypt</code>: Mandatory encryption |
| 143 | at first boot (starting in Android L). |
| 144 | <li>Encrypt an existing device: User-initiated encryption (Android K and earlier). |
| 145 | </ul> |
| 146 | <li>Boot an encrypted device: |
| 147 | <ul> |
| 148 | <li>Starting an encrypted device with no password: Booting an encrypted device that |
| 149 | has no set password (relevant for devices running Android 5.0 and later). |
| 150 | <li>Starting an encrypted device with a password: Booting an encrypted device that |
| 151 | has a set password. |
| 152 | </ul> |
| 153 | </ul> |
| 154 | |
| 155 | <p>In addition to these flows, the device can also fail to encrypt <code>/data</code>. |
| 156 | Each of the flows are explained in detail below.</p> |
| 157 | |
| 158 | |
| 159 | <h3 id=encrypt_a_new_device_with_forceencrypt>Encrypt a new device with forceencrypt</h3> |
| 160 | |
| 161 | <p>This is the normal first boot for an Android 5.0 device.</p> |
| 162 | |
| 163 | <ol> |
| 164 | <li><strong>Detect unencrypted filesystem with <code>forceencrypt</code> flag</strong> |
| 165 | |
| 166 | <p> |
| 167 | <code>/data</code> is not encrypted but needs to be because <code>forceencrypt</code> mandates it. |
| 168 | Unmount <code>/data</code>.</p> |
| 169 | |
| 170 | <li><strong>Start encrypting <code>/data</code></strong> |
| 171 | |
| 172 | <p><code>vold.decrypt = "trigger_encryption"</code> triggers <code>init.rc</code>, |
| 173 | which will cause <code>vold</code> to encrypt <code>/data</code> with no password. |
| 174 | (None is set because this should be a new device.)</p> |
| 175 | |
| 176 | |
| 177 | <li><strong>Mount tmpfs</strong> |
| 178 | |
| 179 | |
| 180 | <p><code>vold</code> mounts a tmpfs <code>/data</code> (using the tmpfs options from |
| 181 | <code>ro.crypto.tmpfs_options</code>) and sets the property <code>vold.encrypt_progress</code> to 0. |
| 182 | <code>vold</code> prepepares the tmpfs <code>/data</code> for booting an encrypted system and sets the |
| 183 | property <code>vold.decrypt</code> to: <code>trigger_restart_min_framework</code> |
| 184 | </p> |
| 185 | |
| 186 | <li><strong>Bring up framework to show progress</strong> |
| 187 | |
| 188 | |
| 189 | <p>Because the device has virtually no data to encrypt, the progress bar will |
| 190 | often not actually appear because encryption happens so quickly. See |
| 191 | <a href="#encrypt_an_existing_device">Encrypt an existing device</a> for more |
| 192 | details about the progress UI.</p> |
| 193 | |
| 194 | <li><strong>When <code>/data</code> is encrypted, take down the framework</strong> |
| 195 | |
| 196 | <p><code>vold</code> sets <code>vold.decrypt</code> to |
| 197 | <code>trigger_default_encryption</code> which starts the |
| 198 | <code>defaultcrypto</code> service. (This starts the flow below for mounting a |
| 199 | default encrypted userdata.) <code>trigger_default_encryption</code> checks the |
| 200 | encryption type to see if <code>/data</code> is encrypted with or without a |
| 201 | password. Because Android 5.0 devices are encrypted on first boot, there should |
| 202 | be no password set; therefore we decrypt and mount <code>/data</code>.</p> |
| 203 | |
| 204 | <li><strong>Mount <code>/data</code></strong> |
| 205 | |
| 206 | <p><code>init</code> then mounts <code>/data</code> on a tmpfs RAMDisk using |
| 207 | parameters it picks up from <code>ro.crypto.tmpfs_options</code>, which is set |
| 208 | in <code>init.rc</code>.</p> |
| 209 | |
| 210 | <li><strong>Start framework</strong> |
| 211 | |
| 212 | <p>Set <code>vold</code> to <code>trigger_restart_framework</code>, which |
| 213 | continues the usual boot process.</p> |
| 214 | </ol> |
| 215 | |
| 216 | <h3 id=encrypt_an_existing_device>Encrypt an existing device</h3> |
| 217 | |
| 218 | <p>This is what happens when you encrypt an unencrypted Android K or earlier |
| 219 | device that has been migrated to L.</p> |
| 220 | |
| 221 | <p>This process is user-initiated and is referred to as “inplace encryption” in |
| 222 | the code. When a user selects to encrypt a device, the UI makes sure the |
| 223 | battery is fully charged and the AC adapter is plugged in so there is enough |
| 224 | power to finish the encryption process.</p> |
| 225 | |
| 226 | <p class="warning"><strong>Warning:</strong> If the device runs out of power and shuts down before it has finished |
| 227 | encrypting, file data is left in a partially encrypted state. The device must |
| 228 | be factory reset and all data is lost.</p> |
| 229 | |
| 230 | <p>To enable inplace encryption, <code>vold</code> starts a loop to read each |
| 231 | sector of the real block device and then write it |
| 232 | to the crypto block device. <code>vold</code> checks to see if a sector is in |
| 233 | use before reading and writing it, which makes |
| 234 | encryption much faster on a new device that has little to no data. </p> |
| 235 | |
| 236 | <p><strong>State of device</strong>: Set <code>ro.crypto.state = "unencrypted"</code> |
| 237 | and execute the <code>on nonencrypted</code> <code>init</code> trigger to continue booting.</p> |
| 238 | |
| 239 | <ol> |
| 240 | <li><strong>Check password</strong> |
| 241 | |
| 242 | <p>The UI calls <code>vold</code> with the command <code>cryptfs enablecrypto inplace</code> |
| 243 | where <code>passwd</code> is the user's lock screen password.</p> |
| 244 | |
| 245 | <li><strong>Take down the framework</strong> |
| 246 | |
| 247 | <p><code>vold</code> checks for errors, returns -1 if it can't encrypt, and |
| 248 | prints a reason in the log. If it can encrypt, it sets the property <code>vold.decrypt</code> |
| 249 | to <code>trigger_shutdown_framework</code>. This causes <code>init.rc</code> to |
| 250 | stop services in the classes <code>late_start</code> and <code>main</code>. </p> |
| 251 | |
| 252 | <li><strong>Create a crypto footer</strong></li> |
| 253 | <li><strong>Create a breadcrumb file</strong></li> |
| 254 | <li><strong>Reboot</strong></li> |
| 255 | <li><strong>Detect breadcrumb file</strong></li> |
| 256 | <li><strong>Start encrypting <code>/data</code></strong> |
| 257 | |
| 258 | <p><code>vold</code> then sets up the crypto mapping, which creates a virtual crypto block device |
| 259 | that maps onto the real block device but encrypts each sector as it is written, |
| 260 | and decrypts each sector as it is read. <code>vold</code> then creates and writes |
| 261 | out the crypto metadata.</p> |
| 262 | |
| 263 | <li><strong>While it’s encrypting, mount tmpfs</strong> |
| 264 | |
| 265 | <p><code>vold</code> mounts a tmpfs <code>/data</code> (using the tmpfs options |
| 266 | from <code>ro.crypto.tmpfs_options</code>) and sets the property |
| 267 | <code>vold.encrypt_progress</code> to 0. <code>vold</code> prepares the tmpfs |
| 268 | <code>/data</code> for booting an encrypted system and sets the property |
| 269 | <code>vold.decrypt</code> to: <code>trigger_restart_min_framework</code> </p> |
| 270 | |
| 271 | <li><strong>Bring up framework to show progress</strong> |
| 272 | |
| 273 | <p><code>trigger_restart_min_framework </code>causes <code>init.rc</code> to |
| 274 | start the <code>main</code> class of services. When the framework sees that |
| 275 | <code>vold.encrypt_progress</code> is set to 0, it brings up the progress bar |
| 276 | UI, which queries that property every five seconds and updates a progress bar. |
| 277 | The encryption loop updates <code>vold.encrypt_progress</code> every time it |
| 278 | encrypts another percent of the partition.</p> |
| 279 | |
| 280 | <li><strong>When<code> /data</code> is encrypted, update the crypto footer</strong> |
| 281 | |
| 282 | <p>When <code>/data</code> is successfully encrypted, <code>vold</code> clears |
| 283 | the flag <code>ENCRYPTION_IN_PROGRESS</code> in the metadata.</p> |
| 284 | |
| 285 | <p>When the device is successfully unlocked, the password is then used to |
| 286 | encrypt the master key and the crypto footer is updated.</p> |
| 287 | |
| 288 | <p> If the reboot fails for some reason, <code>vold</code> sets the property |
| 289 | <code>vold.encrypt_progress</code> to <code>error_reboot_failed</code> and |
| 290 | the UI should display a message asking the user to press a button to |
| 291 | reboot. This is not expected to ever occur.</p> |
| 292 | </ol> |
| 293 | |
| 294 | <h3 id=starting_an_encrypted_device_with_default_encryption> |
| 295 | Starting an encrypted device with default encryption</h3> |
| 296 | |
| 297 | <p>This is what happens when you boot up an encrypted device with no password. |
| 298 | Because Android 5.0 devices are encrypted on first boot, there should be no set |
| 299 | password and therefore this is the <em>default encryption</em> state.</p> |
| 300 | |
| 301 | <ol> |
| 302 | <li><strong>Detect encrypted <code>/data</code> with no password</strong> |
| 303 | |
| 304 | <p>Detect that the Android device is encrypted because <code>/data</code> |
| 305 | cannot be mounted and one of the flags <code>encryptable</code> or |
| 306 | <code>forceencrypt</code> is set.</p> |
| 307 | |
| 308 | <p><code>vold</code> sets <code>vold.decrypt</code> to |
| 309 | <code>trigger_default_encryption</code>, which starts the |
| 310 | <code>defaultcrypto</code> service. <code>trigger_default_encryption</code> |
| 311 | checks the encryption type to see if <code>/data</code> is encrypted with or |
| 312 | without a password. </p> |
| 313 | |
| 314 | <li><strong>Decrypt /data</strong> |
| 315 | |
| 316 | <p>Creates the <code>dm-crypt</code> device over the block device so the device |
| 317 | is ready for use.</p> |
| 318 | |
| 319 | <li><strong>Mount /data</strong> |
| 320 | |
| 321 | <p><code>vold</code> then mounts the decrypted real <code>/data</code> partition |
| 322 | and then prepares the new partition. It sets the property |
| 323 | <code>vold.post_fs_data_done</code> to 0 and then sets <code>vold.decrypt</code> |
| 324 | to <code>trigger_post_fs_data</code>. This causes <code>init.rc</code> to run |
| 325 | its <code>post-fs-data</code> commands. They will create any necessary directories |
| 326 | or links and then set <code>vold.post_fs_data_done</code> to 1.</p> |
| 327 | |
| 328 | <p>Once <code>vold</code> sees the 1 in that property, it sets the property |
| 329 | <code>vold.decrypt</code> to: <code>trigger_restart_framework.</code> This |
| 330 | causes <code>init.rc</code> to start services in class <code>main</code> |
| 331 | again and also start services in class <code>late_start</code> for the first |
| 332 | time since boot.</p> |
| 333 | |
| 334 | <li><strong>Start framework</strong> |
| 335 | |
| 336 | <p>Now the framework boots all its services using the decrypted <code>/data</code>, |
| 337 | and the system is ready for use.</p> |
| 338 | </ol> |
| 339 | |
| 340 | <h3 id=starting_an_encrypted_device_without_default_encryption> |
| 341 | Starting an encrypted device without default encryption</h3> |
| 342 | |
| 343 | <p>This is what happens when you boot up an encrypted device that has a set |
| 344 | password. The device’s password can be a pin, pattern, or password. </p> |
| 345 | |
| 346 | <ol> |
| 347 | <li><strong>Detect encrypted device with a password</strong> |
| 348 | |
| 349 | <p>Detect that the Android device is encrypted because the flag |
| 350 | <code>ro.crypto.state = "encrypted"</code></p> |
| 351 | |
| 352 | <p><code>vold</code> sets <code>vold.decrypt</code> to |
| 353 | <code>trigger_restart_min_framework</code> because <code>/data</code> is |
| 354 | encrypted with a password.</p> |
| 355 | |
| 356 | <li><strong>Mount tmpfs</strong> |
| 357 | |
| 358 | <p><code>init</code> sets five properties to save the initial mount options |
| 359 | given for <code>/data</code> with parameters passed from <code>init.rc</code>. |
| 360 | <code>vold</code> uses these properties to set up the crypto mapping:</p> |
| 361 | |
| 362 | <ol> |
| 363 | <li><code>ro.crypto.fs_type</code> |
| 364 | <li><code>ro.crypto.fs_real_blkdev</code> |
| 365 | <li><code>ro.crypto.fs_mnt_point</code> |
| 366 | <li><code>ro.crypto.fs_options</code> |
| 367 | <li><code>ro.crypto.fs_flags </code>(ASCII 8-digit hex number preceded by 0x) |
| 368 | </ol> |
| 369 | |
| 370 | <li><strong>Start framework to prompt for password</strong> |
| 371 | |
| 372 | <p>The framework starts up and sees that <code>vold.decrypt</code> is set to |
| 373 | <code>trigger_restart_min_framework</code>. This tells the framework that it is |
| 374 | booting on a tmpfs <code>/data</code> disk and it needs to get the user password.</p> |
| 375 | |
| 376 | <p>First, however, it needs to make sure that the disk was properly encrypted. It |
| 377 | sends the command <code>cryptfs cryptocomplete</code> to <code>vold</code>. |
| 378 | <code>vold</code> returns 0 if encryption was completed successfully, -1 on internal error, or |
| 379 | -2 if encryption was not completed successfully. <code>vold</code> determines |
| 380 | this by looking in the crypto metadata for the <code>CRYPTO_ENCRYPTION_IN_PROGRESS</code> |
| 381 | flag. If it's set, the encryption process was interrupted, and there is no |
| 382 | usable data on the device. If <code>vold</code> returns an error, the UI should |
| 383 | display a message to the user to reboot and factory reset the device, and give |
| 384 | the user a button to press to do so.</p> |
| 385 | |
| 386 | <li><strong>Decrypt data with password</strong> |
| 387 | |
| 388 | <p>Once <code>cryptfs cryptocomplete</code> is successful, the framework |
| 389 | displays a UI asking for the disk password. The UI checks the password by |
| 390 | sending the command <code>cryptfs checkpw</code> to <code>vold</code>. If the |
| 391 | password is correct (which is determined by successfully mounting the |
| 392 | decrypted <code>/data</code> at a temporary location, then unmounting it), |
| 393 | <code>vold</code> saves the name of the decrypted block device in the property |
| 394 | <code>ro.crypto.fs_crypto_blkdev</code> and returns status 0 to the UI. If the |
| 395 | password is incorrect, it returns -1 to the UI.</p> |
| 396 | |
| 397 | <li><strong>Stop framework</strong> |
| 398 | |
| 399 | <p>The UI puts up a crypto boot graphic and then calls <code>vold</code> with |
| 400 | the command <code>cryptfs restart</code>. <code>vold</code> sets the property |
| 401 | <code>vold.decrypt</code> to <code>trigger_reset_main</code>, which causes |
| 402 | <code>init.rc</code> to do <code>class_reset main</code>. This stops all services |
| 403 | in the main class, which allows the tmpfs <code>/data</code> to be unmounted. </p> |
| 404 | |
| 405 | <li><strong>Mount <code>/data</code></strong> |
| 406 | |
| 407 | <p><code>vold</code> then mounts the decrypted real <code>/data</code> partition |
| 408 | and prepares the new partition (which may never have been prepared if |
| 409 | it was encrypted with the wipe option, which is not supported on first |
| 410 | release). It sets the property <code>vold.post_fs_data_done</code> to 0 and then |
| 411 | sets <code>vold.decrypt</code> to <code>trigger_post_fs_data</code>. This causes |
| 412 | <code>init.rc</code> to run its <code>post-fs-data</code> commands. They will |
| 413 | create any necessary directories or links and then set |
| 414 | <code>vold.post_fs_data_done</code> to 1. Once <code>vold</code> sees the 1 in |
| 415 | that property, it sets the property <code>vold.decrypt</code> to |
| 416 | <code>trigger_restart_framework</code>. This causes <code>init.rc</code> to start |
| 417 | services in class <code>main</code> again and also start services in class |
| 418 | <code>late_start</code> for the first time since boot.</p> |
| 419 | |
| 420 | <li><strong>Start full framework</strong> |
| 421 | |
| 422 | <p>Now the framework boots all its services using the decrypted <code>/data</code> |
| 423 | filesystem, and the system is ready for use.</p> |
| 424 | </ol> |
| 425 | |
| 426 | <h3 id=failure>Failure</h3> |
| 427 | |
| 428 | <p>A device that fails to decrypt might be awry for a few reasons. The device |
| 429 | starts with the normal series of steps to boot:</p> |
| 430 | |
| 431 | <ol> |
| 432 | <li>Detect encrypted device with a password |
| 433 | <li>Mount tmpfs |
| 434 | <li>Start framework to prompt for password |
| 435 | </ol> |
| 436 | |
| 437 | <p>But after the framework opens, the device can encounter some errors:</p> |
| 438 | |
| 439 | <ul> |
| 440 | <li>Password matches but cannot decrypt data |
| 441 | <li>User enters wrong password 30 times |
| 442 | </ul> |
| 443 | |
| 444 | <p>If these errors are not resolved, <strong>prompt user to factory wipe</strong>:</p> |
| 445 | |
| 446 | <p>If <code>vold</code> detects an error during the encryption process, and if |
| 447 | no data has been destroyed yet and the framework is up, <code>vold</code> sets |
| 448 | the property <code>vold.encrypt_progress </code>to <code>error_not_encrypted</code>. |
| 449 | The UI prompts the user to reboot and alerts them the encryption process |
| 450 | never started. If the error occurs after the framework has been torn down, but |
| 451 | before the progress bar UI is up, <code>vold</code> will reboot the system. If |
| 452 | the reboot fails, it sets <code>vold.encrypt_progress</code> to |
| 453 | <code>error_shutting_down</code> and returns -1; but there will not be anything |
| 454 | to catch the error. This is not expected to happen.</p> |
| 455 | |
| 456 | <p>If <code>vold</code> detects an error during the encryption process, it sets |
| 457 | <code>vold.encrypt_progress</code> to <code>error_partially_encrypted</code> |
| 458 | and returns -1. The UI should then display a message saying the encryption |
| 459 | failed and provide a button for the user to factory reset the device. </p> |
| 460 | |
| 461 | <h2 id=storing_the_encrypted_key>Storing the encrypted key</h2> |
| 462 | |
| 463 | <p>The encrypted key is stored in the crypto metadata. Hardware backing is |
| 464 | implemented by using Trusted Execution Environment’s (TEE) signing capability. |
| 465 | Previously, we encrypted the master key with a key generated by applying scrypt |
| 466 | to the user's password and the stored salt. In order to make the key resilient |
| 467 | against off-box attacks, we extend this algorithm by signing the resultant key |
| 468 | with a stored TEE key. The resultant signature is then turned into an appropriate |
| 469 | length key by one more application of scrypt. This key is then used to encrypt |
| 470 | and decrypt the master key. To store this key:</p> |
| 471 | |
| 472 | <ol> |
| 473 | <li>Generate random 16-byte disk encryption key (DEK) and 16-byte salt. |
| 474 | <li>Apply scrypt to the user password and the salt to produce 32-byte intermediate |
| 475 | key 1 (IK1). |
| 476 | <li>Pad IK1 with zero bytes to the size of the hardware-bound private key (HBK). |
| 477 | Specifically, we pad as: 00 || IK1 || 00..00; one zero byte, 32 IK1 bytes, 223 |
| 478 | zero bytes. |
| 479 | <li>Sign padded IK1 with HBK to produce 256-byte IK2. |
| 480 | <li>Apply scrypt to IK2 and salt (same salt as step 2) to produce 32-byte IK3. |
| 481 | <li>Use the first 16 bytes of IK3 as KEK and the last 16 bytes as IV. |
| 482 | <li>Encrypt DEK with AES_CBC, with key KEK, and initialization vector IV. |
| 483 | </ol> |
| 484 | |
| 485 | <h2 id=changing_the_password>Changing the password</h2> |
| 486 | |
| 487 | <p>When a user elects to change or remove their password in settings, the UI sends |
| 488 | the command <code>cryptfs changepw</code> to <code>vold</code>, and |
| 489 | <code>vold</code> re-encrypts the disk master key with the new password.</p> |
| 490 | |
| 491 | <h2 id=encryption_properties>Encryption properties</h2> |
| 492 | |
| 493 | <p><code>vold</code> and <code>init</code> communicate with each other by |
| 494 | setting properties. Here is a list of available properties for encryption.</p> |
| 495 | |
| 496 | <h3 id=vold_properties>Vold properties</h3> |
| 497 | |
| 498 | <table> |
| 499 | <tr> |
| 500 | <th>Property</th> |
| 501 | <th>Description</th> |
| 502 | </tr> |
| 503 | <tr> |
| 504 | <td><code>vold.decrypt trigger_encryption</code></td> |
| 505 | <td>Encrypt the drive with no |
| 506 | password.</td> |
| 507 | </tr> |
| 508 | <tr> |
| 509 | <td><code>vold.decrypt trigger_default_encryption</code></td> |
| 510 | <td>Check the drive to see if it is encrypted with no password. |
| 511 | If it is, decrypt and mount it, |
| 512 | else set <code>vold.decrypt</code> to trigger_restart_min_framework.</td> |
| 513 | </tr> |
| 514 | <tr> |
| 515 | <td><code>vold.decrypt trigger_reset_main</code></td> |
| 516 | <td>Set by vold to shutdown the UI asking for the disk password.</td> |
| 517 | </tr> |
| 518 | <tr> |
| 519 | <td><code>vold.decrypt trigger_post_fs_data</code></td> |
| 520 | <td> Set by vold to prep /data with necessary directories, et al.</td> |
| 521 | </tr> |
| 522 | <tr> |
| 523 | <td><code>vold.decrypt trigger_restart_framework</code></td> |
| 524 | <td>Set by vold to start the real framework and all services.</td> |
| 525 | </tr> |
| 526 | <tr> |
| 527 | <td><code>vold.decrypt trigger_shutdown_framework</code></td> |
| 528 | <td>Set by vold to shutdown the full framework to start encryption.</td> |
| 529 | </tr> |
| 530 | <tr> |
| 531 | <td><code>vold.decrypt trigger_restart_min_framework</code></td> |
| 532 | <td>Set by vold to start the |
| 533 | progress bar UI for encryption or |
| 534 | prompt for password, depending on |
| 535 | the value of <code>ro.crypto.state</code>.</td> |
| 536 | </tr> |
| 537 | <tr> |
| 538 | <td><code>vold.encrypt_progress</code></td> |
| 539 | <td>When the framework starts up, |
| 540 | if this property is set, enter |
| 541 | the progress bar UI mode.</td> |
| 542 | </tr> |
| 543 | <tr> |
| 544 | <td><code>vold.encrypt_progress 0 to 100</code></td> |
| 545 | <td>The progress bar UI should |
| 546 | display the percentage value set.</td> |
| 547 | </tr> |
| 548 | <tr> |
| 549 | <td><code>vold.encrypt_progress error_partially_encrypted</code></td> |
| 550 | <td>The progress bar UI should display a message that the encryption failed, and |
| 551 | give the user an option to |
| 552 | factory reset the device.</td> |
| 553 | </tr> |
| 554 | <tr> |
| 555 | <td><code>vold.encrypt_progress error_reboot_failed</code></td> |
| 556 | <td>The progress bar UI should display a message saying encryption |
| 557 | completed, and give the user a button to reboot the device. This error |
| 558 | is not expected to happen.</td> |
| 559 | </tr> |
| 560 | <tr> |
| 561 | <td><code>vold.encrypt_progress error_not_encrypted</code></td> |
| 562 | <td>The progress bar UI should |
| 563 | display a message saying an error |
| 564 | occurred, no data was encrypted or |
| 565 | lost, and give the user a button to reboot the system.</td> |
| 566 | </tr> |
| 567 | <tr> |
| 568 | <td><code>vold.encrypt_progress error_shutting_down</code></td> |
| 569 | <td>The progress bar UI is not running, so it is unclear who will respond |
| 570 | to this error. And it should never happen anyway.</td> |
| 571 | </tr> |
| 572 | <tr> |
| 573 | <td><code>vold.post_fs_data_done 0</code></td> |
| 574 | <td>Set by <code>vold</code> just before setting <code>vold.decrypt</code> |
| 575 | to <code>trigger_post_fs_data</code>.</td> |
| 576 | </tr> |
| 577 | <tr> |
| 578 | <td><code>vold.post_fs_data_done 1</code></td> |
| 579 | <td>Set by <code>init.rc</code> or |
| 580 | <code>init.rc</code> just after finishing the task <code>post-fs-data</code>.</td> |
| 581 | </tr> |
| 582 | </table> |
| 583 | <h3 id=init_properties>init properties</h3> |
| 584 | |
| 585 | <table> |
| 586 | <tr> |
| 587 | <th>Property</th> |
| 588 | <th>Description</th> |
| 589 | </tr> |
| 590 | <tr> |
| 591 | <td><code>ro.crypto.fs_crypto_blkdev</code></td> |
| 592 | <td>Set by the <code>vold</code> command <code>checkpw</code> for later use |
| 593 | by the <code>vold</code> command <code>restart</code>.</td> |
| 594 | </tr> |
| 595 | <tr> |
| 596 | <td><code>ro.crypto.state unencrypted</code></td> |
| 597 | <td>Set by <code>init</code> to say this system is running with an unencrypted |
| 598 | <code>/data ro.crypto.state encrypted</code>. Set by <code>init</code> to say |
| 599 | this system is running with an encrypted <code>/data</code>.</td> |
| 600 | </tr> |
| 601 | <tr> |
| 602 | <td><p><code>ro.crypto.fs_type<br> |
| 603 | ro.crypto.fs_real_blkdev <br> |
| 604 | ro.crypto.fs_mnt_point<br> |
| 605 | ro.crypto.fs_options<br> |
| 606 | ro.crypto.fs_flags <br> |
| 607 | </code></p></td> |
| 608 | <td> These five properties are set by |
| 609 | <code>init</code> when it tries to mount <code>/data</code> with parameters passed in from |
| 610 | <code>init.rc</code>. <code>vold</code> uses these to setup the crypto mapping.</td> |
| 611 | </tr> |
| 612 | <tr> |
| 613 | <td><code>ro.crypto.tmpfs_options</code></td> |
| 614 | <td>Set by <code>init.rc</code> with the options init should use when |
| 615 | mounting the tmpfs /data filesystem.</td> |
| 616 | </tr> |
| 617 | </table> |
| 618 | <h2 id=init_actions>Init actions</h2> |
| 619 | |
| 620 | <pre> |
| 621 | on post-fs-data |
| 622 | on nonencrypted |
| 623 | on property:vold.decrypt=trigger_reset_main |
| 624 | on property:vold.decrypt=trigger_post_fs_data |
| 625 | on property:vold.decrypt=trigger_restart_min_framework |
| 626 | on property:vold.decrypt=trigger_restart_framework |
| 627 | on property:vold.decrypt=trigger_shutdown_framework |
| 628 | on property:vold.decrypt=trigger_encryption |
| 629 | on property:vold.decrypt=trigger_default_encryption |
| 630 | </pre> |