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