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