1 page.title=File-Based Encryption 2 @jd:body 3 4 <!-- 5 Copyright 2016 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> 29 Android 7.0 and above supports file-based encryption (FBE). File-based 30 encryption allows different files to be encrypted with different keys that can 31 be unlocked independently. 32 </p> 33 <p> 34 This article describes how to enable file-based encryption on new devices 35 and how system applications can be updated to take full advantage of the new 36 Direct Boot APIs and offer users the best, most secure experience possible. 37 </p> 38 <h2 id="direct-boot">Direct Boot</h2> 39 <p> 40 File-based encryption enables a new feature introduced in Android 7.0 called <a 41 href="https://developer.android.com/preview/features/direct-boot.html">Direct 42 Boot</a>. Direct Boot allows encrypted devices to boot straight to the lock 43 screen. Previously, on encrypted devices using <a href="full-disk.html">full disk 44 encryption</a> (FDE), users needed to provided credentials before any data could 45 be accessed, preventing the phone from performing all but the most basic of 46 operations. For example, alarms could not operate, accessibility services were 47 unavailable, and phones could not receive calls but were limited to only basic 48 emergency dialer operations. 49 </p> 50 <p> 51 With the introduction of file-based encryption (FBE) and new APIs to make 52 applications aware of encryption, it is possible for these apps to operate 53 within a limited context. This can happen before users have provided their 54 credentials while still protecting private user information. 55 </p> 56 <p> 57 On an FBE-enabled device, each user of the device has two storage locations 58 available to applications: 59 </p><ul> 60 <li>Credential Encrypted (CE) storage, which is the default storage location and 61 only available after the user has unlocked the device. 62 <li>Device Encrypted (DE) storage, which is a storage location available both 63 during Direct Boot mode and after the user has unlocked the device.</li></ul> 64 <p> 65 This separation makes work profiles more secure because it allows more than one 66 user to be protected at a time as the encryption is no longer based solely on a 67 boot time password. 68 </p> 69 <p> 70 The Direct Boot API allows encryption-aware applications to access each of these 71 areas. There are changes to the application lifecycle to accommodate the need to 72 notify applications when a users CE storage is <em>unlocked</em> in response to 73 first entering credentials at the lock screen, or in the case of work profile 74 providing a <a 75 href="https://developer.android.com/preview/api-overview.html#android_for_work">work 76 challenge</a>. Devices running Android 7.0 must support these new APIs and 77 lifecycles regardless of whether or not they implement FBE. Although, without 78 FBE, DE and CE storage will always be in the unlocked state. 79 </p> 80 <p> 81 A complete implementation of file based encryption on an Ext4 file system is 82 provided in the Android Open Source Project (AOSP) and needs only be enabled on 83 devices that meet the requirements. Manufacturers electing to use FBE may wish 84 to explore ways of optimizing the feature based on the system on chip (SoC) 85 used. 86 </p> 87 <p> 88 All the necessary packages in AOSP have been updated to be direct-boot aware. 89 However, where device manufacturers use customized versions of these apps, they 90 will want to ensure at a minimum there are direct-boot aware packages providing 91 the following services: 92 </p> 93 94 <ul> 95 <li>Telephony Services and Dialer 96 <li>Input method for entering passwords into the lock screen 97 </ul> 98 99 <h2 id="examples-and-source">Examples and source</h2> 100 101 <p> 102 Android provides a reference implementation of file-based encryption, in which 103 vold (system/vold) provides the functionality for managing storage devices and 104 volumes on Android. The addition of FBE provides vold with several new commands 105 to support key management for the CE and DE keys of multiple users. In addition 106 to the core changes to use the <a href="#kernel-support">ext4 Encryption</a> 107 capabilities in kernel many system packages including the lockscreen and the 108 SystemUI have been modified to support the FBE and Direct Boot features. These 109 include: 110 </p> 111 112 <ul> 113 <li>AOSP Dialer (packages/apps/Dialer) 114 <li>Desk Clock (packages/apps/DeskClock) 115 <li>LatinIME (packages/inputmethods/LatinIME)* 116 <li>Settings App (packages/apps/Settings)* 117 <li>SystemUI (frameworks/base/packages/SystemUI)*</li></ul> 118 <p> 119 <em>* System applications that use the <code><a 120 href="#supporting-direct-boot-in-system-applications">defaultToDeviceProtectedStorage</a></code> 121 manifest attribute</em> 122 </p> 123 <p> 124 More examples of applications and services that are encryption aware can be 125 found by running the command <code>mangrep directBootAware</code> in the 126 frameworks or packages directory of the AOSP 127 source tree. 128 </p> 129 <h2 id="dependencies">Dependencies</h2> 130 <p> 131 To use the AOSP implementation of FBE securely, a device needs to meet the 132 following dependencies: 133 </p> 134 135 <ul> 136 <li><strong>Kernel Support</strong> for ext4 encryption (Kernel config option: 137 EXT4_FS_ENCRYPTION) 138 <li><strong><a 139 href="{@docRoot}security/keystore/index.html">Keymaster 140 Support</a></strong> with a HAL version 1.0 or 2.0. There is no support for 141 Keymaster 0.3 as that does not provide that necessary capabilities or assure 142 sufficient protection for encryption keys. 143 <li><strong>Keymaster/<a 144 href="{@docRoot}security/keystore/index.html">Keystore</a> and 145 Gatekeeper</strong> must be implemented in a <a 146 href="{@docRoot}security/trusty/index.html">Trusted Execution 147 Environment</a> (TEE) to provide protection for the DE keys so that an 148 unauthorized OS (custom OS flashed onto the device) cannot simply request the 149 DE keys. 150 <li><strong>Encryption performance</strong> in the kernel of at least 50MB/s 151 using AES XTS to ensure a good user experience. 152 <li><strong>Hardware Root of Trust</strong> and <strong>Verified Boot</strong> 153 bound to the keymaster initialisation is required to ensure that Device 154 Encryption credentials are not accessible by an unauthorized operating 155 system.</li> 156 </ul> 157 158 <p class="note"> 159 <strong>Note</strong>: Storage policies are applied to a folder and all of its 160 subfolders. Manufacturers should limit the contents that go unencrypted to the 161 OTA folder and the folder that holds the key that decrypts the system. Most 162 contents should reside in credential-encrypted storage rather than 163 device-encrypted storage. 164 </p> 165 166 <h2 id="implementation">Implementation</h2> 167 <p> 168 First and foremost, apps such as alarm clocks, phone, accessibility features 169 should be made android:directBootAware according to <a 170 href="https://developer.android.com/preview/features/direct-boot.html">Direct 171 Boot</a> developer documentation. 172 </p> 173 <h3 id="kernel-support">Kernel Support</h3> 174 <p> 175 The AOSP implementation of file-based encryption uses the ext4 encryption 176 features in the Linux 4.4 kernel. The recommended solution is to use a kernel 177 based on 4.4 or later. Ext4 encryption has also been backported to a 3.10 kernel 178 in the Android common repositories and for the supported Nexus kernels. 179 </p> 180 <p> 181 The android-3.10.y branch in the AOSP kernel/common git repository may 182 provide a good starting point for device manufacturers that want to import this 183 capability into their own device kernels. However, it is necessary to apply 184 the most recent patches from the latest stable Linux kernel (currently <a 185 href="https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/log/?id=refs/tags/v4.6">linux-4.6</a>) 186 of the ext4 and jbd2 projects. The Nexus device kernels already include many of 187 these patches. 188 </p> 189 <table> 190 <tr> 191 <th>Device</th> 192 <th>Kernel</th> 193 </tr> 194 <tr> 195 <td>Android Common 196 </td> 197 <td><strong>kernel/common</strong> android-3.10.y (<a 198 href="https://android.googlesource.com/kernel/common/+/android-3.10.y">git</a>) 199 </td> 200 </tr> 201 <tr> 202 <td>Nexus 5X (bullhead) 203 </td> 204 <td><strong>kernel/msm</strong> android-msm-bullhead-3.10-n-preview-2 (<a 205 href="https://android.googlesource.com/kernel/msm/+/android-msm-bullhead-3.10-n-preview-2">git</a>) 206 </td> 207 </tr> 208 <tr> 209 <td>Nexus 6P (angler) 210 </td> 211 <td><strong>kernel/msm</strong> android-msm-angler-3.10-n-preview-2 (<a 212 href="https://android.googlesource.com/kernel/msm/+/android-msm-angler-3.10-n-preview-2">git</a> 213 ) 214 </td> 215 </tr> 216 </table> 217 <p> 218 Note that each of these kernels uses a backport to 3.10. The ext4 219 and jbd2 drivers from linux 3.18 were transplanted into existing kernels based 220 on 3.10. Due to interdependencies between parts of the kernel, this backport 221 breaks support for a number of features that are not used by Nexus devices. 222 These include: 223 </p> 224 225 <ul> 226 <li>The ext3 driver, although ext4 can still mount and use ext3 filesystems 227 <li>Global File Sytem (GFS) Support 228 <li>ACL support in ext4</li> 229 </ul> 230 231 <p> 232 In addition to functional support for ext4 encryption, device manufacturers may 233 also consider implementing cryptographic acceleration to speed up file-based 234 encryption and improve the user experience. 235 </p> 236 <h3 id="enabling-file-based-encryption">Enabling file-based encryption</h3> 237 <p> 238 FBE is enabled by adding the flag <code>fileencryption</code> with no parameters 239 to the <code>fstab</code> line in the final column for the <code>userdata</code> 240 partition. You can see an example at: 241 <a href="https://android.googlesource.com/device/lge/bullhead/+/nougat-release/fstab_fbe.bullhead"> 242 https://android.googlesource.com/device/lge/bullhead/+/nougat-release/fstab_fbe.bullhead</a> 243 </p> 244 <p> 245 Whilst testing the FBE implementation on a device, it is possible to specify the 246 following flag: 247 <code>forcefdeorfbe="<path/to/metadata/partition>"</code> 248 </p> 249 <p> 250 This sets the device up with FDE but allows conversion to FBE for developers. By 251 default, this behaves like <code>forceencrypt</code>, putting the device into 252 FDE mode. However, it will expose a debug option allowing a device to be put 253 into FBE mode as is the case in the developer preview. It is also possible to 254 enable FBE from fastboot using this command: 255 </p> 256 <p> 257 <code>$ fastboot --wipe-and-use-fbe</code> 258 </p> 259 <p> 260 This is intended solely for development purposes as a platform for demonstrating 261 the feature before actual FBE devices are released. This flag may be deprecated 262 in the future. 263 </p> 264 <h3 id="integrating-with-keymaster">Integrating with Keymaster</h3> 265 <p> 266 The generation of keys and management of the kernel keyring is handled by 267 <code>vold</code>. The AOSP implementation of FBE requires that the device 268 support Keymaster HAL version 1.0 or later. There is no support for earlier 269 versions of the Keymaster HAL. 270 </p> 271 <p> 272 On first boot, user 0s keys are generated and installed early in the boot 273 process. By the time the <code>on-post-fs</code> phase of <code>init</code> 274 completes, the Keymaster must be ready to handle requests. On Nexus devices, 275 this is handled by having a script block: 276 </p> 277 278 <ul> 279 <li>Ensure Keymaster is started before <code>/data</code> is mounted 280 <li>Specify the file encryption cipher suite: AOSP implementation of file-based 281 encryption uses AES-256 in XTS mode 282 <p class="note"> 283 <strong>Note</strong>: All encryption is based on AES-256 in 284 XTS mode. Due to the way XTS is defined, it needs two 256-bit keys; so in 285 effect, both CE and DE keys are 512-bit keys.i 286 </p> 287 </li> 288 </ul> 289 290 <h3 id="encryption-policy">Encryption policy</h3> 291 <p> 292 Ext4 encryption applies the encryption policy at the directory level. When a 293 devices <code>userdata</code> partition is first created, the basic structures 294 and policies are applied by the <code>init</code> scripts. These scripts will 295 trigger the creation of the first users (user 0s) CE and DE keys as well as 296 define which directories are to be encrypted with these keys. When additional 297 users and profiles are created, the necessary additional keys are generated and 298 stored in the keystore; their credential and devices storage locations are 299 created and the encryption policy links these keys to those directories. 300 </p> 301 <p> 302 In the current AOSP implementation, the encryption policy is hardcoded into this 303 location: 304 </p> 305 <p> 306 <code>/system/extras/ext4_utils/ext4_crypt_init_extensions.cpp</code> 307 </p> 308 <p> 309 It is possible to add exceptions in this file to prevent certain directories 310 from being encrypted at all, by adding to the <code>directories_to_exclude</code> 311 list. If modifications of this sort are made then the device 312 manufacturer should include <a href="{@docRoot}security/selinux/device-policy.html"> 313 SELinux policies</a> that only grant access to the 314 applications that need to use the unencrypted directory. This should exclude all 315 untrusted applications. 316 </p> 317 <p> 318 The only known acceptable use case for this is in support of legacy OTA 319 capabilities. 320 </p> 321 <h3 id="supporting-direct-boot-in-system-applications"> 322 Supporting Direct Boot in system applications</h3> 323 324 <h4 id="making-applications-direct-boot-aware"> 325 Making applications Direct Boot aware</h4> 326 <p> 327 To facilitate rapid migration of system apps, there are two new attributes that 328 can be set at the application level. The 329 <code>defaultToDeviceProtectedStorage</code> attribute is available only to 330 system apps. The <code>directBootAware</code> attribute is available to all. 331 </p> 332 333 <pre> 334 <application 335 android:directBootAware="true" 336 android:defaultToDeviceProtectedStorage="true"> 337 </pre> 338 339 <p> 340 The <code>directBootAware</code> attribute at the application level is shorthand for marking 341 all components in the app as being encryption aware. 342 </p> 343 <p> 344 The <code>defaultToDeviceProtectedStorage</code> attribute redirects the default 345 app storage location to point at DE storage instead of pointing at CE storage. 346 System apps using this flag must carefully audit all data stored in the default 347 location, and change the paths of sensitive data to use CE storage. Device 348 manufactures using this option should carefully inspect the data that they are 349 storing to ensure that it contains no personal information. 350 </p> 351 <p> 352 When running in this mode, the following System APIs are 353 available to explicitly manage a Context backed by CE storage when needed, which 354 are equivalent to their Device Protected counterparts. 355 </p> 356 357 <ul> 358 <li><code>Context.createCredentialProtectedStorageContext()</code> 359 <li><code>Context.isCredentialProtectedStorage()</code></li> 360 </ul> 361 <h4 id="supporting-multiple-users">Supporting multiple users</h4> 362 <p> 363 Each user in a multi-user environment gets a separate encryption key. Every user 364 gets two keys: a DE and a CE key. User 0 must log into the device first as it is 365 a special user. This is pertinent for <a 366 href="{@docRoot}devices/tech/admin/index.html">Device 367 Administration</a> uses. 368 </p> 369 <p> 370 Crypto-aware applications interact across users in this manner: 371 <code>INTERACT_ACROSS_USERS</code> and <code>INTERACT_ACROSS_USERS_FULL</code> 372 allow an application to act across all the users on the device. However, those 373 apps will be able to access only CE-encrypted directories for users that are 374 already unlocked. 375 </p> 376 <p> 377 An application may be able to interact freely across the DE areas, but one user 378 unlocked does not mean that all the users on the device are unlocked. The 379 application should check this status before trying to access these areas. 380 </p> 381 <p> 382 Each work profile user ID also gets two keys: DE and CE. When the work challenge 383 is met, the profile user is unlocked and the Keymaster (in TEE) can provide the 384 profiles TEE key. 385 </p> 386 <h3 id="handling-updates">Handling updates</h3> 387 <p> 388 The recovery partition is unable to access the DE protected storage on the 389 userdata partition. Devices implementing FBE are strongly recommended to support 390 OTA using the upcoming A/B system updates. As the OTA can be applied during 391 normal operation there is no need for recovery to access data on the encrypted drive. 392 </p> 393 <p> 394 If a legacy OTA solution is used, which requires recovery to access the OTA file 395 on the userdata partition then: 396 </p> 397 398 <ul> 399 <li>Create a top level directory (for example misc_ne) in the userdata 400 partition. 401 <li>Add this top level directory to the encryption policy exception (see <a 402 href="#encryption-policy">Encryption policy</a> above). 403 <li>Create a directory with this to hold OTA packages. 404 <li>Add an SELinux rule and file contexts to control access to this folder and 405 it contents. Only the process or applications receiving OTA updates should be be 406 able to read and write to this folder. 407 <li>No other application or process should have access to this folder.</li> 408 </ul> 409 410 <p> 411 Within this folder create a directory to contain the OTA packages. 412 </p> 413 <h2 id="validation">Validation</h2> 414 <p> 415 To ensure the implemented version of the feature works as intended, employ the 416 many <a href="://android.googlesource.com/platform/cts/+/nougat-cts-release/hostsidetests/appsecurity/src/android/appsecurity/cts/DirectBootHostTest.java"> 417 CTS encryption tests</a>. 418 </p> 419 <p> 420 Once the kernel builds for your board, it should be tested by building an x86 421 kernel that can be tested using QEMU. This will allow the implementation to be 422 tested using 423 <a hre="https://git.kernel.org/cgit/fs/ext2/xfstests-bld.git/plain/quick-start?h=META"> 424 xfstest</a>. Test the crypto support using: 425 </p> 426 <pre> 427 $ kvm-xfstests -c encrypt -g auto 428 </pre> 429 <p> 430 In addition, device manufacturers may perform these manual tests. On a device 431 with FBE enabled: 432 </p> 433 434 <ul> 435 <li>Check that <code>ro.crypto.state</code> exists 436 <ul> 437 <li>Ensure <code>ro.crypto.state</code> is encrypted</li> 438 </ul> 439 </li> 440 <li>Check that <code>ro.crypto.type</code> exists 441 <ul> 442 <li>Ensure <code>ro.crypto.type</code> is set to <code>file</code></li> 443 </ul> 444 </li> 445 </ul> 446 447 <p> 448 Additionally, testers can boot a <code>userdebug</code> instance with a lockscreen set on the 449 primary user. Then <code>adb</code> shell into the device and use 450 <code>su</code> to become root. Make sure <code>/data/data</code> contains 451 encrypted filenames; if it does not, something is wrong. 452 </p> 453 <h2 id="aosp-implementation-details">AOSP implementation details</h2> 454 <p> 455 This section provides details on the AOSP implementation and describes how 456 file-based encryption works. It should not be necessary for device manufacturers 457 to make any changes here to use FBE and Direct Boot on their devices. 458 </p> 459 <h3 id="ext4-encryption">ext4 encryption</h3> 460 <p> 461 The AOSP implementation uses ext4 encryption in kernel and is configured to: 462 </p><ul> 463 <li>Encrypt file contents with AES-256 in XTS mode 464 <li>Encrypt file names with AES-256 in CBC-CTS mode</li></ul> 465 <h3 id="key-derivation">Key derivation</h3> 466 <p> 467 Disk encryption keys, which are 512-bit AES-XTS keys, are stored encrypted 468 by another key (a 256-bit AES-GCM key) held in the TEE. To use this TEE key, 469 three requirements must be met: 470 </p><ul> 471 <li>The auth token 472 <li>The stretched credential 473 <li>The secdiscardable hash</li></ul> 474 <p> 475 The <em>auth token</em> is a cryptographically authenticated token generated by 476 <a 477 href="{@docRoot}security/authentication/gatekeeper.html">Gatekeeper</a> 478 when a user successfully logs in. The TEE will refuse to use the key unless the 479 correct auth token is supplied. If the user has no credential, then no auth 480 token is used nor needed. 481 </p> 482 <p> 483 The <em>stretched credential</em> is the user credential after salting and 484 stretching with the <code>scrypt</code> algorithm. The credential is actually 485 hashed once in the lock settings service before being passed to 486 <code>vold</code> for passing to <code>scrypt</code>. This is cryptographically 487 bound to the key in the TEE with all the guarantees that apply to 488 <code>KM_TAG_APPLICATION_ID</code>. If the user has no credential, then no 489 stretched credential is used nor needed. 490 </p> 491 <p> 492 The <code>secdiscardable hash</code> is a 512-bit hash of a random 16 KB file 493 stored alongside other information used to reconstruct the key, such as the 494 seed. This file is securely deleted when the key is deleted, or it is encrypted 495 in a new way; this added protection ensures an attacker must recover every bit 496 of this securely deleted file to recover the key. This is cryptographically 497 bound to the key in the TEE with all the guarantees that apply to 498 <code>KM_TAG_APPLICATION_ID</code>. See the <a 499 href="{@docRoot}security/keystore/implementer-ref.html">Keystore 500 Implementer's Reference</a>. 501